Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7d0ee2d8
提交
7d0ee2d8
authored
8月 09, 2025
作者:
copilot-swe-agent[bot]
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Document inplace_on_inputs method and remove TODO comment
Co-authored-by:
ricardoV94
<
28983449+ricardoV94@users.noreply.github.com
>
上级
af38ad9b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
39 行增加
和
1 行删除
+39
-1
inplace.rst
doc/extending/inplace.rst
+39
-0
op.py
pytensor/graph/op.py
+0
-1
没有找到文件。
doc/extending/inplace.rst
浏览文件 @
7d0ee2d8
...
@@ -206,6 +206,45 @@ input(s)'s memory). From there, go to the previous section.
...
@@ -206,6 +206,45 @@ input(s)'s memory). From there, go to the previous section.
Consider using :class:`DebugMode` when developing
Consider using :class:`DebugMode` when developing
a new :class:`Op` that uses :attr:`Op.view_map` and/or :attr:`Op.destroy_map`.
a new :class:`Op` that uses :attr:`Op.view_map` and/or :attr:`Op.destroy_map`.
The `inplace_on_inputs` Method
==============================
PyTensor provides a method :meth:`Op.inplace_on_inputs` that allows an `Op` to
create a version of itself that operates inplace on as many of the requested
inputs as possible while avoiding inplace operations on non-requested inputs.
This method takes a list of input indices where inplace operations are allowed
and returns a new `Op` instance that will perform inplace operations only on
those inputs where it is safe and beneficial to do so.
.. testcode::
def inplace_on_inputs(self, allowed_inplace_inputs: list[int]) -> "Op":
"""Try to return a version of self that tries to inplace in as many as `allowed_inplace_inputs`."""
# Implementation would create a new Op with appropriate destroy_map
# Return self by default if no inplace version is available
return self
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.
The rewriting system automatically calls this method to optimize memory usage by
enabling inplace operations where they do not interfere with the computation graph's
correctness.
When implementing this method in a custom `Op`:
- Return a new instance of your `Op` with a :attr:`destroy_map` that reflects
the inplace operations on the allowed inputs
- Ensure that inplace operations are only performed on inputs that are in the
`allowed_inplace_inputs` list
- Return `self` if no inplace optimization is possible or beneficial
- The returned `Op` should be functionally equivalent to the original but with
better memory efficiency
.. note::
This method is automatically used by PyTensor's optimization pipeline and typically
does not need to be called directly by user code.
Inplace Rewriting and `DebugMode`
Inplace Rewriting and `DebugMode`
=================================
=================================
...
...
pytensor/graph/op.py
浏览文件 @
7d0ee2d8
...
@@ -605,7 +605,6 @@ class Op(MetaObject):
...
@@ -605,7 +605,6 @@ class Op(MetaObject):
def
inplace_on_inputs
(
self
,
allowed_inplace_inputs
:
list
[
int
])
->
"Op"
:
def
inplace_on_inputs
(
self
,
allowed_inplace_inputs
:
list
[
int
])
->
"Op"
:
"""Try to return a version of self that tries to inplace in as many as `allowed_inplace_inputs`."""
"""Try to return a version of self that tries to inplace in as many as `allowed_inplace_inputs`."""
# TODO: Document this in the Create your own Op docs
# By default, do nothing
# By default, do nothing
return
self
return
self
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论