Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2d0057d4
提交
2d0057d4
authored
7月 15, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
8月 17, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename OpSub to SubstitutionNodeRewriter
上级
66974655
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
39 行增加
和
28 行删除
+39
-28
opt.py
aesara/graph/opt.py
+8
-3
graph_rewriting.rst
doc/extending/graph_rewriting.rst
+7
-7
test_destroyhandler.py
tests/graph/test_destroyhandler.py
+18
-12
test_opt.py
tests/graph/test_opt.py
+6
-6
没有找到文件。
aesara/graph/opt.py
浏览文件 @
2d0057d4
...
...
@@ -1396,7 +1396,7 @@ class SequentialNodeRewriter(NodeRewriter):
opt
.
add_requirements
(
fgraph
)
class
OpSub
(
NodeRewriter
):
class
SubstitutionNodeRewriter
(
NodeRewriter
):
"""
Replaces the application of a certain `Op` by the application of
...
...
@@ -1411,12 +1411,12 @@ class OpSub(NodeRewriter):
Examples
--------
OpSub
(add, sub) ==>
SubstitutionNodeRewriter
(add, sub) ==>
add(div(x, y), add(y, x)) -> sub(div(x, y), sub(y, x))
"""
# an
OpSub
does not apply to the nodes it produces
# an
SubstitutionNodeRewriter
does not apply to the nodes it produces
reentrant
=
False
# all the inputs of the original node are transferred to the outputs
retains_inputs
=
True
...
...
@@ -3173,6 +3173,11 @@ DEPRECATED_NAMES = [
"`LocalOptGroup` is deprecated: use `SequentialNodeRewriter` instead."
,
SequentialNodeRewriter
,
),
(
"OpSub"
,
"`OpSub` is deprecated: use `SubstitutionNodeRewriter` instead."
,
SubstitutionNodeRewriter
,
),
]
...
...
doc/extending/graph_rewriting.rst
浏览文件 @
2d0057d4
...
...
@@ -270,12 +270,12 @@ FunctionGraph(add(z, mul(true_div(mul(y, x), y), true_div(z, x))))
>>> e
FunctionGraph(add(z, mul(x, true_div(z, x))))
:class:`
OpSub
`, :class:`OpRemove`, :class:`PatternSub`
++++++++++++++++++++++++++++++++++++++++++++++++++++++
:class:`
SubstitutionNodeRewriter
`, :class:`OpRemove`, :class:`PatternSub`
++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++
Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
.. function::
OpSub
(op1, op2)
.. function::
SubstitutionNodeRewriter
(op1, op2)
Replaces all uses of ``op1`` by ``op2``. In other
words, the outputs of all :class:`Apply` nodes using ``op1`` by the outputs
...
...
@@ -296,11 +296,11 @@ Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
.. code::
from aesara.scalar import identity
from aesara.graph.opt import
OpSub
, OpRemove, PatternSub
from aesara.graph.opt import
SubstitutionNodeRewriter
, OpRemove, PatternSub
# Replacing `add` by `mul` (this is not recommended for primarily
# mathematical reasons):
add_to_mul =
OpSub
(add, mul)
add_to_mul =
SubstitutionNodeRewriter
(add, mul)
# Removing `identity`
remove_identity = OpRemove(identity)
...
...
@@ -313,12 +313,12 @@ Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
.. note::
:class:`
OpSub
`, :class:`OpRemove` and :class:`PatternSub` produce local optimizers, which
:class:`
SubstitutionNodeRewriter
`, :class:`OpRemove` and :class:`PatternSub` produce local optimizers, which
means that everything we said previously about local optimizers
apply (e.g. they need to be wrapped in a :class:`NavigatorOptimizer`, etc.)
When an optimization can be naturally expressed using :class:`
OpSub
`, :class:`OpRemove`
When an optimization can be naturally expressed using :class:`
SubstitutionNodeRewriter
`, :class:`OpRemove`
or :class:`PatternSub`, it is highly recommended to use them.
.. _unification:
...
...
tests/graph/test_destroyhandler.py
浏览文件 @
2d0057d4
...
...
@@ -11,8 +11,8 @@ from aesara.graph.op import Op
from
aesara.graph.opt
import
(
NavigatorOptimizer
,
OpKeyOptimizer
,
OpSub
,
PatternSub
,
SubstitutionNodeRewriter
,
TopoOptimizer
,
)
from
aesara.graph.type
import
Type
...
...
@@ -24,8 +24,12 @@ def PatternOptimizer(p1, p2, ign=True):
return
OpKeyOptimizer
(
PatternSub
(
p1
,
p2
),
ignore_newtrees
=
ign
)
def
OpSubOptimizer
(
op1
,
op2
,
fail
=
NavigatorOptimizer
.
warn_ignore
,
ign
=
True
):
return
TopoOptimizer
(
OpSub
(
op1
,
op2
),
ignore_newtrees
=
ign
,
failure_callback
=
fail
)
def
TopoSubstitutionNodeRewriter
(
op1
,
op2
,
fail
=
NavigatorOptimizer
.
warn_ignore
,
ign
=
True
):
return
TopoOptimizer
(
SubstitutionNodeRewriter
(
op1
,
op2
),
ignore_newtrees
=
ign
,
failure_callback
=
fail
)
def
as_variable
(
x
):
...
...
@@ -127,7 +131,7 @@ def create_fgraph(inputs, outputs, validate=True):
class
FailureWatch
:
# when passed to
OpSubOptimiz
er or PatternOptimizer, counts the
# when passed to
SubstitutionNodeRewrit
er or PatternOptimizer, counts the
# number of failures
def
__init__
(
self
):
self
.
failures
=
0
...
...
@@ -326,7 +330,7 @@ def test_long_destroyers_loop():
e
=
dot
(
dot
(
add_in_place
(
x
,
y
),
add_in_place
(
y
,
z
)),
add
(
z
,
x
))
g
=
create_fgraph
([
x
,
y
,
z
],
[
e
])
assert
g
.
consistent
()
OpSubOptimiz
er
(
add
,
add_in_place
)
.
optimize
(
g
)
TopoSubstitutionNodeRewrit
er
(
add
,
add_in_place
)
.
optimize
(
g
)
assert
g
.
consistent
()
# we don't want to see that!
assert
(
...
...
@@ -362,7 +366,7 @@ def test_multi_destroyers_through_views():
g
=
create_fgraph
([
x
,
y
,
z
],
[
e
])
assert
g
.
consistent
()
fail
=
FailureWatch
()
OpSubOptimiz
er
(
add
,
add_in_place
,
fail
)
.
optimize
(
g
)
TopoSubstitutionNodeRewrit
er
(
add
,
add_in_place
,
fail
)
.
optimize
(
g
)
assert
g
.
consistent
()
assert
fail
.
failures
==
1
# should have succeeded once and failed once
...
...
@@ -384,7 +388,7 @@ def test_usage_loop():
g
=
create_fgraph
([
x
,
y
,
z
],
[
dot
(
add_in_place
(
x
,
z
),
x
)],
False
)
assert
not
g
.
consistent
()
# replace add_in_place with add
OpSubOptimiz
er
(
add_in_place
,
add
)
.
optimize
(
g
)
TopoSubstitutionNodeRewrit
er
(
add_in_place
,
add
)
.
optimize
(
g
)
assert
g
.
consistent
()
...
...
@@ -405,7 +409,7 @@ def test_usage_loop_insert_views():
g
=
create_fgraph
([
x
,
y
,
z
],
[
e
])
assert
g
.
consistent
()
fail
=
FailureWatch
()
OpSubOptimiz
er
(
sigmoid
,
transpose_view
,
fail
)
.
optimize
(
g
)
TopoSubstitutionNodeRewrit
er
(
sigmoid
,
transpose_view
,
fail
)
.
optimize
(
g
)
assert
g
.
consistent
()
# it must keep one sigmoid in the long sigmoid chain
assert
fail
.
failures
==
1
...
...
@@ -450,24 +454,26 @@ def test_multiple_inplace():
# try to work in-place on x/0 and y/1 (this should fail)
fail
=
FailureWatch
()
OpSubOptimiz
er
(
multiple
,
multiple_in_place_0_1
,
fail
)
.
optimize
(
g
)
TopoSubstitutionNodeRewrit
er
(
multiple
,
multiple_in_place_0_1
,
fail
)
.
optimize
(
g
)
assert
g
.
consistent
()
assert
fail
.
failures
==
1
# try to work in-place on x/0 (this should fail)
fail
=
FailureWatch
()
OpSubOptimiz
er
(
multiple
,
multiple_in_place_0
,
fail
)
.
optimize
(
g
)
TopoSubstitutionNodeRewrit
er
(
multiple
,
multiple_in_place_0
,
fail
)
.
optimize
(
g
)
assert
g
.
consistent
()
assert
fail
.
failures
==
1
# try to work in-place on y/1 (this should succeed)
fail
=
FailureWatch
()
OpSubOptimiz
er
(
multiple
,
multiple_in_place_1
,
fail
)
.
optimize
(
g
)
TopoSubstitutionNodeRewrit
er
(
multiple
,
multiple_in_place_1
,
fail
)
.
optimize
(
g
)
assert
g
.
consistent
()
assert
fail
.
failures
==
0
# try to work in-place on x/0 and y/1 (this should still fail)
fail
=
FailureWatch
()
OpSubOptimizer
(
multiple_in_place_1
,
multiple_in_place_0_1
,
fail
)
.
optimize
(
g
)
TopoSubstitutionNodeRewriter
(
multiple_in_place_1
,
multiple_in_place_0_1
,
fail
)
.
optimize
(
g
)
assert
g
.
consistent
()
assert
fail
.
failures
==
1
tests/graph/test_opt.py
浏览文件 @
2d0057d4
...
...
@@ -9,10 +9,10 @@ from aesara.graph.opt import (
EquilibriumOptimizer
,
MergeOptimizer
,
OpKeyOptimizer
,
OpSub
,
OpToRewriterTracker
,
PatternSub
,
SequentialNodeRewriter
,
SubstitutionNodeRewriter
,
TopoOptimizer
,
in2out
,
logging
,
...
...
@@ -223,23 +223,23 @@ class TestPatternOptimizer:
assert
str_g
==
"FunctionGraph(Op4(z, y))"
def
OpSubOptimiz
er
(
op1
,
op2
):
return
OpKeyOptimizer
(
OpSub
(
op1
,
op2
))
def
KeyedSubstitutionNodeRewrit
er
(
op1
,
op2
):
return
OpKeyOptimizer
(
SubstitutionNodeRewriter
(
op1
,
op2
))
class
Test
OpSubOptimiz
er
:
class
Test
SubstitutionNodeRewrit
er
:
def
test_straightforward
(
self
):
x
,
y
,
z
=
MyVariable
(
"x"
),
MyVariable
(
"y"
),
MyVariable
(
"z"
)
e
=
op1
(
op1
(
op1
(
op1
(
op1
(
x
)))))
g
=
FunctionGraph
([
x
,
y
,
z
],
[
e
])
OpSubOptimiz
er
(
op1
,
op2
)
.
optimize
(
g
)
KeyedSubstitutionNodeRewrit
er
(
op1
,
op2
)
.
optimize
(
g
)
assert
str
(
g
)
==
"FunctionGraph(Op2(Op2(Op2(Op2(Op2(x))))))"
def
test_straightforward_2
(
self
):
x
,
y
,
z
=
MyVariable
(
"x"
),
MyVariable
(
"y"
),
MyVariable
(
"z"
)
e
=
op1
(
op2
(
x
),
op3
(
y
),
op4
(
z
))
g
=
FunctionGraph
([
x
,
y
,
z
],
[
e
])
OpSubOptimiz
er
(
op3
,
op4
)
.
optimize
(
g
)
KeyedSubstitutionNodeRewrit
er
(
op3
,
op4
)
.
optimize
(
g
)
assert
str
(
g
)
==
"FunctionGraph(Op1(Op2(x), Op4(y), Op4(z)))"
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论