I spent the past year implementing five LLM architectures from scratch in PyTorch and wrote a book documenting the process.
What's covered:
- Vanilla encoder-decoder transformer (English to Hindi translation)
- GPT-2 (124M), loading real OpenAI pretrained weights
- Llama 3.2-3B, showing the exact 4 component swaps from GPT-2 (RMSNorm, RoPE, SwiGLU, GQA), loading Meta's pretrained weights
- KV cache mechanics, MQA, GQA
- DeepSeek: Multi-Head Latent Attention with absorption trick and decoupled RoPE, DeepSeekMoE with shared experts and fine-grained segmentation, Multi-Token Prediction, FP8 quantisation
All code is open source: https://github.com/S1LV3RJ1NX/mal-code
The book (explanations, derivations, diagrams) is on Leanpub with a free sample: https://leanpub.com/adventures-with-llms
I'm a Senior Forward Deployed Engineer at TrueFoundry, where I work with enterprises on LLM systems. I wrote this because I wanted a resource that went past GPT-2 and into the architectures actually running in production. Happy to discuss any of the implementations.
[link] [comments]




