Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ba5336f6
提交
ba5336f6
authored
3月 06, 2026
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
3月 06, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Graph replace: Handle redundant replacements
上级
5946b4de
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
29 行增加
和
5 行删除
+29
-5
replace.py
pytensor/graph/replace.py
+10
-5
test_replace.py
tests/graph/test_replace.py
+19
-0
没有找到文件。
pytensor/graph/replace.py
浏览文件 @
ba5336f6
import
warnings
from
collections.abc
import
Iterable
,
Mapping
,
Sequence
from
functools
import
partial
,
singledispatch
from
functools
import
singledispatch
from
typing
import
cast
,
overload
from
pytensor.graph.basic
import
(
...
...
@@ -169,6 +169,9 @@ def graph_replace(
fg_replace
=
{
equiv
[
c
]:
c
for
c
in
conditions
}
# add the replacements on top of input mappings
fg_replace
.
update
({
equiv
[
r
]:
v
for
r
,
v
in
replace_dict
.
items
()
if
r
in
equiv
})
# Filter out replacements whose keys are not in the FunctionGraph
# This can happen when a replacement makes an ancestor replacement redundant
fg_replace
=
{
k
:
v
for
k
,
v
in
fg_replace
.
items
()
if
k
in
fg
.
variables
}
# replacements have to be done in reverse topological order so that nested
# expressions get recursively replaced correctly
...
...
@@ -183,11 +186,13 @@ def graph_replace(
toposort
=
fg
.
toposort
()
def
toposort_key
(
fg
:
FunctionGraph
,
ts
:
list
[
Apply
],
pair
:
tuple
[
Variable
,
Variable
]
pair
:
tuple
[
Variable
,
Variable
],
toposort
=
toposort
,
fg
=
fg
,
)
->
int
:
key
,
_
=
pair
if
key
.
owner
is
not
None
:
return
t
s
.
index
(
key
.
owner
)
if
(
node
:
=
key
.
owner
)
is
not
None
:
return
t
oposort
.
index
(
node
)
# type: ignore[no-any-return]
else
:
if
key
in
fg
.
variables
:
return
-
1
...
...
@@ -197,7 +202,7 @@ def graph_replace(
sorted_replacements
=
sorted
(
fg_replace
.
items
(),
# sort based on the fg toposort, if a variable has no owner, it goes first
key
=
partial
(
toposort_key
,
fg
,
toposort
)
,
key
=
toposort_key
,
reverse
=
True
,
)
fg
.
replace_all
(
sorted_replacements
,
import_missing
=
True
)
...
...
tests/graph/test_replace.py
浏览文件 @
ba5336f6
...
...
@@ -15,6 +15,7 @@ from pytensor.graph.traversal import graph_inputs
from
pytensor.tensor
import
dvector
,
fvector
,
vector
from
tests
import
unittest_tools
as
utt
from
tests.graph.utils
import
MyOp
,
MyVariable
,
op_multiple_outputs
from
tests.unittest_tools
import
assert_equal_computations
class
TestCloneReplace
:
...
...
@@ -233,6 +234,24 @@ class TestGraphReplace:
with
pytest
.
raises
(
ValueError
,
match
=
"Some replacements were not used"
):
graph_replace
([
out
],
{
fake
:
x
.
clone
()},
strict
=
True
)
def
test_replace_var_and_ancestor
(
self
):
"""Replacing both a variable and its ancestor should not crash.
When x depends on a and y only depends on a through x,
replacing both x and a should work: x->xx makes a->aa a no-op.
"""
op
=
MyOp
(
"op"
)
a
=
MyVariable
(
"a"
)
x
=
op
(
a
)
# x depends on a
y
=
op
(
x
)
# y depends on x (and transitively on a)
new_a
=
MyVariable
(
"new_a"
)
new_x
=
MyVariable
(
"new_x"
)
[
new_y
]
=
graph_replace
([
y
],
{
a
:
new_a
,
x
:
new_x
})
assert
new_y
.
owner
.
inputs
[
0
]
is
new_x
assert_equal_computations
([
new_y
],
[
op
(
new_x
)])
class
TestVectorizeGraph
:
def
test_basic
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论