Skip to content

fix(cuda): reject mid-buffer KV slices in the cuDNN decode test - #3138

Merged
inureyes merged 1 commit into
mainfrom
fix/cuda-sdpa-decode-mid-buffer-kv
Nov 16, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/cuda-sdpa-decode-mid-buffer-kv

Conversation

@inureyes

Copy link
Copy Markdown
Member

On CUDA, Llama 4 Scout decoded garbage (Item followed by U+FFFD runs) after a prompt longer than 8192 tokens, found while measuring #3133. MLXCEL_SDPA_DETERMINISTIC=1 hid it, which pointed at the cuDNN dispatch.

A chunked layer's decode window is a slice that starts partway into the cache buffer. The overlay's is_kv_cache_slice only checked that the widened view matched the allocation's size, so the window passed. unslice_kv then rebased it to the buffer start, and cuDNN attended to the keys at the front of the buffer once the window reached kv_cache_step (256). kv_cache_slice_extent already rejected a non-zero kv.offset() but called the case hypothetical. is_kv_cache_slice now carries the same check, and that comment is corrected.

Validation (GB10, sm_121)

  • Scout, 8720-token prompt, --temp 0: correct answer and identical output with MLXCEL_PREFILL_CHUNK=0 and the default chunking. Before the fix, both were garbage, and so were builds from before perf(models): fold the remaining hand-masked MLA prefills into SDPA #3114 and perf(models): run attention-sink prefill in MLX causal and window mode #3119.
  • Teacher-forced decode for 300 positions after the 8720 prefill, compared against the deterministic SDPA reference: 0/149 decided disagreements, mean NLL 1.853 vs 1.854.
  • New test chunked_decode_over_a_long_mid_buffer_window_matches_the_chunk_mask: chunk 512, f16, an 800-token prefill, then decode over a window of 289 or more keys. It fails without the fix (relative rms 1.12) and passes with it. All cache::attend tests pass on CUDA.
  • Unrelated to this change: under the plain debug profile, cache::paged_detach has two tests that trip a debug_assert at paged_detach.rs:1095, with or without this commit. They pass under test-fast.

Refs #3133

On CUDA, Llama 4 Scout decoded garbage after a prompt longer than 8192 tokens. A chunked layer's decode window is a slice that starts partway into the cache buffer. The overlay's `is_kv_cache_slice` checked only that the widened view matched the allocation's size, so the window passed, `unslice_kv` rebased it to the buffer start, and cuDNN attended to the keys at the front of the buffer once the window reached `kv_cache_step` (256). `kv_cache_slice_extent` already had the base-offset check; `is_kv_cache_slice` now has it too, and the comment that called the case hypothetical is corrected.

Validation on GB10: Scout with an 8720-token prompt answers correctly with and without prefill chunking, and a teacher-forced decode after that prefill shows 0/149 decided disagreements against the deterministic SDPA reference. The new `chunked_decode_over_a_long_mid_buffer_window_matches_the_chunk_mask` test fails without the fix (relative rms 1.12) and passes with it.

Refs #3133
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions status:review Under review priority:high High priority platform:linux Linux (CUDA / packaging) specific labels Nov 16, 2026
@inureyes
inureyes merged commit 168e804 into main Nov 16, 2026
32 checks passed
@inureyes
inureyes deleted the fix/cuda-sdpa-decode-mid-buffer-kv branch November 16, 2026 21:55
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 16, 2026
inureyes added a commit that referenced this pull request Nov 17, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Correction to the validation notes above (the merged report is fixed in #3149).

  • The reference for the teacher-forced Scout decode (0/149 decided after the 8720-token prefill; first-step NLL 21.5 vs 2.85 before the fix) was a single-pass 9216-wide prefill trace over the same text, not MLXCEL_SDPA_DETERMINISTIC=1. The prefill path does not go through the decode dispatch, so the conclusion is unchanged.
  • The earlier 8001-token run did not stay entirely clean. Its trace ended at position 8448, and its last three steps, where the new chunk's window reaches 256 keys, already show the defect: NLL 13.1 vs 4.3, 23.2 vs 10.2 and 12.4 vs 5.5. The decided filter did not count those three positions.

inureyes added a commit that referenced this pull request Nov 18, 2026
…3149)

Issue #3133 measured the 2026-11 MLA and prefill-memory changes on CUDA (GB10). Absorbed decode on CUDA takes the composed path, which on Metal had cost about 10% at 512 tokens before the fused kernel. On GB10 it decodes within run-to-run noise of the decompressed path at 512 tokens (medians +0.3 to +2.6% over three five-run cells), about 20% faster behind 2K and 2.4x behind 16K, with 0 decided disagreements in the teacher-forced trace, so #3102's switch rule holds.

`mla::default_absorbs_on` now makes the unset `MLXCEL_MLA_ABSORBED` absorb where the fused kernel runs and on CUDA; ROCm, the CPU backend and Metal with the kernel off keep the decompressed default. `kv_arch` already reads `mla::absorbed_enabled()`, so V2-Lite at 32K now estimates 972.0 MiB of KV on CUDA instead of 8.44 GiB.

Adds the GB10 results (decode, prefill peak, traces, gate) under `docs/benchmark_results/`, updates the MLA and environment docs and the CHANGELOG, and corrects the #3138 report: its Scout reference was a single-pass prefill trace, not the deterministic SDPA path, and the 8001-token run's last three steps already showed the defect.

Closes #3133
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform:linux Linux (CUDA / packaging) specific priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant