Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
755f385a
提交
755f385a
authored
12月 06, 2020
作者:
Michael Osthege
提交者:
Brandon T. Willard
12月 08, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Get rid of most global variables in configparser
上级
8e3e8399
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
222 行增加
和
223 行删除
+222
-223
test_config.py
tests/test_config.py
+14
-13
configdefaults.py
theano/configdefaults.py
+3
-5
configparser.py
theano/configparser.py
+202
-199
cc.py
theano/gof/cc.py
+1
-2
vm.py
theano/gof/vm.py
+2
-4
没有找到文件。
tests/test_config.py
浏览文件 @
755f385a
...
@@ -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"
),
...
...
theano/configdefaults.py
浏览文件 @
755f385a
...
@@ -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}"
)
theano/configparser.py
浏览文件 @
755f385a
...
@@ -20,64 +20,12 @@ class TheanoConfigWarning(Warning):
...
@@ -20,64 +20,12 @@ class TheanoConfigWarning(Warning):
warn
=
classmethod
(
warn
)
warn
=
classmethod
(
warn
)
def
parse_config_string
(
config_string
,
issue_warnings
=
True
):
class
_ChangeFlagsDecorator
:
"""
def
__init__
(
self
,
*
args
,
_root
=
None
,
**
kwargs
):
Parses a config string (comma-separated key=value components) into a dict.
# the old API supported passing a dict as the first argument:
"""
config_dict
=
{}
my_splitter
=
shlex
.
shlex
(
config_string
,
posix
=
True
)
my_splitter
.
whitespace
=
","
my_splitter
.
whitespace_split
=
True
for
kv_pair
in
my_splitter
:
kv_pair
=
kv_pair
.
strip
()
if
not
kv_pair
:
continue
kv_tuple
=
kv_pair
.
split
(
"="
,
1
)
if
len
(
kv_tuple
)
==
1
:
if
issue_warnings
:
TheanoConfigWarning
.
warn
(
f
"Config key '{kv_tuple[0]}' has no value, ignoring it"
,
stacklevel
=
1
,
)
else
:
k
,
v
=
kv_tuple
# subsequent values for k will override earlier ones
config_dict
[
k
]
=
v
return
config_dict
# THEANORC can contain a colon-delimited list of config files, like
# THEANORC=~lisa/.theanorc:~/.theanorc
# In that case, definitions in files on the right (here, ~/.theanorc) have
# precedence over those in files on the left.
def
config_files_from_theanorc
():
rval
=
[
os
.
path
.
expanduser
(
s
)
for
s
in
os
.
getenv
(
"THEANORC"
,
"~/.theanorc"
)
.
split
(
os
.
pathsep
)
]
if
os
.
getenv
(
"THEANORC"
)
is
None
and
sys
.
platform
==
"win32"
:
# to don't need to change the filename and make it open easily
rval
.
append
(
os
.
path
.
expanduser
(
"~/.theanorc.txt"
))
return
rval
class
change_flags
:
"""
Use this as a decorator or context manager to change the value of
Theano config variables.
Useful during tests.
"""
def
__init__
(
self
,
args
=
(),
**
kwargs
):
confs
=
dict
()
args
=
dict
(
args
)
args
=
dict
(
args
)
args
.
update
(
kwargs
)
args
.
update
(
kwargs
)
for
k
in
args
:
self
.
confs
=
{
k
:
_root
.
_config_var_dict
[
k
]
for
k
in
args
}
l
=
[
v
for
v
in
_config_var_list
if
v
.
fullname
==
k
]
assert
len
(
l
)
==
1
,
l
confs
[
k
]
=
l
[
0
]
self
.
confs
=
confs
self
.
new_vals
=
args
self
.
new_vals
=
args
def
__call__
(
self
,
f
):
def
__call__
(
self
,
f
):
...
@@ -105,7 +53,116 @@ class change_flags:
...
@@ -105,7 +53,116 @@ class change_flags:
v
.
__set__
(
None
,
self
.
old_vals
[
k
])
v
.
__set__
(
None
,
self
.
old_vals
[
k
])
def
fetch_val_for_key
(
key
,
delete_key
=
False
):
def
_hash_from_code
(
msg
):
"""This function was copied from theano.gof.utils to get rid of that import."""
# hashlib.sha256() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if
isinstance
(
msg
,
str
):
msg
=
msg
.
encode
()
# Python 3 does not like module names that start with
# a digit.
return
"m"
+
hashlib
.
sha256
(
msg
)
.
hexdigest
()
class
TheanoConfigParser
:
""" Object that holds configuration settings. """
def
__init__
(
self
,
flags_dict
:
dict
,
theano_cfg
,
theano_raw_cfg
):
self
.
_flags_dict
=
flags_dict
self
.
_theano_cfg
=
theano_cfg
self
.
_theano_raw_cfg
=
theano_raw_cfg
self
.
_config_var_dict
=
{}
super
()
.
__init__
()
def
__str__
(
self
,
print_doc
=
True
):
sio
=
StringIO
()
self
.
config_print
(
buf
=
sio
,
print_doc
=
print_doc
)
return
sio
.
getvalue
()
def
config_print
(
self
,
buf
,
print_doc
=
True
):
for
cv
in
self
.
_config_var_dict
.
values
():
print
(
cv
,
file
=
buf
)
if
print_doc
:
print
(
" Doc: "
,
cv
.
doc
,
file
=
buf
)
print
(
" Value: "
,
cv
.
__get__
(
True
,
None
),
file
=
buf
)
print
(
""
,
file
=
buf
)
def
get_config_hash
(
self
):
"""
Return a string sha256 of the current config options. In the past,
it was md5.
The string should be such that we can safely assume that two different
config setups will lead to two different strings.
We only take into account config options for which `in_c_key` is True.
"""
all_opts
=
sorted
(
[
c
for
c
in
self
.
_config_var_dict
.
values
()
if
c
.
in_c_key
],
key
=
lambda
cv
:
cv
.
fullname
,
)
return
_hash_from_code
(
"
\n
"
.
join
(
[
"{} = {}"
.
format
(
cv
.
fullname
,
cv
.
__get__
(
True
,
None
))
for
cv
in
all_opts
]
)
)
def
add
(
self
,
name
,
doc
,
configparam
,
in_c_key
=
True
):
"""Add a new variable to TheanoConfigParser.
This method performs some of the work of initializing `ConfigParam` instances.
Parameters
----------
name: string
The full name for this configuration variable. Takes the form
``"[section0__[section1__[etc]]]_option"``.
doc: string
A string that provides documentation for the config variable.
configparam: ConfigParam
An object for getting and setting this configuration parameter
in_c_key: boolean
If ``True``, then whenever this config option changes, the key
associated to compiled C modules also changes, i.e. it may trigger a
compilation of these modules (this compilation will only be partial if it
turns out that the generated C code is unchanged). Set this option to False
only if you are confident this option should not affect C code compilation.
"""
if
"."
in
name
:
raise
ValueError
(
f
"Dot-based sections were removed. Use double underscores! ({name})"
)
if
hasattr
(
self
,
name
):
raise
AttributeError
(
f
"The name {name} is already taken"
)
configparam
.
doc
=
doc
configparam
.
fullname
=
name
configparam
.
in_c_key
=
in_c_key
# Trigger a read of the value from config files and env vars
# This allow to filter wrong value from the user.
if
not
callable
(
configparam
.
default
):
configparam
.
__get__
(
self
,
type
(
self
),
delete_key
=
True
)
else
:
# We do not want to evaluate now the default value
# when it is a callable.
try
:
self
.
fetch_val_for_key
(
name
)
# The user provided a value, filter it now.
configparam
.
__get__
(
self
,
type
(
self
),
delete_key
=
True
)
except
KeyError
:
_logger
.
error
(
f
"Suppressed KeyError in AddConfigVar for parameter '{name}'!"
)
# the ConfigParam implements __get__/__set__, enabling us to create a property:
setattr
(
self
.
__class__
,
name
,
configparam
)
# keep the ConfigParam object in a dictionary:
self
.
_config_var_dict
[
name
]
=
configparam
def
fetch_val_for_key
(
self
,
key
,
delete_key
=
False
):
"""Return the overriding config value for a key.
"""Return the overriding config value for a key.
A successful search returns a string value.
A successful search returns a string value.
An unsuccessful search raises a KeyError
An unsuccessful search raises a KeyError
...
@@ -117,10 +174,10 @@ def fetch_val_for_key(key, delete_key=False):
...
@@ -117,10 +174,10 @@ def fetch_val_for_key(key, delete_key=False):
"""
"""
# first try to find it in the FLAGS
# first try to find it in the FLAGS
if
key
in
THEANO_FLAGS_DICT
:
if
key
in
self
.
_flags_dict
:
if
delete_key
:
if
delete_key
:
return
THEANO_FLAGS_DICT
.
pop
(
key
)
return
self
.
_flags_dict
.
pop
(
key
)
return
THEANO_FLAGS_DICT
[
key
]
return
self
.
_flags_dict
[
key
]
# next try to find it in the config file
# next try to find it in the config file
...
@@ -135,61 +192,20 @@ def fetch_val_for_key(key, delete_key=False):
...
@@ -135,61 +192,20 @@ def fetch_val_for_key(key, delete_key=False):
section
,
option
=
"global"
,
key
section
,
option
=
"global"
,
key
try
:
try
:
try
:
try
:
return
theano_cfg
.
get
(
section
,
option
)
return
self
.
_
theano_cfg
.
get
(
section
,
option
)
except
ConfigParser
.
InterpolationError
:
except
ConfigParser
.
InterpolationError
:
return
theano_raw_cfg
.
get
(
section
,
option
)
return
self
.
_
theano_raw_cfg
.
get
(
section
,
option
)
except
(
ConfigParser
.
NoOptionError
,
ConfigParser
.
NoSectionError
):
except
(
ConfigParser
.
NoOptionError
,
ConfigParser
.
NoSectionError
):
raise
KeyError
(
key
)
raise
KeyError
(
key
)
def
change_flags
(
self
,
*
args
,
**
kwargs
)
->
_ChangeFlagsDecorator
:
def
_config_print
(
thing
,
buf
,
print_doc
=
True
):
for
cv
in
_config_var_list
:
print
(
cv
,
file
=
buf
)
if
print_doc
:
print
(
" Doc: "
,
cv
.
doc
,
file
=
buf
)
print
(
" Value: "
,
cv
.
__get__
(
True
,
None
),
file
=
buf
)
print
(
""
,
file
=
buf
)
def
_hash_from_code
(
msg
):
"""This function was copied from theano.gof.utils to get rid of that import."""
# hashlib.sha256() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if
isinstance
(
msg
,
str
):
msg
=
msg
.
encode
()
# Python 3 does not like module names that start with
# a digit.
return
"m"
+
hashlib
.
sha256
(
msg
)
.
hexdigest
()
def
get_config_hash
():
"""
"""
Return a string sha256 of the current config options. In the past,
Use this as a decorator or context manager to change the value of
it was md5.
Theano config variables.
The string should be such that we can safely assume that two different
config setups will lead to two different strings.
We only take into account config options for which `in_c_key` is True
.
Useful during tests
.
"""
"""
all_opts
=
sorted
(
return
_ChangeFlagsDecorator
(
*
args
,
_root
=
self
,
**
kwargs
)
[
c
for
c
in
_config_var_list
if
c
.
in_c_key
],
key
=
lambda
cv
:
cv
.
fullname
)
return
_hash_from_code
(
"
\n
"
.
join
(
[
"{} = {}"
.
format
(
cv
.
fullname
,
cv
.
__get__
(
True
,
None
))
for
cv
in
all_opts
]
)
)
class
TheanoConfigParser
:
# properties are installed by AddConfigVar
_i_am_a_config_class
=
True
def
__str__
(
self
,
print_doc
=
True
):
sio
=
StringIO
()
_config_print
(
self
.
__class__
,
sio
,
print_doc
=
print_doc
)
return
sio
.
getvalue
()
class
ConfigParam
:
class
ConfigParam
:
...
@@ -198,6 +214,9 @@ class ConfigParam:
...
@@ -198,6 +214,9 @@ class ConfigParam:
A ConfigParam has not only default values and configurable mutability, but
A ConfigParam has not only default values and configurable mutability, but
also documentation text, as well as filtering and validation routines
also documentation text, as well as filtering and validation routines
that can be context-dependent.
that can be context-dependent.
This class implements __get__ and __set__ methods to eventually become
a property on an instance of TheanoConfigParser.
"""
"""
def
__init__
(
def
__init__
(
...
@@ -230,9 +249,10 @@ class ConfigParam:
...
@@ -230,9 +249,10 @@ class ConfigParam:
self
.
_validate
=
validate
self
.
_validate
=
validate
self
.
_mutable
=
mutable
self
.
_mutable
=
mutable
self
.
is_default
=
True
self
.
is_default
=
True
# set by
AddConfigVar
:
# set by
TheanoConfigParser.add
:
self
.
fullname
=
None
self
.
fullname
=
None
self
.
doc
=
None
self
.
doc
=
None
self
.
in_c_key
=
None
# Note that we do not call `self.filter` on the default value: this
# Note that we do not call `self.filter` on the default value: this
# will be done automatically in AddConfigVar, potentially with a
# will be done automatically in AddConfigVar, potentially with a
...
@@ -279,7 +299,7 @@ class ConfigParam:
...
@@ -279,7 +299,7 @@ class ConfigParam:
return
self
return
self
if
not
hasattr
(
self
,
"val"
):
if
not
hasattr
(
self
,
"val"
):
try
:
try
:
val_str
=
fetch_val_for_key
(
self
.
fullname
,
delete_key
=
delete_key
)
val_str
=
cls
.
fetch_val_for_key
(
self
.
fullname
,
delete_key
=
delete_key
)
self
.
is_default
=
False
self
.
is_default
=
False
except
KeyError
:
except
KeyError
:
if
callable
(
self
.
default
):
if
callable
(
self
.
default
):
...
@@ -421,16 +441,58 @@ class ContextsParam(ConfigParam):
...
@@ -421,16 +441,58 @@ class ContextsParam(ConfigParam):
return
val
return
val
# TODO: Not all of the following variables need to exist. Most should be private.
def
parse_config_string
(
config_string
,
issue_warnings
=
True
):
THEANO_FLAGS
=
os
.
getenv
(
"THEANO_FLAGS"
,
""
)
"""
# The THEANO_FLAGS environment variable should be a list of comma-separated
Parses a config string (comma-separated key=value components) into a dict.
# [section__]option=value entries. If the section part is omitted, there should
"""
# be only one section that contains the given option.
config_dict
=
{}
THEANO_FLAGS_DICT
=
parse_config_string
(
THEANO_FLAGS
,
issue_warnings
=
True
)
my_splitter
=
shlex
.
shlex
(
config_string
,
posix
=
True
)
_config_var_list
=
[]
my_splitter
.
whitespace
=
","
my_splitter
.
whitespace_split
=
True
for
kv_pair
in
my_splitter
:
kv_pair
=
kv_pair
.
strip
()
if
not
kv_pair
:
continue
kv_tuple
=
kv_pair
.
split
(
"="
,
1
)
if
len
(
kv_tuple
)
==
1
:
if
issue_warnings
:
TheanoConfigWarning
.
warn
(
f
"Config key '{kv_tuple[0]}' has no value, ignoring it"
,
stacklevel
=
1
,
)
else
:
k
,
v
=
kv_tuple
# subsequent values for k will override earlier ones
config_dict
[
k
]
=
v
return
config_dict
def
config_files_from_theanorc
():
"""
THEANORC can contain a colon-delimited list of config files, like
THEANORC=~lisa/.theanorc:~/.theanorc
In that case, definitions in files on the right (here, ~/.theanorc) have
precedence over those in files on the left.
"""
rval
=
[
os
.
path
.
expanduser
(
s
)
for
s
in
os
.
getenv
(
"THEANORC"
,
"~/.theanorc"
)
.
split
(
os
.
pathsep
)
]
if
os
.
getenv
(
"THEANORC"
)
is
None
and
sys
.
platform
==
"win32"
:
# to don't need to change the filename and make it open easily
rval
.
append
(
os
.
path
.
expanduser
(
"~/.theanorc.txt"
))
return
rval
def
_create_default_config
():
# The THEANO_FLAGS environment variable should be a list of comma-separated
# [section__]option=value entries. If the section part is omitted, there should
# be only one section that contains the given option.
THEANO_FLAGS
=
os
.
getenv
(
"THEANO_FLAGS"
,
""
)
THEANO_FLAGS_DICT
=
parse_config_string
(
THEANO_FLAGS
,
issue_warnings
=
True
)
config_files
=
config_files_from_theanorc
()
config_files
=
config_files_from_theanorc
()
theano_cfg
=
ConfigParser
.
ConfigParser
(
theano_cfg
=
ConfigParser
.
ConfigParser
(
{
{
"USER"
:
os
.
getenv
(
"USER"
,
os
.
path
.
split
(
os
.
path
.
expanduser
(
"~"
))[
-
1
]),
"USER"
:
os
.
getenv
(
"USER"
,
os
.
path
.
split
(
os
.
path
.
expanduser
(
"~"
))[
-
1
]),
"LSCRATCH"
:
os
.
getenv
(
"LSCRATCH"
,
""
),
"LSCRATCH"
:
os
.
getenv
(
"LSCRATCH"
,
""
),
...
@@ -439,85 +501,26 @@ theano_cfg = ConfigParser.ConfigParser(
...
@@ -439,85 +501,26 @@ theano_cfg = ConfigParser.ConfigParser(
"TMP"
:
os
.
getenv
(
"TMP"
,
""
),
"TMP"
:
os
.
getenv
(
"TMP"
,
""
),
"PID"
:
str
(
os
.
getpid
()),
"PID"
:
str
(
os
.
getpid
()),
}
}
)
theano_cfg
.
read
(
config_files
)
# Having a raw version of the config around as well enables us to pass
# through config values that contain format strings.
# The time required to parse the config twice is negligible.
theano_raw_cfg
=
ConfigParser
.
RawConfigParser
()
theano_raw_cfg
.
read
(
config_files
)
# N.B. all instances of TheanoConfigParser give access to the same properties.
config
=
TheanoConfigParser
()
def
AddConfigVar
(
name
,
doc
,
configparam
,
root
=
config
,
in_c_key
=
True
):
"""Add a new variable to `theano.config`.
The data structure at work here is a tree of classes with class
attributes/properties that are either a) instantiated dynamically-generated
classes, or b) `ConfigParam` instances. The root of this tree is the
`TheanoConfigParser` class, and the internal nodes are the ``SubObj``
classes created inside of `AddConfigVar`.
Why this design?
- The config object is a true singleton. Every instance of
`TheanoConfigParser` is an empty instance that looks up
attributes/properties in the [single] ``TheanoConfigParser.__dict__``
- The subtrees provide the same interface as the root
- `ConfigParser` subclasses control get/set of config properties to guard
against craziness.
This method also performs some of the work of initializing `ConfigParam`
instances
Parameters
----------
name: string
The full name for this configuration variable. Takes the form
``"[section0__[section1__[etc]]]_option"``.
doc: string
A string that provides documentation for the config variable.
configparam: ConfigParam
An object for getting and setting this configuration parameter
root: object
Used for recursive calls. Do not provide a value for this parameter.
in_c_key: boolean
If ``True``, then whenever this config option changes, the key
associated to compiled C modules also changes, i.e. it may trigger a
compilation of these modules (this compilation will only be partial if it
turns out that the generated C code is unchanged). Set this option to False
only if you are confident this option should not affect C code compilation.
"""
if
root
is
config
:
# Only set the name in the first call, not the recursive ones
configparam
.
fullname
=
name
if
"."
in
name
:
raise
ValueError
(
f
"Dot-based sections were removed. Use double underscores! ({name})"
)
)
if
hasattr
(
root
,
name
):
theano_cfg
.
read
(
config_files
)
raise
AttributeError
(
f
"The name {configparam.fullname} is already taken"
)
# Having a raw version of the config around as well enables us to pass
configparam
.
doc
=
doc
# through config values that contain format strings.
configparam
.
in_c_key
=
in_c_key
# The time required to parse the config twice is negligible.
# Trigger a read of the value from config files and env vars
theano_raw_cfg
=
ConfigParser
.
RawConfigParser
()
# This allow to filter wrong value from the user.
theano_raw_cfg
.
read
(
config_files
)
if
not
callable
(
configparam
.
default
):
configparam
.
__get__
(
root
,
type
(
root
),
delete_key
=
True
)
# Instances of TheanoConfigParser can have independent current values!
else
:
# But because the properties are assinged to the type, their existence is global.
# We do not want to evaluate now the default value
config
=
TheanoConfigParser
(
# when it is a callable.
flags_dict
=
THEANO_FLAGS_DICT
,
try
:
theano_cfg
=
theano_cfg
,
fetch_val_for_key
(
configparam
.
fullname
)
theano_raw_cfg
=
theano_raw_cfg
,
# The user provided a value, filter it now.
configparam
.
__get__
(
root
,
type
(
root
),
delete_key
=
True
)
except
KeyError
:
_logger
.
error
(
f
"Suppressed KeyError in AddConfigVar for parameter '{name}' with fullname '{configparam.fullname}'!"
)
)
setattr
(
root
.
__class__
,
name
,
configparam
)
return
config
# TODO: After assigning the configvar to the "root" object, there should be
# no reason to keep the _config_var_list around!
_config_var_list
.
append
(
configparam
)
config
=
_create_default_config
()
# aliasing for old API
AddConfigVar
=
config
.
add
change_flags
=
config
.
change_flags
_config_print
=
config
.
config_print
theano/gof/cc.py
浏览文件 @
755f385a
...
@@ -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>"
)
...
...
theano/gof/vm.py
浏览文件 @
755f385a
...
@@ -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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论