Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ef158c39
提交
ef158c39
authored
3月 27, 2013
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1301 from lamblin/unique_fct_names
Compute hash on whole source file
上级
54f89c9a
acc084bf
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
43 行增加
和
25 行删除
+43
-25
cc.py
theano/gof/cc.py
+14
-18
cmodule.py
theano/gof/cmodule.py
+29
-7
没有找到文件。
theano/gof/cc.py
浏览文件 @
ef158c39
...
@@ -4,7 +4,6 @@ Defines Linkers that deal with C implementations.
...
@@ -4,7 +4,6 @@ Defines Linkers that deal with C implementations.
# Python imports
# Python imports
from
copy
import
copy
from
copy
import
copy
import
re
# for set_compiledir
import
os
import
os
import
StringIO
import
StringIO
import
sys
import
sys
...
@@ -589,7 +588,14 @@ class CLinker(link.Linker):
...
@@ -589,7 +588,14 @@ class CLinker(link.Linker):
## ivnames + ovnames):
## ivnames + ovnames):
## sub[vname] = symbol[variable]
## sub[vname] = symbol[variable]
name
=
"node_
%
i"
%
node_num
# The placeholder will be replaced by a hash of the entire
# code (module + support code) in DynamicModule.code.
# This ensures that, when defining functions in support code,
# we cannot have two different functions, in different modules,
# that have the same name.
# It was problematic, in particular, on Mac OS X (10.6 and 10.7)
# when defining CUDA kernels (with Cuda 4.2 and 5.0). See gh-1172.
name
=
"node_<<<<HASH_PLACEHOLDER>>>>_
%
i"
%
node_num
isyms
=
[
symbol
[
r
]
for
r
in
node
.
inputs
]
isyms
=
[
symbol
[
r
]
for
r
in
node
.
inputs
]
osyms
=
[
symbol
[
r
]
for
r
in
node
.
outputs
]
osyms
=
[
symbol
[
r
]
for
r
in
node
.
outputs
]
...
@@ -643,24 +649,15 @@ class CLinker(link.Linker):
...
@@ -643,24 +649,15 @@ class CLinker(link.Linker):
args
+=
[
"storage_
%
s"
%
symbol
[
variable
]
for
variable
args
+=
[
"storage_
%
s"
%
symbol
[
variable
]
for
variable
in
utils
.
uniq
(
self
.
inputs
+
self
.
outputs
+
self
.
orphans
)]
in
utils
.
uniq
(
self
.
inputs
+
self
.
outputs
+
self
.
orphans
)]
# <<<<HASH_PLACEHOLDER>>>> will be replaced by a hash of the whole
# code in the file, including support code, in DynamicModule.code.
struct_name
=
'__struct_compiled_op_
%
s'
%
'<<<<HASH_PLACEHOLDER>>>>'
struct_code
=
struct_gen
(
args
,
init_blocks
,
blocks
,
struct_code
=
struct_gen
(
args
,
init_blocks
,
blocks
,
dict
(
failure_var
=
failure_var
,
dict
(
failure_var
=
failure_var
,
name
=
"<<<<NAME>>>>"
))
name
=
struct_name
))
# TODO: still needed? We do not use weave anymore.
# The hash calculated on the code identifies it so weave can
# cache properly. (the hash has to be used outside of the
# support code because weave does not consider changes in the
# support code)
hash
=
hash_from_code
(
struct_code
)
struct_name
=
'__struct_compiled_op_
%
s'
%
hash
#struct_code %= dict(name = struct_name)
struct_code
=
re
.
sub
(
"<<<<NAME>>>>"
,
struct_name
,
struct_code
)
self
.
struct_code
=
struct_code
self
.
struct_code
=
struct_code
self
.
struct_name
=
struct_name
self
.
struct_name
=
struct_name
self
.
hash
=
hash
self
.
args
=
args
self
.
args
=
args
self
.
r2symbol
=
symbol
self
.
r2symbol
=
symbol
self
.
init_blocks
=
init_blocks
self
.
init_blocks
=
init_blocks
...
@@ -1204,7 +1201,7 @@ class CLinker(link.Linker):
...
@@ -1204,7 +1201,7 @@ class CLinker(link.Linker):
_logger
.
debug
(
"LOCATION
%
s"
,
str
(
location
))
_logger
.
debug
(
"LOCATION
%
s"
,
str
(
location
))
try
:
try
:
module
=
c_compiler
.
compile_str
(
module
=
c_compiler
.
compile_str
(
module_name
=
mod
.
name
,
module_name
=
mod
.
code_hash
,
src_code
=
src_code
,
src_code
=
src_code
,
location
=
location
,
location
=
location
,
include_dirs
=
self
.
header_dirs
(),
include_dirs
=
self
.
header_dirs
(),
...
@@ -1224,9 +1221,8 @@ class CLinker(link.Linker):
...
@@ -1224,9 +1221,8 @@ class CLinker(link.Linker):
for our fgraph.
for our fgraph.
"""
"""
self
.
code_gen
()
self
.
code_gen
()
module_name
=
self
.
hash
mod
=
cmodule
.
DynamicModule
(
module_name
)
mod
=
cmodule
.
DynamicModule
()
# The code of instantiate
# The code of instantiate
# the 1 is for error_storage
# the 1 is for error_storage
...
...
theano/gof/cmodule.py
浏览文件 @
ef158c39
...
@@ -5,6 +5,7 @@ import cPickle
...
@@ -5,6 +5,7 @@ import cPickle
import
logging
import
logging
import
operator
import
operator
import
os
import
os
import
re
import
shutil
import
shutil
import
stat
import
stat
import
StringIO
import
StringIO
...
@@ -121,8 +122,18 @@ class ExtFunction(object):
...
@@ -121,8 +122,18 @@ class ExtFunction(object):
class
DynamicModule
(
object
):
class
DynamicModule
(
object
):
def
__init__
(
self
,
name
):
def
__init__
(
self
,
name
=
None
):
self
.
name
=
name
assert
name
is
None
,
(
"The 'name' parameter of DynamicModule"
" cannot be specified anymore. Instead, 'code_hash'"
" will be automatically computed and can be used as"
" the module's name."
)
# While the module is not finalized, we can call add_...
# when it is finalized, a hash is computed and used instead of
# the placeholder, and as module name.
self
.
finalized
=
False
self
.
code_hash
=
None
self
.
hash_placeholder
=
'<<<<HASH_PLACEHOLDER>>>>'
self
.
support_code
=
[]
self
.
support_code
=
[]
self
.
functions
=
[]
self
.
functions
=
[]
self
.
includes
=
[
"<Python.h>"
,
"<iostream>"
]
self
.
includes
=
[
"<Python.h>"
,
"<iostream>"
]
...
@@ -150,31 +161,35 @@ static struct PyModuleDef moduledef = {{
...
@@ -150,31 +161,35 @@ static struct PyModuleDef moduledef = {{
-1,
-1,
MyMethods,
MyMethods,
}};
}};
"""
.
format
(
name
=
self
.
name
)
"""
.
format
(
name
=
self
.
hash_placeholder
)
print
>>
stream
,
"PyMODINIT_FUNC PyInit_
%
s(void) {"
%
self
.
name
print
>>
stream
,
"PyMODINIT_FUNC PyInit_
%
s(void) {"
%
self
.
hash_placeholder
for
block
in
self
.
init_blocks
:
for
block
in
self
.
init_blocks
:
print
>>
stream
,
' '
,
block
print
>>
stream
,
' '
,
block
print
>>
stream
,
" PyObject *m = PyModule_Create(&moduledef);"
print
>>
stream
,
" PyObject *m = PyModule_Create(&moduledef);"
print
>>
stream
,
" return m;"
print
>>
stream
,
" return m;"
else
:
else
:
print
>>
stream
,
"PyMODINIT_FUNC init
%
s(void){"
%
self
.
name
print
>>
stream
,
"PyMODINIT_FUNC init
%
s(void){"
%
self
.
hash_placeholder
for
block
in
self
.
init_blocks
:
for
block
in
self
.
init_blocks
:
print
>>
stream
,
' '
,
block
print
>>
stream
,
' '
,
block
print
>>
stream
,
' '
,
(
'(void) Py_InitModule("
%
s", MyMethods);'
print
>>
stream
,
' '
,
(
'(void) Py_InitModule("
%
s", MyMethods);'
%
self
.
name
)
%
self
.
hash_placeholder
)
print
>>
stream
,
"}"
print
>>
stream
,
"}"
def
add_include
(
self
,
str
):
def
add_include
(
self
,
str
):
assert
not
self
.
finalized
self
.
includes
.
append
(
str
)
self
.
includes
.
append
(
str
)
def
add_init_code
(
self
,
code
):
def
add_init_code
(
self
,
code
):
assert
not
self
.
finalized
self
.
init_blocks
.
append
(
code
)
self
.
init_blocks
.
append
(
code
)
def
add_support_code
(
self
,
code
):
def
add_support_code
(
self
,
code
):
assert
not
self
.
finalized
if
code
not
in
self
.
support_code
:
# TODO: KLUDGE
if
code
not
in
self
.
support_code
:
# TODO: KLUDGE
self
.
support_code
.
append
(
code
)
self
.
support_code
.
append
(
code
)
def
add_function
(
self
,
fn
):
def
add_function
(
self
,
fn
):
assert
not
self
.
finalized
self
.
functions
.
append
(
fn
)
self
.
functions
.
append
(
fn
)
def
code
(
self
):
def
code
(
self
):
...
@@ -205,7 +220,14 @@ static struct PyModuleDef moduledef = {{
...
@@ -205,7 +220,14 @@ static struct PyModuleDef moduledef = {{
self
.
print_methoddef
(
sio
)
self
.
print_methoddef
(
sio
)
self
.
print_init
(
sio
)
self
.
print_init
(
sio
)
return
sio
.
getvalue
()
rval
=
sio
.
getvalue
()
self
.
code_hash
=
hash_from_code
(
rval
)
rval
=
re
.
sub
(
self
.
hash_placeholder
,
self
.
code_hash
,
rval
)
# Finalize the Module, so no support code or function
# can be added
self
.
finalized
=
True
return
rval
def
list_code
(
self
,
ofile
=
sys
.
stdout
):
def
list_code
(
self
,
ofile
=
sys
.
stdout
):
"""Print out the code with line numbers to `ofile` """
"""Print out the code with line numbers to `ofile` """
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论