nn

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: 5 Imported by: 0

Documentation

Overview

Package nn is the host side of wasi-nn for go-wasmcontainers: the preview 1 (witx) interface, import module "wasi_ephemeral_nn" (github.com/WebAssembly/wasi-nn, wasi-nn.witx), served by the engine's WASI host as a host module, natively and in the browser.

A graph is loaded by a Backend chosen by encoding, as Wasmtime does; the execution target is checked, not passed through: "gpu" runs on a GPU backend or fails with unsupported_operation, never quietly on the CPU, and needs the container's "gpu" capability (run --gpu).

Everything a guest passes is bounded: model bytes, graphs and contexts per process, tensor dimensions and data.

Index

Constants

View Source
const (
	TypeF16 = 0
	TypeF32 = 1
	TypeF64 = 2
	TypeU8  = 3
	TypeI32 = 4
	TypeI64 = 5
)

tensor_type values.

Variables

View Source
var DefaultLimits = Limits{ModelBytes: 64 << 20, Graphs: 8, Contexts: 32, Dims: 8, TensorData: 64 << 20}

DefaultLimits suit small models.

Functions

This section is empty.

Types

type AsyncBackend added in v0.3.0

type AsyncBackend interface {
	Async() bool
}

AsyncBackend is a backend whose calls wait on something outside the Go program (WebGPU promises in the browser). Its load and compute run off the host call, which returns a Wait, so the engine never blocks.

type Backend

type Backend interface {
	// Encoding is the graph encoding it reads.
	Encoding() Encoding
	// Targets reports whether it runs on target.
	Runs(target Target) bool
	// Load builds a graph from the guest's builder buffers.
	Load(builders [][]byte, target Target) (Graph, Errno)
}

Backend loads graphs of one encoding.

type CPUBackend

type CPUBackend struct {
	Limits onnx.Limits
}

CPUBackend runs ONNX graphs with package onnx, in pure Go. It serves the "cpu" target only.

func (CPUBackend) Encoding

func (CPUBackend) Encoding() Encoding

Encoding implements Backend.

func (CPUBackend) Load

func (b CPUBackend) Load(builders [][]byte, _ Target) (Graph, Errno)

Load implements Backend: one builder, the encoded model.

func (CPUBackend) Runs

func (CPUBackend) Runs(t Target) bool

Runs implements Backend.

type Context

type Context interface {
	SetInput(index uint32, t *onnx.Tensor) Errno
	Compute() Errno
	Output(index uint32) (*onnx.Tensor, Errno)
}

Context is one inference's inputs and outputs.

type Encoding

type Encoding = uint32

Encoding is graph_encoding.

const (
	OpenVINO       Encoding = 0
	ONNX           Encoding = 1
	TensorFlow     Encoding = 2
	PyTorch        Encoding = 3
	TensorFlowLite Encoding = 4
	Autodetect     Encoding = 5
)

graph_encoding values.

type Errno

type Errno = uint32

Errno is wasi-nn's nn_errno (witx order).

const (
	Success              Errno = 0
	InvalidArgument      Errno = 1
	InvalidEncoding      Errno = 2
	MissingMemory        Errno = 3
	Busy                 Errno = 4
	RuntimeError         Errno = 5
	UnsupportedOperation Errno = 6
	TooLarge             Errno = 7
	NotFound             Errno = 8
)

nn_errno values.

type Graph

type Graph interface {
	NewContext() (Context, Errno)
}

Graph is a loaded model.

type Limits

type Limits struct {
	ModelBytes int // all builders of one load, together
	Graphs     int // graphs alive per process
	Contexts   int // contexts alive per process
	Dims       int // tensor rank
	TensorData int // bytes of one input tensor
}

Limits bound what one process may make the host hold.

type Module

type Module struct {
	Backends []Backend
	Limits   Limits
	// Named are the graphs load_by_name knows: name to its builders and
	// encoding. load_by_name of any other name is not_found.
	Named map[string]Named
}

Module is the wasi_ephemeral_nn host module.

func NewModule

func NewModule() *Module

NewModule returns the module with the pure-Go CPU backend.

func (*Module) Funcs

func (*Module) Funcs() []wasi.HostFunc

Funcs implements wasi.HostModule (wasi-nn.witx, as the wasi-nn crate's generated bindings lower it: every argument an i32, nn_errno returned).

func (*Module) Module

func (*Module) Module() string

Module implements wasi.HostModule.

func (*Module) Open

func (m *Module) Open(p *wasi.Proc) wasi.Instance

Open implements wasi.HostModule.

type Named

type Named struct {
	Encoding Encoding
	Builders [][]byte
}

Named is a graph registered for load_by_name.

type Target

type Target = uint32

Target is execution_target.

const (
	CPU Target = 0
	GPU Target = 1
	TPU Target = 2
)

execution_target values.

Directories

Path Synopsis
Package webgpu is a wasi-nn backend that runs ONNX graphs on the browser's GPU through WebGPU (WGSL compute kernels), for the "gpu" execution target.
Package webgpu is a wasi-nn backend that runs ONNX graphs on the browser's GPU through WebGPU (WGSL compute kernels), for the "gpu" execution target.

Jump to

Keyboard shortcuts

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