The Four Basic Arithmetic Operations That Form the Mathematical Backbone of Large Language Models
This article breaks down how large language models work at the mathematical level, showing that nearly everything—embeddings, attention, softmax, MLPs, residual connections, normalization, and even training—rests on just four operations: addition, multiplication, max, and exponentiation with normalization. Readers will gain a clear, layer-by-layer intuition for how LLMs represent words, exchange information, generate text token by token, and learn from data, making the model easier to reason about, evaluate, and optimize.
1. From a Sentence to a String of Numbers
Computers cannot understand text directly, so everything starts with vectorization. Each word (or sub-word token) is mapped to a high-dimensional vector of real numbers, where every dimension is an ordinary number. These values originate from training and ultimately determine the semantic information each word carries—words with similar meanings sit closer together in the high-dimensional space.
The math involved here is only table lookup plus multiply-add: you look up an embedding matrix to turn a token ID into a vector. There is no calculus and no complex logic—the entire foundation is addition and multiplication.
2. How Words “See” Each Other: The Attention Mechanism
Within a sentence, figuring out what “it” refers to, or whether “bank” means a riverbank or a financial institution, depends on context. The attention mechanism lets each word “glance at” every other word in the sentence and absorb information weighted by relevance.
The computation can be summarized in three steps:
- Compute similarity: take the dot product of every pair of vectors—at its core, a chain of multiplications and additions;
- Compute weights: use the exponential function to turn scores into positive numbers, then divide by the sum to normalize—this is softmax;
- Weighted sum: each word’s new representation = the other words’ vectors added together according to their weights.
Why are there three roles, Q, K, and V? A useful way to think about it:
- Q (Query): what information am I looking for;
- K (Key): what label can I offer;
- V (Value): the actual content I carry.
Q and K handle “pairwise scoring,” while V handles “delivering content.” All three are produced from the same input through three different linear transformations, and a linear transformation is still just multiplication and addition. The reason for separating the three roles is that searching for information and expressing information can learn different patterns, greatly increasing expressive power.
3. Softmax: Turning “Who Matters More” into Probabilities
Raw scores can be any real number. Softmax does two things:
- It uses the exponential function
exp(x)to turn all values into positive numbers while amplifying differences; - It divides by the sum so the results fall between 0 and 1 and add up to 1.
Example code:
import numpy as npdef softmax(x):e = np.exp(x - np.max(x)) # subtract the max for numerical stabilityreturn e / e.sum()
Softmax is one of the few “nonlinear amplifiers” in the whole model, and it is the tool the output layer uses to turn scores into probabilities. Without it, the model could neither distribute weights in attention nor “speak” a probability distribution over the next word at the end.
4. What the MLP Does: Pointwise Nonlinear Filtering
Attention handles “exchanging information between words,” while the multi-layer perceptron (MLP) handles “deep processing within each word.” It applies two linear transformations independently to the vector at each position, with a nonlinear activation in between (such as ReLU, which is “take the maximum, max(x, 0)”).
The mathematical meaning of ReLU is extremely simple: negatives become zero, positives are kept. This is a kind of “filtering”—switching off useless feature channels and letting only the useful ones through. Combined with an expand-then-contract bottleneck structure, the MLP effectively projects information into a wider space to filter it, then compresses it back, storing a large amount of retrievable “knowledge.”
5. Residual Connections and Normalization: Why Both Are Essential
Dozens or even hundreds of consecutive matrix multiplications cause values to explode or vanish quickly. Two engineering remedies address this:
- Residual connection: output = sub-layer output + original input. The identity mapping guarantees a “highway” for gradients to flow back directly, making deep networks trainable;
- Normalization: pulling each layer’s output back into a stable range to avoid drifting numerical scales.
A residual is addition; normalization is subtracting the mean and dividing by the variance (subtraction and multiplication). At this point, all four operations have appeared: addition, multiplication, max, and exponentiation with normalization. All of a Transformer’s “intelligence” is built on these four building blocks.
6. Why Generation Happens One Token at a Time
The model’s output is a probability distribution over the next word. After generating the first word, you append it back to the input and compute the next one, repeating in a loop. This explains two common phenomena:
- Generation is serial: even though inference is highly parallel internally, the token-by-token chain cannot be fully parallelized;
- The same prompt produces slightly different answers each time: at each step the model does not always pick the highest-probability word but samples from the distribution (optionally with temperature adjustment), introducing randomness. Sampling brings diversity, and also makes answers not fully reproducible.
7. How Hundreds of Billions of Parameters Are Learned
During training, the parameters start as random numbers and gradually learn to “speak human” through the following loop:
- Feed in a piece of text and have the model predict the next word at every position;
- Use softmax to get a probability distribution, compare it with the correct answer, and compute the cross-entropy loss;
- Use backpropagation to compute each parameter’s contribution to the loss (this is the only place where derivatives are used heavily);
- Nudge all parameters in the direction that reduces the loss, and repeat hundreds of millions of times.
175 billion parameters are not mysterious—the vast majority live inside the attention matrices and the linear transformations of the MLPs, that is, in huge multiplication weight tables. The essence of training is to keep fine-tuning these numbers against massive amounts of text.
8. Summary
Inference answers “given a sentence, how is the next word computed”; training answers “where do these numbers come from.” Either way, the underlying layer has only four kinds of operations:
| Operation | Where it appears | Role |
|---|---|---|
| Multiplication | Dot products, linear transformations | Similarity computation, information transformation |
| Addition | Weighted sums, residuals | Aggregating information, stabilizing gradients |
| Max | ReLU | Nonlinear filtering |
| Exponentiation and normalization | softmax, LayerNorm | Producing probabilities, stabilizing distributions |
Once you understand these four building blocks, a large model is no longer a black box but a mathematical system that can be fully decomposed and examined layer by layer. For developers who want to study further or handle engineering deployment (such as building inference services on cloud GPUs and container platforms), this mathematical intuition is also the prerequisite for evaluating performance and doing quantization and optimization.
