提交 526d5077 authored 作者: Frederic Bastien's avatar Frederic Bastien

new config system based on the execution of theano/config.py. Changed all call…

new config system based on the execution of theano/config.py. Changed all call to os.getenv to use this file and put the config from gof/utils.py in the new file.
上级 0f22419d
...@@ -11,6 +11,7 @@ from theano.gof import Env, graph, utils, link ...@@ -11,6 +11,7 @@ from theano.gof import Env, graph, utils, link
from theano.gof.link import WrapLinkerMany, raise_with_op from theano.gof.link import WrapLinkerMany, raise_with_op
#from theano.gof.cutils import run_cthunk #from theano.gof.cutils import run_cthunk
from theano.gof.cc import OpWiseCLinker, CLinker from theano.gof.cc import OpWiseCLinker, CLinker
import theano.config as config
from theano.compile.function_module import (FunctionMaker, from theano.compile.function_module import (FunctionMaker,
Function, Function,
infer_reuse_pattern, infer_reuse_pattern,
...@@ -1374,27 +1375,27 @@ class DebugMode(Mode): ...@@ -1374,27 +1375,27 @@ class DebugMode(Mode):
""" """
stability_patience = int(os.getenv('THEANO_DEBUGMODE_PATIENCE', 10)) stability_patience = config.THEANO_DEBUGMODE_PATIENCE
""" """
When checking for the stability of optimization, recompile the graph this many times. When checking for the stability of optimization, recompile the graph this many times.
""" """
check_c_code = bool(int(os.getenv('THEANO_DEBUGMODE_CHECK_C', 1))) check_c_code = config.THEANO_DEBUGMODE_CHECK_C
""" """
Should we evaluate (and check) the `c_code` implementations? Should we evaluate (and check) the `c_code` implementations?
""" """
check_py_code = bool(int(os.getenv('THEANO_DEBUGMODE_CHECK_PY', 1))) check_py_code = config.THEANO_DEBUGMODE_CHECK_PY
""" """
Should we evaluate (and check) the `perform` implementations? Should we evaluate (and check) the `perform` implementations?
""" """
check_isfinite = bool(int(os.getenv('THEANO_DEBUGMODE_CHECK_FINITE', 1))) check_isfinite = config.THEANO_DEBUGMODE_CHECK_FINITE
""" """
Should we check for (and complain about) NaN/Inf ndarray elements? Should we check for (and complain about) NaN/Inf ndarray elements?
""" """
require_matching_strides = bool(int(os.getenv('THEANO_DEBUGMODE_CHECK_STRIDES', 1))) require_matching_strides = config.THEANO_DEBUGMODE_CHECK_STRIDES
""" """
Should we check for (and complain about) Ops whose python and C outputs are ndarrays with Should we check for (and complain about) Ops whose python and C outputs are ndarrays with
different strides? (This can catch bugs, but is generally overly strict.) 0 no check, 1 warn, 2 err. different strides? (This can catch bugs, but is generally overly strict.) 0 no check, 1 warn, 2 err.
......
...@@ -5,6 +5,7 @@ import os, logging ...@@ -5,6 +5,7 @@ import os, logging
import numpy import numpy
import scipy.sparse as sp import scipy.sparse as sp
from theano import gof from theano import gof
import theano.config as config
_logger = logging.getLogger('theano.compile.mode') _logger = logging.getLogger('theano.compile.mode')
...@@ -211,7 +212,7 @@ predefined_modes = {'FAST_COMPILE': FAST_COMPILE, ...@@ -211,7 +212,7 @@ predefined_modes = {'FAST_COMPILE': FAST_COMPILE,
# is not set, it will default to 'FAST_RUN' # is not set, it will default to 'FAST_RUN'
# keep default_mode.optimizer==default_optimizer and default_mode.linker==default_linker! # keep default_mode.optimizer==default_optimizer and default_mode.linker==default_linker!
## ##
default_mode = os.getenv('THEANO_DEFAULT_MODE','FAST_RUN') default_mode = config.THEANO_DEFAULT_MODE
def get_default_mode(): def get_default_mode():
if not predefined_modes.has_key(default_mode): if not predefined_modes.has_key(default_mode):
......
...@@ -5,7 +5,7 @@ from theano.gof.cutils import run_cthunk ...@@ -5,7 +5,7 @@ from theano.gof.cutils import run_cthunk
from theano.compile.mode import Mode, register_mode, predefined_modes, predefined_linkers, predefined_optimizers, default_linker, default_optimizer from theano.compile.mode import Mode, register_mode, predefined_modes, predefined_linkers, predefined_optimizers, default_linker, default_optimizer
from theano.gof.cc import OpWiseCLinker from theano.gof.cc import OpWiseCLinker
from theano import gof from theano import gof
from theano.gof.utils import config import theano.config as config
class ProfileMode(Mode): class ProfileMode(Mode):
def __init__(self, linker=default_linker, optimizer=default_optimizer): def __init__(self, linker=default_linker, optimizer=default_optimizer):
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
""" """
import os, tempfile, StringIO, sys, logging, subprocess, cPickle, atexit, time, shutil, stat import os, tempfile, StringIO, sys, logging, subprocess, cPickle, atexit, time, shutil, stat
import distutils.sysconfig import distutils.sysconfig
import theano.config as config
import numpy.distutils #TODO: TensorType should handle this import numpy.distutils #TODO: TensorType should handle this
import compilelock # we will abuse the lockfile mechanism when reading and writing the registry import compilelock # we will abuse the lockfile mechanism when reading and writing the registry
...@@ -519,7 +520,7 @@ class ModuleCache(object): ...@@ -519,7 +520,7 @@ class ModuleCache(object):
def _rmtree(parent): def _rmtree(parent):
try: try:
if not os.getenv('THEANO_NOCLEANUP',0): if not config.THEANO_NOCLEANUP:
shutil.rmtree(parent) shutil.rmtree(parent)
except Exception, e: except Exception, e:
try: try:
......
...@@ -4,6 +4,8 @@ import os ...@@ -4,6 +4,8 @@ import os
import platform import platform
import re import re
import theano.config as config
def set_compiledir(path=None): def set_compiledir(path=None):
"""Set the directory into which theano will compile code objects """Set the directory into which theano will compile code objects
...@@ -25,15 +27,15 @@ def set_compiledir(path=None): ...@@ -25,15 +27,15 @@ def set_compiledir(path=None):
if path is None: if path is None:
# we need to set the default, which can come from one of two places # we need to set the default, which can come from one of two places
if os.getenv('THEANO_COMPILEDIR'): if config.THEANO_COMPILEDIR:
path = os.getenv('THEANO_COMPILEDIR') path = config.THEANO_COMPILEDIR
else: else:
platform_id = platform.platform() + '-' + platform.processor() platform_id = platform.platform() + '-' + platform.processor()
platform_id = re.sub("[\(\)\s]+", "_", platform_id) platform_id = re.sub("[\(\)\s]+", "_", platform_id)
if os.getenv('THEANO_BASE_COMPILEDIR'): if config.THEANO_BASE_COMPILEDIR:
base = os.getenv('THEANO_BASE_COMPILEDIR') base = config.THEANO_BASE_COMPILEDIR
else: else:
base = os.path.join(os.getenv('HOME'),'.theano') base = os.path.join(config.HOME,'.theano')
path = os.path.join(base, 'compiledir_'+platform_id) path = os.path.join(base, 'compiledir_'+platform_id)
if not os.access(path, os.R_OK | os.W_OK): if not os.access(path, os.R_OK | os.W_OK):
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
# import variable # import variable
import re, os import re, os
import ConfigParser
def hashgen(): def hashgen():
hashgen.next += 1 hashgen.next += 1
...@@ -325,115 +324,3 @@ def type_guard(type1): ...@@ -325,115 +324,3 @@ def type_guard(type1):
return new_f return new_f
return wrap return wrap
default_={
'ProfileMode.n_apply_to_print':15,
'ProfileMode.n_ops_to_print':20,
'tensor_opt.local_elemwise_fusion':False,
'lib.amdlibm':False,
}
class TheanoConfig(object):
"""Return the value for a key after parsing ~/.theano.cfg and
the THEANO_FLAGS environment variable.
We parse in that order the value to have:
1)the pair 'section.option':value in default_
2)The ~/.theano.cfg file
3)The value value provided in the get*() fct.
The last value found is the value returned.
The THEANO_FLAGS environement variable should be a list of comma-separated [section.]option[=value] entries. If the section part is omited, their should be only one section with that contain the gived option.
"""
def __init__(self):
d={} # no section
for k,v in default_.items():
if len(k.split('.'))==1:
d[k]=v
#set default value common for all section
self.config = ConfigParser.SafeConfigParser(d)
#set default value specific for each section
for k, v in default_.items():
sp = k.split('.',1)
if len(sp)==2:
if not self.config.has_section(sp[0]):
self.config.add_section(sp[0])
self.config.set(sp[0], sp[1], str(v))
#user config file override the default value
self.config.read(['theano.cfg', os.path.expanduser('~/.theano.cfg')])
self.env_flags=os.getenv("THEANO_FLAGS","")
#The value in the env variable THEANO_FLAGS override the previous value
for flag in self.env_flags.split(','):
if not flag:
continue
sp=flag.split('=',1)
if len(sp)==1:
val=True
else:
val=sp[1]
val=str(val)
sp=sp[0].split('.',1)#option or section.option
if len(sp)==2:
self.config.set(sp[0],sp[1],val)
else:
found=0
for sec in self.config.sections():
for opt in self.config.options(sec):
if opt == sp[0]:
found+=1
section=sec
option=opt
if found==1:
self.config.set(section,option,val)
elif found>1:
raise Exception("Ambiguous option (%s) in THEANO_FLAGS"%(sp[0]))
def __getitem__(self, key):
""":returns: a str with the value associated to the key"""
return self.get(key)
def get(self, key, val=None):
"""
:param key: the key that we want the value
:type key: str
:returns: a str with the value associated to the key
"""
#self.config.get(section, option, raw, vars)
if val is not None:
return val
sp = key.split('.',1)
if len(sp)!=2:
raise Exception("When we get a key, their must be a section and an option")
return self.config.get(sp[0],sp[1], False)
def getfloat(self, key, val=None):
""" :return: cast the output of self.get to a float"""
if val is not None:
return float(val)
return float(self.get(key))
def getboolean(self, key, val=None):
""" :return: cast the output of self.get to a boolean"""
if val is None:
val=self.get(key)
if val == "False" or val == "0" or not val:
val = False
else:
val = True
return val
def getint(self, key, val=None):
""" :return: cast the output of self.get to an int"""
if val is not None:
return int(val)
return int(self.get(key))
config = TheanoConfig()
...@@ -11,7 +11,8 @@ import cuda_ndarray ...@@ -11,7 +11,8 @@ import cuda_ndarray
import theano.compile.sandbox import theano.compile.sandbox
import logging, os import os
import theano.config as config
import logging, copy import logging, copy
_logger_name = 'theano_cuda_ndarray' _logger_name = 'theano_cuda_ndarray'
...@@ -26,11 +27,9 @@ def debug(*msg): ...@@ -26,11 +27,9 @@ def debug(*msg):
_logger.debug(_logger_name+'DEBUG: '+' '.join(str(m) for m in msg)) _logger.debug(_logger_name+'DEBUG: '+' '.join(str(m) for m in msg))
def use(device=None): def use(device=config.THEANO_GPU):
if use.device_number is None: if use.device_number is None:
# No successful call to use() has been made yet # No successful call to use() has been made yet
if device is None:
device = os.getenv("THEANO_GPU",0)
if device=="-1" or device=="CPU": if device=="-1" or device=="CPU":
return return
device=int(device) device=int(device)
......
import sys, os, subprocess, logging import sys, os, subprocess, logging
from theano.gof.cmodule import (std_libs, std_lib_dirs, std_include_dirs, dlimport, from theano.gof.cmodule import (std_libs, std_lib_dirs, std_include_dirs, dlimport,
get_lib_extension) get_lib_extension)
import theano.config as config
_logger=logging.getLogger("theano_cuda_ndarray.nvcc_compiler") _logger=logging.getLogger("theano_cuda_ndarray.nvcc_compiler")
_logger.setLevel(logging.WARN) _logger.setLevel(logging.WARN)
...@@ -34,7 +35,7 @@ def nvcc_module_compile_str(module_name, src_code, location=None, include_dirs=[ ...@@ -34,7 +35,7 @@ def nvcc_module_compile_str(module_name, src_code, location=None, include_dirs=[
preargs= [] if preargs is None else list(preargs) preargs= [] if preargs is None else list(preargs)
preargs.append('-fPIC') preargs.append('-fPIC')
no_opt = False no_opt = False
cuda_root = os.getenv('CUDA_ROOT') cuda_root = config.CUDA_ROOT
include_dirs = std_include_dirs() + include_dirs include_dirs = std_include_dirs() + include_dirs
libs = std_libs() + ['cudart'] + libs libs = std_libs() + ['cudart'] + libs
lib_dirs = std_lib_dirs() + lib_dirs lib_dirs = std_lib_dirs() + lib_dirs
......
...@@ -5,6 +5,7 @@ import numpy ...@@ -5,6 +5,7 @@ import numpy
from theano import Op, Type, Apply, Variable, Constant from theano import Op, Type, Apply, Variable, Constant
from theano import tensor from theano import tensor
import theano.config as config
import cuda_ndarray import cuda_ndarray
...@@ -236,14 +237,14 @@ class CudaNdarrayType(Type): ...@@ -236,14 +237,14 @@ class CudaNdarrayType(Type):
def c_header_dirs(self): def c_header_dirs(self):
"""Override `CLinkerOp.c_headers` """ """Override `CLinkerOp.c_headers` """
ret = [os.path.dirname(cuda_ndarray.__file__)] ret = [os.path.dirname(cuda_ndarray.__file__)]
cuda_root = os.getenv("CUDA_ROOT") cuda_root = config.CUDA_ROOT
if cuda_root: if cuda_root:
ret.append(os.path.join(cuda_root,'include')) ret.append(os.path.join(cuda_root,'include'))
return ret return ret
def c_lib_dirs(self): def c_lib_dirs(self):
ret = [os.path.dirname(cuda_ndarray.__file__)] ret = [os.path.dirname(cuda_ndarray.__file__)]
cuda_root = os.getenv("CUDA_ROOT") cuda_root = config.CUDA_ROOT
if cuda_root: if cuda_root:
ret.append(os.path.join(cuda_root,'lib')) ret.append(os.path.join(cuda_root,'lib'))
return ret return ret
......
...@@ -7,6 +7,7 @@ import numpy ...@@ -7,6 +7,7 @@ import numpy
from theano import gof from theano import gof
from theano.gof import Op, utils, Variable, Constant, Type, Apply, Env from theano.gof import Op, utils, Variable, Constant, Type, Apply, Env
from theano.gof.python25 import partial, all, any from theano.gof.python25 import partial, all, any
import theano.config as config
def upcast(dtype, *dtypes): def upcast(dtype, *dtypes):
z = numpy.zeros((), dtype = dtype) z = numpy.zeros((), dtype = dtype)
...@@ -74,18 +75,18 @@ class Scalar(Type): ...@@ -74,18 +75,18 @@ class Scalar(Type):
def c_headers(self): def c_headers(self):
l=['<math.h>'] l=['<math.h>']
if utils.config.getboolean('lib.amdlibm'): if config.config.getboolean('lib.amdlibm'):
l+=['<amdlibm.h>'] l+=['<amdlibm.h>']
return l return l
def c_libraries(self): def c_libraries(self):
l=[] l=[]
if utils.config.getboolean('lib.amdlibm'): if config.config.getboolean('lib.amdlibm'):
l+=['amdlibm'] l+=['amdlibm']
return l return l
def c_compile_args(self): def c_compile_args(self):
if utils.config.getboolean('lib.amdlibm'): if config.config.getboolean('lib.amdlibm'):
return ['-DREPLACE_WITH_AMDLIBM'] return ['-DREPLACE_WITH_AMDLIBM']
else: return [] else: return []
......
...@@ -4,7 +4,7 @@ __docformat__ = "restructuredtext en" ...@@ -4,7 +4,7 @@ __docformat__ = "restructuredtext en"
import __builtin__ import __builtin__
import sys # for sys.maxint import sys # for sys.maxint
import os # for getenv THEANO_CMP_SLOPPY import theano.config as config # for THEANO_CMP_SLOPPY
import traceback #for overriding Op.__call__ import traceback #for overriding Op.__call__
if sys.version_info >= (2,5): if sys.version_info >= (2,5):
import functools import functools
...@@ -217,7 +217,7 @@ def _wrap_tensor_into_member(x): ...@@ -217,7 +217,7 @@ def _wrap_tensor_into_member(x):
return compile.module.Member(constant(x)) return compile.module.Member(constant(x))
compile.module.register_wrapper(_obj_is_wrappable_as_tensor, _wrap_tensor_into_member) compile.module.register_wrapper(_obj_is_wrappable_as_tensor, _wrap_tensor_into_member)
if int(os.getenv('THEANO_CMP_SLOPPY', 0)): if int(config.THEANO_CMP_SLOPPY):
# This environment variable is a quick-and-dirty way to get low-precision comparisons. # This environment variable is a quick-and-dirty way to get low-precision comparisons.
# For a more precise setting of these tolerances set them explicitly in your user code by # For a more precise setting of these tolerances set them explicitly in your user code by
# assigning, for example, "theano.tensor.basic.float32_atol = ..." # assigning, for example, "theano.tensor.basic.float32_atol = ..."
......
"""Ops and optimizations for using BLAS function calls to evaluate linear algebra expressions""" """Ops and optimizations for using BLAS function calls to evaluate linear algebra expressions"""
import os, sys, traceback, logging import sys, traceback, logging
import numpy import numpy
import theano.config as config
from theano.gof import (utils, Op, Apply, view_roots, PatternSub, DestroyHandler, from theano.gof import (utils, Op, Apply, view_roots, PatternSub, DestroyHandler,
SeqOptimizer, local_optimizer, Optimizer, LocalOptimizer, OpKeyOptimizer, SeqOptimizer, local_optimizer, Optimizer, LocalOptimizer, OpKeyOptimizer,
InconsistencyError, toolbox) InconsistencyError, toolbox)
...@@ -33,8 +33,8 @@ def ldflags(libs=True, flags=False): ...@@ -33,8 +33,8 @@ def ldflags(libs=True, flags=False):
Default: ['blas'], but environment variable THEANO_BLAS_LDFLAGS overrides this. Default: ['blas'], but environment variable THEANO_BLAS_LDFLAGS overrides this.
""" """
rval = [] rval = []
if os.getenv('THEANO_BLAS_LDFLAGS'): if config.THEANO_BLAS_LDFLAGS:
tokens = os.getenv('THEANO_BLAS_LDFLAGS').split() tokens = config.THEANO_BLAS_LDFLAGS.split()
for t in tokens: for t in tokens:
try: try:
t0, t1, t2 = t[0:3] t0, t1, t2 = t[0:3]
......
...@@ -8,7 +8,8 @@ _logger = logging.getLogger('theano.tensor.opt') ...@@ -8,7 +8,8 @@ _logger = logging.getLogger('theano.tensor.opt')
from theano import gof from theano import gof
from theano.gof import opt, InconsistencyError, TopoOptimizer, graph from theano.gof import opt, InconsistencyError, TopoOptimizer, graph
from theano.gof.utils import MethodNotDefined, config from theano.gof.utils import MethodNotDefined
import theano.config as config
from elemwise import Elemwise, DimShuffle from elemwise import Elemwise, DimShuffle
from theano import scalar from theano import scalar
import basic as T import basic as T
...@@ -1368,7 +1369,7 @@ class FusionOptimizer(Optimizer): ...@@ -1368,7 +1369,7 @@ class FusionOptimizer(Optimizer):
pass pass
if config.getboolean('tensor_opt.local_elemwise_fusion'): if config.config.getboolean('tensor_opt.local_elemwise_fusion'):
_logger.debug("enabling optimization fusion elemwise in fast_run") _logger.debug("enabling optimization fusion elemwise in fast_run")
compile.optdb.register('elemwise_fusion', FusionOptimizer(), 71.00, 'fast_run', 'fusion', 'local_elemwise_fusion') compile.optdb.register('elemwise_fusion', FusionOptimizer(), 71.00, 'fast_run', 'fusion', 'local_elemwise_fusion')
else: else:
......
import unittest import unittest
import numpy import numpy
import theano.tensor as T import theano.tensor as T
import theano.config as config
import os, sys import os, sys
def fetch_seed(pseed=None): def fetch_seed(pseed=None):
...@@ -17,7 +17,7 @@ def fetch_seed(pseed=None): ...@@ -17,7 +17,7 @@ def fetch_seed(pseed=None):
>>> rng = numpy.random.RandomState(unittest_tools.fetch_seed()) >>> rng = numpy.random.RandomState(unittest_tools.fetch_seed())
""" """
seed = pseed or os.getenv("THEANO_UNITTEST_SEED", 666) seed = pseed or config.THEANO_UNITTEST_SEED
if seed=='random': if seed=='random':
seed = None seed = None
#backport #backport
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论