onnx.reference#

DefaultNone#

class onnx.reference.op_run.DefaultNone#

Default value for parameters when the parameter is not set but the operator has a default behavior for it.

Inference#

class onnx.reference.ReferenceEvaluator(proto: Any, opsets: Optional[Dict[str, int]] = None, functions: Optional[List[Union[ReferenceEvaluator, FunctionProto]]] = None, verbose: int = 0, new_ops: Optional[List[OpRun]] = None)#

Computes the outputs of an ONNX proto (ModelProto, FunctionProto, GraphProto, NodeProto). This is a pure python implementation of ONNX specifications. Mismatches may remain between the official specifications and the implementation here. In the case of such a mismatch, the official spec overrides this implementation.

Parameters:
  • protoonnx.ModelProto, onnx.GraphProto, onnx.FunctionProto, onnx.NodeProto, filename or bytes

  • verbose – display intermediate results on the standard output during the execution

  • opsets – if proto is an instance of GraphProto, opsets must be defined by a dictionary of

  • functions – known onnx functions

  • new_ops – this runtime can be used to test the implementations of new operators, new_ops is a list of classes derived from OpRun, every class must define the static attribute domain

The class maps every node to its associated implementation. When a subgraph of a function is met, it uses this class to execute the subgraph or the function. Next example shows how to run ReferenceEvaluator with an onnx model stored in file model.onnx.

import numpy as np
from onnx.reference import ReferenceEvaluator

X = np.array(...)
sess = ReferenceEvaluator("model.onnx")
results = sess.run(None, {"X": X})
print(results[0])  # display the first result

Parameter verbose may be used to show intermediate results.

import numpy as np
from onnx.reference import ReferenceEvaluator

X = np.array(...)
sess = ReferenceEvaluator("model.onnx", verbose=1)
results = sess.run(None, {"X": X})
print(results[0])  # display the first result

The class can use any implementation available in folder ops. Adding an implementation requires two changes. The first one is the implementation itself. Any existing node can be used as a template. The second is one line in file _op_list.py to import the file and let the reference evaluator know it exists.

This class can also be used to test an implementation of a custom operator. Let’s assume this new operator is InvAlpha from domain custom. The implementation must take place in a class inheriting from OpRun. It must also define attribute op_domain. Here is an example which computes \frac{1}{X + \alpha}.

alpha is an attribute. It can be defined by the onnx node or be defined by the function using this node. It is safe to assume that attributes are known at the same time as the input. Class ReferenceEvaluator must know about this new implementation and this can be done by specified argument new_ops.

sess = ReferenceEvaluator(onnx_model, new_ops=[InvAlpha])
got = sess.run(None, {"X": x})[0]

A specific node can be simply evaluated.

This can also be expressed as:

property input_names#

Returns the input names.

property opsets#

Returns the opsets.

property output_names#

Returns the output names.

run(output_names, feed_inputs: Dict[str, Any], attributes: Optional[Dict[str, Any]] = None)#

Executes the onnx model.

Parameters:
  • output_names – requested outputs by names, None for all

  • feed_inputs – dictionary { input name: input value }

  • attributes – attributes value if the instance runs a FunctionProto

Returns:

list of requested outputs

OpFunction#

class onnx.reference.op_run.OpFunction(onnx_node: NodeProto, log_function: Any, impl: Optional[Any] = None)#

Runs a custom function.

classmethod create(n_inputs: Optional[int] = None, n_outputs: Optional[int] = None, verbose: int = 0, **kwargs: Any) Any#

Instantiates this class based on the given information.

Parameters:
  • n_inputs – number of inputs (default is defined by the operator schema)

  • n_outputs – number of outputs (default is defined by the operator schema)

  • verbose – verbosity

  • kwargs – node attributes

Returns:

NodeProto

property domain: str#

Returns node attribute domain.

classmethod eval(*args: List[Any], n_outputs: Optional[int] = None, verbose: int = 0, **kwargs: Any) Any#

Evaluates this operator.

Parameters:
  • args – inputs

  • n_outputs – number of outputs (default is defined by the operator schema)

  • verbose – verbosity

  • kwargs – node attributes

Returns:

NodeProto

static implicit_inputs(graph: GraphProto) List[str]#

Returns all varibles not registered as inputs and not produced by an node inside the graph. This inputs are part of the context existing in the graph calling this one.

property input: Iterable[str]#

Returns node attribute input.

classmethod make_node(n_inputs: Optional[int] = None, n_outputs: Optional[int] = None, **kwargs: Any) NodeProto#

Creates an ONNX node for this class based on the given information.

Parameters:
  • n_inputs – number of inputs (default is defined by the operator schema)

  • n_outputs – number of outputs (default is defined by the operator schema)

  • verbose – verbosity

  • kwargs – node attributes

Returns:

NodeProto

Method eval creates an onnx node returned by method make_node.

need_context() bool#

Tells the runtime if this node needs the context (all the results produced so far) as it may silently access one of them (operator Scan, If, Loop). The default answer is False.

property output: Iterable[str]#

Returns node attribute output.

run(*args, linked_attributes=None, context=None)#

Calls method _run, catches exceptions, displays a longer error message.

Parameters:
  • args – inputs

  • linked_attributes – used if this has an attriute linked to the attribute of the function it belongs to

  • context – if this node is part of the subgraph, context is a dictionary with the values this node may use

Returns:

tuple of results

OpRun#

class onnx.reference.op_run.OpRun(onnx_node: NodeProto, run_params: Dict[str, Any], schema: Optional[Any] = None)#

Ancestor to all operators in this subfolder.

Parameters:
  • onnx_nodeonnx node

  • run_params – additional parameters such as verbose, opsets (it can be more than one if the operator has a subgraph), log for a logging function

  • schema – operator schema

classmethod create(n_inputs: Optional[int] = None, n_outputs: Optional[int] = None, verbose: int = 0, **kwargs: Any) Any#

Instantiates this class based on the given information.

Parameters:
  • n_inputs – number of inputs (default is defined by the operator schema)

  • n_outputs – number of outputs (default is defined by the operator schema)

  • verbose – verbosity

  • kwargs – node attributes

Returns:

NodeProto

property domain: str#

Returns node attribute domain.

classmethod eval(*args: List[Any], n_outputs: Optional[int] = None, verbose: int = 0, **kwargs: Any) Any#

Evaluates this operator.

Parameters:
  • args – inputs

  • n_outputs – number of outputs (default is defined by the operator schema)

  • verbose – verbosity

  • kwargs – node attributes

Returns:

NodeProto

static implicit_inputs(graph: GraphProto) List[str]#

Returns all varibles not registered as inputs and not produced by an node inside the graph. This inputs are part of the context existing in the graph calling this one.

property input: Iterable[str]#

Returns node attribute input.

classmethod make_node(n_inputs: Optional[int] = None, n_outputs: Optional[int] = None, **kwargs: Any) NodeProto#

Creates an ONNX node for this class based on the given information.

Parameters:
  • n_inputs – number of inputs (default is defined by the operator schema)

  • n_outputs – number of outputs (default is defined by the operator schema)

  • verbose – verbosity

  • kwargs – node attributes

Returns:

NodeProto

Method eval creates an onnx node returned by method make_node.

need_context() bool#

Tells the runtime if this node needs the context (all the results produced so far) as it may silently access one of them (operator Scan, If, Loop). The default answer is False.

property output: Iterable[str]#

Returns node attribute output.

run(*args, linked_attributes=None, context=None)#

Calls method _run, catches exceptions, displays a longer error message.

Parameters:
  • args – inputs

  • linked_attributes – used if this has an attriute linked to the attribute of the function it belongs to

  • context – if this node is part of the subgraph, context is a dictionary with the values this node may use

Returns:

tuple of results

RuntimeTypeError#

class onnx.reference.op_run.RuntimeTypeError#

Raised when a type of a variable is unexpected.

SparseTensor#

class onnx.reference.op_run.SparseTensor(values: ndarray, indices: ndarray, shape: Tuple[int])#

Simple representation of a sparse tensor. It is based on numpy but does not require scipy.

__init__(values: ndarray, indices: ndarray, shape: Tuple[int]) None#