BetterGPT is a personal engineering project to build a modern decoder-only Transformer from scratch while understanding every component of an LLM training pipeline.
The project is divided into multiple phases, with each phase introducing more advanced architectures, datasets, and training techniques.
✅ Phase 0 - SimplestGPT
A minimal GPT implementation built entirely from scratch.
Understand the complete training pipeline before moving to larger language models.
- Character-level tokenizer
- Custom Multi-Head Self Attention
- Transformer decoder architecture
- Learned positional embeddings
- Autoregressive generation
- Trained on the Shakespeare dataset
This phase focused on understanding the fundamentals of:
- Tokenization
- Attention
- Training loops
- Loss computation
- Text generation
A modern decoder-only Small Language Model with approximately 30 million parameters.
- Dataset: TinyStories
- Tokenizer: Byte Pair Encoding (BPE)
- Vocabulary trained from scratch
- Dataset: Alpaca
- Supervised Instruction Fine-tuning
Modern LLM architecture inspired by recent open-source models.
Implemented features include:
- Decoder-only Transformer
- Multi-Head Self Attention
- Rotary Positional Embeddings (RoPE)
- RMSNorm
- SwiGLU Feed Forward Network
- Weight Tying
- Causal Self Attention
- Mixed Precision Training
- Gradient Clipping
- AdamW Optimizer
- Cosine Learning Rate Scheduler
- Flash Attention (PyTorch SDPA)
configs/
scripts/
src/
│
├── data_preparation/
├── models/
├── pretraining/
├── finetuning/
└── paths.py
- Byte Pair Encoding (BPE)
- Hugging Face Tokenizers
- Custom tokenizer training
- Vocabulary generation
- Tokenizer serialization
- Dataset preprocessing
- Dataset sharding
- Memory-efficient loading
- Training-ready binary format
- Mixed Precision
- Gradient Clipping
- Checkpoint Saving
- Resume Training
- Evaluation Loop
- Learning Rate Scheduling
- Top-k Sampling
- Temperature Sampling
- Autoregressive Generation
- Instruction Fine-tuned Generation
- Python
- PyTorch
- Hugging Face Tokenizers
- Transformers
- Flash Attention (Scaled Dot Product Attention)
- Automatic Mixed Precision (AMP)
- AdamW
- Cosine LR Scheduler
- RoPE
- RMSNorm
- SwiGLU
- Weight Tying
- Decoder-only Transformer
Build a 100M+ parameter language model trained on substantially larger and more diverse corpora.
Planned improvements include:
- Multi-dataset pretraining
- Larger context length
- Expanded vocabulary
- Grouped Query Attention (GQA)
- Gradient Checkpointing
- Distributed Training
- Better data packing
- Improved evaluation benchmarks
- Enhanced instruction tuning pipeline
Potential datasets include:
- FineWeb-Edu
- Cosmopedia
- OpenWebText
- Other high-quality curated corpora
This repository is not intended to compete with production LLMs.
The goal is to understand and implement every major component involved in training modern language models, including:
- Tokenization
- Data preprocessing
- Transformer architecture
- Optimization
- Pretraining
- Instruction tuning
- Inference
- Scaling techniques
| Phase | Status |
|---|---|
| Phase 0 | ✅ Completed |
| Phase 1 | 🚧 In Progress |
| Phase 2 | 📅 Planned |
This project draws inspiration from modern open-source LLM implementations including:
- nanoGPT
- llm.c
- Llama
- Qwen
- Gemma