onnx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package onnx loads ONNX models (onnx/onnx.proto) and runs a bounded set of operators on the CPU, in pure Go.

A model is untrusted input: Decode bounds every size (Limits), refuses what it does not implement with an error naming it, and is fuzzed. Load further refuses a graph that uses an operator outside Supported, listing every such operator, so a container learns at load time, not half-way through an inference, that its model cannot run here.

Index

Constants

View Source
const MinOpset = 7

MinOpset is the oldest default-domain operator set Load accepts: before 7, Add and its kin broadcast through an attribute this package lacks.

Variables

View Source
var DefaultLimits = Limits{
	ModelBytes:  64 << 20,
	Nodes:       4096,
	Dims:        8,
	TensorElems: 16 << 20,
	TotalElems:  32 << 20,
	MACs:        4 << 30,
	Strings:     4096,
}

DefaultLimits suit small vision models (MNIST-sized to a few MB).

View Source
var Supported = map[string]string{
	"Add":         "7+ (multidirectional broadcasting)",
	"AveragePool": "7+ (ceil_mode from 10, dilations from 19)",
	"Concat":      "4+",
	"Constant":    "1+ (value attribute)",
	"Conv":        "1+",
	"Div":         "7+",
	"Dropout":     "1+ (inference: identity; no mask output)",
	"Flatten":     "1+",
	"Gemm":        "7+",
	"Identity":    "1+",
	"MatMul":      "1+",
	"MaxPool":     "1+ (no Indices output)",
	"Mul":         "7+",
	"Relu":        "6+",
	"Reshape":     "5+ (allowzero from 14)",
	"Softmax":     "1+ (2-D coercion before 13, one axis from 13)",
	"Sub":         "7+",
	"Transpose":   "1+",
}

Supported lists the operators (default domain) this package runs, with the operator-set versions their semantics follow.

Functions

This section is empty.

Types

type Attr

type Attr struct {
	Name   string
	Type   AttrType
	F      float32
	I      int64
	S      string
	T      *Tensor
	Floats []float32
	Ints   []int64
}

Attr is a node attribute.

type AttrType

type AttrType int32

AttrType is AttributeProto.AttributeType.

const (
	AttrFloat   AttrType = 1
	AttrInt     AttrType = 2
	AttrString  AttrType = 3
	AttrTensor  AttrType = 4
	AttrGraph   AttrType = 5
	AttrFloats  AttrType = 6
	AttrInts    AttrType = 7
	AttrStrings AttrType = 8
)

Attribute types this package reads.

type DataType

type DataType int32

DataType is TensorProto.DataType.

const (
	Float  DataType = 1
	Uint8  DataType = 2
	Int8   DataType = 3
	Int32  DataType = 6
	Int64  DataType = 7
	Double DataType = 11
)

The data types this package reads (onnx.proto TensorProto.DataType).

func (DataType) String

func (t DataType) String() string

type Graph

type Graph struct {
	Name         string
	Nodes        []*Node
	Initializers map[string]*Tensor
	Inputs       []ValueInfo
	Outputs      []ValueInfo
}

Graph is a model's main graph.

type Limits

type Limits struct {
	// ModelBytes bounds the encoded model.
	ModelBytes int
	// Nodes bounds the graph's nodes.
	Nodes int
	// Dims bounds a tensor's rank.
	Dims int
	// TensorElems bounds one tensor's elements (initializers, inputs and
	// every intermediate result).
	TensorElems int64
	// TotalElems bounds the sum of the initializers' elements.
	TotalElems int64
	// MACs bounds the multiply-accumulates of one inference.
	MACs int64
	// Strings bounds a name or attribute string, in bytes.
	Strings int
}

Limits bound what a model may make this package allocate or compute.

type Model

type Model struct {
	IRVersion int64
	// Opsets maps an operator-set domain ("" is the default ai.onnx) to
	// its version.
	Opsets   map[string]int64
	Producer string
	Graph    *Graph
}

Model is a decoded ModelProto.

func Decode

func Decode(b []byte, lim Limits) (*Model, error)

Decode parses an encoded ModelProto within lim. It does not check the operators (Load does).

type Node

type Node struct {
	Name, Op, Domain string
	Inputs, Outputs  []string
	Attrs            map[string]*Attr
}

Node is one operator application.

type Session

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

Session is a loaded model ready to run.

func Load

func Load(b []byte, lim Limits) (*Session, error)

Load decodes a model and checks that this package can run it: opset, operators, and that the nodes are in topological order with every input defined.

func NewSession

func NewSession(m *Model, lim Limits) (*Session, error)

NewSession checks a decoded model (see Load).

func (*Session) Inputs

func (s *Session) Inputs() []ValueInfo

Inputs are the graph inputs the caller provides, in order.

func (*Session) Opset

func (s *Session) Opset() int64

Opset is the model's default-domain operator-set version.

func (*Session) Outputs

func (s *Session) Outputs() []ValueInfo

Outputs are the graph outputs, in order.

func (*Session) Run

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

Run computes the graph outputs from the inputs (by name). Every intermediate tensor is bounded by Limits.TensorElems and the whole run by Limits.MACs.

type Tensor

type Tensor struct {
	Name string
	Dims []int64
	Type DataType
	F    []float32
	I    []int64
}

Tensor is a dense tensor in row-major order. Float tensors use F, integer ones (Int64, Int32) I; other types are converted on decoding (Double to Float; Uint8 and Int8 to Float, as their values).

func DecodeTensor

func DecodeTensor(b []byte, lim Limits) (*Tensor, error)

DecodeTensor parses an encoded TensorProto (the input_N.pb / output_N.pb files of the ONNX test data) within lim.

func NewFloat

func NewFloat(dims []int64, data []float32) (*Tensor, error)

NewFloat returns a float tensor of the given shape over data (not copied); its length must be the shape's product.

func (*Tensor) Len

func (t *Tensor) Len() int64

Len is the product of the dimensions (1 for a scalar).

type UnsupportedError

type UnsupportedError struct {
	Ops map[string]int // "domain:Op" or "Op" -> nodes
}

UnsupportedError lists every operator of a graph this package cannot run, with how many nodes use it.

func (*UnsupportedError) Error

func (e *UnsupportedError) Error() string

type ValueInfo

type ValueInfo struct {
	Name string
	Type DataType
	Dims []int64
}

ValueInfo is a graph input or output: its name, element type and dimensions (-1 for a symbolic or unknown one).

Jump to

Keyboard shortcuts

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