Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
documentation + arrayratureextraction
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Jun 21, 2019
1 parent fa9ca89 commit 3c575f6
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion _doc/sphinxdoc/source/_exts/generate_automated_pages.py
Expand Up @@ -95,7 +95,7 @@ def shorten(text):
title = "Available of scikit-learn model for runtime {0}".format(
runtime)
f.write(dedent('''
_l-onnx-bench-{0}:
.. _l-onnx-bench-{0}:
{1}
{2}
Expand Down
7 changes: 6 additions & 1 deletion _doc/sphinxdoc/source/onnx_bench.rst
Expand Up @@ -9,7 +9,12 @@ models into :epkg:`ONNX`. Every of them is tested against
a couple of runtimes. The following pages shows
which models are correctly converted and compares
the predictions obtained by every runtime. It also
displays some benchmark.
displays some benchmark. The benchmark evaluates
every model on a dataset inspired from the :epkg:`Iris`
dataset, so with four features, and different number of
observations *N= 1, 10, 100, 1000, 100.00, 100.000*.
The measures for high values of *N* may be missing
because the first one took too long.

.. toctree::
:maxdepth: 1
Expand Down
4 changes: 2 additions & 2 deletions _doc/sphinxdoc/source/onnx_python.rst
Expand Up @@ -114,8 +114,8 @@ the cause of the error if it does not work.

piv["ERROR-msg"] = piv["ERROR-msg"].apply(shorten)

print(df2rst(piv))
print(df2rst(piv, number_format=2))

build_table()

Full results are available at :ref:`l-onnx-bench-python`.
Full results are available at :ref:`l-onnx-bench-CPU`.
19 changes: 18 additions & 1 deletion _unittests/ut_onnxrt/test_onnxrt_python_runtime_.py
Expand Up @@ -5,12 +5,16 @@
from logging import getLogger
import numpy
from pyquickhelper.pycode import ExtTestCase
from pyquickhelper.texthelper.version_helper import compare_module_version
from skl2onnx.algebra.onnx_ops import ( # pylint: disable=E0611
OnnxAdd, OnnxArgMax, OnnxArgMin, OnnxDiv,
OnnxAdd, OnnxArgMax, OnnxArgMin,
OnnxArrayFeatureExtractor, OnnxDiv,
OnnxGemm, OnnxMatMul, OnnxMean, OnnxMul,
OnnxReduceSum, OnnxReduceSumSquare, OnnxSqrt,
OnnxSub,
)
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx import __version__ as skl2onnx_version
from mlprodict.onnxrt import OnnxInference


Expand Down Expand Up @@ -82,6 +86,19 @@ def test_onnxt_runtime_argmin(self):
self.assertEqualArray(numpy.argmin(X, axis=1).ravel(),
got['Y'].ravel())

@unittest.skipIf(compare_module_version(skl2onnx_version, "1.5.0") <= 0,
reason="int64 not implemented for constants")
def test_onnxt_runtime_array_feature_extractor(self):
onx = OnnxArrayFeatureExtractor('X', numpy.array([1], dtype=numpy.int64),
output_names=['Y'])
X = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64)
model_def = onx.to_onnx({'X': X.astype(numpy.float32)},
outputs=[('Y', FloatTensorType([2]))])
oinf = OnnxInference(model_def)
got = oinf.run({'X': X})
self.assertEqual(list(sorted(got)), ['Y'])
self.assertEqualArray(X[:, 1], got['Y'], decimal=6)

def test_onnxt_runtime_div(self):
idi = numpy.identity(2)
onx = OnnxDiv('X', idi, output_names=['Y'])
Expand Down
1 change: 1 addition & 0 deletions mlprodict/onnxrt/ops_cpu/_op_list.py
Expand Up @@ -9,6 +9,7 @@
from .op_add import Add
from .op_argmax import ArgMax
from .op_argmin import ArgMin
from .op_array_feature_extractor import ArrayFeatureExtractor
from .op_cast import Cast
from .op_div import Div
from .op_gemm import Gemm
Expand Down
22 changes: 22 additions & 0 deletions mlprodict/onnxrt/ops_cpu/op_array_feature_extractor.py
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
# pylint: disable=E0203,E1101,C0111
"""
@file
@brief Runtime operator.
"""
from ._op import OpRun


class ArrayFeatureExtractor(OpRun):

def __init__(self, onnx_node, desc=None, **options):
if desc is None:
raise ValueError("desc should not be None.")
OpRun.__init__(self, onnx_node, desc=desc,
**options)

def _run(self, data, indices): # pylint: disable=W0221
index = tuple(indices.tolist())
sl = slice(0, data.shape[0])
index = (sl, ) + index
return (data[index], )
13 changes: 7 additions & 6 deletions mlprodict/onnxrt/validate.py
Expand Up @@ -910,15 +910,16 @@ def fct():
return mes


def benchmark_fct(fct, X):
def benchmark_fct(fct, X, time_limit=4):
"""
Benchmarks a function which takes an array
as an input and changes the number of rows.
@param fct function to benchmark, signature
is fct(xo)
@param X array
@return dictionary with the results
@param fct function to benchmark, signature
is fct(xo)
@param X array
@param time_limit above this time, measurement as stopped
@return dictionary with the results
"""

def make(x, n):
Expand Down Expand Up @@ -948,7 +949,7 @@ def make(x, n):
number = 1
res[N] = measure_time(fct, x, repeat=repeat,
number=number, div_by_number=True)
if res[N] is not None and res[N].get('total', 2.) >= 2.:
if res[N] is not None and res[N].get('total', time_limit) >= time_limit:
# too long
break
return res

0 comments on commit 3c575f6

Please sign in to comment.