Skip to content

fix(models): continue Qwen3-Next cache across multi-token forwards - #2493

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-2463-qwen3-next-cache-forward
Oct 20, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-2463-qwen3-next-cache-forward

Conversation

@inureyes

Copy link
Copy Markdown
Member

One T=5 forward behind a populated Qwen3-Next cache ran from an empty state: forward_for_sequence reset the model-owned GatedDeltaNet and KV caches on every multi-token no-sequence-id call, while five T=1 forwards behind the same cache continued it. On the real Qwen3-Next-80B-A3B-4bit checkpoint the divergent width-5 trace scored ppl 987 against 27 (width 1) and 24 (width 256), which is issue #2463.

The reset block is removed so every multi-token forward continues the populated state, and the supports_chunked_prefill() == false override added by #2295 is dropped so the family uses the trait default again (chunked prefill keeps state once the reset is gone). Every language-model entry routes through the fixed forward_for_sequence, including the speculative verify path (forward_with_position_ids_verify and forward_hidden_with_position_ids_verify, qwen3_next.rs lines 641 and 686 on main), whose KV cache comes from the model-owned sequence state.

Regression tests in src/models/qwen3_next_tests.rs:

  • qwen3_next_multi_token_forward_behind_cache_matches_stepwise prefills 12 tokens then requires one T=5 forward to match five T=1 forwards from the same cache state within 2e-3; on current main it fails at position 0 with divergence 0.025385085, on this branch it passes.
  • qwen3_next_multi_token_state_update_behind_cache_matches_stepwise pins the same equality through the inherent model forward with explicit causal masks.
  • qwen3_next_loaded_model_chunking_contract_is_safe now asserts the trait default and that a no-ID prompt continues instead of reset.

Trace on models/mlx/qwen3-next-80b-a3b-instruct-4bit with docs/architecture.md as corpus, MLX_ENABLE_TF32=0:

Width main (reverted arm) this branch
1 27.140 27.140 (byte-identical)
5 987.424 18.678
256 24.059 24.059 (byte-identical)

All 16 qwen3_next and 11 gated_delta tests pass with --test-threads=1 under the test-fast cuda profile; fmt and clippy -D warnings clean.

Closes #2463

Qwen3NextModel::forward_for_sequence reset the model-owned GatedDelta and KV state on every multi-token forward that carried no sequence id, so one multi-token forward behind a populated cache ran from an empty state while the same tokens forwarded one at a time behind it continued the cache. This is the shape speculative-decode verification uses, and teacher-forced traces on Qwen3-Next-80B-A3B-4bit showed width 5 behind 512 tokens of context reading perplexity 980 while widths 1 and 256 read 27 and 24.

Drop the reset so a no-ID multi-token forward continues the fallback cache, matching the Qwen3.5 model, and remove the no-ID chunked-prefill opt-out #2295 added while the reset existed. Callers that start a new prompt reset through reset_runtime_state (the logit-trace and perplexity harnesses already do); server requests isolate per sequence through SequenceId.

Validated with a synthetic eight-layer Qwen3-Next test that compares one T=5 forward against five T=1 forwards from the same post-prefill state (fails on main, passes here), and with fixed-versus-reverted traces on the real checkpoint: width 5 gives ppl 18.68 (was 980.26) and widths 1 and 256 are unchanged at 27.140 and 24.059.

Refs #2463
@inureyes inureyes added status:review Under review area:models Model architectures, weights, loading, metadata priority:high High priority type:bug Bug fixes, error corrections, or issue resolutions labels Oct 20, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review of the state-isolation change (cross-request state leaks and chunked-prefill cost). No CRITICAL or HIGH findings; the diff is clean in both areas.

State isolation: the removed reset was gated on seq_id.is_none(), so it never applied to any server path. Every server entry point passes a per-sequence id (decode Engine::forward_rows, prefill Engine::prefill/score, the speculative burst, engine-side verify), and isolation is structural: Engine::open overwrites the model-owned slot with fresh state via prepare_sequence_state, Engine::close releases it, and with_or_create_sequence_state(Some(id)) never falls back to the shared internal slot. The fallback cache's only multi-token production caller is the deprecated classic speculative walk (CLI, one generation per process). No cross-request leak is reachable through the removed reset.

Performance: the mask offset (first full-attention layer) is valid for all full-attention layers, which advance identically per forward and are never trimmed on model-owned layouts; create_causal_mask is O(chunk^2) transient bounded by the 2048/512 chunk policy; GatedDeltaCache state is fixed-size with no per-chunk reallocation. Chunked prefill restores chunk-bounded transient memory at equal total lm-head work.

Validation: full-workspace gate on 2ff6601 green (161 test binaries, 9375 passed / 0 failed including the three qwen3_next tests).

Convert the issue-2463 chunked-prefill decision note orphaned onto `eos_token_ids` when the `supports_chunked_prefill` override was deleted from a rustdoc comment into a plain `//` comment, and correct the `hybrid_weights` fixture comment's `in_proj_qkvz` width formula to `[2*nk*dk + 2*nv*dv, hidden]` (q, k, v, z per k-head group, 48 rows for the fixture), matching the `[48, 16]` tensor. The docs sweep found no stale chunked-prefill claim to update. Validation: cargo fmt --all -- --check, cargo clippy -p mlxcel --profile test-fast --features cuda -- -D warnings, and the 16 qwen3_next lib tests pass under gpu-lock with MLX_ENABLE_TF32=0 and --test-threads=1.

Refs #2463
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 20, 2026
@inureyes
inureyes merged commit 6a11591 into main Oct 20, 2026
27 checks passed
@inureyes
inureyes deleted the fix/issue-2463-qwen3-next-cache-forward branch October 20, 2026 10:20
@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 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.

fix(models): Qwen3-Next multi-token forward behind a cache is wrong

1 participant