Skip to content

fix(test): bound iquestloop chunked prefill by measured bf16 noise - #2418

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-2143-iquest-chunked-prefill-bound
Oct 16, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-2143-iquest-chunked-prefill-bound

Conversation

@inureyes

@inureyes inureyes commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Closes #2143

Root cause: precision, not chunk-boundary logic

The chunked and single-pass routes use the same keys, offsets and masks; they round differently in bf16. Measured on this GB10 (~/models/mlx/iquest-coder-v1-40b-loop-instruct-4bit, --test-threads=1, deterministic across runs and identical with and without MLXCEL_SDPA_DETERMINISTIC=1) with a temporary, uncommitted patch that promotes every bf16 tensor (1445 of them) to f32 at load:

run chunked vs single-pass, full 76 800-logit row mlxcel vs f32 oracle, 218-token top-5
bf16 (as shipped) top-5 worst 0.000 / 0.500 / 0.375 / 0.625 for chunk 128 / 97 / 64 / 32, row max up to 1.23 1.65 to 1.87, ranks 2 and 3 swapped
f32, TF32 on row max at most 0.018 about 0.037
f32, MLX_ENABLE_TF32=0 row max at most 2.1e-05 (chunk 1: 5e-04) 4.7e-04, oracle rank order (38-token prompt: 1.3e-04)

Chunks 63 and 65 behave the same. The 1.7 to 1.8 oracle deltas and the rank swap that the issue pairs with this failure also come from bf16 activations. The 4-bit weights are not the cause, since the f32 run uses them too. Neither bf16 route is more accurate: KL(f32 || bf16 single) is 0.0047, and the chunked rows sit at 0.0041 to 0.0071.

The investigation comment on #2143 names the same mechanisms: short chunks take a different SDPA route, and the keys sit at different block offsets. Its proposed corrections (scoped cuDNN dispatch, alignment-preserving K/V padding) would narrow a precision gap, not fix a logic error, so they are left for a follow-up.

Change (tests only, no production change)

  • check_chunked_prefill measures all four chunk sizes before asserting and reports every failure. It keeps the greedy token and the TIE_GAP-separated order, and bounds the top-5 delta at 1.0 (measured worst 0.625) and KL(single || chunked) at 0.015 (measured worst 0.0074). The doc comment records the measurement table.
  • New chunked_prefill_matches_single_pass_past_the_window (synthetic, f32): 218 tokens, window 64, chunks 128/97/65/64/63/32/1, plus 100, then 30 single tokens, then 88 (a multi-token append after the ring has wrapped). It compares every position against a 1e-4 bound (clean max 5.4e-07). The existing 64+32 test checks only the last row and missed a one-key cache slip.
  • Both checks assert that every logit is finite first, because f32::max and the bound comparisons skip NaN, which is this seam's historical failure (fix(models): gemma4 sliding-window single-pass prefill degenerates to <pad> beyond the window #401/fix: harden sliding-window >window prefill across non-Gemma models; guard causal_attention windowed K/V slice #408).
  • Docs that blamed f16 and a quantized lm_head now name bf16.

What the bf16 bound can and cannot see (fault injection in forward_pass2 on continuation chunks)

fault real checkpoint, bf16 new f32 test
cache keeps one key too few passes (KL at most 0.0031) fails, 6.4e-03 at position 128
pass-2 RoPE offset minus 1 passes (KL at most 0.0015) fails, 7.2e-02
pass-2 rotated from position 0 fails on KL (0.024 to 0.030) not run

Validation

  • Original test on main fails as the issue reports. The new test passes in 43 s: gpu-lock run -- target/test-fast/deps/iquestloopcoder_parity-<hash> --test-threads=1, run from $HOME so models/mlx/... resolves.
  • models::iquestloopcoder_tests passes 18/18. Clippy (--lib --tests -D warnings), fmt and the five contract checks pass.
  • Not run: Metal, and the full make verify-test-cuda workspace gate (left to the merge gate).

@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata platform:linux Linux (CUDA / packaging) specific status:review Under review status:done Completed and removed status:review Under review labels Oct 16, 2026
check_chunked_prefill failed on GB10 at chunk 97 against a 0.25 single-pass bound. The divergence is bf16 precision, not a chunk-boundary bug: with every floating tensor promoted to f32 at load and MLX_ENABLE_TF32=0, chunk sizes 128/97/64/32 land within 2.1e-05 of the single-pass row, and mlxcel matches the f32 oracle to 4.7e-04 (the 1.7 to 1.9 bf16 oracle deltas and the rank 2/3 swap disappear).

The real-checkpoint check now measures every chunk size before asserting, holds the greedy token and TIE_GAP-separated order, and bounds the top-5 logit delta at 1.0 (measured worst 0.625) and KL(single || chunked) at 0.015 (measured worst 0.0074). A new f32 synthetic test, chunked_prefill_matches_single_pass_past_the_window, compares every position at the 218-token, window-64 geometry and fails on a one-key cache slip and a one-position pass-2 RoPE slip, which stay inside bf16 noise on the checkpoint. Doc comments that blamed a quantized lm_head and f16 now name bf16 activations.

Closes #2143
f32::max and the > comparisons skip NaN, so both chunked-prefill checks could pass on NaN rows, the failure this seam had before (#401/#408). Both now assert every logit is finite before comparing.

The f32 synthetic test gains a 100 + 30 x 1 + 88 schedule, so a multi-token chunk is appended after single-token decode has wrapped the ring. Its docs no longer claim that a multi-token tail sees a wrapped ring. The parity file shares one top-k helper, and its docs now agree on the 1.87 oracle deviation and give the chunked routes' own distance from f32 (1.87 to 2.33).

Refs #2143
@inureyes
inureyes force-pushed the fix/issue-2143-iquest-chunked-prefill-bound branch from f8f9c1f to 58b51d0 Compare October 16, 2026 01:52
@inureyes
inureyes merged commit bf4cd6b into main Oct 16, 2026
26 of 27 checks passed
@inureyes
inureyes deleted the fix/issue-2143-iquest-chunked-prefill-bound branch October 16, 2026 02:49
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata platform:linux Linux (CUDA / packaging) specific priority:medium Medium 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.

fix(cuda): iquestloopcoder chunked prefill disagrees with single-pass on GB10

1 participant