Documentation
¶
Overview ¶
Package metal provides Metal GPU acceleration for macOS and Apple Silicon.
This package implements GPU-accelerated vector similarity search using Apple's Metal API. It provides significant performance improvements for large-scale embedding searches on M1/M2/M3 Macs.
Architecture:
┌─────────────────────────────────────┐
│ Go Application │
│ (EmbeddingIndex API) │
└─────────────┬───────────────────────┘
│ CGO
┌─────────────▼───────────────────────┐
│ metal_bridge.go │
│ (Go bindings via CGO) │
└─────────────┬───────────────────────┘
│ C ABI
┌─────────────▼───────────────────────┐
│ metal_bridge.m │
│ (Objective-C Metal wrapper) │
└─────────────┬───────────────────────┘
│ Metal API
┌─────────────▼───────────────────────┐
│ Apple Silicon GPU │
│ (M1/M2/M3 Neural Engine) │
└─────────────────────────────────────┘
Performance on Apple Silicon:
- M1 Pro: ~500K-1M embeddings/sec (1024-dim)
- M2 Max: ~1M-2M embeddings/sec
- M3 Max: ~2M-4M embeddings/sec
Memory Requirements:
- Each embedding: dimensions × 4 bytes
- 1M embeddings @ 1024-dim = 4GB GPU memory
- Unified memory architecture allows efficient CPU-GPU sharing
Usage:
device, err := metal.NewDevice()
if err != nil {
log.Fatal("Metal not available:", err)
}
defer device.Release()
// Create buffer for embeddings
buffer, err := device.NewBuffer(embeddings, metal.StorageShared)
if err != nil {
log.Fatal(err)
}
// Search
results, err := device.Search(buffer, query, 10, 0.7)
Build Requirements:
- macOS 10.15+ or iOS 13+
- Xcode Command Line Tools
- CGO enabled (CGO_ENABLED=1)
The package is only built on Darwin (macOS/iOS) due to Metal API availability.
Package metal provides Metal GPU acceleration for macOS and Apple Silicon. This file provides stubs for non-Darwin systems.
Index ¶
- Variables
- func IsAvailable() bool
- func MPSIsSupported() bool
- func PrintDeviceInfo()
- type Buffer
- type Device
- func (d *Device) ComputeCosineSimilarity(embeddings, query, scores *Buffer, n, dimensions uint32, normalized bool) error
- func (d *Device) ComputeTopK(scores, indices, topkScores *Buffer, n, k uint32) error
- func (d *Device) GetCapabilities() DeviceCapabilities
- func (d *Device) GetMemoryInfo() MemoryInfo
- func (d *Device) HNSWBuildTopK(frontier, queries *Buffer, frontierN, queryN, dimensions uint32, k int) ([]uint32, []float32, error)
- func (d *Device) MPSBatchCosineSimilarity(embeddings, query, scores *Buffer, n, dims uint32) error
- func (d *Device) MPSMatrixMultiply(a, b, c *Buffer, m, n, k uint32, alpha, beta float32) error
- func (d *Device) MPSMatrixVectorMultiply(a, x, y *Buffer, m, n uint32, alpha, beta float32) error
- func (d *Device) MemoryBytes() uint64
- func (d *Device) MemoryMB() int
- func (d *Device) Name() string
- func (d *Device) NewBuffer(data []float32, mode StorageMode) (*Buffer, error)
- func (d *Device) NewBufferNoCopy(data []float32, mode StorageMode) (*Buffer, error)
- func (d *Device) NewEmptyBuffer(sizeBytes uint64, mode StorageMode) (*Buffer, error)
- func (d *Device) NormalizeVectors(vectors *Buffer, n, dimensions uint32) error
- func (d *Device) Release()
- func (d *Device) Search(embeddings *Buffer, query []float32, n, dimensions uint32, k int, ...) ([]SearchResult, error)
- type DeviceCapabilities
- type MemoryInfo
- type SearchResult
- type StorageMode
Constants ¶
This section is empty.
Variables ¶
var ( ErrMetalNotAvailable = errors.New("metal: Metal is only available on macOS") ErrDeviceCreation = errors.New("metal: failed to create Metal device") ErrBufferCreation = errors.New("metal: failed to create buffer") ErrKernelExecution = errors.New("metal: kernel execution failed") ErrInvalidBuffer = errors.New("metal: invalid buffer") )
Errors
Functions ¶
func IsAvailable ¶
func IsAvailable() bool
IsAvailable checks if Metal is available (always false on non-Darwin).
func MPSIsSupported ¶
func MPSIsSupported() bool
MPSIsSupported returns true if Metal Performance Shaders are available.
func PrintDeviceInfo ¶
func PrintDeviceInfo()
PrintDeviceInfo logs detailed Metal device information (stub).
Types ¶
type Device ¶
type Device struct{}
Device represents a Metal GPU device (stub for non-Darwin).
func (*Device) ComputeCosineSimilarity ¶
func (d *Device) ComputeCosineSimilarity(embeddings, query, scores *Buffer, n, dimensions uint32, normalized bool) error
ComputeCosineSimilarity computes cosine similarity (stub).
func (*Device) ComputeTopK ¶
ComputeTopK finds the k highest scoring indices (stub).
func (*Device) GetCapabilities ¶
func (d *Device) GetCapabilities() DeviceCapabilities
GetCapabilities returns detailed device capabilities (stub).
func (*Device) GetMemoryInfo ¶
func (d *Device) GetMemoryInfo() MemoryInfo
GetMemoryInfo returns current memory statistics (stub).
func (*Device) HNSWBuildTopK ¶ added in v1.1.7
func (d *Device) HNSWBuildTopK(frontier, queries *Buffer, frontierN, queryN, dimensions uint32, k int) ([]uint32, []float32, error)
HNSWBuildTopK computes batched HNSW construction candidates (stub).
func (*Device) MPSBatchCosineSimilarity ¶
MPSBatchCosineSimilarity computes cosine similarities using MPS (stub).
func (*Device) MPSMatrixMultiply ¶
MPSMatrixMultiply performs GPU-accelerated matrix multiplication (stub).
func (*Device) MPSMatrixVectorMultiply ¶
MPSMatrixVectorMultiply performs GPU-accelerated matrix-vector multiplication (stub).
func (*Device) MemoryBytes ¶
MemoryBytes returns the GPU memory size in bytes.
func (*Device) NewBuffer ¶
func (d *Device) NewBuffer(data []float32, mode StorageMode) (*Buffer, error)
NewBuffer creates a new GPU buffer with copied data.
func (*Device) NewBufferNoCopy ¶
func (d *Device) NewBufferNoCopy(data []float32, mode StorageMode) (*Buffer, error)
NewBufferNoCopy creates a GPU buffer that shares memory.
func (*Device) NewEmptyBuffer ¶
func (d *Device) NewEmptyBuffer(sizeBytes uint64, mode StorageMode) (*Buffer, error)
NewEmptyBuffer creates an uninitialized GPU buffer.
func (*Device) NormalizeVectors ¶
NormalizeVectors normalizes vectors in-place (stub).
type DeviceCapabilities ¶
type DeviceCapabilities struct {
Name string
Architecture string
GPUFamily int
MaxThreadsPerThreadgroup int
MaxBufferLength uint64 // Can be very large (16GB+) on Apple Silicon
IsLowPower bool
IsHeadless bool
HasUnifiedMemory bool
RegistryID int
}
DeviceCapabilities contains detailed GPU capabilities.
func GetCapabilitiesNoDevice ¶
func GetCapabilitiesNoDevice() DeviceCapabilities
GetCapabilitiesNoDevice returns capabilities without creating a device (stub).
type MemoryInfo ¶
type MemoryInfo struct {
TotalMemory uint64
UsedMemory uint64
AvailableMemory uint64
GPURecommended uint64
CurrentAllocated uint64
}
MemoryInfo contains GPU and system memory statistics.
type SearchResult ¶
SearchResult holds a similarity search result.
type StorageMode ¶
type StorageMode int
StorageMode defines how buffer memory is managed.
const ( StorageManaged StorageMode = 1 StoragePrivate StorageMode = 2 )