metal

package
v0.0.0-...-7e94837 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package metal owns a Metal device and its default command queue and provides buffer and pipeline construction helpers.

Raw, Queue, and objects constructed from a Device are borrowed from or derived from that Device. Callers must finish command buffers and release derived objects before closing the Device. Device methods may be called concurrently, but borrowed objects must not be used concurrently with Close.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("metal: device is closed")

ErrClosed is returned by operations on a closed Device.

Functions

func SetScalar

func SetScalar[T Scalar](enc metal.MTLComputeCommandEncoder, v T, index int)

SetScalar binds v into enc's argument table at index. Kernel scalars are small and change on every dispatch, so copying them inline is cheaper than allocating a buffer per argument per dispatch.

func Threadgroups

func Threadgroups(n, group int) metal.MTLSize

Threadgroups returns the one-dimensional size covering n threads in groups of group threads each, rounding up so a partial final group still runs. A group below one is treated as one.

func Threads

func Threads(n int) metal.MTLSize

Threads returns the one-dimensional size covering n threads, for dispatching by thread count. An n below one becomes one: Metal rejects an empty grid.

Types

type Device

type Device struct {
	// contains filtered or unexported fields
}

Device represents an Apple Metal GPU device.

func NewDevice

func NewDevice() (*Device, error)

NewDevice returns the system default Metal GPU device.

func (*Device) AllocBuffer

func (d *Device) AllocBuffer(bytes int) (metal.MTLBuffer, error)

AllocBuffer allocates a shared Metal buffer owned by the caller.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/objectivec"
	"github.com/tmc/lib/apple/x/metal"
)

func main() {
	dev, err := metal.NewDevice()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer dev.Close()

	buf, err := dev.AllocBuffer(16)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer objectivec.ObjectFromID(buf.GetID()).Release()

	fmt.Println(buf.Length())
}
Output:
16

func (*Device) AllocBufferBytes

func (d *Device) AllocBufferBytes(data []byte) (metal.MTLBuffer, error)

AllocBufferBytes copies host data into a shared buffer owned by the caller.

func (*Device) Close

func (d *Device) Close() error

Close releases the command queue and Metal device. It is idempotent. The caller must complete all command buffers and release device-derived objects before calling Close. Close waits for Device methods already in progress; borrowed values returned by Raw and Queue must not be used concurrently with Close.

func (*Device) CompileLibrary

func (d *Device) CompileLibrary(source string) (metal.MTLLibrary, error)

CompileLibrary compiles MSL source into a library owned by the caller. The caller must release the library after creating its functions and pipelines.

func (*Device) Name

func (d *Device) Name() string

Name returns the device name.

func (*Device) NewComputePipeline

func (d *Device) NewComputePipeline(lib metal.MTLLibrary, name string) (metal.MTLComputePipelineState, error)

NewComputePipeline creates a pipeline owned by the caller. The temporary function used to create the pipeline is released before the method returns.

func (*Device) Queue

func (d *Device) Queue() metal.MTLCommandQueue

Queue returns the device's borrowed default command queue. It returns nil after Close. Before Close it is non-nil because NewDevice rejects a device that cannot create a queue. The result must not be used concurrently with Close.

func (*Device) Raw

func (d *Device) Raw() metal.MTLDevice

Raw returns the underlying Metal device. The result is borrowed and remains valid only until Close. Raw must not run concurrently with Close.

type Scalar

type Scalar interface {
	~int8 | ~int16 | ~int32 | ~int64 |
		~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

Scalar is the set of types a Metal kernel scalar argument may have.

Go's int and uint are deliberately absent. They are 64 bits wide on Apple silicon, while MSL's int and uint are 32, so binding one writes twice the bytes the kernel reads and leaves the kernel with a value it never agreed to. Convert to a fixed-width type at the call site instead.

Directories

Path Synopsis
Package egraph provides equality saturation, expression extraction, and execution-plan telemetry.
Package egraph provides equality saturation, expression extraction, and execution-plan telemetry.
Package mempool provides typed memory allocations and pooling for Apple Silicon Unified Memory.
Package mempool provides typed memory allocations and pooling for Apple Silicon Unified Memory.
Package metalattn provides SIMD-group causal FlashAttention kernels and configurations for Apple Metal.
Package metalattn provides SIMD-group causal FlashAttention kernels and configurations for Apple Metal.
Package metalbench provides experimental wall-clock timing and empirical autotuning harnesses for Apple Silicon Metal.
Package metalbench provides experimental wall-clock timing and empirical autotuning harnesses for Apple Silicon Metal.
Package metalcapture provides pure Go portable recording, serialization, and inspection of Metal profiling captures.
Package metalcapture provides pure Go portable recording, serialization, and inspection of Metal profiling captures.
Package metalgrammar provides zero-overhead FSM grammar and bitmask logit filtering for Metal.
Package metalgrammar provides zero-overhead FSM grammar and bitmask logit filtering for Metal.
Package metalgraphexec provides Metal Indirect Command Buffer (ICB) and compute graph execution caching for single-launch multi-layer transformer dispatch on Apple Silicon GPUs.
Package metalgraphexec provides Metal Indirect Command Buffer (ICB) and compute graph execution caching for single-launch multi-layer transformer dispatch on Apple Silicon GPUs.
Package metalicb provides Indirect Command Buffer (ICB) execution abstractions for GPU-driven hardware graph replays on Apple Silicon.
Package metalicb provides Indirect Command Buffer (ICB) execution abstractions for GPU-driven hardware graph replays on Apple Silicon.
Package metalio provides high-performance asynchronous storage ingestion and streaming into Apple Unified Memory buffers.
Package metalio provides high-performance asynchronous storage ingestion and streaming into Apple Unified Memory buffers.
Package metalir provides a symbolic computation graph and memory arena planner for Apple Metal.
Package metalir provides a symbolic computation graph and memory arena planner for Apple Metal.
Package metalkv provides static KV-cache management in Apple Silicon Unified Memory.
Package metalkv provides static KV-cache management in Apple Silicon Unified Memory.
Package metalmegakernel provides experimental Metal kernels used by the llama inference example.
Package metalmegakernel provides experimental Metal kernels used by the llama inference example.
Package metalmem provides typed memory management, zero-copy unified memory allocations, and host-device copy primitives for Apple Silicon Metal GPUs.
Package metalmem provides typed memory management, zero-copy unified memory allocations, and host-device copy primitives for Apple Silicon Metal GPUs.
Package metalmlx is an experimental, minimal Go conversion of the host-side runtime of MLX's Metal backend (mlx/backend/metal), built on the pure-Go Metal bindings.
Package metalmlx is an experimental, minimal Go conversion of the host-side runtime of MLX's Metal backend (mlx/backend/metal), built on the pure-Go Metal bindings.
Package metalmoe provides Mixture of Experts routing and expert cache offloading for Apple Silicon Metal.
Package metalmoe provides Mixture of Experts routing and expert cache offloading for Apple Silicon Metal.
Package metalmtp implements Multi-Token Prediction (MTP) and speculative tree decoding optimized via equality saturation (E-Graph) megakernels on Apple Metal.
Package metalmtp implements Multi-Token Prediction (MTP) and speculative tree decoding optimized via equality saturation (E-Graph) megakernels on Apple Metal.
Package metalmxfp4 provides Apple Silicon Metal compute kernels for OCP Microscaling FP4 (E2M1) dequantization and high-throughput fused GEMV matrix-vector multiplication.
Package metalmxfp4 provides Apple Silicon Metal compute kernels for OCP Microscaling FP4 (E2M1) dequantization and high-throughput fused GEMV matrix-vector multiplication.
Package metalprof provides Apple Metal capture helpers, hardware timestamp sampling, caller-supplied event recording, and pprof integration.
Package metalprof provides Apple Metal capture helpers, hardware timestamp sampling, caller-supplied event recording, and pprof integration.
Package metalquant encodes and decodes Q4_0 and Q8_0 blocks used by Metal kernels.
Package metalquant encodes and decodes Q4_0 and Q8_0 blocks used by Metal kernels.
Package metalref provides host CPU reference oracle implementations for parity testing.
Package metalref provides host CPU reference oracle implementations for parity testing.
Package metalsafetensors reads safetensors files and copies selected tensors into shared Metal buffers.
Package metalsafetensors reads safetensors files and copies selected tensors into shared Metal buffers.
Package metalsample provides greedy argmax selection for Apple Metal.
Package metalsample provides greedy argmax selection for Apple Metal.
Package metalspec provides speculative decoding drafters, tree verifiers, and candidate token verification for Metal.
Package metalspec provides speculative decoding drafters, tree verifiers, and candidate token verification for Metal.
Package metaltensor provides high-level symbolic and device-backed tensor operations for Apple Silicon Metal.
Package metaltensor provides high-level symbolic and device-backed tensor operations for Apple Silicon Metal.
Package metalx compiles Metal Shading Language libraries and owns device-scoped compute pipeline caches.
Package metalx compiles Metal Shading Language libraries and owns device-scoped compute pipeline caches.

Jump to

Keyboard shortcuts

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