Example 1
Arguments: [
1,
1,
1,
1,
1,
2
]
Expected result: 4One key scalar plus one value scalar, each 2 bytes, requires 4 bytes.
KV memory and quantization · Easy
Problem 31 of 50 · Python and NumPy
Return the number of bytes needed to store all keys and values for a dense grouped-query KV cache. Include both K and V, every layer, every sequence, and every cached token. Count stored KV heads, not query heads. This calculation excludes allocator padding and metadata.
def kv_storage_bytes(batch, tokens, layers, kv_heads, head_dim, bytes_per_element):Arguments: [
1,
1,
1,
1,
1,
2
]
Expected result: 4One key scalar plus one value scalar, each 2 bytes, requires 4 bytes.
Arguments: [
2,
4,
3,
2,
8,
2
]
Expected result: 15362×2×4×3×2×8×2 = 1536 bytes.