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