Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6302cef1
提交
6302cef1
authored
7月 15, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
8月 17, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename PatternSub to PatternNodeRewriter
上级
371c5447
显示空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
67 行增加
和
62 行删除
+67
-62
opt.py
aesara/graph/opt.py
+12
-7
opt.py
aesara/sparse/opt.py
+2
-2
math_opt.py
aesara/tensor/math_opt.py
+16
-16
graph_rewriting.rst
doc/extending/graph_rewriting.rst
+12
-12
test_types.py
tests/compile/function/test_types.py
+2
-2
test_destroyhandler.py
tests/graph/test_destroyhandler.py
+2
-2
test_opt.py
tests/graph/test_opt.py
+20
-20
test_math_opt.py
tests/tensor/test_math_opt.py
+1
-1
没有找到文件。
aesara/graph/opt.py
浏览文件 @
6302cef1
...
@@ -1479,7 +1479,7 @@ class RemovalNodeRewriter(NodeRewriter):
...
@@ -1479,7 +1479,7 @@ class RemovalNodeRewriter(NodeRewriter):
)
)
class
Pattern
Sub
(
NodeRewriter
):
class
Pattern
NodeRewriter
(
NodeRewriter
):
"""Replace all occurrences of an input pattern with an output pattern.
"""Replace all occurrences of an input pattern with an output pattern.
The input and output patterns have the following syntax:
The input and output patterns have the following syntax:
...
@@ -1517,7 +1517,7 @@ class PatternSub(NodeRewriter):
...
@@ -1517,7 +1517,7 @@ class PatternSub(NodeRewriter):
trying to match and returns True or False according to an
trying to match and returns True or False according to an
arbitrary criterion.
arbitrary criterion.
The constructor creates a `Pattern
Sub
` that replaces occurrences of
The constructor creates a `Pattern
NodeRewriter
` that replaces occurrences of
`in_pattern` by occurrences of `out_pattern`.
`in_pattern` by occurrences of `out_pattern`.
Parameters
Parameters
...
@@ -1548,11 +1548,11 @@ class PatternSub(NodeRewriter):
...
@@ -1548,11 +1548,11 @@ class PatternSub(NodeRewriter):
Examples
Examples
--------
--------
Pattern
Sub
((add, 'x', 'y'), (add, 'y', 'x'))
Pattern
NodeRewriter
((add, 'x', 'y'), (add, 'y', 'x'))
Pattern
Sub
((multiply, 'x', 'x'), (square, 'x'))
Pattern
NodeRewriter
((multiply, 'x', 'x'), (square, 'x'))
Pattern
Sub
((subtract, (add, 'x', 'y'), 'y'), 'x')
Pattern
NodeRewriter
((subtract, (add, 'x', 'y'), 'y'), 'x')
Pattern
Sub
((power, 'x', Constant(double, 2.0)), (square, 'x'))
Pattern
NodeRewriter
((power, 'x', Constant(double, 2.0)), (square, 'x'))
Pattern
Sub
((boggle, {'pattern': 'x',
Pattern
NodeRewriter
((boggle, {'pattern': 'x',
'constraint': lambda expr: expr.type == scrabble}),
'constraint': lambda expr: expr.type == scrabble}),
(scrabble, 'x'))
(scrabble, 'x'))
...
@@ -3183,6 +3183,11 @@ DEPRECATED_NAMES = [
...
@@ -3183,6 +3183,11 @@ DEPRECATED_NAMES = [
"`OpRemove` is deprecated: use `RemovalNodeRewriter` instead."
,
"`OpRemove` is deprecated: use `RemovalNodeRewriter` instead."
,
RemovalNodeRewriter
,
RemovalNodeRewriter
,
),
),
(
"PatternSub"
,
"`PatternSub` is deprecated: use `PatternNodeRewriter` instead."
,
PatternNodeRewriter
,
),
]
]
...
...
aesara/sparse/opt.py
浏览文件 @
6302cef1
...
@@ -4,7 +4,7 @@ import aesara
...
@@ -4,7 +4,7 @@ import aesara
import
aesara.scalar
as
aes
import
aesara.scalar
as
aes
from
aesara.configdefaults
import
config
from
aesara.configdefaults
import
config
from
aesara.graph.basic
import
Apply
from
aesara.graph.basic
import
Apply
from
aesara.graph.opt
import
Pattern
Sub
,
TopoOptimizer
,
node_rewriter
from
aesara.graph.opt
import
Pattern
NodeRewriter
,
TopoOptimizer
,
node_rewriter
from
aesara.link.c.op
import
COp
,
_NoPythonCOp
from
aesara.link.c.op
import
COp
,
_NoPythonCOp
from
aesara.misc.safe_asarray
import
_asarray
from
aesara.misc.safe_asarray
import
_asarray
from
aesara.sparse
import
basic
as
sparse
from
aesara.sparse
import
basic
as
sparse
...
@@ -928,7 +928,7 @@ usmm_csc_dense_inplace = UsmmCscDense(inplace=True)
...
@@ -928,7 +928,7 @@ usmm_csc_dense_inplace = UsmmCscDense(inplace=True)
# This is tested in tests/test_basic.py:UsmmTests
# This is tested in tests/test_basic.py:UsmmTests
local_usmm
=
Pattern
Sub
(
local_usmm
=
Pattern
NodeRewriter
(
(
(
sub
,
sub
,
"z"
,
"z"
,
...
...
aesara/tensor/math_opt.py
浏览文件 @
6302cef1
...
@@ -11,7 +11,7 @@ import aesara.scalar.math as aes_math
...
@@ -11,7 +11,7 @@ import aesara.scalar.math as aes_math
from
aesara.graph.basic
import
Constant
,
Variable
from
aesara.graph.basic
import
Constant
,
Variable
from
aesara.graph.opt
import
(
from
aesara.graph.opt
import
(
NodeRewriter
,
NodeRewriter
,
Pattern
Sub
,
Pattern
NodeRewriter
,
SequentialNodeRewriter
,
SequentialNodeRewriter
,
copy_stack_trace
,
copy_stack_trace
,
in2out
,
in2out
,
...
@@ -2512,7 +2512,7 @@ get_clients_at_depth1 = partial(get_clients_at_depth, depth=1)
...
@@ -2512,7 +2512,7 @@ get_clients_at_depth1 = partial(get_clients_at_depth, depth=1)
get_clients_at_depth2
=
partial
(
get_clients_at_depth
,
depth
=
2
)
get_clients_at_depth2
=
partial
(
get_clients_at_depth
,
depth
=
2
)
# 1+erf(x)=>erfc(-x)
# 1+erf(x)=>erfc(-x)
local_one_plus_erf
=
Pattern
Sub
(
local_one_plus_erf
=
Pattern
NodeRewriter
(
(
add
,
1
,
(
erf
,
"x"
)),
(
add
,
1
,
(
erf
,
"x"
)),
(
erfc
,
(
neg
,
"x"
)),
(
erfc
,
(
neg
,
"x"
)),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2527,7 +2527,7 @@ register_specialize(local_one_plus_erf)
...
@@ -2527,7 +2527,7 @@ register_specialize(local_one_plus_erf)
# Only one of the two rewrites below is needed if a canonicalization is added
# Only one of the two rewrites below is needed if a canonicalization is added
# for sub(x, y) -> add(x, -y) or a specialization for add(x, -y) -> sub(x, y)
# for sub(x, y) -> add(x, -y) or a specialization for add(x, -y) -> sub(x, y)
# 1-erf(x)=>erfc(x)
# 1-erf(x)=>erfc(x)
local_one_minus_erf
=
Pattern
Sub
(
local_one_minus_erf
=
Pattern
NodeRewriter
(
(
sub
,
1
,
(
erf
,
"x"
)),
(
sub
,
1
,
(
erf
,
"x"
)),
(
erfc
,
"x"
),
(
erfc
,
"x"
),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2539,7 +2539,7 @@ register_canonicalize(local_one_minus_erf)
...
@@ -2539,7 +2539,7 @@ register_canonicalize(local_one_minus_erf)
register_stabilize
(
local_one_minus_erf
)
register_stabilize
(
local_one_minus_erf
)
register_specialize
(
local_one_minus_erf
)
register_specialize
(
local_one_minus_erf
)
local_one_minus_erf2
=
Pattern
Sub
(
local_one_minus_erf2
=
Pattern
NodeRewriter
(
(
add
,
1
,
(
neg
,
(
erf
,
"x"
))),
(
add
,
1
,
(
neg
,
(
erf
,
"x"
))),
(
erfc
,
"x"
),
(
erfc
,
"x"
),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2554,7 +2554,7 @@ register_specialize(local_one_minus_erf2)
...
@@ -2554,7 +2554,7 @@ register_specialize(local_one_minus_erf2)
# (-1)+erf(x) => -erfc(x)
# (-1)+erf(x) => -erfc(x)
# There is no need for erf(x)+(-1) nor erf(x) - 1, as the canonicalize will
# There is no need for erf(x)+(-1) nor erf(x) - 1, as the canonicalize will
# convert those to the matched pattern
# convert those to the matched pattern
local_erf_minus_one
=
Pattern
Sub
(
local_erf_minus_one
=
Pattern
NodeRewriter
(
(
add
,
-
1
,
(
erf
,
"x"
)),
(
add
,
-
1
,
(
erf
,
"x"
)),
(
neg
,
(
erfc
,
"x"
)),
(
neg
,
(
erfc
,
"x"
)),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2569,7 +2569,7 @@ register_specialize(local_erf_minus_one)
...
@@ -2569,7 +2569,7 @@ register_specialize(local_erf_minus_one)
# Only one of the two rewrites below is needed if a canonicalization is added
# Only one of the two rewrites below is needed if a canonicalization is added
# for sub(x, y) -> add(x, -y) or a specialization for add(x, -y) -> sub(x, y)
# for sub(x, y) -> add(x, -y) or a specialization for add(x, -y) -> sub(x, y)
# 1-erfc(x) => erf(x)
# 1-erfc(x) => erf(x)
local_one_minus_erfc
=
Pattern
Sub
(
local_one_minus_erfc
=
Pattern
NodeRewriter
(
(
sub
,
1
,
(
erfc
,
"x"
)),
(
sub
,
1
,
(
erfc
,
"x"
)),
(
erf
,
"x"
),
(
erf
,
"x"
),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2581,7 +2581,7 @@ register_canonicalize(local_one_minus_erfc)
...
@@ -2581,7 +2581,7 @@ register_canonicalize(local_one_minus_erfc)
register_stabilize
(
local_one_minus_erfc
)
register_stabilize
(
local_one_minus_erfc
)
register_specialize
(
local_one_minus_erfc
)
register_specialize
(
local_one_minus_erfc
)
local_one_minus_erfc2
=
Pattern
Sub
(
local_one_minus_erfc2
=
Pattern
NodeRewriter
(
(
add
,
1
,
(
neg
,
(
erfc
,
"x"
))),
(
add
,
1
,
(
neg
,
(
erfc
,
"x"
))),
(
erf
,
"x"
),
(
erf
,
"x"
),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2594,7 +2594,7 @@ register_stabilize(local_one_minus_erfc2)
...
@@ -2594,7 +2594,7 @@ register_stabilize(local_one_minus_erfc2)
register_specialize
(
local_one_minus_erfc2
)
register_specialize
(
local_one_minus_erfc2
)
# (-1)+erfc(-x)=>erf(x)
# (-1)+erfc(-x)=>erf(x)
local_erf_neg_minus_one
=
Pattern
Sub
(
local_erf_neg_minus_one
=
Pattern
NodeRewriter
(
(
add
,
-
1
,
(
erfc
,
(
neg
,
"x"
))),
(
add
,
-
1
,
(
erfc
,
(
neg
,
"x"
))),
(
erf
,
"x"
),
(
erf
,
"x"
),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2914,7 +2914,7 @@ def _is_1(expr):
...
@@ -2914,7 +2914,7 @@ def _is_1(expr):
return
False
return
False
logsigm_to_softplus
=
Pattern
Sub
(
logsigm_to_softplus
=
Pattern
NodeRewriter
(
(
log
,
(
sigmoid
,
"x"
)),
(
log
,
(
sigmoid
,
"x"
)),
(
neg
,
(
softplus
,
(
neg
,
"x"
))),
(
neg
,
(
softplus
,
(
neg
,
"x"
))),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2923,7 +2923,7 @@ logsigm_to_softplus = PatternSub(
...
@@ -2923,7 +2923,7 @@ logsigm_to_softplus = PatternSub(
tracks
=
[
sigmoid
],
tracks
=
[
sigmoid
],
get_nodes
=
get_clients_at_depth1
,
get_nodes
=
get_clients_at_depth1
,
)
)
log1msigm_to_softplus
=
Pattern
Sub
(
log1msigm_to_softplus
=
Pattern
NodeRewriter
(
(
log
,
(
sub
,
dict
(
pattern
=
"y"
,
constraint
=
_is_1
),
(
sigmoid
,
"x"
))),
(
log
,
(
sub
,
dict
(
pattern
=
"y"
,
constraint
=
_is_1
),
(
sigmoid
,
"x"
))),
(
neg
,
(
softplus
,
"x"
)),
(
neg
,
(
softplus
,
"x"
)),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -2932,13 +2932,13 @@ log1msigm_to_softplus = PatternSub(
...
@@ -2932,13 +2932,13 @@ log1msigm_to_softplus = PatternSub(
tracks
=
[
sigmoid
],
tracks
=
[
sigmoid
],
get_nodes
=
get_clients_at_depth2
,
get_nodes
=
get_clients_at_depth2
,
)
)
log1pexp_to_softplus
=
Pattern
Sub
(
log1pexp_to_softplus
=
Pattern
NodeRewriter
(
(
log1p
,
(
exp
,
"x"
)),
(
log1p
,
(
exp
,
"x"
)),
(
softplus
,
"x"
),
(
softplus
,
"x"
),
values_eq_approx
=
values_eq_approx_remove_inf
,
values_eq_approx
=
values_eq_approx_remove_inf
,
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
)
)
log1p_neg_sigmoid
=
Pattern
Sub
(
log1p_neg_sigmoid
=
Pattern
NodeRewriter
(
(
log1p
,
(
neg
,
(
sigmoid
,
"x"
))),
(
log1p
,
(
neg
,
(
sigmoid
,
"x"
))),
(
neg
,
(
softplus
,
"x"
)),
(
neg
,
(
softplus
,
"x"
)),
values_eq_approx
=
values_eq_approx_remove_inf
,
values_eq_approx
=
values_eq_approx_remove_inf
,
...
@@ -3511,7 +3511,7 @@ def local_reciprocal_1_plus_exp(fgraph, node):
...
@@ -3511,7 +3511,7 @@ def local_reciprocal_1_plus_exp(fgraph, node):
# 1 - sigmoid(x) -> sigmoid(-x)
# 1 - sigmoid(x) -> sigmoid(-x)
local_1msigmoid
=
Pattern
Sub
(
local_1msigmoid
=
Pattern
NodeRewriter
(
(
sub
,
dict
(
pattern
=
"y"
,
constraint
=
_is_1
),
(
sigmoid
,
"x"
)),
(
sub
,
dict
(
pattern
=
"y"
,
constraint
=
_is_1
),
(
sigmoid
,
"x"
)),
(
sigmoid
,
(
neg
,
"x"
)),
(
sigmoid
,
(
neg
,
"x"
)),
tracks
=
[
sigmoid
],
tracks
=
[
sigmoid
],
...
@@ -3522,7 +3522,7 @@ register_stabilize(local_1msigmoid)
...
@@ -3522,7 +3522,7 @@ register_stabilize(local_1msigmoid)
register_specialize
(
local_1msigmoid
)
register_specialize
(
local_1msigmoid
)
log1pmexp_to_log1mexp
=
Pattern
Sub
(
log1pmexp_to_log1mexp
=
Pattern
NodeRewriter
(
(
log1p
,
(
neg
,
(
exp
,
"x"
))),
(
log1p
,
(
neg
,
(
exp
,
"x"
))),
(
log1mexp
,
"x"
),
(
log1mexp
,
"x"
),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
@@ -3532,7 +3532,7 @@ register_stabilize(log1pmexp_to_log1mexp, name="log1pmexp_to_log1mexp")
...
@@ -3532,7 +3532,7 @@ register_stabilize(log1pmexp_to_log1mexp, name="log1pmexp_to_log1mexp")
# log(sigmoid(x) / (1 - sigmoid(x))) -> x
# log(sigmoid(x) / (1 - sigmoid(x))) -> x
# i.e logit(sigmoid(x)) -> x
# i.e logit(sigmoid(x)) -> x
local_logit_sigmoid
=
Pattern
Sub
(
local_logit_sigmoid
=
Pattern
NodeRewriter
(
(
log
,
(
true_div
,
(
sigmoid
,
"x"
),
(
sub
,
1
,
(
sigmoid
,
"x"
)))),
(
log
,
(
true_div
,
(
sigmoid
,
"x"
),
(
sub
,
1
,
(
sigmoid
,
"x"
)))),
"x"
,
"x"
,
tracks
=
[
sigmoid
],
tracks
=
[
sigmoid
],
...
@@ -3546,7 +3546,7 @@ register_specialize(local_logit_sigmoid)
...
@@ -3546,7 +3546,7 @@ register_specialize(local_logit_sigmoid)
# sigmoid(log(x / (1-x)) -> x
# sigmoid(log(x / (1-x)) -> x
# i.e., sigmoid(logit(x)) -> x
# i.e., sigmoid(logit(x)) -> x
local_sigmoid_logit
=
Pattern
Sub
(
local_sigmoid_logit
=
Pattern
NodeRewriter
(
(
sigmoid
,
(
log
,
(
true_div
,
"x"
,
(
sub
,
1
,
"x"
)))),
(
sigmoid
,
(
log
,
(
true_div
,
"x"
,
(
sub
,
1
,
"x"
)))),
"x"
,
"x"
,
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
...
...
doc/extending/graph_rewriting.rst
浏览文件 @
6302cef1
...
@@ -89,7 +89,7 @@ For starters, let's define the following simplification:
...
@@ -89,7 +89,7 @@ For starters, let's define the following simplification:
\frac{xy}{y} = x
\frac{xy}{y} = x
We will implement it in three ways: using a global optimization, a
We will implement it in three ways: using a global optimization, a
local optimization with a :class:`NavigatorOptimizer` and then using the :class:`Pattern
Sub
`
local optimization with a :class:`NavigatorOptimizer` and then using the :class:`Pattern
NodeRewriter
`
facility.
facility.
Global optimization
Global optimization
...
@@ -270,8 +270,8 @@ FunctionGraph(add(z, mul(true_div(mul(y, x), y), true_div(z, x))))
...
@@ -270,8 +270,8 @@ FunctionGraph(add(z, mul(true_div(mul(y, x), y), true_div(z, x))))
>>> e
>>> e
FunctionGraph(add(z, mul(x, true_div(z, x))))
FunctionGraph(add(z, mul(x, true_div(z, x))))
:class:`SubstitutionNodeRewriter`, :class:`RemovalNodeRewriter`, :class:`Pattern
Sub
`
:class:`SubstitutionNodeRewriter`, :class:`RemovalNodeRewriter`, :class:`Pattern
NodeRewriter
`
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++
Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
...
@@ -288,15 +288,15 @@ Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
...
@@ -288,15 +288,15 @@ Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
outputs as it has inputs. The first output becomes the first input,
outputs as it has inputs. The first output becomes the first input,
the second output becomes the second input, and so on.
the second output becomes the second input, and so on.
.. function:: Pattern
Sub
(pattern1, pattern2)
.. function:: Pattern
NodeRewriter
(pattern1, pattern2)
Replaces all occurrences of the first pattern by the second pattern.
Replaces all occurrences of the first pattern by the second pattern.
See :class:`Pattern
Sub
`.
See :class:`Pattern
NodeRewriter
`.
.. code::
.. code::
from aesara.scalar import identity
from aesara.scalar import identity
from aesara.graph.opt import SubstitutionNodeRewriter, RemovalNodeRewriter, Pattern
Sub
from aesara.graph.opt import SubstitutionNodeRewriter, RemovalNodeRewriter, Pattern
NodeRewriter
# Replacing `add` by `mul` (this is not recommended for primarily
# Replacing `add` by `mul` (this is not recommended for primarily
# mathematical reasons):
# mathematical reasons):
...
@@ -308,25 +308,25 @@ Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
...
@@ -308,25 +308,25 @@ Aesara defines some shortcuts to make :class:`NodeRewriter`\s:
# The "simplify" operation we've been defining in the past few
# The "simplify" operation we've been defining in the past few
# sections. Note that we need two patterns to account for the
# sections. Note that we need two patterns to account for the
# permutations of the arguments to `mul`.
# permutations of the arguments to `mul`.
local_simplify_1 = Pattern
Sub
((true_div, (mul, 'x', 'y'), 'y'), 'x')
local_simplify_1 = Pattern
NodeRewriter
((true_div, (mul, 'x', 'y'), 'y'), 'x')
local_simplify_2 = Pattern
Sub
((true_div, (mul, 'x', 'y'), 'x'), 'y')
local_simplify_2 = Pattern
NodeRewriter
((true_div, (mul, 'x', 'y'), 'x'), 'y')
.. note::
.. note::
:class:`SubstitutionNodeRewriter`, :class:`RemovalNodeRewriter` and :class:`Pattern
Sub
` produce local optimizers, which
:class:`SubstitutionNodeRewriter`, :class:`RemovalNodeRewriter` and :class:`Pattern
NodeRewriter
` produce local optimizers, which
means that everything we said previously about local optimizers
means that everything we said previously about local optimizers
apply (e.g. they need to be wrapped in a :class:`NavigatorOptimizer`, etc.)
apply (e.g. they need to be wrapped in a :class:`NavigatorOptimizer`, etc.)
When an optimization can be naturally expressed using :class:`SubstitutionNodeRewriter`, :class:`RemovalNodeRewriter`
When an optimization can be naturally expressed using :class:`SubstitutionNodeRewriter`, :class:`RemovalNodeRewriter`
or :class:`Pattern
Sub
`, it is highly recommended to use them.
or :class:`Pattern
NodeRewriter
`, it is highly recommended to use them.
.. _unification:
.. _unification:
Unification and reification
Unification and reification
===========================
===========================
The :class:`Pattern
Sub
` class uses `unification and reification
The :class:`Pattern
NodeRewriter
` class uses `unification and reification
<https://en.wikipedia.org/wiki/Unification_(computer_science)>`_ to implement a
<https://en.wikipedia.org/wiki/Unification_(computer_science)>`_ to implement a
more succinct and reusable form of "pattern matching and replacement".
more succinct and reusable form of "pattern matching and replacement".
In general, *use of the unification and reification tools is preferable when
In general, *use of the unification and reification tools is preferable when
...
@@ -345,7 +345,7 @@ In order to use :func:`unify` and :func:`reify` with Aesara graphs, we need an i
...
@@ -345,7 +345,7 @@ In order to use :func:`unify` and :func:`reify` with Aesara graphs, we need an i
structure that will allow us to represent Aesara graphs that contain :class:`var`\s, because
structure that will allow us to represent Aesara graphs that contain :class:`var`\s, because
Aesara :class:`Op`\s and :class:`Apply` nodes will not accept these foreign objects as inputs.
Aesara :class:`Op`\s and :class:`Apply` nodes will not accept these foreign objects as inputs.
:class:`Pattern
Sub
` uses Python ``tuple``\s to effectively represent :class:`Apply` nodes and
:class:`Pattern
NodeRewriter
` uses Python ``tuple``\s to effectively represent :class:`Apply` nodes and
``str``\s to represent logic variables (i.e. :class:`var`\s in the :mod:`unification` library).
``str``\s to represent logic variables (i.e. :class:`var`\s in the :mod:`unification` library).
Behind the scenes, these ``tuple``\s are converted to a ``tuple`` subclass called :class:`ExpressionTuple`\s,
Behind the scenes, these ``tuple``\s are converted to a ``tuple`` subclass called :class:`ExpressionTuple`\s,
which behave just like normal ``tuple``\s except for some special caching features that allow for easy
which behave just like normal ``tuple``\s except for some special caching features that allow for easy
...
...
tests/compile/function/test_types.py
浏览文件 @
6302cef1
...
@@ -13,7 +13,7 @@ from aesara.compile.io import In, Out
...
@@ -13,7 +13,7 @@ from aesara.compile.io import In, Out
from
aesara.compile.mode
import
Mode
,
get_default_mode
from
aesara.compile.mode
import
Mode
,
get_default_mode
from
aesara.configdefaults
import
config
from
aesara.configdefaults
import
config
from
aesara.graph.basic
import
Constant
from
aesara.graph.basic
import
Constant
from
aesara.graph.opt
import
OpKeyOptimizer
,
Pattern
Sub
from
aesara.graph.opt
import
OpKeyOptimizer
,
Pattern
NodeRewriter
from
aesara.graph.utils
import
MissingInputError
from
aesara.graph.utils
import
MissingInputError
from
aesara.link.vm
import
VMLinker
from
aesara.link.vm
import
VMLinker
from
aesara.tensor.math
import
dot
from
aesara.tensor.math
import
dot
...
@@ -35,7 +35,7 @@ from aesara.utils import exc_message
...
@@ -35,7 +35,7 @@ from aesara.utils import exc_message
def
PatternOptimizer
(
p1
,
p2
,
ign
=
True
):
def
PatternOptimizer
(
p1
,
p2
,
ign
=
True
):
return
OpKeyOptimizer
(
Pattern
Sub
(
p1
,
p2
),
ignore_newtrees
=
ign
)
return
OpKeyOptimizer
(
Pattern
NodeRewriter
(
p1
,
p2
),
ignore_newtrees
=
ign
)
class
TestFunction
:
class
TestFunction
:
...
...
tests/graph/test_destroyhandler.py
浏览文件 @
6302cef1
...
@@ -11,7 +11,7 @@ from aesara.graph.op import Op
...
@@ -11,7 +11,7 @@ from aesara.graph.op import Op
from
aesara.graph.opt
import
(
from
aesara.graph.opt
import
(
NavigatorOptimizer
,
NavigatorOptimizer
,
OpKeyOptimizer
,
OpKeyOptimizer
,
Pattern
Sub
,
Pattern
NodeRewriter
,
SubstitutionNodeRewriter
,
SubstitutionNodeRewriter
,
TopoOptimizer
,
TopoOptimizer
,
)
)
...
@@ -21,7 +21,7 @@ from tests.unittest_tools import assertFailure_fast
...
@@ -21,7 +21,7 @@ from tests.unittest_tools import assertFailure_fast
def
PatternOptimizer
(
p1
,
p2
,
ign
=
True
):
def
PatternOptimizer
(
p1
,
p2
,
ign
=
True
):
return
OpKeyOptimizer
(
Pattern
Sub
(
p1
,
p2
),
ignore_newtrees
=
ign
)
return
OpKeyOptimizer
(
Pattern
NodeRewriter
(
p1
,
p2
),
ignore_newtrees
=
ign
)
def
TopoSubstitutionNodeRewriter
(
def
TopoSubstitutionNodeRewriter
(
...
...
tests/graph/test_opt.py
浏览文件 @
6302cef1
...
@@ -10,7 +10,7 @@ from aesara.graph.opt import (
...
@@ -10,7 +10,7 @@ from aesara.graph.opt import (
MergeOptimizer
,
MergeOptimizer
,
OpKeyOptimizer
,
OpKeyOptimizer
,
OpToRewriterTracker
,
OpToRewriterTracker
,
Pattern
Sub
,
Pattern
NodeRewriter
,
SequentialNodeRewriter
,
SequentialNodeRewriter
,
SubstitutionNodeRewriter
,
SubstitutionNodeRewriter
,
TopoOptimizer
,
TopoOptimizer
,
...
@@ -51,11 +51,11 @@ class AssertNoChanges(Feature):
...
@@ -51,11 +51,11 @@ class AssertNoChanges(Feature):
def
PatternOptimizer
(
p1
,
p2
,
ign
=
False
):
def
PatternOptimizer
(
p1
,
p2
,
ign
=
False
):
return
OpKeyOptimizer
(
Pattern
Sub
(
p1
,
p2
),
ignore_newtrees
=
ign
)
return
OpKeyOptimizer
(
Pattern
NodeRewriter
(
p1
,
p2
),
ignore_newtrees
=
ign
)
def
TopoPatternOptimizer
(
p1
,
p2
,
ign
=
True
):
def
TopoPatternOptimizer
(
p1
,
p2
,
ign
=
True
):
return
TopoOptimizer
(
Pattern
Sub
(
p1
,
p2
),
ignore_newtrees
=
ign
)
return
TopoOptimizer
(
Pattern
NodeRewriter
(
p1
,
p2
),
ignore_newtrees
=
ign
)
class
TestPatternOptimizer
:
class
TestPatternOptimizer
:
...
@@ -448,9 +448,9 @@ class TestEquilibrium:
...
@@ -448,9 +448,9 @@ class TestEquilibrium:
# print g
# print g
opt
=
EquilibriumOptimizer
(
opt
=
EquilibriumOptimizer
(
[
[
Pattern
Sub
((
op1
,
"x"
,
"y"
),
(
op2
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op1
,
"x"
,
"y"
),
(
op2
,
"x"
,
"y"
)),
Pattern
Sub
((
op4
,
"x"
,
"y"
),
(
op1
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op4
,
"x"
,
"y"
),
(
op1
,
"x"
,
"y"
)),
Pattern
Sub
((
op3
,
(
op2
,
"x"
,
"y"
)),
(
op4
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op3
,
(
op2
,
"x"
,
"y"
)),
(
op4
,
"x"
,
"y"
)),
],
],
max_use_ratio
=
10
,
max_use_ratio
=
10
,
)
)
...
@@ -465,11 +465,11 @@ class TestEquilibrium:
...
@@ -465,11 +465,11 @@ class TestEquilibrium:
# print g
# print g
opt
=
EquilibriumOptimizer
(
opt
=
EquilibriumOptimizer
(
[
[
Pattern
Sub
((
op1
,
(
op2
,
"x"
,
"y"
)),
(
op4
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op1
,
(
op2
,
"x"
,
"y"
)),
(
op4
,
"x"
,
"y"
)),
Pattern
Sub
((
op3
,
"x"
,
"y"
),
(
op4
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op3
,
"x"
,
"y"
),
(
op4
,
"x"
,
"y"
)),
Pattern
Sub
((
op4
,
"x"
,
"y"
),
(
op5
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op4
,
"x"
,
"y"
),
(
op5
,
"x"
,
"y"
)),
Pattern
Sub
((
op5
,
"x"
,
"y"
),
(
op6
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op5
,
"x"
,
"y"
),
(
op6
,
"x"
,
"y"
)),
Pattern
Sub
((
op6
,
"x"
,
"y"
),
(
op2
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op6
,
"x"
,
"y"
),
(
op2
,
"x"
,
"y"
)),
],
],
max_use_ratio
=
10
,
max_use_ratio
=
10
,
)
)
...
@@ -490,9 +490,9 @@ class TestEquilibrium:
...
@@ -490,9 +490,9 @@ class TestEquilibrium:
try
:
try
:
opt
=
EquilibriumOptimizer
(
opt
=
EquilibriumOptimizer
(
[
[
Pattern
Sub
((
op1
,
"x"
,
"y"
),
(
op2
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op1
,
"x"
,
"y"
),
(
op2
,
"x"
,
"y"
)),
Pattern
Sub
((
op4
,
"x"
,
"y"
),
(
op1
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op4
,
"x"
,
"y"
),
(
op1
,
"x"
,
"y"
)),
Pattern
Sub
((
op3
,
(
op2
,
"x"
,
"y"
)),
(
op4
,
"x"
,
"y"
)),
Pattern
NodeRewriter
((
op3
,
(
op2
,
"x"
,
"y"
)),
(
op4
,
"x"
,
"y"
)),
],
],
max_use_ratio
=
1.0
/
len
(
g
.
apply_nodes
),
max_use_ratio
=
1.0
/
len
(
g
.
apply_nodes
),
)
# each opt can only be applied once
)
# each opt can only be applied once
...
@@ -595,14 +595,14 @@ def test_pre_greedy_node_rewriter():
...
@@ -595,14 +595,14 @@ def test_pre_greedy_node_rewriter():
@pytest.mark.parametrize
(
"tracks"
,
[
True
,
False
])
@pytest.mark.parametrize
(
"tracks"
,
[
True
,
False
])
@pytest.mark.parametrize
(
"out_pattern"
,
[(
op2
,
"x"
),
"x"
,
1.0
])
@pytest.mark.parametrize
(
"out_pattern"
,
[(
op2
,
"x"
),
"x"
,
1.0
])
def
test_patternsub_values_eq_approx
(
out_pattern
,
tracks
):
def
test_patternsub_values_eq_approx
(
out_pattern
,
tracks
):
# Pattern
Sub
would fail when `values_eq_approx` and `get_nodes` were specified
# Pattern
NodeRewriter
would fail when `values_eq_approx` and `get_nodes` were specified
x
=
MyVariable
(
"x"
)
x
=
MyVariable
(
"x"
)
e
=
op1
(
x
)
e
=
op1
(
x
)
fg
=
FunctionGraph
([
x
],
[
e
],
clone
=
False
)
fg
=
FunctionGraph
([
x
],
[
e
],
clone
=
False
)
opt
=
EquilibriumOptimizer
(
opt
=
EquilibriumOptimizer
(
[
[
Pattern
Sub
(
Pattern
NodeRewriter
(
(
op1
,
"x"
),
(
op1
,
"x"
),
out_pattern
,
out_pattern
,
tracks
=
[
op1
]
if
tracks
else
(),
tracks
=
[
op1
]
if
tracks
else
(),
...
@@ -628,14 +628,14 @@ def test_patternsub_values_eq_approx(out_pattern, tracks):
...
@@ -628,14 +628,14 @@ def test_patternsub_values_eq_approx(out_pattern, tracks):
@pytest.mark.parametrize
(
"out_pattern"
,
[(
op1
,
"x"
),
"x"
])
@pytest.mark.parametrize
(
"out_pattern"
,
[(
op1
,
"x"
),
"x"
])
def
test_patternsub_invalid_dtype
(
out_pattern
):
def
test_patternsub_invalid_dtype
(
out_pattern
):
# Pattern
Sub
would wrongly return output of different dtype as the original node
# Pattern
NodeRewriter
would wrongly return output of different dtype as the original node
x
=
MyVariable
(
"x"
)
x
=
MyVariable
(
"x"
)
e
=
op_cast_type2
(
x
)
e
=
op_cast_type2
(
x
)
fg
=
FunctionGraph
([
x
],
[
e
])
fg
=
FunctionGraph
([
x
],
[
e
])
opt
=
EquilibriumOptimizer
(
opt
=
EquilibriumOptimizer
(
[
[
Pattern
Sub
(
Pattern
NodeRewriter
(
(
op_cast_type2
,
"x"
),
(
op_cast_type2
,
"x"
),
out_pattern
,
out_pattern
,
)
)
...
@@ -647,8 +647,8 @@ def test_patternsub_invalid_dtype(out_pattern):
...
@@ -647,8 +647,8 @@ def test_patternsub_invalid_dtype(out_pattern):
def
test_patternsub_different_output_lengths
():
def
test_patternsub_different_output_lengths
():
# Test that Pattern
Sub
won't replace nodes with different numbers of outputs
# Test that Pattern
NodeRewriter
won't replace nodes with different numbers of outputs
ps
=
Pattern
Sub
(
ps
=
Pattern
NodeRewriter
(
(
op1
,
"x"
),
(
op1
,
"x"
),
(
"x"
),
(
"x"
),
name
=
"ps"
,
name
=
"ps"
,
...
...
tests/tensor/test_math_opt.py
浏览文件 @
6302cef1
...
@@ -4312,7 +4312,7 @@ class TestSigmoidOpts:
...
@@ -4312,7 +4312,7 @@ class TestSigmoidOpts:
# tests exp_over_1_plus_exp
# tests exp_over_1_plus_exp
f
=
aesara
.
function
([
x
],
1
-
exp
(
x
)
/
(
1
+
exp
(
x
)),
mode
=
m
)
f
=
aesara
.
function
([
x
],
1
-
exp
(
x
)
/
(
1
+
exp
(
x
)),
mode
=
m
)
# FIXME: Pattern
Sub
does not copy stack trace
# FIXME: Pattern
NodeRewriter
does not copy stack trace
# (see https://github.com/Theano/Theano/issues/4581)
# (see https://github.com/Theano/Theano/issues/4581)
# assert check_stack_trace(f, ops_to_check=[neg, sigmoid])
# assert check_stack_trace(f, ops_to_check=[neg, sigmoid])
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
==
[
neg
,
sigmoid
]
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
==
[
neg
,
sigmoid
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论