39How many GPUs a token service needs

mediumpython
40:00

39 How many GPUs a token service needs

Anthropic's question lists include designing a token-generation service for 100,000 requests a second (1 source), and a prep site's OpenAI guide lists GPU scheduling and autoscaling for a ChatGPT-style service (1 source). Both start from the same numbers: how many conversations one GPU can hold, how fast it decodes them, and how many GPUs the traffic needs. This is practice in that shape, not a leaked question.

Set a 40-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/39_serving_budget -k level1. Earlier levels must keep passing.

A model is a dict with params (the parameter count), bytes (bytes per number: 2 for bf16), layers, kv_heads (the key and value heads, not the query heads) and head_dim. A gpu is a dict with memory_gb, bandwidth_tbs (memory bandwidth in terabytes a second) and tflops (dense peak, in trillions of operations a second). A gigabyte here is 10^9 bytes, and a terabyte 10^12.

Level 1. Memory. kv_bytes_per_token(model) returns the bytes of KV cache one token holds: a key and a value in every layer, each kv_heads * head_dim numbers of bytes each. sequences_that_fit(model, gpu, context, reserve_gb=10) returns how many sequences of context tokens fit in the GPU's memory after the weights (params * bytes) and reserve_gb kept for everything else. It's a whole number, and 0 when nothing fits.

Level 2. Speed. step_ms(model, gpu, batch, context, utilization=0.5) returns one decode step's time in milliseconds, for batch sequences that each hold context tokens. A step reads every weight once and every sequence's whole cache, at the GPU's bandwidth. It also does 2 operations per parameter per sequence, at utilization times the peak. Those two happen at the same time, so the step takes the longer one. largest_batch(model, gpu, context, target_ms, reserve_gb=10, utilization=0.5) returns the biggest batch that fits in memory and whose step takes at most target_ms (a step of exactly target_ms counts), or 0 if not even one sequence makes it.

Level 3. The fleet. gpus_needed(model, gpu, context, target_ms, rate, first_token_s, output_tokens, target_use=0.7, reserve_gb=10, utilization=0.5). Each GPU runs largest_batch sequences. A request takes first_token_s until its first token, then one step for each of its other output_tokens - 1 tokens. A step takes step_ms at that batch, which can be under target_ms. With rate requests arriving a second, the number in flight is the rate times the time each one takes. Return the fewest GPUs that keep each GPU's batch at most target_use full: the number in flight over target_use * batch (not rounded), rounded up to a whole number. No traffic needs 0. Raise ValueError when no batch meets target_ms.

Things to get right: the key and the value, the reserve, which of the two limits sets the step, the memory cap on the batch, and rounding GPUs up. Follow-ups from the lesson: chunked prefill for long prompts, a prefix cache for a shared system prompt, scaling on queued requests and cache use, and how fewer bytes per weight change level 2.

solution.py
Run the tests to see which pass.