Skip to content

Supports OrtValue in function ort_profile #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Supports OrtValue in function ort_profile
  • Loading branch information
xadupre committed Jun 22, 2023
commit a763dc0f798642f7bd7499477942d7f652b754ad
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ build/*
.eggs/*
.hypothesis/*
*egg-info/*
onnxruntime_profile*
_doc/auto_examples/*
_doc/examples/_cache/*
_doc/examples/onnxruntime_profile*
_doc/examples/plot_*.png
_doc/examples/plot_*.xlsx
_doc/examples/data/*.optimized.onnx
Expand Down
1 change: 1 addition & 0 deletions CHANGELOGS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ Change Logs
0.2.0
+++++

* :pr:`27`: support OrtValue in function :func:`ort_profile`
* :pr:`17`: implements ArrayAPI
* :pr:`3`: fixes Array API with onnxruntime and scikit-learn
35 changes: 34 additions & 1 deletion _unittests/ut_ort/test_ort_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from onnx_array_api.ext_test_case import ExtTestCase
from onnx_array_api.ort.ort_optimizers import ort_optimized_model
from onnx_array_api.ort.ort_profile import ort_profile, merge_ort_profile
from onnxruntime.capi._pybind_state import (
OrtValue as C_OrtValue,
OrtDevice as C_OrtDevice,
)


class TestOrtProfile(ExtTestCase):
Expand All @@ -28,7 +32,36 @@ def myloss(x, y):
self.assertRaise(lambda: ort_optimized_model(onx, "NO"), ValueError)
optimized = ort_optimized_model(onx)
prof = ort_profile(optimized, feeds)
prof.to_csv("prof.csv", index=False)
self.assertIsInstance(prof, DataFrame)
prof = ort_profile(optimized, feeds, as_df=False)
self.assertIsInstance(prof, list)

def test_ort_profile_ort_value(self):
def to_ort_value(m):
device = C_OrtDevice(C_OrtDevice.cpu(), C_OrtDevice.default_memory(), 0)
ort_value = C_OrtValue.ortvalue_from_numpy(m, device)
return ort_value

def l1_loss(x, y):
return absolute(x - y).sum()

def l2_loss(x, y):
return ((x - y) ** 2).sum()

def myloss(x, y):
return l1_loss(x[:, 0], y[:, 0]) + l2_loss(x[:, 1], y[:, 1])

jitted_myloss = jit_onnx(myloss)
x = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)
y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)
jitted_myloss(x, y)
onx = jitted_myloss.get_onnx()
np_feeds = {"x0": x, "x1": y}
feeds = {k: to_ort_value(v) for k, v in np_feeds.items()}

self.assertRaise(lambda: ort_optimized_model(onx, "NO"), ValueError)
optimized = ort_optimized_model(onx)
prof = ort_profile(optimized, feeds)
self.assertIsInstance(prof, DataFrame)
prof = ort_profile(optimized, feeds, as_df=False)
self.assertIsInstance(prof, list)
Expand Down
12 changes: 10 additions & 2 deletions onnx_array_api/ort/ort_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ def ort_profile(
if providers is None:
providers = ["CPUExecutionProvider"]
sess = InferenceSession(obj, sess_options, providers=providers, **kwargs)
for i in range(repeat):
sess.run(None, feeds)
first = list(feeds.values())[0]

if isinstance(first, numpy.ndarray):
for i in range(repeat):
sess.run(None, feeds)
else:
out_names = [o.name for o in sess.get_outputs()]
for i in range(repeat):
sess._sess.run_with_ort_values(feeds, out_names, None)

prof = sess.end_profiling()
with open(prof, "r") as f:
content = f.read()
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy