提交 755f385a authored 作者: Michael Osthege's avatar Michael Osthege 提交者: Brandon T. Willard

Get rid of most global variables in configparser

上级 8e3e8399
...@@ -6,13 +6,15 @@ import pytest ...@@ -6,13 +6,15 @@ import pytest
from theano import configdefaults, configparser from theano import configdefaults, configparser
from theano.configdefaults import default_blas_ldflags from theano.configdefaults import default_blas_ldflags
from theano.configparser import THEANO_FLAGS_DICT, AddConfigVar, ConfigParam from theano.configparser import ConfigParam
def test_invalid_default(): def test_invalid_default():
# Ensure an invalid default value found in the Theano code only causes # Ensure an invalid default value found in the Theano code only causes
# a crash if it is not overridden by the user. # a crash if it is not overridden by the user.
root = configdefaults.config
def validate(val): def validate(val):
if val == "invalid": if val == "invalid":
raise ValueError("Test-triggered") raise ValueError("Test-triggered")
...@@ -20,17 +22,17 @@ def test_invalid_default(): ...@@ -20,17 +22,17 @@ def test_invalid_default():
with pytest.raises(ValueError, match="Test-triggered"): with pytest.raises(ValueError, match="Test-triggered"):
# This should raise a ValueError because the default value is # This should raise a ValueError because the default value is
# invalid. # invalid.
AddConfigVar( root.add(
"T_config__test_invalid_default_a", "T_config__test_invalid_default_a",
doc="unittest", doc="unittest",
configparam=ConfigParam("invalid", validate=validate), configparam=ConfigParam("invalid", validate=validate),
in_c_key=False, in_c_key=False,
) )
THEANO_FLAGS_DICT["T_config__test_invalid_default_b"] = "ok" root._flags_dict["T_config__test_invalid_default_b"] = "ok"
# This should succeed since we defined a proper value, even # This should succeed since we defined a proper value, even
# though the default was invalid. # though the default was invalid.
AddConfigVar( root.add(
"T_config__test_invalid_default_b", "T_config__test_invalid_default_b",
doc="unittest", doc="unittest",
configparam=ConfigParam("invalid", validate=validate), configparam=ConfigParam("invalid", validate=validate),
...@@ -39,7 +41,7 @@ def test_invalid_default(): ...@@ -39,7 +41,7 @@ def test_invalid_default():
# TODO We should remove these dummy options on test exit. # TODO We should remove these dummy options on test exit.
# Check that the flag has been removed # Check that the flag has been removed
assert "T_config__test_invalid_default_b" not in THEANO_FLAGS_DICT assert "T_config__test_invalid_default_b" not in root._flags_dict
@patch("theano.configdefaults.try_blas_flag", return_value=None) @patch("theano.configdefaults.try_blas_flag", return_value=None)
...@@ -84,20 +86,19 @@ def test_config_param_apply_and_validation(): ...@@ -84,20 +86,19 @@ def test_config_param_apply_and_validation():
def test_config_hash(): def test_config_hash():
# TODO: use custom config instance for the test # TODO: use custom config instance for the test
root = configparser.config root = configparser.config
configparser.AddConfigVar( root.add(
"test_config_hash", "test_config_hash",
"A config var from a test case.", "A config var from a test case.",
configparser.StrParam("test_default"), configparser.StrParam("test_default"),
root=root,
) )
h0 = configparser.get_config_hash() h0 = root.get_config_hash()
with configparser.change_flags(test_config_hash="new_value"): with configparser.change_flags(test_config_hash="new_value"):
assert root.test_config_hash == "new_value" assert root.test_config_hash == "new_value"
h1 = configparser.get_config_hash() h1 = root.get_config_hash()
h2 = configparser.get_config_hash() h2 = root.get_config_hash()
assert h1 != h0 assert h1 != h0
assert h2 == h0 assert h2 == h0
...@@ -141,11 +142,10 @@ class TestConfigTypes: ...@@ -141,11 +142,10 @@ class TestConfigTypes:
def test_config_context(): def test_config_context():
# TODO: use custom config instance for the test # TODO: use custom config instance for the test
root = configparser.config root = configparser.config
configparser.AddConfigVar( root.add(
"test_config_context", "test_config_context",
"A config var from a test case.", "A config var from a test case.",
configparser.StrParam("test_default"), configparser.StrParam("test_default"),
root=root,
) )
assert hasattr(root, "test_config_context") assert hasattr(root, "test_config_context")
assert root.test_config_context == "test_default" assert root.test_config_context == "test_default"
...@@ -156,8 +156,9 @@ def test_config_context(): ...@@ -156,8 +156,9 @@ def test_config_context():
def test_no_more_dotting(): def test_no_more_dotting():
root = configparser.config
with pytest.raises(ValueError, match="Dot-based"): with pytest.raises(ValueError, match="Dot-based"):
AddConfigVar( root.add(
"T_config.something", "T_config.something",
doc="unittest", doc="unittest",
configparam=ConfigParam("invalid"), configparam=ConfigParam("invalid"),
......
...@@ -12,8 +12,8 @@ import warnings ...@@ -12,8 +12,8 @@ import warnings
import numpy as np import numpy as np
import theano import theano
import theano.configparser
from theano.configparser import ( from theano.configparser import (
THEANO_FLAGS_DICT,
AddConfigVar, AddConfigVar,
BoolParam, BoolParam,
ConfigParam, ConfigParam,
...@@ -23,7 +23,6 @@ from theano.configparser import ( ...@@ -23,7 +23,6 @@ from theano.configparser import (
FloatParam, FloatParam,
IntParam, IntParam,
StrParam, StrParam,
TheanoConfigParser,
) )
from theano.misc.windows import call_subprocess_Popen, output_subprocess_Popen from theano.misc.windows import call_subprocess_Popen, output_subprocess_Popen
from theano.utils import maybe_add_to_os_environ_pathlist from theano.utils import maybe_add_to_os_environ_pathlist
...@@ -2236,7 +2235,7 @@ SUPPORTED_DNN_CONV_PRECISION = ( ...@@ -2236,7 +2235,7 @@ SUPPORTED_DNN_CONV_PRECISION = (
# TODO: we should add the configs explicitly to an instance of the TheanoConfigParser # TODO: we should add the configs explicitly to an instance of the TheanoConfigParser
# Even if we treat it as a singleton, we should implement as if it wasn't, so we can # Even if we treat it as a singleton, we should implement as if it wasn't, so we can
# test it independently. # test it independently.
config = TheanoConfigParser() config = theano.configparser.config
add_basic_configvars() add_basic_configvars()
add_dnn_configvars() add_dnn_configvars()
add_magma_configvars() add_magma_configvars()
...@@ -2272,6 +2271,5 @@ except OSError: ...@@ -2272,6 +2271,5 @@ except OSError:
add_caching_dir_configvars() add_caching_dir_configvars()
# Check if there are remaining flags provided by the user through THEANO_FLAGS. # Check if there are remaining flags provided by the user through THEANO_FLAGS.
# TODO: the global variable THEANO_FLAGS_DICT should probably become an attribute on the `config` singleton for key in config._flags_dict.keys():
for key in THEANO_FLAGS_DICT.keys():
warnings.warn(f"Theano does not recognise this flag: {key}") warnings.warn(f"Theano does not recognise this flag: {key}")
差异被折叠。
...@@ -11,7 +11,6 @@ from io import StringIO ...@@ -11,7 +11,6 @@ from io import StringIO
import numpy as np import numpy as np
import theano
from theano import config from theano import config
from theano.gof import cmodule, graph, link, utils from theano.gof import cmodule, graph, link, utils
from theano.gof.callcache import CallCache from theano.gof.callcache import CallCache
...@@ -1472,7 +1471,7 @@ class CLinker(link.Linker): ...@@ -1472,7 +1471,7 @@ class CLinker(link.Linker):
# NOTE: config md5 is not using md5 hash, but sha256 instead. Function # NOTE: config md5 is not using md5 hash, but sha256 instead. Function
# string instances of md5 will be updated at a later release. # string instances of md5 will be updated at a later release.
if insert_config_hash: if insert_config_hash:
sig.append("md5:" + theano.configparser.get_config_hash()) sig.append("md5:" + config.get_config_hash())
else: else:
sig.append("md5: <omitted>") sig.append("md5: <omitted>")
......
...@@ -13,7 +13,7 @@ import warnings ...@@ -13,7 +13,7 @@ import warnings
from collections import defaultdict from collections import defaultdict
import theano.gof.cmodule import theano.gof.cmodule
from theano.configparser import _config_var_list, config from theano.configparser import config
from . import link from . import link
...@@ -715,9 +715,7 @@ except (OSError, theano.gof.cmodule.MissingGXX) as e: ...@@ -715,9 +715,7 @@ except (OSError, theano.gof.cmodule.MissingGXX) as e:
# already changed the default linker to something else then CVM. # already changed the default linker to something else then CVM.
# Currently this is the py linker. # Currently this is the py linker.
# Here we assert that the default linker is not cvm. # Here we assert that the default linker is not cvm.
assert not [x for x in _config_var_list if x.fullname == "linker"][ assert not config._config_var_dict["linker"].default.startswith("cvm"), e
0
].default.startswith("cvm"), e
class VM_Linker(link.LocalLinker): class VM_Linker(link.LocalLinker):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论