Skip to content

fix(kv_cache): count chunked attention, skip non-attention blocks, allow fp8 - #4516

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Lumea-art:fix/kv-cache-layout-gaps
Nov 17, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Lumea-art:fix/kv-cache-layout-gaps

Conversation

@Lumea-art

Copy link
Copy Markdown
Contributor

Summary

The sweep that shipped with #4158 left three model types marked exact=False. Checking why turned up a real under-report, not a cosmetic gap.

Llama4 was under-reported 4x

Its text config declares 48 layers as 36 chunked_attention + 12 full_attention. chunked_attention is not an unknown layout: it is global attention with a chunked causal mask, i.e. the global half of Llama4's hybrid cache (transformers' create_chunked_causal_mask documents it that way). It was being excluded, so only 12 of 48 layers counted:

before: KV cache: 48 KiB/token (12 growing layer(s), gqa, float16)  | 32k ctx 1.5 GiB | unknown layer type(s): chunked_attention
after:  KV cache: 192 KiB/token (48 growing layer(s), gqa, float16) | 32k ctx 6 GiB

4.00x, measured by comparing the two resolutions on the real config.

Nemotron-H's moe / mlp are blocks, not attention

Its layer_types is ['linear_attention', 'moe', 'full_attention', 'mlp']. The modeling code dispatches those to NemotronHMoE / NemotronHMLP mixers, and its own comment says:

Nemotron-H layer_types include non-attention block types (moe / mlp)

They are now a block kind that holds no cache state, reported separately from linear-attention layers so the line does not claim a recurrent state that is not there:

KV cache: 4 KiB/token (1 growing layer(s), gqa, float16) | ... | 1 linear-attention layer(s) hold constant state | 2 non-attention block(s)

fp8 KV cache

torch.float8_e4m3fn, e5m2 and the fnuz variants are one byte per element - what a serving engine stores when it runs an fp8 KV cache - so the halved footprint can be estimated instead of None:

estimate_kv_cache(config, dtype=torch.float8_e4m3fn).bytes_per_token * 2 == estimate_kv_cache(config).bytes_per_token

Sweep after the change

models=206 ok=132 no_cache=3 unmapped=71 errors=0
exact: 132 / 132          (was 129 / 132)
llama4       48 growing layers, exact=True
llama4_text  48 growing layers, exact=True
nemotron_h    1 growing + 1 linear + 2 non-attention block(s), exact=True

no_cache remains 3: falcon_mamba and granitemoehybrid (every layer recurrent) and unlimited_ocr (no decoder stack exposed). unmapped remains 71 whose config class is not installed in this transformers.

Tests

tests/test_kv_cache_estimate.py      27 passed   (3 new)
tests/test_kv_cache_all_models.py   137 passed, 71 skipped
tests/test_quantize_run_summary.py   24 passed
format/check.sh                      clean

The three new tests pin the Llama4 ratio (48 growing layers, exactly 48x the per-layer cost), the Nemotron-H block accounting, and the fp8 halving.

…low fp8

The estimator ModelCloud#4158 resolves the cache layout per layer, and the sweep that came
with it flagged three models as inexact. Looking at why turned up a real
under-report rather than a cosmetic gap.

Llama4 declares 48 layers as 36 `chunked_attention` + 12 `full_attention`.
`chunked_attention` is not an unknown layout: it is global attention with a
chunked causal mask, so its cache covers the whole context (transformers'
`create_chunked_causal_mask` documents it as the global half of Llama4's hybrid
cache). It was being excluded, so only 12 of 48 layers were counted:

    before: 48 KiB/token (12 growing layers), 32k ctx 1.5 GiB
    after:  192 KiB/token (48 growing layers), 32k ctx 6 GiB
            -> 4.00x under-report

Nemotron-H lists `moe` / `mlp` in `layer_types`, but those are blocks, not
attention: the modeling code dispatches them to `NemotronHMoE` / `NemotronHMLP`
mixers and its own comment says "Nemotron-H layer_types include non-attention
block types (moe / mlp)". They are now counted as `block` layers that hold no
cache state, reported separately from linear-attention layers so the line does
not claim a recurrent state that is not there.

`torch.float8_e4m3fn` and friends are now one byte per element, which is what a
serving engine stores when it runs an fp8 KV cache, so the halved footprint can
be estimated instead of returning None.

Sweep after this change:

    models=206 ok=132 no_cache=3 unmapped=71 errors=0
    exact: 132 / 132        (was 129 / 132)
    llama4      48 growing layers, exact=True
    llama4_text 48 growing layers, exact=True
    nemotron_h   1 growing + 1 linear + 2 non-attention block(s), exact=True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants