Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0b558d8e
提交
0b558d8e
authored
8月 01, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
8月 03, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Retain more precise types in MergeOptimizer
This can avoid some infinite rewrite loops where a SpecifyShape is lifted, removed and then reintroduced at the bottom by the MergeOptimizer
上级
4cc13bce
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
35 行增加
和
10 行删除
+35
-10
basic.py
pytensor/graph/rewriting/basic.py
+16
-8
test_basic.py
tests/graph/rewriting/test_basic.py
+19
-2
没有找到文件。
pytensor/graph/rewriting/basic.py
浏览文件 @
0b558d8e
...
...
@@ -743,14 +743,22 @@ class MergeOptimizer(GraphRewriter):
):
continue
if
len
(
pairs
)
==
1
and
pairs
[
0
][
0
]
.
type
!=
pairs
[
0
][
1
]
.
type
:
res
=
pairs
[
0
][
0
]
.
type
.
convert_variable
(
pairs
[
0
][
1
])
# Since the fgraph.replace only checks the convert_variable
# in one way, we change the order in the case that
# convert_variable will not be successful.
if
not
res
:
pairs
=
[(
pairs
[
0
][
1
],
pairs
[
0
][
0
])]
# Keep the variable with the most specific static type from the pairs
# E.g the second in (TensorType(shape=(None,), TensorType(shape=(5,))
# Otherwise we could end up reverting type inference progress done elsewhere.
for
pair_idx
in
range
(
len
(
pairs
)):
old
,
new
=
pairs
[
pair_idx
]
if
old
.
type
==
new
.
type
:
continue
# Check if type of new replacement is at least as specific as that of the old variable
if
not
old
.
type
.
is_super
(
new
.
type
):
# Check the other way around
if
new
.
type
.
is_super
(
old
.
type
):
pairs
[
pair_idx
]
=
(
new
,
old
)
else
:
# Replacement requires some operation like specify_shape
new_repl
=
old
.
type
.
convert_variable
(
new
)
pairs
[
pair_idx
]
=
(
old
,
new_repl
)
try
:
# If they're all `AtomicVariable`s, there's no need to call validate.
...
...
tests/graph/rewriting/test_basic.py
浏览文件 @
0b558d8e
...
...
@@ -21,10 +21,10 @@ from pytensor.graph.rewriting.basic import (
pre_greedy_node_rewriter
,
)
from
pytensor.raise_op
import
assert_op
from
pytensor.tensor.math
import
Dot
,
add
,
dot
from
pytensor.tensor.math
import
Dot
,
add
,
dot
,
exp
from
pytensor.tensor.rewriting.basic
import
constant_folding
from
pytensor.tensor.subtensor
import
AdvancedSubtensor
from
pytensor.tensor.type
import
matrix
,
values_eq_approx_always_true
from
pytensor.tensor.type
import
matrix
,
values_eq_approx_always_true
,
vector
from
pytensor.tensor.type_other
import
MakeSlice
,
SliceConstant
,
slicetype
from
tests.graph.utils
import
(
MyOp
,
...
...
@@ -441,6 +441,23 @@ class TestMergeOptimizer:
assert
fg
.
outputs
[
0
]
is
fg
.
outputs
[
1
]
assert
fg
.
outputs
[
0
]
is
not
fg
.
outputs
[
2
]
@pytest.mark.parametrize
(
"reverse"
,
[
False
,
True
])
def
test_merge_more_specific_types
(
self
,
reverse
):
"""Check that we choose the most specific static type when merging variables."""
x1
=
vector
(
"x1"
,
shape
=
(
None
,))
x2
=
vector
(
"x2"
,
shape
=
(
500
,))
y1
=
exp
(
x1
)
y2
=
exp
(
x2
)
# Simulate case where we find that x2 is equivalent to x1
fg
=
FunctionGraph
([
x1
,
x2
],
[
y2
,
y1
]
if
reverse
else
[
y1
,
y2
],
clone
=
False
)
fg
.
replace
(
x1
,
x2
)
MergeOptimizer
()
.
rewrite
(
fg
)
assert
fg
.
outputs
==
[
y2
,
y2
]
class
TestEquilibrium
:
def
test_1
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论