onnx_light.onnx.reference#
Mixed native and NumPy evaluation#
Use MixedReferenceEvaluator when
the native Gemm, MatMul, Conv, or Attention kernels are
too slow for a model. It has the same constructor and run API as
ReferenceEvaluator, but automatically
registers NumPy implementations for these four operators in each session.
Other operators continue to use native kernels. Performance varies with
the model, tensor sizes, and the cost of crossing the NumPy/native boundary.
from onnx_light.onnx.reference import MixedReferenceEvaluator
outputs = MixedReferenceEvaluator(model).run(None, feeds)
Native dtype behavior#
The compiled runtime preserves UTF-8 string initializers across repeated
evaluations, including inputs passed to custom callbacks. Explicit feeds may
override initializers for one evaluation without modifying the model or its
cached defaults. Compress supports string tensors, including empty results.
Less compares DOUBLE inputs without narrowing. ReduceMin and
ReduceMax retain numeric input types (and support BOOL), while
ReduceMean supports FLOAT, DOUBLE, FLOAT16 and BFLOAT16.
Half-precision reductions accumulate in float32; double reductions retain
float64 precision. Reductions propagate NaNs. Empty extrema reductions use
positive/negative infinity for floating-point inputs and the corresponding
integer or boolean identity; empty floating-point means produce NaN.
Axes attributes, axes inputs, keepdims and noop_with_empty_axes retain
their ONNX semantics.
LogSoftmax supports FLOAT, DOUBLE and FLOAT16 with stable
maximum-subtracted exponentiation and float32 half-precision accumulation.
Before opset 13 it flattens the dimensions starting at axis (default 1);
from opset 13 it reduces only that axis (default -1).
Where supports float16 selection without changing the selected bits,
including signed zero, and broadcasts empty dimensions.
- class onnx_light.onnx.reference.MixedReferenceEvaluator(proto, **kwargs)#
Uses NumPy for slow native operators and native kernels for the rest.
Registers per-session NumPy implementations for
Gemm,MatMul,Conv, andAttentionbefore the first run. The evaluator retains theReferenceEvaluatorAPI; other operators still use native kernels. Actual performance depends on the model and tensor sizes.
- class onnx_light.onnx.reference.ReferenceEvaluator(proto: ModelProto | GraphProto | FunctionProto | bytes | str | PathLike, *, verbose: int = 0, events_enabled: bool = False, release_intermediates: bool = True, allocator: Any = None, io_allocator: Any = None, cpu_execution: Any = None, cpu_execution_counters: bool = False, consume_initializers: bool = False)#
Evaluates an ONNX model using the C++
KernelDispatchTable.For models where native
Gemm,MatMul,Conv, orAttentionkernels are slow,MixedReferenceEvaluatorsubstitutes NumPy implementations while retaining native dispatch for other operators.The class is constructed from a
ModelProto/GraphProto/FunctionProto(or the bytes / file path of a serialisedModelProto).run()then takes a feed dictionary whose tensor values may be NumPy arrays or runtimeTensorobjects and returns the requested outputs as a list of NumPy arrays, mirroring the calling convention ofonnx.reference.ReferenceEvaluator(andonnxruntime.InferenceSession).An evaluator instance is not thread-safe for concurrent execution. The caller must serialize
run()andgenerate()calls on the same instance, including overlapping calls to different methods. Independent evaluators with independent runtime contexts are required for concurrent requests. Releasing the Python GIL and parallel kernel execution do not make an evaluator or its context safe to use concurrently.- Parameters:
verbose – Verbosity level forwarded to the runtime.
0disables progress printing; positive values print one line per dispatched node while the model is executing.events_enabled – When
True, the runtime records aRuntimeEventfor every tensor map mutation and node dispatch, retrievable throughevents()afterrun().release_intermediates – When
True(the default), the runtime frees each intermediate tensor as soon as its last consumer has run.allocator – Optional
SimpleRawBufferAllocator(or anyRawBufferAllocator) attached to the internalRuntimeContext. When provided together withevents_enabled=True, every recordedRuntimeEventcarries the allocator’s live (allocated_bytes) and peak (peak_bytes) memory, so the event log doubles as a per-node memory profile. The allocator must have enough slotcapacityfor the number of buffers alive at the same time; the caller retains ownership.io_allocator – Optional
IOArena(or anyRawBufferAllocator) dedicated to declared graph outputs. When neither allocator is provided, the evaluator creates persistent execution and I/O arenas and reuses them across runs. Passing only one allocator creates a default persistent arena for the other lifetime domain; pass the same allocator as bothallocatorandio_allocatorto request single-allocator behaviour explicitly.consume_initializers – Allows preparations to replace exclusively consumed initializer payloads. Defaults to
False. PassingTruetransfers the supplied model’s payload ownership: do not reuse/share that model or reset its resolved kernels afterward. Shared, overridable, and graph-output initializers remain unchanged. Prepared kernels must support every inference shape without reading the retired representation. Model-based generation requires reusable sources and must leave this option disabled.
Example
import numpy as np from onnx_light.onnx_lib import parser from onnx_light.onnx.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)
- property cpu_execution_counters: Any#
Returns cumulative counters for the shared executor.
Compatible evaluators lease the same executor, so enabling counters on any lease enables them for that executor and the snapshot includes dispatches from every compatible leaseholder.
- property cpu_execution_resolution: Any#
Returns the immutable CPU policy resolved for this evaluator.
- property cpu_executor_instance_id: int#
Returns the process-local identity of the leased CPU executor.
Compatible evaluators that lease the same executor return the same value. This diagnostic identity is not stable across processes and must not be used as a tuning-cache key.
- events() list[Any]#
Returns the event log from
run()orgenerate()calls.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 no execution has been recorded yet.
- generate(feed_inputs: dict[str, Any], max_new_tokens: int = 20, temperature: float = 0.0, eos_token_ids: list[int] | None = None, pad_token_id: int | None = None, seed: int | None = None, *, embedding: ReferenceEvaluator | None = None, position_axes: int = 0, prefill_only_inputs: list[str] | None = None, top_k: int = 0, top_p: float | None = None) ndarray#
Generates token IDs with the native runtime.
feed_inputscontains INT64input_idsof shape[batch, sequence]and any other model inputs, including initial empty caches. Declaredattention_maskandposition_idsinputs are maintained automatically; prompts may be unpadded or left-padded. Graph persistent bindings enable cached decoding; models without bindings evaluate the full prefix. Rank-3position_idsuse[axes, batch, sequence]coordinates.position_axesdefaults to the axis count declared by the model; specify it when that dimension is dynamic. Text-only prompts replicate sequential coordinates on every axis. Generated tokens use the maximum unmasked prefill coordinate plus one on all axes.Zero temperature selects greedy decoding. Positive temperature samples softmax-scaled logits, reproducibly when
seedis supplied.top_kretains the highest-scoring tokens (zero disables it; k above the vocabulary retains all). Thentop_pretains the smallest probability-ranked prefix reaching p of the remaining, renormalized mass, including the crossing token. Ties favor lower token IDs.top_pmust be in (0, 1]; None or one disables it. Negative k and nonfinite p are invalid, even for greedy decoding, which ignores valid filters. Generation stops atmax_new_tokensor when every row emits a token ineos_token_ids. An empty list or None disables EOS stopping. Finished rows receivepad_token_id(the first EOS ID by default). Each call starts fresh state and does not modify the supplied feeds.This method is not thread-safe on a shared evaluator. The caller must serialize it with other
generateandruncalls on this instance; fresh generation state does not make the shared context thread-safe.With
embedding, token IDs are sent to itsinput_idsinput on every step, including the prompt. This evaluator’s model handles both prompt prefill and subsequent tokens. Graphs connect through their declared input/output names, without additional mappings. Both graphs use one call-local context configured from this evaluator: its custom kernels, CPU executor and execution settings apply to both models. The embedding evaluator supplies only its model, not its context or CPU policy. With cached decoding,prefill_only_inputslists embedding-only auxiliary tensor feeds such as image/video features. After prefill, their leading dimension becomes zero, preserving dtype and trailing dimensions. Each listed input requires an explicit nonscalar tensor feed and cannot be a token, mask or position input. Other feeds remain fixed.- Returns:
INT64 token IDs containing the prompt and generated tokens.
- Return type:
- get_kernel_usage() list[str]#
Returns a snapshot of recorded backend kernel names.
Recording is disabled by default and retains at most the first 1024 invocations after each
clear_kernel_usage()call. Backends may include implementation-path diagnostics in addition to kernel names.
- 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.
Map-typed (
map(K, V)) inputs are listed under their original graph-input name. These are fed as a Pythondict(e.g.{"x": {10: 1.5, 30: 2.5}}) when callingrun().
- 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, *, with_opset: bool = False) None#
Registers a Python custom kernel for
(domain, op_type).Future node resolutions use this registration. Already-resolved kernels keep their callable across repeated runs and shape changes; to use a replacement for those nodes, create a new evaluator and register the callback on it before its first run. Custom kernels override any built-in onnx-light kernel with the same key (model-local functions and the built-in control-flow operators
If/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.with_opset – If true, passes the node’s effective opset as an
opsetkeyword argument, using the enclosing model or function’s imports.
Examples
def square(node, x): return x * x sess.register_custom_kernel("my.domain", "Square", square)
- static register_custom_kernel_global(domain: str, op_type: str, fn: Any) None#
Registers a process-wide (global) numpy custom kernel.
Unlike
register_custom_kernel(), which only affects the evaluator it is called on, a global kernel is picked up by everyReferenceEvaluator(and any other runtime context). Register the kernel before running an evaluator, since an evaluator caches its runtime sessions on first run. Replacing either global or local registrations does not invalidate already-resolved kernels.fnfollows the samefn(node, *inputs)numpy contract asregister_custom_kernel(). A per-session registration for the same(domain, op_type)overrides the global one.- 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); seeregister_custom_kernel().
Examples
def square(node, x): return x * x ReferenceEvaluator.register_custom_kernel_global("my.domain", "Square", square)
- run(output_names: list[str] | None, feed_inputs: dict[str, Any]) list[ndarray | list[ndarray]]#
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.ndarrayor a runtimeTensor; both forms may be mixed in one call.seq(T)inputs are fed as alist(ortuple) of arrays or runtime tensors, one per sequence element;map(K, V)inputs are fed as a Pythondict(e.g.{"x": {10: 1.5}}). 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
- set_kernel_usage_enabled(enabled: bool) None#
Enables or disables backend kernel usage recording.
Existing entries are preserved when recording is disabled. The setting and recorded entries are shared with nested subgraphs and model-local functions, but not with independent evaluators.
- unregister_custom_kernel(domain: str, op_type: str) bool#
Removes a custom kernel previously registered for
(domain, op_type).Custom kernels are consulted before the built-in onnx-light dispatch table, so unregistering one restores the original built-in kernel for future resolutions when there is one. Already-resolved kernels are unaffected. If no built-in kernel exists for the pair, resolving a graph that uses it fails with an
unsupported op_typeerror, as it would before any custom kernel was registered.- Parameters:
domain – Operator domain. The empty string is treated as
ai.onnx.op_type – Operator name (
NodeProto.op_type).
- Returns:
Truewhen a custom kernel was removed,Falsewhen no custom kernel was registered for(domain, op_type).- Return type:
Examples
sess.register_custom_kernel("", "Abs", lambda node, x: -x) sess.unregister_custom_kernel("", "Abs") # restores built-in Abs
- static unregister_custom_kernel_global(domain: str, op_type: str) bool#
Removes a process-wide custom kernel registered by
register_custom_kernel_global().The empty domain is normalised to
ai.onnx. ReturnsTruewhen a global custom kernel was removed,Falseotherwise. Note that evaluators which already built (and cached) their runtime sessions keep dispatching to the previously resolved kernel until their sessions are rebuilt.- Parameters:
domain – Operator domain. The empty string is treated as
ai.onnx.op_type – Operator name (
NodeProto.op_type).
- Returns:
Truewhen a global custom kernel was removed.- Return type: