Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d9e8728a
提交
d9e8728a
authored
6月 23, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
7月 02, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Do not skip validation between consecutive Elemwise inplace replacements
上级
7d091be3
全部展开
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
60 行增加
和
34 行删除
+60
-34
destroyhandler.py
pytensor/graph/destroyhandler.py
+18
-13
blockwise.py
pytensor/tensor/rewriting/blockwise.py
+13
-21
elemwise.py
pytensor/tensor/rewriting/elemwise.py
+0
-0
test_elemwise.py
tests/tensor/rewriting/test_elemwise.py
+29
-0
没有找到文件。
pytensor/graph/destroyhandler.py
浏览文件 @
d9e8728a
...
...
@@ -7,7 +7,6 @@ and inplace operations.
import
itertools
from
collections
import
deque
import
pytensor
from
pytensor.configdefaults
import
config
from
pytensor.graph.basic
import
Constant
from
pytensor.graph.features
import
AlreadyThere
,
Bookkeeper
...
...
@@ -223,7 +222,7 @@ def _build_droot_impact(destroy_handler):
return
droot
,
impact
,
root_destroyer
def
fast_inplace_check
(
fgraph
,
inputs
):
def
inplace_candidates
(
fgraph
,
inputs
,
protected_inputs
=
None
):
"""
Return the variables in inputs that are possible candidate for as inputs of
inplace operation.
...
...
@@ -234,22 +233,28 @@ def fast_inplace_check(fgraph, inputs):
Inputs Variable that you want to use as inplace destination.
"""
Supervisor
=
pytensor
.
compile
.
function
.
types
.
Supervisor
protected_inputs
=
list
(
if
protected_inputs
is
None
:
from
pytensor.compile.function.types
import
Supervisor
protected_inputs
=
set
(
itertools
.
chain
.
from_iterable
(
f
.
protected
for
f
in
fgraph
.
_features
if
isinstance
(
f
,
Supervisor
)
)
)
protected_inputs
.
extend
(
fgraph
.
outputs
)
inputs
=
[
i
for
i
in
inputs
if
not
isinstance
(
i
,
Constant
)
and
not
fgraph
.
has_destroyers
([
i
])
and
i
not
in
protected_inputs
protected_inputs
.
update
(
fgraph
.
outputs
)
has_destroyers
=
fgraph
.
has_destroyers
return
[
inp
# Remove duplicates, while preserving order by using dict.fromkeys
for
inp
in
dict
.
fromkeys
(
inputs
)
if
(
not
isinstance
(
inp
,
Constant
)
and
inp
not
in
protected_inputs
and
not
has_destroyers
([
inp
])
)
]
return
inputs
class
DestroyHandler
(
Bookkeeper
):
...
...
pytensor/tensor/rewriting/blockwise.py
浏览文件 @
d9e8728a
import
itertools
from
pytensor.compile
import
Supervisor
from
pytensor.compile.mode
import
optdb
from
pytensor.graph
import
Constant
,
node_rewriter
from
pytensor.graph.destroyhandler
import
inplace_candidates
from
pytensor.graph.replace
import
vectorize_node
from
pytensor.graph.rewriting.basic
import
copy_stack_trace
,
in2out
,
out2in
from
pytensor.tensor.basic
import
Alloc
,
ARange
,
alloc
,
shape_padleft
...
...
@@ -274,25 +272,19 @@ def blockwise_inplace(fgraph, node):
batch_ndim
=
blockwise_op
.
batch_ndim
(
node
)
out_batch_bcast
=
node
.
outputs
[
0
]
.
type
.
broadcastable
[:
batch_ndim
]
protected_inputs
=
[
f
.
protected
for
f
in
fgraph
.
_features
if
isinstance
(
f
,
Supervisor
)
]
protected_inputs
=
list
(
itertools
.
chain
.
from_iterable
(
protected_inputs
))
protected_inputs
.
extend
(
fgraph
.
outputs
)
allowed_inplace_inputs
=
[
idx
for
idx
,
inp
in
enumerate
(
node
.
inputs
)
if
(
# Constants would need to be recreated every time if inplaced
not
isinstance
(
inp
,
Constant
)
# We can only inplace on inputs that are not being broadcasted
# As those are reused across iterations of Blockwise
and
node
.
inputs
[
idx
]
.
type
.
broadcastable
[:
batch_ndim
]
==
out_batch_bcast
# Inputs that are marked as protected or destroyed can't be inplaced
and
not
fgraph
.
has_destroyers
([
inp
])
and
inp
not
in
protected_inputs
inputs
=
node
.
inputs
candidate_inputs
=
set
(
inplace_candidates
(
fgraph
,
[
inp
for
inp
in
inputs
if
inp
.
type
.
broadcastable
[:
batch_ndim
]
==
out_batch_bcast
],
)
)
allowed_inplace_inputs
=
[
i
for
i
,
inp
in
enumerate
(
inputs
)
if
inp
in
candidate_inputs
]
if
not
allowed_inplace_inputs
:
...
...
pytensor/tensor/rewriting/elemwise.py
浏览文件 @
d9e8728a
差异被折叠。
点击展开。
tests/tensor/rewriting/test_elemwise.py
浏览文件 @
d9e8728a
...
...
@@ -8,6 +8,7 @@ from pytensor import In, shared
from
pytensor
import
scalar
as
ps
from
pytensor
import
tensor
as
pt
from
pytensor.compile.function
import
function
from
pytensor.compile.function.types
import
add_supervisor_to_fgraph
from
pytensor.compile.mode
import
Mode
,
get_default_mode
from
pytensor.configdefaults
import
config
from
pytensor.gradient
import
grad
...
...
@@ -1529,3 +1530,31 @@ def test_constant_fold_branches_add_mul(op):
new_out
=
rewrite_graph
(
out
,
include
=
(
"add_mul_fusion"
,))
assert
len
(
new_out
.
owner
.
inputs
)
==
3
assert
equal_computations
([
new_out
],
[
op
(
py_op
(
a
,
b
),
c
,
x
)])
def
test_InplaceElemwiseOptimizer_bug
():
# Regression test for https://github.com/pymc-devs/pytensor/issues/1420
# This graph fails if InplaceElemwiseOptimizer were to try to skip `fgraph.validate`
# in between two invalid inplace rewrites.
z
=
pt
.
matrix
(
"z"
)
z1
=
ps
.
float64
(
"z1"
)
z2
=
ps
.
float64
(
"z2"
)
out1
,
out2
=
Elemwise
(
ps
.
Composite
([
z1
,
z2
],
[
z1
+
z2
,
z2
-
z1
]))(
z
[
1
:],
z
[:
-
1
])
out
=
pt
.
exp
(
z
[
1
:
-
1
])
.
sum
()
+
out1
.
sum
()
+
out2
.
sum
()
# Add 500 unrelated nodes to trigger the old special behavior
irrelevant_outs
=
[
pt
.
specify_shape
(
z
,
(
4
,
4
))
for
_
in
range
(
500
)]
fgraph
=
FunctionGraph
(
inputs
=
[
z
],
outputs
=
[
out
,
*
irrelevant_outs
],
clone
=
False
)
add_supervisor_to_fgraph
(
fgraph
,
[
In
(
z
)])
# with config.change_flags(tensor__insert_inplace_optimizer_validate_nb=10):
rewrite_graph
(
fgraph
,
include
=
(
"inplace"
,))
pytensor
.
config
.
tensor__insert_inplace_optimizer_validate_nb
=
1
with
pytest
.
warns
(
FutureWarning
,
match
=
"tensor__insert_inplace_optimizer_validate_nb config is deprecated"
,
):
rewrite_graph
(
fgraph
,
include
=
(
"inplace"
,))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论