module npy.xop
#
Short summary#
module mlprodict.npy.xop
Xop API to build onnx graphs. Inspired from sklearn-onnx.
Classes#
class |
truncated documentation |
---|---|
Graph builder. It takes a graph structure made with instances of |
|
Automatically creating all operators from onnx packages takes time. That’s why function |
|
Ancestor to every ONNX operator exposed in |
|
Base class for |
|
This operator is used to insert existing ONNX function into the ONNX graph being built. |
|
Accessor to one of the output returned by a |
|
Class used to return multiple |
Functions#
function |
truncated documentation |
---|---|
Returns the default mapping between opset and ir_version. |
|
Converts domain into a name. |
|
Automatically generates classes for each of the operators module onnx defines and described at Operators … |
|
Determines the domain of an operator. Raises an exception if not found or if there is an ambiguity. |
|
Populates all schemas. |
|
Dynamically creates a class for a specific operator. |
|
Dynamically creates a class for a every operator type in the given list. |
Properties#
property |
truncated documentation |
---|---|
Returns the only inputs in a list. |
|
Returns the only inputs in a list. |
|
Returns self.output_names_. |
|
This method should be overwritten. |
|
|
Returns self.output_names_. |
Returns None. |
|
Returns ‘output_names’ of attribute ‘unique’. |
|
Returns ‘output_names’ of attribute ‘unique’. |
Static Methods#
staticmethod |
truncated documentation |
---|---|
Enables expression cls[opset]. It returns the appropriate class cls_opset. Parameter op_version should be … |
|
|
Enables expression cls[opset]. It returns the appropriate class cls_opset. Parameter op_version should be … |
|
|
|
|
|
|
|
|
|
|
Converts an attribute into a value using python structures. |
|
Converts a numbers into a string keeping the same alphabetical order. |
Methods#
method |
truncated documentation |
---|---|
Automatically adds operator OnnxAbs to the graph. |
|
|
Automatically adds operator OnnxAbs to the graph. |
Automatically adds operator OnnxAdd to the graph. |
|
|
Automatically adds operator OnnxAdd to the graph. |
Creates an instance of class |
|
|
Creates an instance of class |
Automatically adds operator OnnxEqual to the graph. |
|
|
Automatically adds operator OnnxEqual to the graph. |
Enables expressions such as: |
|
Returns an accessor to one of the output of this node. |
|
|
Returns an accessor to one of the output of this node. |
usual |
|
Automatically adds operator OnnxGreater to the graph. |
|
|
Automatically adds operator OnnxGreater to the graph. |
Allows expressions such as |
|
|
Allows expressions such as |
Iterates on the outputs. |
|
usual |
|
Automatically adds operator OnnxLess to the graph. |
|
|
Automatically adds operator OnnxLess to the graph. |
Automatically adds operator OnnxMatMul to the graph. |
|
|
Automatically adds operator OnnxMatMul to the graph. |
Automatically adds operator OnnxMod to the graph. |
|
|
Automatically adds operator OnnxMod to the graph. |
Automatically adds operator OnnxMul to the graph. |
|
|
Automatically adds operator OnnxMul to the graph. |
Automatically adds operator OnnxNot x OnnxEqual to the graph. |
|
|
Automatically adds operator OnnxNot x OnnxEqual to the graph. |
Automatically adds operator OnnxPow to the graph. |
|
|
Automatically adds operator OnnxPow to the graph. |
usual |
|
usual |
|
usual |
|
usual |
|
usual |
|
Automatically adds operator OnnxSub to the graph. |
|
|
Automatically adds operator OnnxSub to the graph. |
Automatically adds operator OnnxDiv to the graph. |
|
|
Automatically adds operator OnnxDiv to the graph. |
|
|
Builds a graph as a list of nodes to walk through in that order. |
|
|
Builds a graph as a list of nodes to walk through in that order. |
Walks through attributes and replaces them by ONNX values. |
|
|
Walks through attributes and replaces them by ONNX values. |
Adds a function to the graph. |
|
Adds an initializer to the graph. |
|
Adds a node to the graph. |
|
Adds to graph builder. |
|
This method should be overwritten. |
|
Adds to graph builder. |
|
Adds to graph builder. Does nothing because the original node is already added. |
|
Adds to graph builder. Does nothing because the original node is already added. |
|
Automatically adds operator OnnxAnd to the graph. |
|
|
Automatically adds operator OnnxAnd to the graph. |
Automatically adds operator OnnxCast to the graph. |
|
|
Automatically adds operator OnnxCast to the graph. |
Computes the predictions for this node. Similar to an eager evaluation. |
|
Evaluates this node. |
|
|
Computes the predictions for this node. Similar to an eager evaluation. |
Computes the predictions for this node. Similar to an eager evaluation. |
|
|
Evaluates this node. |
Retrieves all named inputs in this graph. |
|
Returns all inputs to the graph. |
|
|
Retrieves all named inputs in this graph. |
Returns all inputs to the graph. |
|
|
Returns all inputs to the graph. |
Checks if there is an existing schema for a specific version. |
|
|
Checks if there is an existing schema for a specific version. |
Returns input names for node node and inputs inputs. |
|
Returns the output name at position i. |
|
|
Returns the output name at position i. |
Returns the output name at position i. |
|
Returns a unique name to name an output. |
|
Returns a unique output_name for a NodeResultName. |
|
Automatically adds operator OnnxNot to the graph. |
|
|
Automatically adds operator OnnxNot to the graph. |
Automatically adds operator OnnxOr to the graph. |
|
|
Automatically adds operator OnnxOr to the graph. |
Returns the list of predecessors. |
|
|
Returns the list of predecessors. |
Reserves a name so that it cannot be changed. |
|
Adds names to the list of reserved names. All must be unique. |
|
Other name for OnnxInference.f. |
|
|
Other name for OnnxInference.f. |
Converts this operator into an ONNX graph. |
|
|
Converts this operator into an ONNX graph. |
Converts this operator into an ONNX graph. It follows the same signature as |
|
Converts this operator into an ONNX graph. |
|
Returns a simple ONNX graph corresponding to this node. |
|
|
Returns a simple ONNX graph corresponding to this node. |
Some operators return a undefined number of outputs. The method is called when require one of them (with __getitem__) … |
|
|
Some operators return a undefined number of outputs. The method is called when require one of them (with __getitem__) … |
Documentation#
Xop API to build onnx graphs. Inspired from sklearn-onnx.
New in version 0.9.
- mlprodict.npy.xop.ClassFactory(class_name, op_name, inputs, outputs, input_range, output_range, domain, attr_names, doc, deprecated, since_version, past_version)#
Dynamically creates a class for a specific operator.
- Parameters
class_name – class name
op_name – operator type
inputs – expected inputs
outputs – expected outputs
input_range – input range
output_range – output_range
domain – domain
attr_names – attributes names
doc – docstring
deprecated – is the operator deprecated
since_version – available since version
past_version – list of versions
- class mlprodict.npy.xop.OnnxLoadFactory#
Bases:
object
Automatically creating all operators from onnx packages takes time. That’s why function
loadop
only creates classes for the requested operators. This class does the same when an attributes is requested.cl = OnnxLoadOperators() x = cl.Add(...)
It is equivalent to:
OnnxAdd = loadop('Add') x = OnnxAdd(...)
- __getattr__(name)#
Enables expressions such as:
ops = OnnxLoadFactory() op = ops.Abs('X')
- __init__()#
- class mlprodict.npy.xop.OnnxOperator(*inputs, op_version=None, output_names=None, domain=None, global_context=None, **kwargs)#
Bases:
mlprodict.npy.xop.OnnxOperatorBase
Ancestor to every ONNX operator exposed in
mlprodict.npy.xops
andmlprodict.npy.xops_ml
.- Parameters
inputs – list of inputs expected by the operator
op_version – to select a specific version of the operator
output_names – used defined names for the outputs
domain – to overwrite the default domain
global_context – operator If executes one subgraph whose nodes may use one existing output in the current context. If not used in the main graph, these operators are not linked to the output and cannot be retrieved. global_context is a dictionary mapped the subgraph input names to these operators.
kwargs – additional parameters of the operator
- __abs__()#
Automatically adds operator OnnxAbs to the graph.
- Parameters
ov – onnx node
- Returns
OnnxAbs(self, ov)
- __add__(ov)#
Automatically adds operator OnnxAdd to the graph.
- Parameters
ov – onnx node
- Returns
OnnxAdd(self, ov)
- __call__(*args, function_name=None, function_domain=None, **kwargs)#
Creates an instance of class
OnnxOperatorFunction
. Equivalent to OnnxOperatorFunction(proto, *args, **kwargs).- Parameters
args – see
OnnxOperatorFunction
function_name – name to be given to the function
function_domain – function domain, if None, it is given a default value
kwargs – see
OnnxOperatorFunction
- Returns
instance of type
OnnxOperatorFunction
- classmethod __class_getitem__(opset)#
Enables expression cls[opset]. It returns the appropriate class cls_opset. Parameter op_version should be specified.
- __eq__(ov)#
Automatically adds operator OnnxEqual to the graph.
- Parameters
ov – onnx node
- Returns
OnnxEqual(self, ov)
- __getitem__(index)#
Returns an accessor to one of the output of this node.
- __gt__(ov)#
Automatically adds operator OnnxGreater to the graph.
- Parameters
ov – onnx node
- Returns
OnnxGreater(self, ov)
- __hash__ = None#
- __init__(*inputs, op_version=None, output_names=None, domain=None, global_context=None, **kwargs)#
- __iter__()#
Allows expressions such as
a, b = OnnxTopK(...)
.
- __lt__(ov)#
Automatically adds operator OnnxLess to the graph.
- Parameters
ov – onnx node
- Returns
OnnxLess(self, ov)
- __matmul__(ov)#
Automatically adds operator OnnxMatMul to the graph.
- Parameters
ov – onnx node
- Returns
OnnMatMul(self, ov)
- __mod__(ov)#
Automatically adds operator OnnxMod to the graph.
- Parameters
ov – onnx node
- Returns
OnnxMod(self, ov)
- __mul__(ov)#
Automatically adds operator OnnxMul to the graph.
- Parameters
ov – onnx node
- Returns
OnnxMul(self, ov)
- __ne__(ov)#
Automatically adds operator OnnxNot x OnnxEqual to the graph.
- Parameters
ov – onnx node
- Returns
OnnxNot(OnnxEqual(self, ov))
- __pow__(ov)#
Automatically adds operator OnnxPow to the graph.
- Parameters
ov – onnx node
- Returns
OnnPow(self, ov)
- __repr__()#
usual
- __sub__(ov)#
Automatically adds operator OnnxSub to the graph.
- Parameters
ov – onnx node
- Returns
OnnxSub(self, ov)
- __truediv__(ov)#
Automatically adds operator OnnxDiv to the graph.
- Parameters
ov – onnx node
- Returns
OnnxDiv(self, ov)
- _check()#
- static _merge_op_version(n1, n2)#
- _node_to_graph(other_outputs=None, inputs=None, outputs=None, as_function=False)#
Builds a graph as a list of nodes to walk through in that order.
- static _node_to_graph_get_type(node, name=None, outputs=None, outputs_dtype=None)#
- static _node_to_graph_preprocess_list(inputs)#
- static _node_to_graph_process_input(inputs, set_inputs, node, inp, new_inputs, new_stack, inputs_dtype, as_function=False)#
- static _node_to_graph_reorder_by_name(new_inputs, inputs)#
- _post_process_attributes()#
Walks through attributes and replaces them by ONNX values.
- add_to(builder)#
Adds to graph builder.
- Parameters
builder – instance of
_GraphBuilder
, it must have a method add_node
- and_(ov)#
Automatically adds operator OnnxAnd to the graph.
- Parameters
ov – onnx node
- Returns
OnnxAnd(self, ov)
- astype(to)#
Automatically adds operator OnnxCast to the graph.
- Parameters
ov – onnx node
- Returns
OnnxCast(self, ov, to=to)
- f(*inputs, verbose=0, fLOG=None, clear_cache=False, runtime=None)#
Computes the predictions for this node. Similar to an eager evaluation.
- Parameters
inputs – inputs as dictionary or a list of inputs (see below)
verbose – display information while predicting
fLOG – logging function if verbose > 0
clear_cache – onnx graph is created once unless this parameter is True
runtime – runtime to use for the evaluation, see
OnnxInference
- Returns
outputs as a dictionary if the input were given as a dictionary or a single result or a tuple otherwise
The inputs refer to the inputs of the graph. The method walks through all inputs and finds inputs defined as string. It replaces them by the value found in the dictionary. If the inputs are specified in a list, the function retrieves the list of inputs defined as a string and assigns them a value. Logging function can be used to get more insight about it. During the evaluation every node is independently converted into ONNX. The ONNX graph is cached in the class itself.
- find_named_inputs()#
Retrieves all named inputs in this graph.
- find_schema(op_version)#
Checks if there is an existing schema for a specific version.
- Parameters
op_version – requested version
- Returns
schema
- get_output_result(i=0)#
Returns the output name at position i.
- not_()#
Automatically adds operator OnnxNot to the graph.
- Parameters
ov – onnx node
- Returns
OnnxNot(self, ov)
- or_(ov)#
Automatically adds operator OnnxOr to the graph.
- Parameters
ov – onnx node
- Returns
OnnxOr(self, ov)
- property output_names#
Returns self.output_names_.
- predecessors()#
Returns the list of predecessors.
- Returns
list of
OnnxOperator
- run(*inputs, verbose=0, fLOG=None, clear_cache=False, runtime=None)#
Other name for OnnxInference.f.
- to_onnx(inputs=None, outputs=None, other_outputs=None, target_opset=None, optim=True, verbose=0, run_shape=True, function_name=None, function_domain=None, fLOG=<built-in function print>)#
Converts this operator into an ONNX graph.
- Parameters
inputs – information about type, it should not be None
outputs – information about types, if None, the function will use shape inference to guess the final output type and shape
other_outputs – additional nodes to consider as graph outputs but not outputs of this particular node
target_opset – dictionary with target opset per domain, None for the default one
optim – optimize the model with function
onnx_optimisations
run_shape – in case output shapes are not specify, the function runs function infer_shapes to guess them, False would disable that default behaviour
verbose – prints information
function_name – if not None, returns a FunctionProto
function_domain – in case of a function, declares the function as part of this domain
fLOG – logging function
:return ONNX stucture
- to_onnx_this(evaluated_inputs)#
Returns a simple ONNX graph corresponding to this node.
- Parameters
evaluated_inputs – inputs as a list
- Returns
ONNX graph
- update_max_item(index)#
Some operators return a undefined number of outputs. The method is called when require one of them (with __getitem__) and keeps the greater requested index assuming the node does not output any result beyond that index.
- Parameters
index – requested index
- class mlprodict.npy.xop.OnnxOperatorBase#
Bases:
object
Base class for
OnnxOperator
,OnnxOperator
.- __init__()#
- add_to(builder)#
This method should be overwritten.
- f(*args, **kwargs)#
Evaluates this node.
- find_named_inputs()#
Returns all inputs to the graph.
- property output_names#
This method should be overwritten.
- class mlprodict.npy.xop.OnnxOperatorFunction(function_proto, *inputs, output_names=None)#
Bases:
mlprodict.npy.xop.OnnxOperator
This operator is used to insert existing ONNX function into the ONNX graph being built.
- __init__(function_proto, *inputs, output_names=None)#
- __repr__()#
usual
- add_to(builder)#
Adds to graph builder.
- Parameters
builder – instance of
_GraphBuilder
, it must have a method add_node
- static attribute_to_value(att)#
Converts an attribute into a value using python structures.
- class mlprodict.npy.xop.OnnxOperatorItem(onx_op, index, op_version=None)#
Bases:
mlprodict.npy.xop.OnnxOperatorBase
Accessor to one of the output returned by a
OnnxOperator
.- Parameters
onx_op –
OnnxOperator
index – integer
op_version – defines the opset version
- __init__(onx_op, index, op_version=None)#
- __repr__()#
usual
- __str__()#
usual
- add_to(builder)#
Adds to graph builder. Does nothing because the original node is already added.
- Parameters
builder – instance of
_GraphBuilder
, it must have a method add_node
- f(*inputs, verbose=0, fLOG=None, clear_cache=False, runtime=None)#
Computes the predictions for this node. Similar to an eager evaluation.
- Parameters
inputs – inputs as dictionary or a list of inputs (see below)
verbose – display information while predicting
fLOG – logging function if verbose > 0
clear_cache – onnx graph is created once unless this parameter is True
runtime – runtime to use for the evaluation, see
OnnxInference
- Returns
outputs as a dictionary if the input were given as a dictionary or a single result or a tuple otherwise
The inputs refer to the inputs of the graph. The method walks through all inputs and finds inputs defined as string. It replaces them by the value found in the dictionary. If the inputs are specified in a list, the function retrieves the list of inputs defined as a string and assigns them a value. Logging function can be used to get more insight about it. During the evaluation every node is independently converted into ONNX. The ONNX graph is cached in the class itself.
- find_named_inputs()#
Returns all inputs to the graph.
- get_output_result(i=0)#
Returns the output name at position i.
- property inputs#
Returns the only inputs in a list.
- property output_names#
Returns None.
- class mlprodict.npy.xop.OnnxOperatorTuple(first, *args)#
Bases:
mlprodict.npy.xop.OnnxOperatorBase
Class used to return multiple
OnnxVar
at the same time.- __getitem__(i)#
usual
- __init__(first, *args)#
- __iter__()#
Iterates on the outputs.
- __len__()#
usual
- __repr__()#
usual
- add_to(builder)#
Adds to graph builder. Does nothing because the original node is already added.
- Parameters
builder – instance of
_GraphBuilder
, it must have a method add_node
- property inputs#
Returns the only inputs in a list.
- property output_names#
Returns ‘output_names’ of attribute ‘unique’.
- property outputs#
Returns ‘output_names’ of attribute ‘unique’.
- to_onnx(inputs=None, outputs=None, other_outputs=None, target_opset=None, optim=True, verbose=0, run_shape=True)#
Converts this operator into an ONNX graph. It follows the same signature as
OnnxOperator.to_onnx
and calls this method of the unique input object or the first one if there are several. In that case, other inputs in attribute values are moved into container other_outputs.
- class mlprodict.npy.xop._GraphBuilder#
Bases:
object
Graph builder. It takes a graph structure made with instances of
OnnxOperatorBase
. The main method is to_onnx.initializer: list of initializers to add to the ONNX graph
node: list of nodes to add to the ONNX graph
input: list of inputs to add to the ONNX graph
output: list of inputs to add to the ONNX graph
opsets: opsets of the ONNX graph
- input_names: dictionary of input names
{name: InputDetectedVariable}
- node_output_names: memorizes a name for a node output
when the user did not specify any {(id(node), index): OutputDetectedVariable}
- reserved_names: dictionary { name : (node, index) },
name which should remain unchanged in the ONNX graph
names: list of uniques names
functions: dictionary { domain, name: function_proto }
function_hashes: dictionary { domain, name: hash of function_proto }
- __init__()#
- _add_name(name)#
- _process_io(inputs, input_names)#
- add_function(function_proto, raise_if_exist=False, check_unique=True, opset=1)#
Adds a function to the graph.
- Parameters
function_proto – instance of type FunctionProto
raise_if_exist – raises an exception if a function of the same name was already added
check_unique – checks if a function was added twice, it is the same
opset – opset for the domain the function belongs to
- add_initializer(name, init)#
Adds an initializer to the graph.
- Parameters
name – initializer name
init – initializer to copy
- Returns
created intializer
- add_node(op_type, name, inputs, outputs, domain='', opset=None, **attributes)#
Adds a node to the graph.
- Parameters
op_type – operator type
name – node name
inputs – inputs name list
outputs – outputs name list
domain – node domain
opset – node opset
- Returns
created node
- get_input_names(node, inputs)#
Returns input names for node node and inputs inputs.
- Parameters
node – node
inputs – inputs
- Returns
name
- get_unique_name(name, reserved=True)#
Returns a unique name to name an output.
- Parameters
name – name
reserved – bypass if the name is a reserved one
- Returns
unique name, may be the same if not taken already
- get_unique_output_name(result)#
Returns a unique output_name for a NodeResultName.
- Parameters
result – instance of
NodeResultName
- static number2alpha(index)#
Converts a numbers into a string keeping the same alphabetical order.
- reserve_name(node, name, index)#
Reserves a name so that it cannot be changed.
- Parameters
node – node or None for an input
name – name
index – input index
- reserve_names(node, output_names)#
Adds names to the list of reserved names. All must be unique.
- Parameters
node – node or None for an input
output_names – names of the output
- to_onnx(inputs=None, outputs=None, target_opset=None, run_shape=False, optim=True, function_name=None, function_domain=None, verbose=0)#
Converts this operator into an ONNX graph.
- Parameters
inputs – specific inputs (as a dictionary) or default inputs if not specified
outputs – specific outputs
target_opset – dictionary with target opset per domain, None for the default one
run_shape – run shape inference before returning the model
optim – optimize the model with function
onnx_optimisations
function_name – if not None builds a FunctionProto use this name
function_domain – in case of a function, declares the function as part of this domain, ‘mlprodict’ if None
verbose – prints information
- Returns
onnx graph
- mlprodict.npy.xop._default_OPSET_TO_IR_VERSION()#
Returns the default mapping between opset and ir_version.
<<<
import pprint from mlprodict.npy.xop import _default_OPSET_TO_IR_VERSION pprint.pprint(_default_OPSET_TO_IR_VERSION())
>>>
{1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3, 7: 3, 8: 4, 9: 4, 10: 5, 11: 6, 12: 7, 13: 7, 14: 7, 15: 8, 16: 8}
- mlprodict.npy.xop._domain_to_class_name(domain)#
Converts domain into a name.
- Parameters
domain – domain name such as ai.onnx.ml
- Returns
string
<<<
from mlprodict.npy.xop import _domain_to_class_name print(_domain_to_class_name('ai.onnx.ml'))
>>>
AiOnnxMl
- mlprodict.npy.xop._dynamic_class_creation(operator_names=None, cache=False, include_past=False, verbose=0, fLOG=<built-in function print>)#
Automatically generates classes for each of the operators module onnx defines and described at Operators and Operators.
- Parameters
operator_names – list of operators to request or None for all
cache – extract the documentation from onnx package and saves it on disk it True
include_past – includes past versions if operator_names is None
verbose – display some progress
fLOG – logging function
- Returns
list of requested operators as a tuple
- mlprodict.npy.xop._find_operator_domain(name)#
Determines the domain of an operator. Raises an exception if not found or if there is an ambiguity.
- Parameters
name – operator name
- Returns
domain
- mlprodict.npy.xop._populate_schemas()#
Populates all schemas.
- mlprodict.npy.xop.loadop(*names, cache=False, verbose=0, fLOG=<built-in function print>)#
Dynamically creates a class for a every operator type in the given list.