.. 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_cleanup.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_cleanup.py: .. _l-example-plot-pattern-replay-cleanup: Replaying graph cleanup modifications ===================================== Graph cleanup algorithms also produce :class:`~onnx_light.onnx_core.optimization.LocalRewriting` records. These records replay identity removal, dead-end removal, and initializer deduplication without running cleanup again. .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python # sphinx_gallery_thumbnail_path = "_static/gallery_thumbnails/pattern_replay_cleanup.png" from __future__ import annotations from onnx_light.onnx import TensorProto import onnx_light.onnx.helper as oh from onnx_light.onnx_core.optimization import GraphBuilder, GraphGraph, replay from onnx_light.tools import pretty_onnx .. GENERATED FROM PYTHON SOURCE LINES 23-28 Create and clean up the source model ++++++++++++++++++++++++++++++++++++ This graph contains an ``Identity`` node, a dead-end ``Neg`` node, and two equal initializers used by retained nodes. .. GENERATED FROM PYTHON SOURCE LINES 28-62 .. code-block:: Python model = oh.make_model( oh.make_graph( [ oh.make_node("Add", ["x", "weight"], ["summed"]), oh.make_node("Identity", ["summed"], ["forwarded"]), oh.make_node("Add", ["forwarded", "duplicate_weight"], ["y"]), oh.make_node("Neg", ["x"], ["dead_end"]), ], "cleanup", [oh.make_tensor_value_info("x", TensorProto.FLOAT, [1])], [oh.make_tensor_value_info("y", TensorProto.FLOAT, [1])], initializer=[ oh.make_tensor("weight", TensorProto.FLOAT, [1], [1.0]), oh.make_tensor("duplicate_weight", TensorProto.FLOAT, [1], [1.0]), ], ), opset_imports=[oh.make_opsetid("", 18)], ) builder = GraphBuilder(model) graph = GraphGraph(builder, patterns=False) rewrites = list(graph.optimize()) optimized_graph = builder.build_graph() assert {"RemoveIdentityNodes", "RemoveUnusedNodes", "RemoveDuplicateInitializers"} <= { rewrite.pattern_name for rewrite in rewrites } 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='cleanup' input: float[1] x init: float[1] weight init: float[1] duplicate_weight 0: Add(x, weight) -> summed 1: Identity(summed) -> forwarded 2: Add(forwarded, duplicate_weight) -> y 3: Neg(x) -> dead_end output: float[1] y Optimized graph: opset: domain='ai.onnx' version=18 graph: name='cleanup' input: float[1] x init: float[1] weight 0: Add(x, weight) -> summed 1: Add(summed, weight) -> y output: float[1] y .. GENERATED FROM PYTHON SOURCE LINES 63-68 Inspect and replay the cleanup modifications ++++++++++++++++++++++++++++++++++++++++++++ Every cleanup operation is captured as a ``LocalRewriting`` record. Replay applies the records to a fresh copy of the source model. .. GENERATED FROM PYTHON SOURCE LINES 68-76 .. code-block:: Python for rewrite in rewrites: print(rewrite) 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 LocalRewriting(pattern=RemoveIdentityNodes, graph_path=, matched_nodes=4, added_nodes=3) LocalRewriting(pattern=RemoveUnusedNodes, graph_path=, matched_nodes=3, added_nodes=2) LocalRewriting(pattern=RemoveDuplicateInitializers, graph_path=, matched_nodes=2, added_nodes=2) Replayed graph: graph: name='cleanup' input: float[1] x init: float[1] weight 0: Add(x, weight) -> summed 1: Add(summed, weight) -> y output: float[1] 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_cleanup.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_cleanup.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pattern_replay_cleanup.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_pattern_replay_cleanup.zip ` .. include:: plot_pattern_replay_cleanup.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_