Repository navigation
feat: add fused paged-attention decode Metal kernel (gated off) - #181
Merged
Merged
Conversation
Phase 6 of #116 implements the ADR 0001 (B) fused Metal paged-attention decode kernel and benchmarks it against the (A) gather-then-SDPA path. The kernel (`src/lib/mlx-cpp/turbo/paged_attention.cpp`, JIT-compiled via `mlx::core::fast::metal_kernel`) is a split-K flash-decoding attention: one threadgroup per (batch, query head), `NumSplits` SIMD groups split the token range, and the 32 lanes of each SIMD group partition the head dimension so the per-token QK reduction is a single barrier-free `simd_sum`. It reads scattered KV blocks straight from the pool through the block table, with no gather copy. Correctness is fp32-exact. `layers::paged_decode_attention_pooled` dispatches to the kernel when `MLXCEL_PAGED_ATTENTION_NATIVE` is set (or its `use_native_paged_kernel` argument is true) and falls back to the gather reference (`paged_decode_attention_pooled_fallback`) otherwise. The two agree within RMS about 2e-8 over 200 fragmented decode steps, plus grouped-query and batched cases (`test_fused_paged_decode_*`). `PagedBlockPool::paged_decode_fused` builds the flattened block-table metadata and casts Q to f32 so the result is a drop-in for the gather path. The kernel lands gated off because it does not beat the gather path at long context on Apple Silicon. The bench (`examples/paged_attention_kernel_bench.rs`, M1 Ultra) shows it winning at 4096 tokens once decode is batched (up to 1.36x at batch 16) and scaling better with batch, but running about 0.8 to 0.9x of the gather path at 16384 tokens. On unified memory there is no host copy to avoid, and MLX folds the gather into its tuned fused SDPA, so the gather path already runs near the contiguous lower bound. ADR 0001's Phase 6 outcome section records the result and the default stays on gather. Closes #123.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…up#181) Phase 6 of lablup#116 implements the ADR 0001 (B) fused Metal paged-attention decode kernel and benchmarks it against the (A) gather-then-SDPA path. The kernel (`src/lib/mlx-cpp/turbo/paged_attention.cpp`, JIT-compiled via `mlx::core::fast::metal_kernel`) is a split-K flash-decoding attention: one threadgroup per (batch, query head), `NumSplits` SIMD groups split the token range, and the 32 lanes of each SIMD group partition the head dimension so the per-token QK reduction is a single barrier-free `simd_sum`. It reads scattered KV blocks straight from the pool through the block table, with no gather copy. Correctness is fp32-exact. `layers::paged_decode_attention_pooled` dispatches to the kernel when `MLXCEL_PAGED_ATTENTION_NATIVE` is set (or its `use_native_paged_kernel` argument is true) and falls back to the gather reference (`paged_decode_attention_pooled_fallback`) otherwise. The two agree within RMS about 2e-8 over 200 fragmented decode steps, plus grouped-query and batched cases (`test_fused_paged_decode_*`). `PagedBlockPool::paged_decode_fused` builds the flattened block-table metadata and casts Q to f32 so the result is a drop-in for the gather path. The kernel lands gated off because it does not beat the gather path at long context on Apple Silicon. The bench (`examples/paged_attention_kernel_bench.rs`, M1 Ultra) shows it winning at 4096 tokens once decode is batched (up to 1.36x at batch 16) and scaling better with batch, but running about 0.8 to 0.9x of the gather path at 16384 tokens. On unified memory there is no host copy to avoid, and MLX folds the gather into its tuned fused SDPA, so the gather path already runs near the contiguous lower bound. ADR 0001's Phase 6 outcome section records the result and the default stays on gather. Closes lablup#123.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 6 of #116 implements the ADR 0001 strategy (B) fused Metal paged-attention decode kernel and benchmarks it against the strategy (A) gather-then-SDPA path. The kernel lands gated off: it is correct and available behind a flag, but the gather path stays the default on Apple Silicon because it is faster at long context.
The kernel
src/lib/mlx-cpp/turbo/paged_attention.cpp, JIT-compiled viamlx::core::fast::metal_kernel, modeled onsparse_v_sdpa.cpp. It is a split-K flash-decoding attention:(batch, query head).NumSplitsSIMD groups split the visible token range (token parallelism).simd_sumand the weighted-V accumulator stays in registers (noacc[Dim]spill).PagedBlockPool::paged_decode_fusedbuilds the flattened block-table metadata and calls the kernel;layers::paged_decode_attention_pooledis the gated dispatcher.Correctness
test_fused_paged_decode_matches_gather_over_200_steps(single sequence, NRep=1) andtest_fused_paged_decode_gqa_and_batched(Hq>Hkv, B=2 unequal lengths) compare the fused kernel againstpaged_decode_attention_pooled_fallbackover fragmented pools. Max RMS is about 2e-8 (fp32 round-off), well inside the issue's 5e-3 bar.Throughput and why it is gated off
Bench (
examples/paged_attention_kernel_bench.rs, M1 Ultra, f16 pool, head_dim 128, 32 q-heads, 8 kv-heads, block 32). Speedup above 1 means the fused kernel is faster:The fused kernel wins at 4096 tokens once decode is batched, and scales better with batch than gather (the behaviour ADR 0001 predicted). At 16384 tokens it runs about 0.8 to 0.9x of the gather path. On unified memory there is no host copy to avoid, and MLX folds the gather's
take/reshape/transposeinto its tuned fused SDPA, so the gather path already runs near the contiguous lower bound. ADR 0001's Phase 6 outcome section records this.The native path is opt-in through
MLXCEL_PAGED_ATTENTION_NATIVE(or theuse_native_paged_kernelargument); the default path is unchanged, so this PR carries zero risk to live decode.Test plan
cargo build -p mlxcel-core+ coldcargo clippy -p mlxcel-core --lib --tests -D warnings+ example clippy +cargo fmt --all --check: cleancargo test -p mlxcel-core --lib: 843 pass (includes the 2 new fused tests; no regression)cargo run --release --example paged_attention_kernel_bench: reproduces the tableCloses #123.