Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
36d1eca8
提交
36d1eca8
authored
7月 13, 2015
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3082 from yaoli/alloc_alloc_local_opt
Opt: Alloc(Alloc(...),...) -> Alloc(...) #1902
上级
25706f6c
3331671c
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
63 行增加
和
0 行删除
+63
-0
opt.py
theano/tensor/opt.py
+32
-0
test_opt.py
theano/tensor/tests/test_opt.py
+31
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
36d1eca8
...
@@ -5887,3 +5887,35 @@ register_canonicalize(gof.OpRemove(theano.gradient.disconnected_grad_),
...
@@ -5887,3 +5887,35 @@ register_canonicalize(gof.OpRemove(theano.gradient.disconnected_grad_),
def
local_grad_clip
(
node
):
def
local_grad_clip
(
node
):
if
isinstance
(
node
.
op
,
theano
.
gradient
.
GradClip
):
if
isinstance
(
node
.
op
,
theano
.
gradient
.
GradClip
):
return
node
.
inputs
return
node
.
inputs
@register_canonicalize
@register_stabilize
@register_specialize
@gof.local_optimizer
([
T
.
Alloc
])
def
local_merge_alloc
(
node
):
# This opt takes care of several cases:
# Alloc(Alloc(m, x, 1, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w)
# Alloc(Alloc(m, y, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w)
if
not
isinstance
(
node
.
op
,
T
.
Alloc
):
return
False
if
not
node
.
inputs
[
0
]
.
owner
or
not
isinstance
(
node
.
inputs
[
0
]
.
owner
.
op
,
T
.
Alloc
):
return
False
inputs_outer
=
node
.
inputs
inputs_inner
=
node
.
inputs
[
0
]
.
owner
.
inputs
dims_outer
=
inputs_outer
[
1
:]
dims_inner
=
inputs_inner
[
1
:]
dims_outer_rev
=
dims_outer
[::
-
1
]
dims_inner_rev
=
dims_inner
[::
-
1
]
# check if the pattern of broadcasting is matched, in the reversed ordering.
# The reverse ordering is needed when an Alloc add an implicit new
# broadcasted dimensions to its inputs[0]. Eg:
# Alloc(Alloc(m, y, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w)
for
dim_inner
,
dim_outer
in
zip
(
dims_inner_rev
,
dims_outer_rev
):
if
dim_inner
!=
dim_outer
:
if
isinstance
(
dim_inner
,
Constant
)
and
dim_inner
.
data
==
1
:
pass
else
:
return
False
return
[
T
.
alloc
(
inputs_inner
[
0
],
*
dims_outer
)]
theano/tensor/tests/test_opt.py
浏览文件 @
36d1eca8
...
@@ -5443,6 +5443,37 @@ class TestIntDivByOne(unittest.TestCase):
...
@@ -5443,6 +5443,37 @@ class TestIntDivByOne(unittest.TestCase):
assert
len
(
divs
)
==
0
assert
len
(
divs
)
==
0
def
test_local_merge_alloc
():
# Add this opt to the default mode,
# otherwise, FAST_COMPILE fails.
default_mode
=
theano
.
compile
.
mode
.
get_default_mode
()
opt_mode
=
default_mode
.
including
(
"local_merge_alloc"
)
x
=
T
.
iscalar
(
'x'
)
y
=
T
.
iscalar
(
'y'
)
z
=
T
.
iscalar
(
'z'
)
w
=
T
.
iscalar
(
'w'
)
m
=
T
.
fscalar
(
'm'
)
# case 1
# Alloc(Alloc(m, x, 1, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w)
output
=
T
.
alloc
(
T
.
alloc
(
m
,
1
,
y
,
1
,
1
),
x
,
y
,
z
,
w
)
f
=
theano
.
function
([
m
,
x
,
y
,
z
,
w
],
output
,
mode
=
opt_mode
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
T
.
Alloc
)
o
=
f
(
0.
,
1
,
2
,
3
,
4
)
assert
o
.
shape
==
(
1
,
2
,
3
,
4
)
# case 2
# Alloc(Alloc(m, y, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w)
output
=
T
.
alloc
(
T
.
alloc
(
m
,
y
,
1
,
1
),
x
,
y
,
z
,
w
)
f
=
theano
.
function
([
m
,
x
,
y
,
z
,
w
],
output
,
mode
=
opt_mode
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
T
.
Alloc
)
o
=
f
(
0.
,
1
,
2
,
3
,
4
)
assert
o
.
shape
==
(
1
,
2
,
3
,
4
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
t
=
TestMakeVector
(
'setUp'
)
t
=
TestMakeVector
(
'setUp'
)
t
.
setUp
()
t
.
setUp
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论