ortapi

package
v0.0.0-...-2fc3ca7 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package ortapi is a minimal, purego-based (no cgo) binding to the subset of the ONNX Runtime C API needed to load a model and run inference. Field order below mirrors the real OrtApi struct from onnxruntime_c_api.h for ORT_API_VERSION 23 (verified against https://github.com/microsoft/onnxruntime/blob/v1.23.0/include/onnxruntime/core/session/onnxruntime_c_api.h); getting this order wrong is a silent crash, not a compile error, so do not reorder or insert fields without re-checking against that header.

Index

Constants

View Source
const (
	AllocatorTypeDevice OrtAllocatorType = 0
	MemTypeCPU          OrtMemType       = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIBase

type APIBase struct {
	GetAPI           uintptr // const OrtApi*(*)(uint32_t version)
	GetVersionString uintptr // const char*(*)()
}

APIBase mirrors struct OrtApiBase: the two-function entry point returned by the exported OrtGetApiBase symbol.

type Funcs

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

Funcs holds cached, typed function pointers for the subset of the ONNX Runtime C API this package needs. Obtained once via Load and reused for the lifetime of a loaded library.

func Load

func Load(libPath string) (*Funcs, error)

Load loads the ONNX Runtime shared library at libPath (no cgo, no C compiler - dlopen on Linux/macOS, LoadLibrary on Windows, see loadLibrary's per-platform implementations) and resolves the C API function pointers for apiVersion.

type ONNXTensorElementDataType

type ONNXTensorElementDataType int32

ONNXTensorElementDataType mirrors the C enum, values needed by this package.

const (
	TensorElementDataTypeFloat ONNXTensorElementDataType = 1
	TensorElementDataTypeInt64 ONNXTensorElementDataType = 7
)

type OrtAllocator

type OrtAllocator uintptr

Opaque native handles. Each is a raw pointer value returned by the C API; Go never dereferences them directly, only passes them back into API calls.

type OrtAllocatorType

type OrtAllocatorType int32

OrtAllocatorType / OrtMemType mirror the C enums; these are the values CreateCpuMemoryInfo needs (device allocator, CPU memory).

type OrtEnv

type OrtEnv uintptr

Opaque native handles. Each is a raw pointer value returned by the C API; Go never dereferences them directly, only passes them back into API calls.

type OrtErrorCode

type OrtErrorCode int32

OrtErrorCode mirrors the C enum OrtErrorCode.

type OrtLoggingLevel

type OrtLoggingLevel int32

OrtLoggingLevel mirrors the C enum OrtLoggingLevel.

const (
	LoggingLevelVerbose OrtLoggingLevel = 0
	LoggingLevelInfo    OrtLoggingLevel = 1
	LoggingLevelWarning OrtLoggingLevel = 2
	LoggingLevelError   OrtLoggingLevel = 3
	LoggingLevelFatal   OrtLoggingLevel = 4
)

type OrtMemType

type OrtMemType int32

OrtAllocatorType / OrtMemType mirror the C enums; these are the values CreateCpuMemoryInfo needs (device allocator, CPU memory).

type OrtMemoryInfo

type OrtMemoryInfo uintptr

Opaque native handles. Each is a raw pointer value returned by the C API; Go never dereferences them directly, only passes them back into API calls.

type OrtSession

type OrtSession uintptr

Opaque native handles. Each is a raw pointer value returned by the C API; Go never dereferences them directly, only passes them back into API calls.

type OrtSessionOptions

type OrtSessionOptions uintptr

Opaque native handles. Each is a raw pointer value returned by the C API; Go never dereferences them directly, only passes them back into API calls.

type OrtStatus

type OrtStatus uintptr

Opaque native handles. Each is a raw pointer value returned by the C API; Go never dereferences them directly, only passes them back into API calls.

type OrtTensorTypeAndShapeInfo

type OrtTensorTypeAndShapeInfo uintptr

Opaque native handles. Each is a raw pointer value returned by the C API; Go never dereferences them directly, only passes them back into API calls.

type OrtValue

type OrtValue uintptr

Opaque native handles. Each is a raw pointer value returned by the C API; Go never dereferences them directly, only passes them back into API calls.

type Runtime

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

Runtime owns a loaded ONNX Runtime library, one OrtEnv, and the default CPU allocator/memory-info needed to create tensors and sessions against it. Create one Runtime per loaded library; multiple Sessions may share it.

func NewRuntime

func NewRuntime(libPath, logID string) (*Runtime, error)

NewRuntime loads the ONNX Runtime shared library at libPath and creates an environment + default CPU allocator/memory-info ready for session creation.

func (*Runtime) Close

func (r *Runtime) Close()

Close releases the environment and CPU memory info. Sessions and Tensors created from this Runtime must be closed first - Close does not cascade.

func (*Runtime) NewFloat32Tensor

func (r *Runtime) NewFloat32Tensor(data []float32, shape []int64) (*Tensor, error)

NewFloat32Tensor creates a tensor from data using CPU memory, wrapping the slice without copying - data must stay alive and unmodified for the lifetime of the returned Tensor (and of any Run call it's passed to).

func (*Runtime) NewSession

func (r *Runtime) NewSession(modelPath string, intraOpNumThreads int) (*Session, error)

NewSession loads the ONNX model at modelPath into a new inference session. intraOpNumThreads <= 0 leaves ONNX Runtime's default thread count in place.

type Session

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

Session is a loaded model bound to one Runtime, ready to run inference.

func (*Session) Close

func (s *Session) Close()

Close releases the session. Safe to call multiple times.

func (*Session) InputNames

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

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

func (*Session) OutputNames

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

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

func (*Session) Run

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

Run executes the model. inputs must have one entry per name in InputNames. The returned map has one entry per name in OutputNames; callers must Close every returned Tensor.

type Tensor

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

Tensor wraps an OrtValue holding a tensor - either one created from Go data via NewFloat32Tensor (an input) or one returned by Session.Run (an output).

func (*Tensor) Close

func (t *Tensor) Close()

Close releases the underlying OrtValue. Safe to call multiple times.

func (*Tensor) Float32Data

func (t *Tensor) Float32Data() ([]float32, error)

Float32Data copies out the tensor's underlying data as []float32. The caller is responsible for knowing the tensor's element type matches (this package only produces/consumes float32 tensors - PP-OCRv5's det/rec models are float32 end to end).

func (*Tensor) Shape

func (t *Tensor) Shape() ([]int64, error)

Shape returns the tensor's dimensions, e.g. [1, 3, 960, 960].

Jump to

Keyboard shortcuts

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