Skip to content

fix: size prompt-cache stores for large models, clarify MTP declines - #1978

Merged
inureyes merged 1 commit into
mainfrom
fix/gemma4-snapshot-capacity-mtp-decline-msg
Sep 26, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/gemma4-snapshot-capacity-mtp-decline-msg

Conversation

@inureyes

Copy link
Copy Markdown
Member

Gemma 4 31B QAT on GB10 showed cached=0 on every turn of one conversation, and its assistant drafter was declined with a message that read as "target is not Gemma 4". Both reproduced on this host.

Prompt cache

Every snapshot (2.97 GB boundary, 1.11 GB completion) was larger than the 512 MiB snapshot store and rejected at DEBUG. The existing model-aware raise never applied:

  • It matched only text_config.model_type (gemma4_text); Qwen 3.5 VLM checkpoints (qwen3_5_text) got a zero recurrent-state term for the same reason. Both spellings are now matched, and Gemma 3, Llama 4, AFMoE, and Inkling are included.
  • context_size == 0 (no --ctx-size) was sized as 32 tokens, which also disabled the Qwen3.8 raise at default launch.
  • Boundary snapshots keep sliding layers unwrapped (about 900 KB/token on the 31B), but were sized window-capped; Gemma 4 global-layer geometry was ignored.

The ceiling is now a quarter of the memory left after the model's estimated footprint, with a one-entry floor. The dense-KV store gets the same model-aware default (Llama 3.1 8B at 17k tokens needed 2.24 GB against the fixed 2 GiB). An oversized reject on either store logs one WARN naming the flag. MiniCPM-V 4.6 did not forward the snapshot methods to its Qwen 3.5 backbone and never cached; a source-scan test guards the wrappers.

MTP

On CUDA the verify block differs from the single-token chain (226171 of 524288 logit bytes at position 0), so the fail-closed policy declines; that default is unchanged. The decline now states the probe verdict and MLXCEL_MTP_ALLOW_INEXACT, once per process, and the Metal-only qmv_wide retry is skipped elsewhere. With the override, 31B QAT decodes 1.3 to 1.8x faster with near-tie divergence from classic.

Validation

  • Default-launch 3-turn sweep over 28 local checkpoints on GB10: every snapshot family hits on turns 2 and 3 (31B QAT: 8.8 s to 0.68 s); Jamba v0.1 and Mamba2 misses are test artifacts (base-model reasoning split, 1024-token context).
  • 1017 related unit tests, clippy, and fmt pass.

Follow-ups for the unfixed findings are filed separately.

Gemma 4 31B QAT on GB10 reported cached=0 on every turn and an MTP decline that read as "target is not Gemma 4". Both reproduced.

Prompt cache: one snapshot (2.97 GB boundary, 1.11 GB completion) exceeded the 512 MiB store, so every insert was rejected at DEBUG. The model-aware raise never applied because it read only `text_config.model_type` (`gemma4_text`, and `qwen3_5_text` zeroed Qwen 3.5's recurrent term), treated `context_size == 0` as 32 tokens, sized sliding layers window-capped although boundary snapshots are unwrapped, and ignored Gemma 4's global-layer geometry. All four are fixed; the ceiling now comes from memory left after the model, with a one-entry floor. The dense-KV store gets the same model-aware default (Llama 3.1 8B at 17k tokens needed 2.24 GB against 2 GiB). Oversized rejects on either store now log one WARN naming the flag. MiniCPM-V 4.6 never forwarded the snapshot methods to its Qwen 3.5 backbone; it does now, guarded by a source-scan test.

MTP: the decline names the probe verdict and logs once per process; the Metal-only qmv_wide retry is skipped on other backends. The fail-closed default is unchanged.

Validation: default-launch multi-turn sweep over 28 local checkpoints on GB10 (31B QAT: 8.8 s to 0.68 s per turn), 1017 related unit tests, clippy, fmt.
@inureyes inureyes added the type:bug Bug fixes, error corrections, or issue resolutions label Sep 26, 2026
@inureyes
inureyes merged commit d7279c5 into main Sep 26, 2026
20 checks passed
@inureyes
inureyes deleted the fix/gemma4-snapshot-capacity-mtp-decline-msg branch September 26, 2026 12:26
inureyes added a commit that referenced this pull request Sep 26, 2026
Gemma 3 text_config often omits num_attention_heads, num_key_value_heads, head_dim, and sliding_window_pattern (gemma-3-4b-it-4bit carries only hidden_size, intermediate_size, num_hidden_layers, sliding_window). Without them, attn_dims fell back to num_heads=1 and head_dim=hidden_size/1, which misclassified the model as plain standard attention and overstated its per-token KV footprint by about 2.6x, inflating the #1978 prompt-cache snapshot capacity default, estimate_total_memory, and mlxcel inspect for this family.

classify now fills in the HF Gemma3TextConfig defaults (num_attention_heads 8, num_key_value_heads 4, head_dim 256, sliding_window_pattern 6) for model_type gemma3 / gemma3_text whenever the corresponding field is absent, leaving any explicit config value untouched, and other families unchanged.

Verified on gemma-3-4b-it-4bit: the startup line now reports architecture=sliding-window (5 global / 29 windowed layers) and 139,264 bytes/token, matching the real snapshot measurement, down from the previous 348,160 bytes/token standard-attention misclassification.

Closes #1980
inureyes added a commit that referenced this pull request Oct 6, 2026
…hes (#2129)

## 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

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

## Not verified on this host

- Llama 4 Scout real run: the checkpoint is 57 GB, over this host's 40 GB rule. Covered by a unit test against the #1752 measurement (192 KiB per token, within 1%).
- AFMoE real run: no local checkpoint; unit test only.
- Metal, and the issue's workspace-wide `cargo test` and `clippy --workspace` (narrow selectors run instead).

Closes #1761
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
@inureyes inureyes added priority:medium Medium priority status:done Completed labels Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium 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.

1 participant