"""KV-cache bytes, bf16 (2 bytes per element). Not a runtime measurement.

Part 1 applies the formulas to configs opened on 2026-11-05.
The hybrid row is a hypothetical mix on the Llama-3.1-8B attention
dimensions. It is not a released model.

Part 2 recomputes three October 2026 figures from official cards or
config.json. A gallery number that does not match is printed as
gallery-only.
"""
B = 2
CTX = 32768
KiB = 1024


def mha_or_gqa(layers, kv_heads, head_dim):
    return layers * 2 * kv_heads * head_dim * B


def mla(layers, latent, rope):
    # Compressed KV: latent plus the decoupled RoPE key. Not the per-head nope/v dims.
    return layers * (latent + rope) * B


def swa_bytes(swa_layers, kv_heads, head_dim, window, global_layers, ctx):
    tokens = swa_layers * min(window, ctx) + global_layers * ctx
    return tokens * 2 * kv_heads * head_dim * B


rows = [
    ("MHA  llama-2-7b config (32L, 32kv, d128)", mha_or_gqa(32, 32, 128)),
    ("GQA  Llama-3.1-8B config (32L, 8kv, d128)", mha_or_gqa(32, 8, 128)),
    ("MLA  DeepSeek-V3 config (61L, c512+r64)", mla(61, 512, 64)),
]
print(f"{'variant':46}{'KiB/token':>10}{'GiB@32k':>9}")
for name, nbytes in rows:
    print(f"{name:46}{nbytes / KiB:10.1f}{nbytes * CTX / 2**30:9.2f}")

hybrid = swa_bytes(24, 8, 128, 4096, 8, CTX)
print(f"{'Hybrid 3:1 SWA4k/global (hypothetical)':46}{'(total)':>10}{hybrid / 2**30:9.2f}")

print("\nRecomputed from cards/configs opened 2026-11-05, bf16, naive full length:")
# Muse Spark Open model card: 62 layers, 16 KV heads, head dim 64. Every layer counted full.
muse = mha_or_gqa(62, 16, 64)
# LFM3-1.7B config.json: 6 full_attention + 5 sliding_attention, 4 KV heads, head dim 128.
lfm_naive = mha_or_gqa(11, 4, 128)
lfm_capped = swa_bytes(5, 4, 128, 256, 6, CTX)
# Mistral Large 4 config.json: 72 layers, kv_lora_rank 512, qk_rope_head_dim 64.
mistral_naive = mla(72, 512, 64)
# kv_source_layers has 63 unique sources out of 72. Sharing formula, not the gallery figure.
mistral_shared = mla(63, 512, 64)
for name, nbytes in [
    ("Muse Spark Open, all 62 layers full", muse),
    ("LFM3, 11 attention layers as if full", lfm_naive),
    ("Mistral Large 4, 72 MLA layers, no sharing", mistral_naive),
    ("Mistral Large 4, 63 unique KV sources", mistral_shared),
]:
    print(f"  {name:48}{nbytes / KiB:8.3f} KiB/token -> {nbytes * CTX / 2**30:6.2f} GiB @32k")
print(
    f"  {'LFM3, 5 layers capped at window 256':48}"
    f"{'n/a':>8} {'(not per-token)':>18} {lfm_capped / 2**30:6.2f} GiB @32k"
)
print("Gallery figure not reproduced from the config: Mistral Large 4 80.75 KiB/token")
print("  naive x32k of that gallery figure would be "
      f"{80.75 * KiB * CTX / 2**30:6.2f} GiB, and this script does not adopt 80.75 as calculated.")
