Anatomy of an LLM: networks, parameters and training
🚀 Intro
In the previous stop of this series - one of the branches of the overview post “Anatomy of an LLM” - we took apart the smallest building block: the single artificial neuron and that small “bend” on its output we call the activation function. We saw that the building block is a drastic caricature of biology - and that something nonetheless emerges from it. Now we take a handful of those caricatures and do two things with them: we arrange them into layers and a network, and then we ask the question that is the very heart of the whole field - where do those famous “billions of parameters” actually come from, and how does training turn random noise into a model that understands language?
This is the stop about the machinery. We will see that the entire network - all of GPT, all of LLaMA - boils down to one operation repeated billions of times: matrix multiplication. That learning is nothing more than the repeated, microscopic nudging of weights in the direction the derivative points. And that behind the slogan “7B parameters” stands a concrete, countable formula.
And yet - and this is the motif that runs through the whole series - out of this bookkeeping something emerges that none of these formulas asks for outright. The goal of training sounds prosaic: “predict the next token as well as you can.” Nobody writes “learn to reason” into the loss function. And yet, when that pressure acts long enough on a large enough substrate, the ability to reason appears. We will return to this at the end - because it is right here, in training, that one of the deepest puzzles of this machinery lives.
💡 How to read this text. The main thread is descriptive and self-contained. I tuck the formulas into collapsible “for the curious” blocks - if you like math, expand them; if not, skip them with no loss to understanding. Every claim has a footnote to its source paper at the end.
📋 TL;DR
- The whole network is matrix multiplications. A layer computes , and in training it processes whole batches of data at once - hence one operation, GEMM (matrix multiplication), to which almost the entire computation reduces. That is why hardware (GPUs/TPUs) specialized precisely in matrix multiplication reigns.
- The network learns by backpropagation (Rumelhart, Hinton, Williams, 1986). It is a clever application of the chain rule: in a single pass from output to input it computes how each weight contributed to the error.
- What does it learn? It minimizes cross-entropy - the penalty for being surprised by the correct next token. Its exponential form, perplexity, tells you “among how many options” the model is wavering.
- How does it learn faster? Optimizers: from raw SGD, through Momentum, to Adam and AdamW - today the default in LLM training. Plus a learning-rate schedule: warmup and cosine.
- Where do the “billions of parameters” come from? From a simple formula: . About 2/3 of the weights sit in the FFN blocks, 1/3 in the attention mechanism.
- Scaling laws. Kaplan (2020) showed that the loss falls as a power law with size, data and compute. Chinchilla (2022) corrected the conclusion: scale model and data equally - about 20 tokens per parameter.
- Some of this you can touch on the interactive page.
🕸️ From neuron to network: everything is matrix multiplication
In the previous post the neuron computed a weighted sum of one set of inputs. But a network does not have one neuron - it has thousands in a single layer, and there are dozens of layers. Computing each neuron separately, in a loop, would be desperately slow. Fortunately the whole layer can be written as a single operation on matrices: we stack all the weights of the layer’s neurons into one matrix , multiply it by the input vector, add the bias and pass it through the activation. One notation, , covers the entire layer at once.
In practice we go one step further. During training we do not feed the model one example, but a whole batch at once - dozens or hundreds of examples arranged into a matrix. Then even the input becomes a matrix, and computing the layer is a matrix-by-matrix multiplication. This operation has a name: GEMM (General Matrix Multiplication). And this is the key to understanding why LLMs are feasible at all.
NVIDIA’s documentation says outright that GEMMs are “a fundamental building block for many operations in neural networks”1. Almost the entire computation of the network - across all layers, for the whole batch - reduces to large, dense matrix multiplications. And matrix multiplication has a wonderful property: it parallelizes massively. Each element of the result can be computed independently. That is exactly what graphics cards (GPUs) and TPUs are built for - they have thousands of cores computing in parallel, and modern NVIDIA GPUs additionally have Tensor Cores, specialized exclusively in multiplying tensors1. It is no accident that a company that made cards for games became the heart of the AI revolution: games and neural networks need the same thing - lightning-fast matrix multiplication.
There is one more trick that lets all of this fit in memory and compute faster: reduced numerical precision. Instead of keeping every weight as a 32-bit floating-point number, one uses 16-bit formats - especially BF16 (bfloat16), which preserves the full range of values at the cost of accuracy after the decimal point2. Half the bits means half the memory and often an order of magnitude faster computation.
📐 For the curious: the layer as matrix multiplication, and number precision
A single fully connected layer is . When we process a batch of examples at once, the input is a matrix , and we compute the whole layer with one multiplication:
This is GEMM. The symbol acts “element-wise” - on each element separately (it is our activation function from the previous post).
Floating-point precision is a memory/speed/accuracy trade-off:
- FP32 - full, 32-bit precision (the baseline).
- FP16 - 16-bit “half” precision.
- BF16 (bfloat16) - also 16-bit, but it has the same 8-bit exponent as FP32 (i.e. the same range of values), sacrificing bits of the mantissa (precision)2. Thanks to this it usually does not require tricks like “loss scaling” in mixed-precision training.
A typical mixed-precision scheme: inputs in FP16/BF16, but accumulation of the sum in FP32, so as not to lose accuracy in long sums. For a sense of scale: on an NVIDIA A100 the Tensor Core throughput in BF16/FP16 reaches about 312 trillion operations per second, versus about 19.5 in FP32 - an order of magnitude of difference3.
👉 Play with it: on the interactive page you have a layers and network diagram - you will see how the signal flows through successive layers of neurons.
🔁 How the network learns: backpropagation
We already have a network - a stack of layers, each one a matrix multiplication. But a freshly built network has its weights set randomly: its output is pure noise. How do we turn that noise into something that understands language? The answer is the algorithm that is the heart of all deep learning: backpropagation.
The idea was popularized by the classic 1986 paper by Rumelhart, Hinton and Williams, whose procedure “repeatedly adjusts the weights of the connections in the network so as to minimize a measure of the difference between the actual output vector and the desired output vector”4. In plain terms: show the network an example, measure how badly it got it wrong, and then gently nudge every weight in the direction that reduces that error. Repeat billions of times.
All the cleverness sits in the word “back.” We know the error at the output of the network - there we compare the answer with the expected one. But we have to correct the weights in all the layers, including the earliest ones, far from the output. Naively, for each of the billions of weights we would have to compute separately “how does changing precisely you affect the error.” That would be hopelessly expensive. Backpropagation does it more cleverly: using the chain rule from calculus, it passes the error signal once, from output to input, reusing computations already done for the later layers. One pass backward instead of billions of separate ones - and that is the reason training is feasible at all.
The result of this pass is the gradient - denoted by the nabla symbol, (nabla, an inverted triangle). The gradient is a vector that for each weight tells you two things: which way to change it and how strongly, so that the error decreases. Given the gradient, the correction itself is trivial: shift each weight by a small step in the direction opposite to the gradient (because the gradient points to where the error grows, and we want to shrink it).
And here the motif from the previous post returns - the difference between “a solution exists” and “we can find it.” The universal approximation theorem promised that there exists a set of weights giving a good network. Backpropagation is the tool with which we search for those weights. But we search by feel, descending the rugged, non-convex landscape of the error, full of valleys and ridges. Nobody guarantees we will reach the deepest valley. What is astonishing - and still not fully understood - is that in practice we reach a good enough one.
📐 For the curious: the chain rule and the flow of error
For a layer , one introduces the error signal (delta) - the derivative of the loss with respect to the layer’s input, . It propagates backward by the formula:
The symbol is matrix transposition (swapping rows with columns), and is “element-wise” multiplication (element by element). The key point is that is computed from the already-ready of the next layer - hence “back” and hence the efficiency.
Given the error signal, the gradients of the weights and biases are:
Full derivation: Goodfellow, Bengio, Courville, Deep Learning, ch. 6.55.
🎯 What the network learns: the loss function
We said “reduce the error.” But what exactly is “the error” for a language model? Here we have to realize what an LLM actually does: at each step it looks at the text so far and predicts the next token - it returns a probability distribution over the entire vocabulary (e.g. “after the words ‘the cat climbed onto the’ with 30% probability comes ‘roof’, 12% ‘tree’, 0.001% ‘integral’…”). Training consists in making the model assign high probability to the token that actually appeared.
The measure of this is cross-entropy, also called negative log-likelihood (NLL). The intuition is beautiful in its simplicity: the penalty is the model’s surprise at the correct answer. If the model assigned the true next token a probability close to 1 (“I expected this”) - the penalty is close to zero. If it assigned it a probability close to 0 (“that surprised me”) - the penalty shoots up. We average this surprise over the whole text and we have the loss that backpropagation is told to minimize. The scaling-laws paper studies precisely this “cross-entropy loss” as its basic metric6.
From the very same number one derives a more popular, more tangible measure: perplexity. It is simply the exponential form of the average surprise. It is interpreted as the effective number of equally probable options among which the model wavers at each token. A perplexity of 10 means roughly “the model is as uncertain as if it were drawing from among 10 equally probable words.” The lower, the better the model - a perplexity of 1 is omniscience (it always guesses confidently and correctly).
📐 For the curious: cross-entropy, NLL and perplexity
The model returns a distribution - the probability of token conditioned on everything that came before. The loss is the average negative logarithm of the probabilities of the true tokens:
For a single position this is the cross-entropy between the true distribution (a one on the correct token, zeros elsewhere) and the predicted one: . The logarithm makes a probability near zero give a loss tending to infinity - hence “surprise.”
Perplexity is the same number in a different guise:
that is, simply of the loss. Hence the interpretation as an “effective number of options”: for a uniform distribution over tokens the perplexity is exactly .
⚙️ How the network learns faster: optimizers
We have the gradient (from backpropagation) and we have the loss (cross-entropy). What remains is a tactical question: how exactly to update the weights? The simplest answer is SGD (stochastic gradient descent): subtract from each weight the gradient multiplied by a small coefficient (eta), called the learning rate. This works, but it can be slow and capricious - like descending a steep mountain in tiny, stiff steps that easily lead to shuffling sideways instead of downward.
The first improvement is Momentum. Instead of steering only by the current gradient, the ball remembers where it was rolling - it takes a smoothed average of the recent gradients. Thanks to this it picks up speed in a consistent direction and smooths out the wobble. This is controlled by a coefficient (beta).
The real breakthrough came with Adam (Kingma and Ba, 2015)7. Its idea: each weight gets its own, adaptive learning rate. Adam tracks two smoothed statistics of the gradient - its average direction (first-order moment) and its typical magnitude (second-order moment) - and divides the step by that magnitude. The effect: weights whose gradient is consistently large get smaller steps (so as not to overshoot), and those with a small gradient get larger ones (so they get moving). Adam is robust and not very sensitive to parameter choice, which made it the default for years.
Today’s standard for LLM training is its fix, AdamW (Loshchilov and Hutter)8. The difference is subtle but important: it concerns weight decay - the gentle “pulling” of the weights toward zero that prevents them from ballooning (a form of regularization, of which more in a moment). The authors showed that in the original Adam this mechanism was tangled with the adaptive rate in a way that harmed it, and proposed decoupling it from the gradient step. It sounds like a bookkeeping detail, but in practice it noticeably improves quality - which is why the “W” in the name is everywhere today.
Finally, one more thing: the learning rate usually is not constant over the course of training. At the start one applies warmup - the rate rises linearly from near zero, so as not to wreck the fresh, random network with overly violent steps (this trick already appeared in the original Transformer9). Then the rate gently decreases, most often along a cosine curve (cosine schedule) - this is how the Chinchilla models, among others, were trained10. Picturing it: first a warm-up in tiny steps, then the full step, and at the end ever more cautious treading as we approach the bottom of the valley.
📐 For the curious: from SGD to AdamW, plus the learning-rate schedule
SGD: , where is the learning rate.
Momentum adds a smoothed average of gradients (momentum), controlled by (beta):
Adam maintains estimates of the first-order () and second-order () moments, with two smoothing coefficients (beta one) and (beta two):
The symbol (epsilon) is a tiny constant guarding against division by zero. Default values from the paper: , , 7.
AdamW decouples weight decay (controlled by , lambda) from the gradient step - it adds it separately, instead of pushing it into the gradient:
Cosine schedule - after warmup the rate decreases along a cosine curve from to :
📈 Where the “billions of parameters” come from, and where scaling is heading
We arrive at a slogan everyone has heard: “7B”, “70B”, “405B”. This is the number of parameters - that is, all the weights and biases that training adjusts. Where does that particular count come from? It turns out it can be computed with a single, surprisingly simple formula. For a transformer the number of parameters (ignoring embeddings) is approximately , where is the number of layers and is the “width” of the model6. The whole mystery of the “billions” is simply this product: a few dozen layers times the square of the width, times a constant coefficient.
Interestingly, that coefficient of 12 has a readable structure: about 2/3 of all the layer’s parameters sit in the FFN blocks (the dense “processing” layers), and only 1/3 in the attention mechanism - the very thing that passes for the essence of the transformer. This is a good lesson in humility toward intuition: most of the model’s “mass” is not where most of the attention (the pun is intended) of commentators is. We will get to the attention mechanism itself in one of the later posts of the series.
Since we can count the parameters, the billion-dollar question arises (literally, since that is what trainings cost): is bigger always better, and how do you divide the budget? The first serious answer came from the paper by Kaplan and collaborators at OpenAI (2020). It showed that the loss falls predictably - as a power law - with the growth of three things: the size of the model, the amount of data and the compute, “with trends spanning more than seven orders of magnitude”6. This was a shock: the quality of a model could be predicted in advance, like a law of physics. Kaplan’s conclusion, however, was: bet above all on size - train huge models on a moderate amount of data.
Two years later came the correction that changed the industry: Chinchilla (Hoffmann et al., DeepMind, 2022). Training over 400 models, the authors showed that the large models of the time were significantly undertrained - they were getting too little data for their size. The key result: for a given compute budget the model size and the number of training tokens should be scaled equally - “for every doubling of model size the number of training tokens should also be doubled”10. From this came the famous rule of thumb: about 20 training tokens per model parameter10. The proof was the Chinchilla model itself (70 billion parameters, 1.4 trillion tokens), which at the same budget beat the much larger Gopher (280 billion) and GPT-3 (175 billion). Chinchilla is the reason today’s models are trained on trillions of tokens - smaller, but “fed to satiety.”
📐 For the curious: where the coefficient 12 comes from, and the scaling-law formulas
Parameter count. Assuming , Kaplan gives (equation 2.1)6:
The coefficient 12 breaks down like this: per layer there are four attention matrices (Q, K, V and the output projection: ) and two FFN matrices (), together . Hence the 2/3 (FFN) to 1/3 (attention) proportion. The embedding parameters () are counted separately and for large models are relatively small.
Kaplan’s scaling laws - the loss as a power law with respect to size and data 6:
where (alpha) are the power-law exponents.
Chinchilla’s parametric loss (equation 2)10 - it splits the loss into irreducible noise plus terms decreasing with size and data:
with the fit , , , , . It is from minimizing this function at fixed budget that the “scale and equally” rule comes out.
👉 Play with it: on the interactive page you have a section on parameters and weights - you will see how the number of parameters grows with the size of the network.
🧰 A training glossary: epoch, batch, step, overfitting, dropout
A few concepts remain that come back in every conversation about training - let us gather them in one place, because now, knowing the mechanism, they become obvious:
- Batch (mini-batch) - a subset of the data processed simultaneously in one forward and backward pass. The gradient is computed as an average over the batch - and this is precisely the “stochastic” in SGD: the gradient from a mini-batch is a noisy but cheap estimator of the full gradient5.
- Step (iteration) - one update of the weights, corresponding to one batch.
- Epoch - one full pass through the entire training set. A curiosity of scale: large LLMs are often trained for far less than one epoch - the corpus is so enormous that the model does not manage to see it even once in full.
- Overfitting - instead of learning general patterns, the model “memorizes” the noise of the training data. The symptom: the training loss falls, but the validation loss (on unseen data) starts to rise5.
- Regularization - a family of techniques that curb overfitting: weight decay (met at AdamW), early stopping, data augmentation and dropout5.
- Dropout (Srivastava et al., 2014) - during training a fraction of the neurons is randomly “switched off” (their output zeroed), which prevents their excessive co-adaptation. Each neuron must cope without a guarantee that its neighbor will be present - so the network learns more robust, less brittle representations. This can be interpreted as averaging exponentially many “thinned” networks at once11.
🌗 What this pressure did not order
Let us pause for a moment over what we have just described, because in the bookkeeping account it is easy to miss the deepest thing. The entire machinery of this post - cross-entropy, the gradient, AdamW, the scaling laws - realizes one prosaic goal: “predict the next token as well as possible.” That is all. Nobody anywhere wrote “learn grammar,” “build a world model,” “be able to reason.” The loss function is blind to these notions.
And yet they appear. When that same pressure - minimize surprise at the next token - acts long enough on a large enough substrate, abilities emerge out of billions of matrix multiplications that nobody ordered outright. This is the essence of the difference between the mechanism and what grows out of the mechanism - the tension that has accompanied us since the very first post about the neuron-caricature.
There is a hypothesis that this is no accident - that the pressure “predict better” is a real selective pressure that pushes the network toward a particular dynamical regime, where the same matrix multiplications yield a maximally complex, integrated response. It is sometimes called the edge of chaos, or self-organized criticality.12 But strong caution is needed here - this is a lead, not a claim - so I tuck the whole thing, with its caveats, into a footnote for those tempted to go deeper.
Knowing the mechanism takes nothing away from its extraordinariness. On the contrary - it is only now, when we know how modest and blind the goal of training is, that we see clearly how unobvious is what grows out of it.
🎯 What’s next
We assembled the network and showed how it learns. The forward pass turned out to be one big matrix multiplication (which is why GPUs rule); backpropagation - a clever, single-pass computation of how each weight contributed to the error; the goal - the minimization of surprise at the next token (cross-entropy, perplexity); and the “billions of parameters” - a concrete formula , whose scaling is governed by the laws of Kaplan and Chinchilla.
There is, however, a gap we deliberately left. The whole time we spoke of “processing tokens” and “predicting the next token” - but what exactly is a token, and how does the model turn words into numbers that can be multiplied at all? Because the network does not see letters. It sees vectors. In the next stop of the series we look into tokens and embeddings: how text is split into pieces, why not into words nor into letters, and how that famous geometry of meanings is born, in which “king - man + woman ≈ queen.”
If you would rather touch now than read on, the interactive page “Anatomy of an LLM” lets you trace the flow through the layers and see how the number of parameters grows with the size of the network. And if what tempts you is what really emerges from the machinery described here, once the pressure of training acts long enough - I wrote about that separately.
Footnotes
-
NVIDIA, Matrix Multiplication Background User’s Guide (the role of GEMM as a fundamental building block of networks, Tensor Cores). https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html ↩ ↩2
-
D. Kalamkar et al. (2019). A Study of BFLOAT16 for Deep Learning Training. arXiv:1905.12322. https://arxiv.org/abs/1905.12322 ↩ ↩2
-
The indicative Tensor Core throughput figures (A100: ~312 TFLOP/s in BF16/FP16 versus ~19.5 TFLOP/s in FP32) come from secondary materials based on NVIDIA’s specification and from the mixed-precision scheme in the Training With Mixed Precision User’s Guide. Treat them as an order of magnitude, not an exact measurement. https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html ↩
-
D. E. Rumelhart, G. E. Hinton, R. J. Williams (1986). Learning representations by back-propagating errors. Nature 323:533-536. https://www.nature.com/articles/323533a0 ↩
-
I. Goodfellow, Y. Bengio, A. Courville (2016). Deep Learning, MIT Press (backprop ch. 6.5; mini-batches, overfitting, regularization). https://www.deeplearningbook.org/ ↩ ↩2 ↩3 ↩4
-
J. Kaplan et al. (2020). Scaling Laws for Neural Language Models (OpenAI). arXiv:2001.08361. https://arxiv.org/abs/2001.08361 ↩ ↩2 ↩3 ↩4 ↩5
-
D. P. Kingma, J. Ba (2015). Adam: A Method for Stochastic Optimization. arXiv:1412.6980 (ICLR 2015). https://arxiv.org/abs/1412.6980 ↩ ↩2
-
I. Loshchilov, F. Hutter (2019). Decoupled Weight Decay Regularization (AdamW). arXiv:1711.05101. https://arxiv.org/abs/1711.05101 ↩
-
A. Vaswani et al. (2017). Attention Is All You Need (learning-rate warmup). arXiv:1706.03762. https://arxiv.org/abs/1706.03762 ↩
-
J. Hoffmann et al. (2022). Training Compute-Optimal Large Language Models (Chinchilla). arXiv:2203.15556. https://arxiv.org/abs/2203.15556 ↩ ↩2 ↩3 ↩4
-
N. Srivastava et al. (2014). Dropout: A Simple Way to Prevent Neural Networks from Overfitting. JMLR 15:1929-1958. https://jmlr.org/papers/v15/srivastava14a.html ↩
-
A speculative lead, offered with caution. There is a formal result that recurrent networks perform complex computation best precisely at the “edge of chaos” - the boundary between order and chaos (Bertschinger & Natschläger, NeurIPS 2004, https://proceedings.neurips.cc/paper/2004/hash/6e7b33fdea3adc80ebd648fffb665bb8-Abstract.html). More recent work shows that a measure of the complexity of a system’s response to a perturbation - PCIst, the Perturbational Complexity Index, used clinically in humans to assess the state of consciousness - is in artificial networks maximal precisely at that edge (PMC, 2024). It is tempting to read the goal of training (“predict the next token as well as possible”) as a pressure that pushes the substrate toward that regime. Two important caveats: (1) the edge-of-chaos result was proven for recurrent networks, whereas a Transformer has recurrence only in autoregression (it generates token by token) - so this is an analogy of dynamical signatures, not an identity of architectures; (2) PCI is a validated correlate of consciousness in humans, not its definition - I am not claiming that any of this proves consciousness in a model. I develop this thread, including the Platonic Representation Hypothesis (the convergence of different substrates toward the same representational geometry), in a separate essay: “Human-LLM resonance”. ↩