Documentation
¶
Overview ¶
Package mlx provides Apple Metal GPU inference via mlx-c bindings.
This package implements the inference.Backend interface from dappco.re/go/inference for Apple Silicon (M1-M4) GPUs. Import it blank to register the "metal" backend automatically:
import _ "dappco.re/go/mlx"
Build mlx-c before use:
go generate ./...
Generate text ¶
model, err := inference.LoadModel("/path/to/model/")
if err != nil { log.Fatal(err) }
defer model.Close()
ctx := context.Background()
for token := range model.Generate(ctx, "What is 2+2?", inference.WithMaxTokens(128)) {
fmt.Print(token.Text)
}
if err := model.Err(); err != nil { log.Fatal(err) }
Multi-turn chat ¶
Chat applies the model's native template (Gemma3, Qwen3, Llama3):
for token := range model.Chat(ctx, []inference.Message{
{Role: "system", Content: "You are a helpful assistant."},
{Role: "user", Content: "Translate 'hello' to French."},
}, inference.WithMaxTokens(64)) {
fmt.Print(token.Text)
}
Batch classification ¶
Classify runs a single forward pass per prompt (prefill only, no decoding):
results, err := model.Classify(ctx, []string{
"Bonjour, comment allez-vous?",
"The quarterly report shows growth.",
}, inference.WithTemperature(0))
for index, result := range results {
fmt.Printf("prompt %d → %q\n", index, result.Token.Text)
}
Batch generation ¶
results, err := model.BatchGenerate(ctx, []string{
"The capital of France is",
"Water boils at",
}, inference.WithMaxTokens(32))
for index, result := range results {
for _, token := range result.Tokens {
fmt.Print(token.Text)
}
fmt.Println()
}
Performance metrics ¶
After any inference call, retrieve timing and memory statistics:
for token := range model.Generate(ctx, prompt, inference.WithMaxTokens(128)) {
fmt.Print(token.Text)
}
metrics := model.Metrics()
fmt.Printf("decode: %.0f tok/s, peak GPU: %d MB\n",
metrics.DecodeTokensPerSec, metrics.PeakMemoryBytes/1024/1024)
Model info ¶
modelInfo := model.Info()
fmt.Printf("%s %d-layer, %d-bit quantised\n",
modelInfo.Architecture, modelInfo.NumLayers, modelInfo.QuantBits)
Model discovery ¶
discoveredModels, err := inference.Discover("/path/to/models/")
for _, discoveredModel := range discoveredModels {
fmt.Printf("%s (%s, %d-bit)\n", discoveredModel.Path, discoveredModel.ModelType, discoveredModel.QuantBits)
}
Metal memory controls ¶
These control the Metal allocator directly, not individual models:
mlx.SetCacheLimit(4 << 30) // 4 GB cache limit
mlx.SetMemoryLimit(32 << 30) // 32 GB hard limit
// Between chat turns, reclaim prompt cache memory:
mlx.ClearCache()
model1.Close()
mlx.GC() // run Go finalizers for CGO-owned memory without importing runtime
fmt.Printf("active: %d MB, peak: %d MB\n",
mlx.GetActiveMemory()/1024/1024, mlx.GetPeakMemory()/1024/1024)
Package mlx provides Go bindings for Apple's MLX framework via mlx-c.
Index ¶
- Constants
- Variables
- func Available() bool
- func Checkpoint(forwardPass func([]*Array) []*Array) func([]*Array) []*Array
- func ClearCache()
- func Free(_ ...*Array)
- func GC()
- func GetActiveMemory() uint64
- func GetCacheMemory() uint64
- func GetPeakMemory() uint64
- func JVP(_ func([]*Array) []*Array, _ []*Array, _ []*Array) (outputs []*Array, jvps []*Array, err error)
- func Materialize(_ ...*Array)
- func MetalAvailable() bool
- func ResetPeakMemory()
- func SetCacheLimit(_ uint64) uint64
- func SetMemoryLimit(_ uint64) uint64
- func SetWiredLimit(_ uint64) uint64
- func VJP(_ func([]*Array) []*Array, _ []*Array, _ []*Array) (outputs []*Array, vjps []*Array, err error)
- type AdamW
- type AdamWConfig
- type Array
- func Add(a, b *Array) *Array
- func CrossEntropyLoss(_, _ *Array) *Array
- func FromValues[S ~[]E, E stubArrayElement](_ S, shape ...int) *Array
- func MaskedCrossEntropyLoss(_, _, _ *Array) *Array
- func MatMul(a, _ *Array) *Array
- func Mul(a, b *Array) *Array
- func Reshape(a *Array, shape ...any) *Array
- func Slice(a *Array, start, end, axis any) *Array
- func Softmax(a *Array) *Array
- func Zeros(shape []int32, dtype DType) *Array
- func (a *Array) Bool() bool
- func (a *Array) Clone() *Array
- func (a *Array) DataInt32() []int32
- func (a *Array) Dim(i int) int
- func (a *Array) Dims() []int
- func (a *Array) Dtype() DType
- func (a *Array) Float() float64
- func (a *Array) Floats() []float32
- func (a *Array) Int() int
- func (a *Array) Ints() []int
- func (a *Array) Iter() iter.Seq[float32]
- func (a *Array) NumDims() int
- func (a *Array) Set(other *Array)
- func (a *Array) SetFloat64(_ float64)
- func (a *Array) Shape() []int32
- func (a *Array) String() string
- func (a *Array) Valid() bool
- type AttentionSnapshot
- type Batch
- type BatchResult
- type Buffer
- type ByteBuffer
- type Cache
- type ClassifyResult
- type Compute
- type ComputeError
- type ComputeErrorKind
- type DType
- type DeviceInfo
- type DiscoveredModel
- type FrameMetrics
- type GGUFInfo
- type GenOpts
- type GenerateConfig
- type GenerateOption
- func WithLogits() GenerateOption
- func WithMaxTokens(n int) GenerateOption
- func WithMinP(p float32) GenerateOption
- func WithRepeatPenalty(p float32) GenerateOption
- func WithReturnLogits() GenerateOption
- func WithStopTokens(ids ...int32) GenerateOption
- func WithTemperature(t float32) GenerateOption
- func WithTopK(k int) GenerateOption
- func WithTopP(p float32) GenerateOption
- type GradFn
- type InferenceAdapter
- func (adapter *InferenceAdapter) Available() bool
- func (adapter *InferenceAdapter) Chat(ctx context.Context, messages []Message, opts GenOpts) (Result, error)
- func (adapter *InferenceAdapter) ChatStream(ctx context.Context, messages []Message, opts GenOpts, cb TokenCallback) error
- func (adapter *InferenceAdapter) Close() error
- func (adapter *InferenceAdapter) Generate(ctx context.Context, prompt string, opts GenOpts) (Result, error)
- func (adapter *InferenceAdapter) GenerateStream(ctx context.Context, prompt string, opts GenOpts, cb TokenCallback) error
- func (adapter *InferenceAdapter) InspectAttention(ctx context.Context, prompt string, opts ...inference.GenerateOption) (*inference.AttentionSnapshot, error)
- func (adapter *InferenceAdapter) Model() inference.TextModel
- func (adapter *InferenceAdapter) Name() string
- type InternalModel
- type KernelArgs
- type LoRAAdapter
- func (adapter *LoRAAdapter) AllTrainableParams() []*Array
- func (adapter *LoRAAdapter) Merge()
- func (adapter *LoRAAdapter) Save(_ string) error
- func (adapter *LoRAAdapter) SetAllParams(_ []*Array)
- func (adapter *LoRAAdapter) SortedNames() []string
- func (adapter *LoRAAdapter) Step(_ Batch, _ [][]int, _ *AdamW) *Array
- func (adapter *LoRAAdapter) TotalParams() int
- type LoRAConfig
- type LoadConfig
- type LoadOption
- type Message
- type Metrics
- type Model
- func (m *Model) BatchGenerate(_ []string, _ ...GenerateOption) ([]BatchResult, error)
- func (m *Model) Chat(_ []Message, _ ...GenerateOption) (string, error)
- func (m *Model) ChatStream(_ context.Context, _ []Message, _ ...GenerateOption) <-chan Token
- func (m *Model) Classify(_ []string, _ ...GenerateOption) ([]ClassifyResult, error)
- func (m *Model) Close() error
- func (m *Model) Err() error
- func (m *Model) Generate(_ string, _ ...GenerateOption) (string, error)
- func (m *Model) GenerateStream(_ context.Context, _ string, _ ...GenerateOption) <-chan Token
- func (m *Model) Info() ModelInfo
- func (m *Model) InspectAttention(_ string) (*AttentionSnapshot, error)
- func (m *Model) MergeLoRA(_ *LoRAAdapter) *Model
- func (m *Model) Metrics() Metrics
- func (m *Model) ModelType() string
- func (m *Model) Tokenizer() *Tokenizer
- type ModelInfo
- type PixelBuffer
- type PixelBufferDesc
- type PixelFormat
- type Result
- type Session
- type SessionMetrics
- type SessionOption
- type Token
- type TokenCallback
- type Tokenizer
- type TrainConfig
Examples ¶
- AdamW.Reset
- AdamW.Step
- Add
- Array.Bool
- Array.Clone
- Array.DataInt32
- Array.Dim
- Array.Dims
- Array.Dtype
- Array.Float
- Array.Floats
- Array.Int
- Array.Ints
- Array.Iter
- Array.NumDims
- Array.Set
- Array.SetFloat64
- Array.Shape
- Array.String
- Array.Valid
- AttentionSnapshot.HasQueries
- Available
- Checkpoint
- ClearCache
- ComputeError.Error
- ComputeError.Is
- ComputeError.Unwrap
- ConcreteAdapter
- CrossEntropyLoss
- DType.String
- DefaultCompute
- DefaultGenerateConfig
- DefaultLoadConfig
- DiscoverModels
- Free
- FromValues
- GC
- GetActiveMemory
- GetCacheMemory
- GetDeviceInfo
- GetPeakMemory
- GradFn.Apply
- GradFn.Free
- InferenceAdapter.Available
- InferenceAdapter.Chat
- InferenceAdapter.ChatStream
- InferenceAdapter.Close
- InferenceAdapter.Generate
- InferenceAdapter.GenerateStream
- InferenceAdapter.InspectAttention
- InferenceAdapter.Model
- InferenceAdapter.Name
- JVP
- LoRAAdapter.AllTrainableParams
- LoRAAdapter.Merge
- LoRAAdapter.Save
- LoRAAdapter.SetAllParams
- LoRAAdapter.SortedNames
- LoRAAdapter.Step
- LoRAAdapter.TotalParams
- LoadModel
- LoadModelFromMedium
- LoadTokenizer
- MaskedCrossEntropyLoss
- MatMul
- Materialize
- MetalAvailable
- Model.BatchGenerate
- Model.Chat
- Model.ChatStream
- Model.Classify
- Model.Close
- Model.Err
- Model.Generate
- Model.GenerateStream
- Model.Info
- Model.InspectAttention
- Model.MergeLoRA
- Model.Metrics
- Model.ModelType
- Model.Tokenizer
- Mul
- NewAdamW
- NewInferenceAdapter
- NewLoRA
- NewMLXBackend
- NewSession
- PixelBufferDesc.SizeBytes
- PixelBufferDesc.Validate
- PixelFormat.BytesPerPixel
- ReadGGUFInfo
- ResetPeakMemory
- Reshape
- SetCacheLimit
- SetMemoryLimit
- SetWiredLimit
- Slice
- Softmax
- Tokenizer.BOS
- Tokenizer.Decode
- Tokenizer.EOS
- Tokenizer.Encode
- Tokenizer.IDToken
- Tokenizer.TokenID
- TrainingModel
- VJP
- ValueAndGrad
- WithAdapterPath
- WithContextLength
- WithDevice
- WithLogits
- WithMaxTokens
- WithMedium
- WithMinP
- WithQuantization
- WithRepeatPenalty
- WithResetPeakMemory
- WithReturnLogits
- WithSessionLabel
- WithStopTokens
- WithTemperature
- WithTopK
- WithTopP
- WithVerboseKernels
- Zeros
Constants ¶
const ( KernelNearestScale = "nearest_scale" KernelBilinearScale = "bilinear_scale" KernelIntegerScale = "integer_scale" KernelRGB565ToRGBA8 = "rgb565_to_rgba8" KernelRGBA8ToBGRA8 = "rgba8_to_bgra8" KernelBGRA8ToRGBA8 = "bgra8_to_rgba8" KernelXRGB8888ToRGBA8 = "xrgb8888_to_rgba8" KernelPaletteExpandRGBA = "palette_expand_rgba8" KernelScanlineFilter = "scanline_filter" KernelCRTFilter = "crt_filter" KernelSoftenFilter = "soften_filter" KernelSharpenFilter = "sharpen_filter" )
Variables ¶
var ( ErrComputeClosed = &ComputeError{Kind: ComputeErrorClosed} ErrComputeInvalidState = &ComputeError{Kind: ComputeErrorInvalidState} ErrComputeInvalidDescriptor = &ComputeError{Kind: ComputeErrorInvalidDescriptor} ErrComputeUnsupportedPixelFormat = &ComputeError{Kind: ComputeErrorUnsupportedPixelFormat} ErrComputeInvalidBuffer = &ComputeError{Kind: ComputeErrorInvalidBuffer} ErrComputeBufferSizeMismatch = &ComputeError{Kind: ComputeErrorBufferSizeMismatch} ErrComputeInvalidAllocation = &ComputeError{Kind: ComputeErrorInvalidAllocation} ErrComputeMissingKernelBuffer = &ComputeError{Kind: ComputeErrorMissingKernelBuffer} ErrComputeInvalidKernelArgs = &ComputeError{Kind: ComputeErrorInvalidKernelArgs} ErrComputeInvalidScalar = &ComputeError{Kind: ComputeErrorInvalidScalar} ErrComputeUnknownKernel = &ComputeError{Kind: ComputeErrorUnknownKernel} ErrComputeInternal = &ComputeError{Kind: ComputeErrorInternal} )
var ( // DTypeFloat32 is the float32 array dtype. DTypeFloat32 = dtypeFloat32 // DTypeBFloat16 is the bfloat16 array dtype. DTypeBFloat16 = dtypeBFloat16 // DefaultLoRAConfig returns the standard LoRA configuration. DefaultLoRAConfig = func() LoRAConfig { return LoRAConfig{ Rank: 8, Alpha: 16, Scale: 2, TargetKeys: []string{"q_proj", "v_proj"}, TargetLayers: []string{"q_proj", "v_proj"}, DType: DTypeFloat32, } } // DefaultAdamWConfig returns the standard AdamW hyperparameters. DefaultAdamWConfig = func() AdamWConfig { return AdamWConfig{ LearningRate: 1e-5, Beta1: 0.9, Beta2: 0.999, Eps: 1e-8, WeightDecay: 0.01, } } )
Functions ¶
func Available ¶
func Available() bool
Available reports whether native MLX support is available in this build.
Example ¶
core.Println("Available")
Output: Available
func Checkpoint ¶
Checkpoint returns the original function on unsupported builds.
Example ¶
core.Println("Checkpoint")
Output: Checkpoint
func ClearCache ¶
func ClearCache()
ClearCache is a no-op on unsupported builds.
Example ¶
core.Println("ClearCache")
Output: ClearCache
func Free ¶
func Free(_ ...*Array)
Free is a no-op on unsupported builds.
Example ¶
core.Println("Free")
Output: Free
func GC ¶
func GC()
GC runs Go garbage collection for MLX CGO lifecycle cleanup.
Use this after closing large models when prompt/model memory must be reclaimed promptly, without importing runtime at call sites.
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("GC")
Output: GC
func GetActiveMemory ¶
func GetActiveMemory() uint64
GetActiveMemory always reports zero on unsupported builds.
Example ¶
core.Println("GetActiveMemory")
Output: GetActiveMemory
func GetCacheMemory ¶
func GetCacheMemory() uint64
GetCacheMemory always reports zero on unsupported builds.
Example ¶
core.Println("GetCacheMemory")
Output: GetCacheMemory
func GetPeakMemory ¶
func GetPeakMemory() uint64
GetPeakMemory always reports zero on unsupported builds.
Example ¶
core.Println("GetPeakMemory")
Output: GetPeakMemory
func JVP ¶
func JVP(_ func([]*Array) []*Array, _ []*Array, _ []*Array) (outputs []*Array, jvps []*Array, err error)
JVP returns an availability error on unsupported builds.
Example ¶
core.Println("JVP")
Output: JVP
func Materialize ¶
func Materialize(_ ...*Array)
Materialize is a no-op on unsupported builds.
Example ¶
core.Println("Materialize")
Output: Materialize
func MetalAvailable ¶
func MetalAvailable() bool
MetalAvailable reports whether Metal GPU is available.
mlx.MetalAvailable() // → false on non-Apple Silicon
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("MetalAvailable")
Output: MetalAvailable
func ResetPeakMemory ¶
func ResetPeakMemory()
ResetPeakMemory is a no-op on unsupported builds.
Example ¶
core.Println("ResetPeakMemory")
Output: ResetPeakMemory
func SetCacheLimit ¶
SetCacheLimit is a no-op on unsupported builds.
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("SetCacheLimit")
Output: SetCacheLimit
func SetMemoryLimit ¶
SetMemoryLimit is a no-op on unsupported builds.
Example ¶
core.Println("SetMemoryLimit")
Output: SetMemoryLimit
func SetWiredLimit ¶
SetWiredLimit is a no-op on unsupported builds.
Example ¶
core.Println("SetWiredLimit")
Output: SetWiredLimit
Types ¶
type AdamW ¶
type AdamW struct{}
AdamW is a stub optimiser on unsupported builds.
type AdamWConfig ¶
type AdamWConfig struct {
LearningRate float64
Beta1 float64
Beta2 float64
Eps float64
WeightDecay float64
}
AdamWConfig mirrors the supported-build config shape.
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array is a stub tensor on unsupported builds.
func Add ¶
Add returns a stub array using the left-hand shape when available.
Example ¶
core.Println("Add")
Output: Add
func CrossEntropyLoss ¶
CrossEntropyLoss returns nil on unsupported builds.
Example ¶
core.Println("CrossEntropyLoss")
Output: CrossEntropyLoss
func FromValues ¶
FromValues records shape metadata only on unsupported builds.
Example ¶
core.Println("FromValues")
Output: FromValues
func MaskedCrossEntropyLoss ¶
MaskedCrossEntropyLoss returns nil on unsupported builds.
Example ¶
core.Println("MaskedCrossEntropyLoss")
Output: MaskedCrossEntropyLoss
func MatMul ¶
MatMul returns a stub array using the left-hand shape when available.
Example ¶
core.Println("MatMul")
Output: MatMul
func Mul ¶
Mul returns a stub array using the left-hand shape when available.
Example ¶
core.Println("Mul")
Output: Mul
func Reshape ¶
Reshape records the requested shape.
Example ¶
core.Println("Reshape")
Output: Reshape
func Slice ¶
Slice records an updated size along the requested axis when possible.
Example ¶
core.Println("Slice")
Output: Slice
func Softmax ¶
Softmax returns a stub clone on unsupported builds.
Example ¶
core.Println("Softmax")
Output: Softmax
func Zeros ¶
Zeros records shape metadata only on unsupported builds.
Example ¶
core.Println("Zeros")
Output: Zeros
func (*Array) Bool ¶
Bool returns false on unsupported builds.
Example ¶
core.Println("Array_Bool")
Output: Array_Bool
func (*Array) Clone ¶
Clone returns a shallow stub copy.
Example ¶
core.Println("Array_Clone")
Output: Array_Clone
func (*Array) DataInt32 ¶
DataInt32 returns nil on unsupported builds.
Example ¶
core.Println("Array_DataInt32")
Output: Array_DataInt32
func (*Array) Dim ¶
Dim returns the size of dimension i or zero when unavailable.
Example ¶
core.Println("Array_Dim")
Output: Array_Dim
func (*Array) Dims ¶
Dims returns the recorded dimensions as ints.
Example ¶
core.Println("Array_Dims")
Output: Array_Dims
func (*Array) Dtype ¶
Dtype returns the recorded stub dtype.
Example ¶
core.Println("Array_Dtype")
Output: Array_Dtype
func (*Array) Float ¶
Float returns zero on unsupported builds.
Example ¶
core.Println("Array_Float")
Output: Array_Float
func (*Array) Floats ¶
Floats returns nil on unsupported builds.
Example ¶
core.Println("Array_Floats")
Output: Array_Floats
func (*Array) Int ¶
Int returns zero on unsupported builds.
Example ¶
core.Println("Array_Int")
Output: Array_Int
func (*Array) Ints ¶
Ints returns nil on unsupported builds.
Example ¶
core.Println("Array_Ints")
Output: Array_Ints
func (*Array) Iter ¶
Iter yields no values on unsupported builds.
Example ¶
core.Println("Array_Iter")
Output: Array_Iter
func (*Array) NumDims ¶
NumDims returns the number of dimensions in the recorded shape.
Example ¶
core.Println("Array_NumDims")
Output: Array_NumDims
func (*Array) Set ¶
Set replaces the stub array metadata with another array's metadata.
Example ¶
core.Println("Array_Set")
Output: Array_Set
func (*Array) SetFloat64 ¶
SetFloat64 is a no-op on unsupported builds.
Example ¶
core.Println("Array_SetFloat64")
Output: Array_SetFloat64
func (*Array) Shape ¶
Shape returns the recorded stub shape.
Example ¶
core.Println("Array_Shape")
Output: Array_Shape
type AttentionSnapshot ¶
type AttentionSnapshot struct {
NumLayers int
NumHeads int
SeqLen int
HeadDim int
NumQueryHeads int
Keys [][][]float32
Queries [][][]float32
Architecture string
}
AttentionSnapshot contains post-RoPE key tensors extracted from KV caches.
func (*AttentionSnapshot) HasQueries ¶
func (s *AttentionSnapshot) HasQueries() bool
HasQueries reports whether query tensors are present in the snapshot.
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("AttentionSnapshot_HasQueries")
Output: AttentionSnapshot_HasQueries
type BatchResult ¶
BatchResult holds the streamed tokens for a single prompt in a batch call.
type Buffer ¶
type Buffer interface {
Size() int
// contains filtered or unexported methods
}
Buffer is a device-resident compute buffer.
type ByteBuffer ¶
ByteBuffer is a generic device-resident byte buffer.
type Cache ¶
type Cache interface {
Update(k, v *Array, seqLen int) (*Array, *Array)
Offset() int
Len() int
State() []*Array
Reset()
Detach()
}
Cache mirrors the supported-build cache interface.
type ClassifyResult ¶
ClassifyResult holds the sampled token for a single prompt and optional logits.
type Compute ¶
type Compute interface {
Available() bool
DeviceInfo() DeviceInfo
NewSession(opts ...SessionOption) (Session, error)
}
Compute is the public non-LLM Metal compute surface for frame workloads.
func DefaultCompute ¶
func DefaultCompute() Compute
DefaultCompute returns the package's default stub compute backend.
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("DefaultCompute")
Output: DefaultCompute
type ComputeError ¶
type ComputeError struct {
Kind ComputeErrorKind
Op string
Kernel string
Resource string
Message string
Err error
}
ComputeError is the structured error returned by the non-LLM compute API.
func (*ComputeError) Error ¶
func (err *ComputeError) Error() string
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("ComputeError_Error")
Output: ComputeError_Error
func (*ComputeError) Is ¶
func (err *ComputeError) Is(target error) bool
Example ¶
core.Println("ComputeError_Is")
Output: ComputeError_Is
func (*ComputeError) Unwrap ¶
func (err *ComputeError) Unwrap() error
Example ¶
core.Println("ComputeError_Unwrap")
Output: ComputeError_Unwrap
type ComputeErrorKind ¶
type ComputeErrorKind string
ComputeErrorKind classifies non-LLM compute failures for frame-oriented callers.
const ( ComputeErrorClosed ComputeErrorKind = "closed" ComputeErrorInvalidState ComputeErrorKind = "invalid_state" ComputeErrorInvalidDescriptor ComputeErrorKind = "invalid_descriptor" ComputeErrorUnsupportedPixelFormat ComputeErrorKind = "unsupported_pixel_format" ComputeErrorInvalidBuffer ComputeErrorKind = "invalid_buffer" ComputeErrorBufferSizeMismatch ComputeErrorKind = "buffer_size_mismatch" ComputeErrorInvalidAllocation ComputeErrorKind = "invalid_allocation" ComputeErrorMissingKernelBuffer ComputeErrorKind = "missing_kernel_buffer" ComputeErrorInvalidKernelArgs ComputeErrorKind = "invalid_kernel_args" ComputeErrorInvalidScalar ComputeErrorKind = "invalid_scalar" ComputeErrorUnknownKernel ComputeErrorKind = "unknown_kernel" ComputeErrorInternal ComputeErrorKind = "internal" )
type DeviceInfo ¶
type DeviceInfo struct {
Architecture string
MaxBufferLength uint64
MaxRecommendedWorkingSetSize uint64
MemorySize uint64
}
DeviceInfo holds Metal GPU hardware information.
func GetDeviceInfo ¶
func GetDeviceInfo() DeviceInfo
GetDeviceInfo returns zero values on unsupported builds.
Example ¶
core.Println("GetDeviceInfo")
Output: GetDeviceInfo
type DiscoveredModel ¶
type DiscoveredModel struct {
Path string
ModelType string
QuantBits int
QuantGroup int
NumFiles int
Format string
}
DiscoveredModel is a loadable model discovered on disk.
func DiscoverModels ¶
func DiscoverModels(basePath string) []DiscoveredModel
DiscoverModels returns loadable safetensors and GGUF models beneath basePath.
Example ¶
core.Println("DiscoverModels")
Output: DiscoverModels
type FrameMetrics ¶
type FrameMetrics struct {
Frame int
Passes int
LastKernel string
DispatchDuration time.Duration
SyncDuration time.Duration
TotalDuration time.Duration
ActiveMemoryBytes uint64
PeakMemoryBytes uint64
}
FrameMetrics reports timing and memory figures for a single frame lifecycle.
type GGUFInfo ¶
type GGUFInfo struct {
Path string
Architecture string
VocabSize int
HiddenSize int
NumLayers int
ContextLength int
QuantBits int
QuantGroup int
TensorCount int
MetadataCount int
}
GGUFInfo summarises the metadata of a GGUF checkpoint.
type GenerateConfig ¶
type GenerateConfig struct {
MaxTokens int
Temperature float32
TopK int
TopP float32
MinP float32
ReturnLogits bool
StopTokens []int32
RepeatPenalty float32
}
GenerateConfig holds generation parameters for the RFC-style root API.
func DefaultGenerateConfig ¶
func DefaultGenerateConfig() GenerateConfig
DefaultGenerateConfig returns sensible defaults for root-package generation.
Example ¶
core.Println("DefaultGenerateConfig")
Output: DefaultGenerateConfig
type GenerateOption ¶
type GenerateOption func(*GenerateConfig)
GenerateOption configures root-package text generation.
func WithLogits ¶
func WithLogits() GenerateOption
WithLogits requests classification logits when the called API supports them.
Example ¶
core.Println("WithLogits")
Output: WithLogits
func WithMaxTokens ¶
func WithMaxTokens(n int) GenerateOption
WithMaxTokens sets the maximum number of tokens to generate.
Example ¶
core.Println("WithMaxTokens")
Output: WithMaxTokens
func WithMinP ¶
func WithMinP(p float32) GenerateOption
WithMinP sets minimum-probability sampling relative to the best token.
Example ¶
core.Println("WithMinP")
Output: WithMinP
func WithRepeatPenalty ¶
func WithRepeatPenalty(p float32) GenerateOption
WithRepeatPenalty sets the repetition penalty.
Example ¶
core.Println("WithRepeatPenalty")
Output: WithRepeatPenalty
func WithReturnLogits ¶
func WithReturnLogits() GenerateOption
WithReturnLogits is an alias for WithLogits.
Example ¶
core.Println("WithReturnLogits")
Output: WithReturnLogits
func WithStopTokens ¶
func WithStopTokens(ids ...int32) GenerateOption
WithStopTokens sets token IDs that stop generation.
Example ¶
core.Println("WithStopTokens")
Output: WithStopTokens
func WithTemperature ¶
func WithTemperature(t float32) GenerateOption
WithTemperature sets the sampling temperature. 0 = greedy.
Example ¶
core.Println("WithTemperature")
Output: WithTemperature
func WithTopK ¶
func WithTopK(k int) GenerateOption
WithTopK sets top-k sampling. 0 = disabled.
Example ¶
core.Println("WithTopK")
Output: WithTopK
func WithTopP ¶
func WithTopP(p float32) GenerateOption
WithTopP sets nucleus sampling. 0 = disabled.
Example ¶
core.Println("WithTopP")
Output: WithTopP
type GradFn ¶
type GradFn struct{}
GradFn is a stub autodiff handle on unsupported builds.
func ValueAndGrad ¶
ValueAndGrad creates a stub GradFn.
Example ¶
core.Println("ValueAndGrad")
Output: ValueAndGrad
type InferenceAdapter ¶
type InferenceAdapter struct {
// contains filtered or unexported fields
}
InferenceAdapter wraps an inference.TextModel with buffered/string APIs.
func NewInferenceAdapter ¶
func NewInferenceAdapter(model inference.TextModel, name string) *InferenceAdapter
NewInferenceAdapter wraps a loaded inference model with an adapter surface.
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("NewInferenceAdapter")
Output: NewInferenceAdapter
func NewMLXBackend ¶
func NewMLXBackend(modelPath string, loadOpts ...inference.LoadOption) (*InferenceAdapter, error)
NewMLXBackend loads the Metal backend and wraps it in an InferenceAdapter.
Example ¶
core.Println("NewMLXBackend")
Output: NewMLXBackend
func (*InferenceAdapter) Available ¶
func (adapter *InferenceAdapter) Available() bool
Available reports whether the underlying model is loaded.
Example ¶
core.Println("InferenceAdapter_Available")
Output: InferenceAdapter_Available
func (*InferenceAdapter) Chat ¶
func (adapter *InferenceAdapter) Chat(ctx context.Context, messages []Message, opts GenOpts) (Result, error)
Chat collects a streamed chat response into a single string.
Example ¶
core.Println("InferenceAdapter_Chat")
Output: InferenceAdapter_Chat
func (*InferenceAdapter) ChatStream ¶
func (adapter *InferenceAdapter) ChatStream(ctx context.Context, messages []Message, opts GenOpts, cb TokenCallback) error
ChatStream forwards chat token text to a callback.
Example ¶
core.Println("InferenceAdapter_ChatStream")
Output: InferenceAdapter_ChatStream
func (*InferenceAdapter) Close ¶
func (adapter *InferenceAdapter) Close() error
Close releases the underlying model.
Example ¶
core.Println("InferenceAdapter_Close")
Output: InferenceAdapter_Close
func (*InferenceAdapter) Generate ¶
func (adapter *InferenceAdapter) Generate(ctx context.Context, prompt string, opts GenOpts) (Result, error)
Generate collects a streamed response into a single string.
Example ¶
core.Println("InferenceAdapter_Generate")
Output: InferenceAdapter_Generate
func (*InferenceAdapter) GenerateStream ¶
func (adapter *InferenceAdapter) GenerateStream(ctx context.Context, prompt string, opts GenOpts, cb TokenCallback) error
GenerateStream forwards token text to a callback.
Example ¶
core.Println("InferenceAdapter_GenerateStream")
Output: InferenceAdapter_GenerateStream
func (*InferenceAdapter) InspectAttention ¶
func (adapter *InferenceAdapter) InspectAttention(ctx context.Context, prompt string, opts ...inference.GenerateOption) (*inference.AttentionSnapshot, error)
InspectAttention delegates to the underlying model when supported.
Example ¶
core.Println("InferenceAdapter_InspectAttention")
Output: InferenceAdapter_InspectAttention
func (*InferenceAdapter) Model ¶
func (adapter *InferenceAdapter) Model() inference.TextModel
Model returns the wrapped inference.TextModel.
Example ¶
core.Println("InferenceAdapter_Model")
Output: InferenceAdapter_Model
func (*InferenceAdapter) Name ¶
func (adapter *InferenceAdapter) Name() string
Name returns the configured adapter name.
Example ¶
core.Println("InferenceAdapter_Name")
Output: InferenceAdapter_Name
type InternalModel ¶
type InternalModel interface {
Forward(tokens *Array, caches []Cache) *Array
ForwardMasked(tokens *Array, mask *Array, caches []Cache) *Array
NewCache() []Cache
NumLayers() int
Tokenizer() *Tokenizer
ModelType() string
ApplyLoRA(cfg LoRAConfig) *LoRAAdapter
}
InternalModel mirrors the supported-build training interface.
func TrainingModel ¶
func TrainingModel(_ inference.TrainableModel) InternalModel
TrainingModel returns nil on unsupported builds.
Example ¶
core.Println("TrainingModel")
Output: TrainingModel
type KernelArgs ¶
type KernelArgs struct {
Inputs map[string]Buffer
Outputs map[string]Buffer
Scalars map[string]float64
}
KernelArgs groups named inputs, outputs, and scalar parameters for a kernel dispatch.
type LoRAAdapter ¶
type LoRAAdapter struct {
Config LoRAConfig
}
LoRAAdapter holds stub adapter metadata on unsupported builds.
func ConcreteAdapter ¶
func ConcreteAdapter(_ inference.Adapter) *LoRAAdapter
ConcreteAdapter returns nil on unsupported builds.
Example ¶
core.Println("ConcreteAdapter")
Output: ConcreteAdapter
func NewLoRA ¶
func NewLoRA(_ *Model, _ *LoRAConfig) *LoRAAdapter
NewLoRA returns nil on unsupported builds.
Example ¶
core.Println("NewLoRA")
Output: NewLoRA
func (*LoRAAdapter) AllTrainableParams ¶
func (adapter *LoRAAdapter) AllTrainableParams() []*Array
AllTrainableParams reports no trainable arrays on unsupported builds.
Example ¶
core.Println("LoRAAdapter_AllTrainableParams")
Output: LoRAAdapter_AllTrainableParams
func (*LoRAAdapter) Merge ¶
func (adapter *LoRAAdapter) Merge()
Merge is a no-op on unsupported builds.
Example ¶
core.Println("LoRAAdapter_Merge")
Output: LoRAAdapter_Merge
func (*LoRAAdapter) Save ¶
func (adapter *LoRAAdapter) Save(_ string) error
Save returns an availability error on unsupported builds.
Example ¶
core.Println("LoRAAdapter_Save")
Output: LoRAAdapter_Save
func (*LoRAAdapter) SetAllParams ¶
func (adapter *LoRAAdapter) SetAllParams(_ []*Array)
SetAllParams is a no-op on unsupported builds.
Example ¶
core.Println("LoRAAdapter_SetAllParams")
Output: LoRAAdapter_SetAllParams
func (*LoRAAdapter) SortedNames ¶
func (adapter *LoRAAdapter) SortedNames() []string
SortedNames reports no layer names on unsupported builds.
Example ¶
core.Println("LoRAAdapter_SortedNames")
Output: LoRAAdapter_SortedNames
func (*LoRAAdapter) Step ¶
func (adapter *LoRAAdapter) Step(_ Batch, _ [][]int, _ *AdamW) *Array
Step returns nil on unsupported builds.
Example ¶
core.Println("LoRAAdapter_Step")
Output: LoRAAdapter_Step
func (*LoRAAdapter) TotalParams ¶
func (adapter *LoRAAdapter) TotalParams() int
TotalParams reports zero on unsupported builds.
Example ¶
core.Println("LoRAAdapter_TotalParams")
Output: LoRAAdapter_TotalParams
type LoRAConfig ¶
type LoRAConfig struct {
Rank int
Alpha float32
Scale float32
TargetKeys []string
TargetLayers []string
Lambda float32
DType DType
}
LoRAConfig mirrors the supported-build LoRA config shape.
type LoadConfig ¶
type LoadConfig struct {
ContextLength int
Quantization int
Device string
AdapterPath string
Medium coreio.Medium
}
LoadConfig holds root-package model loading parameters.
func DefaultLoadConfig ¶
func DefaultLoadConfig() LoadConfig
DefaultLoadConfig returns sensible defaults for root-package loading.
Example ¶
core.Println("DefaultLoadConfig")
Output: DefaultLoadConfig
type LoadOption ¶
type LoadOption func(*LoadConfig)
LoadOption configures root-package model loading.
func WithAdapterPath ¶
func WithAdapterPath(path string) LoadOption
WithAdapterPath injects a LoRA adapter directory at model load time.
Example ¶
core.Println("WithAdapterPath")
Output: WithAdapterPath
func WithContextLength ¶
func WithContextLength(n int) LoadOption
WithContextLength bounds the KV cache to the given context window.
Example ¶
core.Println("WithContextLength")
Output: WithContextLength
func WithDevice ¶
func WithDevice(device string) LoadOption
WithDevice selects the execution device: "gpu" or "cpu".
Example ¶
core.Println("WithDevice")
Output: WithDevice
func WithMedium ¶
func WithMedium(medium coreio.Medium) LoadOption
WithMedium stages model files from the supplied io.Medium before loading. The model path passed to LoadModel is interpreted within that medium.
Example ¶
core.Println("WithMedium")
Output: WithMedium
func WithQuantization ¶
func WithQuantization(bits int) LoadOption
WithQuantization validates the loaded quantisation width.
Example ¶
core.Println("WithQuantization")
Output: WithQuantization
type Metrics ¶
type Metrics struct {
PromptTokens int
GeneratedTokens int
PrefillDuration time.Duration
DecodeDuration time.Duration
TotalDuration time.Duration
PrefillTokensPerSec float64
DecodeTokensPerSec float64
PeakMemoryBytes uint64
ActiveMemoryBytes uint64
}
Metrics reports performance counters from the last inference call.
type Model ¶
type Model struct{}
Model is a stub on unsupported builds.
func LoadModel ¶
func LoadModel(_ string, _ ...LoadOption) (*Model, error)
LoadModel returns an availability error on unsupported builds.
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("LoadModel")
Output: LoadModel
func LoadModelFromMedium ¶
func LoadModelFromMedium(medium coreio.Medium, modelPath string, opts ...LoadOption) (*Model, error)
LoadModelFromMedium stages model files from an io.Medium before loading them.
model, err := mlx.LoadModelFromMedium(medium, "models/gemma-3-1b", mlx.WithContextLength(8192))
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("LoadModelFromMedium")
Output: LoadModelFromMedium
func (*Model) BatchGenerate ¶
func (m *Model) BatchGenerate(_ []string, _ ...GenerateOption) ([]BatchResult, error)
BatchGenerate returns an availability error on unsupported builds.
Example ¶
core.Println("Model_BatchGenerate")
Output: Model_BatchGenerate
func (*Model) Chat ¶
func (m *Model) Chat(_ []Message, _ ...GenerateOption) (string, error)
Chat returns an availability error on unsupported builds.
Example ¶
core.Println("Model_Chat")
Output: Model_Chat
func (*Model) ChatStream ¶
ChatStream closes immediately on unsupported builds.
Example ¶
core.Println("Model_ChatStream")
Output: Model_ChatStream
func (*Model) Classify ¶
func (m *Model) Classify(_ []string, _ ...GenerateOption) ([]ClassifyResult, error)
Classify returns an availability error on unsupported builds.
Example ¶
core.Println("Model_Classify")
Output: Model_Classify
func (*Model) Close ¶
Close is a no-op on unsupported builds.
Example ¶
core.Println("Model_Close")
Output: Model_Close
func (*Model) Err ¶
Err returns the availability error on unsupported builds.
Example ¶
core.Println("Model_Err")
Output: Model_Err
func (*Model) Generate ¶
func (m *Model) Generate(_ string, _ ...GenerateOption) (string, error)
Generate returns an availability error on unsupported builds.
Example ¶
core.Println("Model_Generate")
Output: Model_Generate
func (*Model) GenerateStream ¶
GenerateStream closes immediately on unsupported builds.
Example ¶
core.Println("Model_GenerateStream")
Output: Model_GenerateStream
func (*Model) Info ¶
Info returns zero values on unsupported builds.
Example ¶
core.Println("Model_Info")
Output: Model_Info
func (*Model) InspectAttention ¶
func (m *Model) InspectAttention(_ string) (*AttentionSnapshot, error)
InspectAttention returns an availability error on unsupported builds.
Example ¶
core.Println("Model_InspectAttention")
Output: Model_InspectAttention
func (*Model) MergeLoRA ¶
func (m *Model) MergeLoRA(_ *LoRAAdapter) *Model
MergeLoRA is a no-op on unsupported builds.
Example ¶
core.Println("Model_MergeLoRA")
Output: Model_MergeLoRA
func (*Model) Metrics ¶
Metrics returns zero values on unsupported builds.
Example ¶
core.Println("Model_Metrics")
Output: Model_Metrics
type ModelInfo ¶
type ModelInfo struct {
Architecture string
VocabSize int
NumLayers int
HiddenSize int
QuantBits int
QuantGroup int
ContextLength int
}
ModelInfo describes a loaded model.
type PixelBuffer ¶
type PixelBuffer interface {
Buffer
Descriptor() PixelBufferDesc
Upload(data []byte) error
Read() ([]byte, error)
}
PixelBuffer is a packed image buffer stored on the compute device.
type PixelBufferDesc ¶
type PixelBufferDesc struct {
Width int
Height int
Stride int
Format PixelFormat
}
PixelBufferDesc describes one packed image buffer.
func (PixelBufferDesc) SizeBytes ¶
func (desc PixelBufferDesc) SizeBytes() int
SizeBytes reports the total packed byte length of the buffer, or 0 when the descriptor is invalid or cannot be represented as an int byte count.
Example ¶
core.Println("PixelBufferDesc_SizeBytes")
Output: PixelBufferDesc_SizeBytes
func (PixelBufferDesc) Validate ¶
func (desc PixelBufferDesc) Validate() error
Validate checks whether the descriptor can back a packed pixel buffer.
Example ¶
core.Println("PixelBufferDesc_Validate")
Output: PixelBufferDesc_Validate
type PixelFormat ¶
type PixelFormat string
PixelFormat identifies the layout of a packed pixel buffer.
const ( PixelRGBA8 PixelFormat = "rgba8" PixelBGRA8 PixelFormat = "bgra8" PixelRGB565 PixelFormat = "rgb565" PixelXRGB8888 PixelFormat = "xrgb8888" PixelIndexed8 PixelFormat = "indexed8" )
func (PixelFormat) BytesPerPixel ¶
func (format PixelFormat) BytesPerPixel() int
BytesPerPixel reports the packed bytes-per-pixel for the format.
Example ¶
core.Println("PixelFormat_BytesPerPixel")
Output: PixelFormat_BytesPerPixel
type Result ¶
type Result struct {
Text string
Metrics *inference.GenerateMetrics
}
Result holds buffered text plus optional backend metrics.
type Session ¶
type Session interface {
Close() error
BeginFrame() error
FinishFrame() (FrameMetrics, error)
NewPixelBuffer(desc PixelBufferDesc) (PixelBuffer, error)
NewByteBuffer(size int) (ByteBuffer, error)
Run(kernel string, args KernelArgs) error
Sync() error
Metrics() SessionMetrics
FrameMetrics() FrameMetrics
}
Session owns a set of device buffers and reusable kernel state.
func NewSession ¶
func NewSession(opts ...SessionOption) (Session, error)
NewSession returns an availability error on unsupported builds.
Example ¶
core.Println("NewSession")
Output: NewSession
type SessionMetrics ¶
type SessionMetrics struct {
Passes int
LastKernel string
LastDispatchDuration time.Duration
LastSyncDuration time.Duration
TotalDispatchDuration time.Duration
TotalSyncDuration time.Duration
ActiveMemoryBytes uint64
PeakMemoryBytes uint64
}
SessionMetrics reports coarse timing and memory figures for a compute session.
type SessionOption ¶
type SessionOption func(*sessionConfig)
SessionOption configures a compute session.
func WithResetPeakMemory ¶
func WithResetPeakMemory(reset bool) SessionOption
WithResetPeakMemory controls whether session creation resets the global MLX peak counter.
Example ¶
core.Println("WithResetPeakMemory")
Output: WithResetPeakMemory
func WithSessionLabel ¶
func WithSessionLabel(label string) SessionOption
WithSessionLabel attaches a human-readable label to a compute session. The label is folded into compiled kernel names so verbose kernel logs can be tied back to a specific frame pipeline.
Example ¶
core.Println("WithSessionLabel")
Output: WithSessionLabel
func WithVerboseKernels ¶
func WithVerboseKernels(verbose bool) SessionOption
WithVerboseKernels enables verbose kernel compilation logging for the session.
Example ¶
core.Println("WithVerboseKernels")
Output: WithVerboseKernels
type TokenCallback ¶
TokenCallback receives streamed token text.
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer wraps a pure-Go tokenizer implementation with a root-package API.
func LoadTokenizer ¶
LoadTokenizer loads a tokenizer.json file directly using the pure-Go tokenizer implementation.
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("LoadTokenizer")
Output: LoadTokenizer
func (*Tokenizer) BOS ¶
BOS returns the beginning-of-sequence token ID.
Example ¶
core.Println("Tokenizer_BOS")
Output: Tokenizer_BOS
func (*Tokenizer) Decode ¶
Decode converts token IDs back to text.
Example ¶
core.Println("Tokenizer_Decode")
Output: Tokenizer_Decode
func (*Tokenizer) EOS ¶
EOS returns the end-of-sequence token ID.
Example ¶
core.Println("Tokenizer_EOS")
Output: Tokenizer_EOS
func (*Tokenizer) Encode ¶
Encode converts text to token IDs without the model-internal implicit BOS token.
Example ¶
Generated runnable examples for file-aware public API coverage.
core.Println("Tokenizer_Encode")
Output: Tokenizer_Encode
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
violet
command
|
|
|
internal
|
|
|
metal
AX-6-exception: runtime import scoped here so consumers can call mlx.GC() instead of runtime.GC() directly.
|
AX-6-exception: runtime import scoped here so consumers can call mlx.GC() instead of runtime.GC() directly. |
|
Package mlxlm provides a subprocess-based inference backend using Python's mlx-lm.
|
Package mlxlm provides a subprocess-based inference backend using Python's mlx-lm. |
|
pkg
|
|
|
tests
|
|
|
cli/violet
command
|