Repository navigation
fix(kv_cache): count chunked attention, skip non-attention blocks, allow fp8 - #4516
Merged
Merged
Conversation
…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
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
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+ 12full_attention.chunked_attentionis 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_maskdocuments it that way). It was being excluded, so only 12 of 48 layers counted:4.00x, measured by comparing the two resolutions on the real config.
Nemotron-H's
moe/mlpare blocks, not attentionIts
layer_typesis['linear_attention', 'moe', 'full_attention', 'mlp']. The modeling code dispatches those toNemotronHMoE/NemotronHMLPmixers, and its own comment says:They are now a
blockkind 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,e5m2and thefnuzvariants 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 ofNone:Sweep after the change
no_cacheremains 3:falcon_mambaandgranitemoehybrid(every layer recurrent) andunlimited_ocr(no decoder stack exposed).unmappedremains 71 whose config class is not installed in this transformers.Tests
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.