Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b2c62589
提交
b2c62589
authored
7月 05, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
10月 08, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Generalize and rename `local_reduce_chain`
上级
5b9c07ec
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
42 行增加
和
32 行删除
+42
-32
math.py
pytensor/tensor/rewriting/math.py
+42
-32
test_math.py
tests/tensor/rewriting/test_math.py
+0
-0
没有找到文件。
pytensor/tensor/rewriting/math.py
浏览文件 @
b2c62589
...
...
@@ -100,7 +100,11 @@ from pytensor.tensor.type import (
values_eq_approx_remove_inf_nan
,
values_eq_approx_remove_nan
,
)
from
pytensor.tensor.variable
import
TensorConstant
,
get_unique_constant_value
from
pytensor.tensor.variable
import
(
TensorConstant
,
TensorVariable
,
get_unique_constant_value
,
)
def
scalarconsts_rest
(
inputs
,
elemwise
=
True
,
only_process_constants
=
False
):
...
...
@@ -1575,42 +1579,48 @@ def local_sum_prod_all_to_none(fgraph, node):
@register_canonicalize
@node_rewriter
([
Sum
,
Prod
])
def
local_
op_of_op
(
fgraph
,
node
)
:
@node_rewriter
([
CAReduce
])
def
local_
reduce_chain
(
fgraph
,
node
)
->
list
[
TensorVariable
]
|
None
:
"""
Prod(Prod()) -> single Prod()
or
Sum(Sum()) -> single Sum()
or any CAReduce(Careduce(x)) of the same type
"""
op_type
=
Sum
if
isinstance
(
node
.
op
,
Sum
)
else
Prod
(
node_inps
,)
=
node
.
inputs
out_dtype
=
node
.
op
.
dtype
# This is done to make sure the rewrite doesn't affect other
# computations.
if
len
(
fgraph
.
clients
[
node_inps
])
==
1
:
if
node_inps
.
owner
and
(
isinstance
(
node_inps
.
owner
.
op
,
node
.
op
.
__class__
)):
# check to see either the inner or outer prod is doing a
# product over all axis, in which case we can remove it
if
node_inps
.
owner
.
op
.
axis
is
None
or
node
.
op
.
axis
is
None
:
return
[
op_type
(
None
,
dtype
=
out_dtype
)(
node_inps
.
owner
.
inputs
[
0
])]
# figure out which axes were in the original sum
newaxis
=
list
(
node_inps
.
owner
.
op
.
axis
)
for
i
in
node
.
op
.
axis
:
new_i
=
i
for
ii
in
node_inps
.
owner
.
op
.
axis
:
if
new_i
>=
ii
:
new_i
+=
1
assert
new_i
not
in
newaxis
newaxis
.
append
(
new_i
)
assert
len
(
newaxis
)
==
len
(
list
(
node_inps
.
owner
.
op
.
axis
)
+
list
(
node
.
op
.
axis
)
)
[
inner_reduce
]
=
node
.
inputs
if
not
(
inner_reduce
.
owner
and
isinstance
(
inner_reduce
.
owner
.
op
,
CAReduce
)):
return
None
# Don't apply rewrite if inner_reduce is used elsewhere
if
len
(
fgraph
.
clients
[
inner_reduce
])
>
1
:
return
None
# Check if CAReduces have the same scalar op
outer_op
:
CAReduce
=
node
.
op
inner_op
=
inner_reduce
.
owner
.
op
if
outer_op
.
scalar_op
!=
inner_op
.
scalar_op
:
return
None
combined
=
op_type
(
newaxis
,
dtype
=
out_dtype
)
return
[
combined
(
node_inps
.
owner
.
inputs
[
0
])]
outer_axis
=
outer_op
.
axis
inner_axis
=
inner_op
.
axis
[
x
]
=
inner_reduce
.
owner
.
inputs
# check to see either the inner or outer prod is doing a
# product over all axis, in which case we can remove it
if
outer_axis
is
None
or
inner_axis
is
None
:
return
[
outer_op
.
clone
(
axis
=
None
)(
x
)]
# Merge axis
newaxis
=
list
(
inner_axis
)
for
i
in
outer_axis
:
new_i
=
i
for
ii
in
inner_axis
:
if
new_i
>=
ii
:
new_i
+=
1
assert
new_i
not
in
newaxis
newaxis
.
append
(
new_i
)
assert
len
(
newaxis
)
==
len
(
inner_axis
)
+
len
(
outer_axis
)
return
[
outer_op
.
clone
(
axis
=
sorted
(
newaxis
))(
x
)]
@register_canonicalize
...
...
tests/tensor/rewriting/test_math.py
浏览文件 @
b2c62589
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论