.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/core/plot_profile.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_core_plot_profile.py: Profiling onnxruntime execution ================================ This example shows how to profile the execution of an ONNX model with onnxruntime and visualize the results with :func:`~yaourt.tools.js_profile.plot_ort_profile` and :func:`~yaourt.tools.js_profile.plot_ort_profile_timeline`. .. GENERATED FROM PYTHON SOURCE LINES 12-14 Build a small ONNX model ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 14-41 .. code-block:: Python import os import tempfile import numpy as np import onnx import onnx.helper as oh import onnx.numpy_helper as onh model = oh.make_model( oh.make_graph( [ oh.make_node("MatMul", ["x", "W1"], ["h"]), oh.make_node("Add", ["h", "b1"], ["relu_in"]), oh.make_node("Relu", ["relu_in"], ["output"]), ], "test_graph", [oh.make_tensor_value_info("x", onnx.TensorProto.FLOAT, (2, 4))], [oh.make_tensor_value_info("output", onnx.TensorProto.FLOAT, (2, 8))], [ onh.from_array(np.random.randn(4, 8).astype(np.float32), name="W1"), onh.from_array(np.zeros(8, dtype=np.float32), name="b1"), ], ), opset_imports=[oh.make_opsetid("", 18)], ir_version=8, ) .. GENERATED FROM PYTHON SOURCE LINES 42-44 Run with onnxruntime profiling enabled -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 44-61 .. code-block:: Python from onnxruntime import InferenceSession, SessionOptions tmpdir = tempfile.mkdtemp() opts = SessionOptions() opts.enable_profiling = True opts.profile_file_prefix = os.path.join(tmpdir, "ort_profile") sess = InferenceSession( model.SerializeToString(), sess_options=opts, providers=["CPUExecutionProvider"] ) x = np.random.randn(2, 4).astype(np.float32) for _ in range(5): sess.run(None, {"x": x}) profile_file = sess.end_profiling() print("Profile written to:", profile_file) .. rst-class:: sphx-glr-script-out .. code-block:: none Profile written to: /tmp/tmp4nk0w9wx/ort_profile_2026-05-06_10-56-11_701.json .. GENERATED FROM PYTHON SOURCE LINES 62-64 Parse the profiling file into a DataFrame ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: Python from yaourt.tools.js_profile import js_profile_to_dataframe df = js_profile_to_dataframe(profile_file, first_it_out=True) print(df[["name", "event_name", "iteration", "dur"]].head(10).to_string()) .. rst-class:: sphx-glr-script-out .. code-block:: none name event_name iteration dur 0 model_loading_array model_loading_array -1 335 1 session_initialization session_initialization -1 1012 2 fused /MatMulAddFusion_kernel_time kernel_time -1 166 3 SequentialExecutor::Execute SequentialExecutor::Execute 0 186 4 model_run model_run 0 222 5 fused /MatMulAddFusion_kernel_time kernel_time 0 31 6 SequentialExecutor::Execute SequentialExecutor::Execute 1 37 7 model_run model_run 1 48 8 fused /MatMulAddFusion_kernel_time kernel_time 1 20 9 SequentialExecutor::Execute SequentialExecutor::Execute 2 26 .. GENERATED FROM PYTHON SOURCE LINES 70-72 Plot a summary by operator type -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 72-84 .. code-block:: Python import matplotlib import matplotlib.pyplot as plt matplotlib.use("Agg") from yaourt.tools.js_profile import plot_ort_profile fig, ax = plt.subplots(figsize=(8, 4)) plot_ort_profile(df, ax0=ax, title="Time per operator (µs)") fig.tight_layout() fig .. image-sg:: /auto_examples/core/images/sphx_glr_plot_profile_001.png :alt: Time per operator (µs) :srcset: /auto_examples/core/images/sphx_glr_plot_profile_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 85-87 Plot the execution timeline --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 87-94 .. code-block:: Python from yaourt.tools.js_profile import plot_ort_profile_timeline fig2, ax2 = plt.subplots(figsize=(6, 6)) plot_ort_profile_timeline(df, ax=ax2, title="Execution timeline") fig2.tight_layout() fig2 .. image-sg:: /auto_examples/core/images/sphx_glr_plot_profile_002.png :alt: Execution timeline :srcset: /auto_examples/core/images/sphx_glr_plot_profile_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 95-97 Cleanup ------- .. GENERATED FROM PYTHON SOURCE LINES 97-101 .. code-block:: Python os.unlink(profile_file) import shutil shutil.rmtree(tmpdir, ignore_errors=True) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.531 seconds) .. _sphx_glr_download_auto_examples_core_plot_profile.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_profile.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_profile.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_profile.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_