OnnxBackend#

class openstef_foundation_models.inference.onnx_backend.OnnxBackend(metadata: CheckpointMetadata, session: InferenceSession) None[source]

Bases: object

An InferenceBackend backed by ONNX Runtime.

The session is built once on construction and reused for every run() call, so a single backend instance can be shared across an entire backtest. Users may either let the backend build a session from a resolved checkpoint and provider chain, or pass a pre-built session they own.

Parameters:
  • metadata (CheckpointMetadata)

  • session (InferenceSession)

__init__(metadata: CheckpointMetadata, session: InferenceSession) None[source]

Wrap a pre-built ONNX Runtime session.

Prefer from_checkpoint() unless you need to own the session lifecycle yourself.

Parameters:
  • metadata (CheckpointMetadata) – Metadata describing the checkpoint the session executes.

  • session (InferenceSession) – A pre-built ONNX Runtime inference session.

  • metadata

  • session

classmethod from_checkpoint(checkpoint: ResolvedCheckpoint, providers: Sequence[Annotated[CpuProvider | CudaProvider | TensorRTProvider | CoreMLProvider, FieldInfo(annotation=NoneType, required=True, discriminator='kind')]] | None = None, session_options: SessionOptionsConfig | None = None, *, policy: ProviderPolicy | None = None) Self[source]

Build a backend by loading a checkpoint into a new ONNX Runtime session.

With providers=None the policy selects a chain from the checkpoint and host; an explicit providers list is used as given and policy is ignored. See ProviderPolicy for how a chain is chosen and how strictly its realization is enforced.

Parameters:
  • checkpoint (ResolvedCheckpoint) – The resolved checkpoint (weights + metadata) to load.

  • providers (Sequence[CpuProvider | CudaProvider | TensorRTProvider | CoreMLProvider] | None) – Ordered execution providers to try. None lets the policy pick a host-appropriate chain from the checkpoint metadata.

  • session_options (SessionOptionsConfig | None) – Optional ONNX Runtime session options.

  • policy (ProviderPolicy | None) – Selection policy used when providers is None. Defaults to DefaultProviderPolicy.

  • checkpoint

  • providers

  • session_options

  • policy

Returns:

A backend wrapping the newly built session.

Return type:

Self

property metadata: CheckpointMetadata

Metadata describing the checkpoint this backend executes.

run(inputs: Mapping[str, ndarray]) Mapping[str, ndarray][source]

Execute the ONNX model on a batch of named input tensors.

Parameters:
  • inputs (Mapping[str, ndarray]) – Named input tensors. Keys must match metadata.input_names.

  • inputs

Returns:

Named output tensors keyed by the model’s output names.

Raises:

RuntimeError – If the backend has been closed.

Return type:

Mapping[str, ndarray]

close() None[source]

Release the underlying ONNX Runtime session.

ONNX Runtime frees native resources on garbage collection, so dropping the reference is the supported way to release them.

Return type:

None