Repository navigation
fix: apply HF Gemma 3 head-field defaults in the KV estimator - #1984
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Gemma 3
text_configoften omitsnum_attention_heads,num_key_value_heads,head_dim, andsliding_window_pattern, sokv_arch.rs::attn_dimsfell back tonum_heads=1/head_dim=hidden_size, misclassifying the model as plain standard attention and overstating its per-token KV footprint by about 2.6x.classifynow applies the HFGemma3TextConfigdefaults (num_attention_heads8,num_key_value_heads4,head_dim256,sliding_window_pattern6) formodel_typegemma3/gemma3_textwhenever a field is missing, leaving explicit config values and every other family untouched.What changed
src/execution/kv_arch.rs: addedapply_gemma3_defaults, called at the top ofclassify, which fills in the four Gemma 3 fields above only where absent, soattn_dimsand thesliding_window_patternglobal/windowed split both see the correct geometry.text_configshape and assertingmarginal_bytes_per_token == 34 * 2 * 4 * 256 * 2, and one confirming explicit values win while still-absent fields keep defaulting independently.1980-gemma3-kv-defaults-20260926.en.md/.ko.md).Before / after (gemma-3-4b-it-4bit)
standard attention (34 layers, full context)sliding-window: 29 layer(s) capped at 1024 tokens, 5 globalTest 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 --allcargo clippy --release --features cuda --lib --tests -- -D warnings(clean)gemma-3-4b-it-4bitcheckpoint (figures above)Closes #1980