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 ¶
const LibraryEnv = "AGENTMON_ONNXRUNTIME_LIB"
LibraryEnv names the environment variable holding the path to libonnxruntime. It is checked before any default location.
Variables ¶
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 ¶
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 (*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) InputNames ¶
InputNames returns the model's input names, in order.
func (*Session) OutputNames ¶
OutputNames returns the model's output names, in order.
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 ¶
Tensor is a dense tensor. Exactly one of Floats and Int64s is populated.
func NewFloatTensor ¶
NewFloatTensor builds a float32 tensor.
func NewInt64Tensor ¶
NewInt64Tensor builds an int64 tensor, which is what token IDs are.