Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
555af254
提交
555af254
authored
7月 30, 2012
作者:
lamblin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #791 from nouiz/fix_test3
fix crash and test related to local_sum_broadcastable
上级
24264dd7
50a13568
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
74 行增加
和
54 行删除
+74
-54
opt.py
theano/tensor/opt.py
+19
-5
test_opt.py
theano/tensor/tests/test_opt.py
+55
-49
没有找到文件。
theano/tensor/opt.py
浏览文件 @
555af254
...
@@ -426,8 +426,11 @@ def dimshuffle_as_view(node):
...
@@ -426,8 +426,11 @@ def dimshuffle_as_view(node):
new_op
=
DimShuffle
(
op
.
input_broadcastable
,
op
.
new_order
,
inplace
=
True
)
new_op
=
DimShuffle
(
op
.
input_broadcastable
,
op
.
new_order
,
inplace
=
True
)
return
[
new_op
(
*
node
.
inputs
)]
return
[
new_op
(
*
node
.
inputs
)]
#Step 60 is the inplace optimization stage.
register_specialize
(
dimshuffle_as_view
,
'inplace'
)
compile
.
optdb
.
register
(
'dimshuffle_as_view'
,
TopoOptimizer
(
dimshuffle_as_view
,
failure_callback
=
TopoOptimizer
.
warn_inplace
),
60
,
'fast_run'
,
'inplace'
)
register_canonicalize
(
local_dimshuffle_lift
)
register_canonicalize
(
local_dimshuffle_lift
)
register_specialize
(
local_dimshuffle_lift
)
register_specialize
(
local_dimshuffle_lift
)
...
@@ -3142,9 +3145,14 @@ def local_cut_useless_reduce(node):
...
@@ -3142,9 +3145,14 @@ def local_cut_useless_reduce(node):
return
[
summed
]
return
[
summed
]
@register_canonicalize
#Enabling this optimization at canonicalization step break this test:
#theano/tensor/tests/test_opt.py:T_local_reduce.test_local_reduce_broadcast_some_0
# see gh-790 issue.
#
#@register_canonicalize
@register_specialize
@gof.local_optimizer
([])
@gof.local_optimizer
([])
def
local_
sum
_broadcastable
(
node
):
def
local_
reduce
_broadcastable
(
node
):
"""Remove reduction over broadcastable dimensions"""
"""Remove reduction over broadcastable dimensions"""
if
isinstance
(
node
.
op
,
T
.
CAReduce
):
if
isinstance
(
node
.
op
,
T
.
CAReduce
):
reduced
,
=
node
.
inputs
reduced
,
=
node
.
inputs
...
@@ -3169,12 +3177,18 @@ def local_sum_broadcastable(node):
...
@@ -3169,12 +3177,18 @@ def local_sum_broadcastable(node):
ii
+=
1
ii
+=
1
new_reduced
=
reduced
.
dimshuffle
(
*
pattern
)
new_reduced
=
reduced
.
dimshuffle
(
*
pattern
)
if
new_axis
:
if
new_axis
:
new_op
=
node
.
op
.
__class__
(
axis
=
new_axis
)
if
type
(
node
.
op
)
==
theano
.
tensor
.
elemwise
.
CAReduce
:
# This happen for tensor.max(), tensor.min()
new_op
=
node
.
op
.
__class__
(
node
.
op
.
scalar_op
,
axis
=
new_axis
)
else
:
new_op
=
node
.
op
.
__class__
(
axis
=
new_axis
)
return
[
new_op
(
new_reduced
)]
return
[
new_op
(
new_reduced
)]
else
:
else
:
# -- in this case we can remove the reduction completely
# -- in this case we can remove the reduction completely
return
[
new_reduced
.
astype
(
odtype
)]
return
[
new_reduced
.
astype
(
odtype
)]
@register_specialize
@register_specialize
@gof.local_optimizer
([])
@gof.local_optimizer
([])
def
local_sum_alloc
(
node
):
def
local_sum_alloc
(
node
):
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
555af254
...
@@ -3188,7 +3188,8 @@ class test_local_remove_switch_const_cond(unittest.TestCase):
...
@@ -3188,7 +3188,8 @@ class test_local_remove_switch_const_cond(unittest.TestCase):
class
T_local_sum
(
unittest
.
TestCase
):
class
T_local_sum
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'canonicalize'
)
self
.
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'canonicalize'
,
'specialize'
)
def
test_local_sum_all_to_none
(
self
):
def
test_local_sum_all_to_none
(
self
):
a
=
T
.
tensor3
()
a
=
T
.
tensor3
()
...
@@ -3312,54 +3313,59 @@ class T_local_sum(unittest.TestCase):
...
@@ -3312,54 +3313,59 @@ class T_local_sum(unittest.TestCase):
finally
:
finally
:
config
.
on_opt_error
=
backup
config
.
on_opt_error
=
backup
def
test_local_sum_broadcast_all_0
(
self
):
optimizer
=
optdb
.
query
(
self
.
mode
.
_optimizer
)
class
T_local_reduce
(
unittest
.
TestCase
):
def
setUp
(
self
):
x
=
T
.
TensorType
(
'int64'
,
(
True
,
True
,
True
))()
self
.
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'canonicalize'
,
g
=
FunctionGraph
([
x
],
[
x
.
sum
()])
'specialize'
)
optimizer
.
optimize
(
g
)
assert
not
any
([
def
test_local_reduce_broadcast_all_0
(
self
):
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
fct
in
[
tensor
.
sum
,
tensor
.
all
,
tensor
.
any
,
tensor
.
prod
,
for
node
in
g
.
toposort
()])
tensor
.
max
,
tensor
.
min
]:
x
=
T
.
TensorType
(
'int64'
,
(
True
,
True
,
True
))()
def
test_local_sum_broadcast_all_1
(
self
):
f
=
theano
.
function
([
x
],
[
fct
(
x
)],
mode
=
self
.
mode
)
optimizer
=
optdb
.
query
(
self
.
mode
.
_optimizer
)
assert
not
any
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
x
=
T
.
TensorType
(
'int64'
,
(
True
,
True
))()
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
g
=
FunctionGraph
([
x
],
[
x
.
sum
(
axis
=
[
0
,
1
])])
optimizer
.
optimize
(
g
)
def
test_local_reduce_broadcast_all_1
(
self
):
assert
not
any
([
for
fct
in
[
tensor
.
sum
,
tensor
.
all
,
tensor
.
any
,
tensor
.
prod
,
isinstance
(
node
.
op
,
T
.
CAReduce
)
tensor
.
max
,
tensor
.
min
]:
for
node
in
g
.
toposort
()])
x
=
T
.
TensorType
(
'int64'
,
(
True
,
True
))()
f
=
theano
.
function
([
x
],
[
fct
(
x
,
axis
=
[
0
,
1
])],
mode
=
self
.
mode
)
def
test_local_sum_broadcast_some_0
(
self
):
assert
not
any
([
optimizer
=
optdb
.
query
(
self
.
mode
.
_optimizer
)
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
x
=
T
.
TensorType
(
'int64'
,
(
True
,
False
,
True
))()
g
=
FunctionGraph
([
x
],
[
x
.
sum
(
axis
=
[
0
,
1
])])
def
test_local_reduce_broadcast_some_0
(
self
):
optimizer
.
optimize
(
g
)
for
fct
in
[
tensor
.
sum
,
tensor
.
all
,
tensor
.
any
,
tensor
.
prod
,
order
=
g
.
toposort
()
tensor
.
max
,
tensor
.
min
]:
assert
1
==
sum
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
node
in
order
])
x
=
T
.
TensorType
(
'int64'
,
(
True
,
False
,
True
))()
if
config
.
mode
==
'FAST_COMPILE'
:
f
=
theano
.
function
([
x
],
[
fct
(
x
,
axis
=
[
0
,
1
])],
mode
=
self
.
mode
)
node
=
order
[
-
1
]
else
:
order
=
f
.
maker
.
fgraph
.
toposort
()
node
=
order
[
-
2
]
assert
1
==
sum
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
op
=
node
.
op
for
node
in
order
])
assert
isinstance
(
op
,
T
.
CAReduce
)
# -- the leading broadcastable dimension has been dropped
node
=
[
node
for
node
in
order
if
isinstance
(
node
.
op
,
# by the local_sum_broadcastable optimization
tensor
.
CAReduce
)][
0
]
# now summation is over the original x's dimension 1.
assert
node
.
inputs
[
0
]
.
ndim
==
2
,
node
op
=
node
.
op
assert
op
.
axis
==
(
0
,),
op
.
axis
assert
isinstance
(
op
,
T
.
CAReduce
)
# -- the leading broadcastable dimension has been dropped
def
test_local_sum_broadcast_some_1
(
self
):
# by the local_reduce_broadcastable optimization
optimizer
=
optdb
.
query
(
self
.
mode
.
_optimizer
)
# now summation is over the original x's dimension 1.
assert
node
.
inputs
[
0
]
.
ndim
==
2
,
node
x
=
T
.
TensorType
(
'int64'
,
(
True
,
False
,
True
))()
assert
op
.
axis
==
(
0
,),
op
.
axis
g
=
FunctionGraph
([
x
],
[
x
.
sum
(
axis
=
[
0
,
2
])])
optimizer
.
optimize
(
g
)
def
test_local_reduce_broadcast_some_1
(
self
):
order
=
g
.
toposort
()
for
fct
in
[
tensor
.
sum
,
tensor
.
all
,
tensor
.
any
,
tensor
.
prod
,
assert
0
==
sum
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
node
in
order
])
tensor
.
max
,
tensor
.
min
]:
x
=
T
.
TensorType
(
'int64'
,
(
True
,
True
,
True
))()
f
=
theano
.
function
([
x
],
[
fct
(
x
,
axis
=
[
0
,
2
])],
mode
=
self
.
mode
)
assert
not
any
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
class
T_local_sum_dimshuffle
(
unittest
.
TestCase
):
class
T_local_sum_dimshuffle
(
unittest
.
TestCase
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论