Documentation
¶
Overview ¶
Package tract runs ONNX and NNEF neural networks in-process with Sonos' tract (https://github.com/sonos/tract), a self-contained CPU inference engine written in Rust.
The tract C API is linked statically from the prebuilt libraries this module ships for linux/amd64, linux/arm64, darwin/arm64 and windows/amd64 (built for MinGW, which cgo uses there), so a program using it needs no shared library at run time and no Rust toolchain to build. Other platforms, and builds with cgo disabled, compile against a stub whose every call returns ErrUnsupported.
A model goes through the same stages as in tract's other APIs:
onnx, _ := tract.NewOnnx()
defer onnx.Close()
im, _ := onnx.Load("model.onnx") // *InferenceModel
_ = im.SetInputFactSpec(0, "1,S,i64") // optional: types and shapes
m, _ := im.IntoModel() // *Model (consumes im)
r, _ := m.IntoRunnable() // *Runnable (consumes m)
defer r.Close()
out, _ := r.Run(input) // []*Tensor
Every object holds memory owned by tract: Close it when done. A finalizer releases objects that are dropped without Close, but at an unpredictable time. Methods whose name starts with Into, and Runtime.Prepare, consume their receiver or argument whether they succeed or not.
A Runnable may be run from several goroutines at once: every Run uses a state of its own. Other objects must not be used concurrently with their Close.
A Rust panic inside tract aborts the process: the C API does not turn panics into errors.
Example ¶
package main
import (
"fmt"
"log"
"go.riptides.io/tract"
)
func main() {
onnx, err := tract.NewOnnx()
if err != nil {
log.Fatal(err)
}
defer onnx.Close()
im, err := onnx.Load("testdata/linear.onnx")
if err != nil {
log.Fatal(err)
}
// Optional: pin the input's type and shape ("N" stays symbolic otherwise).
if err := im.SetInputFactSpec(0, "1,3,f32"); err != nil {
log.Fatal(err)
}
m, err := im.IntoModel()
if err != nil {
log.Fatal(err)
}
r, err := m.IntoRunnable()
if err != nil {
log.Fatal(err)
}
defer r.Close()
x, err := tract.NewTensor([]int{1, 3}, []float32{1, 1, 1})
if err != nil {
log.Fatal(err)
}
defer x.Close()
out, err := r.Run(x)
if err != nil {
log.Fatal(err)
}
defer out[0].Close()
y, err := tract.TensorData[float32](out[0])
if err != nil {
log.Fatal(err)
}
fmt.Println(y)
}
Output: [9.5 11.5]
Index ¶
- Variables
- func TensorData[T Element](t *Tensor) ([]T, error)
- func Version() string
- type DatumType
- type Dim
- type Element
- type Error
- type Fact
- type InferenceFact
- type InferenceModel
- func (m *InferenceModel) Analyse() error
- func (m *InferenceModel) Close() error
- func (m *InferenceModel) InputCount() (int, error)
- func (m *InferenceModel) InputFact(i int) (*InferenceFact, error)
- func (m *InferenceModel) InputName(i int) (string, error)
- func (m *InferenceModel) IntoModel() (*Model, error)
- func (m *InferenceModel) OutputCount() (int, error)
- func (m *InferenceModel) OutputFact(i int) (*InferenceFact, error)
- func (m *InferenceModel) OutputName(i int) (string, error)
- func (m *InferenceModel) ParseFact(spec string) (*InferenceFact, error)
- func (m *InferenceModel) SetInputFact(i int, f *InferenceFact) error
- func (m *InferenceModel) SetInputFactSpec(i int, spec string) error
- func (m *InferenceModel) SetOutputFact(i int, f *InferenceFact) error
- func (m *InferenceModel) SetOutputFactSpec(i int, spec string) error
- type Model
- func (m *Model) Close() error
- func (m *Model) InputCount() (int, error)
- func (m *Model) InputFact(i int) (*Fact, error)
- func (m *Model) InputName(i int) (string, error)
- func (m *Model) IntoRunnable() (*Runnable, error)
- func (m *Model) OutputCount() (int, error)
- func (m *Model) OutputFact(i int) (*Fact, error)
- func (m *Model) OutputName(i int) (string, error)
- func (m *Model) ParseFact(spec string) (*Fact, error)
- func (m *Model) Transform(spec string) error
- type Nnef
- type Onnx
- type Runnable
- func (r *Runnable) Close() error
- func (r *Runnable) InputCount() (int, error)
- func (r *Runnable) InputFact(i int) (*Fact, error)
- func (r *Runnable) OutputCount() (int, error)
- func (r *Runnable) OutputFact(i int) (*Fact, error)
- func (r *Runnable) Run(inputs ...*Tensor) ([]*Tensor, error)
- func (r *Runnable) SpawnState() (*State, error)
- type Runtime
- type State
- type Tensor
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("tract: object is closed or consumed")
ErrClosed is returned when an object is used after Close, or after a call that consumed it.
var ErrUnsupported = errors.New("tract: unsupported platform or cgo disabled")
ErrUnsupported is returned by every call on a platform without a prebuilt tract library, or when cgo is disabled.
Functions ¶
func TensorData ¶
TensorData is a copy of t's elements, which must be of type T.
Types ¶
type DatumType ¶
type DatumType int
DatumType is the element type of a tensor.
const ( Bool DatumType = 1 U8 DatumType = 17 U16 DatumType = 18 U32 DatumType = 20 U64 DatumType = 24 I8 DatumType = 33 I16 DatumType = 34 I32 DatumType = 36 I64 DatumType = 40 F16 DatumType = 50 F32 DatumType = 52 F64 DatumType = 56 ComplexI16 DatumType = 66 ComplexI32 DatumType = 68 ComplexI64 DatumType = 72 ComplexF16 DatumType = 82 ComplexF32 DatumType = 84 ComplexF64 DatumType = 88 )
The datum types of tract's C API.
type Dim ¶
type Dim struct {
// contains filtered or unexported fields
}
Dim is a tensor dimension: a number or an expression over symbols.
type Element ¶
type Element interface {
bool | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
Element is a Go type with a tract datum type of the same memory layout.
type Error ¶
type Error struct {
// Op is the tract C function that failed.
Op string
// Msg is tract's description of the error.
Msg string
}
Error is an error reported by tract.
type Fact ¶
type Fact struct {
// contains filtered or unexported fields
}
Fact is the type and shape of a typed model's input or output.
type InferenceFact ¶
type InferenceFact struct {
// contains filtered or unexported fields
}
InferenceFact is what is known of an InferenceModel input's or output's type and shape.
func (*InferenceFact) String ¶
func (f *InferenceFact) String() string
String is the fact as a spec.
type InferenceModel ¶
type InferenceModel struct {
// contains filtered or unexported fields
}
InferenceModel is a loaded ONNX model whose types and shapes may still be partly unknown. Set its input facts if needed, then call IntoModel.
func (*InferenceModel) Analyse ¶
func (m *InferenceModel) Analyse() error
Analyse runs type and shape inference on the model.
func (*InferenceModel) InputCount ¶
func (m *InferenceModel) InputCount() (int, error)
InputCount is the number of inputs.
func (*InferenceModel) InputFact ¶
func (m *InferenceModel) InputFact(i int) (*InferenceFact, error)
InputFact is what is known of input i's type and shape.
func (*InferenceModel) InputName ¶
func (m *InferenceModel) InputName(i int) (string, error)
InputName is the name of input i.
func (*InferenceModel) IntoModel ¶
func (m *InferenceModel) IntoModel() (*Model, error)
IntoModel turns the model into a typed Model. It consumes m, even on error.
func (*InferenceModel) OutputCount ¶
func (m *InferenceModel) OutputCount() (int, error)
OutputCount is the number of outputs.
func (*InferenceModel) OutputFact ¶
func (m *InferenceModel) OutputFact(i int) (*InferenceFact, error)
OutputFact is what is known of output i's type and shape.
func (*InferenceModel) OutputName ¶
func (m *InferenceModel) OutputName(i int) (string, error)
OutputName is the name of output i.
func (*InferenceModel) ParseFact ¶
func (m *InferenceModel) ParseFact(spec string) (*InferenceFact, error)
ParseFact parses a fact spec in the context of this model.
func (*InferenceModel) SetInputFact ¶
func (m *InferenceModel) SetInputFact(i int, f *InferenceFact) error
SetInputFact sets input i's type and shape. f stays owned by the caller.
func (*InferenceModel) SetInputFactSpec ¶
func (m *InferenceModel) SetInputFactSpec(i int, spec string) error
SetInputFactSpec sets input i's type and shape from a spec such as "1,S,i64" or "B,224,224,3,f32" (letters are symbolic dimensions).
func (*InferenceModel) SetOutputFact ¶
func (m *InferenceModel) SetOutputFact(i int, f *InferenceFact) error
SetOutputFact sets output i's type and shape. f stays owned by the caller.
func (*InferenceModel) SetOutputFactSpec ¶
func (m *InferenceModel) SetOutputFactSpec(i int, spec string) error
SetOutputFactSpec sets output i's type and shape from a spec.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is a typed model, ready to be transformed and made runnable.
func (*Model) InputCount ¶
InputCount is the number of inputs.
func (*Model) IntoRunnable ¶
IntoRunnable optimises the model for the default CPU runtime. It consumes m, even on error.
func (*Model) OutputCount ¶
OutputCount is the number of outputs.
func (*Model) OutputFact ¶
OutputFact is output i's type and shape.
func (*Model) OutputName ¶
OutputName is the name of output i.
type Nnef ¶
type Nnef struct {
// contains filtered or unexported fields
}
Nnef loads NNEF models.
type Onnx ¶
type Onnx struct {
// contains filtered or unexported fields
}
Onnx loads ONNX models.
func (*Onnx) Load ¶
func (o *Onnx) Load(path string) (*InferenceModel, error)
Load parses the .onnx file at path.
func (*Onnx) LoadBuffer ¶
func (o *Onnx) LoadBuffer(data []byte) (*InferenceModel, error)
LoadBuffer parses an ONNX model held in memory.
type Runnable ¶
type Runnable struct {
// contains filtered or unexported fields
}
Runnable is a model ready to run. Run is safe for concurrent use.
func (*Runnable) InputCount ¶
InputCount is the number of inputs.
func (*Runnable) OutputCount ¶
OutputCount is the number of outputs.
func (*Runnable) OutputFact ¶
OutputFact is output i's type and shape.
func (*Runnable) Run ¶
Run runs the model on one tensor per input, in order, and returns one tensor per output. The inputs stay owned by the caller; the caller owns and must Close the outputs.
func (*Runnable) SpawnState ¶
SpawnState creates a state for running a stateful model step by step.
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime prepares models for a backend.
func RuntimeForName ¶
RuntimeForName returns a runtime by name. "default" is the CPU runtime, which names itself "cpu".
type State ¶
type State struct {
// contains filtered or unexported fields
}
State runs a stateful model step by step. It is not safe for concurrent use.
type Tensor ¶
type Tensor struct {
// contains filtered or unexported fields
}
Tensor is an n-dimensional array owned by tract.
func NewTensorFromBytes ¶
NewTensorFromBytes copies raw little-endian element data into a tensor, for datum types without a Go equivalent such as F16.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
lib
|
|
|
darwin_arm64
Package darwin_arm64 holds the prebuilt static tract library for darwin/arm64.
|
Package darwin_arm64 holds the prebuilt static tract library for darwin/arm64. |
|
linux_amd64
Package linux_amd64 holds the prebuilt static tract library for linux/amd64.
|
Package linux_amd64 holds the prebuilt static tract library for linux/amd64. |
|
linux_arm64
Package linux_arm64 holds the prebuilt static tract library for linux/arm64.
|
Package linux_arm64 holds the prebuilt static tract library for linux/arm64. |
|
windows_amd64
Package windows_amd64 holds the prebuilt static tract library for windows/amd64.
|
Package windows_amd64 holds the prebuilt static tract library for windows/amd64. |