Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0f1f5beb
提交
0f1f5beb
authored
1月 20, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
1月 20, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor local_canonicalize_alloc into local_alloc_sink_dimshuffle
上级
240827cf
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
14 行增加
和
36 行删除
+14
-36
basic_opt.py
aesara/tensor/basic_opt.py
+4
-28
test_basic_opt.py
tests/tensor/test_basic_opt.py
+9
-7
test_subtensor_opt.py
tests/tensor/test_subtensor_opt.py
+1
-1
没有找到文件。
aesara/tensor/basic_opt.py
浏览文件 @
0f1f5beb
...
@@ -1809,19 +1809,9 @@ def local_useless_alloc(fgraph, node):
...
@@ -1809,19 +1809,9 @@ def local_useless_alloc(fgraph, node):
@register_specialize
@register_specialize
@register_stabilize
@register_stabilize
@register_canonicalize
@register_canonicalize
@local_optimizer
([
alloc
])
@local_optimizer
([
Alloc
])
def
local_canonicalize_alloc
(
fgraph
,
node
):
def
local_alloc_sink_dimshuffle
(
fgraph
,
node
):
"""If the input type is the same as the output type (dtype and broadcast)
r"""Convert broadcastable leading dimensions in an `Alloc` to `DimShuffle`\s."""
there is no change in the shape of the input. So this is just a simple copy
of the input. This is not needed. (as local_useless_alloc)
Also, it will canonicalize alloc by creating Dimshuffle after the
alloc to introduce the dimensions of constant size 1.
See https://github.com/Theano/Theano/issues/4072 to know why this
is needed.
"""
op
=
node
.
op
op
=
node
.
op
if
not
isinstance
(
op
,
Alloc
):
if
not
isinstance
(
op
,
Alloc
):
return
False
return
False
...
@@ -1829,22 +1819,7 @@ def local_canonicalize_alloc(fgraph, node):
...
@@ -1829,22 +1819,7 @@ def local_canonicalize_alloc(fgraph, node):
inp
=
node
.
inputs
[
0
]
inp
=
node
.
inputs
[
0
]
output
=
node
.
outputs
[
0
]
output
=
node
.
outputs
[
0
]
# Check if dtype and broadcast remain the same.
if
(
inp
.
type
.
dtype
==
output
.
type
.
dtype
and
inp
.
type
.
broadcastable
==
output
.
type
.
broadcastable
):
# We don't need to copy over any stack traces here
return
[
inp
]
# Allow local_merge_alloc to do its work first
clients
=
fgraph
.
clients
[
output
]
for
client
,
i
in
clients
:
if
client
!=
"output"
and
isinstance
(
client
.
op
,
Alloc
):
return
# Check if alloc adds a broadcastable dimension with shape 1.
# Check if alloc adds a broadcastable dimension with shape 1.
output_shape
=
node
.
inputs
[
1
:]
output_shape
=
node
.
inputs
[
1
:]
num_dims_with_size_1_added_to_left
=
0
num_dims_with_size_1_added_to_left
=
0
for
i
in
range
(
len
(
output_shape
)
-
inp
.
ndim
):
for
i
in
range
(
len
(
output_shape
)
-
inp
.
ndim
):
...
@@ -1852,6 +1827,7 @@ def local_canonicalize_alloc(fgraph, node):
...
@@ -1852,6 +1827,7 @@ def local_canonicalize_alloc(fgraph, node):
num_dims_with_size_1_added_to_left
+=
1
num_dims_with_size_1_added_to_left
+=
1
else
:
else
:
break
break
new_output_shape
=
output_shape
[
num_dims_with_size_1_added_to_left
:]
new_output_shape
=
output_shape
[
num_dims_with_size_1_added_to_left
:]
if
num_dims_with_size_1_added_to_left
>
0
and
len
(
new_output_shape
)
>=
inp
.
ndim
:
if
num_dims_with_size_1_added_to_left
>
0
and
len
(
new_output_shape
)
>=
inp
.
ndim
:
if
(
if
(
...
...
tests/tensor/test_basic_opt.py
浏览文件 @
0f1f5beb
...
@@ -40,7 +40,7 @@ from aesara.tensor.basic_opt import (
...
@@ -40,7 +40,7 @@ from aesara.tensor.basic_opt import (
ShapeFeature
,
ShapeFeature
,
apply_rebroadcast_opt
,
apply_rebroadcast_opt
,
assert_op
,
assert_op
,
local_
canonicalize_alloc
,
local_
alloc_sink_dimshuffle
,
local_dimshuffle_lift
,
local_dimshuffle_lift
,
local_merge_alloc
,
local_merge_alloc
,
local_reshape_to_dimshuffle
,
local_reshape_to_dimshuffle
,
...
@@ -1423,8 +1423,7 @@ class TestLocalCanonicalizeAlloc:
...
@@ -1423,8 +1423,7 @@ class TestLocalCanonicalizeAlloc:
# The optimization 'locall_fill_to_alloc' should call at.alloc,
# The optimization 'locall_fill_to_alloc' should call at.alloc,
# which should return x and not alloc(x, ...)
# which should return x and not alloc(x, ...)
mode
=
mode_opt
.
excluding
(
"local_canonicalize_alloc"
)
f
=
function
([
x
],
[
y
],
mode
=
mode_opt
.
including
(
"local_fill_to_alloc"
))
f
=
function
([
x
],
[
y
],
mode
=
mode
)
assert
not
any
(
assert
not
any
(
[
isinstance
(
node
.
op
,
Alloc
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
[
isinstance
(
node
.
op
,
Alloc
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
)
)
...
@@ -1433,9 +1432,12 @@ class TestLocalCanonicalizeAlloc:
...
@@ -1433,9 +1432,12 @@ class TestLocalCanonicalizeAlloc:
x
=
matrix
(
"x"
)
x
=
matrix
(
"x"
)
y
=
at
.
tile
(
x
,
(
1
,)
*
2
)
y
=
at
.
tile
(
x
,
(
1
,)
*
2
)
mode
=
mode_opt
.
including
(
"local_canonicalize_alloc"
)
mode
=
mode_opt
.
including
(
"local_dimshuffle_lift"
,
"local_useless_dimshuffle_in_reshape"
,
"local_alloc_sink_dimshuffle"
,
)
f
=
function
([
x
],
[
y
],
mode
=
mode
)
f
=
function
([
x
],
[
y
],
mode
=
mode
)
[
node
.
op
.
__class__
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
assert
not
any
(
assert
not
any
(
[
isinstance
(
node
.
op
,
Alloc
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
[
isinstance
(
node
.
op
,
Alloc
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
...
@@ -1454,7 +1456,7 @@ class TestLocalCanonicalizeAlloc:
...
@@ -1454,7 +1456,7 @@ class TestLocalCanonicalizeAlloc:
g
=
FunctionGraph
(
outputs
=
[
x
])
g
=
FunctionGraph
(
outputs
=
[
x
])
assert
any
(
isinstance
(
node
.
op
,
Alloc
)
for
node
in
g
.
toposort
())
assert
any
(
isinstance
(
node
.
op
,
Alloc
)
for
node
in
g
.
toposort
())
alloc_lift
=
out2in
(
local_
canonicalize_alloc
)
alloc_lift
=
out2in
(
local_
alloc_sink_dimshuffle
)
alloc_lift
.
optimize
(
g
)
alloc_lift
.
optimize
(
g
)
if
has_alloc
:
if
has_alloc
:
...
@@ -3217,7 +3219,7 @@ def test_local_Unique_Alloc_lift(
...
@@ -3217,7 +3219,7 @@ def test_local_Unique_Alloc_lift(
# The remaining exclusions simply allow us to perform the check below that
# The remaining exclusions simply allow us to perform the check below that
# makes sure the original `Alloc` is present in our reference (sub)graph.
# makes sure the original `Alloc` is present in our reference (sub)graph.
opt_mode
=
default_mode
.
excluding
(
opt_mode
=
default_mode
.
excluding
(
"local_useless_alloc"
,
"local_
canonicalize_alloc
"
,
"local_Unique_Alloc_lift"
"local_useless_alloc"
,
"local_
alloc_sink_dimshuffle
"
,
"local_Unique_Alloc_lift"
)
)
y_fn
=
function
([
x
],
[
y
,
y_opt
],
mode
=
opt_mode
)
y_fn
=
function
([
x
],
[
y
,
y_opt
],
mode
=
opt_mode
)
# Make sure that the original `Alloc` is used to compute the reference `y`
# Make sure that the original `Alloc` is used to compute the reference `y`
...
...
tests/tensor/test_subtensor_opt.py
浏览文件 @
0f1f5beb
...
@@ -1860,7 +1860,7 @@ class TestLocalElemwiseAlloc:
...
@@ -1860,7 +1860,7 @@ class TestLocalElemwiseAlloc:
# Exclude local_useless_alloc, since it does not introduce
# Exclude local_useless_alloc, since it does not introduce
# assert in all the same cases.
# assert in all the same cases.
self
.
fast_run_mode
=
self
.
fast_run_mode
.
excluding
(
self
.
fast_run_mode
=
self
.
fast_run_mode
.
excluding
(
"local_useless_alloc"
,
"local_
canonicalize_alloc
"
"local_useless_alloc"
,
"local_
alloc_sink_dimshuffle
"
)
)
# No optimization on alloc
# No optimization on alloc
func
=
function
(
func
=
function
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论