Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a5fe2dd0
提交
a5fe2dd0
authored
7月 11, 2012
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
local_sum_broadcastable - unit tests and more complete impl
上级
de93a9fe
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
73 行增加
和
9 行删除
+73
-9
opt.py
theano/tensor/opt.py
+27
-9
test_opt.py
theano/tensor/tests/test_opt.py
+46
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
a5fe2dd0
...
@@ -3140,16 +3140,34 @@ def local_cut_useless_reduce(node):
...
@@ -3140,16 +3140,34 @@ def local_cut_useless_reduce(node):
@gof.local_optimizer
([])
@gof.local_optimizer
([])
def
local_sum_broadcastable
(
node
):
def
local_sum_broadcastable
(
node
):
"""Remove reduction over broadcastable dimensions"""
"""Remove reduction over broadcastable dimensions"""
if
isinstance
(
node
.
op
,
T
.
CAReduce
)
and
node
.
op
.
axis
is
not
None
:
if
isinstance
(
node
.
op
,
T
.
CAReduce
):
reduced
,
=
node
.
inputs
reduced
,
=
node
.
inputs
axis
=
list
(
node
.
op
.
axis
)
odtype
=
node
.
outputs
[
0
]
.
dtype
cuttable
=
[
a
for
a
in
axis
if
reduced
.
broadcastable
[
a
]]
if
node
.
op
.
axis
is
None
:
if
cuttable
==
axis
:
if
all
(
reduced
.
broadcastable
):
# -- in this case we can remove the reduction completely
return
[
reduced
.
dimshuffle
()
.
astype
(
odtype
)]
pattern
=
[
p
for
p
in
range
(
reduced
.
ndim
)
if
p
not
in
cuttable
]
else
:
rval
=
reduced
.
dimshuffle
(
*
pattern
)
axis
=
list
(
node
.
op
.
axis
)
return
[
rval
]
cuttable
=
[
a
for
a
in
axis
if
reduced
.
broadcastable
[
a
]]
if
cuttable
:
# -- we can remove some axes of summation,
# which simplifies the codegen for sum, especially on GPU
new_axis
=
[]
pattern
=
[]
ii
=
0
for
p
in
range
(
reduced
.
ndim
):
if
p
not
in
cuttable
:
if
p
in
axis
:
new_axis
.
append
(
ii
)
pattern
.
append
(
p
)
ii
+=
1
new_reduced
=
reduced
.
dimshuffle
(
*
pattern
)
if
new_axis
:
new_op
=
node
.
op
.
__class__
(
axis
=
new_axis
)
return
[
new_op
(
new_reduced
)]
else
:
# -- in this case we can remove the reduction completely
return
[
new_reduced
.
astype
(
odtype
)]
@register_specialize
@register_specialize
@gof.local_optimizer
([])
@gof.local_optimizer
([])
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
a5fe2dd0
...
@@ -46,6 +46,7 @@ from theano.tensor import (
...
@@ -46,6 +46,7 @@ from theano.tensor import (
)
)
from
theano.tensor.elemwise
import
DimShuffle
from
theano.tensor.elemwise
import
DimShuffle
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano.compile.mode
import
optdb
mode_opt
=
theano
.
config
.
mode
mode_opt
=
theano
.
config
.
mode
if
mode_opt
==
'FAST_COMPILE'
:
if
mode_opt
==
'FAST_COMPILE'
:
...
@@ -3288,6 +3289,51 @@ class T_local_sum(unittest.TestCase):
...
@@ -3288,6 +3289,51 @@ 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
)
x
=
T
.
TensorType
(
'int64'
,
(
True
,
True
,
True
))()
env
=
Env
([
x
],
[
x
.
sum
()])
optimizer
.
optimize
(
env
)
assert
not
any
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
node
in
env
.
toposort
()])
def
test_local_sum_broadcast_all_1
(
self
):
optimizer
=
optdb
.
query
(
self
.
mode
.
_optimizer
)
x
=
T
.
TensorType
(
'int64'
,
(
True
,
True
))()
env
=
Env
([
x
],
[
x
.
sum
(
axis
=
[
0
,
1
])])
optimizer
.
optimize
(
env
)
assert
not
any
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
node
in
env
.
toposort
()])
def
test_local_sum_broadcast_some_0
(
self
):
optimizer
=
optdb
.
query
(
self
.
mode
.
_optimizer
)
x
=
T
.
TensorType
(
'int64'
,
(
True
,
False
,
True
))()
env
=
Env
([
x
],
[
x
.
sum
(
axis
=
[
0
,
1
])])
optimizer
.
optimize
(
env
)
order
=
env
.
toposort
()
assert
1
==
sum
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
node
in
order
])
op
=
order
[
-
2
]
.
op
assert
isinstance
(
op
,
T
.
CAReduce
)
# -- the leading broadcastable dimension has been dropped
# by the local_sum_broadcastable optimization
# now summation is over the original x's dimension 1.
assert
order
[
-
2
]
.
inputs
[
0
]
.
ndim
==
2
,
order
[
-
2
]
assert
op
.
axis
==
(
0
,),
op
.
axis
def
test_local_sum_broadcast_some_1
(
self
):
optimizer
=
optdb
.
query
(
self
.
mode
.
_optimizer
)
x
=
T
.
TensorType
(
'int64'
,
(
True
,
False
,
True
))()
env
=
Env
([
x
],
[
x
.
sum
(
axis
=
[
0
,
2
])])
optimizer
.
optimize
(
env
)
order
=
env
.
toposort
()
assert
0
==
sum
([
isinstance
(
node
.
op
,
T
.
CAReduce
)
for
node
in
order
])
class
T_local_sum_dimshuffle
(
unittest
.
TestCase
):
class
T_local_sum_dimshuffle
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论