onnxrt

package
v0.0.0-...-230ac96 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package onnxrt calls ONNX Runtime from Go without cgo.

It binds the C API through ebitengine/purego, so agentmon keeps pure-Go builds and cross-compilation while running a real inference engine. The cost is a shared library the operator must install; there is no static option, and the package refuses to guess where it is.

The binding is deliberately narrow: enough to load a model, run it once and read the result. Every function it calls is listed in the generator's `wanted` set, so the API surface in use is enumerable rather than whatever happened to be reachable.

Index

Constants

View Source
const LibraryEnv = "AGENTMON_ONNXRUNTIME_LIB"

LibraryEnv names the environment variable holding the path to libonnxruntime. It is checked before any default location.

Variables

View Source
var ErrLibraryNotFound = errors.New("onnxrt: no ONNX Runtime library found")

ErrLibraryNotFound is returned when no ONNX Runtime library could be located. Callers use it to distinguish "not installed" from "failed to load", because the first is an operator action and the second is a bug.

Functions

func FindLibrary

func FindLibrary() (string, error)

FindLibrary returns the path that Open would use, or ErrLibraryNotFound.

Types

type Library

type Library struct {
	// contains filtered or unexported fields
}

Library is a loaded ONNX Runtime.

func Open

func Open(path string) (*Library, error)

Open loads ONNX Runtime. An empty path uses FindLibrary.

func (*Library) Close

func (l *Library) Close()

Close releases the runtime environment. Sessions must be closed first.

func (*Library) NewSession

func (l *Library) NewSession(model []byte, opts SessionOptions) (*Session, error)

NewSession loads a model from memory.

Use it only for a self-contained model. A graph whose weights live in separate .onnx_data files cannot be loaded this way: ONNX Runtime resolves those paths relative to the model's directory, which it does not know when handed bytes. Privacy Filter is such a model -- its graph is 160KB and its weights are 917MB alongside -- so that path uses NewSessionFromPath.

func (*Library) NewSessionFromPath

func (l *Library) NewSessionFromPath(path string, opts SessionOptions) (*Session, error)

NewSessionFromPath loads a model by path.

This is the only way to load a model whose weights are in external .onnx_data files, because ONNX Runtime resolves those relative to the model's own directory and only learns that directory from the path. It also avoids reading a gigabyte of weights into Go's heap just to hand them straight back.

The trade is that the file is read by ONNX Runtime, not by the caller, so a caller that verified a digest is trusting the file not to change in between. The model cache writes atomically and treats its directory as immutable once populated, which is what makes that safe.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is a loaded model.

func (*Session) Close

func (s *Session) Close()

Close releases the session.

func (*Session) InputNames

func (s *Session) InputNames() ([]string, error)

InputNames returns the model's input names, in order.

func (*Session) OutputNames

func (s *Session) OutputNames() ([]string, error)

OutputNames returns the model's output names, in order.

func (*Session) Run

func (s *Session) Run(inputs map[string]*Tensor, outputNames []string) ([]*Tensor, error)

Run executes the model.

Every OrtValue created here is released before returning, including on the error paths, because a failed inference is exactly when a daemon is most likely to retry and least able to afford a leak per attempt.

type SessionOptions

type SessionOptions struct {
	// IntraOpThreads bounds the threads one operator may use. Zero leaves
	// ONNX Runtime's default, which is one thread per core -- too many for a
	// daemon that must stay responsive while inspecting one request.
	IntraOpThreads int

	// CoreML asks for Apple's CoreML execution provider on macOS. It is a
	// request, not a guarantee: ONNX Runtime partitions the graph and runs
	// on CPU whatever CoreML cannot take, so an unsupported model simply
	// runs as before.
	CoreML bool
}

SessionOptions configures a session.

type Tensor

type Tensor struct {
	Shape  []int64
	Floats []float32
	Int64s []int64
}

Tensor is a dense tensor. Exactly one of Floats and Int64s is populated.

func NewFloatTensor

func NewFloatTensor(shape []int64, data []float32) *Tensor

NewFloatTensor builds a float32 tensor.

func NewInt64Tensor

func NewInt64Tensor(shape []int64, data []int64) *Tensor

NewInt64Tensor builds an int64 tensor, which is what token IDs are.

Directories

Path Synopsis
Command gen-ort-api extracts the OrtApi struct member order from onnxruntime_c_api.h and writes it as Go constants.
Command gen-ort-api extracts the OrtApi struct member order from onnxruntime_c_api.h and writes it as Go constants.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL