2020-08-13 02:12:50 +00:00
|
|
|
.. only:: html
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
:class: sphx-glr-download-link-note
|
2019-12-21 01:11:46 +00:00
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
Click :ref:`here <sphx_glr_download_auto_examples_plot_profiling.py>` to download the full example code
|
|
|
|
|
.. rst-class:: sphx-glr-example-title
|
2019-12-21 01:11:46 +00:00
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
.. _sphx_glr_auto_examples_plot_profiling.py:
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
.. _l-example-profiling:
|
|
|
|
|
|
|
|
|
|
Profile the execution of a simple model
|
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
|
|
*ONNX Runtime* can profile the execution of the model.
|
|
|
|
|
This example shows how to interpret the results.
|
|
|
|
|
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
.. code-block:: default
|
2019-12-21 01:11:46 +00:00
|
|
|
|
2020-04-06 22:52:48 +00:00
|
|
|
import onnx
|
2019-12-21 01:11:46 +00:00
|
|
|
import onnxruntime as rt
|
|
|
|
|
import numpy
|
|
|
|
|
from onnxruntime.datasets import get_example
|
|
|
|
|
|
|
|
|
|
|
2020-04-06 22:52:48 +00:00
|
|
|
def change_ir_version(filename, ir_version=6):
|
|
|
|
|
"onnxruntime==1.2.0 does not support opset <= 7 and ir_version > 6"
|
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
|
model = onnx.load(f)
|
|
|
|
|
model.ir_version = 6
|
|
|
|
|
if model.opset_import[0].version <= 7:
|
|
|
|
|
model.opset_import[0].version = 11
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
Let's load a very simple model and compute some prediction.
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
.. code-block:: default
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
example1 = get_example("mul_1.onnx")
|
2020-04-06 22:52:48 +00:00
|
|
|
onnx_model = change_ir_version(example1)
|
|
|
|
|
onnx_model_str = onnx_model.SerializeToString()
|
|
|
|
|
sess = rt.InferenceSession(onnx_model_str)
|
2019-12-21 01:11:46 +00:00
|
|
|
input_name = sess.get_inputs()[0].name
|
|
|
|
|
|
|
|
|
|
x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)
|
|
|
|
|
res = sess.run(None, {input_name: x})
|
|
|
|
|
print(res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
|
|
|
|
|
|
Out:
|
|
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
|
|
[array([[ 1., 4.],
|
|
|
|
|
[ 9., 16.],
|
|
|
|
|
[25., 36.]], dtype=float32)]
|
|
|
|
|
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
|
|
|
|
|
|
2019-12-21 01:11:46 +00:00
|
|
|
We need to enable to profiling
|
|
|
|
|
before running the predictions.
|
|
|
|
|
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
.. code-block:: default
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
options = rt.SessionOptions()
|
|
|
|
|
options.enable_profiling = True
|
2020-04-06 22:52:48 +00:00
|
|
|
sess_profile = rt.InferenceSession(onnx_model_str, options)
|
2019-12-21 01:11:46 +00:00
|
|
|
input_name = sess.get_inputs()[0].name
|
|
|
|
|
|
|
|
|
|
x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)
|
|
|
|
|
|
|
|
|
|
sess.run(None, {input_name: x})
|
|
|
|
|
prof_file = sess_profile.end_profiling()
|
|
|
|
|
print(prof_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
|
|
|
|
|
|
Out:
|
|
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
2021-02-11 00:34:31 +00:00
|
|
|
onnxruntime_profile__2020-12-03_00-54-48.json
|
2020-08-13 02:12:50 +00:00
|
|
|
|
|
|
|
|
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
The results are stored un a file in JSON format.
|
|
|
|
|
Let's see what it contains.
|
|
|
|
|
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
.. code-block:: default
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
with open(prof_file, "r") as f:
|
|
|
|
|
sess_time = json.load(f)
|
|
|
|
|
import pprint
|
|
|
|
|
pprint.pprint(sess_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-script-out
|
|
|
|
|
|
|
|
|
|
Out:
|
|
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
|
|
[{'args': {},
|
|
|
|
|
'cat': 'Session',
|
2021-02-11 00:34:31 +00:00
|
|
|
'dur': 205,
|
|
|
|
|
'name': 'model_loading_array',
|
2019-12-21 01:11:46 +00:00
|
|
|
'ph': 'X',
|
2021-02-11 00:34:31 +00:00
|
|
|
'pid': 22648,
|
|
|
|
|
'tid': 6000,
|
|
|
|
|
'ts': 6},
|
2019-12-21 01:11:46 +00:00
|
|
|
{'args': {},
|
|
|
|
|
'cat': 'Session',
|
2021-02-11 00:34:31 +00:00
|
|
|
'dur': 737,
|
2019-12-21 01:11:46 +00:00
|
|
|
'name': 'session_initialization',
|
|
|
|
|
'ph': 'X',
|
2021-02-11 00:34:31 +00:00
|
|
|
'pid': 22648,
|
|
|
|
|
'tid': 6000,
|
|
|
|
|
'ts': 269}]
|
2020-08-13 02:12:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-21 01:11:46 +00:00
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
.. rst-class:: sphx-glr-timing
|
2019-12-21 01:11:46 +00:00
|
|
|
|
2021-02-11 00:34:31 +00:00
|
|
|
**Total running time of the script:** ( 0 minutes 0.038 seconds)
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
.. _sphx_glr_download_auto_examples_plot_profiling.py:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. only :: html
|
|
|
|
|
|
|
|
|
|
.. container:: sphx-glr-footer
|
|
|
|
|
:class: sphx-glr-footer-example
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
.. container:: sphx-glr-download sphx-glr-download-python
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
:download:`Download Python source code: plot_profiling.py <plot_profiling.py>`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
.. container:: sphx-glr-download sphx-glr-download-jupyter
|
2019-12-21 01:11:46 +00:00
|
|
|
|
|
|
|
|
:download:`Download Jupyter notebook: plot_profiling.ipynb <plot_profiling.ipynb>`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. only:: html
|
|
|
|
|
|
|
|
|
|
.. rst-class:: sphx-glr-signature
|
|
|
|
|
|
2020-08-13 02:12:50 +00:00
|
|
|
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
|