提交 bcea84cf authored 作者: Olivier Breuleux's avatar Olivier Breuleux

more optimizer doc

上级 8bdc44e9
...@@ -211,12 +211,49 @@ for this somewhere in the future. ...@@ -211,12 +211,49 @@ for this somewhere in the future.
Local optimization Local optimization
------------------ ------------------
The local version of the above code would be the following:
.. code-block:: python
class LocalSimplify(gof.LocalOptimizer):
def transform(self, node):
if node.op == div:
x, y = node.inputs
if x.owner and x.owner.op == mul:
a, b = x.owner.inputs
if y == a:
return [b]
elif y == b:
return [a]
return False
local_simplify = LocalSimplify()
The definition of transform is the inner loop of the global optimizer,
where the node is given as argument. If no changes are to be made,
False must be returned. Else, a list of what to replace the node's
outputs with must be returned.
In order to apply the local optimizer we must use it in conjunction
with a :ref:`navigator`. You can follow this :ref:`link <navigator>`
for further documentation, but basically a Navigator is a global
optimizer that loops through all nodes in the graph (or a well-defined
subset of them) and applies one or several local optimizers on them.
>>> x = double('x')
>>> y = double('y')
>>> z = double('z')
>>> a = add(z, mul(div(mul(y, x), y), div(z, x)))
>>> e = gof.Env([x, y, z], [a])
>>> e
[add(z, mul(div(mul(y, x), y), div(z, x)))]
>>> simplify = gof.TopoOptimizer([local_simplify])
>>> simplify.optimize(e)
>>> e
[add(z, mul(x, div(z, x)))]
TODO: test this.
The optimization database (optdb) The optimization database (optdb)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论