My account

GPU memory and parallelism

Before you choose a model or a GPU, do the memory math: weights, KV cache for real chats, and runtime. A GPU has two limits, memory speed and math speed, and the batch size decides which one you hit. When a model does not fit on one GPU, split it with tensor parallelism inside a machine with fast links, or pipeline parallelism across machines, and count the cost of talking between GPUs.

Not reviewedWritten with AI helpReading time: 13 minShop assistant examplePython

Author: bezzad

Contents

The problem: “it fits”

Our evals show that a larger 70B model answers hard support questions better than our 8B model. A teammate says: “The 70B model is 140 GB. We have two 80 GB GPUs. 160 is more than 140, so it fits.”

It does fit, but only the weights. There is almost no room left for the chats, and the two GPUs now must talk to each other all the time. This lesson shows how to see that before buying or renting anything.

The memory budget

Every GPU running a model holds three things:

  1. Weights. Parameters × bytes per parameter.
  2. KV cache. For every token of every running request. (See the lesson on how inference works.)
  3. Runtime. The engine, temporary results of the math, and other small parts. Plan for a few GB per GPU.

Bytes per parameter depend on the number format:

Format Bytes per parameter 8B model 70B model
FP32 4 about 32 GB about 280 GB
BF16 or FP16 2 about 16 GB about 140 GB
FP8 or INT8 1 about 8 GB about 70 GB
INT4 0.5 (plus a little extra for scales) about 4 to 5 GB about 35 to 40 GB

Serving usually starts at BF16. Smaller formats are the topic of the quantization lesson.

A small worksheet helps:

GB = 1e9

def free_for_kv(gpus, gb_per_gpu, params_b, bytes_per_param,
                runtime_gb_per_gpu=4):
    weights = params_b * 1e9 * bytes_per_param
    total = gpus * gb_per_gpu * GB
    return total - weights - gpus * runtime_gb_per_gpu * GB

def tokens_in_flight(free_bytes, layers, kv_heads, head_dim, kv_bytes=2):
    per_token = 2 * layers * kv_heads * head_dim * kv_bytes
    return int(free_bytes / per_token)

free = free_for_kv(gpus=2, gb_per_gpu=80, params_b=70, bytes_per_param=2)
print(free / GB)                                       # about 12 GB
print(tokens_in_flight(free, layers=80, kv_heads=8, head_dim=128))
# about 36,000 tokens: about one full 32k chat

The same function with one byte per parameter (FP8) gives about 82 GB free and about 250,000 tokens. The format of the weights changes how many users we can serve, not only the size of the model.

Two limits of a GPU

A GPU has two speeds:

  1. Memory bandwidth: how fast it can read data from its memory, in GB per second.
  2. Compute: how many math operations it can do per second.

In each decode step, the GPU reads all the weights once and uses them for every request in the batch.

Tokens per second, whole GPUMemory-boundmore requests are almost freeCompute-boundeach step gets slowerthroughput grows slowlyRequests in the same step (batch size)
With a small batch the GPU waits for memory, so more requests are almost free. With a big batch the math becomes the limit.
  1. Small batch: memory-bound. The GPU reads 16 GB for a few tokens of math. Adding requests adds math but almost no reading, so throughput rises almost for free.
  2. Big batch: compute-bound. At some point the math takes longer than the reading. Now each extra request makes every step slower.
  3. Prefill is compute-bound already, because it does math for thousands of prompt tokens at once.
  4. The KV cache must be read too. Each request reads its own cache in each step. Long chats add memory traffic, even when the weights are shared.

Why it matters: many tricks only help in one zone. Weight-only quantization and speculative decoding help most in the memory-bound zone. In the compute-bound zone, they may help little or even hurt.

When the model does not fit on one GPU

There are two main ways to split a model:

Tensor parallelism: every layer is splitGPU 1half of every layerGPU 2half of every layerthey talk inside every layerPipeline parallelism: each GPU has whole layersGPU 1layers 1 to 40GPU 2layers 41 to 80they talk only at the border
Tensor parallelism splits every layer and talks a lot. Pipeline parallelism splits by layers and talks only at the border.
  1. Tensor parallelism (TP). Every layer is split. Each GPU holds part of each weight matrix and does part of the math. After the parts, the GPUs combine their results (an all-reduce), inside every layer, for every step.
    • Good: one request uses all GPUs at once, so latency goes down.
    • Cost: a lot of communication. It needs very fast links between GPUs, like NVLink inside one machine.
  2. Pipeline parallelism (PP). Each GPU holds whole layers. GPU 1 runs layers 1 to 40, then hands the result to GPU 2.
    • Good: little communication, so it works across machines.
    • Cost: one request goes through the GPUs one after another. To keep all GPUs busy, the engine must run several groups of requests at the same time. Latency for one request does not improve.
  3. Replicas (data parallelism). If the model fits on one GPU, the simplest scale-out is many full copies behind a router. No communication between them at all.

A common pattern: tensor parallelism inside one machine with NVLink, pipeline parallelism between machines, and replicas for more traffic.

Mixture of experts (MoE) models need a note. They use only some “experts” for each token, so the math per token is small. But all experts must be in memory, so the memory budget follows the total parameter count, not the active one. Engines can also split experts across GPUs (expert parallelism).

  1. NVLink (NVIDIA) connects GPUs in one machine with very high bandwidth.
  2. PCIe is the normal connection to the computer. It is many times slower than NVLink.
  3. Networks like InfiniBand or fast Ethernet connect machines. Slower again, and with more delay.

Tensor parallelism over PCIe often works, but each token can get slower, and adding GPUs may give less than you expect. Measure it.

A decision order

  1. Pick the smallest model that passes your evals. It is the cheapest change.
  2. Do the memory math for real traffic: weights + KV cache + runtime.
  3. Try a smaller format (FP8, INT8, INT4) if quality stays good.
  4. If one GPU is still not enough, use tensor parallelism inside one machine with fast links.
  5. For more traffic, add replicas.
  6. Load test the final setup at your latency targets.

Common mistakes

Mistake Result Right way
“The weights fit, so the model fits.” No room for chats; long queues. Weights + KV cache + runtime.
Ignoring the model’s KV config Very different cache sizes for models of the same size. Read layers, KV heads and head size from the config.
Tensor parallelism over slow links Slower tokens, poor scaling. NVLink inside a machine, or test pipeline parallelism.
Counting only active parameters of a MoE model Out of memory. Budget memory for all experts.
Expecting a speed-up everywhere Tricks that help at batch 1 may not help at batch 100. Know whether you are memory-bound or compute-bound.

Summary in six lines

  1. Memory = weights + KV cache for real traffic + runtime.
  2. Bytes per parameter: 2 for BF16, 1 for FP8, about 0.5 for INT4.
  3. Small batches are memory-bound; big batches become compute-bound.
  4. Tensor parallelism splits every layer and needs fast links like NVLink.
  5. Pipeline parallelism splits by layers and talks less, but does not lower one request’s latency.
  6. Pick the smallest model that passes evals before you add GPUs.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.