Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a0be97e8
提交
a0be97e8
authored
12月 18, 2025
作者:
jessegrabowski
提交者:
Ricardo Vieira
12月 19, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Canonicalize `SplitDims` and `JoinDims` to `Reshape`
上级
c3347b98
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
84 行增加
和
0 行删除
+84
-0
__init__.py
pytensor/tensor/rewriting/__init__.py
+1
-0
reshape.py
pytensor/tensor/rewriting/reshape.py
+49
-0
test_reshape.py
tests/tensor/rewriting/test_reshape.py
+34
-0
没有找到文件。
pytensor/tensor/rewriting/__init__.py
浏览文件 @
a0be97e8
...
...
@@ -10,6 +10,7 @@ import pytensor.tensor.rewriting.linalg
import
pytensor.tensor.rewriting.math
import
pytensor.tensor.rewriting.numba
import
pytensor.tensor.rewriting.ofg
import
pytensor.tensor.rewriting.reshape
import
pytensor.tensor.rewriting.shape
import
pytensor.tensor.rewriting.special
import
pytensor.tensor.rewriting.subtensor
...
...
pytensor/tensor/rewriting/reshape.py
0 → 100644
浏览文件 @
a0be97e8
from
pytensor.graph
import
node_rewriter
from
pytensor.graph.rewriting.basic
import
copy_stack_trace
from
pytensor.tensor.reshape
import
JoinDims
,
SplitDims
from
pytensor.tensor.rewriting.basic
import
register_canonicalize
@register_canonicalize
@node_rewriter
([
SplitDims
])
def
local_split_dims_to_reshape
(
fgraph
,
node
):
"""
Canonicalize SplitDims Ops to Reshape Ops for further graph reasoning (and dispatch to other backends).
"""
x
,
shape
=
node
.
inputs
axis
=
node
.
op
.
axis
output_shape
=
[
*
x
.
shape
[:
axis
],
*
shape
,
*
x
.
shape
[
axis
+
1
:],
]
new_x
=
x
.
reshape
(
output_shape
)
copy_stack_trace
(
x
,
new_x
)
return
[
new_x
]
@register_canonicalize
@node_rewriter
([
JoinDims
])
def
local_join_dims_to_reshape
(
fgraph
,
node
):
"""
Canonicalize JoinDims Ops to Reshape Ops for further graph reasoning (and dispatch to other backends).
"""
(
x
,)
=
node
.
inputs
start_axis
=
node
.
op
.
start_axis
n_axes
=
node
.
op
.
n_axes
output_shape
=
[
*
x
.
shape
[:
start_axis
],
-
1
,
*
x
.
shape
[
start_axis
+
n_axes
:],
]
new_x
=
x
.
reshape
(
output_shape
)
copy_stack_trace
(
x
,
new_x
)
return
[
new_x
]
tests/tensor/rewriting/test_reshape.py
0 → 100644
浏览文件 @
a0be97e8
from
pytensor.graph
import
FunctionGraph
,
rewrite_graph
from
pytensor.tensor.reshape
import
JoinDims
,
SplitDims
,
join_dims
,
split_dims
from
pytensor.tensor.shape
import
Reshape
from
pytensor.tensor.type
import
tensor
def
test_local_split_dims_to_reshape
():
x
=
tensor
(
"x"
,
shape
=
(
2
,
10
,
3
))
x_split
=
split_dims
(
x
,
axis
=
1
,
shape
=
(
2
,
5
,
1
))
fg
=
FunctionGraph
(
inputs
=
[
x
],
outputs
=
[
x_split
])
assert
sum
([
1
for
node
in
fg
.
toposort
()
if
isinstance
(
node
.
op
,
SplitDims
)])
==
1
rewrite_graph
(
fg
,
include
=
(
"canonicalize"
,))
assert
sum
([
1
for
node
in
fg
.
toposort
()
if
isinstance
(
node
.
op
,
SplitDims
)])
==
0
assert
sum
([
1
for
node
in
fg
.
toposort
()
if
isinstance
(
node
.
op
,
Reshape
)])
==
1
assert
fg
.
outputs
[
0
]
.
type
.
shape
==
(
2
,
2
,
5
,
1
,
3
)
def
test_local_join_dims_to_reshape
():
x
=
tensor
(
"x"
,
shape
=
(
2
,
2
,
5
,
1
,
3
))
x_join
=
join_dims
(
x
,
axis
=
(
1
,
2
,
3
))
fg
=
FunctionGraph
(
inputs
=
[
x
],
outputs
=
[
x_join
])
assert
sum
([
1
for
node
in
fg
.
toposort
()
if
isinstance
(
node
.
op
,
JoinDims
)])
==
1
rewrite_graph
(
fg
,
include
=
(
"canonicalize"
,))
assert
sum
([
1
for
node
in
fg
.
toposort
()
if
isinstance
(
node
.
op
,
JoinDims
)])
==
0
assert
sum
([
1
for
node
in
fg
.
toposort
()
if
isinstance
(
node
.
op
,
Reshape
)])
==
1
assert
fg
.
outputs
[
0
]
.
type
.
shape
==
(
2
,
10
,
3
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论