Documentation
¶
Overview ¶
Package ane provides high-level access to the Apple Neural Engine.
Open a Runtime, compile MIL programs into Kernels, and evaluate them on the ANE hardware. IOSurface buffers are managed automatically.
rt, err := ane.Open()
if err != nil {
log.Fatal(err)
}
defer rt.Close()
k, err := rt.Compile(ane.CompileOptions{
MILText: milText,
WeightBlob: blob,
ModelType: ane.ModelTypeMIL,
})
if err != nil {
log.Fatal(err)
}
defer k.Close()
k.WriteInputF32(0, input)
if err := k.Eval(); err != nil {
log.Fatal(err)
}
k.ReadOutputF32(0, output)
Index ¶
- Variables
- func FP16ToFloat32(h uint16) float32
- func Float32ToFP16(f float32) uint16
- func RowStrideFor(width, elemSize int) int
- type ANEError
- type CompileOptions
- type DeviceInfo
- type Diagnostics
- type EvalStats
- type Kernel
- func (k *Kernel) Close() error
- func (k *Kernel) Diagnostics() Diagnostics
- func (k *Kernel) Eval() error
- func (k *Kernel) EvalAsync() <-chan error
- func (k *Kernel) EvalAsyncWithCallback(fn func(error))
- func (k *Kernel) EvalBidirectional(waitPort uint32, waitValue uint64, signalPort uint32, signalValue uint64, ...) error
- func (k *Kernel) EvalWithSignalEvent(signalPort uint32, signalValue uint64, cfg SharedEventEvalOptions) error
- func (k *Kernel) EvalWithStats() (EvalStats, error)
- func (k *Kernel) InputAllocSize(i int) int
- func (k *Kernel) InputChannels(i int) int
- func (k *Kernel) InputLayout(i int) TensorLayout
- func (k *Kernel) InputSurface(i int) uintptr
- func (k *Kernel) InputSurfaces() []uintptr
- func (k *Kernel) MetalInputBuffer(d *MetalDevice, i int) (any, error)
- func (k *Kernel) MetalOutputBuffer(d *MetalDevice, i int) (any, error)
- func (k *Kernel) NumInputs() int
- func (k *Kernel) NumOutputs() int
- func (k *Kernel) OutputAllocSize(i int) int
- func (k *Kernel) OutputChannels(i int) int
- func (k *Kernel) OutputLayout(i int) TensorLayout
- func (k *Kernel) OutputSurface(i int) uintptr
- func (k *Kernel) OutputSurfaces() []uintptr
- func (k *Kernel) ReadOutput(i int, data []byte) error
- func (k *Kernel) ReadOutputF32(i int, data []float32) error
- func (k *Kernel) ReadOutputFP16(i int, data []float32) error
- func (k *Kernel) ReadOutputFP16Channels(i, channel int, data []float32) error
- func (k *Kernel) Spatial(i int) int
- func (k *Kernel) WriteInput(i int, data []byte) error
- func (k *Kernel) WriteInputF32(i int, data []float32) error
- func (k *Kernel) WriteInputFP16(i int, data []float32) error
- func (k *Kernel) WriteInputFP16Channels(i, channel int, data []float32) error
- type MetalDevice
- type ModelType
- type Pipeline
- func (p *Pipeline) ANEEvent() *SharedEvent
- func (p *Pipeline) ANEToMetal(_ *Kernel) error
- func (p *Pipeline) Bidirectional(_ *Kernel) error
- func (p *Pipeline) Close() error
- func (p *Pipeline) Counter() uint64
- func (p *Pipeline) Metal() *MetalDevice
- func (p *Pipeline) MetalEvent() any
- func (p *Pipeline) WaitOnANE(_ *Kernel) error
- type PooledRequest
- type RequestPool
- type Runtime
- type SharedEvent
- type SharedEventEvalOptions
- type StateHandle
- type TensorLayout
- type WeightFile
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoANE = errors.New("ane: no ANE hardware available") ErrCompileBudgetExhausted = errors.New("ane: compile budget exhausted") ErrMapFailed = errors.New("ane: IOSurface mapping failed") ErrUnsupportedSelector = errors.New("ane: unsupported selector") ErrModelLoad = errors.New("ane: model load failed") ErrEval = errors.New("ane: evaluation failed") ErrUnsupportedLayout = errors.New("ane: unsupported tensor layout") )
Functions ¶
func FP16ToFloat32 ¶
FP16ToFloat32 converts an IEEE 754 half-precision value to float32.
func Float32ToFP16 ¶
Float32ToFP16 converts a float32 to IEEE 754 half-precision.
func RowStrideFor ¶
RowStrideFor computes the minimum 64-byte-aligned row stride.
Types ¶
type ANEError ¶
type ANEError struct {
Op string // operation that failed
Class string // error domain or class
Code int // error code
Err error // underlying error
}
ANEError wraps an error from the ANE subsystem with context.
type CompileOptions ¶
type CompileOptions struct {
ModelType ModelType
// For ModelTypeMIL:
MILText []byte // MIL program text
// Legacy single-weight MIL fields.
WeightBlob []byte // weight binary blob (may be nil)
WeightPath string // model path key for weight dict (default: "@model_path/weights/weight.bin")
// WeightFiles allows MIL graphs with multiple named BLOBFILE inputs.
// If both WeightBlob and WeightFiles are set, they are merged.
WeightFiles []WeightFile
// For ModelTypePackage:
PackagePath string // path to .mlmodelc directory
ModelKey string // key for model dictionary (default: "s")
// QoS selects the ANE quality-of-service scheduling class.
// Zero uses the default value of 21. Higher values may receive
// priority on shared hardware.
QoS uint32
// PerfStatsMask enables hardware performance counters during evaluation.
// Zero disables stats collection. When non-zero, EvalWithStats returns
// timing data in EvalStats. Bit meanings are hardware-specific.
PerfStatsMask uint32
}
CompileOptions configures model compilation.
type DeviceInfo ¶
type DeviceInfo struct {
HasANE bool
NumCores uint32
Architecture string
Product string
BuildVersion string
IsVM bool
}
DeviceInfo describes the ANE hardware present on the system.
func Probe ¶
func Probe() (DeviceInfo, error)
Probe returns device information about the ANE. On non-Darwin platforms, it always reports no ANE.
type Diagnostics ¶
type Diagnostics struct {
ModelQueueDepth int
ModelQueueDepthKnown bool
ProgramClass string
ProgramClassKnown bool
}
Diagnostics contains best-effort runtime diagnostic information about a kernel.
type EvalStats ¶
type EvalStats struct {
HWExecutionNS uint64 // hardware execution time in nanoseconds
}
EvalStats contains hardware performance statistics from an evaluation.
type Kernel ¶
type Kernel struct{}
Kernel represents a compiled and loaded model ready for evaluation.
func (*Kernel) Diagnostics ¶
func (k *Kernel) Diagnostics() Diagnostics
func (*Kernel) EvalAsyncWithCallback ¶
func (*Kernel) EvalBidirectional ¶
func (*Kernel) EvalWithSignalEvent ¶
func (k *Kernel) EvalWithSignalEvent(signalPort uint32, signalValue uint64, cfg SharedEventEvalOptions) error
func (*Kernel) EvalWithStats ¶
func (*Kernel) InputAllocSize ¶
func (*Kernel) InputChannels ¶
func (*Kernel) InputLayout ¶
func (k *Kernel) InputLayout(i int) TensorLayout
func (*Kernel) InputSurface ¶
func (*Kernel) InputSurfaces ¶
func (*Kernel) MetalInputBuffer ¶
func (k *Kernel) MetalInputBuffer(d *MetalDevice, i int) (any, error)
func (*Kernel) MetalOutputBuffer ¶
func (k *Kernel) MetalOutputBuffer(d *MetalDevice, i int) (any, error)
func (*Kernel) NumOutputs ¶
func (*Kernel) OutputAllocSize ¶
func (*Kernel) OutputChannels ¶
func (*Kernel) OutputLayout ¶
func (k *Kernel) OutputLayout(i int) TensorLayout
func (*Kernel) OutputSurface ¶
func (*Kernel) OutputSurfaces ¶
func (*Kernel) ReadOutputFP16Channels ¶
type MetalDevice ¶
type MetalDevice struct{}
MetalDevice wraps a Metal GPU device for zero-copy interop with ANE.
func OpenMetal ¶
func OpenMetal() (*MetalDevice, error)
func (*MetalDevice) Close ¶
func (d *MetalDevice) Close() error
func (*MetalDevice) MetalSharedEvent ¶
func (d *MetalDevice) MetalSharedEvent(ev *SharedEvent) (any, error)
func (*MetalDevice) NewMetalSharedEvent ¶
func (d *MetalDevice) NewMetalSharedEvent() (any, *SharedEvent, error)
type Pipeline ¶
type Pipeline struct{}
Pipeline manages SharedEvent synchronization between ANE and Metal.
func NewPipeline ¶
func NewPipeline(_ *MetalDevice) (*Pipeline, error)
func (*Pipeline) ANEEvent ¶
func (p *Pipeline) ANEEvent() *SharedEvent
func (*Pipeline) ANEToMetal ¶
func (*Pipeline) Bidirectional ¶
func (*Pipeline) Metal ¶
func (p *Pipeline) Metal() *MetalDevice
func (*Pipeline) MetalEvent ¶
type PooledRequest ¶
type PooledRequest struct{}
PooledRequest is a request checked out from a pool.
func (*PooledRequest) Eval ¶
func (pr *PooledRequest) Eval() error
func (*PooledRequest) Release ¶
func (pr *PooledRequest) Release()
type RequestPool ¶
type RequestPool struct{}
RequestPool pre-allocates a ring of ANE requests for pipelined evaluation.
func NewRequestPool ¶
func NewRequestPool(k *Kernel, depth int) (*RequestPool, error)
func (*RequestPool) Acquire ¶
func (p *RequestPool) Acquire() *PooledRequest
func (*RequestPool) Close ¶
func (p *RequestPool) Close() error
type Runtime ¶
type Runtime struct{}
Runtime manages a connection to the ANE hardware.
func Open ¶
Open creates a new Runtime for ANE inference. On non-Darwin platforms, it always returns ErrNoANE.
func (*Runtime) Compile ¶
func (rt *Runtime) Compile(opts CompileOptions) (*Kernel, error)
Compile compiles a model and returns a ready-to-evaluate Kernel.
func (*Runtime) CompileCount ¶
CompileCount returns the number of compilations performed.
func (*Runtime) Info ¶
func (rt *Runtime) Info() DeviceInfo
Info returns the device information for this runtime.
type SharedEvent ¶
type SharedEvent struct{}
SharedEvent wraps an IOSurfaceSharedEvent for ANE↔GPU/CPU synchronization.
func NewSharedEvent ¶
func NewSharedEvent() (*SharedEvent, error)
func SharedEventFromPort ¶
func SharedEventFromPort(port uint32) (*SharedEvent, error)
func (*SharedEvent) Close ¶
func (e *SharedEvent) Close() error
func (*SharedEvent) Port ¶
func (e *SharedEvent) Port() uint32
func (*SharedEvent) Signal ¶
func (e *SharedEvent) Signal(value uint64)
func (*SharedEvent) SignaledValue ¶
func (e *SharedEvent) SignaledValue() uint64
type SharedEventEvalOptions ¶
type SharedEventEvalOptions struct {
}
SharedEventEvalOptions configures shared event evaluation behavior.
type StateHandle ¶
type StateHandle struct{}
StateHandle manages KV cache state for stateful MIL models.
func NewStateHandle ¶
func NewStateHandle(_ *Kernel, _ int) *StateHandle
func (*StateHandle) Advance ¶
func (s *StateHandle) Advance(_ int)
func (*StateHandle) Close ¶
func (s *StateHandle) Close() error
func (*StateHandle) Kernel ¶
func (s *StateHandle) Kernel() *Kernel
func (*StateHandle) MaxSeq ¶
func (s *StateHandle) MaxSeq() int
func (*StateHandle) Position ¶
func (s *StateHandle) Position() int
func (*StateHandle) Remaining ¶
func (s *StateHandle) Remaining() int
func (*StateHandle) Reset ¶
func (s *StateHandle) Reset()
type TensorLayout ¶
type TensorLayout struct {
Channels int
Width int
Height int
ElemSize int
RowStride int
PlaneStride int
}
TensorLayout describes the compiled model's memory layout for a single tensor.
func (TensorLayout) AllocSize ¶
func (l TensorLayout) AllocSize() int
func (TensorLayout) LogicalBytes ¶
func (l TensorLayout) LogicalBytes() int
func (TensorLayout) LogicalElements ¶
func (l TensorLayout) LogicalElements() int
type WeightFile ¶
WeightFile describes a named MIL BLOBFILE entry.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package linear provides a cached linear (matrix multiply) operator backed by the Apple Neural Engine.
|
Package linear provides a cached linear (matrix multiply) operator backed by the Apple Neural Engine. |
|
Package mil generates MIL (Model Intermediate Language) programs and weight blobs for Apple Neural Engine compilation.
|
Package mil generates MIL (Model Intermediate Language) programs and weight blobs for Apple Neural Engine compilation. |