Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
82823dec
提交
82823dec
authored
2月 16, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
3月 13, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove switches when both branches are equivalent constants with different dtype
上级
aad78d57
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
42 行增加
和
5 行删除
+42
-5
basic.py
pytensor/tensor/rewriting/basic.py
+26
-5
test_basic.py
tests/tensor/rewriting/test_basic.py
+16
-0
没有找到文件。
pytensor/tensor/rewriting/basic.py
浏览文件 @
82823dec
...
...
@@ -958,6 +958,25 @@ def local_sum_make_vector(fgraph, node):
return
[
element_sum
]
def
equivalent_up_to_constant_casting
(
a
,
b
)
->
bool
:
"""Return True if a and b are equivalent up to constant casting."""
if
a
==
b
:
return
True
# Return equivalence based on data values, ignoring dtype
if
(
isinstance
(
a
,
TensorConstant
)
and
isinstance
(
b
,
TensorConstant
)
and
a
.
type
.
shape
==
b
.
type
.
shape
# We don't want to spend a lot of time comparing large constant arrays
# First, check if dtype matches, otherwise a == b would be true if they hold the same values
and
a
.
type
.
dtype
!=
b
.
type
.
dtype
# Check property sum() that is cached for TensorConstants, to filter down candidates even more
and
a
.
signature
()
.
sum
==
b
.
signature
()
.
sum
):
return
np
.
array_equal
(
a
.
data
,
b
.
data
)
return
False
@register_useless
(
"shape_unsafe"
)
@register_canonicalize
(
"fast_compile"
,
"shape_unsafe"
)
@register_specialize
(
"shape_unsafe"
)
...
...
@@ -1004,15 +1023,17 @@ def local_useless_switch(fgraph, node):
return
[
out
]
# if left is right -> left
if
left
==
right
:
# Note: No need to copy over stacktrace, because the input node
# already has its own stacktrace
if
equivalent_up_to_constant_casting
(
left
,
right
):
if
left
.
type
.
broadcastable
==
out_bcast
:
out_dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
if
left
.
type
.
dtype
!=
out_dtype
:
left
=
cast
(
left
,
out_dtype
)
copy_stack_trace
(
node
.
outputs
+
left
,
left
)
# When not casting, the other inputs of the switch aren't needed in the traceback
return
[
left
]
else
:
ret
=
broadcast_arrays
(
left
,
cond
)[
0
]
# Copy over stacktrace from switch output and correct branch
copy_stack_trace
(
node
.
outputs
+
left
,
ret
)
return
[
ret
]
...
...
tests/tensor/rewriting/test_basic.py
浏览文件 @
82823dec
...
...
@@ -26,6 +26,7 @@ from pytensor.tensor.basic import (
ScalarFromTensor
,
Split
,
TensorFromScalar
,
as_tensor
,
cast
,
join
,
tile
,
...
...
@@ -983,6 +984,21 @@ class TestLocalUselessSwitch:
assert
np
.
array_equal
(
f0
(
vx
),
vx
)
assert
np
.
array_equal
(
f2
(
vx
,
vc
),
vx
)
def
test_left_is_right_constant
(
self
):
int8_one
=
as_tensor
(
np
.
int8
(
1
))
int8_zero
=
as_tensor
(
np
.
int8
(
0
))
int64_zero
=
as_tensor
(
np
.
int64
(
0
))
cond
=
scalar
(
"cond"
,
dtype
=
bool
)
out
=
pt
.
switch
(
cond
,
int8_zero
,
int64_zero
)
assert
equal_computations
([
rewrite_graph
(
out
)],
[
int64_zero
])
out
=
pt
.
switch
(
cond
,
int64_zero
,
int8_zero
)
assert
equal_computations
([
rewrite_graph
(
out
)],
[
int64_zero
])
out
=
pt
.
switch
(
cond
,
int8_one
,
int8_zero
)
assert
equal_computations
([
rewrite_graph
(
out
)],
[
out
])
@pytest.mark.parametrize
(
"dtype1"
,
[
"float32"
,
"float64"
],
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论