module npy.xop#

Inheritance diagram of mlprodict.npy.xop

Short summary#

module mlprodict.npy.xop

Xop API to build onnx graphs. Inspired from sklearn-onnx.

Classes#

class

truncated documentation

_CustomSchema

For operators defined outside onnx.

_GraphBuilder

Graph builder. It takes a graph structure made with instances of OnnxOperatorBase. The main method is to_onnx. …

_StaticVariables

Holds static variables.

_WrapperLogger

Wrappers around class logging.Logger to take indentation into account.

OnnxExisting

Wrapper around OnnxIdentity to specify this operator is not part of the subgraph it is used in.

OnnxLoadFactory

Automatically creating all operators from onnx packages takes time. That’s why function loadop only creates …

OnnxOperator

Ancestor to every ONNX operator exposed in mlprodict.npy.xops and mlprodict.npy.xops_ml.

OnnxOperatorBase

Base class for OnnxOperator, OnnxOperator`Item, :class:`OnnxOperatorTuple.

OnnxOperatorFunction

This operator is used to insert existing ONNX function into the ONNX graph being built.

OnnxOperatorItem

Accessor to one of the output returned by a OnnxOperator.

OnnxOperatorTuple

Class used to return multiple OnnxVar at the same time.

Functions#

function

truncated documentation

_default_OPSET_TO_IR_VERSION

Returns the default mapping between opset and ir_version.

_domain_to_class_name

Converts domain into a name.

_dynamic_class_creation

Automatically generates classes for each of the operators module onnx defines and described at Operators

_find_operator_domain

Determines the domain of an operator. Raises an exception if not found or if there is an ambiguity.

_get_all_operator_schema

_populate_schemas

Populates all schemas.

_split_op_name

ClassFactory

Dynamically creates a class for a specific operator.

loadop

Dynamically creates a class for a every operator type in the given list.

Properties#

property

truncated documentation

all_classes

Returns all operators wrapped in classes.

all_domains

Returns all domains.

all_schemas

Returns all schemas.

all_schemas_versions

Returns all operators, domains, versions.

external_inputs

Returns the list of implicit inputs the subgraph assumes to be existing even if they are not referenced as …

inputs

Returns the only inputs in a list.

inputs

Returns the only inputs in a list.

output_names

Returns self.output_names_.

output_names

Returns self.output_names_.

output_names

This method should be overwritten.

output_names

Returns self.output_names_.

output_names

Returns None.

output_names

Returns ‘output_names’ of attribute ‘unique’.

outputs

Returns ‘output_names’ of attribute ‘unique’.

Static Methods#

staticmethod

truncated documentation

__class_getitem__

Enables expression cls[opset]. It returns the appropriate class cls_opset. Parameter op_version should be …

__class_getitem__

Enables expression cls[opset]. It returns the appropriate class cls_opset. Parameter op_version should be …

__class_getitem__

Enables expression cls[opset]. It returns the appropriate class cls_opset. Parameter op_version should be …

_merge_op_version

_merge_op_version

_merge_op_version

_node_to_graph_get_type

_node_to_graph_get_type

_node_to_graph_get_type

_node_to_graph_preprocess_list

_node_to_graph_preprocess_list

_node_to_graph_preprocess_list

_node_to_graph_process_input

_node_to_graph_process_input

_node_to_graph_process_input

_node_to_graph_reorder_by_name

_node_to_graph_reorder_by_name

_node_to_graph_reorder_by_name

attribute_to_value

Converts an attribute into a value using python structures.

get_unique_name

Returns a unique variable name.

number2alpha

Converts a numbers into a string keeping the same alphabetical order.

ParseFromString

Parses this class from a json string.

Methods#

method

truncated documentation

__abs__

Automatically adds operator OnnxAbs to the graph.

__abs__

Automatically adds operator OnnxAbs to the graph.

__abs__

Automatically adds operator OnnxAbs to the graph.

__add__

Automatically adds operator OnnxAdd to the graph.

__add__

Automatically adds operator OnnxAdd to the graph.

__add__

Automatically adds operator OnnxAdd to the graph.

__call__

Creates an instance of class OnnxOperatorFunction. Equivalent to OnnxOperatorFunction(proto, *args, **kwargs). …

__call__

Creates an instance of class OnnxOperatorFunction. Equivalent to OnnxOperatorFunction(proto, *args, **kwargs). …

__call__

Creates an instance of class OnnxOperatorFunction. Equivalent to OnnxOperatorFunction(proto, *args, **kwargs). …

__eq__

Automatically adds operator OnnxEqual to the graph.

__eq__

Automatically adds operator OnnxEqual to the graph.

__eq__

Automatically adds operator OnnxEqual to the graph.

__eq__

__getattr__

Enables expressions such as:

__getitem__

Returns an accessor to one of the output of this node.

__getitem__

Returns an accessor to one of the output of this node.

__getitem__

Returns an accessor to one of the output of this node.

__getitem__

usual

__gt__

Automatically adds operator OnnxGreater to the graph.

__gt__

Automatically adds operator OnnxGreater to the graph.

__gt__

Automatically adds operator OnnxGreater to the graph.

__init__

__init__

__init__

__init__

__init__

__init__

__init__

__init__

__init__

__init__

__init__

constructor

__iter__

Allows expressions such as a, b = OnnxTopK(...).

__iter__

Allows expressions such as a, b = OnnxTopK(...).

__iter__

Allows expressions such as a, b = OnnxTopK(...).

__iter__

Iterates on the outputs.

__len__

usual

__lt__

Automatically adds operator OnnxLess to the graph.

__lt__

Automatically adds operator OnnxLess to the graph.

__lt__

Automatically adds operator OnnxLess to the graph.

__matmul__

Automatically adds operator OnnxMatMul to the graph.

__matmul__

Automatically adds operator OnnxMatMul to the graph.

__matmul__

Automatically adds operator OnnxMatMul to the graph.

__mod__

Automatically adds operator OnnxMod to the graph.

__mod__

Automatically adds operator OnnxMod to the graph.

__mod__

Automatically adds operator OnnxMod to the graph.

__mul__

Automatically adds operator OnnxMul to the graph.

__mul__

Automatically adds operator OnnxMul to the graph.

__mul__

Automatically adds operator OnnxMul to the graph.

__ne__

Automatically adds operator OnnxNot x OnnxEqual to the graph.

__ne__

Automatically adds operator OnnxNot x OnnxEqual to the graph.

__ne__

Automatically adds operator OnnxNot x OnnxEqual to the graph.

__pow__

Automatically adds operator OnnxPow to the graph.

__pow__

Automatically adds operator OnnxPow to the graph.

__pow__

Automatically adds operator OnnxPow to the graph.

__repr__

usual

__repr__

usual

__repr__

usual

__repr__

usual

__repr__

usual

__repr__

__str__

usual

__sub__

Automatically adds operator OnnxSub to the graph.

__sub__

Automatically adds operator OnnxSub to the graph.

__sub__

Automatically adds operator OnnxSub to the graph.

__truediv__

Automatically adds operator OnnxDiv to the graph.

__truediv__

Automatically adds operator OnnxDiv to the graph.

__truediv__

Automatically adds operator OnnxDiv to the graph.

_add_domain

_add_name

_add_subgraph

Fills attribute attribute.

_add_subgraph

Fills attribute attribute.

_add_subgraph

Fills attribute attribute.

_check

_check

_check

_node_to_graph

Builds a graph as a list of nodes to walk through in that order.

_node_to_graph

Builds a graph as a list of nodes to walk through in that order.

_node_to_graph

Builds a graph as a list of nodes to walk through in that order.

_post_process_attributes

Walks through attributes and replaces them by ONNX values.

_post_process_attributes

Walks through attributes and replaces them by ONNX values.

_post_process_attributes

Walks through attributes and replaces them by ONNX values.

_process_io

_set_control_op

_set_control_op

Sets control_op for every instance of OnnxExisting node.

_set_control_op

Tells this operator is part of a subgraph.

_set_control_op

Sets control_op for every instance of OnnxExisting node.

_set_control_op

Tells this operator is part of a subgraph.

_set_control_op

Tells this operator is part of a subgraph.

_to_onnx_attribute

Converts one subgraph into ONNX. Returns the ONNX graph and the hidden inputs.

_to_onnx_attribute

Converts one subgraph into ONNX. Returns the ONNX graph and the hidden inputs.

_to_onnx_attribute

Converts one subgraph into ONNX. Returns the ONNX graph and the hidden inputs.

_to_onnx_attributes

Converts attributes into ONNX. Returns the hidden inputs.

_to_onnx_attributes

Converts attributes into ONNX. Returns the hidden inputs.

_to_onnx_attributes

Converts attributes into ONNX. Returns the hidden inputs.

_to_onnx_attributes

Calls self.onx_op._to_onnx_attributes.

_to_onnx_attributes

Calls self.onx_op._to_onnx_attributes.

add_external_input

Tells a subgraph this node comes from a graph calling this one.

add_external_input

Tells a subgraph this node comes from a graph calling this one.

add_external_input

Tells a subgraph this node comes from the main graph. It may be used only by the subgraph but it must be processed …

add_external_input

Tells a subgraph this node comes from a graph calling this one.

add_external_input

Tells a subgraph this node comes from the main graph. It may be used only by the subgraph but it must be processed …

add_external_input

Tells a subgraph this node comes from the main graph. It may be used only by the subgraph but it must be processed …

add_function

Adds a function to the graph.

add_initializer

Adds an initializer to the graph.

add_node

Adds a node to the graph.

add_to

Adds to graph builder.

add_to

Adds to graph builder.

add_to

This method should be overwritten.

add_to

Adds to graph builder.

add_to

Adds to graph builder. Does nothing because the original node is already added.

add_to

Adds to graph builder. Does nothing because the original node is already added.

and_

Automatically adds operator OnnxAnd to the graph.

and_

Automatically adds operator OnnxAnd to the graph.

and_

Automatically adds operator OnnxAnd to the graph.

astype

Automatically adds operator OnnxCast to the graph.

astype

Automatically adds operator OnnxCast to the graph.

astype

Automatically adds operator OnnxCast to the graph.

data

Returns all data in that class in a dictionary.

debug

debug

dedent

unindent

else_do

Fills attribute else_branch.

else_do

Fills attribute else_branch.

else_do

Fills attribute else_branch.

f

For the eager mode.

f

Computes the predictions for this node. Similar to an eager evaluation.

f

Evaluates this node.

f

Computes the predictions for this node. Similar to an eager evaluation.

f

Computes the predictions for this node. Similar to an eager evaluation.

f

Evaluates this node.

find_named_inputs

Retrieves all named inputs in this graph.

find_named_inputs

Retrieves all named inputs in this graph.

find_named_inputs

Returns all inputs to the graph.

find_named_inputs

Retrieves all named inputs in this graph.

find_named_inputs

Returns all inputs to the graph.

find_named_inputs

Returns all inputs to the graph.

find_schema

Checks if there is an existing schema for a specific version.

find_schema

Checks if there is an existing schema for a specific version.

find_schema

Checks if there is an existing schema for a specific version.

get_input_names

Returns input names for node node and inputs inputs.

get_output_result

Returns the output name at position i.

get_output_result

Returns the output name at position i.

get_output_result

Returns the output name at position i.

get_output_result

Returns the output name at position i.

get_unique_name

Returns a unique name to name an output.

get_unique_output_name

Returns a unique output_name for a NodeResultName.

indent

indent

not_

Automatically adds operator OnnxNot to the graph.

not_

Automatically adds operator OnnxNot to the graph.

not_

Automatically adds operator OnnxNot to the graph.

or_

Automatically adds operator OnnxOr to the graph.

or_

Automatically adds operator OnnxOr to the graph.

or_

Automatically adds operator OnnxOr to the graph.

populate

Populates static variables.

predecessors

Returns the list of predecessors.

predecessors

Returns the list of predecessors.

predecessors

Returns the list of predecessors.

reserve_name

Reserves a name so that it cannot be changed.

reserve_names

Adds names to the list of reserved names. All must be unique.

run

Other name for OnnxInference.f.

run

Other name for OnnxInference.f.

run

Other name for OnnxInference.f.

SerializeToString

Serializes this class into json.

then_do

Fills attribute then_branch.

then_do

Fills attribute then_branch.

then_do

Fills attribute then_branch.

to_onnx

Converts this operator into an ONNX graph.

to_onnx

Converts this operator into an ONNX graph.

to_onnx

Converts this operator into an ONNX graph.

to_onnx

Converts this operator into an ONNX graph. It follows the same signature as OnnxOperator.to_onnx

to_onnx

Converts this operator into an ONNX graph.

to_onnx_this

Returns a simple ONNX graph corresponding to this node.

to_onnx_this

Returns a simple ONNX graph corresponding to this node.

to_onnx_this

Returns a simple ONNX graph corresponding to this node.

update_max_item

Some operators return a undefined number of outputs. The method is called when require one of them (with __getitem__) …

update_max_item

Some operators return a undefined number of outputs. The method is called when require one of them (with __getitem__) …

update_max_item

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.

source on GitHub

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

source on GitHub

class mlprodict.npy.xop.OnnxExisting(*args, **kwargs)#

Bases: OnnxOperator

Wrapper around OnnxIdentity to specify this operator is not part of the subgraph it is used in.

source on GitHub

__init__(*args, **kwargs)#
__repr__()#

usual

source on GitHub

_set_control_op(op)#

Sets control_op for every instance of OnnxExisting node.

Parameters:

op – operator calling the subgraph.

source on GitHub

_unique_names = {}#
f(*inputs, verbose=0, fLOG=None, clear_cache=False, runtime=None)#

For the eager mode.

find_named_inputs()#

Retrieves all named inputs in this graph.

source on GitHub

static get_unique_name(var)#

Returns a unique variable name.

Parameters:

var – an instance of OnnxOperator.

Returns:

unique variable name

source on GitHub

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(...)

source on GitHub

__getattr__(name)#

Enables expressions such as:

ops = OnnxLoadFactory()
op = ops.Abs('X')

source on GitHub

__init__()#
class mlprodict.npy.xop.OnnxOperator(*inputs, op_version=None, output_names=None, domain=None, global_context=None, **kwargs)#

Bases: OnnxOperatorBase

Ancestor to every ONNX operator exposed in mlprodict.npy.xops and mlprodict.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

source on GitHub

class _InputContainer#

Bases: object

__init__()#
__iter__()#
__len__()#
__repr__()#

Return repr(self).

append(inp)#

Append one element to the list.

has_input(inp)#

Checks that input inp is part the list of names.

__abs__()#

Automatically adds operator OnnxAbs to the graph.

Parameters:

ov – onnx node

Returns:

OnnxAbs(self, ov)

source on GitHub

__add__(ov)#

Automatically adds operator OnnxAdd to the graph.

Parameters:

ov – onnx node

Returns:

OnnxAdd(self, ov)

source on GitHub

__call__(*args, function_name=None, function_domain=None, **kwargs)#

Creates an instance of class OnnxOperatorFunction. Equivalent to OnnxOperatorFunction(proto, *args, **kwargs).

Parameters:
Returns:

instance of type OnnxOperatorFunction

source on GitHub

classmethod __class_getitem__(opset)#

Enables expression cls[opset]. It returns the appropriate class cls_opset. Parameter op_version should be specified.

source on GitHub

__eq__(ov)#

Automatically adds operator OnnxEqual to the graph.

Parameters:

ov – onnx node

Returns:

OnnxEqual(self, ov)

source on GitHub

__getitem__(index)#

Returns an accessor to one of the output of this node.

source on GitHub

__gt__(ov)#

Automatically adds operator OnnxGreater to the graph.

Parameters:

ov – onnx node

Returns:

OnnxGreater(self, ov)

source on GitHub

__hash__ = None#
__init__(*inputs, op_version=None, output_names=None, domain=None, global_context=None, **kwargs)#
__iter__()#

Allows expressions such as a, b = OnnxTopK(...).

source on GitHub

__lt__(ov)#

Automatically adds operator OnnxLess to the graph.

Parameters:

ov – onnx node

Returns:

OnnxLess(self, ov)

source on GitHub

__matmul__(ov)#

Automatically adds operator OnnxMatMul to the graph.

Parameters:

ov – onnx node

Returns:

OnnMatMul(self, ov)

source on GitHub

__mod__(ov)#

Automatically adds operator OnnxMod to the graph.

Parameters:

ov – onnx node

Returns:

OnnxMod(self, ov)

source on GitHub

__mul__(ov)#

Automatically adds operator OnnxMul to the graph.

Parameters:

ov – onnx node

Returns:

OnnxMul(self, ov)

source on GitHub

__ne__(ov)#

Automatically adds operator OnnxNot x OnnxEqual to the graph.

Parameters:

ov – onnx node

Returns:

OnnxNot(OnnxEqual(self, ov))

source on GitHub

__pow__(ov)#

Automatically adds operator OnnxPow to the graph.

Parameters:

ov – onnx node

Returns:

OnnPow(self, ov)

source on GitHub

__repr__()#

usual

source on GitHub

__sub__(ov)#

Automatically adds operator OnnxSub to the graph.

Parameters:

ov – onnx node

Returns:

OnnxSub(self, ov)

source on GitHub

__truediv__(ov)#

Automatically adds operator OnnxDiv to the graph.

Parameters:

ov – onnx node

Returns:

OnnxDiv(self, ov)

source on GitHub

_add_subgraph(attribute, branch)#

Fills attribute attribute.

Parameters:
  • attribute – attribute name

  • branch – onnx graph or OnnxOperator

Returns:

self

source on GitHub

_check()#
static _merge_op_version(n1, n2)#
_node_to_graph(other_outputs=None, inputs=None, outputs=None, as_function=False, processed=None)#

Builds a graph as a list of nodes to walk through in that order.

source on GitHub

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(processed, 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.

source on GitHub

_set_control_op(op)#

Sets control_op for every instance of OnnxExisting node.

Parameters:

op – operator calling the subgraph.

source on GitHub

_to_onnx_attribute(oxop, inputs=None, target_opset=None, optim=True, verbose=0, run_shape=True, fLOG=<built-in function print>, processed=None)#

Converts one subgraph into ONNX. Returns the ONNX graph and the hidden inputs.

source on GitHub

_to_onnx_attributes(inputs=None, target_opset=None, optim=True, verbose=0, run_shape=True, fLOG=<built-in function print>, processed=None)#

Converts attributes into ONNX. Returns the hidden inputs.

source on GitHub

add_external_input(op)#

Tells a subgraph this node comes from a graph calling this one.

source on GitHub

add_to(builder)#

Adds to graph builder.

Parameters:

builder – instance of _GraphBuilder, it must have a method add_node

source on GitHub

and_(ov)#

Automatically adds operator OnnxAnd to the graph.

Parameters:

ov – onnx node

Returns:

OnnxAnd(self, ov)

source on GitHub

astype(to)#

Automatically adds operator OnnxCast to the graph.

Parameters:

ov – onnx node

Returns:

OnnxCast(self, ov, to=to)

source on GitHub

else_do(branch)#

Fills attribute else_branch.

Parameters:

branch – onnx graph or OnnxOperator

Returns:

self

source on GitHub

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.

source on GitHub

find_named_inputs()#

Retrieves all named inputs in this graph.

source on GitHub

find_schema(op_version)#

Checks if there is an existing schema for a specific version.

Parameters:

op_version – requested version

Returns:

schema

source on GitHub

get_output_result(i=0)#

Returns the output name at position i.

source on GitHub

not_()#

Automatically adds operator OnnxNot to the graph.

Parameters:

ov – onnx node

Returns:

OnnxNot(self, ov)

source on GitHub

or_(ov)#

Automatically adds operator OnnxOr to the graph.

Parameters:

ov – onnx node

Returns:

OnnxOr(self, ov)

source on GitHub

property output_names#

Returns self.output_names_.

predecessors()#

Returns the list of predecessors.

Returns:

list of OnnxOperator

source on GitHub

run(*inputs, verbose=0, fLOG=None, clear_cache=False, runtime=None)#

Other name for OnnxInference.f.

source on GitHub

then_do(branch)#

Fills attribute then_branch.

Parameters:

branch – onnx graph or OnnxOperator

Returns:

self

source on GitHub

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>, processed=None, check_model=True, return_builder=False)#

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

  • processed – keeps track the of the processed nodes

  • check_model – checks the output model

  • return_builder – if True, returns the instance of GraphBuilder used to build the onnx graph.

Returns:

ONNX stucture

inputs and outputs parameters work the same way. Here is some possible walues:

  • inputs=numpy.float32: all inputs are dense tensors of unknown shapes sharing the same element type

  • inputs={‘X’: numpy.float32, ‘Y’: numpy.in64}`: input X is a dense tensor of float32, input Y is a dense tensor of int64,

  • {‘X’: numpy.array(…)}}: input X is a dense tensor with a precise shape

  • inputs=[Variable(‘X’, numpy.float32, [1, 2])]: input X is a dense tensor of float32 with shape [1, 2]

  • inputs=[Variable(‘X’, numpy.float32, [None, 2])]: input X is a dense tensor of float32 with a 2D tensor with an unknown dimension (first one)

  • see Variable

(OnnxOperator)

source on GitHub

to_onnx_this(evaluated_inputs)#

Returns a simple ONNX graph corresponding to this node.

Parameters:

evaluated_inputs – inputs as a list

Returns:

ONNX graph

source on GitHub

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

source on GitHub

class mlprodict.npy.xop.OnnxOperatorBase#

Bases: object

Base class for OnnxOperator, OnnxOperator.

source on GitHub

__init__()#
_set_control_op(op)#

Tells this operator is part of a subgraph.

source on GitHub

add_external_input(op)#

Tells a subgraph this node comes from the main graph. It may be used only by the subgraph but it must be processed as well.

source on GitHub

add_to(builder)#

This method should be overwritten.

f(*args, **kwargs)#

Evaluates this node.

source on GitHub

find_named_inputs()#

Returns all inputs to the graph.

source on GitHub

property output_names#

This method should be overwritten.

class mlprodict.npy.xop.OnnxOperatorFunction(function_proto, *inputs, output_names=None, sub_functions=None)#

Bases: OnnxOperator

This operator is used to insert existing ONNX function into the ONNX graph being built.

Parameters:
  • function_proto – instance of type FunctionProto

  • inputs – inputs

  • output_names – output names

  • sub_functions – functions called by this one

source on GitHub

__init__(function_proto, *inputs, output_names=None, sub_functions=None)#
__repr__()#

usual

add_to(builder)#

Adds to graph builder.

Parameters:

builder – instance of _GraphBuilder, it must have a method add_node

source on GitHub

static attribute_to_value(att)#

Converts an attribute into a value using python structures.

source on GitHub

class mlprodict.npy.xop.OnnxOperatorItem(onx_op, index, op_version=None)#

Bases: OnnxOperatorBase

Accessor to one of the output returned by a OnnxOperator.

Parameters:
  • onx_opOnnxOperator

  • index – integer

  • op_version – defines the opset version

source on GitHub

__init__(onx_op, index, op_version=None)#
__repr__()#

usual

__str__()#

usual

_to_onnx_attributes(inputs=None, target_opset=None, optim=True, verbose=0, run_shape=True, fLOG=<built-in function print>, processed=None)#

Calls self.onx_op._to_onnx_attributes.

source on GitHub

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

source on GitHub

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.

source on GitHub

find_named_inputs()#

Returns all inputs to the graph.

source on GitHub

get_output_result(i=0)#

Returns the output name at position i.

source on GitHub

property inputs#

Returns the only inputs in a list.

property output_names#

Returns None.

class mlprodict.npy.xop.OnnxOperatorTuple(first, *args)#

Bases: OnnxOperatorBase

Class used to return multiple OnnxVar at the same time.

source on GitHub

__getitem__(i)#

usual

__init__(first, *args)#
__iter__()#

Iterates on the outputs.

__len__()#

usual

__repr__()#

usual

_to_onnx_attributes(inputs=None, target_opset=None, optim=True, verbose=0, run_shape=True, fLOG=<built-in function print>, processed=None)#

Calls self.onx_op._to_onnx_attributes.

source on GitHub

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

source on GitHub

property external_inputs#

Returns the list of implicit inputs the subgraph assumes to be existing even if they are not referenced as explicit input for the graph.

source on GitHub

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. (OnnxOperatorTuple)

source on GitHub

class mlprodict.npy.xop._CustomSchema(schema)#

Bases: object

For operators defined outside onnx.

source on GitHub

static ParseFromString(s)#

Parses this class from a json string.

SerializeToString()#

Serializes this class into json.

__eq__(ot)#

Return self==value.

__hash__ = None#
__init__(schema)#
__repr__()#

Return repr(self).

class _attribute(att)#

Bases: object

attribute

__eq__(ot)#

Return self==value.

__hash__ = None#
__init__(att)#
data()#

Returns all data in that class in a dictionary.

_atts = ['domain', 'name', 'since_version', 'inputs', 'outputs', 'attributes', 'min_input', 'max_input', 'min_output', 'max_output', 'doc']#
class _empty#

Bases: object

dummy class

static from_attribute(data)#

Creates an instance of _CustomSchema._attribute.

static from_io(data)#

Creates an instance of _CustomSchema._io.

class _io(t)#

Bases: object

input, output

__eq__(ot)#

Return self==value.

__hash__ = None#
__init__(t)#
data()#

Returns all data in that class in a dictionary.

data()#

Returns all data in that class in a dictionary.

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 }

source on GitHub

__init__()#
_add_domain(domain, version)#
_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

source on GitHub

add_initializer(name, init)#

Adds an initializer to the graph.

Parameters:
  • name – initializer name

  • init – initializer to copy

Returns:

created intializer

source on GitHub

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

source on GitHub

get_input_names(node, inputs)#

Returns input names for node node and inputs inputs.

Parameters:
  • node – node

  • inputs – inputs

Returns:

name

source on GitHub

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

source on GitHub

get_unique_output_name(result)#

Returns a unique output_name for a NodeResultName.

Parameters:

result – instance of NodeResultName

source on GitHub

static number2alpha(index)#

Converts a numbers into a string keeping the same alphabetical order.

source on GitHub

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

source on GitHub

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

source on GitHub

to_onnx(inputs=None, outputs=None, target_opset=None, run_shape=False, optim=True, function_name=None, function_domain=None, verbose=0, check_model=True)#

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

  • check_model – checks the output model

Returns:

onnx graph

(_GraphBuilder)

source on GitHub

class mlprodict.npy.xop._StaticVariables#

Bases: object

Holds static variables.

source on GitHub

__init__()#
property all_classes#

Returns all operators wrapped in classes.

property all_domains#

Returns all domains.

property all_schemas#

Returns all schemas.

property all_schemas_versions#

Returns all operators, domains, versions.

populate()#

Populates static variables.

class mlprodict.npy.xop._WrapperLogger(lg)#

Bases: object

Wrappers around class logging.Logger to take indentation into account.

source on GitHub

constructor

__init__(lg)#

constructor

debug(msg, *args)#
dedent()#

unindent

indent()#
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}

source on GitHub

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

source on GitHub

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

source on GitHub

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

source on GitHub

mlprodict.npy.xop._get_all_operator_schema()#
mlprodict.npy.xop._populate_schemas()#

Populates all schemas.

source on GitHub

mlprodict.npy.xop._split_op_name(name)#
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.

source on GitHub