Module 3 · GPUs and Kubernetes for AI · Chapter 09 · Free
GPU sizing: memory math for weights, KV cache and concurrency
- A GPU budget has four parts: weights, KV cache, engine overhead, and headroom. Weights alone never size a card.
- Concurrency is what is left after the first three are paid. It is a result of the math, not a wish.
- When one card cannot hold the budget, split the model across cards or change the budget.
The picture: GPU memory is a fixed shelf. Weights are boxes that never move, the cache is boxes added per customer, and overhead is the space your hands need to work.
- The full memory budget, line by line, in the order you must compute it.
- A worked 8B deployment sized end to end, to the card it lands on.
- Card classes by memory, and what tensor parallelism costs.
- A broken sizing plan to debug: three planted errors, one hint, one corrected plan.
Copperline is a made up B2B SaaS company. Priya Nair, its staff platform engineer, rents a 24 GB GPU for the support copilot. The plan looks sound: the 8B model at FP16 needs 16 GB of weights, so 8 GB spare.
- Load test target: 40 concurrent users, at an illustrative context of 2,048 tokens each.
- It serves four, not forty. The weights fit. The workload did not.
The budget had one line, and a real budget has four.
- Step 1: weights. Step 2: KV cache.
- Step 3: overhead and headroom, then what is left.
- Steps 4 and 5: card classes by memory, then the picture.
- Steps 6 and 7: a worked 8B example, and tensor parallelism.
- Step 8: the break-it lab.
Next: the one line everyone gets right.
Step 1: Weights, the fixed first line
Weights are the simplest line in the budget:
Weights = parameter count x bytes per parameter
FP16 or BF16: 2 bytes INT8: 1 byte INT4: 0.5 bytes
- 8B at FP16: 8 billion times 2 bytes = 16 GB, the cold open figure.
- Precision is a sizing lever: the same model at INT8 needs about 8 GB.
- Weights never shrink: loaded once, resident for every request.
Think of weights as furniture bolted to the floor of a rented room. Where the picture breaks down:
- Furniture can be moved out. Weights cannot: every sequence shares the same copy.
- The room also fills with people and their bags. That is the cache, next line.
Chapter 3 builds this line from the model card. Deeper math: GPUs and memory sizing in the AI engineering course.
Next: the line that grows while you serve.
Step 2: KV cache, priced per token, per sequence
Each live sequence keeps a KV cache: saved key and value vectors for every token in its context. Chapter 3 gives the approximation reused here:
KV bytes per token (approximation)
= layers x hidden size x 2 (keys and values) x bytes per value
8B example (illustrative): about 0.5 MB per token
- Per sequence: cache per token times the full context it can grow to, not the expected reply.
- For the deployment: per sequence cache times concurrent sequences.
- It is an approximation: grouped query attention and engine paging change the real figure. Size with it, then load test.
Now the cold open adds up. At 2,048 tokens, one sequence needs about 1 GB of cache. After weights and overhead, the card has about 4 GB left: four sequences. Forty users need about 40 GB of cache alone.
Next: the allowance nobody wants to budget.
Step 3: Overhead, headroom, and what is left
Weights plus cache is still not the whole card. A serving engine needs working space:
- Activations: temporary values computed while a batch runs. They grow with batch size.
- Engine overhead: the runtime, its buffers, fragmentation, and allocator blocks.
- Planning allowance: as a rule of thumb, budget about 4 GB for an 8B deployment of this size. Label it a rule of thumb, then measure it.
Then keep headroom: memory you do not allocate. Spikes land there instead of an out of memory error.
The order of operations is the whole lesson:
The budget, in order
Card memory, minus weights, minus overhead, minus headroom
= KV budget. Concurrency = KV budget / cache per sequence
Compute concurrency last, from what is left.
Next: matching the finished budget to a card class.
Step 4: Choosing a card class by memory
Size by memory first. Speed comes after the model fits with its workload. Well known card classes:
- L4, 24 GB: small models, short context, low concurrency, or quantised weights.
- L40S, 48 GB: mid size models with a real cache budget.
- A100, 40 or 80 GB: the name alone does not tell you the memory.
- H100, 80 GB: large budgets on one card, common for multi card splits.
Rent follows the class. Illustrative only: a mid class card might list at an illustrative price of $1.50 per GPU hour, a top class card at $3.50. Check current listings before any budget meeting with Greg, the finance partner.
Predict first:
The model loads because weights fit. The failure waits for traffic: as caches grow, the total crosses 48 GB and the engine must refuse work, preempt sequences, or crash.
Next: the picture, one card split into its four budget lines.
Step 5: The picture, one card, four lines
Press Next to fill the card line by line.
- The card total is the ceiling. Every line is paid out of it, in order.
- Parameters times bytes per parameter. Fixed, shared by every sequence.
- Activations, runtime and fragmentation, subtracted before any concurrency claim.
- What remains buys caches: this budget divided by cache per sequence.
- Reserve a slice on purpose. Allocating every gigabyte plans a peak failure.
Next: the full arithmetic, on an 8B deployment with a stated target.
Step 6: Worked example, sizing an 8B FP16 deployment
Target: an illustrative 8B model at FP16, full context 8,192 tokens, 8 concurrent sequences, cache at the chapter 3 approximation of 0.5 MB per token.
1. Weights. 8 billion times 2 bytes = 16 GB.
2. Cache per sequence. 8,192 tokens times 0.5 MB = 4,096 MB, about 4 GB.
3. Cache for the target. 8 sequences times 4 GB = 32 GB.
4. Overhead. Rule of thumb allowance: 4 GB.
5. Total. 16 + 32 + 4 = 52 GB, before headroom.
6. The card. An L40S at 48 GB does not fit it. An A100 80 GB or an H100 at 80 GB does, with 28 GB unallocated: headroom plus growth room for about 7 more full sequences.
What decided the card was not the weights, but the 32 GB of cache. Halve the context and the total drops to about 36 GB, and the 48 GB class works.
Next: when one card is not enough.
Step 7: When one GPU is not enough, tensor parallelism
Sometimes the budget beats every card: a 70B model at FP16 needs about 140 GB of weights alone. The standard answer is tensor parallelism: split each layer's work across several GPUs, so each holds part of the weights and part of the cache.
- What it buys: the budget is divided. Four 80 GB cards hold what no one card could.
- What it costs: cards swap partial results on every layer, many times per token. That interconnect chatter needs fast links, ideally in one server, and eats some speed.
- The platform catch: the split group acts as one unit. Kubernetes must place those cards together, the next chapter's topic.
Think of four cooks sharing one recipe book, each holding some pages. Where the picture breaks down:
- Cooks can shout across one kitchen. GPUs need high bandwidth interconnects, and chatter grows per split.
- Splitting fixes memory, not demand. If concurrency is the problem, replicas may beat a wider split.
Next: the lab, where a confident plan meets your red pen.
Step 8: Break-it lab, debug the sizing plan
Why this exercise: sizing errors are cheap on paper and expensive at load test. This plan for Copperline's extraction workload contains exactly three planted errors.
Lab: Step 8 hands you Copperline's sizing plan for the extraction model, and it is wrong by more than ten times on concurrency. This lab runs in your browser and needs JavaScript enabled.
- None of the errors is exotic. Wrong order, wrong length, no headroom: most real sizing failures are one of these.
- The broken plan was wrong by over ten times on concurrency. That is why load tests surprise teams.
Next: a field note, then the mix-ups.
Serving engines in this category, including vLLM, TensorRT-LLM and SGLang, expose settings for the share of GPU memory the cache may use, because operators are expected to set that budget deliberately. In production practice, sizing sheets that list only weights are a common cause of first load test failures. Treat the four line budget as the minimum a review should show.
Mix-ups to avoid
- "The weights fit, so the model fits." Weights are one line of four. Cache at load usually breaks the card.
- "Size the cache on average reply length." A sequence can grow to full context. Budget for the context you allow.
- "Overhead is small enough to skip." Skipping it frees nothing. It moves the error to the pager.
- "A full card is an efficient card." Full means zero headroom. The first spike becomes an outage.
- "An A100 is an A100." It comes in 40 and 80 GB. The purchase order cares which.
Chapter quiz
Five questions. Score 70% or more to mark this chapter complete.
Review this chapter in 5 minutes
Watch the whole chapter recapped: GPU memory math, KV cache and concurrency, card classes, and tensor parallelism when one card is not enough.
- Four lines, in order: card total, minus weights, minus overhead, minus headroom. The rest is the KV budget.
- Weights: parameters times bytes per parameter. 8B at FP16 is 16 GB.
- KV cache: about 0.5 MB per token in the 8B approximation, times full context, times sequences.
- Concurrency is the KV budget divided by cache per sequence. Compute it last.
- Worked example: 16 + 32 + 4 = 52 GB, landing on an 80 GB card, not a 48 GB one.
- Tensor parallelism splits weights and cache across cards, paid in interconnect chatter.
Decision rule: never approve a GPU plan that does not show all four lines and the concurrency they leave, at full context.
Review
Glossary
- Weights
- The model's parameters, loaded once and resident. Example: 8B at FP16 is 16 GB.
- Precision
- Bytes per parameter. Example: FP16 uses 2, INT8 uses 1.
- KV cache
- Saved keys and values per token per sequence. Example: about 0.5 MB per token here.
- Concurrency
- Sequences a card serves at once. Example: KV budget divided by cache per sequence.
- Activations
- Temporary values computed while a batch runs. Example: they grow with batch size.
- Overhead allowance
- Planned space for engine, buffers and fragmentation. Example: 4 GB here.
- Headroom
- Memory left unallocated for spikes. Example: 8 GB unspent in the corrected plan.
- Card class
- A GPU model grouped by memory. Example: L4 24 GB, L40S 48 GB, H100 80 GB.
- Tensor parallelism
- Splitting each layer across GPUs sharing weights and cache. Example: a 70B model across four cards.
- Interconnect
- Fast links between GPUs in a server. Example: split cards swap partial results over it.
Next: "Scheduling and sharing GPUs: MIG, time-slicing and quotas".