Skip to content

feat(models): add prompt-cache snapshots for qwen4_exp - #2911

Merged
inureyes merged 3 commits into
mainfrom
feature/issue-2885-qwen4-exp-prompt-snapshots
Nov 9, 2026
Merged

inureyes merged 3 commits into
mainfrom
feature/issue-2885-qwen4-exp-prompt-snapshots

Conversation

@inureyes

@inureyes inureyes commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Part A of #2885. Part B (mid-prompt checkpoints) is not implemented here; the issue scopes it to later PRs, so this PR only refs the issue.

qwen4_exp now implements the snapshot hooks. A snapshot holds the GatedDeltaNet state, PLE window and n-gram contexts, KV and the QSA index, at stored dtypes, and never the n-gram row table, so resident and external stores restore alike. Restore fails with a named error on a foreign family, layer count or kind, shape, or KV mode.

Decisions:

Validation

  • Unit tests on the tiny fixture: restore then 16 decode steps equal a cold prefill bit for bit for dense QSA, selected blocks, mid-block and EOS prompts; a mid-block multi-token suffix matches a same-split cold run bit for bit. Omitting the n-gram contexts fails the comparison. Validation, KV-mode, speculative, media-decline and row-store tests pass. Clippy and fmt pass.
  • OptiQ 2-bit through mlxcel-server: a second turn restored 4327/4363 tokens (/v1/cache/stats snapshot hit). A repeated request hit at its history boundary (4327/4332) and all 256 greedy tokens matched the cache-reset run.

Differences from the acceptance criteria

  • Omitted block keys do NOT fail the logit comparison: the next selection recomputes them to the same bits from the stored raw keys. The test asserts that, asserts the restored cache lacks them, and shows wrong block keys do fail the comparison.
  • The true second turn (hit at 4327, inside the cold plan's history piece) diverged from the cache-reset run at a near-tie after about 130 tokens. Cold runs were unchanged by --prefill-chunk-size, but the history segment is always one uncut forward, so that control did not vary the partition. This matches the partition class in docs/CONTINUOUS_BATCHING.md; it was not separately proven to be rounding alone.

Not verified: the 16K and 32K TTFT benchmarks (deferred, shared host), real external-table restore (a delegating test store stands in), and the workspace test suite. Part B criteria are not started.

Known limits: the growth gate charges a whole entry even when the insert replaces or supersedes one, so under memory pressure a turn's update can be refused and later turns reuse less; refusals appear in logs, not in /v1/cache/stats.

Measured on the OptiQ checkpoint: 240 MB per snapshot at 4327 tokens, store capacity 2.10 GB, estimate within 0.4%.

Refs #2885

Qwen3.8-Flash-Next re-read its whole history on every turn because it implemented no snapshot hooks. The family now snapshots all per-sequence state for the exact-prefix store: GatedDeltaNet state, PLE window and n-gram contexts, KV and the QSA index.

Restore validates family, layer count, layer kind, shapes and KV mode with named errors. A capture whose attention offset differs from the claimed token length is refused, restores start with an empty rollback log, and sequences with image or video MRoPE state decline the snapshot. Snapshot sizing counts the family's state, and growth of the snapshot store now re-checks the macOS memory reserve (#2883 step 4).

Refs #2885
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) modelsize:xlarge 4-bit checkpoint > 100GB; exceeds a 128GB dev box, needs bigger hardware arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) status:review Under review labels Nov 9, 2026
A snapshot with a token length but no GatedDeltaNet state or PLE window would have restored as a silently reset sequence; it now fails with a named error. Also move the snapshot-growth gate above the doc comment it had split from `whole_prompt_reuse_cap` and correct the memory doc, which still said no prompt-cache store is gated.

Refs #2885
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 9, 2026
@inureyes
inureyes marked this pull request as ready for review November 9, 2026 18:08
@inureyes
inureyes merged commit 254bf5d into main Nov 9, 2026
1 check passed
@inureyes
inureyes deleted the feature/issue-2885-qwen4-exp-prompt-snapshots branch November 9, 2026 18:09
inureyes added a commit that referenced this pull request Nov 10, 2026
## Summary

Records the timing and memory measurements that #2908, #2906, #2912, #2909, #2911 and #2918 deferred to a quiet host, in `docs/benchmark_results/qwen4-exp-deferred-measurements-m3ultra-2026-11-10.md` (Mac Studio M3 Ultra, 512 GB, `main` @ `60a3270c`).

- Native MTP (#2880, #2917): 1.30x / 1.10x / 1.76x (code / prose / enumeration) for the OptiQ 2-bit target with the bf16 split head, 1.38x / 1.00x / 1.74x for the 4-bit target with its embedded head.
- SSD expert streaming (#2882): decode 42.8 tok/s resident, 23.0 at a 24 GB arena (89.8% hit), 18.3 at 8 GB (74.9% hit). Router sync costs 8.0 s per run at both sizes, more than the I/O wait. Output identical across arms.
- External PLE (#2881): 31 GB less resident (78.5 to 47.3 GB), same TTFT at 4K and 32K, identical output in every mode.
- Prompt-cache second turn (#2885): 0.51 to 0.60 s cached vs 15.1 s (16K) and 28.6 to 28.9 s (32K) after a reset, byte-identical in 10/10 repetitions.
- macOS memory soak (#2883): 0 compressions and 0 swap-outs in every setting. A 512 GB host cannot show the eviction the issue targets.

## Script changes

- `scripts/bench_speculative.sh`: new `flashnext` and `flashnext4` pairings.
- `scripts/macos_memory_soak.sh`: send the `model` field (every request got HTTP 400 without it), and default `--out` gets a `-wire-weights` suffix under `--wire-weights` so it no longer overwrites the plain run.

## Not covered

A Mac with less memory than the checkpoint, the `prepare-expert-stream` contiguous file, and streaming combined with the external PLE table.

Refs #2880, #2881, #2882, #2883, #2885, #2917
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata modelsize:xlarge 4-bit checkpoint > 100GB; exceeds a 128GB dev box, needs bigger hardware priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant