Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ef22377d
提交
ef22377d
authored
3月 25, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
3月 28, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix bug when broadcasting branches in local_useless_switch rewrite
上级
5a47550f
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
24 行增加
和
8 行删除
+24
-8
basic.py
pytensor/tensor/rewriting/basic.py
+5
-8
test_basic.py
tests/tensor/rewriting/test_basic.py
+19
-0
没有找到文件。
pytensor/tensor/rewriting/basic.py
浏览文件 @
ef22377d
...
@@ -1023,18 +1023,15 @@ def local_useless_switch(fgraph, node):
...
@@ -1023,18 +1023,15 @@ def local_useless_switch(fgraph, node):
# if left is right -> left
# if left is right -> left
if
equivalent_up_to_constant_casting
(
left
,
right
):
if
equivalent_up_to_constant_casting
(
left
,
right
):
if
left
.
type
.
broadcastable
==
out_bcast
:
if
left
.
type
.
broadcastable
!=
out_bcast
:
left
,
_
=
broadcast_arrays
(
left
,
cond
)
out_dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
out_dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
if
left
.
type
.
dtype
!=
out_dtype
:
if
left
.
type
.
dtype
!=
out_dtype
:
left
=
cast
(
left
,
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
:
copy_stack_trace
(
node
.
outputs
+
node
.
inputs
,
left
)
ret
=
broadcast_arrays
(
left
,
cond
)[
0
]
return
[
left
]
copy_stack_trace
(
node
.
outputs
+
left
,
ret
)
return
[
ret
]
# This case happens with scan.
# This case happens with scan.
# Elemwise{switch}(le(shape_i{id}(X), 0), 0, shape_i{id}(X)) -> shape_i{id}(X)
# Elemwise{switch}(le(shape_i{id}(X), 0), 0, shape_i{id}(X)) -> shape_i{id}(X)
...
...
tests/tensor/rewriting/test_basic.py
浏览文件 @
ef22377d
...
@@ -1089,6 +1089,25 @@ class TestLocalUselessSwitch:
...
@@ -1089,6 +1089,25 @@ class TestLocalUselessSwitch:
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
Alloc
)
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
Alloc
)
assert
not
any
(
node
.
op
==
pt
.
switch
for
node
in
f
.
maker
.
fgraph
.
toposort
())
assert
not
any
(
node
.
op
==
pt
.
switch
for
node
in
f
.
maker
.
fgraph
.
toposort
())
def
test_broadcasting_different_dtype
(
self
):
cond
=
vector
(
"x"
,
dtype
=
"bool"
)
float32_branch
=
as_tensor
(
np
.
array
([
0
],
dtype
=
"float32"
))
float64_branch
=
as_tensor
(
np
.
array
([
0
],
dtype
=
"float64"
))
out
=
pt
.
switch
(
cond
,
float32_branch
,
float64_branch
)
expected_out
=
pt
.
alloc
(
float64_branch
,
cond
.
shape
)
rewritten_out
=
rewrite_graph
(
out
,
include
=
(
"canonicalize"
,
"stabilize"
,
"specialize"
)
)
assert
equal_computations
([
rewritten_out
],
[
expected_out
])
out
=
pt
.
switch
(
cond
,
float64_branch
,
float32_branch
)
rewritten_out
=
rewrite_graph
(
out
,
include
=
(
"canonicalize"
,
"stabilize"
,
"specialize"
)
)
assert
equal_computations
([
rewritten_out
],
[
expected_out
])
class
TestLocalMergeSwitchSameCond
:
class
TestLocalMergeSwitchSameCond
:
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论