Transformers, ELI5 · Part 9 / 10
Making it big, affordably
Bigger models are predictably better — and a few tricks (the KV cache, LoRA) keep the cost in check.
Bigger models are better. The catch is cost — so there are clever tricks.
Predictably better
Add , data, or compute and the loss falls along a straight line on log-log axes — a . These predictable curves are called — you can predict the payoff before you spend a cent.
Go deeper: it's a budget you split (Chinchilla)
Params aren’t the only dial. For a fixed compute budget you split it between model size and training data — and there’s a sweet spot. That’s .
Don’t redo the past: the KV cache
For each new word, the model would re-attend over the whole history every time. The stores the past instead, so it only computes the new row.
see this in PyTorch
To generate text, the model keeps reading everything written so far, guesses the next token, sticks it on the end, and runs again.
import torch
# `model` maps a sequence of token ids -> logits for every position.
# logits[..., t, :] = scores over the whole vocab for "what comes after position t".
tokens = torch.tensor([[1, 14, 27]]) # our prompt so far: (batch=1, seq=3)
for _ in range(5): # generate 5 new tokens, one at a time
logits = model(tokens) # run model on ALL tokens so far: (1, seq, vocab)
last_logits = logits[:, -1, :] # we only care about the LAST position: (1, vocab)
probs = torch.softmax(last_logits, dim=-1) # turn scores into probabilities
next_token = torch.multinomial(probs, num_samples=1) # sample one token: (1, 1)
tokens = torch.cat([tokens, next_token], dim=1) # append it -> seq grows by 1
# after the loop, `tokens` holds prompt + 5 freshly generated tokens
print(tokens)Because attention compares every token with every other (~n² work), a model can only hold so many at once — its (commonly ~128k, up to ~1M+ tokens). Run past it and the start of the chat falls off.
Fine-tune cheaply: LoRA
Re-training a giant matrix means changing millions of numbers. freezes it and learns a tiny shortcut beside it — a handful of numbers instead of millions.
Go deeper: two more 'big but cheap' tricks
stores each weight in fewer bits (tiny model, barely-worse answers). keeps many specialist sub-networks but wakes only a couple per word.
Last stop: can we see what the model learned?
Sources · 9
- Kaplan, Jared, et al. "Scaling Laws for Neural Language Models." 2020. arXiv:2001.08361.
- Hoffmann, Jordan, et al. "Training Compute-Optimal Large Language Models." NeurIPS, 2022. arXiv:2203.15556.
- Jurafsky, Daniel, and James H. Martin. Speech and Language Processing (3rd ed. draft, 2026), Chapter 8: "Transformers."
- Ainslie, Joshua, et al. "GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints." EMNLP, 2023. arXiv:2305.13245.
- Vaswani, Ashish, et al. "Attention Is All You Need." NeurIPS, 2017. arXiv:1706.03762.
- Gemini Team, Google. "Gemini 1.5: Unlocking multimodal understanding across millions of tokens of context." 2024. arXiv:2403.05530.
- Hu, Edward J., et al. "LoRA: Low-Rank Adaptation of Large Language Models." 2021. arXiv:2106.09685.
- Dettmers, Tim, Mike Lewis, Younes Belkada, and Luke Zettlemoyer. "LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale." NeurIPS, 2022. arXiv:2208.07339.
- Fedus, William, Barret Zoph, and Noam Shazeer. "Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity." 2021. arXiv:2101.03961.