Skip to content

perf: Test whether per-row RoPE lifts the Qwen 3.5 DFlash decline on CUDA #2191

Closed

Description

@inureyes

Problem / Background

Qwen 3.5 never takes DFlash speculative decoding on CUDA by default. PR #1944 (d8020beb, closing #1935) made Qwen35Model::probe_block_chain_exactness return NotRun for head_dim 256 or 288 while MLXCEL_SDPA_VECTOR_LARGE_D is on, because on GB10 the served width 2 and 4 greedy completions on qwen3.5-4b-4bit + qwen3.5-4b-dflash part from classic decode at generated token 105, and the probe could not see it. The burst is worth 1.18x to 1.22x over classic on that pairing (docs/benchmark_results/dflash-sdpav-options-qwen35-gb10-2026-09-21.md), all of it forfeited today.

PR #2185 (#2160, 97f35bca) found that Gemma 4's equivalent CUDA failure was not the attention kernel alone: MLX's CUDA RoPE runs rope_single for B=1, L=1 and the general rope kernel otherwise, and the two differ in the last float bit, so a width-B verify block cannot be byte-identical to single-token decode. It fixed that by rotating Q and K one row at a time (TECHNICAL_REPORTS/pr-2185-gemma4-mtp-cuda-verify.en.md, Follow-up: "Qwen 3.5 DFlash's CUDA residual (#1935) may be the same RoPE kernel choice; untested here"). This issue tests that, and either fixes it or records the negative result.

Current Behavior

  • The Qwen 3.5 full-attention verify forward is Qwen3NextAttention::forward_hidden_with_position_ids_verify (src/models/qwen3_next.rs:679). Attention is already per row (attend_per_position, :811), so the sdpa_vector half of fix(gemma4): make MTP verify byte-identical to decode on CUDA #2185 is already done here. RoPE is not: fast_rope rotates the whole [B, H, L, D] block for Q and K at once (:750-761), and the block-rotated K is then written into the KV cache by update_and_fetch (:764). If the two kernels differ, the difference persists in the cache for every accepted token, which fits the residual's shape (first differing row 36 tokens past the prompt, first differing layer 15, a full-attention layer, position-dependent rather than round-dependent).
  • The decline is Qwen35Model::cuda_sdpa_vector_verify_hazard (src/models/qwen3_5.rs:1635-1662), called first in probe_block_chain_exactness (:1664-1691). Widths 8 and 16 also decline for a separate, real reason (MLX's M * B < 8 switch from qmv to qmm_sm80), which the probe observes and per-row RoPE will not change.
  • The evidence behind fix(speculative): decline the Qwen 3.5 DFlash burst the probe cannot vet #1944 does not exclude RoPE. docs/benchmark_results/dflash-width-2-4-residual-qwen35-gb10-2026-09-21.md:73-75,105 says the per-row Q/K/V tensors have "the same values in both paths", but that was inferred from copy predicates and a slice-copy A/B, not from capturing post-RoPE Q and K. Its in-process LARGE_D=0 result (1 of 201 disagreements to 0) counts argmaxes, not logit bytes, and the record notes an unexplained 0/201 vs 1/201 discrepancy across sessions.
  • qwen3.5-4b-4bit loads as LoadedModel::Qwen35VLM and its config declares rope_parameters.mrope_interleaved: true, partial_rotary_factor: 0.25, head_dim: 256. src/models/qwen3_5.rs:2121 says text-only requests take fast_rope (position_ids = None), but which branch the served verify actually takes has to be confirmed, because the mrope branch (qwen3_next.rs:723-749) uses cos/sin multiplies and no RoPE kernel at all.

Proposed Solution

Step 1, attribution (GB10, real checkpoints /home/inureyes/models/mlx/qwen3.5-4b-4bit and qwen3.5-4b-dflash).

  1. Confirm which RoPE branch the verify block and classic decode take on this checkpoint (a temporary log or capture in forward_hidden_with_position_ids_verify).
  2. Op-level check: fast_rope on a [1, 16, 4, 256] bf16 tensor (and the K shape [1, 4, 4, 256]) with dims 64, traditional false, base 1e7, scale 1.0, against four L=1 calls at offset + row, compared in bytes, over several offsets (include ~194) and random draws.
  3. Model-level capture, the fix(gemma4): make MTP verify byte-identical to decode on CUDA #2185 method: a temporary capture of post-RoPE Q and K inside the attention layer, comparing verify row r against the decode step at the same absolute position, at every full-attention layer, over the first ~60 positions past the perf(speculative): the default draft block width of 16 loses on GB10 #1797 harness's 158-token prompt. Drive it from the existing block_versus_chain_byte_bisect_on_the_real_transcript diagnostic (src/models/qwen3_5_dflash_probe_tests.rs:974; env MLXCEL_Q35_PROBE_PROMPT, MLXCEL_Q35_PROBE_REFERENCE, MLXCEL_Q35_PROBE_BLOCK, MLXCEL_Q35_PROBE_ACCEPTS) and record the first differing sub-op (post-RoPE Q, post-RoPE K, attention output, o_proj, MLP).
  4. Attribution matrix in logit bytes, not argmaxes, n >= 3 repeats per cell: per-row RoPE experiment {off, on} x MLXCEL_SDPA_VECTOR_LARGE_D {1, 0}, at widths 2 and 4. Add one cell checking that the dense MLP activation (compiled_silu, qwen3_next.rs:909) runs the same graph at M=1 and M>1; fix(gemma4): make MTP verify byte-identical to decode on CUDA #2185's compiled-vs-eager cause has no visible switch here, so this is a check, not a hypothesis. Do not rerun the gated-delta chain-parity control; fix(speculative): prefill the Qwen 3.5 DFlash burst like classic decode #1939 excluded it.

Decision rule: if post-RoPE K bytes differ between block and decode and the per-row RoPE cell with LARGE_D=1 makes the bisect report zero differing rows, RoPE is the cause; go to Step 2. If post-RoPE Q/K are byte-identical, or per-row RoPE leaves the divergence in place, go to Step 3.

Step 2, fix (only if Step 1 implicates RoPE).

  • In forward_hidden_with_position_ids_verify, on the fast_rope branch only, when target_verify && l > 1 && b == 1 on CUDA, rotate Q and K one row at a time: slice row r along axis 2, fast_rope(..., offset + r), concatenate_many along axis 2. Mirror Attention::head_rows_like_decode (src/models/gemma4_attention.rs:163); do not call it (it is an inherent method on Gemma's Attention). Read mlxcel_core::cuda_is_available() once at construction into a field, as Gemma's mtp_row_rope does (src/models/gemma4.rs:1704), not per call.
  • Make the probe able to see the hazard before trusting it. The shipped probe compares one block right after prefill and reported byte-identity at widths 2 and 4 at every prompt length from 8 to 512 while the served path diverged. Add a long draw to probe_block_chain_exactness that walks blocks and the chain together for at least 64 positions past the prompt, following Gemma 4's long buffered draw (mtp_probe_prompt_lengths, src/models/gemma4.rs:290-296). Then delete the cuda_sdpa_vector_verify_hazard decline.
  • Widths 8 and 16 must still decline through the probe's measured verdict. Do not change the MLXCEL_MTP_ALLOW_INEXACT override or mtp_exactness_gate.

Step 3, negative result. Leave the decline in place, add a record under docs/benchmark_results/ with the matrix and captures, and comment on #1935 with the result and the first differing sub-op.

Scope

In scope: src/models/qwen3_next.rs (verify RoPE), src/models/qwen3_5.rs (probe long draw, hazard removal), src/models/qwen3_5_dflash_probe_tests.rs (capture diagnostics), tests/speculative_parity.rs (greedy_parity_dflash_qwen35_4b at :740, assert_server_byte_equality_at_widths at :305), a record under docs/benchmark_results/, and a technical report under TECHNICAL_REPORTS/ for the PR.

Out of scope: patching MLX's rope.cu (it would move classic decode numerics for every CUDA model, as #2185 notes); the mrope/image branch; Qwen 3.5 MTP on CUDA (mtp_exactness_allows requires Metal); the width 8/16 qmm_sm80 decline; the default draft width (#1797).

Acceptance Criteria

  • The attribution is recorded under docs/benchmark_results/ with the RoPE-branch confirmation, the op-level result, the first differing sub-op from the capture, and the full matrix with n >= 3 per cell in logit bytes.
  • If fixed: a test in qwen3_5_dflash_probe_tests.rs shows the new long probe draw returns a divergent verdict at width 2 or 4 with the fix reverted and the decline bypassed, and Equal with the fix. Without this a passing probe proves nothing, because the old probe also passed.
  • If fixed: on GB10 with no env overrides, mlxcel-server --draft-kind dflash logs a passing probe and completed bursts at widths 2 and 4, and greedy /v1/completions output at temperature 0 is byte-identical to the drafter-less server on three prompts (the chat prompt in the test returns empty content on this checkpoint, so it does not count).
  • If fixed: greedy_parity_dflash_qwen35_4b requires a completed burst and byte-equal completions at widths 2 and 4, and accepts a decline at 8 and 16 only with the probe's divergent-verdict reason.
  • If fixed: throughput at the width main resolves for this pairing on GB10, against classic, measured with docs/benchmark_results/data/dflash-sdpav-options-gb10-2026-09-21/harness/price_options.py in n >= 3 interleaved rounds with a classic-vs-classic null arm; report paired deltas and treat any gap inside the null arm's spread as unresolved.
  • Metal is unchanged by construction (CUDA-gated). If any shared code path changes, list it in test: Apple Silicon verification pass for 2026-10-06 CUDA-only changes #2158.
  • If not fixed: the decline stays, and fix(speculative): Qwen 3.5 DFlash verify diverges from classic decode #1935 has a comment with the evidence. (Not applicable: fixed in perf(qwen35): byte-identical CUDA DFlash verify via per-row RoPE #2207; fix(speculative): Qwen 3.5 DFlash verify diverges from classic decode #1935 has a comment with the evidence anyway.)

Verification

cargo build --release --features cuda
cargo test --release --features cuda -p mlxcel --lib -- --ignored --test-threads=1 --nocapture models::qwen3_5::qwen3_5_dflash_probe_tests
cargo test --release --features cuda --test speculative_parity -- --ignored greedy_parity_dflash_qwen35_4b --test-threads=1 --nocapture
cargo clippy -p mlxcel --lib --tests --features cuda -- -D warnings
cargo fmt --check

--test-threads=1 is required on CUDA. Run GPU work under gpu-lock run --tag dflash-qwen35 -- ... and build outside the lock. Record driver, kernel and MLX_CUDA_ARCHITECTURES in the record.

Related: #1935, #1944, #2160, #2185, #675, #1797, #2158.

Activity

  1. added
    type:performance
    priority:medium
    area:inference
    platform:linux
    status:ready
    status:in-progress
    and removed
    status:ready
    on Oct 6, 2026
  2. added
    status:review
    and removed
    status:in-progress
    on Oct 7, 2026
  3. inureyes commented on Oct 7, 2026

    @inureyes
    MemberAuthor

    Findings for PR #2207. RoPE is the cause, so the fix is in.

    • Step 1.1: both classic decode and the verify block take the fast_rope branch on qwen3.5-4b-4bit. Classic runs 96 decode calls at l=1. The verify block runs 32 calls at l=4 and 8 at l=2. No call took MRoPE. A temporary log on the served path showed this.
    • Step 1.2: at op level, fast_rope on a block against decode-shaped rows differs in 1 byte across 144 cases. Rotating row by row gives 0. compiled_silu matches the per-row result exactly (0 bytes).
    • Step 1.3: across 200 kept rows, the first differing sub-op is post-RoPE Q in 68 rows and post-RoPE K in 1. In the other 4 rows it is the attention output, and each of those comes after a K difference was already in the cache. o_proj and the MLP never differ first.
    • Step 1.4: I ran the matrix over logit bytes, n=3 per cell, and every cell gave the same result on each run. With block RoPE, 67 of 200 rows differ at LARGE_D=1 and 129 at LARGE_D=0, at both width 2 and width 4. With per-row RoPE, 0 rows differ in all four cells. The decision rule therefore points to RoPE.

    The new long-draw test lives in qwen3_5_rope_attribution_tests.rs rather than qwen3_5_dflash_probe_tests.rs, which is already over 1000 lines. With the fix reverted, the test diverges at walk position 19; with it applied, both widths return Equal. Record: docs/benchmark_results/qwen35-dflash-verify-rope-gb10-2026-10-07.md.

  4. added
    status:done
    and removed
    status:review
    on Oct 7, 2026
  5. added a commit that references this issue on Oct 7, 2026
    05dda10
  6. 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:inferenceplatform:linuxpriority:mediumstatus:donetype:performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions