module onnx_grammar.onnx_translation
¶
Functions¶
function |
truncated documentation |
---|---|
Returns a default context useful for most of the conversion from a function using numpy into ONNX. |
|
Returns a default useful context to compile the converter returned by |
|
Creates an array with a single element from a constant. |
|
Function for python operator |
|
Function for python unary operator |
|
Function for python operator |
|
Replacements for squareform … |
|
Translates a function into ONNX. The code it produces is using classes OnnxAbs, OnnxAdd, … |
Documentation¶
One class which visits a syntax tree.
-
mlprodict.onnx_grammar.onnx_translation.
get_default_context
()¶ Returns a default context useful for most of the conversion from a function using numpy into ONNX.
-
mlprodict.onnx_grammar.onnx_translation.
get_default_context_cpl
()¶ Returns a default useful context to compile the converter returned by
translate_fct2onnx
.
-
mlprodict.onnx_grammar.onnx_translation.
py_make_float_array
(cst, op_version=None)¶ Creates an array with a single element from a constant.
- Parameters
cst – constant
op_version – unused
- Returns
array
<<<
from mlprodict.onnx_grammar.onnx_translation import py_make_float_array print(py_make_float_array(5.5))
>>>
[5.5]
-
mlprodict.onnx_grammar.onnx_translation.
py_mul
(*x, op_version=None)¶ Function for python operator
*
.- Parameters
x – floats
op_version – unused
- Returns
x*y
-
mlprodict.onnx_grammar.onnx_translation.
py_opp
(x, op_version=None)¶ Function for python unary operator
-
.- Parameters
x – floats
op_version – unused
- Returns
-x
-
mlprodict.onnx_grammar.onnx_translation.
py_pow
(x, p, op_version=None)¶ Function for python operator
**
.- Parameters
x – float
p – power
op_version – unused
- Returns
-
mlprodict.onnx_grammar.onnx_translation.
squareform_pdist
(X, metric='sqeuclidean', op_version=None)¶ Replacements for squareform and pdist.
-
mlprodict.onnx_grammar.onnx_translation.
translate_fct2onnx
(fct, context=None, cpl=False, context_cpl=None, output_names=None, dtype=<class 'numpy.float32'>, verbose=0, fLOG=None)¶ Translates a function into ONNX. The code it produces is using classes OnnxAbs, OnnxAdd, …
- Parameters
fct – function to convert
context – context of the function to convert something like
{'numpy.transpose': numpy.transpose}
, if context is None, it receives a default value returnd byget_default_context
cpl – compile the function after it was created
context_cpl – context used at compiling time if context_cpl is None, it receives a default value returnd by
get_default_context_cpl
output_names – names of the output in the ONNX graph
dtype – numpy float type used to produce the model
verbose – integer, display more information
fLOG – logging function
- Returns
code or compiled code
Convert a function into ONNX code
The following code parses a python function and returns another python function which produces an ONNX graph if executed.
<<<
import numpy from mlprodict.onnx_grammar import translate_fct2onnx def trs(x, y): z = x + numpy.transpose(y, axes=[1, 0]) return x * z onnx_code = translate_fct2onnx( trs, context={'numpy.transpose': numpy.transpose}) print(onnx_code)
>>>
def trs(x, y, dtype=numpy.float32, op_version=None): z = ( OnnxAdd( x, OnnxTranspose( y, perm=[1, 0], op_version=op_version ), op_version=op_version ) ) return ( OnnxMul( x, z, op_version=op_version ) )
Next example goes further and compile the outcome.
Convert a function into ONNX code and run
The following code parses a python function and returns another python function which produces an ONNX graph if executed. The example executes the function, creates an ONNX then uses
OnnxInference
to compute predictions. Finally it compares them to the original.<<<
import numpy from mlprodict.onnx_grammar import translate_fct2onnx from mlprodict.onnxrt import OnnxInference from skl2onnx.algebra.onnx_ops import ( OnnxAdd, OnnxTranspose, OnnxMul, OnnxIdentity ) ctx = {'OnnxAdd': OnnxAdd, 'OnnxTranspose': OnnxTranspose, 'OnnxMul': OnnxMul, 'OnnxIdentity': OnnxIdentity} def trs(x, y): z = x + numpy.transpose(y, axes=[1, 0]) return x * z inputs = {'x': numpy.array([[1, 2]], dtype=numpy.float32), 'y': numpy.array([[-0.3, 0.4]], dtype=numpy.float32).T} original = trs(inputs['x'], inputs['y']) print('original output:', original) onnx_fct = translate_fct2onnx( trs, context={'numpy.transpose': numpy.transpose}, cpl=True, context_cpl=ctx, output_names=['Z']) onnx_code = onnx_fct('x', 'y', opset_version=12) print('ONNX code:', onnx_code) onnx_g = onnx_code.to_onnx(inputs, target_opset=12) oinf = OnnxInference(onnx_g) res = oinf.run(inputs) print("ONNX inference:", res['Z']) print("ONNX graph:", onnx_g)
>>>
original output: [[0.7 4.8]] [runpythonerror] Traceback (most recent call last): File "somewhere/workspace/mlprodict/mlprodict_UT_39_std/_doc/sphinxdoc/fct2onnx3.py", line 36, in <module> onnx_code = onnx_fct('x', 'y', opset_version=12) TypeError: trs() got an unexpected keyword argument 'opset_version'
The function to be converted may include python functions which must not be converted. In that case, their name must be prefixed by
py_
. The execution of the function this one builds produces the following error:TypeError: Parameter to MergeFrom() must be instance of same class: expected onnx.TensorProto got onnx.AttributeProto.
It indicates that constants in the code marges multiple types, usually floats and tensor of floats. Floats should be converted using the following function:
def py_make_float_array(cst): return numpy.array([cst], dtype=numpy.float32)
The function replaces empty contexts by default values which covers many numpy functions. The tutorial ONNX and Python Runtime gives an example of how it can be used on a more complex function.