FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Removes measure_time by xadupre · Pull Request #331 · sdpython/mlprodict · GitHub

This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ipynb  (1) .py  (22) .rst  (2) .txt  (1) All 4 file types selected
Only manifest files
Deleted files Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def ti(r, n):
for r in sorted(res1):
r1 = res1[r]
r2 = res2[r]
ratio = r2['total'] / r1['total']
ratio = r2['ttime'] / r1['ttime']
res[r] = ratio
return res

Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_op_add.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from onnxruntime import InferenceSession
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.algebra.onnx_ops import OnnxAdd
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from tqdm import tqdm
from mlprodict.testing.experimental_c import code_optimisation
print(code_optimisation())
Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_op_einsum.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from onnxruntime import InferenceSession
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.algebra.onnx_ops import OnnxEinsum
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from tqdm import tqdm
from opt_einsum import contract
from mlprodict.testing.experimental_c import (
Expand Down
4 changes: 2 additions & 2 deletions _doc/examples/plot_op_onnx_topk.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from pandas import DataFrame
from onnxruntime import InferenceSession, __version__ as ort_version
from tqdm import tqdm
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from pyquickhelper.pycode.profiling import profile
from skl2onnx.algebra.onnx_ops import OnnxTopK_11
from skl2onnx.common.data_types import FloatTensorType
Expand Down Expand Up @@ -139,7 +139,7 @@ def ti(n):
for r in sorted(res1):
r1 = res1[r]
r2 = res2[r]
ratio = r2['total'] / r1['total']
ratio = r2['ttime'] / r1['ttime']
res[r] = ratio
return res

Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_op_reducemax.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from onnxruntime import InferenceSession
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.algebra.onnx_ops import OnnxReduceMax
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from tqdm import tqdm
from mlprodict.testing.experimental_c import code_optimisation
print(code_optimisation())
Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_op_reducemean.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from onnxruntime import InferenceSession
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.algebra.onnx_ops import OnnxReduceMean
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from tqdm import tqdm
from mlprodict.testing.experimental_c import code_optimisation
print(code_optimisation())
Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_op_reducesum.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from onnxruntime import InferenceSession
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.algebra.onnx_ops import OnnxReduceSumApi11
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from tqdm import tqdm
from mlprodict.testing.experimental_c import (
code_optimisation, custom_reducesum_rk_float)
Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_op_reducesumsquare.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from onnxruntime import InferenceSession
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.algebra.onnx_ops import OnnxReduceSumSquare
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from tqdm import tqdm
from mlprodict.testing.experimental_c import code_optimisation
print(code_optimisation())
Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_op_transpose.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from onnxruntime import InferenceSession
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.algebra.onnx_ops import OnnxTranspose
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from tqdm import tqdm
from mlprodict.testing.experimental_c import code_optimisation
print(code_optimisation())
Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_op_where.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from onnxruntime import InferenceSession
from skl2onnx.common.data_types import FloatTensorType, BooleanTensorType
from skl2onnx.algebra.onnx_ops import OnnxWhere, OnnxSub, OnnxMul
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from tqdm import tqdm
from mlprodict.testing.experimental_c import code_optimisation
print(code_optimisation())
Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_parallelism.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from sklearn.datasets import make_regression
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.model_selection import train_test_split
from mlprodict.tools import measure_time
from cpyquickhelper.numbers import measure_time
from pyquickhelper.pycode.profiling import profile
from mlprodict.onnx_conv import to_onnx, register_rewritten_operators
from mlprodict.onnxrt import OnnxInference
Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_speedup_pca.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from sklearn.decomposition import PCA
from pyquickhelper.pycode.profiling import profile
from mlprodict.sklapi import OnnxSpeedupTransformer
from mlprodict.tools.speed_measure import measure_time
from cpyquickhelper.numbers.speed_measure import measure_time
from tqdm import tqdm

################################
Expand Down
6 changes: 3 additions & 3 deletions _doc/notebooks/topk_cpp.ipynb

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions _doc/sphinxdoc/source/api/tools.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ the possibility later to only show a part of a graph.
Others
======

Benchmark
+++++++++

.. autosignature:: mlprodict.tools.speed_measure.measure_time

Plotting
++++++++

Expand Down
22 changes: 12 additions & 10 deletions _doc/sphinxdoc/source/index.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,10 @@ mlprodict
all_notebooks
HISTORY

*mlprodict* explores couple of ways to compute predictions faster
than the library used to build the machine learned model,
mostly :epkg:`scikit-learn` which is optimized for training,
which is equivalent to batch predictions.
One way is to use :epkg:`ONNX`.
:epkg:`onnxruntime` provides an efficient way
to compute predictions. *mlprodict* implements
a *python/numpy* runtime for :epkg:`ONNX` which
does not have any dependency on :epkg:`scikit-learn`.
*mlprodict* was initially started to help implementing converters
to :epkg:`ONNX`. The main feature is a python runtime for
:epkg:`ONNX`. It gives more feedback than :epkg:`onnxruntime`
when the execution fails.

.. runpython::
:showcode:
Expand All @@ -112,7 +107,8 @@ does not have any dependency on :epkg:`scikit-learn`.

# Conversion into ONNX.
from mlprodict.onnx_conv import to_onnx
model_onnx = to_onnx(lr, X.astype(numpy.float32))
model_onnx = to_onnx(lr, X.astype(numpy.float32),
black_op={'LinearRegressor'})
print("ONNX:", str(model_onnx)[:200] + "\n...")

# Predictions with onnxruntime
Expand All @@ -124,6 +120,12 @@ does not have any dependency on :epkg:`scikit-learn`.
# Measuring the maximum difference.
print("max abs diff:", measure_relative_difference(expected, ypred['variable']))

# And the python runtime
oinf = OnnxInference(model_onnx, runtime='python')
ypred = oinf.run({'X': X[:5].astype(numpy.float32)},
verbose=1, fLOG=print)
print("ONNX output:", ypred)

These predictions are obtained with the
following :epkg:`ONNX` graph.

Expand Down
35 changes: 34 additions & 1 deletion _unittests/ut_onnxrt/test_onnxrt_simple.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from pyquickhelper.pycode import ExtTestCase, get_temp_folder, ignore_warnings
from skl2onnx.algebra.onnx_ops import ( # pylint: disable=E0611
OnnxAdd, OnnxLinearRegressor, OnnxLinearClassifier,
OnnxConstantOfShape, OnnxShape, OnnxIdentity)
OnnxConstantOfShape, OnnxShape, OnnxIdentity,
OnnxIf, OnnxSub, OnnxGreater, OnnxReduceSum)
from skl2onnx.common.data_types import FloatTensorType, Int64TensorType
from skl2onnx import __version__ as skl2onnx_version
from mlprodict.onnx_conv import to_onnx
Expand Down Expand Up @@ -462,6 +463,38 @@ def test_blofat16(self):
[1, -5, -6], dtype=numpy.float16))
self.assertIn('n0_min(X, Y)', str(oinf))

@ignore_warnings(DeprecationWarning)
def test_onnx_if_to_dot(self):
opv = 15
x1 = numpy.array([[0, 3], [7, 0]], dtype=numpy.float32)
x2 = numpy.array([[1, 0], [2, 0]], dtype=numpy.float32)

node = OnnxAdd(
'x1', 'x2', output_names=['absxythen'], op_version=opv)
then_body = node.to_onnx(
{'x1': x1, 'x2': x2}, target_opset=opv,
outputs=[('absxythen', FloatTensorType())])
node = OnnxSub(
'x1', 'x2', output_names=['absxyelse'], op_version=opv)
else_body = node.to_onnx(
{'x1': x1, 'x2': x2}, target_opset=opv,
outputs=[('absxyelse', FloatTensorType())])
del else_body.graph.input[:]
del then_body.graph.input[:]

cond = OnnxGreater(
OnnxReduceSum('x1', op_version=opv),
OnnxReduceSum('x2', op_version=opv),
op_version=opv)
ifnode = OnnxIf(cond, then_branch=then_body.graph,
else_branch=else_body.graph,
op_version=opv, output_names=['y'])
model_def = ifnode.to_onnx(
{'x1': x1, 'x2': x2}, target_opset=opv,
outputs=[('y', FloatTensorType())])
dot = OnnxInference(model_def, skip_run=True).to_dot(recursive=True)
self.assertIn("subgraph cluster_If", dot)


if __name__ == "__main__":
unittest.main()
29 changes: 0 additions & 29 deletions _unittests/ut_tools/test_mesure_time.py

This file was deleted.

4 changes: 4 additions & 0 deletions mlprodict/onnxrt/onnx_inference.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class OnnxInference:
outputs may share the same name)
:param static_inputs: Loop can use static variables,
variables from the graph which runs the loop
(enumerate of strings)
:param new_outputs: if the loading fails, it might worth
cutting the graph, if not None, the graph will
be cut to have these new_outputs as the final outputs
Expand All @@ -80,6 +81,9 @@ class OnnxInference:

.. versionchanged:: 0.7
Parameters *new_outputs*, *new_opset* were added.

.. versionchanged:: 0.8
Parameter *static_inputs* was added.
"""

def __init__(self, onnx_or_bytes_or_stream, runtime=None,
Expand Down
6 changes: 5 additions & 1 deletion mlprodict/onnxrt/onnx_inference_exports.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def dot_label(text):

# nodes
fill_names = {}
static_inputs = [n.name for n in self.oinf.obj.graph.input]
static_inputs.extend(n.name for n in self.oinf.obj.graph.initializer)
for node in self.oinf.obj.graph.node:
exp.append("")
for out in node.output:
Expand All @@ -195,6 +197,7 @@ def dot_label(text):
exp.append(
' {2}{0} [shape=box label="{0}{3}" fontsize={1}];'.format(
dot_name(out), fontsize, dot_name(prefix), dot_label(sh)))
static_inputs.append(out)

dobj = _var_as_dict(node)
if dobj['name'].strip() == '': # pragma: no cover
Expand Down Expand Up @@ -231,7 +234,8 @@ def dot_label(text):
# creates the subgraph
body = dobj['atts'][field]['value']
oinf = self.oinf.__class__(
body, runtime=self.oinf.runtime, skip_run=self.oinf.skip_run)
body, runtime=self.oinf.runtime, skip_run=self.oinf.skip_run,
static_inputs=static_inputs)
subprefix = prefix + "B_"
subdot = oinf.to_dot(recursive=recursive, prefix=subprefix,
add_rt_shapes=add_rt_shapes)
Expand Down
2 changes: 1 addition & 1 deletion mlprodict/onnxrt/validate/validate_benchmark.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def allow(N, obs):
number=number, div_by_number=True)
if (skip_long_test and not node_time and
res[N] is not None and
res[N].get('total', time_limit) >= time_limit):
res[N].get('ttime', time_limit) >= time_limit):
# too long
break # pragma: no cover
if node_time:
Expand Down
33 changes: 13 additions & 20 deletions mlprodict/onnxrt/validate/validate_helper.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"""
import math
import copy
from timeit import Timer
import os
import warnings
from importlib import import_module
import pickle
from time import perf_counter
import numpy
from cpyquickhelper.numbers import measure_time as _c_measure_time
from sklearn.base import BaseEstimator
from sklearn.linear_model._base import LinearModel
from sklearn.model_selection import train_test_split
Expand Down Expand Up @@ -325,7 +325,8 @@ def default_time_kwargs():
}


def measure_time(stmt, x, repeat=10, number=50, div_by_number=False, first_run=True):
def measure_time(stmt, x, repeat=10, number=50, div_by_number=False,
first_run=True, max_time=None):
"""
Measures a statement and returns the results as a dictionary.

Expand All @@ -335,6 +336,9 @@ def measure_time(stmt, x, repeat=10, number=50, div_by_number=False, first_run=T
:param number: number of executions in one row
:param div_by_number: divide by the number of executions
:param first_run: if True, runs the function once before measuring
:param max_time: execute the statement until the total goes
beyond this time (approximatively), *repeat* is ignored,
*div_by_number* must be set to True
:return: dictionary

See `Timer.repeat <https://docs.python.org/3/library/timeit.html?timeit.Timer.repeat>`_
Expand All @@ -345,28 +349,17 @@ def measure_time(stmt, x, repeat=10, number=50, div_by_number=False, first_run=T
if x is None:
raise ValueError("x cannot be None") # pragma: no cover

try:
stmt(x)
except RuntimeError as e: # pragma: no cover
raise RuntimeError("{}-{}".format(type(x), x.dtype)) from e

def fct():
stmt(x)

if first_run:
fct()
tim = Timer(fct)
res = numpy.array(tim.repeat(repeat=repeat, number=number))
total = numpy.sum(res)
if div_by_number:
res /= number
mean = numpy.mean(res)
dev = numpy.mean(res ** 2)
dev = max(0, (dev - mean**2)) ** 0.5
mes = dict(average=mean, deviation=dev, min_exec=numpy.min(res),
max_exec=numpy.max(res), repeat=repeat, number=number,
total=total)
return mes
try:
fct()
except RuntimeError as e: # pragma: no cover
raise RuntimeError("{}-{}".format(type(x), x.dtype)) from e

return _c_measure_time(fct, context={}, repeat=repeat, number=number,
div_by_number=div_by_number, max_time=max_time)


def _multiply_time_kwargs(time_kwargs, time_kwargs_fact, inst):
Expand Down
Loading

Back | FazBrowse Home | New Git URL