Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b811b4ab
提交
b811b4ab
authored
3月 11, 2016
作者:
f0k
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
More efficient implementation of switch merge optimizer (as suggested by nouiz)
上级
842dcea8
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
29 行增加
和
20 行删除
+29
-20
opt.py
theano/tensor/opt.py
+22
-19
test_opt.py
theano/tensor/tests/test_opt.py
+7
-1
没有找到文件。
theano/tensor/opt.py
浏览文件 @
b811b4ab
...
@@ -3806,27 +3806,30 @@ def local_div_switch_sink(node):
...
@@ -3806,27 +3806,30 @@ def local_div_switch_sink(node):
return
False
return
False
# Merge add/sub/mul/div/minimum/maximum
of two
switches sharing the same
# Merge add/sub/mul/div/minimum/maximum
/... of
switches sharing the same
# condition, to enable further simplification of their branches
# condition, to enable further simplification of their branches
# Example: switch(c, a, b) + switch(c, x, y) -> switch(c, a+x, b+y)
# Example: switch(c, a, b) + switch(c, x, y) -> switch(c, a+x, b+y)
# TODO: generalize to all elemwise Ops? generalize to Ops with 3+ inputs?
@register_canonicalize
for
_op_name
in
(
'add'
,
'sub'
,
'mul'
,
'true_div'
,
'int_div'
,
'floor_div'
,
@gof.local_optimizer
([
T
.
Elemwise
])
'minimum'
,
'maximum'
,
'gt'
,
'lt'
,
'ge'
,
'le'
,
'eq'
,
'neq'
,
def
local_merge_switch_same_cond
(
node
):
'and_'
,
'or_'
,
'xor'
,
scal
=
theano
.
scalar
'bitwise_and'
,
'bitwise_or'
,
'bitwise_xor'
,
'pow'
):
# node must be binary elemwise or add or mul
_op
=
getattr
(
T
,
_op_name
)
if
not
isinstance
(
node
.
op
,
T
.
Elemwise
)
or
not
isinstance
(
_opt_name
=
'Merge
%
s of switch with same condition'
%
_op_name
node
.
op
.
scalar_op
,
(
scal
.
BinaryScalarOp
,
scal
.
Add
,
scal
.
Mul
)):
_opt
=
gof
.
PatternSub
(
return
in_pattern
=
(
_op
,
# all inputs must be switch
(
T
.
switch
,
'c'
,
'a1'
,
'b1'
),
if
not
all
(
s
.
owner
and
isinstance
(
s
.
owner
.
op
,
T
.
Elemwise
)
and
(
T
.
switch
,
'c'
,
'a2'
,
'b2'
)),
isinstance
(
s
.
owner
.
op
.
scalar_op
,
scal
.
Switch
)
out_pattern
=
(
T
.
switch
,
'c'
,
for
s
in
node
.
inputs
):
(
_op
,
'a1'
,
'a2'
),
return
(
_op
,
'b1'
,
'b2'
)),
# all switch conditions must be the same
name
=
_opt_name
,
cond
=
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
allow_multiple_clients
=
True
)
if
not
all
(
s
.
owner
.
inputs
[
0
]
is
cond
for
s
in
node
.
inputs
[
1
:]):
register_canonicalize
(
_opt
,
'fast_run'
,
name
=
_opt_name
)
return
del
_op_name
,
_op
,
_opt_name
,
_opt
# pull out switch
return
[
T
.
switch
(
cond
,
node
.
op
(
*
[
s
.
owner
.
inputs
[
1
]
for
s
in
node
.
inputs
]),
node
.
op
(
*
[
s
.
owner
.
inputs
[
2
]
for
s
in
node
.
inputs
]))]
#############
#############
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
b811b4ab
...
@@ -4914,7 +4914,7 @@ class test_local_useless_switch(unittest.TestCase):
...
@@ -4914,7 +4914,7 @@ class test_local_useless_switch(unittest.TestCase):
isinstance
(
node
.
op
,
theano
.
tensor
.
Elemwise
)])
==
0
isinstance
(
node
.
op
,
theano
.
tensor
.
Elemwise
)])
==
0
class
test_merge_switch_same_cond
(
unittest
.
TestCase
):
class
test_
local_
merge_switch_same_cond
(
unittest
.
TestCase
):
def
test_elemwise
(
self
):
def
test_elemwise
(
self
):
# float Ops
# float Ops
mats
=
theano
.
tensor
.
matrices
(
'cabxy'
)
mats
=
theano
.
tensor
.
matrices
(
'cabxy'
)
...
@@ -4935,6 +4935,12 @@ class test_merge_switch_same_cond(unittest.TestCase):
...
@@ -4935,6 +4935,12 @@ class test_merge_switch_same_cond(unittest.TestCase):
T
.
bitwise_and
,
T
.
bitwise_or
,
T
.
bitwise_xor
):
T
.
bitwise_and
,
T
.
bitwise_or
,
T
.
bitwise_xor
):
g
=
optimize
(
FunctionGraph
(
mats
,
[
op
(
s1
,
s2
)]))
g
=
optimize
(
FunctionGraph
(
mats
,
[
op
(
s1
,
s2
)]))
assert
str
(
g
)
.
count
(
'Switch'
)
==
1
assert
str
(
g
)
.
count
(
'Switch'
)
==
1
# add/mul with more than two inputs
u
,
v
=
theano
.
tensor
.
matrices
(
'uv'
)
s3
=
T
.
switch
(
c
,
u
,
v
)
for
op
in
(
T
.
add
,
T
.
mul
):
g
=
optimize
(
FunctionGraph
(
mats
+
[
u
,
v
],
[
op
(
s1
,
s2
,
s3
)]))
assert
str
(
g
)
.
count
(
'Switch'
)
==
1
class
T_local_sum_prod
(
unittest
.
TestCase
):
class
T_local_sum_prod
(
unittest
.
TestCase
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论