Skip to content

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

Closed

Description

@inureyes

Problem

Found while validating PR #1978. attn_dims in src/execution/kv_arch.rs:148 falls back to head_dim = hidden_size / num_heads and num_kv_heads = num_heads (with num_heads defaulting to 1) when a config omits the head fields. 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 2560, intermediate_size 10240, num_hidden_layers 34, sliding_window 1024. HF Gemma3TextConfig defaults are num_attention_heads 8, num_key_value_heads 4, head_dim 256, sliding_window_pattern 6.

Measured: the real snapshot is about 139 KB/token while the estimate is about 348 KB/token (about 2.6x). The model-aware prompt-cache store default added in #1978 (15.9 GiB on GB10), estimate_total_memory, and mlxcel inspect all overstate for this family.

Fix

In classify / attn_dims, apply the Gemma 3 config defaults for model_type gemma3 and gemma3_text (checking both text_config and the top-level model_type, as kv_arch.rs:519 already does) when the fields are absent. Explicit config values keep priority. Other families are unchanged.

Acceptance Criteria

  • Unit test in kv_arch.rs using the real 4B text_config shape asserts 34 x 2 x 4 x 256 x 2 bytes per token for bf16 (fails on current code).
  • Existing kv_arch.rs tests pass unchanged.
  • On the real gemma-3-4b-it-4bit checkpoint, the startup line "Applied model-aware prompt-cache snapshot capacity default" reports the corrected figure; record it in the PR.

Activity

  1. added
    status:ready
    type:bug
    priority:low
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Sep 26, 2026
  2. added a commit that references this issue on Sep 29, 2026
    3f07e91
  3. added
    status:done
    and removed
    status:review
    on Sep 30, 2026
  4. added this to the 0.8 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:lowstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions