neelbhattacharya80-creator/transformer-decoder-from-scratch — explained in plain English
Analysis updated 2026-05-18
Study a hand written implementation of self attention, RoPE, and AdamW for learning purposes.
Train a small decoder only language model on a text dataset like WikiText-103.
Generate autoregressive text samples from a from-scratch Transformer model.
Reference the gradient accumulation and learning rate warmup techniques used here.
| neelbhattacharya80-creator/transformer-decoder-from-scratch | 0xallam/my-recipe | 0xhassaan/nn-from-scratch | |
|---|---|---|---|
| Stars | 0 | — | 0 |
| Language | Python | Python | Python |
| Last pushed | — | 2022-11-22 | — |
| Maintenance | — | Dormant | — |
| Setup difficulty | hard | moderate | moderate |
| Complexity | 4/5 | 2/5 | 4/5 |
| Audience | researcher | general | developer |
Figures from each repo's GitHub metadata at analysis time.
Requires a GPU for practical training and understanding of Transformer internals to follow along.
This project is a personal learning exercise where the author built a decoder only Transformer, the type of architecture behind modern language models, almost entirely by hand instead of using pre built libraries. PyTorch is used only for basic building blocks like automatic differentiation, tensor math, and running computations on the GPU. Everything else, including the attention mechanism, positional encoding, normalization layers, the optimizer, the loss function, and the training loop, was written from scratch. The resulting model has about 30 million trainable parameters and can both be trained and used to generate text one word at a time. The README is written as a personal account of what the author learned along the way, rather than as a polished product description. They describe debugging issues like their custom optimizer accidentally staying on the CPU while the model ran on the GPU, and several bugs in the positional encoding caused by reshaping tensors incorrectly. They also describe efficiency improvements they made, such as combining three separate calculations into one, and building then abandoning a custom tokenizer in favor of an existing one called tiktoken because their own version was too slow. Training the model required adjusting the learning rate schedule and using a technique called gradient accumulation to work around limited GPU memory on the author's own graphics card. The model was trained on a public dataset called WikiText-103, and its training loss dropped steadily over time before leveling off. Sample output included in the README shows the model producing text that is grammatically correct and readable, but which tends to drift away from the original topic over longer passages and does not stay factually accurate. The author attributes this mainly to the size of the training data and the amount of compute used rather than to the architecture itself, and plans to continue training on a larger dataset using rented GPUs to improve results.
A personal, hand built decoder only Transformer language model written in PyTorch, documented as a learning project with training notes and sample output.
Mainly Python. The stack also includes Python, PyTorch, tiktoken.
Setup difficulty is rated hard, with roughly 1day+ to a first successful run.
Mainly researcher.
This repo across BitVibe Labs
Don't trust strangers blindly. Verify against the repo.