.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples_patterns/plot_pattern_replay.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_patterns_plot_pattern_replay.py: .. _l-example-plot-pattern-replay: Replaying graph-rewriting patterns ================================== Every successful pattern optimization produces a :class:`~onnx_light.onnx_core.optimization.LocalRewriting` record. These records can reconstruct the optimized graph from the original model without running pattern matching again. .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python # sphinx_gallery_thumbnail_path = "_static/gallery_thumbnails/pattern_replay.png" from __future__ import annotations from onnx_light.onnx_lib import parser from onnx_light.onnx_core.optimization import GraphBuilder, GraphGraph, replay, standard_patterns from onnx_light.tools import pretty_onnx .. GENERATED FROM PYTHON SOURCE LINES 22-27 Create and optimize the source model ++++++++++++++++++++++++++++++++++++ The source graph contains two type-preserving ``Cast`` nodes. The optimizer rewrites them and returns the corresponding modification records. .. GENERATED FROM PYTHON SOURCE LINES 27-47 .. code-block:: Python model = parser.parse_model( '\n' "agraph (float[4] x) => (float[4] y) {\n" " casted = Cast (x)\n" " negated = Neg(casted)\n" " y = Cast (negated)\n" "}\n" ) builder = GraphBuilder(model) graph = GraphGraph(builder, standard_patterns(["Cast"])) rewrites = graph.optimize() optimized_graph = builder.build_graph() print("Original graph:") print(pretty_onnx(model)) print("Optimized graph:") print(pretty_onnx(builder.to_onnx("model"))) .. rst-class:: sphx-glr-script-out .. code-block:: none Original graph: opset: domain='' version=18 graph: name='agraph' input: float[4] x 0: Cast(x) -> casted 1: Neg(casted) -> negated 2: Cast(negated) -> y output: float[4] y Optimized graph: opset: domain='ai.onnx' version=18 graph: name='agraph' input: float[4] x 0: Neg(x) -> negated 1: Identity(negated) -> y output: float[4] y .. GENERATED FROM PYTHON SOURCE LINES 48-54 Inspect the captured modifications ++++++++++++++++++++++++++++++++++ Each record has a concise one-line display. Use ``to_detailed_string`` to inspect the pattern, matched nodes, inserted nodes, and optimization iteration needed to reproduce one modification. .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. code-block:: Python for rewrite in rewrites: print(rewrite) print(rewrite.to_detailed_string()) .. rst-class:: sphx-glr-script-out .. code-block:: none LocalRewriting(pattern=Cast, graph_path=, matched_nodes=1, added_nodes=1) LocalRewriting: pattern: Cast graph_path: iteration: 0 matched_nodes: positions: [0] added_nodes: nodes: [Identity(outputs=[casted])] positions: [0] initializers: added: [] added_positions: [] removed: [] value_renames: [] timings: match_time_ns: 2695 apply_time_ns: 3897 LocalRewriting(pattern=Cast, graph_path=, matched_nodes=1, added_nodes=1) LocalRewriting: pattern: Cast graph_path: iteration: 0 matched_nodes: positions: [2] added_nodes: nodes: [Identity(outputs=[y])] positions: [2] initializers: added: [] added_positions: [] removed: [] value_renames: [] timings: match_time_ns: 231 apply_time_ns: 821 LocalRewriting(pattern=RemoveIdentityNodes, graph_path=, matched_nodes=3, added_nodes=2) LocalRewriting: pattern: RemoveIdentityNodes graph_path: iteration: 1 matched_nodes: positions: [0, 1, 2] added_nodes: nodes: [Neg(outputs=[negated]), Identity(outputs=[y])] positions: [0, 1] initializers: added: [] added_positions: [] removed: [] value_renames: [casted->x] timings: match_time_ns: 0 apply_time_ns: 0 .. GENERATED FROM PYTHON SOURCE LINES 60-66 Replay without matching patterns ++++++++++++++++++++++++++++++++ :func:`~onnx_light.onnx_core.optimization.replay` applies the captured records to a fresh copy of the source model. The reconstructed graph is byte-for-byte identical to the graph produced by the optimizer. .. GENERATED FROM PYTHON SOURCE LINES 66-72 .. code-block:: Python replayed_graph = replay(model, rewrites) assert replayed_graph.SerializeToString() == optimized_graph.SerializeToString() print("Replayed graph:") print(pretty_onnx(replayed_graph)) .. rst-class:: sphx-glr-script-out .. code-block:: none Replayed graph: graph: name='agraph' input: float[4] x 0: Neg(x) -> negated 1: Identity(negated) -> y output: float[4] y .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.004 seconds) .. _sphx_glr_download_auto_examples_patterns_plot_pattern_replay.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pattern_replay.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pattern_replay.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_pattern_replay.zip ` .. include:: plot_pattern_replay.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_