Beyond: what exists and we have not implemented
This project builds the core of a modern language model. It is complete in the sense that no necessary piece is missing: with what is here, scaled up, you have GPT-2.
But things have been added between 2019 and today. None of them changes the fundamentals — they are still transformers predicting tokens — and all of them can be understood with what you already know. This document explains them so the map is complete.
They are ordered by how often you will run into them.
1. RoPE — positions that actually extrapolate
Ours (stage 06): a wpe table with one learned vector per position. Simple and it works. The problem: if you train with 64 positions, position 65 does not exist. There is no way to extend the context without retraining.
RoPE (Rotary Position Embedding): instead of adding a position vector, it rotates the query and key vectors by an angle proportional to their position.
for each pair of dimensions (2i, 2i+1) in the vector:
angle = position / (10000^(2i/d))
rotate that pair of coordinates by that angle
Why it is better, and it is elegant: the dot product between the query at position m and the key at position n ends up depending only on (m − n), the relative distance. Not on the absolute positions.
Consequences:
- Attention becomes naturally relative: "the token 3 back" instead of "token number 17".
- It extrapolates: you can use positions you never saw in training (with degradation, but it works).
- It lets you extend the context after training, by stretching the angle base (YaRN, NTK-aware scaling). It is how a model trained at 8k ends up serving 128k.
LLaMA, Mistral, Qwen, Gemma use it… practically everything since 2023.
How you would implement it here: in AtencionCausal.forward, after computing q and k and before the dot product, apply the rotation. About 15 lines. wpe disappears.
2. Mixture of experts (MoE) — more parameters without more compute
The problem: quality grows with parameters, but so does inference cost. Can you have 1,000,000 M parameters while paying the compute of 30,000 M?
The idea: replace each block's MLP with N MLPs ("experts") plus a router (a tiny linear layer) that, for each token, picks the 2 most suitable experts. Only those 2 run.
dense MLP: every token goes through the same weights
MoE MLP: the router scores 8 experts, activates the best 2
- Total parameters: 8 experts → 8 times more parameters in the MLP.
- Compute per token: only 2 of 8 → a quarter of what it would be.
Mixtral 8x7B has 47,000 M parameters and computes like a 13,000 M one. DeepSeek-V3 has 671,000 M and activates 37,000 M per token. GPT-4 is believed to be an MoE.
What nobody mentions: it is an engineering headache.
- Load balancing: if the router gets attached to 2 experts, the other 6 never learn. You need an auxiliary loss that forces the spread.
- Memory: you have to keep all the experts loaded even though you use
- MoE saves compute, not memory. That is why an MoE model is hard to run locally despite "activating few parameters".
- Communication: with experts spread across GPUs, each token has to travel to its expert's GPU and back.
How you would implement it here: replace MLP with a list of MLPs plus an nn.Linear(C, n_experts) giving the scores, a top-2, and a weighted sum of the outputs. ~40 lines.
3. Reasoning models — compute at answering time
The most important conceptual change since the transformer itself, and what explains the 2024-2025 generation of models.
The observation: a transformer does a fixed amount of computation per token. A trivial question and a hard integral get the same. That is absurd.
The solution: let the model generate many "thinking" tokens before answering. Every generated token is a complete pass through the network, so generating more tokens is literally thinking more. The chain of reasoning is not an explanation: it is external working memory.
How it is trained (and here is the new part): with RL and verifiable reward. In maths or code, the answer can be checked automatically. So:
- The model generates many different chains of reasoning for the same problem.
- You check which ones reach the correct result.
- Those get reinforced.
No human is needed. And the model discovers strategies on its own like checking its work, backtracking when it spots an error, or trying another route.
The economic consequence: a second scaling law appears. No longer just "more training = better", but "more compute when answering = better". One question can cost 100 tokens or 100,000 depending on how much you think.
How it relates to what you have built: no new architecture is needed. It is the same transformer, trained with a different objective and given permission to generate a lot before answering.
4. Speculative decoding — generating faster without losing quality
Remember from stage 08: generating is limited by memory, not computation. To produce one token you have to read the 140 GB of weights. The GPU is nearly idle.
The idea: a small, fast model proposes the next 5 tokens. The large model verifies them all in one pass (which costs almost the same as verifying one, because the bottleneck was reading the weights).
- If all 5 match what it would have generated, all 5 are accepted. 5 times faster.
- If the third one fails, 2 are accepted and it carries on from there.
The important part: with the right acceptance criterion (rejection sampling), the output distribution is exactly the same as the large model's on its own. It is not an approximation. You lose no quality at all.
Variants: Medusa (several prediction heads inside the model itself), n-gram decoding (the draft comes from the context itself, free).
5. Efficient attention — GQA, MQA, windows
You know from stage 08 that the KV cache is the scarce resource when serving.
- MQA (Multi-Query): all the query heads share a single key and value. Cache divided by the number of heads. Degrades somewhat.
- GQA (Grouped-Query): the middle ground. 32 query heads share 8 key/value groups. Cache 4 times smaller, quality practically untouched. It is the current standard.
- Sliding window: each token only looks at the last N. Mistral uses 4,096. Combined with depth, information still flows a long way (layer by layer the effective reach multiplies).
- Sparse attention: fixed patterns (local + a few global tokens).
How you would implement it here: GQA means changing the shape of c_attn so it produces C + 2·(C·n_kv/n_head) instead of 3C, and repeating the keys and values when computing attention. ~10 lines.
6. State space models — Mamba and company
The only serious proposal to replace attention.
The problem: attention is O(T²) and the KV cache grows with length.
The alternative: a state space model keeps a fixed-size state that gets updated token by token, like an RNN. Linear cost, constant memory.
Why it had not worked before: a classic RNN compresses the whole past into a fixed state and loses information. Mamba adds selectivity: the parameters of the update depend on the input, so the model can decide what to remember and what to forget.
Where it stands today: very competitive on very long sequences (DNA, audio), somewhat behind on language with precise copying and retrieval — which is exactly what attention is good at. The most promising direction is hybrids: mostly Mamba layers with a few attention layers interleaved (Jamba, Zamba).
7. Multimodality — images, audio, video
The surprising part: almost nothing changes.
- An encoder (usually a Vision Transformer) turns the image into a sequence of vectors, one per 14×14 pixel patch.
- A linear layer projects them into the language model's dimension.
- They go into the sequence as if they were tokens.
The transformer does not tell the difference. To it they are vectors in a sequence, just like the text ones. Attention mixes text and image without knowing they are different things.
Audio is the same: converted into a spectrogram, chunked, projected. Video is the same, at the cost of being an awful lot of patches.
8. What is still unsolved
It is worth keeping in mind which problems are genuinely still open:
| Problem | Status |
|---|---|
| Prompt injection | Unsolved. There is no structural separation between data and instructions |
| Hallucinations | Mitigable with RAG and tools; the underlying mechanism is still there |
| Continual learning | The weights are frozen. Updating without forgetting is an open problem |
| Uncertainty calibration | Models do not know well what they do not know |
| Interpretability | Advancing (sparse autoencoders, circuits), far from solved |
| Reliable evaluation | Endemic contamination; public comparisons are worth little |
| Data efficiency | A child learns a language from a billion words; an LLM needs trillions |
How all of this fits with what you have built
None of these techniques requires understanding anything new relative to what you already know:
| Technique | What changes | Where you would touch it |
|---|---|---|
| RoPE | how position gets in | AtencionCausal.forward, ~15 lines |
| GQA | how many keys and values there are | c_attn and the reshape, ~10 lines |
| MoE | the MLP becomes N MLPs and a router | MLP, ~40 lines |
| Speculative decoding | the generation loop | MotorGPT.generar, ~50 lines |
| Reasoning | the training objective | stage 10, not the architecture |
| Multimodality | where the input vectors come from | before wte, an encoder |
And that is, perhaps, the most important conclusion of the whole project:
The transformer of 2017 is still the transformer of today. Everything else is optimization around it. If you understand the core — and you do understand it, because you wrote it — you understand the entire field, and you can read any new paper knowing exactly which piece it is touching.
Back to the index: 00-mapa.md