This repository has been archived by the owner on Jul 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from aigamedev/theano
Simple theano configuration via a `backend` pseudo-module.
- Loading branch information
Showing
8 changed files
with
146 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,55 @@ | ||
from __future__ import (absolute_import, unicode_literals) | ||
# -*- coding: utf-8 -*- | ||
from __future__ import (absolute_import, unicode_literals, print_function) | ||
|
||
__author__ = 'ssamot, alexjc' | ||
__version__ = '0.1' | ||
|
||
|
||
import os | ||
import sys | ||
import logging | ||
|
||
|
||
class TheanoConfigurator(object): | ||
|
||
def __init__(self): | ||
self.configured = False | ||
self.log = logging.getLogger('sknn') | ||
|
||
def configure(self, flags): | ||
if self.configured is True: | ||
return | ||
self.configured = True | ||
|
||
if 'theano' in sys.modules: | ||
self.log.warning('Theano was already imported and cannot be reconfigured.') | ||
return | ||
|
||
os.environ.setdefault('THEANO_FLAGS', flags+',print_active_device=False') | ||
cuda = logging.getLogger('theano.sandbox.cuda') | ||
cuda.setLevel(logging.CRITICAL) | ||
import theano | ||
cuda.setLevel(logging.WARNING) | ||
|
||
try: | ||
import theano.sandbox.cuda as cd | ||
self.log.info('Using device gpu%i: %s', cd.active_device_number(), cd.active_device_name()) | ||
except AttributeError: | ||
self.log.info('Using device cpu0, with %r.', theano.config.floatX) | ||
|
||
def __getattr__(self, name): | ||
flags = '' | ||
if name.endswith('32'): | ||
flags = ',floatX=float32' | ||
if name.endswith('64'): | ||
flags = ',floatX=float64' | ||
|
||
if name.startswith('cpu'): | ||
return self.configure('device=cpu'+flags) | ||
if name.startswith('gpu'): | ||
return self.configure('device=gpu'+flags) | ||
|
||
return getattr(sys.modules['sknn'], name) | ||
|
||
|
||
sys.modules['sknn.backend'] = TheanoConfigurator() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import unittest | ||
from nose.tools import (assert_in, assert_equal) | ||
|
||
import io | ||
import os | ||
import sys | ||
import logging | ||
|
||
import sknn | ||
|
||
|
||
class TestBackendPseudoModule(unittest.TestCase): | ||
|
||
def setUp(self): | ||
if 'THEANO_FLAGS' in os.environ: | ||
del os.environ['THEANO_FLAGS'] | ||
|
||
import theano | ||
|
||
self.removed = {} | ||
for name in list(sys.modules.keys()): | ||
if name.startswith('theano'): | ||
self.removed[name] = sys.modules[name] | ||
del sys.modules[name] | ||
sys.modules['sknn.backend'].configured = False | ||
|
||
self.buf = io.StringIO() | ||
self.hnd = logging.StreamHandler(self.buf) | ||
logging.getLogger('sknn').addHandler(self.hnd) | ||
logging.getLogger().setLevel(logging.WARNING) | ||
|
||
def tearDown(self): | ||
for name, module in self.removed.items(): | ||
sys.modules[name] = module | ||
logging.getLogger('sknn').removeHandler(self.hnd) | ||
|
||
def test_TheanoWarning(self): | ||
import theano | ||
from sknn.backend import cpu | ||
assert_equal('Theano was already imported and cannot be reconfigured.\n', | ||
self.buf.getvalue()) | ||
|
||
def _check(self, flags): | ||
assert_in('THEANO_FLAGS', os.environ) | ||
variable = os.environ['THEANO_FLAGS'] | ||
for f in flags: | ||
assert_in(f, variable) | ||
|
||
def test_FlagsGPU32(self): | ||
from sknn.backend import gpu32 | ||
self._check(['floatX=float32','device=gpu']) | ||
|
||
def test_FlagsCPU32(self): | ||
from sknn.backend import cpu32 | ||
self._check(['floatX=float32','device=cpu']) | ||
|
||
def test_FlagsGPU64(self): | ||
from sknn.backend import gpu64 | ||
self._check(['floatX=float64','device=gpu']) | ||
|
||
def test_FlagsCPU64(self): | ||
from sknn.backend import cpu64 | ||
self._check(['floatX=float64','device=cpu']) |