Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f1db1bd6
提交
f1db1bd6
authored
1月 20, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
5月 09, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Lift Subtensor over transpose
上级
db7b988f
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
88 行增加
和
2 行删除
+88
-2
subtensor_lift.py
pytensor/tensor/rewriting/subtensor_lift.py
+58
-1
test_subtensor_lift.py
tests/tensor/rewriting/test_subtensor_lift.py
+30
-1
没有找到文件。
pytensor/tensor/rewriting/subtensor_lift.py
浏览文件 @
f1db1bd6
from
collections.abc
import
Iterable
from
collections.abc
import
Iterable
,
Sequence
import
numpy
as
np
...
...
@@ -17,12 +17,14 @@ from pytensor.tensor.basic import (
)
from
pytensor.tensor.elemwise
import
DimShuffle
,
Elemwise
from
pytensor.tensor.exceptions
import
NotScalarConstantError
from
pytensor.tensor.extra_ops
import
squeeze
from
pytensor.tensor.math
import
Dot
,
ceil_intdiv
,
dot
from
pytensor.tensor.rewriting.basic
import
(
register_canonicalize
,
register_specialize
,
register_stabilize
,
)
from
pytensor.tensor.rewriting.elemwise
import
local_dimshuffle_lift
from
pytensor.tensor.rewriting.subtensor
import
is_full_slice
,
register_useless
from
pytensor.tensor.shape
import
(
Shape
,
...
...
@@ -42,6 +44,12 @@ from pytensor.tensor.type import TensorType
from
pytensor.tensor.type_other
import
SliceType
def
_dims_dropped_by_basic_index
(
idxs
:
Sequence
[
slice
|
int
])
->
tuple
[
int
,
...
]:
# Inputs can be slice or integer indexes
# Slices keep the dimensions, integers collapse them
return
tuple
(
i
for
i
,
idx
in
enumerate
(
idxs
)
if
not
isinstance
(
idx
,
slice
))
@register_canonicalize
@register_stabilize
@register_specialize
...
...
@@ -243,6 +251,55 @@ def local_subtensor_of_expand_dims(fgraph, node):
return
[
out
]
@register_canonicalize
@register_specialize
@node_rewriter
([
Subtensor
])
def
local_subtensor_of_transpose
(
fgraph
,
node
):
"""Lift a Subtensor through a DimShuffle that only transposes.
transpose(x, (1, 0, 2))[i:, j:, k:] -> transpose(x[j:, i:, k:], (1, 0, 2))
"""
ds
,
*
idx
=
node
.
inputs
if
not
(
ds
.
owner
and
isinstance
(
ds
.
owner
.
op
,
DimShuffle
)):
return
None
ds_op
=
ds
.
owner
.
op
if
not
ds_op
.
is_transpose
:
return
None
transposition
=
ds_op
.
transposition
[
x
]
=
ds
.
owner
.
inputs
idx_tuple
=
indices_from_subtensor
(
idx
,
node
.
op
.
idx_list
)
# Apply the transposition to the indexes
ndim
=
x
.
type
.
ndim
n_implicit_idxs
=
ndim
-
len
(
idx_tuple
)
idx_tuple
=
idx_tuple
+
(
slice
(
None
),)
*
n_implicit_idxs
new_idxs
=
[
idx_tuple
[
transposition
.
index
(
i
)]
for
i
in
range
(
ndim
)]
new_x
=
x
[
tuple
(
new_idxs
)]
# Reintroduce any dims dropped by indexing so the original transpose still works
dims_dropped_by_new_idx
=
_dims_dropped_by_basic_index
(
new_idxs
)
if
dims_dropped_by_new_idx
:
new_x
=
expand_dims
(
new_x
,
axis
=
dims_dropped_by_new_idx
)
# Apply the transpose
new_out
=
ds_op
(
new_x
)
# Squeeze dims again now that the transpose is done
if
dims_dropped_by_new_idx
:
dims_dropped_by_original_idx
=
_dims_dropped_by_basic_index
(
idx_tuple
)
new_out
=
squeeze
(
new_out
,
axis
=
dims_dropped_by_original_idx
)
# Cleanup consecutive expand_dims / transpose / squeeze (if any)
if
dims_dropped_by_new_idx
:
[
new_out
]
=
local_dimshuffle_lift
.
transform
(
fgraph
,
new_out
.
owner
)
return
[
new_out
]
@register_infer_shape
@register_useless
@register_canonicalize
...
...
tests/tensor/rewriting/test_subtensor_lift.py
浏览文件 @
f1db1bd6
...
...
@@ -252,7 +252,7 @@ def test_local_subtensor_of_expand_dims(original_fn, expected_fn):
out
=
original_fn
(
x
)
expected_opt_out
=
expected_fn
(
x
)
opt_out
=
rewrite_graph
(
out
,
exclude
=
[
"local_uint_constant_indices"
]
)
opt_out
=
rewrite_graph
(
out
)
assert
equal_computations
([
opt_out
],
[
expected_opt_out
]),
debugprint
(
[
opt_out
,
expected_opt_out
],
print_type
=
True
)
...
...
@@ -262,6 +262,35 @@ def test_local_subtensor_of_expand_dims(original_fn, expected_fn):
)
@pytest.mark.parametrize
(
"original_fn, expected_fn"
,
[
(
lambda
x
:
x
.
transpose
(
2
,
1
,
0
)[
0
],
lambda
x
:
x
[:,
:,
0
]
.
transpose
(
1
,
0
)),
(
lambda
x
:
x
.
transpose
(
2
,
1
,
0
)[:,
:,
1
:],
lambda
x
:
x
[
1
:]
.
transpose
(
2
,
1
,
0
)),
(
lambda
x
:
x
.
transpose
(
2
,
1
,
0
)[
0
,
:
1
,
1
:],
lambda
x
:
x
[
1
:,
:
1
,
0
]
.
transpose
(
1
,
0
),
),
(
lambda
x
:
x
.
transpose
(
2
,
1
,
0
)[
0
,
:
1
,
1
],
lambda
x
:
x
[
1
,
:
1
,
0
]),
],
)
def
test_local_subtensor_of_transpose
(
original_fn
,
expected_fn
):
rng
=
np
.
random
.
default_rng
(
232
)
x
=
tensor
(
"x"
,
shape
=
(
7
,
5
,
3
))
x_test
=
rng
.
normal
(
size
=
x
.
type
.
shape
)
.
astype
(
x
.
dtype
)
out
=
original_fn
(
x
)
expected_opt_out
=
expected_fn
(
x
)
opt_out
=
rewrite_graph
(
out
)
assert
equal_computations
([
opt_out
],
[
expected_opt_out
]),
debugprint
(
[
expected_opt_out
,
opt_out
],
print_type
=
True
)
np
.
testing
.
assert_allclose
(
opt_out
.
eval
({
x
:
x_test
},
mode
=
NO_OPTIMIZATION_MODE
),
out
.
eval
({
x
:
x_test
},
mode
=
NO_OPTIMIZATION_MODE
),
)
def
test_local_subtensor_of_alloc
():
# DebugMode should detect if something goes wrong.
# test shape combination of odd and event shape.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论