Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
34b91eff
提交
34b91eff
authored
3月 25, 2025
作者:
ricardoV94
提交者:
Ricardo Vieira
4月 08, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow inplace of Elemwise Composite with multiple outputs
上级
0699b48d
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
24 行增加
和
25 行删除
+24
-25
basic.py
pytensor/scalar/basic.py
+17
-16
elemwise.py
pytensor/tensor/rewriting/elemwise.py
+0
-2
test_elemwise.py
tests/tensor/rewriting/test_elemwise.py
+7
-7
没有找到文件。
pytensor/scalar/basic.py
浏览文件 @
34b91eff
...
@@ -4441,16 +4441,12 @@ class Composite(ScalarInnerGraphOp):
...
@@ -4441,16 +4441,12 @@ class Composite(ScalarInnerGraphOp):
if
hasattr
(
self
,
"_c_code"
):
if
hasattr
(
self
,
"_c_code"
):
return
self
.
_c_code
return
self
.
_c_code
subd
=
dict
(
fg
=
self
.
fgraph
chain
(
subd
=
{
e
:
f
"
%
(i{int(i)})s"
for
i
,
e
in
enumerate
(
fg
.
inputs
)}
((
e
,
f
"
%
(i{int(i)})s"
)
for
i
,
e
in
enumerate
(
self
.
fgraph
.
inputs
)),
((
e
,
f
"
%
(o{int(i)})s"
)
for
i
,
e
in
enumerate
(
self
.
fgraph
.
outputs
)),
)
)
for
var
in
self
.
fgraph
.
variables
:
for
var
in
fg
.
variables
:
if
var
.
owner
is
None
:
if
var
.
owner
is
None
:
if
var
not
in
self
.
fgraph
.
inputs
:
if
var
not
in
fg
.
inputs
:
# This is an orphan
# This is an orphan
if
isinstance
(
var
,
Constant
)
and
isinstance
(
var
.
type
,
CLinkerType
):
if
isinstance
(
var
,
Constant
)
and
isinstance
(
var
.
type
,
CLinkerType
):
subd
[
var
]
=
f
"({var.type.c_literal(var.data)})"
subd
[
var
]
=
f
"({var.type.c_literal(var.data)})"
...
@@ -4465,23 +4461,24 @@ class Composite(ScalarInnerGraphOp):
...
@@ -4465,23 +4461,24 @@ class Composite(ScalarInnerGraphOp):
# flag for elemwise ops to check.
# flag for elemwise ops to check.
self
.
inner_float16
=
True
self
.
inner_float16
=
True
_c_code
=
"{
\n
"
self
.
nodenames
=
nodenames
=
[]
# Used by self.c_support_code_apply
self
.
nodenames
=
[
f
"
%(nodename)
s_subnode{int(j)}"
for
j
,
n
in
enumerate
(
self
.
fgraph
.
toposort
())
]
_c_code
=
"{
\n
"
i
=
0
i
=
0
for
j
,
node
in
enumerate
(
self
.
fgraph
.
toposort
()):
for
j
,
node
in
enumerate
(
fg
.
toposort
()):
for
output
in
node
.
outputs
:
for
output
in
node
.
outputs
:
if
output
not
in
subd
:
if
output
not
in
subd
:
i
+=
1
i
+=
1
name
=
f
"V
%(id)
s_tmp{int(i)}"
name
=
f
"V
%(id)
s_tmp{int(i)}"
subd
[
output
]
=
name
subd
[
output
]
=
name
_c_code
+=
f
"{output.type.dtype_specs()[1]} {name};
\n
"
_c_code
+=
f
"{output.type.dtype_specs()[1]} {name};
\n
"
nodename
=
f
"
%(nodename)
s_subnode{int(j)}"
nodenames
.
append
(
nodename
)
s
=
node
.
op
.
c_code
(
s
=
node
.
op
.
c_code
(
node
,
node
,
self
.
nodenames
[
j
]
,
nodename
,
[
subd
[
input
]
for
input
in
node
.
inputs
],
[
subd
[
input
]
for
input
in
node
.
inputs
],
[
subd
[
output
]
for
output
in
node
.
outputs
],
[
subd
[
output
]
for
output
in
node
.
outputs
],
dict
(
fail
=
"
%(fail)
s"
,
id
=
f
"
%(id)
s_{int(j)}"
),
dict
(
fail
=
"
%(fail)
s"
,
id
=
f
"
%(id)
s_{int(j)}"
),
...
@@ -4489,6 +4486,10 @@ class Composite(ScalarInnerGraphOp):
...
@@ -4489,6 +4486,10 @@ class Composite(ScalarInnerGraphOp):
_c_code
+=
s
_c_code
+=
s
_c_code
+=
"
\n
"
_c_code
+=
"
\n
"
# Copy the temporary outputs to the real outputs
for
i
,
output
in
enumerate
(
fg
.
outputs
):
_c_code
+=
f
"
%
(o{int(i)})s = {subd[output]};
\n
"
_c_code
+=
"}
\n
"
_c_code
+=
"}
\n
"
self
.
_c_code
=
_c_code
self
.
_c_code
=
_c_code
...
@@ -4512,7 +4513,7 @@ class Composite(ScalarInnerGraphOp):
...
@@ -4512,7 +4513,7 @@ class Composite(ScalarInnerGraphOp):
return
self
.
c_code_template
%
d
return
self
.
c_code_template
%
d
def
c_code_cache_version_outer
(
self
)
->
tuple
[
int
,
...
]:
def
c_code_cache_version_outer
(
self
)
->
tuple
[
int
,
...
]:
return
(
5
,)
return
(
6
,)
class
Compositef32
:
class
Compositef32
:
...
...
pytensor/tensor/rewriting/elemwise.py
浏览文件 @
34b91eff
...
@@ -80,8 +80,6 @@ class InplaceElemwiseOptimizer(GraphRewriter):
...
@@ -80,8 +80,6 @@ class InplaceElemwiseOptimizer(GraphRewriter):
# and ScalarLoops
# and ScalarLoops
if
isinstance
(
node
.
op
.
scalar_op
,
ScalarLoop
):
if
isinstance
(
node
.
op
.
scalar_op
,
ScalarLoop
):
return
[]
return
[]
if
isinstance
(
node
.
op
.
scalar_op
,
ps
.
Composite
)
and
(
len
(
node
.
outputs
)
>
1
):
return
[]
else
:
else
:
return
range
(
len
(
node
.
outputs
))
return
range
(
len
(
node
.
outputs
))
...
...
tests/tensor/rewriting/test_elemwise.py
浏览文件 @
34b91eff
...
@@ -1104,7 +1104,8 @@ class TestFusion:
...
@@ -1104,7 +1104,8 @@ class TestFusion:
np
.
random
.
random
((
5
,
5
)),
np
.
random
.
random
((
5
,
5
)),
np
.
random
.
random
((
5
,
5
))
np
.
random
.
random
((
5
,
5
)),
np
.
random
.
random
((
5
,
5
)),
np
.
random
.
random
((
5
,
5
))
)
)
def
test_fusion_multiout_inplace
(
self
):
@pytest.mark.parametrize
(
"linker"
,
[
"cvm"
,
"py"
])
def
test_fusion_multiout_inplace
(
self
,
linker
):
x
=
vector
(
"x"
)
x
=
vector
(
"x"
)
# Create Composite where inplacing the first non-constant output would corrupt the second output
# Create Composite where inplacing the first non-constant output would corrupt the second output
...
@@ -1118,17 +1119,16 @@ class TestFusion:
...
@@ -1118,17 +1119,16 @@ class TestFusion:
f
=
pytensor
.
function
(
f
=
pytensor
.
function
(
[
In
(
x
,
mutable
=
True
)],
[
In
(
x
,
mutable
=
True
)],
outs
,
outs
,
mode
=
self
.
mode
.
including
(
"inplace"
),
mode
=
Mode
(
linker
=
linker
,
optimizer
=
self
.
rewrites
.
including
(
"inplace"
)
),
)
)
(
composite_node
,)
=
f
.
maker
.
fgraph
.
apply_nodes
(
composite_node
,)
=
f
.
maker
.
fgraph
.
apply_nodes
# Destroy map must be None or the last toposorted output
destroy_map
=
composite_node
.
op
.
destroy_map
destroy_map
=
composite_node
.
op
.
destroy_map
assert
(
destroy_map
==
{})
or
(
assert
destroy_map
==
{
0
:
[
0
]}
destroy_map
==
{
1
:
[
composite_node
.
inputs
.
index
(
x
)]}
)
res
=
f
([
0
,
1
,
2
])
inp
=
np
.
array
([
0
,
1
,
2
],
dtype
=
config
.
floatX
)
res
=
f
(
inp
)
assert
not
np
.
allclose
(
inp
,
[
0
,
1
,
2
])
assert
np
.
allclose
(
res
[
0
],
[
1
,
2
,
3
])
assert
np
.
allclose
(
res
[
0
],
[
1
,
2
,
3
])
assert
np
.
allclose
(
res
[
1
],
np
.
cos
([
1
,
2
,
3
])
+
np
.
array
([
0
,
1
,
2
]))
assert
np
.
allclose
(
res
[
1
],
np
.
cos
([
1
,
2
,
3
])
+
np
.
array
([
0
,
1
,
2
]))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论