Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0c987405
提交
0c987405
authored
3月 21, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
documented PatternOptimizer and added the option to set arbitrary constraints
上级
0bb9219e
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
73 行增加
和
6 行删除
+73
-6
_test_opt.py
gof/_test_opt.py
+13
-0
opt.py
gof/opt.py
+60
-6
没有找到文件。
gof/_test_opt.py
浏览文件 @
0c987405
...
@@ -182,6 +182,19 @@ class _test_PatternOptimizer(unittest.TestCase):
...
@@ -182,6 +182,19 @@ class _test_PatternOptimizer(unittest.TestCase):
(
Op2
,
'1'
,
z
))
.
optimize
(
g
)
(
Op2
,
'1'
,
z
))
.
optimize
(
g
)
assert
str
(
g
)
==
"[Op1(Op2(y, z), y)]"
assert
str
(
g
)
==
"[Op1(Op2(y, z), y)]"
def
test_constraints
(
self
):
x
,
y
,
z
=
inputs
()
e
=
op4
(
op1
(
op2
(
x
,
y
)),
op1
(
op1
(
x
,
y
)))
g
=
env
([
x
,
y
,
z
],
[
e
])
def
constraint
(
env
,
r
):
# Only replacing if the input is an instance of Op2
return
isinstance
(
r
.
owner
,
Op2
)
PatternOptimizer
((
Op1
,
{
'pattern'
:
'1'
,
'constraint'
:
constraint
}),
(
Op3
,
'1'
))
.
optimize
(
g
)
assert
str
(
g
)
==
"[Op4(Op3(Op2(x, y)), Op1(Op1(x, y)))]"
class
_test_OpSubOptimizer
(
unittest
.
TestCase
):
class
_test_OpSubOptimizer
(
unittest
.
TestCase
):
...
...
gof/opt.py
浏览文件 @
0c987405
...
@@ -209,19 +209,63 @@ class OpRemover(Optimizer):
...
@@ -209,19 +209,63 @@ class OpRemover(Optimizer):
class
PatternOptimizer
(
OpSpecificOptimizer
):
class
PatternOptimizer
(
OpSpecificOptimizer
):
"""
"""
Replaces all occurrences of the first pattern by the second pattern.
Replaces all occurrences of the input pattern by the output pattern.
"""
input_pattern ::= (OpClass, <sub_pattern1>, <sub_pattern2>, ...)
def
__init__
(
self
,
in_pattern
,
out_pattern
,
failure_callback
=
None
):
input_pattern ::= dict(pattern = <input_pattern>,
constraint = <constraint>)
sub_pattern ::= input_pattern
sub_pattern ::= string
sub_pattern ::= a Result r such that r.constant is True
constraint ::= lambda env, expr: additional matching condition
output_pattern ::= (OpClass, <output_pattern1>, <output_pattern2>, ...)
output_pattern ::= string
Each string in the input pattern is a variable that will be set to
whatever expression is found in its place. If the same string is
used more than once, the same expression must be found in those
places. If a string used in the input pattern is used in the
output pattern, the matching expression will be inserted in its
place. The input pattern cannot just be a string but the output
pattern can.
If you put a constant result in the input pattern, there will be a
match iff a constant result with the same value is found in its
place.
You can add a constraint to the match by using the dict(...) form
described above with a 'constraint' key. The constraint must be a
function that takes the env and the current Result that we are
trying to match and returns True or False according to an
arbitrary criterion.
Examples:
PatternOptimizer((Add, 'x', 'y'), (Add, 'y', 'x'))
PatternOptimizer((Multiply, 'x', 'x'), (Square, 'x'))
PatternOptimizer((Subtract, (Add, 'x', 'y'), 'y'), 'x')
PatternOptimizer((Power, 'x', Double(2.0, constant = True)), (Square, 'x'))
PatternOptimizer((Boggle, {'pattern': 'x',
'constraint': lambda env, expr: expr.owner.scrabble == True}),
(Scrabble, 'x'))
"""
def
__init__
(
self
,
in_pattern
,
out_pattern
,
allow_multiple_clients
=
False
,
failure_callback
=
None
):
"""
"""
Sets in_pattern for replacement by out_pattern.
Sets in_pattern for replacement by out_pattern.
self.opclass is set to in_pattern[0] to accelerate the search.
self.opclass is set to in_pattern[0] to accelerate the search.
"""
"""
self
.
in_pattern
=
in_pattern
self
.
in_pattern
=
in_pattern
self
.
out_pattern
=
out_pattern
self
.
out_pattern
=
out_pattern
if
isinstance
(
in_pattern
,
(
list
,
tuple
)):
self
.
opclass
=
self
.
in_pattern
[
0
]
self
.
opclass
=
self
.
in_pattern
[
0
]
elif
isinstance
(
in_pattern
,
dict
):
self
.
opclass
=
self
.
in_pattern
[
'pattern'
][
0
]
else
:
raise
TypeError
(
"The pattern to search for must start with a specific Op class."
)
self
.
__doc__
=
self
.
__class__
.
__doc__
+
"
\n\n
This instance does: "
+
str
(
self
)
+
"
\n
"
self
.
__doc__
=
self
.
__class__
.
__doc__
+
"
\n\n
This instance does: "
+
str
(
self
)
+
"
\n
"
self
.
failure_callback
=
failure_callback
self
.
failure_callback
=
failure_callback
self
.
allow_multiple_clients
=
allow_multiple_clients
def
apply_on_op
(
self
,
env
,
op
):
def
apply_on_op
(
self
,
env
,
op
):
"""
"""
...
@@ -231,11 +275,13 @@ class PatternOptimizer(OpSpecificOptimizer):
...
@@ -231,11 +275,13 @@ class PatternOptimizer(OpSpecificOptimizer):
If self.failure_callback is not None, if there is a match but a
If self.failure_callback is not None, if there is a match but a
replacement fails to occur, the callback will be called with
replacement fails to occur, the callback will be called with
arguments (results_to_replace, replacement, exception).
arguments (results_to_replace, replacement, exception).
"""
If self.allow_multiple_clients is False, he pattern matching will fail
if one of the subpatterns has more than one client.
"""
def
match
(
pattern
,
expr
,
u
,
first
=
False
):
def
match
(
pattern
,
expr
,
u
,
first
=
False
):
if
isinstance
(
pattern
,
(
list
,
tuple
)):
if
isinstance
(
pattern
,
(
list
,
tuple
)):
if
not
issubclass
(
expr
.
owner
.
__class__
,
pattern
[
0
])
or
(
not
first
and
env
.
nclients
(
expr
.
owner
)
>
1
):
if
not
issubclass
(
expr
.
owner
.
__class__
,
pattern
[
0
])
or
(
self
.
allow_multiple_clients
and
not
first
and
env
.
nclients
(
expr
.
owner
)
>
1
):
return
False
return
False
if
len
(
pattern
)
-
1
!=
len
(
expr
.
owner
.
inputs
):
if
len
(
pattern
)
-
1
!=
len
(
expr
.
owner
.
inputs
):
return
False
return
False
...
@@ -243,6 +289,14 @@ class PatternOptimizer(OpSpecificOptimizer):
...
@@ -243,6 +289,14 @@ class PatternOptimizer(OpSpecificOptimizer):
u
=
match
(
p
,
v
,
u
)
u
=
match
(
p
,
v
,
u
)
if
not
u
:
if
not
u
:
return
False
return
False
elif
isinstance
(
pattern
,
dict
):
try
:
real_pattern
=
pattern
[
'pattern'
]
constraint
=
pattern
[
'constraint'
]
except
KeyError
:
raise
KeyError
(
"Malformed pattern:
%
s (expected keys pattern and constraint)"
%
pattern
)
if
constraint
(
env
,
expr
):
return
match
(
real_pattern
,
expr
,
u
,
False
)
elif
isinstance
(
pattern
,
str
):
elif
isinstance
(
pattern
,
str
):
v
=
unify
.
Var
(
pattern
)
v
=
unify
.
Var
(
pattern
)
if
u
[
v
]
is
not
v
and
u
[
v
]
is
not
expr
:
if
u
[
v
]
is
not
v
and
u
[
v
]
is
not
expr
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论