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 ¶
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 ¶
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).
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 ¶
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 DataType ¶
type DataType int32
DataType is TensorProto.DataType.
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.
type Node ¶
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 ¶
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 ¶
NewSession checks a decoded model (see Load).
func (*Session) Plan ¶ added in v0.3.0
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.
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 ¶
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 ¶
DecodeTensor parses an encoded TensorProto (the input_N.pb / output_N.pb files of the ONNX test data) within lim.
type UnsupportedError ¶
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 ¶
ValueInfo is a graph input or output: its name, element type and dimensions (-1 for a symbolic or unknown one).