Skip to content

Add Metal GPU acceleration for Apple Silicon - #3

Draft
mudler wants to merge 3 commits into
mainfrom
metal
Draft

Add Metal GPU acceleration for Apple Silicon#3
mudler wants to merge 3 commits into
mainfrom
metal

Conversation

@mudler

@mudler mudler commented Mar 26, 2026

Copy link
Copy Markdown
Owner

Metal Shaders:

  • rms_norm: threadgroup reduction with shared memory
  • silu_mul: fused SwiGLU activation
  • rope_apply / rope_apply_pos: rotary position embeddings
  • decoder_attention: online softmax with SIMD cooperative dot products
  • kv_cache_copy: single-token and batched KV cache writes
  • causal_softmax: for prefill attention scores
  • bf16_to_f32, time_embedding, argmax helper kernels

Metal Implementation:

  • BF16->F16 weight cache (MPS doesn't support bf16 natively)
  • F32 weight cache for norm weights
  • Activation buffer pool (reusable scratch buffers)
  • Zero-copy shared memory KV cache (MTLResourceStorageModeShared)
  • MPS MatrixMultiplication for GEMM (f32 activations x f16 weights)
  • Monolithic decoder forward: all 26 layers in ONE command buffer with MPS matmuls + custom compute kernels interleaved
  • Memory barriers between dependent operations

Build: make apple-gpu

Full Metal backend

Metal Shaders (voxtral_tts_shaders.metal, 446 lines):
- rms_norm: threadgroup reduction with shared memory
- silu_mul: fused SwiGLU activation
- rope_apply / rope_apply_pos: rotary position embeddings
- decoder_attention: online softmax with SIMD cooperative dot products
- kv_cache_copy: single-token and batched KV cache writes
- causal_softmax: for prefill attention scores
- bf16_to_f32, time_embedding, argmax helper kernels

Metal Implementation (voxtral_tts_metal.m, 872 lines):
- BF16->F16 weight cache (MPS doesn't support bf16 natively)
- F32 weight cache for norm weights
- Activation buffer pool (reusable scratch buffers)
- Zero-copy shared memory KV cache (MTLResourceStorageModeShared)
- MPS MatrixMultiplication for GEMM (f32 activations x f16 weights)
- Monolithic decoder forward: all 26 layers in ONE command buffer
  with MPS matmuls + custom compute kernels interleaved
- Memory barriers between dependent operations

Integration:
- #ifdef USE_METAL dispatch in tts_llm_forward()
- Metal shared alloc for KV cache
- Metal init/shutdown in model load/free
- Makefile: make apple-gpu (requires Xcode + Metal frameworks)
- Shaders embedded via xxd -i at compile time

Build: make apple-gpu
Requires: macOS with Apple Silicon (M1+)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
mudler and others added 2 commits March 27, 2026 08:44
- Added #ifdef USE_METAL test paths for rms_norm, silu_mul, add_inplace, gemm_bf16
- Added standalone Metal kernel wrappers (rms_norm, silu_mul, add_inplace)
  for test harness and individual dispatch
- Fixed ARC __strong pointer annotation for pipeline state array
- Added test-metal Makefile target

Metal test results (Apple M4):
  - add_inplace: PASS (exact match)
  - silu_mul: PASS (max_rel=3.4e-7)
  - rms_norm: PASS (max_rel=2.4e-7)
  - gemm_bf16: PASS (max_rel=8.9e-2 — bf16->f16 lossy conversion)
  - Attention: PASS (CPU-self, Metal attention wrapper TODO)

The high GEMM relative error (8.9% max) from bf16->f16 conversion may
cause accumulated divergence in the monolithic 26-layer forward pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Fixed duplicate function definition from bulk rename
- Removed unused f32 weight cache (causes OOM on 16GB Macs)
- Using bf16->f16 converted weights for MPS matmul (same as voxtral.c)
- Added standalone kernel wrappers for test harness

Known limitation: bf16->f16 conversion has ~8.9% max relative error
which causes audio quality degradation on Metal. The TTS model is
more precision-sensitive than ASR. Macs with >16GB may use f32
weight cache for better quality (toggle in code).

Test results (M4, 16GB):
  - Kernels: 11/11 PASS
  - Model loads and generates audio
  - Audio quality: degraded due to f16 precision

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant