Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
975ca888
提交
975ca888
authored
1月 16, 2026
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
1月 17, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
local_[div|mul]_switch_sink: Allow zero inside a DimShuffle
上级
a314476f
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
48 行增加
和
20 行删除
+48
-20
math.py
pytensor/tensor/rewriting/math.py
+27
-20
test_math.py
tests/tensor/rewriting/test_math.py
+21
-0
没有找到文件。
pytensor/tensor/rewriting/math.py
浏览文件 @
975ca888
...
@@ -840,6 +840,7 @@ def local_expm1(fgraph, node):
...
@@ -840,6 +840,7 @@ def local_expm1(fgraph, node):
@register_specialize
@register_specialize
@register_stabilize
@register_canonicalize
@register_canonicalize
@node_rewriter
([
mul
])
@node_rewriter
([
mul
])
def
local_mul_switch_sink
(
fgraph
,
node
):
def
local_mul_switch_sink
(
fgraph
,
node
):
...
@@ -878,14 +879,17 @@ def local_mul_switch_sink(fgraph, node):
...
@@ -878,14 +879,17 @@ def local_mul_switch_sink(fgraph, node):
switch_node
=
mul_inp
.
owner
switch_node
=
mul_inp
.
owner
# Look for a zero as the first or second branch of the switch
# Look for a zero as the first or second branch of the switch
for
branch
in
range
(
2
):
for
branch
in
range
(
2
):
zero_switch_input
=
switch_node
.
inputs
[
1
+
branch
]
zero_inp
=
underlying_zero
=
switch_node
.
inputs
[
1
+
branch
]
if
(
not
get_underlying_scalar_constant_value
(
# Allow zero inside a DimShuffle or Alloc
zero_switch_input
,
if
zero_inp
.
owner
is
not
None
and
isinstance
(
only_process_constants
=
True
,
zero_inp
.
owner
.
op
,
DimShuffle
|
Alloc
raise_not_constant
=
False
,
):
)
underlying_zero
=
zero_inp
.
owner
.
inputs
[
0
]
==
0.0
if
not
(
isinstance
(
underlying_zero
,
TensorConstant
)
and
underlying_zero
.
unique_value
==
0
):
):
continue
continue
...
@@ -904,9 +908,9 @@ def local_mul_switch_sink(fgraph, node):
...
@@ -904,9 +908,9 @@ def local_mul_switch_sink(fgraph, node):
copy_stack_trace
(
node
.
outputs
,
fmul
)
copy_stack_trace
(
node
.
outputs
,
fmul
)
if
branch
==
0
:
if
branch
==
0
:
fct
=
switch
(
switch_cond
,
zero_
switch_input
,
fmul
)
fct
=
switch
(
switch_cond
,
zero_
inp
,
fmul
)
else
:
else
:
fct
=
switch
(
switch_cond
,
fmul
,
zero_
switch_input
)
fct
=
switch
(
switch_cond
,
fmul
,
zero_
inp
)
# Tell debug_mode than the output is correct, even if nan disappear
# Tell debug_mode than the output is correct, even if nan disappear
fct
.
tag
.
values_eq_approx
=
values_eq_approx_remove_nan
fct
.
tag
.
values_eq_approx
=
values_eq_approx_remove_nan
...
@@ -942,14 +946,17 @@ def local_div_switch_sink(fgraph, node):
...
@@ -942,14 +946,17 @@ def local_div_switch_sink(fgraph, node):
switch_node
=
num
.
owner
switch_node
=
num
.
owner
# Look for a zero as the first or second branch of the switch
# Look for a zero as the first or second branch of the switch
for
branch
in
range
(
2
):
for
branch
in
range
(
2
):
zero_switch_input
=
switch_node
.
inputs
[
1
+
branch
]
zero_inp
=
underlying_zero
=
switch_node
.
inputs
[
1
+
branch
]
if
(
not
get_underlying_scalar_constant_value
(
# Allow zero inside a DimShuffle or Alloc
zero_switch_input
,
if
zero_inp
.
owner
is
not
None
and
isinstance
(
only_process_constants
=
True
,
zero_inp
.
owner
.
op
,
DimShuffle
|
Alloc
raise_not_constant
=
False
,
):
)
underlying_zero
=
zero_inp
.
owner
.
inputs
[
0
]
==
0.0
if
not
(
isinstance
(
underlying_zero
,
TensorConstant
)
and
underlying_zero
.
unique_value
==
0
):
):
continue
continue
...
@@ -966,9 +973,9 @@ def local_div_switch_sink(fgraph, node):
...
@@ -966,9 +973,9 @@ def local_div_switch_sink(fgraph, node):
copy_stack_trace
(
node
.
outputs
,
fdiv
)
copy_stack_trace
(
node
.
outputs
,
fdiv
)
if
branch
==
0
:
if
branch
==
0
:
fct
=
switch
(
switch_cond
,
zero_
switch_input
,
fdiv
)
fct
=
switch
(
switch_cond
,
zero_
inp
,
fdiv
)
else
:
else
:
fct
=
switch
(
switch_cond
,
fdiv
,
zero_
switch_input
)
fct
=
switch
(
switch_cond
,
fdiv
,
zero_
inp
)
# Tell debug_mode than the output is correct, even if nan disappear
# Tell debug_mode than the output is correct, even if nan disappear
fct
.
tag
.
values_eq_approx
=
values_eq_approx_remove_nan
fct
.
tag
.
values_eq_approx
=
values_eq_approx_remove_nan
...
...
tests/tensor/rewriting/test_math.py
浏览文件 @
975ca888
...
@@ -2278,6 +2278,27 @@ class TestLocalSwitchSink:
...
@@ -2278,6 +2278,27 @@ class TestLocalSwitchSink:
[
new_left
,
new_right
],
[
expected_left
,
expected_right
]
[
new_left
,
new_right
],
[
expected_left
,
expected_right
]
)
)
def
test_safe_constant_fold
(
self
):
inner_mul
=
[
1.5
,
0
,
1
]
outer_mul
=
[
1.5
,
-
np
.
inf
,
-
np
.
inf
]
valid
=
[
True
,
False
,
True
]
for
inner_zero
in
(
pt
.
expand_dims
(
np
.
array
(
0
),
0
),
pt
.
zeros
((
3
,)),
):
out
=
(
switch
(
valid
,
inner_mul
,
inner_zero
,
)
*
outer_mul
)
# If the rewrite doesn't happen before constant_folding, the middle term will be nan
np
.
testing
.
assert_allclose
(
out
.
eval
(),
[
1.5
**
2
,
0
,
-
np
.
inf
])
@pytest.mark.skipif
(
@pytest.mark.skipif
(
config
.
cxx
==
""
,
config
.
cxx
==
""
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论