Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4d539fa5
提交
4d539fa5
authored
7月 14, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
7月 15, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Canonicalize subtensor negative integer indices
上级
617964ff
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
56 行增加
和
1 行删除
+56
-1
subtensor.py
pytensor/tensor/rewriting/subtensor.py
+38
-0
test_subtensor.py
tests/tensor/rewriting/test_subtensor.py
+17
-0
test_subtensor_lift.py
tests/tensor/rewriting/test_subtensor_lift.py
+1
-1
没有找到文件。
pytensor/tensor/rewriting/subtensor.py
浏览文件 @
4d539fa5
...
...
@@ -724,6 +724,44 @@ def local_useless_subtensor(fgraph, node):
return
[
node
.
inputs
[
0
]]
@register_canonicalize
@node_rewriter
([
Subtensor
])
def
local_convert_negative_indices
(
fgraph
,
node
):
"""Convert negative indices in `Subtensor` with static length to positive indices."""
x
,
*
raw_idxs
=
node
.
inputs
idxs
=
indices_from_subtensor
(
raw_idxs
,
node
.
op
.
idx_list
)
new_idxs
=
None
for
i
,
(
dim_length
,
idx
)
in
enumerate
(
zip
(
x
.
type
.
shape
,
idxs
)):
if
(
dim_length
is
None
or
isinstance
(
idx
,
slice
)
or
not
isinstance
(
idx
,
Constant
)
):
continue
val
=
idx
.
data
if
val
>=
0
:
continue
new_val
=
val
+
dim_length
if
new_val
<
0
:
# This is an invalid index, keep original to not confuse the user
return
None
if
new_idxs
is
None
:
new_idxs
=
list
(
idxs
)
new_idxs
[
i
]
=
new_val
if
new_idxs
is
None
:
# No negative indices to convert
return
None
new_subtensor
=
x
[
tuple
(
new_idxs
)]
copy_stack_trace
(
node
.
outputs
,
new_subtensor
)
return
[
new_subtensor
]
@register_canonicalize
@register_specialize
@node_rewriter
([
AdvancedSubtensor1
])
...
...
tests/tensor/rewriting/test_subtensor.py
浏览文件 @
4d539fa5
...
...
@@ -1992,3 +1992,20 @@ def test_extract_diag_of_diagonal_set_subtensor():
expected_outs
.
append
(
outs
[
-
1
])
assert
equal_computations
(
rewritten_outs
,
expected_outs
)
def
test_local_convert_negative_indices
():
x
=
pt
.
tensor
(
"x"
,
shape
=
(
None
,
3
,
1
))
# Dim length is unknown rewrite can't be applied
rewritten_out
=
rewrite_graph
(
x
[
-
2
])
assert
equal_computations
([
rewritten_out
],
[
x
[
-
2
]])
# Rewrite applies
rewritten_out
=
rewrite_graph
(
x
[:,
-
2
])
assert
equal_computations
([
rewritten_out
],
[
x
[:,
1
]])
# Rewrite doesn't apply because index is invalid
# TODO: If Subtensor decides to raise on make_node, this test can be removed
rewritten_out
=
rewrite_graph
(
x
[:,
:,
-
2
])
assert
equal_computations
([
rewritten_out
],
[
x
[:,
:,
-
2
]])
tests/tensor/rewriting/test_subtensor_lift.py
浏览文件 @
4d539fa5
...
...
@@ -202,7 +202,7 @@ def test_local_subtensor_of_reduce(original_fn, expected_fn):
out
=
original_fn
(
x
)
expected_opt_out
=
expected_fn
(
x
)
opt_out
=
rewrite_graph
(
out
)
opt_out
=
rewrite_graph
(
out
,
exclude
=
(
"local_convert_negative_indices"
,)
)
assert
equal_computations
([
opt_out
],
[
expected_opt_out
]),
debugprint
(
[
expected_opt_out
,
opt_out
],
print_type
=
True
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论