Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4273eb87
提交
4273eb87
authored
3月 04, 2026
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
3月 10, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Inline constants and merge duplicate inputs in OptimizeOp
上级
2bb2ec6d
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
140 行增加
和
3 行删除
+140
-3
optimize.py
pytensor/tensor/optimize.py
+9
-3
__init__.py
pytensor/tensor/rewriting/__init__.py
+1
-0
optimize.py
pytensor/tensor/rewriting/optimize.py
+48
-0
test_optimize.py
tests/tensor/rewriting/test_optimize.py
+82
-0
没有找到文件。
pytensor/tensor/optimize.py
浏览文件 @
4273eb87
...
...
@@ -199,10 +199,16 @@ class ScipyWrapperOp(Op, HasInnerGraph):
def
inner_outputs
(
self
):
return
self
.
fgraph
.
outputs
def
clone_with_new_fgraph
(
self
,
fgraph
):
clone_op
=
copy
(
self
)
clone_op
.
_fn
=
None
clone_op
.
_fn_wrapped
=
None
clone_op
.
fgraph
=
fgraph
return
clone_op
def
clone
(
self
):
copy_op
=
copy
(
self
)
copy_op
.
fgraph
=
self
.
fgraph
.
clone
(
clone_inner_graphs
=
True
)
return
copy_op
clone_fgraph
=
self
.
fgraph
.
clone
(
clone_inner_graphs
=
True
)
return
self
.
clone_with_new_fgraph
(
clone_fgraph
)
def
prepare_node
(
self
,
...
...
pytensor/tensor/rewriting/__init__.py
浏览文件 @
4273eb87
...
...
@@ -10,6 +10,7 @@ import pytensor.tensor.rewriting.linalg
import
pytensor.tensor.rewriting.math
import
pytensor.tensor.rewriting.numba
import
pytensor.tensor.rewriting.ofg
import
pytensor.tensor.rewriting.optimize
import
pytensor.tensor.rewriting.reshape
import
pytensor.tensor.rewriting.shape
import
pytensor.tensor.rewriting.special
...
...
pytensor/tensor/rewriting/optimize.py
0 → 100644
浏览文件 @
4273eb87
from
pytensor.graph.basic
import
Constant
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.graph.replace
import
clone_replace
from
pytensor.graph.rewriting.basic
import
node_rewriter
from
pytensor.tensor.optimize
import
ScipyWrapperOp
from
pytensor.tensor.rewriting.basic
import
register_canonicalize
@register_canonicalize
@node_rewriter
([
ScipyWrapperOp
])
def
remove_constants_and_duplicate_inputs_scipy
(
fgraph
,
node
):
"""Inline constants and remove duplicate inputs from ScipyWrapperOp nodes.
Constants in the outer graph are free symbolic variables in the inner graph.
Moving them into the inner graph enables constant-folding. Duplicate outer
inputs can share a single inner variable.
Only args (inputs[1:]) are candidates — inputs[0] is always the
optimization variable x.
"""
op
:
ScipyWrapperOp
=
node
.
op
inner_x
,
*
inner_args
=
op
.
inner_inputs
outer_x
,
*
outer_args
=
list
(
node
.
inputs
)
givens
=
{}
new_inner_args
=
[]
new_outer_args
=
[]
for
inner_in
,
outer_in
in
zip
(
inner_args
,
outer_args
):
if
isinstance
(
outer_in
,
Constant
):
givens
[
inner_in
]
=
outer_in
elif
outer_in
in
new_outer_args
:
# De-duplicate outer variable
idx
=
new_outer_args
.
index
(
outer_in
)
givens
[
inner_in
]
=
new_inner_args
[
idx
]
else
:
new_inner_args
.
append
(
inner_in
)
new_outer_args
.
append
(
outer_in
)
if
not
givens
:
return
None
new_inner_outputs
=
clone_replace
(
op
.
inner_outputs
,
replace
=
givens
)
new_inner_inputs
=
(
inner_x
,
*
new_inner_args
)
new_fgraph
=
FunctionGraph
(
new_inner_inputs
,
new_inner_outputs
,
clone
=
False
)
new_op
=
op
.
clone_with_new_fgraph
(
new_fgraph
)
new_outer_inputs
=
(
outer_x
,
*
new_outer_args
)
return
new_op
.
make_node
(
*
new_outer_inputs
)
.
outputs
tests/tensor/rewriting/test_optimize.py
0 → 100644
浏览文件 @
4273eb87
import
numpy
as
np
import
pytensor.tensor
as
pt
from
pytensor
import
function
from
pytensor.tensor.optimize
import
MinimizeOp
,
ScipyWrapperOp
def
test_inline_constants
():
"""Constants passed as args should be inlined into the inner graph."""
x
=
pt
.
scalar
(
"x"
)
a
=
pt
.
scalar
(
"a"
)
b
=
pt
.
scalar
(
"b"
,
dtype
=
int
)
c
=
pt
.
scalar
(
"c"
)
objective
=
(
x
-
c
*
a
)
**
b
minimize_op
=
MinimizeOp
(
x
,
a
,
b
,
c
,
objective
=
objective
,
method
=
"BFGS"
,
)
two_float
=
pt
.
full
((),
2.0
,
dtype
=
a
.
dtype
)
two_int
=
two_float
.
astype
(
b
.
dtype
)
minimize_node
=
minimize_op
.
make_node
(
x
,
two_float
,
two_int
,
c
)
assert
len
(
minimize_node
.
inputs
)
==
4
f
=
function
([
x
,
c
],
minimize_node
.
outputs
)
# Check the two constants are inlined
[
minimize_node
]
=
[
node
for
node
in
f
.
maker
.
fgraph
.
apply_nodes
if
isinstance
(
node
.
op
,
ScipyWrapperOp
)
]
assert
len
(
minimize_node
.
inputs
)
==
2
# Check correctness
c_val
=
3.0
minimized_x_val
,
success_val
=
f
(
np
.
pi
,
c_val
)
assert
success_val
np
.
testing
.
assert_allclose
(
minimized_x_val
,
2
*
c_val
,
)
def
test_remove_duplicate_inputs
(
new_minimize_outs
=
None
):
"""Duplicate outer inputs should be deduplicated."""
x
=
pt
.
scalar
(
"x"
)
a
=
pt
.
scalar
(
"a"
)
b
=
pt
.
scalar
(
"b"
)
objective
=
(
x
+
a
)
**
2
+
(
x
-
b
)
**
2
minimize_op
=
MinimizeOp
(
x
,
a
,
b
,
objective
=
objective
,
method
=
"BFGS"
,
)
# Use same outer variable for both a, b
c
=
pt
.
scalar
(
"c"
)
minimized_node
=
minimize_op
.
make_node
(
x
,
c
,
c
)
assert
len
(
minimized_node
.
inputs
)
==
3
f
=
function
([
x
,
c
],
minimized_node
.
outputs
)
[
minimize_node
]
=
[
node
for
node
in
f
.
maker
.
fgraph
.
apply_nodes
if
isinstance
(
node
.
op
,
ScipyWrapperOp
)
]
assert
len
(
minimize_node
.
inputs
)
==
2
# Check correctness: minimum of (x+a)^2 + (x-a)^2 = 2x^2 + 2a^2 is at x=0
minimized_x_val
,
success_val
=
f
(
np
.
pi
,
np
.
e
)
assert
success_val
np
.
testing
.
assert_allclose
(
minimized_x_val
,
0.0
,
atol
=
1e-8
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论