Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2e20f232
提交
2e20f232
authored
2月 09, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make a machanism to don't have Alloc and GpuAlloc constant folded in some cases.
上级
7b943a5b
显示空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
104 行增加
和
0 行删除
+104
-0
op.txt
doc/extending/op.txt
+14
-0
op.py
theano/gof/op.py
+10
-0
basic_ops.py
theano/sandbox/cuda/basic_ops.py
+11
-0
test_basic_ops.py
theano/sandbox/cuda/tests/test_basic_ops.py
+9
-0
basic.py
theano/tensor/basic.py
+10
-0
opt.py
theano/tensor/opt.py
+3
-0
test_basic.py
theano/tensor/tests/test_basic.py
+47
-0
没有找到文件。
doc/extending/op.txt
浏览文件 @
2e20f232
...
@@ -217,6 +217,20 @@ following methods:
...
@@ -217,6 +217,20 @@ following methods:
``__str__`` method include the name of the op and the Op's parameters'
``__str__`` method include the name of the op and the Op's parameters'
values.
values.
.. function:: do_constant_folding(node)
*Default:* Return True
By default when optimizations are enabled, we remove during
function compilation apply node that have all their input
constants. We replace the Apply node with a Theano constant
variable. This way, the apply node is not executed at each function
call. If you want to force the execution of an op during the
function call, make do_constant_folding return False.
As done in the Alloc op, you can return False only in some case by
analysing the graph from the node parameter.
At a bare minimum, a new Op must define ``make_node`` and ``perform``, which
At a bare minimum, a new Op must define ``make_node`` and ``perform``, which
have no defaults.
have no defaults.
...
...
theano/gof/op.py
浏览文件 @
2e20f232
...
@@ -509,6 +509,16 @@ class PureOp(object):
...
@@ -509,6 +509,16 @@ class PureOp(object):
"""
"""
raise
utils
.
MethodNotDefined
(
"perform"
,
type
(
self
),
self
.
__class__
.
__name__
)
raise
utils
.
MethodNotDefined
(
"perform"
,
type
(
self
),
self
.
__class__
.
__name__
)
def
do_constant_folding
(
self
,
node
):
"""
This allow each op to dertermine if they want to be constant
folded when all there in put are constant. This allow them to
choose where they put their memory/speed trade off. Also, it
could make thing faster as Constant can't be used for inplace
operation(see *IncSubtensor)
"""
return
True
class
Op
(
utils
.
object2
,
PureOp
,
CLinkerOp
):
class
Op
(
utils
.
object2
,
PureOp
,
CLinkerOp
):
"""Convenience class to bundle `PureOp` and `CLinkerOp`"""
"""Convenience class to bundle `PureOp` and `CLinkerOp`"""
...
...
theano/sandbox/cuda/basic_ops.py
浏览文件 @
2e20f232
...
@@ -2004,6 +2004,17 @@ class GpuAlloc(Op):
...
@@ -2004,6 +2004,17 @@ class GpuAlloc(Op):
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
3
,)
return
(
3
,)
def
do_constant_folding
(
self
,
node
):
if
any
([
isinstance
(
client
[
0
]
.
op
,
(
tensor
.
IncSubtensor
,
tensor
.
AdvancedIncSubtensor1
,
GpuIncSubtensor
,
GpuAdvancedIncSubtensor1
))
for
client
in
node
.
outputs
[
0
]
.
clients
]):
return
False
return
True
gpu_alloc
=
GpuAlloc
()
gpu_alloc
=
GpuAlloc
()
...
...
theano/sandbox/cuda/tests/test_basic_ops.py
浏览文件 @
2e20f232
...
@@ -728,7 +728,16 @@ def test_gpualloc_output_to_gpu():
...
@@ -728,7 +728,16 @@ def test_gpualloc_output_to_gpu():
assert
numpy
.
allclose
(
numpy
.
ones
(
a
.
get_value
(
borrow
=
True
)
.
shape
)
+
9
,
f_gpu
(
9
))
assert
numpy
.
allclose
(
numpy
.
ones
(
a
.
get_value
(
borrow
=
True
)
.
shape
)
+
9
,
f_gpu
(
9
))
assert
numpy
.
allclose
(
f
(
5
),
f_gpu
(
5
))
assert
numpy
.
allclose
(
f
(
5
),
f_gpu
(
5
))
import
theano.tensor.tests.test_basic
import
theano.tensor.tests.test_basic
class
TestAlloc
(
theano
.
tensor
.
tests
.
test_basic
.
TestAlloc
):
dtype
=
"float32"
mode
=
mode_with_gpu
shared
=
staticmethod
(
cuda
.
shared_constructor
)
allocs
=
[
B
.
GpuAlloc
,
B
.
GpuAlloc
,
tensor
.
Alloc
]
class
T_Join_and_Split
(
theano
.
tensor
.
tests
.
test_basic
.
T_Join_and_Split
):
class
T_Join_and_Split
(
theano
.
tensor
.
tests
.
test_basic
.
T_Join_and_Split
):
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
...
...
theano/tensor/basic.py
浏览文件 @
2e20f232
...
@@ -2616,6 +2616,16 @@ class Alloc(gof.Op):
...
@@ -2616,6 +2616,16 @@ class Alloc(gof.Op):
return
[
None
]
return
[
None
]
return
self
.
make_node
(
eval_points
[
0
],
*
inputs
[
1
:])
.
outputs
return
self
.
make_node
(
eval_points
[
0
],
*
inputs
[
1
:])
.
outputs
def
do_constant_folding
(
self
,
node
):
if
python_any
([
isinstance
(
client
[
0
]
.
op
,
(
IncSubtensor
,
AdvancedIncSubtensor1
,
AdvancedIncSubtensor
,
))
for
client
in
node
.
outputs
[
0
]
.
clients
]):
return
False
return
True
alloc
=
Alloc
()
alloc
=
Alloc
()
pprint
.
assign
(
alloc
,
printing
.
FunctionPrinter
(
'alloc'
))
pprint
.
assign
(
alloc
,
printing
.
FunctionPrinter
(
'alloc'
))
...
...
theano/tensor/opt.py
浏览文件 @
2e20f232
...
@@ -3767,6 +3767,9 @@ def constant_folding(node):
...
@@ -3767,6 +3767,9 @@ def constant_folding(node):
if
not
isinstance
(
input
,
Constant
):
if
not
isinstance
(
input
,
Constant
):
return
False
return
False
#condition: all inputs are constant
#condition: all inputs are constant
if
not
node
.
op
.
do_constant_folding
(
node
):
# The op ask to don't be constant folded.
return
False
storage_map
=
dict
([(
i
,
[
i
.
data
])
for
i
in
node
.
inputs
])
storage_map
=
dict
([(
i
,
[
i
.
data
])
for
i
in
node
.
inputs
])
compute_map
=
dict
([(
i
,
[
True
])
for
i
in
node
.
inputs
])
compute_map
=
dict
([(
i
,
[
True
])
for
i
in
node
.
inputs
])
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
2e20f232
...
@@ -48,6 +48,11 @@ except ImportError:
...
@@ -48,6 +48,11 @@ except ImportError:
mode_no_scipy
=
"FAST_RUN"
mode_no_scipy
=
"FAST_RUN"
floatX
=
config
.
floatX
floatX
=
config
.
floatX
if
config
.
mode
==
"FAST_COMPILE"
:
mode_opt
=
"FAST_RUN"
else
:
mode_opt
=
get_default_mode
()
### seed random number generator so that unittests are deterministic ###
### seed random number generator so that unittests are deterministic ###
utt
.
seed_rng
()
utt
.
seed_rng
()
...
@@ -1266,6 +1271,48 @@ Alloc13GradTester = makeBroadcastTester(
...
@@ -1266,6 +1271,48 @@ Alloc13GradTester = makeBroadcastTester(
),
),
)
)
class
TestAlloc
(
unittest
.
TestCase
):
dtype
=
config
.
floatX
mode
=
mode_opt
shared
=
staticmethod
(
theano
.
shared
)
allocs
=
[
tensor
.
Alloc
]
*
3
def
test_alloc_constant_folding
(
self
):
test_params
=
numpy
.
asarray
(
numpy
.
random
.
randn
(
50
*
60
),
self
.
dtype
)
some_vector
=
vector
(
'some_vector'
,
dtype
=
self
.
dtype
)
some_matrix
=
some_vector
.
reshape
((
60
,
50
))
variables
=
self
.
shared
(
numpy
.
ones
((
50
,),
dtype
=
self
.
dtype
))
idx
=
tensor
.
constant
(
numpy
.
arange
(
50
))
for
alloc
,
(
subtensor
,
n_alloc
)
in
zip
(
self
.
allocs
,
[
#IncSubtensor1
(
some_matrix
[:
60
],
2
),
#AdvancedIncSubtensor1
(
some_matrix
[
arange
(
60
)],
2
),
#AdvancedIncSubtensor
(
some_matrix
[
idx
,
idx
],
1
)]):
derp
=
sum
(
dot
(
subtensor
,
variables
))
fobj
=
theano
.
function
([
some_vector
],
derp
,
mode
=
self
.
mode
)
grad_derp
=
theano
.
grad
(
derp
,
some_vector
)
fgrad
=
theano
.
function
([
some_vector
],
grad_derp
,
mode
=
self
.
mode
)
topo_obj
=
fobj
.
maker
.
env
.
toposort
()
assert
numpy
.
sum
([
isinstance
(
node
.
op
,
alloc
)
for
node
in
topo_obj
])
==
0
topo_grad
=
fgrad
.
maker
.
env
.
toposort
()
#print subtensor
#theano.printing.debugprint(fgrad)
assert
numpy
.
sum
([
isinstance
(
node
.
op
,
alloc
)
for
node
in
topo_grad
])
==
n_alloc
fobj
(
test_params
)
fgrad
(
test_params
)
def
test_eye
():
def
test_eye
():
def
check
(
dtype
,
N
,
M_
=
None
,
k
=
0
):
def
check
(
dtype
,
N
,
M_
=
None
,
k
=
0
):
# Theano does not accept None as a tensor.
# Theano does not accept None as a tensor.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论