Skip to content

test(prompt-cache): pin model-aware snapshot sizing for attention caches - #2129

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-1761-snapshot-sizing-model-aware
Oct 6, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-1761-snapshot-sizing-model-aware

Conversation

@inureyes

@inureyes inureyes commented Oct 6, 2026

Copy link
Copy Markdown
Member

Summary

The code defects named in #1761 already landed in #1978 (d7279c52): gemma3, llama4 and afmoe are in the model-aware family list, and model_types() strips _text. This PR adds what the issue still required: tests built from the geometry the real checkpoints carry (including the gemma4_unified_text and muse_glimmer_text spellings), the real-checkpoint measurement of the default memory change, and the policy wording in docs/environment-variables.md. The sizing module's tests move to snapshot_sizing_tests.rs (the file was 643 lines). No runtime code changes, so this does not touch admission or restore.

Default policy

capacity = max(512 MiB, min(6 * entry, left / 4)), raised to one entry when it fits in left / 2. entry is the unwindowed snapshot at min(ctx, 8192) tokens (8192 on a default launch) and left is memory after the model's estimated footprint. It is a cap, not an allocation: resident bytes grow only as snapshots are inserted. An explicit flag or env value is never replaced.

Measurement (GB10, default launch, multi-turn mlxcel-server)

"Before" is the same binary with MLXCEL_PROMPT_CACHE_SNAPSHOT_CAPACITY_BYTES=536870912, which marks the capacity explicit and skips the raise (startup.rs), reproducing the fixed bucket the issue describes.

Checkpoint Capacity Peak stored Oversized rejects Snapshot hits Last turn
gemma-3-4b-it-4bit, 8 turns to 31.8k tokens 512 MiB -> 6.375 GiB 532 MB -> 2.02 GB 13 -> 0 15/16 both; before stalls at cached=19918 from turn 6 15.5 s -> 2.07 s
gemma-4-12b-it-4bit (gemma4_unified_text), 6 turns to 15.8k 512 MiB -> 15.75 GiB 512 MB -> 4.39 GB 8 -> 0 0/12 -> 11/12 37.7 s -> 4.3 s
muse-glimmer-30b-4bit (muse_glimmer_text), 6 turns to 17.4k 512 MiB -> 2.44 GiB 315 MB -> 739 MB 0 -> 0 5/6 both; self-evictions 6 -> 0 7.4 s both

On Gemma 4 12B the "before" completion entries fit, but the next-turn warm-ups were all skipped (0 run, 6 skipped against 5 run, 0 skipped after) once the boundary entries (0.9 to 5.4 GB) were rejected, so no turn hit. Per-token estimates against first-turn boundary snapshots: 139,264 vs 140,847 measured (Gemma 3), 344,064 vs 345,330 (Gemma 4 unified), 53,248 vs 53,465 (Muse Glimmer), all within 1.2% and now asserted at 2%.

Test plan

  • prompt_cache::snapshot_sizing (14), execution::kv_arch (28), server::prompt_cache (184) with --features cuda --release, --test-threads=1
  • cargo clippy --lib --tests --features cuda -- -D warnings, cargo fmt --check
  • Real multi-turn runs above

Not verified on this host

Closes #1761

Issue #1761 reported that gemma3, llama4 and afmoe fell back to the fixed 512 MiB snapshot store and that `_text` model_type spellings missed the model-aware path. PR #1978 already added those families and the `_text` normalization; nothing covered them with the geometry the real checkpoints carry, so a regression would go unnoticed.

The snapshot_sizing tests move to snapshot_sizing_tests.rs (the module was over 600 lines) and gain cases built from the on-disk configs of gemma-3-4b, llama-4-scout, gemma-4-12b (gemma4_unified_text), muse-glimmer-30b and an AFMoE config: each must take the model-aware path, size an unwindowed 8192-token entry, and skip the dense KV-store raise. Per-token estimates are asserted within 2% of first-turn boundary snapshots measured on GB10, and the Scout figure against the #1752 measurement.

docs/environment-variables.md states the sizing formula, that the value is a cap rather than an allocation, and the measured effect on Gemma 3 4B and Gemma 4 12B.

Validation on GB10: snapshot_sizing (14), kv_arch (28) and server::prompt_cache (184) tests, clippy --lib --tests --features cuda, fmt, and default-launch multi-turn mlxcel-server runs on three checkpoints against a 512 MiB control.

Refs #1761
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 6, 2026
@inureyes
inureyes merged commit b920f51 into main Oct 6, 2026
25 of 26 checks passed
@inureyes
inureyes deleted the fix/issue-1761-snapshot-sizing-model-aware branch October 6, 2026 05:47
@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:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:test Test related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(prompt-cache): snapshot bucket sizing is not model-aware for the attention-cache families, so large-context reuse silently never happens

1 participant