Skip to content

fix: apply HF Gemma 3 head-field defaults in the KV estimator - #1984

Merged
inureyes merged 1 commit into
mainfrom
fix/issue-1980-gemma3-kv-defaults
Sep 26, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/issue-1980-gemma3-kv-defaults

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Gemma 3 text_config often omits num_attention_heads, num_key_value_heads, head_dim, and sliding_window_pattern, so kv_arch.rs::attn_dims fell back to num_heads=1 / head_dim=hidden_size, misclassifying the model as plain standard attention and overstating its per-token KV footprint by about 2.6x. classify now applies 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 a field is missing, leaving explicit config values and every other family untouched.

What changed

  • src/execution/kv_arch.rs: added apply_gemma3_defaults, called at the top of classify, which fills in the four Gemma 3 fields above only where absent, so attn_dims and the sliding_window_pattern global/windowed split both see the correct geometry.
  • Two new unit tests: one reproducing the real 4B text_config shape and asserting marginal_bytes_per_token == 34 * 2 * 4 * 256 * 2, and one confirming explicit values win while still-absent fields keep defaulting independently.
  • Added a bilingual TECHNICAL_REPORTS entry for this fix (1980-gemma3-kv-defaults-20260926.en.md / .ko.md).

Before / after (gemma-3-4b-it-4bit)

Before After
architecture standard attention (34 layers, full context) sliding-window: 29 layer(s) capped at 1024 tokens, 5 global
bytes/token 348,160 139,264 (matches the real ~139 KB/token snapshot)
recommended snapshot capacity 17,112,760,320 (15.9 GiB) 6,845,104,128 (6.37 GiB)

Test plan

  • cargo test --release --features cuda --lib -- kv_arch:: --test-threads=1 (28 passed)
  • cargo test --release --features cuda --lib -- execution:: server::prompt_cache --test-threads=1 (324 passed)
  • cargo test --release --features cuda --lib -- kv_cache_advisor:: quant_advisor:: memory_estimate:: --test-threads=1 (83 passed, confirms other estimator callers unaffected)
  • cargo fmt --all
  • cargo clippy --release --features cuda --lib --tests -- -D warnings (clean)
  • Startup-line check on the real gemma-3-4b-it-4bit checkpoint (figures above)
  • 3-turn chat against the running server: turns 2 and 3 still hit the prompt cache (3294/3303 and 3309/3318 tokens cached)

Closes #1980

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 inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority labels Sep 26, 2026
@inureyes
inureyes merged commit 3f07e91 into main Sep 26, 2026
20 checks passed
@inureyes
inureyes deleted the fix/issue-1980-gemma3-kv-defaults branch September 26, 2026 12:33
@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

priority:low Low priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: KV estimator over-sizes Gemma 3 when text_config omits head fields

1 participant