02.04

Transformer blocks and attention

skillmaxingInference EngineeringTransformer blocks and attention
Visual lesson

Transformer blocks and attention

Included with Pro · 4:45
Lessons · 1,176 words

A decoder-only transformer is one block copied many times. Llama 3.1 8B stacks 32 of them, Qwen3-30B-A3B stacks 48. Each block does two things to every token's vector. Attention lets the token gather information from the tokens before it. The MLP then transforms each token on its own. Both results are added back onto the vector that came in, so information flows straight down the stack and each block only adds a correction.

One block, repeated 32 times
Figure 1One block, repeated 32 timesAttention is the only step where tokens see each other. The MLP works per token.

In code, the whole block is four lines: normalize, attend, add; normalize, MLP, add. The MLP and the projections are the matrix multiplications you already counted. Attention is different, because it is the one operation whose work depends on how many tokens are in the context, and that is where long prompts get expensive.

Pro

Continue reading

Pro unlocks every video lesson, the full notes, worked sizing and cost calculations, and runnable configs across the Inference Engineering course, from GPUs and kernels to quantization, speculative decoding, disaggregation and production autoscaling.

Unlock full access to
Python with AIInference EngineeringAI researchComplete system design
30% off with LAUNCH30first payment
Buy now
View all plans ·Already Pro? Sign in