WRITE · TRACE · UNDERSTAND

Reserve KV-cache memory with explicit grouped-query dimensions

Compute unpaged KV bytes per token as 2*layers*kv_heads*head_dim*bytes_per_element.

Read lesson: Reserve KV-cache memory with explicit grouped-query dimensions →

Your experiment

Compute unpaged KV bytes per token as 2*layers*kv_heads*head_dim*bytes_per_element. Reserve each request's prompt+maximum-output tokens. In input order admit requests that fit remaining byte capacity, skipping those that do not fit. Return admitted,rejected,reserved_bytes,bytes_per_token. IDs are unique. Dimensions are positive integers; capacity and token counts are nonnegative integers. This estimate excludes model weights, activations, allocator/page overhead and tensor parallel replication.

Python runs in your browser. Your draft stays on this device.

What to look for

Compare the estimate with real accelerator measurements and paged-cache behavior. Treat memory reservation, throughput, fairness and latency as separate constraints.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Reserve KV-cache memory with explicit grouped-query dimensions works

The idea

A server must decide how many requests fit in memory before generating. The part that grows with a request is its KV cache, whose size is simple arithmetic: every token stores one key and one value vector for each layer and each KV head. That gives 2 * layers * kv_heads * head_dim * bytes_per_element bytes per token. With grouped-query attention the KV head count matters, not the number of query heads.

Each request reserves room for its prompt plus its maximum output, so an admitted request cannot run out of memory halfway through. Requests are taken in input order; one that does not fit is rejected, and a later, smaller one may still be admitted. The figure is an estimate: it leaves out weights, activations and allocator overhead.

How it works

  1. Validate that the four dimensions are positive integers, with booleans refused, that capacity and token counts are nonnegative integers, and that request ids are unique.
  2. Compute bytes_per_token = 2 * layers * kv_heads * head_dim * bytes_per_element.
  3. For each request in input order, compute cost = (prompt + output) * bytes_per_token.
  4. Admit the request and add cost to reserved when reserved + cost <= capacity.
  5. Otherwise add its id to rejected and move on to the next request.
  6. Return the admitted ids, the rejected ids, reserved_bytes and bytes_per_token.

What it costs

Time
O(requests)
Per token
2·L·Hkv·d·bytes
Reserves
prompt + max output

Planning is a single pass over the requests; what matters is the bytes it reserves, which grow linearly with layers, KV heads, head width and token count.

Where it is used

  • vLLM preallocates GPU memory for KV cache blocks within its gpu_memory_utilization budget and admits requests only while free blocks remain.
  • llama.cpp allocates the KV cache for the whole context window when a context is created and prints its size in the load log.
  • Hugging Face Text Generation Inference caps each request with --max-total-tokens, the sum of input tokens and max_new_tokens.

Watch out for

  • Counting query heads when the model shares KV heads: with grouped-query attention only kv_heads are stored per layer.
  • Reserving the prompt only: generated tokens enter the cache as well, so an admitted request could exhaust memory partway through its output.
  • Reading the result as total memory use: it leaves out model weights, activations, and allocator and paging overhead.