Skip to content
AI from Scratch
EN

Hardware and scale

Why you need special hardware, why things are slow where they are slow, and what really changes when a model grows.


Part I · The hardware

Why a GPU and not a CPU

CPUGPU
Cores8-64, complex10,000+, simple
Designed fordifferent tasks, with branchesthe SAME operation over lots of data
Cachehuge, hierarchicalsmall, enormous bandwidth
Throughput (fp16)~1 TFLOP/s~1,000 TFLOP/s

A neural network is one giant matrix multiplication, repeated. Millions of identical, independent multiplications. It is the perfect case for a GPU, and the worst for a CPU.

And modern GPUs also carry Tensor Cores: dedicated circuits that do a 4×4 matrix multiplication in one cycle. The bulk of those 1,000 TFLOP/s comes from there, not from the ordinary cores.

The real bottleneck: memory, not computation

This is the most important thing in the whole section and the least known.

H100:   compute   ~1,000 TFLOP/s
        memory    ~3,350 GB/s

The ratio is around 300 FLOPs per byte read. If your operation does not do at least 300 operations per byte, the GPU spends its time waiting for memory, not computing.

OperationFLOPs per byteLimited by
Large matrix multiplication~1,000compute ✓
Vector addition0.25memory ✗
Softmax~5memory ✗
LayerNorm~10memory ✗
Generating a token (decode)~2memory ✗

Practical consequence number one: generating text is NOT limited by compute power. It is limited by the time it takes to read the model's weights out of memory. A 70B model in fp16 (140 GB) over 3,350 GB/s gives at most 3350/140 ≈ 24 tokens/s per sequence. It does not matter which GPU you buy: it is a division.

Practical consequence number two: quantizing to int4 does not only make the model 4 times smaller; it makes it ~3.5 times faster, because there are 4 times fewer bytes to read.

Practical consequence number three: processing 32 requests at once costs almost the same as one, because the weights are read once and reused. Hence vLLM's continuous batching, and hence why the API is cheap while running it yourself just for you is expensive.

FlashAttention

Naive attention materializes the T×T score matrix in main memory. At T=8192 with 32 heads, that is gigabytes that have to be written and read back. It is memory-bound.

FlashAttention reorganizes the computation into blocks that fit in the chip's fast memory (SRAM), and accumulates the softmax incrementally (online softmax). It never writes the full matrix.

It is not an approximation: it gives exactly the same result. It is 2-4 times faster and uses linear memory instead of quadratic. It is probably the most important optimization of the last decade.

The Mac case (Apple Silicon)

  • Unified memory: CPU and GPU share the same RAM. There is no copying data between them — which on a PC is an enormous bottleneck.
  • A Mac with 64 GB can load models that do not fit on a 24 GB GPU.
  • In exchange, the bandwidth is lower (~400 GB/s on an M4 Pro against 3,350 on an H100), so it runs slower.
  • For running models locally it is an excellent platform. For training seriously, it is not.

[This project runs entirely on a laptop CPU, with NumPy, at ~116 ms per training step. Measure it yourself with comprobar_entorno.py.]


Part II · Scale

The scaling laws

Around 2020 something unsettling was discovered: a language model's loss follows a power law with respect to parameters, data and compute:

L(N) ≈ (Nc/N)^α        with α ≈ 0.076

That is: no surprises, no visible ceiling, and predictable. You can train small models, fit the curve, and predict with reasonable accuracy what loss a model 1,000 times larger will have before spending the money. That is what has allowed this industry to invest billions with some confidence.

Chinchilla (2022): the optimal split

D  ≈  20 · N
In plain English: about twenty training tokens for every parameter in the model.
Below that
the model is undertrained: it has capacity it never uses
Above it
you spend more compute than you need... unless you are going to serve it to millions of people, and then it pays off (LLaMA 3 used 1,875)

With a fixed compute budget: large model and little data, or small model and lots?

Answer: ~20 training tokens per parameter.

ModelParametersTokensTokens/paramVerdict
GPT-3 (2020)175,000 M300,000 M1.7badly undertrained
Chinchilla (2022)70,000 M1.4 trillion20the optimum
LLaMA 3 8B (2024)8,000 M15 trillion1,875deliberately "overdone"

The third row looks like a mistake and it is not. Chinchilla optimizes the cost of training. But if you are going to serve the model to millions of people, the inference cost dominates for the model's entire life. A smaller, more-trained model is more expensive to train and far cheaper to run. For total cost, it pays off.

Emergent capabilities (and the controversy)

Some capabilities appear all at once past a certain size: multi-digit arithmetic, chain-of-thought reasoning, following complex instructions.

But there is a serious counterargument and you should know it: much of that "emergence" is an artefact of the metric. If you measure with "exact match" (either you get the whole answer right or you score zero), the curve looks like a jump. If you measure with a continuous metric (the probability assigned to the correct answer), the improvement is smooth the whole way.

In other words: the model was improving gradually; what jumps abruptly is the threshold past which your way of measuring starts giving it points.

It is not settled. But it is worth not repeating "it emerges magically" without qualification.

What really changes as it grows

SizeWhat it can do
~1 MSpelling, word structure. [Your model, roughly.]
~100 MCorrect grammar, coherent sentences, very common facts
~1,000 MCoherent paragraphs, following simple instructions
~10,000 MIn-context learning, simple reasoning, basic code
~100,000 MMulti-step reasoning, complex code, good translation
~1 trillionAll of the above, more reliably and with fewer hallucinations

And something that does not change with scale: the model still has no memory, still does not know what time it is, and is still predicting the next token.


Part III · A table to keep to hand

Memory needed

TRAIN (AdamW, fp32):      16 bytes/parameter  + activations
TRAIN (bf16 + Adam 8bit):  6 bytes/parameter  + activations
INFER (fp16):              2 bytes/parameter  + KV cache
INFER (int8):              1 byte /parameter  + KV cache
INFER (int4):           ~0.55 bytes/parameter + KV cache

KV cache

bytes = 2 × layers × kv_heads × head_dim × context × batch × type_bytes

GQA reduces kv_heads (from 64 to 8 in LLaMA 3): 8 times less cache. That is why everything has carried it since 2023.

Compute

FLOPs  ≈  6 · N · D
In plain English: the cost of training is six times the parameters times the tokens.
N
number of parameters
D
number of training tokens
the 6
2 forward + 4 backward
With this formula
training runs costing 50 million euros get budgeted. And you verify it yourself in stage 06.2
train    ≈ 6 · N · D         FLOPs   (N parameters, D tokens)
infer    ≈ 2 · N             FLOPs per token

Parameters of a transformer

N  ≈  12 · layers · dimension²
In plain English: parameters grow with the SQUARE of the width, and only linearly with the depth.
Consequence
doubling the dimension quadruples the model; doubling the layers only doubles it. That is why models grow mostly in width
Verified
in stage 06.2: 809,856 derived, 809,856 actual
N ≈ 12 · layers · dimension²    (not counting embeddings)

Notice: the dimension squared. Doubling the width quadruples the model; doubling the depth only doubles it. That is why models grow mostly in width.

[All of these formulas are verified against the real model in etapas/06_transformer/02_anatomia.py, which derives the 809,856 parameters and then counts them to check that it matches.]


Next: 05-fallos-y-limites.md