Skip to content

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

Closed

Description

@inureyes

Problem

On GB10 (sm_121, --features cuda, test-fast profile, --test-threads=1), iquestloopcoder_matches_the_float32_oracle fails in check_chunked_prefill (tests/iquestloopcoder_parity.rs:350-382), at the hard-coded 0.25 logit bound at line 376:

chunk 97: logit for token 5123 is 29.5, single-pass says 29.125

The failure is identical at the #1873 baseline 56d82fbc and at main a4974fab; neither the test nor src/models/iquestloopcoder*.rs changed between them, so it predates the ROCm routing work. It also fails with MLX_ENABLE_TF32=0, so it is not the TF32 class. Metal status is unknown.

The delta is 3 bf16 ulps at that magnitude, so it may be accumulated precision between chunked and single-pass prefill rather than a logic bug, but chunk 97 straddles the 64-token loop_window_size boundary, which is exactly where the pass-2 RotatingKVCache (kept + L keys) and the windowed mask from mlxcel_core::causal_attention (src/models/iquestloopcoder.rs:451-466) must agree. The earlier oracle sections pass, but their logged deltas grow past the window: up to 0.41 on the short prompt, about 1.7 to 1.8 on the 218-token prompt, with the 2nd and 3rd ranked tokens swapped against the f32 oracle. Look at both together.

Approach

Determine which it is: compare chunked against single-pass per layer (or run both in f32) for chunk sizes 128/97/64/32. A divergence that disappears in f32 is precision; one that persists, or that appears first at the window boundary, is a mask or cache-offset bug to fix in src/models/iquestloopcoder.rs.

Acceptance Criteria

  • Root cause identified and stated in the PR: chunk-boundary logic or precision.
  • Either chunked prefill is fixed to agree with single-pass within the test's tolerance on CUDA, or, if precision only, the tolerance is widened to a measured bound with the measurement and reason in the PR.
  • iquestloopcoder_parity passes under make verify-test-cuda on GB10.

Reproduce

cargo test -p mlxcel --profile test-fast --features cuda --test iquestloopcoder_parity --no-run
gpu-lock run --tag iquest-parity -- target/test-fast/deps/iquestloopcoder_parity-<hash> --test-threads=1

Refs #1873. Port issue: #1360.

Activity

  1. added
    status:ready
    type:bug
    priority:medium
    area:models
    platform:linux
    on Oct 6, 2026
  2. Jaeyeong-CHOI commented on Oct 7, 2026

    @Jaeyeong-CHOI
    Contributor

    Investigation: attention dispatch and local-KV alignment, rather than missing window tokens

    I investigated the 218-token single-pass versus 97 + 97 + 24 chunked case on DGX Spark GB10 (sm_121), using an instrumented checkout based on cb83045220de3fa3ead28d33447e9ffa66ba0ccf. The attention inputs are BF16, head dimension 128, local window 64; the alignment experiments used cuDNN 9.13.0 / frontend 1.16.0.

    Two distinct sources of numerical disagreement were isolated. This is an investigation report and proposed correction strategy, not a validated production fix.

    1. Small-chunk dispatch changes the numerical path

    In the first attention layer, the corresponding Q/K/V values agree, but the final 24-query chunk takes the unfused fallback while the larger calls use cuDNN. The existing small-masked-append routing is in supports_sdpa_cudnn.

    Replaying the same captured Q/K/V, without model execution or cache updates:

    • Original routing: 34,880 differing attention-output elements.
    • Routing the last chunk through cuDNN as well: 0.
    • Restoring the original routing: 34,880 again.

    The original-output replay gates passed byte-for-byte. Disabling cuDNN globally is not an equivalent fix: the fallback-only comparison still had 15 differing elements, with differences observed in its QK/probability/PV stages.

    At whole-model level, route unification reduced the maximum full-versus-chunk difference across the final token's 76,800 logits from 0.625 to 0.1875 in this case, but did not eliminate it. This does not establish general output quality or complete model equivalence.

    2. Cropping local KV changes physical key alignment, not the valid token set

    After route unification, the first remaining traced divergence is pass 2, layer 0, local attention. Both layouts contain exactly the same valid 64-token windows and matching Q/K/V values. The two continuation chunks differ in 8,854 + 846 = 9,700 output elements.

    The following controls distinguish length from placement:

    • Nine tested length-only changes, with active-token placement held fixed, left the outputs unchanged.
    • Moving the same active keys and their mask at fixed tensor length reproduced the original 8,854 / 846 differences.
    • Retaining the masked prefix reproduced the corresponding full-input outputs exactly.
    • In the tail capture, offsets 0/64/128, 2/130, 3/131, and 32/96 formed exact-output groups. At fixed length 218, three masked dummy prefix slots reproduced the original placement's output; retaining all 131 real prefix tokens was unnecessary.
    • Eight controls that actually changed finite masked-padding values left the outputs unchanged.

    Thus, in these captures, physical KV placement is causal; length alone and loss of required cache tokens do not explain the difference. Right-padding alone does not restore the original alignment.

    3. Strongly supported arithmetic explanation, with a defined limit

    A forward, 64-key-block online-softmax model, rounding the unnormalized exponentials to BF16 before the value multiplication while retaining the pre-quantization denominator, reproduced all 15,019 observed basis-V diagnostic output changes, including locations and signed deltas, with no extra changed locations. An independent CPU implementation agreed.

    The same fixed model matched 1,239,008 of 1,239,040 original-V output elements. Removing only that intermediate BF16 rounding in the CPU model removed the tested placement-dependent differences.

    This supports the mechanism: cropping changes block boundaries and running maxima; intermediate BF16 rounding makes the otherwise equivalent blockwise softmax computations numerically non-equivalent.

    However, this is not an instruction-exact cuDNN reconstruction. The model was selected using the basis-V observations, not in a blind experiment. Those diagnostic outputs are not direct dumps of the original fused kernel's probability tensor. The remaining 32 elements are CPU-model-versus-cuDNN residuals, not the original 9,700 cropping differences; exact internal instructions and bitwise normalization statistics remain unresolved. Also, 64 is observed for this configuration, not a universal cuDNN tile-size contract.

    Across 5,200 same-condition repeats in the earlier captured-input tests, no repeat-to-repeat output variation was observed. The evidence therefore supports path/layout-dependent precision here, rather than establishing the same failure as cuDNN #1412. This finite sample does not prove universal determinism.

    Proposed correction and PR scope

    1. Keep the IQuest prefill numerical path consistent across chunk sizes. Prefer an explicit, scoped dispatch policy over removing the small-query performance threshold globally. Preserve existing backend eligibility and decode safeguards.
    2. Prototype an alignment-preserving local-attention view without changing the shared rotating cache. For a chronological multi-query KV view after append, let S = cache.offset - returned_kv_len; prepend S % B finite zero K/V slots and mask them out, with experimentally validated B = 64 here. Apply this at forward_pass2. Keep stored cache contents, offsets, RoPE, and the valid key set unchanged. This requires at most 63 extra slots, although concatenation/copy costs still need measurement. The minimal-size padded view and the combined whole-model change have not yet been validated on GPU; this is a candidate, not a ready-to-merge patch.
    3. Handle a one-token final prefill chunk explicitly. The current causal_attention single-query branch slices over-window KV again, removing such padding; a wrapped rotating cache may also return physical ring order. The multi-query formula cannot simply be applied to decode or a one-token tail.
    4. Compare an FP32-intermediate accuracy path as an alternative. This means controlling QK, softmax, and PV intermediates, not merely casting the final output or making softmax alone FP32. It avoids depending on this observed BF16 rounding mechanism but has performance/memory costs and does not itself guarantee bitwise equality across every shape.

    Before proposing a fix PR, I would require captured-input regression tests, correction/revert controls, chunk sizes around the 32-query dispatch and 64-key boundaries, one-token tails, append-after-ring-wrap coverage, full-model logits and continued decoding, and latency/peak-memory measurements. I would keep the existing tolerance unchanged during those comparisons rather than treating a relaxed bound as proof of a correction.

    The investigation and independent numerical checks were AI-assisted. No combined production correction or performance improvement is claimed by this report.

  3. added
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Oct 16, 2026
  4. added
    status:done
    and removed
    status:review
    on Oct 16, 2026
  5. added 2 commits that reference this issue on Oct 16, 2026
    8413adb
    58b51d0
  6. added a commit that references this issue on Oct 18, 2026
    bf4cd6b
  7. added this to the 0.8 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:modelsplatform:linuxpriority:mediumstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions