Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
77f434b8
提交
77f434b8
authored
12月 11, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
12月 13, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add a section for unification in graph_rewriting docs
上级
806709aa
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
94 行增加
和
1 行删除
+94
-1
graph_rewriting.rst
doc/extending/graph_rewriting.rst
+94
-1
没有找到文件。
doc/extending/graph_rewriting.rst
浏览文件 @
77f434b8
...
@@ -321,7 +321,100 @@ Aesara defines some shortcuts to make :class:`LocalOptimizer`\s:
...
@@ -321,7 +321,100 @@ Aesara defines some shortcuts to make :class:`LocalOptimizer`\s:
When an optimization can be naturally expressed using :class:`OpSub`, :class:`OpRemove`
When an optimization can be naturally expressed using :class:`OpSub`, :class:`OpRemove`
or :class:`PatternSub`, it is highly recommended to use them.
or :class:`PatternSub`, it is highly recommended to use them.
.. _unification:
Unification and reification
===========================
The :class:`PatternSub` class uses `unification and reification
<https://en.wikipedia.org/wiki/Unification_(computer_science)>`_ to implement a
more succinct and reusable form of "pattern matching and replacement".
In general, *use of the unification and reification tools is preferable when
a rewrite's matching and replacement are non-trivial*, so we will briefly explain
them in the following.
Aesara's unification and reification tools are provided by the
`logical-unification <https://github.com/pythological/unification>`_ package.
The basic tools are :func:`unify`, :func:`reify`, and :class:`var`. The class :class:`var`
construct *logic variables*, which represent the elements to be unified/matched, :func:`unify`
performs the "matching", and :func:`reify` performs the "replacements".
See :mod:`unification`'s documentation for an introduction to unification and reification.
In order to use :func:`unify` and :func:`reify` with Aesara graphs, we need an intermediate
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.
:class:`PatternSub` 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).
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
evaluation and caching. These :class:`ExpressionTuple`\s are provided by the
`etuples <https://github.com/pythological/etuples>`_ library.
Here is an illustration of all the above components used together:
>>> from unification import unify, reify, var
>>> from etuples import etuple
>>> y_lv = var() # Create a logic variable
>>> y_lv
~_1
>>> s = unify(add(x, y), etuple(add, x, y_lv))
>>> s
{~_1: y}
In this example, :func:`unify` matched the Aesara graph in the first argument with the "pattern"
given by the :func:`etuple` in the second. The result is a ``dict`` mapping logic variables to
the objects to which they were successfully unified. When a :func:`unify` doesn't succeed, it will
return ``False``.
:func:`reify` uses ``dict``\s like the kind produced by :func:`unify` to replace
logic variables within structures:
>>> res = reify(etuple(add, y_lv, y_lv), s)
>>> res
e(<aesara.scalar.basic.Add at 0x7f54dfa5a350>, y, y)
Since :class:`ExpressionTuple`\s can be evaluated, we can produce a complete Aesara graph from these
results as follows:
>>> res.evaled_obj
add.0
>>> aesara.dprint(res.evaled_obj)
add [id A] ''
|y [id B]
|y [id B]
Because :class:`ExpressionTuple`\s effectively model `S-expressions
<https://en.wikipedia.org/wiki/S-expression>`_, they can be used with the `cons
<https://github.com/pythological/python-cons>`_ package to unify and reify
graphs structurally.
Let's say we want to match graphs that use the :class:`add`\ :class:`Op` but could have a
varying number of arguments:
>>> from cons import cons
>>> op_lv = var()
>>> args_lv = var()
>>> s = unify(cons(op_lv, args_lv), add(x, y))
>>> s
{~_2: <aesara.scalar.basic.Add at 0x7f54dfa5a350>, ~_3: e(x, y)}
>>> s = unify(cons(op_lv, args_lv), add(x, y, z))
>>> s
{~_2: <aesara.scalar.basic.Add at 0x7f54dfa5a350>, ~_3: e(x, y, z)}
From here, we can check ``s[op_lv] == add`` to confirm that we have the correct :class:`Op` and
proceed with our rewrite.
>>> res = reify(cons(mul, args_lv), s)
>>> res
e(<aesara.scalar.basic.Mul at 0x7f54dfa5ae10>, x, y, z)
>>> aesara.dprint(res.evaled_obj)
mul [id A] ''
|x [id B]
|y [id C]
|z [id D]
.. _optdb:
.. _optdb:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论