Repository navigation
fix(models): continue Qwen3-Next cache across multi-token forwards - #2493
Conversation
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
|
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 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; 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
One T=5 forward behind a populated Qwen3-Next cache ran from an empty state:
forward_for_sequencereset 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() == falseoverride 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 fixedforward_for_sequence, including the speculative verify path (forward_with_position_ids_verifyandforward_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_stepwiseprefills 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_stepwisepins the same equality through the inherent model forward with explicit causal masks.qwen3_next_loaded_model_chunking_contract_is_safenow asserts the trait default and that a no-ID prompt continues instead of reset.Trace on
models/mlx/qwen3-next-80b-a3b-instruct-4bitwithdocs/architecture.mdas corpus,MLX_ENABLE_TF32=0:All 16 qwen3_next and 11 gated_delta tests pass with
--test-threads=1under the test-fast cuda profile; fmt and clippy -D warnings clean.Closes #2463