, ,

What happens inside a transformer when you ask a question?

Follow one question through a Llama-style 8B model: tokens, embeddings, attention with queries, keys and values, the MLP blocks, 32 layers and the final scores, plus where the 8 billion parameters actually sit.

Attention links from the word it to the word animal

AI Infrastructure series · Part 11 of 24

Part 9 said an LLM predicts the next token. This part opens the model and follows one question, “What is the capital of France?”, through Llama 3.1 8B: every matrix it passes through and the shape of each one, where the 8 billion parameters sit, which steps do maths and which mostly read memory, and what gets stored as the KV cache. Once you can see this walk-through, prefill, decode, KV cache sizing and multi-GPU splits stop being separate topics. They are all consequences of the same few matrices.

Try it: Transformer Lab

Step through a question from tokens to the first answer word, click words to see attention working out what “it” refers to, and see the parameter count of Llama 3 8B and 70B broken down by block.

Open the demo →

My post on the 2017 “Attention Is All You Need” paper covers where this design came from and why it changed infrastructure strategy. This one stays inside a running model, with the real shapes of a model you can download today.

What happens between pressing Enter and the first word?

A transformer language model is simpler than its reputation. It is a lookup table at the front, a stack of identical layers in the middle, and a scoring table at the back. Llama 3.1 8B has 32 layers; the 70B model has 80. Every token becomes a list of 4,096 numbers (8,192 in the 70B) that travels down through every layer, each layer adding a correction. At the end, the list for the last token becomes a score for every word the model knows.

One question through Llama 3.1 8B, prefill 1 · Text “What is the capital of France?” plus chat-template tokens 2 · Tokens 7 IDs from a 128,256 vocabulary 3 · Embedding lookup 7 rows of a 128,256 × 4,096 table X = 7 × 4,096 numbers 4 · 32 layers, the same steps in each each layer: RMSNorm, attention, add back, RMSNorm, MLP, add back. Output is again 7 × 4,096. 11632 K and V for all 7 tokens are stored in every layer: 896 KB of KV cache 5 · Final RMSNorm 7 × 4,096 only the last row is needed next 6 · Output head 1 × 4,096 times 4,096 × 128,256 0.53 billion weights 7 · Logits, then a token 128,256 scores ” Paris” scores highest Decode then repeats steps 3 to 7 for one new token at a time, reading K and V back from the cache.

Each box lights up in the order the work happens. The 32 layer bars light one after another because layer 2 cannot start until layer 1 has finished: depth is sequential, even though all 7 tokens move through each layer together.

1

Text to tokens. The tokenizer cuts the question into 7 tokens: “What”, ” is”, ” the”, ” capital”, ” of”, ” France”, “?”. A chat model also adds a few special tokens marking the turns; this post counts 7 to keep the numbers readable. Each token is an integer between 0 and 128,255.

2

Embedding lookup. Each integer selects one row of a table with 128,256 rows of 4,096 numbers. No multiplication, only a memory read of 7 rows. The resulting 7 × 4,096 matrix is the residual stream, the only thing that flows from layer to layer.

3

32 layers. Each layer returns a matrix of the same shape. Attention lets tokens exchange information, the MLP transforms each token on its own, and every layer saves its keys and values: the KV cache.

4

Final RMSNorm. One last normalisation of the residual stream.

5

Output head. Only the last row, for “?”, is needed. It is multiplied by a 4,096 × 128,256 matrix, giving 128,256 logits, one per vocabulary entry.

6

Pick a token. Softmax turns the logits into probabilities and the sampler chooses one (part 9). ” Paris” wins.

7

Loop. ” Paris” is fed back in as token 8. Only that one token goes through the 32 layers; the first 7 are not recomputed because their keys and values are in the cache. That is the switch from prefill to decode.

Think of each row of the residual stream as a token’s working notes. At the start, the row for ” capital” only says “the word capital”; by the last layer, the row for “?” holds enough to predict ” Paris”. The rest of this post opens step 3.

What happens inside one layer?

Every layer has two halves, and both follow the same pattern: normalise the input, compute something, add the result back onto the input. The first half is attention, the only place where tokens look at each other. The second half is the MLP, where each token is processed on its own. The diagram shows one layer of the 8B model with the shapes for our 7-token question.

One Llama 3.1 8B layer, shapes for the 7-token question Attention half residual: x is carried around and added back x 7 × 4,096 RMSNorm 4,096 weights Q: 4,096 → 4,096 K: 4,096 → 1,024 V: 4,096 → 1,024 RoPE on Q, K Attention, 32 heads QKᵀ/√128, mask, softmax, × V scores 32 × 7 × 7 Output Wo 4,096 → 4,096 + K and V of each token are saved: the KV cache h = x + attention output MLP half residual: h is carried around and added back h 7 × 4,096 RMSNorm 4,096 weights gate: 4,096 → 14,336 up: 4,096 → 14,336 SiLU(gate) × up 7 × 14,336 down 14,336 → 4,096 + out: 7 × 4,096, to the next layer Weights per layer: attention 41.9 million, MLP 176.2 million, the two norms 8,192. The 8B model repeats this 32 times.

Red boxes are attention, blue boxes are the MLP. Dashed grey lines are the residual path: each half adds its result onto what came in rather than replacing it.

The table lists every step of that layer with its weight matrix and the shape of what comes out. The prefill column is our 7-token question. The decode column is the first answer token, which has 7 earlier tokens plus itself in its history, so its attention covers 8 positions.

StepWeight matrixOutput, prefill (7 tokens)Output, decode (1 token)
Inputnone7 × 4,0961 × 4,096
RMSNorm4,096 (a scale per number)7 × 4,0961 × 4,096
Q projection4,096 × 4,0967 × 4,096, read as 32 heads × 7 × 12832 heads × 1 × 128
K projection4,096 × 1,0248 heads × 7 × 128, saved to cache8 heads × 1 × 128, appended to cache
V projection4,096 × 1,0248 heads × 7 × 128, saved to cache8 heads × 1 × 128, appended to cache
RoPEnonerotates Q and K by positionrotates the new Q and K
Scores QKᵀ/√128none32 × 7 × 732 × 1 × 8
Mask and softmaxnone32 × 7 × 7, future positions set to zero32 × 1 × 8
Weighted sum of Vnone32 × 7 × 128, joined to 7 × 4,0961 × 4,096
Output projection4,096 × 4,0967 × 4,0961 × 4,096
Add (residual)none7 × 4,0961 × 4,096
RMSNorm4,0967 × 4,0961 × 4,096
Gate and up4,096 × 14,336 each7 × 14,336 each1 × 14,336 each
SiLU(gate) × upnone7 × 14,3361 × 14,336
Down projection14,336 × 4,0967 × 4,0961 × 4,096
Add (residual)none7 × 4,0961 × 4,096

Two things stand out. Only seven steps have weights, and all seven are plain matrix multiplications, which is exactly the work Tensor Cores are built for (part 3). And the decode column is the prefill column with 7 replaced by 1, except for attention, where the second dimension is the whole history. That one difference explains most of what follows.

What does RMSNorm do?

RMSNorm takes a token’s 4,096 numbers, computes their root mean square (square each, average, square root), divides every number by it, and multiplies each by a learned scale. The result has a predictable size however large the residual stream has grown, which keeps 32 layers of additions inside the range 16-bit arithmetic handles well. It costs almost nothing in FLOPs, but it reads and writes the whole activation, so inference engines fuse it into the neighbouring kernel. Llama applies it before each half, plus once after the last layer.

Why are there residual connections?

Each half adds its output onto its input: h = x + Attention(RMSNorm(x)), then out = h + MLP(RMSNorm(h)). A layer never replaces the token’s notes, it only writes a correction on top, which is what makes deep stacks trainable. For infrastructure, the residual stream is the one tensor that has to cross a boundary when a model is split by layers across GPUs: 4,096 numbers per token, 8 KB in 16-bit for the 8B model and 16 KB for the 70B.

How does attention work, step by step?

Attention is the only step in which one token reads information from other tokens. Each token’s normalised row is multiplied by three weight matrices to produce three vectors, split into heads of 128 numbers:

VectorWhat it is used forShape per token, 8BKept after this step?
Query (Q)What this token is looking for32 heads × 128No, used once
Key (K)What this token offers to be matched against8 heads × 128Yes, in the KV cache
Value (V)What this token passes on when it is matched8 heads × 128Yes, in the KV cache

Then, in every head:

1

Score. Take the dot product of this token’s query with the key of every token up to and including itself. A dot product multiplies the two lists number by number and adds the results; similar directions give a large score.

2

Scale. Divide every score by √128 ≈ 11.3. Dot products grow with vector length; without scaling, softmax would give one token almost all the weight and training would stall.

3

Mask. Scores for later positions are set to minus infinity, so a token can never look ahead. During prefill this makes the 7 × 7 score grid a triangle.

4

Softmax. Raise e to the power of each score and divide by the total. The weights are now positive and add up to 1.

5

Mix. Multiply each value vector by its weight and add them up: 128 numbers blending the tokens this head found relevant.

6

Join and project. The 32 heads’ outputs are placed side by side (32 × 128 = 4,096) and multiplied by the output matrix Wo, which lets the heads’ findings combine. That is what gets added to the residual stream.

A worked example with three tokens

Real heads use 128 numbers per vector; two are enough to see the mechanism. Take the last three tokens before “?”: ” capital”, ” of”, ” France”. We compute what ” France” gathers. Its query is q = [1, 2]. The keys and values are made-up numbers chosen to show the pattern (illustrative). With 2 numbers per vector, the scale is √2 ≈ 1.414.

Attention for the token ” France”, with 2-number vectors (illustrative) query of ” France”: q = [1, 2] q · k ÷ √2 softmax weight ” capital” k = [1, 2] v = [4, 0] 5 3.54 0.85 ” of” k = [0, 1] v = [0, 2] 2 1.41 0.10 ” France” (itself) k = [1, 0] v = [2, 2] 1 0.71 0.05 output for ” France” 0.85 × [4, 0] + 0.10 × [0, 2] + 0.05 × [2, 2] [3.49, 0.30] The output is mostly the value of ” capital”: the query of ” France” matched that key best, so ” France” now carries information from the word it belongs with. A real head does this with 128 numbers, for every token, in 32 heads at once.

Bars grow to the softmax weight. The values in the last box are what get mixed; the keys only decide how much of each. Numbers rounded to two decimals.

TokenKeyValueq · k÷ √2exWeight
” capital”[1, 2][4, 0]1×1 + 2×2 = 53.5434.3134.31 ÷ 40.46 = 0.85
” of”[0, 1][0, 2]0 + 2 = 21.414.110.10
” France”[1, 0][2, 2]1 + 0 = 10.712.030.05
Total40.461.00

The mix is 0.85 × [4, 0] + 0.10 × [0, 2] + 0.05 × [2, 2] = [3.39 + 0.10, 0.20 + 0.10] ≈ [3.49, 0.30]. Without the scaling step the scores would stay 5, 2 and 1 and the weights would be 0.94, 0.05 and 0.02: sharper, with less of the other tokens getting through. With 128 numbers instead of 2, the difference is far larger, which is why the √128 matters.

At real scale, with 8,000 tokens of history, the newest token’s query is compared with 8,000 stored keys in each of 32 heads and 32 layers, and all those keys and values must be read from memory. That is the cost of long context.

Why do 32 query heads share only 8 KV heads?

Splitting attention into heads lets the model look for several kinds of relationship at once: one head may track which noun a pronoun refers to, another the previous word. Each head runs the steps above on its own 128-number slice. In the original transformer every head also had its own keys and values, which for Llama 3.1 8B would mean storing 32 heads of K and V per layer per token.

Grouped-query attention (GQA, Ainslie et al. 2023) keeps 32 query heads but only 8 key-value heads. Each group of 4 query heads shares one set of keys and values. The query heads still ask different questions; they just look them up in the same index. The K and V matrices shrink from 4,096 × 4,096 to 4,096 × 1,024, and so does everything stored per token.

Grouped-query attention in Llama 3.1 8B: 4 query heads share each KV head 32 query heads (Q), one per square KV 1KV 2KV 3KV 4KV 5KV 6KV 7KV 8 8 KV heads: the only K and V that are computed and stored in the KV cache 32 KV heads (original multi-head) 8 KV heads (GQA, Llama 3.1) 1 KV head (multi-query) 512 KB per token 128 KB per token 16 KB per token

Each line joins a query head to the KV head it reads from. Bars show KV cache per token for the 8B shape (32 layers, 16-bit): GQA stores a quarter of what full multi-head attention would.

The cache size per token is 2 (K and V) × layers × KV heads × 128 × 2 bytes. For the 8B model that is 2 × 32 × 8 × 128 × 2 = 131,072 bytes, the 128 KB used throughout this series. The 70B model has 80 layers and also 8 KV heads, which gives 320 KB. Multi-query attention, with a single KV head, saves more but costs quality; 8 heads is the compromise most current open models chose.

Model and attention typeKV headsPer token8,192 tokens131,072 tokens
8B, if it used full multi-head32512 KB4 GB64 GB
8B, GQA (actual)8128 KB1 GB16 GB
70B, if it used full multi-head642.5 MB20 GB320 GB
70B, GQA (actual)8320 KB2.5 GB40 GB

16-bit K and V, one sequence, binary units (1 GB = 1,024 MB). The full multi-head rows are hypothetical, for comparison.

Notice what GQA does not change: there are still 32 query heads, so the score maths is the same. GQA cuts memory and memory reads, which is what limits decode. Part 14 sizes the cache for real workloads.

How does the model know word order?

Attention on its own ignores order: shuffle the keys and values and the weighted sum is identical. Something has to tell the model that ” France” came right after ” of”.

Llama uses rotary position embeddings (RoPE, Su et al. 2021). Before scores are computed, each query and key is rotated by an amount that depends on its position. The 128 numbers of a head are treated as 64 pairs, and each pair is rotated like the hand of a clock: some pairs turn quickly as position increases, others very slowly. When a rotated query meets a rotated key, the dot product depends on how far apart the two positions are, so attention sees relative distance without any extra weights. Llama 3 sets the base of these rotation speeds (rope_theta) to 500,000 so the slowest pairs turn slowly enough to tell apart positions far into a long document, and Llama 3.1 adds a rope_scaling setting that stretches positions trained at 8,192 tokens out to 131,072.

RoPE has no parameters and only touches Q and K, never V. It has one practical consequence: keys are stored in the KV cache already rotated, so a cached key belongs to one position. That is why prefix caching in serving engines can reuse cached blocks only when the same tokens sit at the same positions, which in practice means an identical prefix at the start of the prompt.

What does the MLP do?

After attention, each token’s row goes through the MLP (also called the feed-forward block). Unlike attention, the MLP never looks at other tokens. It does the same thing to every row, which makes its cost per token fixed, whatever the length of the conversation.

MLP for one token in Llama 3.1 8B (SwiGLU) token row, 4,096 numbers gate = row × W_gate (4,096 × 14,336), then SiLU up = row × W_up (4,096 × 14,336) SiLU(gate) × up, number by number: 14,336 values × W_down (14,336 × 4,096): back to 4,096 3 matrices × 58.7 million weights = 176.2 million per layer. Bar widths are to scale: 14,336 is 3.5 times 4,096.

Widths are drawn to scale. Two expansions run side by side, one acting as a gate on the other, and a third matrix brings the result back to the width of the residual stream.

The 4,096 numbers are multiplied by two separate matrices, gate and up, each producing 14,336 numbers. The gate goes through SiLU (x times the sigmoid of x), a smooth function that passes large positive values and squashes negative ones towards zero. The two 14,336-number vectors are then multiplied number by number, so the gate decides how much of each “up” feature gets through. The down matrix projects the result back to 4,096. This gated design is called SwiGLU (Shazeer 2020); it replaced the simpler two-matrix MLP of the original transformer because it trained better for the same compute.

Research that probes trained models (for example Geva et al. 2021) finds MLP neurons that respond to specific patterns and push the output towards specific words, which is why the MLP is often described as where much stored knowledge lives. It also holds most of the weights: 176.2 million per layer, against 41.9 million for attention. That is the block Mixture of Experts models (part 12) replace with many smaller expert MLPs, of which each token uses only a few.

Where do the 8 billion parameters sit?

Every number in this post comes from the model’s config.json, the file that ships with the weights. These are the fields that set the shapes for Llama 3.1 8B:

"hidden_size": 4096,            # width of the residual stream
"intermediate_size": 14336,     # MLP width
"num_hidden_layers": 32,
"num_attention_heads": 32,      # query heads
"num_key_value_heads": 8,       # KV heads (GQA)
"head_dim": 128,
"vocab_size": 128256,
"hidden_act": "silu",           # SwiGLU MLP
"rms_norm_eps": 1e-05,
"rope_theta": 500000.0,
"max_position_embeddings": 131072,
"tie_word_embeddings": false    # input and output tables are separate

Multiply out the matrix shapes and you get the full count. The 70B model uses hidden size 8,192, MLP size 28,672, 80 layers and 64 query heads with the same 8 KV heads and vocabulary.

ComponentShape, 8B8B totalShape, 70B70B total
Token embedding128,256 × 4,0960.525B128,256 × 8,1921.051B
Q projection4,096 × 4,096, ×320.537B8,192 × 8,192, ×805.369B
K projection4,096 × 1,024, ×320.134B8,192 × 1,024, ×800.671B
V projection4,096 × 1,024, ×320.134B8,192 × 1,024, ×800.671B
Output projection4,096 × 4,096, ×320.537B8,192 × 8,192, ×805.369B
MLP gate4,096 × 14,336, ×321.879B8,192 × 28,672, ×8018.790B
MLP up4,096 × 14,336, ×321.879B8,192 × 28,672, ×8018.790B
MLP down14,336 × 4,096, ×321.879B28,672 × 8,192, ×8018.790B
RMSNorm scales2 × 4,096 per layer, plus final0.0003B2 × 8,192 per layer, plus final0.001B
Output head4,096 × 128,2560.525B8,192 × 128,2561.051B
Total218.1M per layer8.030B855.6M per layer70.554B

Grouped into blocks, the 8B model has 5.64B in the MLPs, 1.34B in attention and 1.05B in the two token tables, 8.03B in all. The 70B has 56.4B in MLPs, 12.1B in attention and 2.1B in token tables, 70.55B in all.

MLP 70.2%Attention 16.7%Tables 13.1%
Llama 3.1 8B: MLP 5.64Battention 1.34Bembedding and output head 1.05B
MLP 79.9%Attention 17.1%
Llama 3.1 70B: MLP 56.4Battention 12.1Btables 2.1B (3.0%)

Attention, the part the paper is named after, holds about a sixth of the weights. The vocabulary tables matter in small models (13% of the 8B) and fade in large ones (3% of the 70B), because they grow with the width while the layers grow with width squared times depth. In 16-bit, the 8B weights are 16.1 GB and the 70B weights 141 GB, which is the starting point for part 7’s memory sizing.

Where do the FLOPs go for each token?

Every weight in a matrix multiplication is used once per token, for one multiply and one add. That gives the standard rule: about 2 × parameters floating-point operations per token. The input embedding is a lookup, not a multiplication, so it does not count; the output head does. For the 8B model, that is 2 × (8.03B − 0.525B) ≈ 15.0 billion operations (GFLOPs) per token. For the 70B it is about 139 GFLOPs.

Attention adds work that is not in the parameter count: the scores and the weighted sum of values. For a token with n positions of history, each layer does 2 × n × 4,096 operations for the scores (32 heads × 128 = 4,096) and the same again for the mix, so 4 × n × 4,096 × 32 layers in total. That grows with context, while the weight work does not.

History (n tokens)8B weights8B attentionAttention share70B weights70B attentionAttention share
1,000 (chat turn)15.00.53%1392.62%
8,192 (RAG prompt)15.04.322%13921.513%
32,768 (coding agent)15.017.253%13985.938%
131,072 (full context)15.068.782%139343.671%

GFLOPs for one new token at that position. Attention share = attention ÷ (weights + attention).

1,000 tokensweights 15.0 + attention 0.515.5
8,192 tokensweights 15.0 + attention 4.319.3
32,768 tokensattention now exceeds weights32.2
131,072 tokensattention 82% of the work83.7

GFLOPs per new token for Llama 3.1 8B at each history length.

The worked number to remember: at 32,768 tokens of history, the attention maths for one new token in the 8B model (17.2 GFLOPs) is larger than the maths for all 8 billion weights (15.0). For prefill the effect is quadratic: a prompt of n tokens does roughly half of n × n score work per layer because of the triangular mask. A 2,000-token prompt on the 8B costs about 2,000 × 15.0 = 30.0 TFLOPs for the weights plus about 1.0 TFLOPs for attention.

What reads memory and what does maths, in prefill and decode?

Counting FLOPs is half the story. A GPU can only use a weight after reading it from memory, and in 16-bit each weight is 2 bytes. In one pass with T tokens, each weight does 2 × T operations. So the ratio of maths to bytes read is roughly T operations per byte: the number of tokens going through the layer together.

An H100 SXM does 989 TFLOPS in BF16 and reads 3.35 TB/s, a ratio of about 295 operations per byte (part 4). Any pass with fewer than about 300 tokens in flight is waiting on memory; any pass with more is waiting on maths. Our 7-token question is so short that even its prefill is memory-bound. A 2,000-token prompt is not.

Llama 3.1 8B, 16-bit, one H100Prefill: 2,000-token promptDecode: 1 token at position 2,000
Weights read16.1 GB, once for all 2,000 tokens16.1 GB, for one token
KV cache262 MB written262 MB read, 128 KB written
Operationsabout 31 TFLOPsabout 16 GFLOPs
Time if limited by mathsabout 31 ms0.016 ms
Time if limited by memoryabout 4.9 msabout 4.9 ms
What sets the paceCompute (Tensor Cores)Memory bandwidth

Ideal peaks, illustrative: real kernels reach a fraction of both, but the bottleneck is the same.

Prefill uses each weight it reads 2,000 times, so the Tensor Cores are the bottleneck and a longer prompt means a longer wait for the first token. Decode reads all 16 GB to do 16 GFLOPs of maths, spending about 300 times longer moving data than calculating. That is why batching many users into one decode step is nearly free until the batch reaches a few hundred (part 17), and why part 13 treats prefill and decode as two different workloads.

What is the KV cache in this walk-through?

It is exactly the K and V outputs from the attention half: 8 heads × 128 numbers for K and the same for V, in each of 32 layers, for every token so far. That is 2 × 32 × 8 × 128 × 2 bytes = 128 KB per token, and 896 KB for our 7-token question. During decode, the new token computes its own K and V, appends them, and reads every earlier token’s K and V to compute its attention. Nothing else from earlier tokens is needed, which is why the cache is enough to skip recomputing them.

The cache read grows with every token. At 2,000 tokens it adds 262 MB to the 16.1 GB weight read, under 2%. At 131,072 tokens it is about 17 GB, more than the weights, so each decode step takes roughly twice as long as it did at the start of the conversation. Multiply by the number of users in a batch and the cache, not the model, becomes what fills the GPU. Part 14 works through that sizing.

How are the layers split across GPUs?

Llama 3.1 70B in 16-bit is 141 GB of weights, more than one 80 GB H100 holds, before any KV cache. The walk-through above shows the two natural ways to cut it: across the width of each layer, or across the depth of the stack.

Tensor parallel, 4 GPUs every GPU holds a slice of every layer GPU 116 Q heads2 KV heads1/4 of MLP80 layers35 GB GPU 216 Q heads2 KV heads1/4 of MLP80 layers35 GB GPU 316 Q heads2 KV heads1/4 of MLP80 layers35 GB GPU 416 Q heads2 KV heads1/4 of MLP80 layers35 GB all-reduce after attention and after MLP 2 per layer, 160 per pass, 16 KB per token each needs NVLink inside one server Pipeline parallel, 4 GPUs each GPU holds a block of whole layers GPU 1: embedding + layers 1 to 20about 36 GB GPU 2: layers 21 to 40about 34 GB GPU 3: layers 41 to 60about 34 GB GPU 4: layers 61 to 80 + output headabout 36 GB residual stream: 8,192 numbers (16 KB) per token same hand-off at each boundary 3 hand-offs per pass; works across servers one request visits the GPUs one after another Llama 3.1 70B, 16-bit: 141 GB of weights split four ways (sizes approximate)

Tensor parallelism cuts every matrix and synchronises inside every layer. Pipeline parallelism cuts the stack and passes the residual stream along. The highlighted stage moves down as a request flows through.

Tensor parallelism (from Megatron-LM, Shoeybi et al. 2019) cuts each layer’s matrices. Q, K and V are split by heads: with 4 GPUs, each holds 16 of the 64 query heads and 2 of the 8 KV heads, and computes its heads without talking to the others. Wo is split by rows, so each GPU produces a partial sum of the full 8,192-number row, and an all-reduce adds the four together. The MLP is split the same way (gate and up into slices of 7,168 columns, down by rows), followed by another all-reduce. That is 2 all-reduces per layer and 160 per forward pass for the 70B. Each carries only 16 KB per token, but no GPU can continue until it finishes, so tensor parallelism stays inside a server on NVLink (part 6). The KV cache splits too: each GPU stores only its 2 KV heads, 80 KB per token instead of 320 KB. With 8 KV heads, a tensor-parallel size above 8 forces engines to copy KV heads onto several GPUs.

Pipeline parallelism gives each GPU a block of whole layers. Only the residual stream crosses between stages, 16 KB per token at each of 3 boundaries, so it tolerates slower links and can span servers. The catch is that layers run in order: one request visits GPU 1, then 2, 3 and 4, and the others wait unless the engine keeps several batches in flight. Pipeline parallelism adds memory capacity, not speed for a single request.

Tensor parallelPipeline parallel
What each GPU holdsA slice of every matrix in every layerAll matrices of some layers
Traffic per token, 70B160 all-reduces of 16 KB each3 hand-offs of 16 KB each
Link it needsNVLink, low latencyAnything reasonable, even between servers
Effect on one requestFaster: each GPU reads a quarter of the weights per stepNot faster: stages run one after another
KV cache per GPUIts own KV heads for all layersAll KV heads for its own layers
Typical useWithin one 4 or 8-GPU serverAcross servers, combined with tensor parallel inside each

In vLLM both are a single flag. The first command splits the 70B across 4 GPUs in one server; the second uses two 8-GPU servers, tensor parallel inside each and two pipeline stages across them:

vllm serve meta-llama/Llama-3.1-70B-Instruct --tensor-parallel-size 4

vllm serve meta-llama/Llama-3.1-405B-Instruct \
  --tensor-parallel-size 8 --pipeline-parallel-size 2

Part 5 covers multi-GPU basics and part 20 works out how many GPUs a given model and workload needs.

What do people get wrong about transformers?

  • “Attention is where the parameters are.” It holds 17% of them. The MLP blocks hold 70% in the 8B and 80% in the 70B. Attention’s cost is in the KV cache and in long-context maths, not in weights.
  • “A longer context only costs memory.” It also costs compute. At 32,768 tokens of history, attention maths for one token of the 8B is larger than the maths for all its weights.
  • “GQA makes attention cheaper to compute.” It makes it cheaper to store and read. There are still 32 query heads doing the same score maths.
  • “The model re-reads the whole conversation for every word.” Within one response, no: the KV cache holds the earlier tokens. Across requests, often yes: chat APIs are stateless, so each new turn sends the whole history and it is prefilled again unless the server’s prefix cache still holds it.
  • “Splitting layers across more GPUs makes one answer faster.” Layer 2 needs layer 1’s output, so pipeline stages run in turn. Only tensor parallelism shortens a single request’s step, and only while its all-reduces stay fast.
  • “The model scores the whole vocabulary for every prompt token.” In training it does. When generating, engines compute logits only for the last position, because that is the only one used to pick the next token.

Short answers to common questions

Why is the “8B” model 8.03 billion parameters?

Model names are rounded. Adding up the published shapes gives 8.030 billion, and the 70B gives 70.55 billion.

Does a longer prompt make the model use more memory?

The weights stay the same size. The KV cache grows by 128 KB per token for the 8B (320 KB for the 70B), and prefill needs temporary working memory that grows with prompt length.

Are the input embedding and the output head the same table?

Not in Llama 3.1 8B or 70B. The config sets tie_word_embeddings to false, so there are two separate tables of 0.525B (8B) or 1.051B (70B) parameters each.

Do all LLMs use these same parts?

Most current open decoder models (Llama, Qwen, Mistral) use RMSNorm, RoPE, SwiGLU and grouped-query attention with different sizes. Some change one block: Mixture of Experts models replace the MLP (part 12), and DeepSeek-V3 replaces GQA with a compressed attention (MLA) that stores about 69 KB per token.

What the transformer means for hardware
  • Seven matrix multiplications per layer do almost all the work: exactly what Tensor Cores are built for.
  • Every weight is read once per pass, so decode speed is set by memory bandwidth, and batching users is nearly free until a few hundred tokens are in flight.
  • K and V are stored per token, per layer: 128 KB per token for Llama 3.1 8B, 320 KB for the 70B. Grouped-query attention is why it is not four or eight times more.
  • 70% to 80% of the parameters sit in the MLP blocks, which is the part Mixture of Experts models make cheaper per token.
  • Attention maths grows with context and overtakes the weight maths of the 8B at about 32,000 tokens.
  • Tensor parallelism needs a fast link for 160 all-reduces per pass of the 70B; pipeline parallelism needs only 3 hand-offs but does not speed up a single request.

Sources: shapes and settings from Meta’s published Llama 3.1 configurations (8B: 32 layers, hidden size 4,096, MLP size 14,336, 32 query heads, 8 key-value heads, head size 128, vocabulary 128,256, rope_theta 500,000, 131,072 positions, untied embeddings; 70B: 80 layers, hidden size 8,192, MLP size 28,672, 64 query heads, 8 key-value heads). Parameter, FLOP and KV cache figures are counted from those shapes. Methods: Vaswani et al. 2017 (transformer, scaled dot-product attention); Zhang and Sennrich 2019 (RMSNorm); Su et al. 2021 (RoPE); Shazeer 2020 (SwiGLU); Ainslie et al. 2023 (grouped-query attention); Geva et al. 2021 (MLP layers as key-value memories); Shoeybi et al. 2019 (Megatron-LM tensor parallelism). H100 SXM figures from NVIDIA’s datasheet. Attention weights in the worked example are illustrative.

About The Author


Discover more from Journal of Intelligent Infrastructure

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Architect’s Toolkit

About the Author

Dr. Pranay Jha is a Cloud and AI Consultant with 18+ years of experience in hybrid cloud, virtualization, and enterprise infrastructure transformation. He specializes in VMware technologies, multi-cloud strategy, and Generative AI solutions. He holds a PhD in Computer Applications with research focused on Cloud and AI, has published multiple research papers, and has been a VMware vExpert since 2016 and a VMUG Community Leader.

Discover more from Journal of Intelligent Infrastructure

Subscribe now to keep reading and get access to the full archive.

Continue reading