Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e03605e4
提交
e03605e4
authored
6月 23, 2025
作者:
jessegrabowski
提交者:
Jesse Grabowski
6月 26, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rewrite `sqr(sqrt(x)) -> |x|` and `sqrt(sqr(x)) -> x`
上级
271c2463
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
70 行增加
和
0 行删除
+70
-0
math.py
pytensor/tensor/rewriting/math.py
+31
-0
test_math.py
tests/tensor/rewriting/test_math.py
+39
-0
没有找到文件。
pytensor/tensor/rewriting/math.py
浏览文件 @
e03605e4
...
@@ -400,6 +400,37 @@ def local_exp_log(fgraph, node):
...
@@ -400,6 +400,37 @@ def local_exp_log(fgraph, node):
return
[
exp
(
x
)]
return
[
exp
(
x
)]
@register_canonicalize
@register_specialize
@node_rewriter
([
sqrt
,
sqr
])
def
local_sqrt_sqr
(
fgraph
,
node
):
x
=
node
.
inputs
[
0
]
if
not
(
x
.
owner
and
isinstance
(
x
.
owner
.
op
,
Elemwise
)):
return
prev_op
=
x
.
owner
.
op
.
scalar_op
node_op
=
node
.
op
.
scalar_op
# Case for sqrt(sqr(x)) -> |x|
if
isinstance
(
prev_op
,
ps
.
Sqrt
)
and
isinstance
(
node_op
,
ps
.
Sqr
):
new_out
=
pt_abs
(
x
.
owner
.
inputs
[
0
])
old_out
=
node
.
outputs
[
0
]
# Handle potential integer to float cast by sqr
if
new_out
.
dtype
!=
old_out
.
dtype
:
new_out
=
cast
(
new_out
,
old_out
.
dtype
)
return
[
new_out
]
# Case for sqr(sqrt(x)) -> x
if
isinstance
(
prev_op
,
ps
.
Sqr
)
and
isinstance
(
node_op
,
ps
.
Sqrt
):
x
=
x
.
owner
.
inputs
[
0
]
old_out
=
node
.
outputs
[
0
]
new_out
=
switch
(
ge
(
x
,
0
),
x
,
np
.
asarray
(
np
.
nan
,
old_out
.
dtype
))
return
[
new_out
]
@register_specialize
@register_specialize
@node_rewriter
([
exp
,
expm1
])
@node_rewriter
([
exp
,
expm1
])
def
local_exp_log_nan_switch
(
fgraph
,
node
):
def
local_exp_log_nan_switch
(
fgraph
,
node
):
...
...
tests/tensor/rewriting/test_math.py
浏览文件 @
e03605e4
...
@@ -2031,6 +2031,45 @@ class TestExpLog:
...
@@ -2031,6 +2031,45 @@ class TestExpLog:
assert
len
(
ops_graph
)
==
expected_switches
assert
len
(
ops_graph
)
==
expected_switches
class
TestSqrSqrt
:
def
setup_method
(
self
):
mode
=
get_default_mode
()
self
.
mode
=
mode
.
including
(
"local_sqrt_sqr"
,
)
.
excluding
(
"fusion"
)
self
.
rng
=
np
.
random
.
default_rng
()
def
test_sqr_sqrt
(
self
):
# sqrt(x) ** 2 -> x
x
=
pt
.
tensor
(
"x"
,
shape
=
(
None
,
None
))
out
=
sqr
(
sqrt
(
x
))
out
=
rewrite_graph
(
out
,
include
=
[
"canonicalize"
,
"specialize"
,
"stabilize"
])
assert
equal_computations
([
out
],
[
pt_abs
(
x
)])
def
test_sqrt_sqr
(
self
):
x
=
pt
.
tensor
(
"x"
,
shape
=
(
None
,
None
))
out
=
sqrt
(
sqr
(
x
))
out
=
rewrite_graph
(
out
,
include
=
[
"canonicalize"
,
"specialize"
,
"stabilize"
])
expected
=
switch
(
ge
(
x
,
np
.
zeros
((
1
,
1
),
dtype
=
"int8"
)),
x
,
np
.
full
((
1
,
1
),
np
.
nan
,
dtype
=
x
.
type
.
dtype
),
)
assert
equal_computations
([
out
],
[
expected
])
def
test_sqr_sqrt_integer_upcast
(
self
):
x
=
ivector
(
"x"
)
out
=
sqr
(
sqrt
(
x
))
dtype
=
out
.
type
.
dtype
out
=
rewrite_graph
(
out
,
include
=
[
"canonicalize"
,
"specialize"
,
"stabilize"
])
expected
=
pt
.
cast
(
pt_abs
(
x
),
dtype
=
dtype
)
assert
equal_computations
([
out
],
[
expected
])
class
TestLocalSwitchSink
:
class
TestLocalSwitchSink
:
def
setup_method
(
self
):
def
setup_method
(
self
):
# condition values
# condition values
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论