metal

package
v1.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 1 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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 Buffer

type Buffer struct{}

Buffer represents a Metal GPU buffer (stub for non-Darwin).

func (*Buffer) Release

func (b *Buffer) Release()

Release frees the buffer resources.

func (*Buffer) Size

func (b *Buffer) Size() uint64

Size returns the buffer size in bytes.

type Device

type Device struct{}

Device represents a Metal GPU device (stub for non-Darwin).

func NewDevice

func NewDevice() (*Device, error)

NewDevice creates a new Metal device (not available on 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

func (d *Device) ComputeTopK(scores, indices, topkScores *Buffer, n, k uint32) error

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

func (d *Device) MPSBatchCosineSimilarity(embeddings, query, scores *Buffer, n, dims uint32) error

MPSBatchCosineSimilarity computes cosine similarities using MPS (stub).

func (*Device) MPSMatrixMultiply

func (d *Device) MPSMatrixMultiply(a, b, c *Buffer, m, n, k uint32, alpha, beta float32) error

MPSMatrixMultiply performs GPU-accelerated matrix multiplication (stub).

func (*Device) MPSMatrixVectorMultiply

func (d *Device) MPSMatrixVectorMultiply(a, x, y *Buffer, m, n uint32, alpha, beta float32) error

MPSMatrixVectorMultiply performs GPU-accelerated matrix-vector multiplication (stub).

func (*Device) MemoryBytes

func (d *Device) MemoryBytes() uint64

MemoryBytes returns the GPU memory size in bytes.

func (*Device) MemoryMB

func (d *Device) MemoryMB() int

MemoryMB returns the GPU memory size in megabytes.

func (*Device) Name

func (d *Device) Name() string

Name returns the GPU device name.

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

func (d *Device) NormalizeVectors(vectors *Buffer, n, dimensions uint32) error

NormalizeVectors normalizes vectors in-place (stub).

func (*Device) Release

func (d *Device) Release()

Release frees the Metal device resources.

func (*Device) Search

func (d *Device) Search(embeddings *Buffer, query []float32, n, dimensions uint32, k int, normalized bool) ([]SearchResult, error)

Search performs a complete similarity search (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

type SearchResult struct {
	Index uint32
	Score float32
}

SearchResult holds a similarity search result.

type StorageMode

type StorageMode int

StorageMode defines how buffer memory is managed.

const (
	StorageShared  StorageMode = 0
	StorageManaged StorageMode = 1
	StoragePrivate StorageMode = 2
)

Jump to

Keyboard shortcuts

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