Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a0f8effb
提交
a0f8effb
authored
8月 12, 2025
作者:
copilot-swe-agent[bot]
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace code snippet with minimal self-contained example for inplace_on_inputs
Co-authored-by:
ricardoV94
<
28983449+ricardoV94@users.noreply.github.com
>
上级
7d0ee2d8
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
60 行增加
和
4 行删除
+60
-4
inplace.rst
doc/extending/inplace.rst
+60
-4
没有找到文件。
doc/extending/inplace.rst
浏览文件 @
a0f8effb
...
@@ -219,12 +219,68 @@ those inputs where it is safe and beneficial to do so.
...
@@ -219,12 +219,68 @@ those inputs where it is safe and beneficial to do so.
.. testcode::
.. testcode::
def inplace_on_inputs(self, allowed_inplace_inputs: list[int]) -> "Op":
import numpy as np
"""Try to return a version of self that tries to inplace in as many as `allowed_inplace_inputs`."""
import pytensor
# Implementation would create a new Op with appropriate destroy_map
import pytensor.tensor as pt
# Return self by default if no inplace version is available
from pytensor.graph.basic import Apply
from pytensor.graph.op import Op
from pytensor.tensor.blockwise import Blockwise
class MyOpWithInplace(Op):
__props__ = ("destroy_a",)
def __init__(self, destroy_a):
self.destroy_a = destroy_a
if destroy_a:
self.destroy_map = {0: [0]}
def make_node(self, a):
return Apply(self, [a], [a.type()])
def perform(self, node, inputs, output_storage):
[a] = inputs
if not self.destroy_a:
a = a.copy()
a[0] += 1
output_storage[0][0] = a
def inplace_on_inputs(self, allowed_inplace_inputs):
if 0 in allowed_inplace_inputs:
return MyOpWithInplace(destroy_a=True)
return self
return self
a = pt.vector("a")
# Only Blockwise trigger inplace automatically for now
# Since the Blockwise isn't needed in this case, it will be removed after the inplace optimization
op = Blockwise(MyOpWithInplace(destroy_a=False), signature="(a)->(a)")
out = op(a)
# Give PyTensor permission to inplace on user provided inputs
fn = pytensor.function([pytensor.In(a, mutable=True)], out)
# Confirm that we have the inplace version of the Op
fn.dprint(print_destroy_map=True)
.. testoutput::
Blockwise{MyOpWithInplace{destroy_a=True}, (a)->(a)} [id A] '' 5
└─ a [id B]
The output shows that the function now uses the inplace version (`destroy_a=True`).
.. testcode::
# Test that inplace modification works
test_a = np.zeros(5)
result = fn(test_a)
print("Function result:", result)
print("Original array after function call:", test_a)
.. testoutput::
Function result: [1. 0. 0. 0. 0.]
Original array after function call: [1. 0. 0. 0. 0.]
Currently, this method is primarily used with Blockwise operations through PyTensor's
Currently, this method is primarily used with Blockwise operations through PyTensor's
rewriting system, but it will be extended to support core ops directly in future versions.
rewriting system, but it will be extended to support core ops directly in future versions.
The rewriting system automatically calls this method to optimize memory usage by
The rewriting system automatically calls this method to optimize memory usage by
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论