onnx_light.onnx.reference#
- class onnx_light.onnx.reference.ReferenceEvaluator(proto: ModelProto | GraphProto | FunctionProto | bytes | str | PathLike, *, verbose: int = 0, events_enabled: bool = False, release_intermediates: bool = True)#
Evaluates an ONNX model using the C++
KernelDispatchTable.The class is constructed from a
ModelProto/GraphProto/FunctionProto(or the bytes / file path of a serialisedModelProto).run()then takes a{name: ndarray}feed dictionary and returns the requested outputs as a list of NumPy arrays, mirroring the calling convention ofonnx.reference.ReferenceEvaluator(andonnxruntime.InferenceSession).- Parameters:
verbose – Verbosity level forwarded to the runtime.
0disables progress printing; positive values print one line per dispatched node while the model is executing.
Example
import numpy as np from onnx_light.onnx_lib import parser from onnx_light.reference import ReferenceEvaluator model = parser.parse_model( '<ir_version: 10, opset_import: ["" : 18]>' 'agraph (float[3] x) => (float[3] y) { y = Abs(x) }' ) sess = ReferenceEvaluator(model) (y,) = sess.run(None, {"x": np.array([-1.0, 2.0, -3.5], dtype=np.float32)}) # y == np.array([1.0, 2.0, 3.5], dtype=np.float32)
- events() list[Any]#
Returns the event log from the most recent
run()call.Each entry is a
RuntimeEventobject with anas_dict()method that returns a dictionary with the keys"action","kind","name","data_type","shape","value_count","values"and"string_values".Returns an empty list if
run()has not been called yet.
- property input_names: list[str]#
Names of the graph (or function) inputs, in declaration order.
Inputs that are also listed in the graph initializers are omitted, since the caller does not have to supply them.
- property opsets: dict[str, int]#
Mapping
domain -> versionextracted fromopset_import.Empty for evaluators built from a bare
GraphProto.
- register_custom_kernel(domain: str, op_type: str, fn: Any) None#
Registers a Python custom kernel for
(domain, op_type).The kernel is invoked on every
run()call whenever a node matches the registered(domain, op_type)pair. Custom kernels override any built-in onnx-light kernel with the same key (model-local functions and the built-in control-flow operatorsIf/Loop/Scan/SequenceMapstill take precedence).- Parameters:
domain – Operator domain. The empty string is treated as
ai.onnx.op_type – Operator name (
NodeProto.op_type).fn – Python callable invoked as
fn(node, *inputs)wherenodeis the matchingNodeProtoandinputsare the input tensors converted tonumpy.ndarray. The callable must return either a singlenumpy.ndarray(for single-output kernels) or a tuple / list of arrays (for multi-output kernels), in the same order as the node’s declared outputs.
Examples
def square(node, x): return x * x sess.register_custom_kernel("my.domain", "Square", square)
- run(output_names: list[str] | None, feed_inputs: dict[str, Any]) list[ndarray | list]#
Executes the wrapped graph / model / function.
- Parameters:
output_names – Names of the outputs to return.
Noneis shorthand for “every declared output, in declaration order”.feed_inputs – Mapping of input name to value. Tensor inputs are fed as a
numpy.ndarray;seq(T)inputs are fed as alist(ortuple) of arrays, one per sequence element. Every name listed byinput_namesmust be present.
- Returns:
One entry per name in
output_names(defaults tooutput_names), in the requested order. Tensor-typed outputs are returned asnumpy.ndarray; sequence-typed outputs are returned as alistofnumpy.ndarray(one array per sequence element).- Return type:
list of
numpy.ndarrayor list ofnumpy.ndarray