onnx

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 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

func NormAxis added in v0.3.0

func NormAxis(a int64, r int, inclusive bool) (int, error)

NormAxis normalises an axis attribute for rank r (inclusive allows r).

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.

func (*Node) AttrFloat added in v0.3.0

func (n *Node) AttrFloat(name string, def float32) float32

AttrFloat is a float attribute or def.

func (*Node) AttrInt added in v0.3.0

func (n *Node) AttrInt(name string, def int64) int64

AttrInt is an int attribute or def.

func (*Node) AttrInts added in v0.3.0

func (n *Node) AttrInts(name string) []int64

AttrInts is an ints attribute or nil.

type Plan added in v0.3.0

type Plan struct {
	Opset  int64
	Steps  []Step
	Shapes map[string][]int64
	Consts map[string]*Tensor
}

Plan is a graph laid out for given input shapes.

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) Plan added in v0.3.0

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

Plan lays the graph out for the given inputs (by name). Only the shapes of float inputs matter (the dry run uses zeros); int64 inputs (shapes, axes) are used by value and count as constants, so a plan holds for inputs of the same float shapes and the same int64 values.

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 Step added in v0.3.0

type Step struct {
	Node      *Node
	InShapes  [][]int64 // nil for an omitted optional input
	OutShapes [][]int64
	// Const says the outputs do not depend on the graph inputs; they are
	// in Plan.Consts.
	Const bool
}

Step is one node of a plan.

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).

type Window added in v0.3.0

type Window struct {
	In, Kernel, Stride, Dil, PadB, PadE, Out []int64
}

Window is the geometry of a convolution or pooling over the spatial axes: sizes, kernel, strides, dilations, padding at both ends, output.

func NodeWindow added in v0.3.0

func NodeWindow(n *Node, x, w []int64) (*Window, error)

NodeWindow resolves a Conv, MaxPool or AveragePool node's window for an input of shape x (N, C, spatial...) and, for Conv, weights of shape w.

Jump to

Keyboard shortcuts

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