0%
LLM Inference and Serving: GPUs, Batching, and Cost
Hardware and Memory
Making Inference Fast
Serving Systems
The Forward Pass for Engineers
You do not need to train a transformer to serve one well, but you do need to know what a forward pass reads, computes and stores, because that determines memory, speed and cost. This lesson walks through a decoder only transformer of the kind used by Llama, Mistral, Qwen, DeepSeek and the closed frontier models, with an eye on the numbers a serving engineer needs. This first section covers how tokens enter the model and the residual stream that carries information through it.
The embedding table
The forward pass begins with a lookup. The embedding table is a matrix with one row per vocabulary entry and one column per hidden dimension. For Llama 3 8B, the vocabulary has 128,256 entries and the hidden size is 4,096, so the table has about 525 million numbers. Each token id selects its row, giving a vector of 4,096 numbers that represents that token. There is no arithmetic here, only memory reads, and only the rows for the tokens in the batch are read, so embedding lookups are cheap even though the table is large.
The table is a meaningful part of the parameter count of small models. For Llama 3 8B, the input embeddings and the separate output projection together account for about one billion of its eight billion parameters. Some models tie the two, using the same matrix for input and output, which saves memory. For very large models the embeddings are a small fraction, and the transformer layers dominate.
The residual stream
After the lookup, each position carries a vector of hidden size through the stack of layers. This vector is called the residual stream. Each layer reads the stream, computes something and adds its result back to the stream rather than replacing it. Information from early layers is therefore still present at the end, and each layer can be thought of as writing a small update into a shared channel. The width of the stream, the hidden size, is one of the main dimensions that sets the size and cost of the model.
Hidden sizes for current open models range from about 2,048 for small models to 8,192 for Llama 3 70B and more than 16,000 for the largest. Doubling the hidden size roughly quadruples the parameters of each layer, since most weight matrices are hidden size by some multiple of hidden size. The stream width also determines the size of activations that must be passed between GPUs when a model is split across devices, which matters for the parallelism strategies in lesson seven.
Normalization
Before each sublayer, the stream is normalized. Modern models use RMSNorm, which divides the vector by its root mean square and multiplies by a learned scale. Normalization keeps values in a stable range so that deep stacks train reliably. It costs very little compute but it is one of several small operations, along with activation functions, rotary position encoding and residual additions, that each read and write the full activation vector. Individually cheap, together they can add measurable time in decode, which is why inference engines fuse them into neighboring kernels instead of launching each one separately.
Two sublayers per layer
Each transformer layer contains two sublayers. The attention sublayer lets each position gather information from earlier positions. The feed forward sublayer, also called the MLP, transforms each position independently with two or three large matrix multiplications. Both are wrapped in the same pattern: normalize the stream, apply the sublayer and add the result back. A model is just this layer repeated many times: 32 times for Llama 3 8B, 80 times for Llama 3 70B and 61 times for DeepSeek V3.
This structure is regular, which makes it easy to reason about. If you know the parameters and cost of one layer, you know the whole model up to the embeddings and the final projection. It also makes the model easy to split: pipeline parallelism assigns contiguous groups of layers to different GPUs, and tensor parallelism splits the matrices inside each layer. Both are covered in lesson seven.
The output projection
After the last layer, a final normalization is applied and the vector at each position is multiplied by the output projection, a matrix of hidden size by vocabulary size, to produce one logit per vocabulary entry. During decode only the last position needs logits, but during prefill an engine computes them only for the final prompt position too, since earlier positions do not choose tokens. For large vocabularies this projection is a noticeable cost per step: with 128,000 entries and a hidden size of 4,096, it is about half a billion multiply accumulates per position, comparable to a couple of transformer layers.
Precision of the stream
Weights and activations are commonly stored in 16 bit formats, either bfloat16 or float16, with some accumulations done in 32 bit for accuracy. Bfloat16 has the same exponent range as 32 bit float with less precision, which avoids overflow in large activations and is the default for most modern models. Lower precision formats, 8 bit and 4 bit, are used for weights and sometimes activations in quantized deployments, covered in lesson ten. The precision of each tensor directly sets its memory footprint and the bytes read per step, which is why precision is the first lever engineers reach for when memory or bandwidth is tight.
Activations are small in decode
During decode each sequence contributes one position, so the activations for a step are tiny: a batch of 64 sequences at hidden size 4,096 in 16 bit is about half a megabyte. The weights read in the same step are 16 gigabytes. This ratio is the root of why decode is memory bound and why batching works: adding a sequence adds a sliver of activation memory and compute while reusing the same weights. During prefill of long prompts the activations are much larger, since every prompt position is present, and engines size their workspace for the largest prefill chunk they will run.
What the stream means for serving
For a serving engineer, the residual stream view gives three practical facts. The model is a stack of identical layers, so cost and memory scale linearly with depth. The hidden size sets the width of every matrix and every activation, so it determines both parameters and communication volume. And the per position work is independent across positions except inside attention, so everything outside attention parallelizes perfectly across tokens in prefill and across sequences in a batch. Attention is the exception, and it is the subject of the next section.
Where the stream lives in memory
During a step, the residual stream for the current batch lives in GPU memory as a matrix with one row per position being processed and one column per hidden dimension. In decode that is one row per active sequence; in prefill it is one row per prompt token in the chunk. Each sublayer reads this matrix, multiplies it by weight matrices and writes a new matrix of the same shape back. Engines allocate these buffers once, sized for the largest batch and chunk they will run, and reuse them every step to avoid allocation overhead. The size of this workspace is one of the reasons engines reserve some memory beyond weights and cache, and one of the knobs that trades maximum prefill chunk size against cache capacity.
When a model is split across GPUs, the stream is what moves between them. With pipeline parallelism, the stream at the boundary layer is sent from one stage to the next. With tensor parallelism, partial results of the stream are summed across GPUs after attention and after the feed forward block in each layer. In both cases the volume is proportional to the hidden size times the number of positions, which is why wide models generate more communication per token than narrow ones of the same depth.
A decoder only transformer is one layer repeated dozens of times plus an embedding table and an output projection. Know the parameters, FLOPs and cache size of one layer and you can estimate the whole model with a multiplication.