Expand description
Metal mega-pass orchestrator: chain all transformer layers into one Metal command buffer with a single fence at the end. No per-layer readback.
This mirrors the CUDA cuda_lane/mega_pass.rs architecture but targets
Apple Silicon via metal-rs or wgpu’s Metal backend. On non-Apple
platforms, all functions return None/false — the wgpu WGSL path
handles inference instead.
§Architecture
- Upload hidden state to device buffer (once).
- For each layer:
a. RMSNorm + QKV + RoPE (fused dispatch via
fused-qkv-ropeMSL kernel) b. KV cache write c. SDPA decode (viasdpa-decodeMSL kernel) d. O-proj GEMV + residual add e. RMSNorm + SwiGLU (fused) f. Down GEMV + residual add - Output norm + logits GEMV + argmax
- Single readback of the final token
All dispatches share one command buffer — the GPU never idles waiting for CPU between layers.
Structs§
- Metal
Pass Layer Dims - Per-layer matmul dimensions (pre-computed by caller).
- Metal
Pass Layer Weights - Per-layer weight references for the Metal mega-pass.
Functions§
- metal_
double_ buffered_ prefetch - MSL double-buffered weight streaming: overlap H2D weight copies with
compute using
MTLBlitCommandEncoderon a separate Metal command buffer. This is the Metal equivalent of CUDA’swrite_view_prefetch+join_prefetchon a secondary stream. - metal_
mega_ pass_ available - Check if the Metal mega-pass is available on this platform.
- try_
metal_ mega_ pass - Metal mega-pass: chain all transformer layers into one Metal command buffer with a single fence at the end. No per-layer readback. The hidden state stays on-device for the entire forward pass; only the final logits/token come back to host.
- warm_
metal_ context - Warm the Metal context (pre-compile kernels, pre-allocate arenas). On non-Apple platforms, this is a no-op.