Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bbf937c3
提交
bbf937c3
authored
8月 06, 2021
作者:
Ricardo
提交者:
Rémi Louf
10月 18, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add rewrite for addition with negation `x + (-y) -> x - y`
* Also reverses test expectation for `local_expm1` rewrite, as previously missed case is now detected
上级
12477108
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
75 行增加
和
3 行删除
+75
-3
math.py
aesara/tensor/rewriting/math.py
+29
-0
test_math.py
tests/tensor/rewriting/test_math.py
+46
-3
没有找到文件。
aesara/tensor/rewriting/math.py
浏览文件 @
bbf937c3
...
@@ -1806,6 +1806,35 @@ def local_sub_neg_to_add(fgraph, node):
...
@@ -1806,6 +1806,35 @@ def local_sub_neg_to_add(fgraph, node):
return
[
new_out
]
return
[
new_out
]
@register_specialize
@node_rewriter
([
add
])
def
local_add_neg_to_sub
(
fgraph
,
node
):
"""
-x + y -> y - x
x + (-y) -> x - y
"""
# This rewrite is only registered during specialization, because the
# `local_neg_to_mul` rewrite modifies the relevant pattern during canonicalization
# Rewrite is only applicable when there are two inputs to add
if
node
.
op
==
add
and
len
(
node
.
inputs
)
==
2
:
# Look for pattern with either input order
for
first
,
second
in
(
node
.
inputs
,
reversed
(
node
.
inputs
)):
if
second
.
owner
:
if
second
.
owner
.
op
==
neg
:
pre_neg
=
second
.
owner
.
inputs
[
0
]
new_out
=
sub
(
first
,
pre_neg
)
return
[
new_out
]
# Check if it is a negative constant
const
=
get_constant
(
second
)
if
const
is
not
None
and
const
<
0
:
new_out
=
sub
(
first
,
np
.
abs
(
const
))
return
[
new_out
]
@register_canonicalize
@register_canonicalize
@node_rewriter
([
mul
])
@node_rewriter
([
mul
])
def
local_mul_zero
(
fgraph
,
node
):
def
local_mul_zero
(
fgraph
,
node
):
...
...
tests/tensor/rewriting/test_math.py
浏览文件 @
bbf937c3
...
@@ -4042,9 +4042,14 @@ def test_local_expm1():
...
@@ -4042,9 +4042,14 @@ def test_local_expm1():
for
n
in
h
.
maker
.
fgraph
.
toposort
()
for
n
in
h
.
maker
.
fgraph
.
toposort
()
)
)
assert
not
any
(
# This rewrite works when `local_add_neg_to_sub` specialization rewrite is invoked
isinstance
(
n
.
op
,
Elemwise
)
and
isinstance
(
n
.
op
.
scalar_op
,
aes
.
basic
.
Expm1
)
expect_rewrite
=
config
.
mode
!=
"FAST_COMPILE"
for
n
in
r
.
maker
.
fgraph
.
toposort
()
assert
(
any
(
isinstance
(
n
.
op
,
Elemwise
)
and
isinstance
(
n
.
op
.
scalar_op
,
aes
.
basic
.
Expm1
)
for
n
in
r
.
maker
.
fgraph
.
toposort
()
)
==
expect_rewrite
)
)
...
@@ -4654,3 +4659,41 @@ def test_local_sub_neg_to_add_const():
...
@@ -4654,3 +4659,41 @@ def test_local_sub_neg_to_add_const():
x_test
=
np
.
array
([
3
,
4
],
dtype
=
config
.
floatX
)
x_test
=
np
.
array
([
3
,
4
],
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
x_test
),
x_test
-
(
-
const
))
assert
np
.
allclose
(
f
(
x_test
),
x_test
-
(
-
const
))
@pytest.mark.parametrize
(
"first_negative"
,
(
True
,
False
))
def
test_local_add_neg_to_sub
(
first_negative
):
x
=
scalar
(
"x"
)
y
=
vector
(
"y"
)
out
=
-
x
+
y
if
first_negative
else
x
+
(
-
y
)
f
=
function
([
x
,
y
],
out
,
mode
=
Mode
(
"py"
))
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
not
isinstance
(
node
.
op
,
DimShuffle
)
]
assert
nodes
==
[
at
.
sub
]
x_test
=
np
.
full
((),
1.0
,
dtype
=
config
.
floatX
)
y_test
=
np
.
full
(
5
,
2.0
,
dtype
=
config
.
floatX
)
exp
=
-
x_test
+
y_test
if
first_negative
else
x_test
+
(
-
y_test
)
assert
np
.
allclose
(
f
(
x_test
,
y_test
),
exp
)
def
test_local_add_neg_to_sub_const
():
x
=
vector
(
"x"
)
const
=
5.0
f
=
function
([
x
],
x
+
(
-
const
),
mode
=
Mode
(
"py"
))
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
not
isinstance
(
node
.
op
,
DimShuffle
)
]
assert
nodes
==
[
at
.
sub
]
x_test
=
np
.
array
([
3
,
4
],
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
x_test
),
x_test
+
(
-
const
))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论