Skip to content

perf(cache): reduce paged attention execution overhead - #3406

Merged
inureyes merged 4 commits into
mainfrom
fix/paged-attention-graph-budget
Nov 24, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/paged-attention-graph-budget

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Reduce fused paged-attention execution overhead by avoiding CUDA graph splits caused by counting entire pool slabs, folding Q widening into attention, and creating scale from host data. This removes standalone cast/fill kernels and an intermediate buffer; it does not establish fewer arithmetic operations or FLOPs. Dependency tracking and output byte accounting remain intact.

Adopt the full change with the owner-approved model-dependent tradeoff (GB10, 8192-token input, B=4):

Model / variant Decode change Interpretation
Qwen3 8B, full +1.16% All three rounds improve (+0.86%, +1.24%, +1.12%); all six variant samples exceed twelve controls
Qwen3 8B, budget only +0.12% Overlaps controls; no consistent gain
Qwen3 0.6B, full -1.16%, rerun -1.36% Accepted regression; no universal speedup claim

Raw measurements, binary hashes, methodology and limitations are in docs/benchmark_results/issue-3385-qwen3-8b-gb10-2026-11-24/. All 144 measured 8B requests completed 3072 tokens and matched control text hashes. Measurements use the retained pre-integration binaries, not a new performance run on latest main.

Validation and scope

Closes #3385

A B=1 decode profile of Gemma 3 1B on GB10 showed the fused paged read costing 25 more CUDA graph launches and 154 more kernels per step than the dense path.

Each fused launch registered its layer's whole pool slab, 32 MiB per side at a 64k context, as kernel inputs. That passes this device's 25 MiB per-graph byte budget, so the graph committed after every attention launch. The kernels read only the pages their page table names, so the CUDA custom-kernel overlay now leaves the inputs of the paged-attention kernels out of that budget (set_input_bytes_uncounted in the device overlay).

Each launch also cast Q to f32 and filled its scale with a kernel. The v2 partial kernel now widens Q as it stages it, as it already does for K and V, and the scale array is built from host data. Widening fp16 to f32 is exact, so the output bytes are unchanged; a new test compares an fp16 query against its f32 cast byte for byte.

Split out of the #3256 work (draft PR #3334), where the profile was taken.
Integrate current main for focused local validation of #3385. Preserve the measured optimization and its documented model-dependent tradeoff.
Preserve the 8B and small-model measurements for #3385 and document the accepted small-model regression. The adoption rationale is reduced execution overhead and the 8B gain, not reduced FLOPs.
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:medium Medium priority labels Nov 24, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 24, 2026
@inureyes
inureyes marked this pull request as ready for review November 24, 2026 15:06
@inureyes
inureyes merged commit 50ac718 into main Nov 24, 2026
27 of 33 checks passed
@inureyes
inureyes deleted the fix/paged-attention-graph-budget branch November 24, 2026 15:06
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
inureyes added a commit that referenced this pull request Dec 1, 2026
…oundary (#3334)

Part of #3259

Closes #3256

## Summary

Implements window-bounded paged sliding-window KV storage and block-boundary prompt-prefix reuse for FP16 scheduler-owned sliding-window families (Gemma 3 and Gemma 4 text/VL/Unified).

- **Window-bounded paged sliding storage (`mlxcel-core`)**: Adds `PagedLayerState.block_base`, `window`, `retired`, and `anchors`, `PagedSlidingKVCache`, and `LayerState::PagedSliding`. Sliding layers retire head blocks as `logical_start` advances, keeping live memory bounded by `ceil(window / block_size) + 1` blocks per sliding layer while retaining retired blocks up to the prompt-cache byte budget when the prompt cache is enabled.
- **Gemma 3 & Gemma 4 opt-in**: Opts FP16 Gemma 3 and Gemma 4 (text, VL, Unified) into `supports_paged_sliding_state()` while keeping quantized KV modes on `RotatingKVCache` and ring snapshots. E-series KV-shared `LayerStateKind::None` layers allocate zero blocks; Gemma 4 linear speculative rollback works over `PagedSliding` while tree gather declines cleanly.
- **Prompt-cache donation, adoption, anchoring, and eviction**: Donates retired sliding blocks with `detach_paged`, adopts the largest block boundary `d` whose sliding blocks cover `[d - window, d)` (allowing partial prefix matches even with APC off), inherits anchored retired blocks across continuation turns, protects tail-window boundary blocks from eviction, and enforces 3-tier store eviction (unanchored retired blocks -> stale whole entries -> non-root anchored retired blocks). HTTP routes render history-boundary and system-root prefixes via `HistoryBoundaryRender::SlidingAnchors`, and `/v1/cache/stats` exposes `sliding_window_evicted`, `sliding_retired_blocks`, and `sliding_retired_bytes`.
- **Fused decode paths & docs**: Serves single-row scheduler-owned sliding attention via `paged_single_decode_attention` and skips the v2 batch floor for all-windowed layers. Updates `docs/adr/0010-scheduler-owned-sequence-state.md` and `docs/CONTINUOUS_BATCHING.md`.

## Validation

- **Unit & server tests (Metal, `--profile test-fast`)**: `mlxcel-core` `cache::paged` / `paged_v2` / `cache::layer_state` (259+ passed), `gemma3` / `gemma4` / `prompt_cache` / `routes::cache` / `scheduler_paged_sliding` (558+ passed, including 32k live-block bound, eviction policy, tail-window protection, and Gemma 3/4 divergence parity with `[0, window)` negative control), `cargo fmt --all -- --check`, and `cargo clippy --lib --bins --tests -- -D warnings` all pass.
- **Real-model replay (`gemma-4-12b-it-4bit`, default 2 GiB budget, Apple Silicon Metal)**: `scripts/bench_prompt_cache_replay.py --edit-target 3` achieves **15/16 hits** (`136,000 / 175,605` cached tokens, 77.4%), reducing total TTFT from `543.79 s` cold to `127.19 s` (**4.28x speedup**), with `reject_sliding_window_evicted = 0` and `reject_oversized = 0`. Session A Turn 8 (edited Turn 3) reuses `4,544` tokens with cold-identical SHA; Sessions B and C Turn 1 reuse `4,512` system-root tokens (`3.76x–3.80x` faster, cold-identical SHAs).
- **Interleaved decode throughput (`gemma-3-1b-it-4bit`, release build, Apple Silicon Metal)**: B=1 `256` +28.68%, B=1 `8192` +6.64%, B=4 `256` +16.08%, B=4 `8192` -11.01% (2-slab gather fallback above 1024-block default slab). Full record in `docs/benchmark_results/paged-sliding-decode-apple-silicon-2026-12-02.md`.
- **Unmeasured**: `gemma-4-12b-it-4bit` B=1/B=4 interleaved decode cells and GB10 CUDA re-measurement after rebasing onto `#3406` + Wave 0 (prior GB10 CUDA measurements remain in `docs/benchmark_results/paged-sliding-decode-gb10-2026-11-23/`).
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:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: Keep CUDA graphs whole across fused paged-attention reads

1 participant