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
2 changes: 1 addition & 1 deletion _doc/sphinxdoc/source/tutorial/onnx_numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ the first examples of `sklearn-onnx tutorial`.
# Conversion to ONNX
try:
onx = to_onnx(log_scale_transformer, X)
except RuntimeError as e:
except (RuntimeError, TypeError) as e:
print(e)

The first step is a `FunctionTransformer` with a custom function
Expand Down
45 changes: 32 additions & 13 deletions _unittests/ut_npy/test_numpy_onnx_pyrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,39 @@
import numpy
import scipy.special as sp
from pyquickhelper.pycode import ExtTestCase
from mlprodict.onnxrt import OnnxInference
import mlprodict.npy.numpy_onnx_pyrt as nxnpy


try:
numpy_bool = numpy.bool_
except AttributeError:
numpy_bool = bool


class TestNumpyOnnxFunction(ExtTestCase):

def common_test1(self, x, npfct, nxfct, dtype, **kwargs):
def common_test1(self, x, npfct, nxfct, dtype, dtype_out=None, **kwargs):
xt = x.astype(dtype)
if kwargs is None or len(kwargs) == 0:
if dtype_out is None and (kwargs is None or len(kwargs) == 0):
expected = npfct(xt)
got = nxfct[dtype](xt)
compiled = nxfct[dtype].compiled
else:
expected = npfct(xt, **kwargs)
kwargs['dtype_onnx'] = dtype
if dtype_out is not None:
kwargs['dtype_onnx_out'] = dtype_out
got = nxfct[kwargs](xt)
compiled = nxfct[kwargs].compiled
self.assertEqualArray(expected, got)
onx = compiled.onnx_
rt2 = OnnxInference(onx, runtime="onnxruntime1")
inputs = rt2.input_names
outputs = rt2.output_names
data = {inputs[0]: xt}
got2 = rt2.run(data)[outputs[0]]
self.assertEqualArray(expected, got2, decimal=6)

def test_abs_float32(self):
x = numpy.array([[-6.1, 5], [-3.5, 7.8]], dtype=numpy.float32)
Expand All @@ -42,29 +60,29 @@ def test_amax_float32(self):
self.common_test1(x, numpy.amax, nxnpy.amax,
numpy.float32, **kw)

def test_argmax_float32(self):
kwargs = [{'axis': 0}, {'axis': 1}]
def test_amin_float32(self):
kwargs = [{'axis': 0}, {}, {'axis': 1}]
for kw in kwargs:
with self.subTest(kw=kw):
x = numpy.array([[-6.1, 5], [-3.5, 7.8]], dtype=numpy.float32)
self.common_test1(x, numpy.argmax, nxnpy.argmax,
self.common_test1(x, numpy.amin, nxnpy.amin,
numpy.float32, **kw)

def test_argmin_float32(self):
def test_argmax_float32(self):
kwargs = [{'axis': 0}, {'axis': 1}]
for kw in kwargs:
with self.subTest(kw=kw):
x = numpy.array([[-6.1, 5], [-3.5, 7.8]], dtype=numpy.float32)
self.common_test1(x, numpy.argmin, nxnpy.argmin,
numpy.float32, **kw)
self.common_test1(x, numpy.argmax, nxnpy.argmax,
numpy.float32, dtype_out=numpy.int64, **kw)

def test_amin_float32(self):
kwargs = [{'axis': 0}, {}, {'axis': 1}]
def test_argmin_float32(self):
kwargs = [{'axis': 0}, {'axis': 1}]
for kw in kwargs:
with self.subTest(kw=kw):
x = numpy.array([[-6.1, 5], [-3.5, 7.8]], dtype=numpy.float32)
self.common_test1(x, numpy.amin, nxnpy.amin,
numpy.float32, **kw)
self.common_test1(x, numpy.argmin, nxnpy.argmin,
numpy.float32, dtype_out=numpy.int64, **kw)

def test_asin_float32(self):
x = numpy.array([[0.5, 0.1], [-0.5, -0.1]], dtype=numpy.float32)
Expand Down Expand Up @@ -97,7 +115,8 @@ def test_exp_float32(self):

def test_isnan_float32(self):
x = numpy.array([[6.1, 5], [3.5, numpy.nan]], dtype=numpy.float32)
self.common_test1(x, numpy.isnan, nxnpy.isnan, numpy.float32)
self.common_test1(x, numpy.isnan, nxnpy.isnan, numpy.float32,
dtype_out=numpy_bool)

def test_log_float32(self):
x = numpy.array([[6.1, 5], [3.5, 7.8]], dtype=numpy.float32)
Expand Down
86 changes: 43 additions & 43 deletions _unittests/ut_npy/test_onnx_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ def test_abs_matmul(x: NDArray[Any, numpy.float32],
@onnxnumpy_default
def test_abs_div(x: NDArray[Any, numpy.float32],
) -> NDArray[Any, numpy.float32]:
"onnx numpy addition"
"onnx numpy division"
return nxnp.abs(x) / x


@onnxnumpy_default
def test_abs_idiv(x: NDArray[Any, numpy.float32],
) -> NDArray[Any, numpy.float32]:
"onnx numpy addition"
return nxnp.abs(x) // x
) -> NDArray[Any, numpy.int64]:
"onnx numpy int division"
return nxnp.abs(x).astype(numpy.int64) // x.astype(numpy.int64)


@onnxnumpy_default
Expand Down Expand Up @@ -145,14 +145,14 @@ def test_abs_less(x: NDArray[Any, numpy.float32],


@onnxnumpy_default
def test_abs_and(x: NDArray[Any, numpy_bool],
def test_abs_and(x: NDArray[Any, numpy.float32],
) -> NDArray[Any, numpy_bool]:
"onnx numpy and"
return (nxnp.abs(x) < x) and (nxnp.abs(x) < numpy.float32(0))


@onnxnumpy_default
def test_abs_or(x: NDArray[Any, numpy_bool],
def test_abs_or(x: NDArray[Any, numpy.float32],
) -> NDArray[Any, numpy_bool]:
"onnx numpy or"
return (nxnp.abs(x) < x) or (nxnp.abs(x) < numpy.float32(0))
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_abs_filter(x: NDArray[Any, numpy.float32],

@onnxnumpy_default
def test_abs_set2(x: NDArray[Any, numpy.float32],
) -> NDArray[Any, numpy.bool]:
) -> NDArray[Any, numpy.float32]:
"onnx numpy set"
temp = nxnp.abs(x).copy()
temp[:2, 0] = numpy.float32(-1)
Expand All @@ -254,7 +254,7 @@ def test_abs_set2(x: NDArray[Any, numpy.float32],

@onnxnumpy_default
def test_abs_set3(x: NDArray[Any, numpy.float32],
) -> NDArray[Any, numpy.bool]:
) -> NDArray[Any, numpy.float32]:
"onnx numpy set"
temp = nxnp.abs(x).copy()
temp[:2, :1] = numpy.array([[-1.5, -1.5]], dtype=numpy.float32).T
Expand Down Expand Up @@ -290,70 +290,70 @@ def test_abs_size(x: NDArray[Any, numpy.float32],

class TestOnnxVariable(ExtTestCase):

def test_onnx_variable_abs(self):
def test_py_abs(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs(x)
self.assertEqualArray(y, numpy.abs(x))
self.assertEqual(test_abs.__doc__, "onnx numpy abs")
self.assertTrue(hasattr(test_abs, 'compiled'))
self.assertIsInstance(test_abs.compiled, ONC)

def test_onnx_variable_abs_add(self):
def test_py_abs_add(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_add(x)
self.assertEqualArray(y, numpy.abs(x) + x)

def test_onnx_variable_abs_addm(self):
def test_py_abs_addm(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_addm(x, x)
self.assertEqualArray(y, numpy.abs(x) + x)

def test_onnx_variable_abs_add_cst(self):
def test_py_abs_add_cst(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_add2(x)
self.assertEqualArray(y, numpy.abs(x) + 2)

def test_onnx_variable_abs_add4(self):
def test_py_abs_add4(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_add4(x)
text = str(test_abs_add4.compiled.onnx_).split('op_type: "Mul"')
self.assertEqual(len(text), 3)
self.assertEqualArray(y, (x * x) * (x * x))

def test_onnx_variable_abs_sub(self):
def test_py_abs_sub(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_sub(x)
self.assertEqualArray(y, numpy.abs(x) - x)

def test_onnx_variable_abs_mul(self):
def test_py_abs_mul(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_mul(x)
self.assertEqualArray(y, numpy.abs(x) * x)

def test_onnx_variable_abs_mod(self):
def test_py_abs_mod(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_mod(x)
self.assertEqualArray(y, numpy.abs(x) % 2)

def test_onnx_variable_abs_pox(self):
def test_py_abs_pox(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_pow(x)
self.assertEqualArray(y, numpy.abs(x) ** 2)

def test_onnx_variable_abs_matmul(self):
def test_py_abs_matmul(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_matmul(x)
self.assertEqualArray(y, numpy.abs(x) @ x)

def test_onnx_variable_abs_div(self):
def test_py_abs_div(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_div(x)
self.assertEqualArray(y, numpy.abs(x) / x)
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.int64)
y = test_abs_div(x)
self.assertEqualArray(y, numpy.abs(x) / x)

def test_onnx_variable_abs_idiv(self):
def test_py_abs_idiv(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_idiv(x)
self.assertEqualArray(y, numpy.abs(x) // x)
Expand All @@ -362,69 +362,69 @@ def test_onnx_variable_abs_idiv(self):
self.assertEqualArray(y, numpy.abs(x) // x)

@ignore_warnings(DeprecationWarning)
def test_onnx_variable_abs_equal(self):
def test_py_abs_equal(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_equal(x)
self.assertEqualArray(y, numpy.abs(x) == x)

@ignore_warnings(DeprecationWarning)
def test_onnx_variable_abs_not_equal(self):
def test_py_abs_not_equal(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_not_equal(x)
self.assertEqualArray(y, numpy.abs(x) != x)

@ignore_warnings(DeprecationWarning)
def test_onnx_variable_abs_greater(self):
def test_py_abs_greater(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_greater(x)
self.assertEqualArray(y, numpy.abs(x) > x)

@ignore_warnings(DeprecationWarning)
def test_onnx_variable_abs_less(self):
def test_py_abs_less(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_less(x)
self.assertEqualArray(y, numpy.abs(x) < x)

@ignore_warnings(DeprecationWarning)
def test_onnx_variable_abs_and(self):
def test_py_abs_and(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_and(x)
self.assertEqualArray(
y, (numpy.abs(x) < x) & (numpy.abs(x) < 0))

@ignore_warnings(DeprecationWarning)
def test_onnx_variable_abs_or(self):
def test_py_abs_or(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_or(x)
self.assertEqualArray(
y, (numpy.abs(x) < x) | (numpy.abs(x) < 0))

def test_onnx_variable_abs_sum1(self):
def test_py_abs_sum1(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_sum1(x)
self.assertEqualArray(y, numpy.sum(numpy.abs(x), axis=0))

def test_onnx_variable_abs_sum2(self):
def test_py_abs_sum2(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_sum2(x)
self.assertEqualArray(y, numpy.sum(numpy.abs(x), axis=1, keepdims=1))

def test_onnx_variable_transpose_t(self):
def test_py_transpose_t(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_transpose_t(x)
self.assertEqualArray(y, numpy.abs(x).T)

def test_onnx_variable_abs_cast(self):
def test_py_abs_cast(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_cast(x)
self.assertEqualArray(y, numpy.abs(x).astype(numpy.int64))

def test_onnx_variable_abs_reshape(self):
def test_py_abs_reshape(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_reshape(x)
self.assertEqualArray(y, numpy.abs(x).reshape((-1, 1)))

def test_onnx_variable_abs_reshape_11(self):
def test_py_abs_reshape_11(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_reshape(x)
self.assertEqualArray(y, numpy.abs(x).reshape((-1, 1)))
Expand All @@ -435,61 +435,61 @@ def test_onnx_variable_abs_reshape_11(self):
compiled = test_abs_reshape_11.compiled
self.assertIn("version: 11", str(compiled.onnx_))

def test_onnx_variable_abs_slice(self):
def test_py_abs_slice(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_slice(x)
self.assertEqualArray(y, numpy.abs(x)[:, 1:])

def test_onnx_variable_abs_slice23(self):
def test_py_abs_slice23(self):
x = numpy.arange(0, 36).reshape((6, 6)).astype(numpy.float32)
y = test_abs_slice23(x)
self.assertEqualArray(y, numpy.abs(x)[::2, ::3])

def test_onnx_variable_abs_neg(self):
def test_py_abs_neg(self):
x = numpy.arange(0, 36).reshape((6, 6)).astype(numpy.float32)
y = test_abs_neg(x)
self.assertEqualArray(y, -numpy.abs(x))

def test_onnx_variable_abs_not(self):
def test_py_abs_not(self):
x = numpy.arange(0, 36).reshape((6, 6)).astype(numpy.float32)
y = test_abs_not(x)
self.assertEqualArray(y, numpy.abs(x) <= 0)

def test_onnx_variable_abs_filter(self):
def test_py_abs_filter(self):
x = numpy.arange(0, 36).reshape((6, 6)).astype(numpy.float32)
y = test_abs_filter(x)
self.assertEqualArray(y, numpy.abs(x)[x[:, 0] > 15])

def test_onnx_variable_abs_set(self):
def test_py_abs_set(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_set2(x)
temp = numpy.abs(x)
temp[:, 0] = -1
self.assertEqualArray(y, temp)

def test_onnx_variable_abs_set3(self):
def test_py_abs_set3(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_set3(x)
temp = numpy.abs(x)
temp[:, 0] = -1.5
self.assertEqualArray(y, temp)

def test_onnx_variable_log(self):
def test_py_log(self):
x = numpy.array([[6.1, 5], [3.5, 7.8]], dtype=numpy.float32)
y = test_log(x)
self.assertEqualArray(y, numpy.log(x))

def test_onnx_variable_abs_log_multi(self):
def test_py_abs_log_multi(self):
x = numpy.array([[6.1, -5], [-3.5, 7.8]], dtype=numpy.float32)
y = test_abs_log_multi(x)
self.assertEqualArray(y, numpy.log(numpy.abs(x)))

def test_onnx_variable_abs_shape(self):
def test_py_abs_shape(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_shape(x)
self.assertEqualArray(y, numpy.abs(x).shape)

def test_onnx_variable_abs_size(self):
def test_py_abs_size(self):
x = numpy.array([[6.1, -5], [3.5, -7.8]], dtype=numpy.float32)
y = test_abs_size(x)
self.assertEqualArray(y, numpy.abs(x).size)
Expand Down
Loading