Skip to content

feat: add fused paged-attention decode Metal kernel (gated off) - #181

Merged
inureyes merged 1 commit into
mainfrom
feat/123-fused-paged-attention-kernel
Jun 8, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/123-fused-paged-attention-kernel

Conversation

@inureyes

@inureyes inureyes commented Jun 8, 2026

Copy link
Copy Markdown
Member

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 via mlx::core::fast::metal_kernel, modeled on sparse_v_sdpa.cpp. It is a split-K flash-decoding attention:

  • One threadgroup per (batch, query head).
  • NumSplits SIMD groups split the visible token range (token parallelism).
  • The 32 lanes of each SIMD group partition the head dimension, so the per-token QK dot product is a single barrier-free simd_sum and the weighted-V accumulator stays in registers (no acc[Dim] spill).
  • It reads scattered KV blocks straight from the pool through the block table, with no gather copy.

PagedBlockPool::paged_decode_fused builds the flattened block-table metadata and calls the kernel; layers::paged_decode_attention_pooled is the gated dispatcher.

Correctness

test_fused_paged_decode_matches_gather_over_200_steps (single sequence, NRep=1) and test_fused_paged_decode_gqa_and_batched (Hq>Hkv, B=2 unequal lengths) compare the fused kernel against paged_decode_attention_pooled_fallback over 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:

ctx b=1 b=4 b=8 b=16
4096 0.50x 1.30x 1.01x 1.36x
16384 0.39x 0.83x 0.82x 0.91x

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/transpose into 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 the use_native_paged_kernel argument); the default path is unchanged, so this PR carries zero risk to live decode.

Test plan

  • cargo build -p mlxcel-core + cold cargo clippy -p mlxcel-core --lib --tests -D warnings + example clippy + cargo fmt --all --check: clean
  • cargo 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 table

Closes #123.

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.
@inureyes inureyes added the type:enhancement New features, capabilities, or significant additions label Jun 8, 2026
@inureyes
inureyes merged commit ffb99bd into main Jun 8, 2026
4 checks passed
@inureyes
inureyes deleted the feat/123-fused-paged-attention-kernel branch June 8, 2026 00:55
@inureyes inureyes self-assigned this Jun 10, 2026
@inureyes inureyes added status:done Completed priority:medium Medium priority labels Jun 10, 2026
@inureyes inureyes added this to the 0.2 milestone Jun 21, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Phase 6: Fused Metal paged-attention kernel

1 participant