Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
cff058c9
提交
cff058c9
authored
12月 03, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
1月 13, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Do not apply `local_add_neg_to_sub` rewrite if negative variabe is a constant
上级
04ce1c6c
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
48 行增加
和
52 行删除
+48
-52
math.py
pytensor/tensor/rewriting/math.py
+47
-27
test_math.py
tests/tensor/rewriting/test_math.py
+1
-25
没有找到文件。
pytensor/tensor/rewriting/math.py
浏览文件 @
cff058c9
...
@@ -535,29 +535,58 @@ def local_mul_pow_to_pow_add(fgraph, node):
...
@@ -535,29 +535,58 @@ def local_mul_pow_to_pow_add(fgraph, node):
@register_stabilize
@register_stabilize
@register_specialize
@register_specialize
@register_canonicalize
@register_canonicalize
@node_rewriter
([
sub
])
@node_rewriter
([
add
,
sub
])
def
local_expm1
(
fgraph
,
node
):
def
local_expm1
(
fgraph
,
node
):
"""Detect ``exp(a) - 1`` and convert them to ``expm1(a)``."""
"""Detect ``exp(a) - 1`` or ``-1 + exp(a)`` and convert them to ``expm1(a)``."""
in1
,
in2
=
node
.
inputs
if
len
(
node
.
inputs
)
!=
2
:
out
=
node
.
outputs
[
0
]
# TODO: handle more than two inputs in add
return
None
if
isinstance
(
node
.
op
.
scalar_op
,
ps
.
Sub
):
exp_x
,
other_inp
=
node
.
inputs
if
not
(
exp_x
.
owner
and
isinstance
(
exp_x
.
owner
.
op
,
Elemwise
)
and
isinstance
(
exp_x
.
owner
.
op
.
scalar_op
,
ps
.
Exp
)
and
get_underlying_scalar_constant_value
(
other_inp
,
raise_not_constant
=
False
)
==
1
):
return
None
else
:
# Try both orders
other_inp
,
exp_x
=
node
.
inputs
for
i
in
range
(
2
):
if
i
==
1
:
other_inp
,
exp_x
=
exp_x
,
other_inp
if
(
if
(
in1
.
owner
exp_x
.
owner
and
isinstance
(
in1
.
owner
.
op
,
Elemwise
)
and
isinstance
(
exp_x
.
owner
.
op
,
Elemwise
)
and
isinstance
(
in1
.
owner
.
op
.
scalar_op
,
ps
.
Exp
)
and
isinstance
(
exp_x
.
owner
.
op
.
scalar_op
,
ps
.
Exp
)
and
get_underlying_scalar_constant_value
(
in2
,
raise_not_constant
=
False
)
==
1
and
get_underlying_scalar_constant_value
(
other_inp
,
raise_not_constant
=
False
)
==
-
1
):
):
in11
=
in1
.
owner
.
inputs
[
0
]
break
new_out
=
expm1
(
in11
)
else
:
# no break
return
None
if
new_out
.
type
.
broadcastable
!=
out
.
type
.
broadcastable
:
[
old_out
]
=
node
.
outputs
new_out
=
broadcast_arrays
(
in11
,
in2
)[
0
]
if
new_out
.
dtype
!=
out
.
dtype
:
[
x
]
=
exp_x
.
owner
.
inputs
new_out
=
cast
(
new_out
,
dtype
=
out
.
dtype
)
if
x
.
type
.
broadcastable
!=
old_out
.
type
.
broadcastable
:
x
=
broadcast_arrays
(
x
,
other_inp
)[
0
]
new_out
=
expm1
(
x
)
if
new_out
.
dtype
!=
old_out
.
dtype
:
new_out
=
cast
(
new_out
,
dtype
=
old_out
.
dtype
)
if
not
old_out
.
type
.
is_super
(
new_out
.
type
):
return
None
if
not
out
.
type
.
is_super
(
new_out
.
type
):
return
return
[
new_out
]
return
[
new_out
]
...
@@ -1824,15 +1853,6 @@ def local_add_neg_to_sub(fgraph, node):
...
@@ -1824,15 +1853,6 @@ def local_add_neg_to_sub(fgraph, node):
new_out
=
sub
(
first
,
pre_neg
)
new_out
=
sub
(
first
,
pre_neg
)
return
[
new_out
]
return
[
new_out
]
# Check if it is a negative constant
if
(
isinstance
(
second
,
TensorConstant
)
and
second
.
unique_value
is
not
None
and
second
.
unique_value
<
0
):
new_out
=
sub
(
first
,
np
.
abs
(
second
.
data
))
return
[
new_out
]
@register_canonicalize
@register_canonicalize
@node_rewriter
([
mul
])
@node_rewriter
([
mul
])
...
@@ -2606,9 +2626,9 @@ register_canonicalize(local_one_minus_erfc)
...
@@ -2606,9 +2626,9 @@ register_canonicalize(local_one_minus_erfc)
register_stabilize
(
local_one_minus_erfc
)
register_stabilize
(
local_one_minus_erfc
)
register_specialize
(
local_one_minus_erfc
)
register_specialize
(
local_one_minus_erfc
)
#
erfc(-x)-1
=>erf(x)
#
-1 + erfc(-x)
=>erf(x)
local_erf_neg_minus_one
=
PatternNodeRewriter
(
local_erf_neg_minus_one
=
PatternNodeRewriter
(
(
sub
,
(
erfc
,
(
neg
,
"x"
)),
1
),
(
add
,
-
1
,
(
erfc
,
(
neg
,
"x"
))
),
(
erf
,
"x"
),
(
erf
,
"x"
),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
name
=
"local_erf_neg_minus_one"
,
name
=
"local_erf_neg_minus_one"
,
...
...
tests/tensor/rewriting/test_math.py
浏览文件 @
cff058c9
...
@@ -3806,15 +3806,10 @@ def test_local_expm1():
...
@@ -3806,15 +3806,10 @@ def test_local_expm1():
for
n
in
h
.
maker
.
fgraph
.
toposort
()
for
n
in
h
.
maker
.
fgraph
.
toposort
()
)
)
# This rewrite works when `local_add_neg_to_sub` specialization rewrite is invoked
assert
any
(
expect_rewrite
=
config
.
mode
!=
"FAST_COMPILE"
assert
(
any
(
isinstance
(
n
.
op
,
Elemwise
)
and
isinstance
(
n
.
op
.
scalar_op
,
ps
.
basic
.
Expm1
)
isinstance
(
n
.
op
,
Elemwise
)
and
isinstance
(
n
.
op
.
scalar_op
,
ps
.
basic
.
Expm1
)
for
n
in
r
.
maker
.
fgraph
.
toposort
()
for
n
in
r
.
maker
.
fgraph
.
toposort
()
)
)
==
expect_rewrite
)
def
compile_graph_log_sum_exp
(
x
,
axis
,
dimshuffle_op
=
None
):
def
compile_graph_log_sum_exp
(
x
,
axis
,
dimshuffle_op
=
None
):
...
@@ -4440,25 +4435,6 @@ def test_local_add_neg_to_sub(first_negative):
...
@@ -4440,25 +4435,6 @@ def test_local_add_neg_to_sub(first_negative):
assert
np
.
allclose
(
f
(
x_test
,
y_test
),
exp
)
assert
np
.
allclose
(
f
(
x_test
,
y_test
),
exp
)
@pytest.mark.parametrize
(
"const_left"
,
(
True
,
False
))
def
test_local_add_neg_to_sub_const
(
const_left
):
x
=
vector
(
"x"
)
const
=
np
.
full
((
3
,
2
),
5.0
)
out
=
-
const
+
x
if
const_left
else
x
+
(
-
const
)
f
=
function
([
x
],
out
,
mode
=
Mode
(
"py"
))
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()
if
not
isinstance
(
node
.
op
,
DimShuffle
|
Alloc
)
]
assert
nodes
==
[
pt
.
sub
]
x_test
=
np
.
array
([
3
,
4
],
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
x_test
),
x_test
+
(
-
const
))
def
test_log1mexp_stabilization
():
def
test_log1mexp_stabilization
():
mode
=
Mode
(
"py"
)
.
including
(
"stabilize"
)
mode
=
Mode
(
"py"
)
.
including
(
"stabilize"
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论