Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
671cb44b
提交
671cb44b
authored
6月 30, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
7月 04, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix local_careduce_fusion rewrite
上级
6b189ee3
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
53 行增加
和
22 行删除
+53
-22
elemwise.py
pytensor/tensor/rewriting/elemwise.py
+31
-16
test_elemwise.py
tests/tensor/rewriting/test_elemwise.py
+22
-6
没有找到文件。
pytensor/tensor/rewriting/elemwise.py
浏览文件 @
671cb44b
...
...
@@ -1150,11 +1150,20 @@ def local_careduce_fusion(fgraph, node):
"""Fuse a `CAReduce` applied to an `Elemwise`."""
(
car_input
,)
=
node
.
inputs
car_scalar_op
=
node
.
op
.
scalar_op
# FIXME: This check is needed because of the faulty logic in the FIXME below!
# Right now, rewrite only works for `Sum`/`Prod`
if
not
isinstance
(
car_scalar_op
,
(
aes
.
Add
,
aes
.
Mul
)):
return
None
elm_node
=
car_input
.
owner
if
elm_node
is
None
or
not
isinstance
(
elm_node
.
op
,
Elemwise
):
return
False
elm_scalar_op
=
elm_node
.
op
.
scalar_op
elm_inputs
=
elm_node
.
inputs
elm_outputs
=
elm_node
.
outputs
...
...
@@ -1166,21 +1175,15 @@ def local_careduce_fusion(fgraph, node):
return
False
# Don't form the fusion when the target language is Python
elm_scalar_op
=
elm_node
.
op
.
scalar_op
car_scalar_op
=
node
.
op
.
scalar_op
if
get_target_language
()
==
(
"py"
,):
return
False
try
:
elm_scalar_op
.
c_code
(
elm_node
,
"test_presence_of_c_code"
,
[
"x"
for
x
in
elm_inputs
],
[
"z"
for
z
in
elm_outputs
],
{
"fail"
:
"
%(fail)
s"
},
)
if
not
elm_scalar_op
.
supports_c_code
(
elm_inputs
,
elm_outputs
):
return
None
# FIXME: This fails with Ops like `Max` whose `c_code` always expects two inputs!
# Should implement a `CAReduce.supports_c_code`?
try
:
car_scalar_op
.
c_code
(
node
,
"test_presence_of_c_code"
,
...
...
@@ -1191,18 +1194,24 @@ def local_careduce_fusion(fgraph, node):
except
(
NotImplementedError
,
MethodNotDefined
):
return
False
car_axis
=
node
.
op
.
axis
car_op
=
node
.
op
car_acc_dtype
=
node
.
op
.
acc_dtype
scalar_elm_inputs
=
[
aes
.
get_scalar_type
(
inp
.
type
.
dtype
)
.
make_variable
()
for
inp
in
elm_inputs
]
elm_output
=
elm_scalar_op
(
*
scalar_elm_inputs
)
# This input represents the previous value in the `CAReduce` binary reduction
carried_car_input
=
elm_output
.
type
()
scalar_fused_outputs
=
[
car_scalar_op
(
carried_car_input
,
elm_output
)]
carried_car_input
=
aes
.
get_scalar_type
(
car_acc_dtype
)
.
make_variable
()
scalar_fused_output
=
car_scalar_op
(
carried_car_input
,
elm_output
)
if
scalar_fused_output
.
type
.
dtype
!=
car_acc_dtype
:
scalar_fused_output
=
aes
.
cast
(
scalar_fused_output
,
car_acc_dtype
)
fused_scalar_op
=
aes
.
Composite
(
inputs
=
[
carried_car_input
]
+
scalar_elm_inputs
,
outputs
=
scalar_fused_outputs
inputs
=
[
carried_car_input
]
+
scalar_elm_inputs
,
outputs
=
[
scalar_fused_output
]
)
# The fused `Op` needs to look and behave like a `BinaryScalarOp`
...
...
@@ -1211,7 +1220,13 @@ def local_careduce_fusion(fgraph, node):
fused_scalar_op
.
nin
=
2
fused_scalar_op
.
nout
=
1
new_car_op
=
CAReduce
(
fused_scalar_op
,
car_axis
)
new_car_op
=
CAReduce
(
scalar_op
=
fused_scalar_op
,
axis
=
car_op
.
axis
,
acc_dtype
=
car_acc_dtype
,
dtype
=
car_op
.
dtype
,
upcast_discrete_output
=
car_op
.
upcast_discrete_output
,
)
return
[
new_car_op
(
*
elm_inputs
)]
...
...
tests/tensor/rewriting/test_elemwise.py
浏览文件 @
671cb44b
...
...
@@ -1177,8 +1177,24 @@ class TestFusion:
)
@pytest.mark.parametrize
(
"linker"
,
[
"cvm"
,
"py"
])
@pytest.mark.parametrize
(
"inp_dtype"
,
(
"floatX"
,
"int32"
))
@pytest.mark.parametrize
(
"axis"
,
[
None
,
0
,
1
,
(
0
,
1
),
(
0
,
1
,
2
)])
def
test_CAReduce_single_input
(
self
,
linker
,
axis
):
@pytest.mark.parametrize
(
"careduce_op, numpy_op"
,
[
(
at_sum
,
np
.
sum
),
pytest
.
param
(
at_all
,
np
.
all
,
marks
=
pytest
.
mark
.
xfail
(
reason
=
"Rewrite logic does not support all CAReduce"
),
),
],
)
def
test_CAReduce_single_input
(
self
,
linker
,
inp_dtype
,
axis
,
careduce_op
,
numpy_op
):
"""Make sure that `CAReduce` and `Elemwise` fusions work with a single input."""
mode
=
Mode
(
linker
=
linker
)
...
...
@@ -1188,8 +1204,8 @@ class TestFusion:
"inplace"
,
)
x
=
tensor
(
dtype
=
"floatX"
,
shape
=
(
None
,
None
,
None
),
name
=
"x"
)
out
=
exp
(
x
)
.
sum
(
axis
=
axis
)
x
=
tensor
(
dtype
=
inp_dtype
,
shape
=
(
None
,
None
,
None
),
name
=
"x"
)
out
=
careduce_op
(
exp
(
x
),
axis
=
axis
)
out_fn
=
function
([
x
],
out
,
mode
=
mode
)
...
...
@@ -1198,9 +1214,9 @@ class TestFusion:
assert
isinstance
(
getattr
(
out_node
.
op
,
"scalar_op"
),
aes
.
basic
.
Composite
)
rng
=
np
.
random
.
default_rng
(
2320
)
x_val
=
rng
.
random
((
4
,
3
,
2
)
,
dtype
=
config
.
floatX
)
x_val
=
rng
.
random
((
4
,
3
,
2
)
)
.
astype
(
x
.
type
.
dtype
)
exp_res
=
n
p
.
exp
(
x_val
)
.
sum
(
axis
=
axis
)
exp_res
=
n
umpy_op
(
np
.
exp
(
x_val
),
axis
=
axis
)
out_val
=
out_fn
(
x_val
)
assert
out_val
.
shape
==
exp_res
.
shape
...
...
@@ -1216,7 +1232,7 @@ class TestFusion:
# `Elemwise`s with more than one client shouldn't be rewritten
x
=
tensor
(
dtype
=
"floatX"
,
shape
=
(
None
,
None
,
None
),
name
=
"x"
)
exp_x
=
exp
(
x
)
out
=
exp_x
.
sum
(
axis
=
axis
)
+
exp
(
x
)
out
=
careduce_op
(
exp_x
,
axis
=
axis
)
+
exp
(
x
)
out_fn
=
function
([
x
],
out
,
mode
=
mode
)
out_nodes
=
out_fn
.
maker
.
fgraph
.
toposort
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论