Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion _doc/sphinxdoc/source/api/onnxrt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ implementated in :epkg:`Python`. The :epkg:`ONNX` model relies
on the following operators :ref:`l-onnx-runtime-operators`.

.. autosignature:: mlprodict.onnxrt.onnx_inference.OnnxInference
:members:
:members: run, shape_inference, check_model, run2onnx, get_profiling

.. autosignature:: mlprodict.onnxrt.onnx_micro_inference.OnnxMicroRuntime
:members: run

Python to ONNX
++++++++++++++
Expand Down
7 changes: 0 additions & 7 deletions _doc/sphinxdoc/source/api/tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ Serialization

.. autosignature:: mlprodict.onnx_tools.onnx2py_helper.to_bytes

Runtime
=======

.. autosignature:: mlprodict.onnxrt.onnx_inference.OnnxInference

.. autosignature:: mlprodict.tools.onnx_micro_runtime.OnnxMicroRuntime

Validation
++++++++++

Expand Down
Original file line number Diff line number Diff line change
@@ -1,121 +1,121 @@
"""
@brief test log(time=3s)
"""
import unittest
import numpy
from pyquickhelper.pycode import ExtTestCase
from skl2onnx.algebra.onnx_ops import ( # pylint: disable=E0611
OnnxAdd, OnnxTranspose, OnnxShape, OnnxPow, OnnxMatMul, OnnxGemm,
OnnxSqueeze, OnnxUnsqueeze)
from mlprodict.tools.onnx_micro_runtime import OnnxMicroRuntime
class TestOnnxMicroRuntime(ExtTestCase):
opset = 15 # opset=13, 14, ...
def test_onnx_micro_runtime(self):
opset = TestOnnxMicroRuntime.opset
dtype = numpy.float32
x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
numpy.float32).reshape((3, 2))
cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=opset)
cop4 = OnnxAdd(cop, numpy.array([2], dtype=dtype), op_version=opset,
output_names=['Y'])
model_def = cop4.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertIn('X', out)
self.assertIn('Y', out)
self.assertIn('Ad_Addcst', out)
self.assertEqual(len(out), 5)
def test_onnx_micro_runtime_exc1(self):
self.assertRaise(lambda: OnnxMicroRuntime(None), TypeError)
def test_onnx_micro_runtime_exc2(self):
opset = TestOnnxMicroRuntime.opset
dtype = numpy.float32
x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
numpy.float32).reshape((3, 2))
cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=opset)
cop4 = OnnxPow(cop, numpy.array([2], dtype=dtype), op_version=opset,
output_names=['Y'])
model_def = cop4.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
self.assertRaise(lambda: rt.run({'X': x}), NotImplementedError)
self.assertRaise(lambda: rt.run(x), TypeError)
def test_onnx_micro_runtime_shape(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
numpy.float32).reshape((3, 2))
cop = OnnxShape('X', op_version=opset, output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(numpy.array(x.shape, dtype=numpy.int64), out['Y'])
def test_onnx_micro_runtime_transpose(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
numpy.float32).reshape((3, 2))
cop = OnnxTranspose('X', perm=[1, 0], op_version=opset,
output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(x.T, out['Y'])
def test_onnx_micro_runtime_matmul(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5]).astype(
numpy.float32).reshape((2, 2))
cop = OnnxMatMul('X', 'X', op_version=opset,
output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(numpy.matmul(x, x), out['Y'])
def test_onnx_micro_runtime_squeeze(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5]).astype(
numpy.float32).reshape((2, 2, 1))
cop = OnnxSqueeze('X', numpy.array([2], dtype=numpy.int64),
op_version=opset, output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(numpy.squeeze(x), out['Y'])
def test_onnx_micro_runtime_unsqueeze(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5]).astype(
numpy.float32).reshape((2, 2))
cop = OnnxUnsqueeze('X', numpy.array([2], dtype=numpy.int64),
op_version=opset, output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(x.reshape((2, 2, 1)), out['Y'])
def test_onnx_micro_runtime_gemm(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5]).astype(
numpy.float32).reshape((2, 2))
for ta in [0, 1]:
for tb in [0, 1]:
cop = OnnxGemm(
'X', 'X', 'X', op_version=opset, alpha=1., beta=1.,
output_names=['Y'], transA=ta, transB=tb)
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
xa = x.T if ta else x
xb = x.T if tb else x
self.assertEqual(numpy.matmul(xa, xb) + x, out['Y'])
if __name__ == "__main__":
unittest.main()
"""
@brief test log(time=3s)
"""
import unittest
import numpy
from pyquickhelper.pycode import ExtTestCase
from skl2onnx.algebra.onnx_ops import ( # pylint: disable=E0611
OnnxAdd, OnnxTranspose, OnnxShape, OnnxPow, OnnxMatMul, OnnxGemm,
OnnxSqueeze, OnnxUnsqueeze)
from mlprodict.onnxrt.onnx_micro_runtime import OnnxMicroRuntime


class TestOnnxMicroRuntime(ExtTestCase):

opset = 15 # opset=13, 14, ...

def test_onnx_micro_runtime(self):
opset = TestOnnxMicroRuntime.opset
dtype = numpy.float32
x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
numpy.float32).reshape((3, 2))
cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=opset)
cop4 = OnnxAdd(cop, numpy.array([2], dtype=dtype), op_version=opset,
output_names=['Y'])
model_def = cop4.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertIn('X', out)
self.assertIn('Y', out)
self.assertIn('Ad_Addcst', out)
self.assertEqual(len(out), 5)

def test_onnx_micro_runtime_exc1(self):
self.assertRaise(lambda: OnnxMicroRuntime(None), TypeError)

def test_onnx_micro_runtime_exc2(self):
opset = TestOnnxMicroRuntime.opset
dtype = numpy.float32
x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
numpy.float32).reshape((3, 2))
cop = OnnxAdd('X', numpy.array([1], dtype=dtype), op_version=opset)
cop4 = OnnxPow(cop, numpy.array([2], dtype=dtype), op_version=opset,
output_names=['Y'])
model_def = cop4.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
self.assertRaise(lambda: rt.run({'X': x}), NotImplementedError)
self.assertRaise(lambda: rt.run(x), TypeError)

def test_onnx_micro_runtime_shape(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
numpy.float32).reshape((3, 2))
cop = OnnxShape('X', op_version=opset, output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(numpy.array(x.shape, dtype=numpy.int64), out['Y'])

def test_onnx_micro_runtime_transpose(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5, 5, 4]).astype(
numpy.float32).reshape((3, 2))
cop = OnnxTranspose('X', perm=[1, 0], op_version=opset,
output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(x.T, out['Y'])

def test_onnx_micro_runtime_matmul(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5]).astype(
numpy.float32).reshape((2, 2))
cop = OnnxMatMul('X', 'X', op_version=opset,
output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(numpy.matmul(x, x), out['Y'])

def test_onnx_micro_runtime_squeeze(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5]).astype(
numpy.float32).reshape((2, 2, 1))
cop = OnnxSqueeze('X', numpy.array([2], dtype=numpy.int64),
op_version=opset, output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(numpy.squeeze(x), out['Y'])

def test_onnx_micro_runtime_unsqueeze(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5]).astype(
numpy.float32).reshape((2, 2))
cop = OnnxUnsqueeze('X', numpy.array([2], dtype=numpy.int64),
op_version=opset, output_names=['Y'])
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
self.assertEqual(x.reshape((2, 2, 1)), out['Y'])

def test_onnx_micro_runtime_gemm(self):
opset = TestOnnxMicroRuntime.opset
x = numpy.array([1, 2, 4, 5]).astype(
numpy.float32).reshape((2, 2))
for ta in [0, 1]:
for tb in [0, 1]:
cop = OnnxGemm(
'X', 'X', 'X', op_version=opset, alpha=1., beta=1.,
output_names=['Y'], transA=ta, transB=tb)
model_def = cop.to_onnx({'X': x}, target_opset=opset)
rt = OnnxMicroRuntime(model_def)
out = rt.run({'X': x})
xa = x.T if ta else x
xb = x.T if tb else x
self.assertEqual(numpy.matmul(xa, xb) + x, out['Y'])


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions mlprodict/onnxrt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
@brief Shortcut to *onnxrt*.
"""
from .onnx_inference import OnnxInference
from .onnx_micro_runtime import OnnxMicroRuntime

Loading