Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1f2d68ea
提交
1f2d68ea
authored
7月 15, 2009
作者:
bergstra@tikuanyin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
clinker working with callcache
上级
9be3ec1a
全部展开
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
311 行增加
和
2 行删除
+311
-2
callcache.py
theano/gof/callcache.py
+52
-0
cc.py
theano/gof/cc.py
+0
-0
cmodule.py
theano/gof/cmodule.py
+252
-0
env.py
theano/gof/env.py
+4
-0
nnet.py
theano/tensor/nnet.py
+3
-2
没有找到文件。
theano/gof/callcache.py
0 → 100644
浏览文件 @
1f2d68ea
import
cPickle
,
logging
,
sys
_logger
=
logging
.
getLogger
(
"theano.gof.callcache"
)
def
warning
(
*
args
):
sys
.
stderr
.
write
(
'WARNING:'
+
' '
.
join
(
str
(
a
)
for
a
in
args
)
+
'
\n
'
)
_logger
.
warning
(
' '
.
join
(
str
(
a
)
for
a
in
args
))
def
error
(
*
args
):
sys
.
stderr
.
write
(
'ERROR:'
+
' '
.
join
(
str
(
a
)
for
a
in
args
)
+
'
\n
'
)
_logger
.
error
(
' '
.
join
(
str
(
a
)
for
a
in
args
))
def
info
(
*
args
):
sys
.
stderr
.
write
(
'INFO:'
+
' '
.
join
(
str
(
a
)
for
a
in
args
)
+
'
\n
'
)
_logger
.
info
(
' '
.
join
(
str
(
a
)
for
a
in
args
))
def
debug
(
*
args
):
sys
.
stderr
.
write
(
'DEBUG:'
+
' '
.
join
(
str
(
a
)
for
a
in
args
)
+
'
\n
'
)
_logger
.
debug
(
' '
.
join
(
str
(
a
)
for
a
in
args
))
class
CallCache
(
object
):
def
__init__
(
self
,
filename
=
None
):
self
.
filename
=
filename
try
:
if
filename
is
None
:
raise
IOError
(
'bad filename'
)
#just goes to except
f
=
file
(
filename
,
'r'
)
self
.
cache
=
cPickle
.
load
(
f
)
f
.
close
()
except
IOError
:
self
.
cache
=
{}
def
persist
(
self
,
filename
=
None
):
filename
=
self
.
filename
if
filename
is
None
else
filename
f
=
file
(
filename
,
'w'
)
cPickle
.
dump
(
self
.
cache
,
f
)
f
.
close
()
def
call
(
self
,
fn
,
args
=
(),
key
=
None
):
key
=
(
fn
,
tuple
(
args
))
if
key
is
None
else
key
if
key
not
in
self
.
cache
:
debug
(
'cache miss'
,
len
(
self
.
cache
))
self
.
cache
[
key
]
=
fn
(
*
args
)
else
:
debug
(
'cache hit'
,
len
(
self
.
cache
))
return
self
.
cache
[
key
]
def
__del__
(
self
):
try
:
if
self
.
filename
:
self
.
persist
()
except
Exception
,
e
:
_logging
.
error
(
'persist failed'
,
self
.
filename
,
e
)
theano/gof/cc.py
浏览文件 @
1f2d68ea
差异被折叠。
点击展开。
theano/gof/cmodule.py
0 → 100644
浏览文件 @
1f2d68ea
"""Generate and compile C modules for Python
"""
import
os
,
tempfile
,
StringIO
,
sys
,
logging
,
subprocess
_logger
=
logging
.
getLogger
(
"theano.gof.cmodule"
)
def
warning
(
*
args
):
sys
.
stderr
.
write
(
'WARNING:'
+
' '
.
join
(
str
(
a
)
for
a
in
args
)
+
'
\n
'
)
_logger
.
warning
(
' '
.
join
(
str
(
a
)
for
a
in
args
))
def
info
(
*
args
):
sys
.
stderr
.
write
(
'INFO:'
+
' '
.
join
(
str
(
a
)
for
a
in
args
)
+
'
\n
'
)
_logger
.
info
(
' '
.
join
(
str
(
a
)
for
a
in
args
))
def
debug
(
*
args
):
#sys.stderr.write('DEBUG:'+ ' '.join(str(a) for a in args)+'\n')
_logger
.
debug
(
' '
.
join
(
str
(
a
)
for
a
in
args
))
METH_VARARGS
=
"METH_VARARGS"
METH_NOARGS
=
"METH_NOARGS"
class
ExtFunction
(
object
):
"""A C function to put into a DynamicModule """
name
=
""
"""string - function's name"""
code_block
=
""
"""string - the entire code for the function. Has the form ``static PyObject*
<name>([...]){ ... }
See Python's C API Reference for how to write c functions for python modules.
"""
method
=
""
"""str - calling method for this function (i.e. 'METH_VARARGS', 'METH_NOARGS')"""
doc
=
""
"""str - documentation string for this function"""
def
__init__
(
self
,
name
,
code_block
,
method
,
doc
=
"undocumented"
):
self
.
name
=
name
self
.
code_block
=
code_block
self
.
method
=
method
self
.
doc
=
doc
def
method_decl
(
self
):
"""Returns the signature for this function that goes into the DynamicModule's method table"""
return
'
\t
{"
%
s",
%
s,
%
s, "
%
s"}'
%
(
self
.
name
,
self
.
name
,
self
.
method
,
self
.
doc
)
class
DynamicModule
(
object
):
def
__init__
(
self
,
name
):
self
.
name
=
name
self
.
support_code
=
[]
self
.
functions
=
[]
self
.
includes
=
[
"<Python.h>"
,
"<iostream>"
]
self
.
includes
.
append
(
'<numpy/arrayobject.h>'
)
#TODO: this should come from TensorType
self
.
init_blocks
=
[
'import_array();'
]
#TODO: from TensorType
def
print_methoddef
(
self
,
stream
):
print
>>
stream
,
"static PyMethodDef MyMethods[] = {"
for
f
in
self
.
functions
:
print
>>
stream
,
f
.
method_decl
(),
','
print
>>
stream
,
"
\t
{NULL, NULL, 0, NULL}"
print
>>
stream
,
"};"
def
print_init
(
self
,
stream
):
print
>>
stream
,
"PyMODINIT_FUNC init
%
s(void){"
%
self
.
name
for
b
in
self
.
init_blocks
:
print
>>
stream
,
' '
,
b
print
>>
stream
,
' '
,
'(void) Py_InitModule("
%
s", MyMethods);'
%
self
.
name
print
>>
stream
,
"}"
def
add_include
(
self
,
str
):
self
.
includes
.
append
(
str
)
def
add_init_code
(
self
,
code
):
self
.
init_blocks
.
append
(
code
)
def
add_support_code
(
self
,
code
):
if
code
not
in
self
.
support_code
:
#TODO: KLUDGE
self
.
support_code
.
append
(
code
)
def
add_function
(
self
,
fn
):
self
.
functions
.
append
(
fn
)
def
code
(
self
):
sio
=
StringIO
.
StringIO
()
for
inc
in
self
.
includes
:
print
>>
sio
,
"#include"
,
inc
print
>>
sio
,
"//////////////////////"
print
>>
sio
,
"//// Support Code"
print
>>
sio
,
"//////////////////////"
for
sc
in
self
.
support_code
:
print
>>
sio
,
sc
print
>>
sio
,
"//////////////////////"
print
>>
sio
,
"//// Functions"
print
>>
sio
,
"//////////////////////"
for
f
in
self
.
functions
:
print
>>
sio
,
f
.
code_block
print
>>
sio
,
"//////////////////////"
print
>>
sio
,
"//// Module init"
print
>>
sio
,
"//////////////////////"
self
.
print_methoddef
(
sio
)
self
.
print_init
(
sio
)
return
sio
.
getvalue
()
def
list_code
(
self
,
ofile
=
sys
.
stdout
):
"""Print out the code with line numbers to `ofile` """
for
i
,
line
in
enumerate
(
self
.
code
()
.
split
(
'
\n
'
)):
print
>>
ofile
,
'
%4
i'
%
(
i
+
1
),
line
ofile
.
flush
()
#TODO: add_type
def
gcc_module_compile_str
(
module_name
,
src_code
,
location
=
None
,
include_dirs
=
[],
lib_dirs
=
[],
libs
=
[],
preargs
=
[],
tmpdir
=
None
):
preargs
=
[]
if
preargs
is
None
else
list
(
preargs
)
preargs
.
append
(
'-fPIC'
)
no_opt
=
False
#TODO: where to find these strings? sys? distutils?
include_dirs
=
[
'/usr/include/python2.6'
]
+
include_dirs
libs
=
[
'python2.6'
]
+
libs
workdir
=
tempfile
.
mkdtemp
(
dir
=
location
)
cppfilename
=
os
.
path
.
join
(
workdir
,
'mod.cpp'
)
cppfile
=
file
(
cppfilename
,
'w'
)
debug
(
'Writing module C++ code to'
,
cppfilename
)
ofiles
=
[]
rval
=
None
try
:
cppfile
.
write
(
src_code
)
cppfile
.
close
()
lib_filename
=
os
.
path
.
join
(
workdir
,
'
%
s.so'
%
module_name
)
debug
(
'Generating shared lib'
,
lib_filename
)
cmd
=
[
'g++'
,
'-shared'
,
'-g'
]
if
no_opt
:
cmd
.
extend
(
p
for
p
in
preargs
if
not
p
.
startswith
(
'-O'
))
else
:
cmd
.
extend
(
preargs
)
cmd
.
extend
(
'-I
%
s'
%
idir
for
idir
in
include_dirs
)
cmd
.
extend
([
'-o'
,
lib_filename
])
cmd
.
append
(
cppfilename
)
cmd
.
extend
([
'-L
%
s'
%
ldir
for
ldir
in
lib_dirs
])
cmd
.
extend
([
'-l
%
s'
%
l
for
l
in
libs
])
debug
(
'Running cmd'
,
' '
.
join
(
cmd
))
p
=
subprocess
.
Popen
(
cmd
)
status
=
p
.
wait
()
if
status
:
warning
(
'g++ return status'
,
status
)
else
:
#touch the __init__ file
file
(
os
.
path
.
join
(
workdir
,
"__init__.py"
),
'w'
)
.
close
()
#load the module
sys
.
path
.
insert
(
0
,
workdir
)
try
:
rval
=
__import__
(
module_name
,
{},
{},
[
module_name
])
if
not
rval
:
debug
(
'__import__ failed'
)
finally
:
del
sys
.
path
[
0
]
finally
:
warning
(
"TODO: cleanup"
)
#os.remove(cppfilename)
for
ofile
in
ofiles
:
#os.remove(ofiles[0])
pass
return
rval
def
nvcc_module_compile_str
(
module_name
,
src_code
,
location
=
None
,
include_dirs
=
[],
lib_dirs
=
[],
libs
=
[],
preargs
=
[],
tmpdir
=
None
):
preargs
=
[]
if
preargs
is
None
else
list
(
preargs
)
preargs
.
append
(
'-fPIC'
)
no_opt
=
False
#TODO: -O preargs should be passed globally, not to -Xcompiler
#TODO: where to find these strings? sys? distutils?
include_dirs
=
[
'/usr/include/python2.6'
]
+
include_dirs
libs
=
[
'python2.6'
,
'cudart'
]
+
libs
lib_dirs
=
[
'/usr/local/cuda/lib'
]
+
lib_dirs
workdir
=
tempfile
.
mkdtemp
(
dir
=
location
)
cppfilename
=
os
.
path
.
join
(
workdir
,
'mod.cpp'
)
#.cpp to use g++
cppfilename
=
os
.
path
.
join
(
workdir
,
'mod.cu'
)
#.cu to use nvopencc
cppfile
=
file
(
cppfilename
,
'w'
)
debug
(
'Writing module C++ code to'
,
cppfilename
)
ofiles
=
[]
rval
=
None
try
:
cppfile
.
write
(
src_code
)
cppfile
.
close
()
lib_filename
=
os
.
path
.
join
(
workdir
,
'
%
s.so'
%
module_name
)
debug
(
'Generating shared lib'
,
lib_filename
)
cmd
=
[
'nvcc'
,
'-shared'
,
'-g'
]
cmd
.
extend
([
'-Xcompiler'
,
','
.
join
(
preargs
)])
cmd
.
extend
(
'-I
%
s'
%
idir
for
idir
in
include_dirs
)
cmd
.
extend
([
'-o'
,
lib_filename
])
cmd
.
append
(
cppfilename
)
cmd
.
extend
([
'-L
%
s'
%
ldir
for
ldir
in
lib_dirs
])
cmd
.
extend
([
'-l
%
s'
%
l
for
l
in
libs
])
debug
(
'Running cmd'
,
' '
.
join
(
cmd
))
p
=
subprocess
.
Popen
(
cmd
)
status
=
p
.
wait
()
if
status
:
warning
(
'nvcc return status'
,
status
)
else
:
#touch the __init__ file
file
(
os
.
path
.
join
(
workdir
,
"__init__.py"
),
'w'
)
.
close
()
#load the module
pathcopy
=
list
(
sys
.
path
)
sys
.
path
.
insert
(
0
,
workdir
)
try
:
rval
=
__import__
(
module_name
,
{},
{},
[
module_name
])
if
not
rval
:
debug
(
'__import__ failed'
)
finally
:
del
sys
.
path
[
0
]
assert
pathcopy
==
sys
.
path
finally
:
warning
(
"TODO: cleanup"
)
#os.remove(cppfilename)
for
ofile
in
ofiles
:
#os.remove(ofiles[0])
pass
return
rval
def
icc_module_compile_str
(
*
args
):
raise
NotImplementedError
()
theano/gof/env.py
浏览文件 @
1f2d68ea
...
@@ -434,6 +434,10 @@ class Env(utils.object2):
...
@@ -434,6 +434,10 @@ class Env(utils.object2):
{node: predecessors} where predecessors is a list of nodes
{node: predecessors} where predecessors is a list of nodes
that should be computed before the key node.
that should be computed before the key node.
"""
"""
if
len
(
self
.
nodes
)
<
2
:
# optimization
# when there are 0 or 1 nodes, no sorting is necessary
return
list
(
self
.
nodes
)
env
=
self
env
=
self
ords
=
{}
ords
=
{}
for
feature
in
env
.
_features
:
for
feature
in
env
.
_features
:
...
...
theano/tensor/nnet.py
浏览文件 @
1f2d68ea
...
@@ -126,7 +126,7 @@ class SoftmaxWithBias(gof.Op):
...
@@ -126,7 +126,7 @@ class SoftmaxWithBias(gof.Op):
return
dx
,
db
return
dx
,
db
def
c_headers
(
self
):
def
c_headers
(
self
):
return
[
'<iostream>
<
math>'
]
return
[
'<iostream>
'
,
'<c
math>'
]
@staticmethod
@staticmethod
def
c_code_template
():
def
c_code_template
():
...
@@ -514,7 +514,8 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
...
@@ -514,7 +514,8 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
db
=
tensor
.
sum
(
dx
,
axis
=
[
0
])
db
=
tensor
.
sum
(
dx
,
axis
=
[
0
])
return
dx
,
db
,
None
return
dx
,
db
,
None
def
c_headers
(
self
):
return
[
'<iostream>'
]
def
c_headers
(
self
):
return
[
'<iostream>'
,
'<cmath>'
]
@staticmethod
@staticmethod
def
c_code_template
():
def
c_code_template
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论