Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e3d27503
提交
e3d27503
authored
7月 05, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
7月 08, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove useless SpecifyShape
上级
f4e249de
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
56 行增加
和
5 行删除
+56
-5
shape.py
pytensor/tensor/rewriting/shape.py
+31
-5
test_shape.py
tests/tensor/rewriting/test_shape.py
+25
-0
没有找到文件。
pytensor/tensor/rewriting/shape.py
浏览文件 @
e3d27503
...
@@ -48,7 +48,7 @@ from pytensor.tensor.shape import (
...
@@ -48,7 +48,7 @@ from pytensor.tensor.shape import (
)
)
from
pytensor.tensor.subtensor
import
Subtensor
,
get_idx_list
from
pytensor.tensor.subtensor
import
Subtensor
,
get_idx_list
from
pytensor.tensor.type
import
TensorType
,
discrete_dtypes
,
integer_dtypes
from
pytensor.tensor.type
import
TensorType
,
discrete_dtypes
,
integer_dtypes
from
pytensor.tensor.type_other
import
NoneConst
from
pytensor.tensor.type_other
import
NoneConst
,
NoneTypeT
class
ShapeFeature
(
Feature
):
class
ShapeFeature
(
Feature
):
...
@@ -974,6 +974,35 @@ def local_reshape_lift(fgraph, node):
...
@@ -974,6 +974,35 @@ def local_reshape_lift(fgraph, node):
return
[
e
]
return
[
e
]
@register_useless
@register_canonicalize
@register_stabilize
@register_specialize
@node_rewriter
([
SpecifyShape
])
def
local_useless_specify_shape
(
fgraph
,
node
):
"""Remove SpecifyShape when the asserted shapes are already encoded in the static type of the input."""
x
,
*
shape
=
node
.
inputs
for
static_dim
,
specified_dim
in
zip
(
x
.
type
.
shape
,
shape
,
strict
=
True
):
if
isinstance
(
specified_dim
.
type
,
NoneTypeT
):
continue
if
static_dim
is
None
:
# There is an unknown static dimension that is being specified
return
None
if
not
(
isinstance
(
specified_dim
,
Constant
)
and
specified_dim
.
data
==
static_dim
):
# The specified dim is either:
# 1. Not constant or
# 2. Constant that does not match the static dim
# Either way, we must keep the SpecifyShape
return
None
# If we arrived here, it means SpecifyShape was already encoded in the static shape
# We don't need it
copy_stack_trace
(
node
.
outputs
[
0
],
x
)
return
[
x
]
@register_infer_shape
@register_infer_shape
@register_useless
@register_useless
@register_canonicalize
@register_canonicalize
...
@@ -1189,10 +1218,7 @@ def local_useless_dimshuffle_in_reshape(fgraph, node):
...
@@ -1189,10 +1218,7 @@ def local_useless_dimshuffle_in_reshape(fgraph, node):
@register_specialize
@register_specialize
@node_rewriter
([
Unbroadcast
])
@node_rewriter
([
Unbroadcast
])
def
local_useless_unbroadcast
(
fgraph
,
node
):
def
local_useless_unbroadcast
(
fgraph
,
node
):
"""Remove `Unbroadcast` if it does not actually change the broadcasting pattern.
"""Remove `Unbroadcast` if it does not actually change the broadcasting pattern."""
TODO: Implement equivalent rewrite for SpecifyShape
"""
if
isinstance
(
node
.
op
,
Unbroadcast
):
if
isinstance
(
node
.
op
,
Unbroadcast
):
x
=
node
.
inputs
[
0
]
x
=
node
.
inputs
[
0
]
if
x
.
type
.
ndim
==
node
.
outputs
[
0
]
.
type
.
ndim
and
all
(
if
x
.
type
.
ndim
==
node
.
outputs
[
0
]
.
type
.
ndim
and
all
(
...
...
tests/tensor/rewriting/test_shape.py
浏览文件 @
e3d27503
...
@@ -23,6 +23,7 @@ from pytensor.tensor.rewriting.shape import (
...
@@ -23,6 +23,7 @@ from pytensor.tensor.rewriting.shape import (
ShapeFeature
,
ShapeFeature
,
local_reshape_to_dimshuffle
,
local_reshape_to_dimshuffle
,
local_useless_reshape
,
local_useless_reshape
,
local_useless_specify_shape
,
)
)
from
pytensor.tensor.shape
import
(
from
pytensor.tensor.shape
import
(
Reshape
,
Reshape
,
...
@@ -476,6 +477,30 @@ class TestSameShape:
...
@@ -476,6 +477,30 @@ class TestSameShape:
shape_feature
.
same_shape
(
x
,
o
,
0
,
1
)
shape_feature
.
same_shape
(
x
,
o
,
0
,
1
)
def
test_useless_specify_shape
():
x
=
tensor
(
"x"
,
shape
=
(
None
,
5
,
3
))
# We avoid the helper specify_shape that optimizes some (but not all) cases eagerly
ss
=
SpecifyShape
()
out
=
ss
(
x
,
None
,
5
,
None
)
assert
isinstance
(
out
.
owner
.
op
,
SpecifyShape
)
ret
=
local_useless_specify_shape
.
transform
(
None
,
out
.
owner
)
assert
ret
==
[
x
]
# SpecifyShape is needed to enfore unknown dim is 3
out
=
ss
(
x
,
3
,
5
,
None
)
assert
isinstance
(
out
.
owner
.
op
,
SpecifyShape
)
ret
=
local_useless_specify_shape
.
transform
(
None
,
out
.
owner
)
assert
ret
is
None
# SpecifyShape is needed to raise mismatch between static and specified dim
out
=
ss
(
x
,
None
,
5
,
4
)
assert
isinstance
(
out
.
owner
.
op
,
SpecifyShape
)
ret
=
local_useless_specify_shape
.
transform
(
None
,
out
.
owner
)
assert
ret
is
None
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"shape"
,
"shape"
,
[
lscalar
(),
iscalar
()],
[
lscalar
(),
iscalar
()],
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论