A minimal character-level GPT implementation built from scratch using PyTorch.
This project is focused on learning the core building blocks behind modern Large Language Models (LLMs) through clean and simple code rather than abstraction-heavy frameworks.
Phase 0 is the foundation stage of the project:
- Character-level tokenization
- Transformer decoder architecture
- Multi-Head Self Attention
- Pre-Norm blocks
- Feed Forward Network (FFN)
- Residual connections
- Weight tying
- Cosine learning rate scheduling with warmup
- Gradient clipping and evaluation loop
- Text generation with temperature and top-k sampling
The model is trained on the Shakespeare dataset.
The purpose of SimplestGPT is to deeply understand:
- How GPT-style decoder-only transformers work
- Tensor shapes through the full forward pass
- Attention implementation from scratch
- Training dynamics of autoregressive language models
- Tokenization and data pipelines
- Sampling strategies during inference
- How modern LLM training loops are structured
This project prioritizes clarity and learning over optimization.
SimplestGPT/
│
├── main.py # Entry point
├── gpt.py # GPT model implementation
├── train.py # Training loop
├── dataset.py # Dataset and sequence creation
├── data_preparation.py # Data loading utilities
├── .env # Dataset URL or configuration
└── README.md| Component | Value |
|---|---|
| Vocabulary Size | 65 |
| Embedding Dimension | 64 |
| Context Length | 64 |
| Attention Heads | 4 |
| Transformer Blocks | 4 |
| Optimizer | AdamW |
| LR Scheduler | Cosine with Warmup |
- Character-level vocabulary creation
- Train / validation split
- Sequence dataset generation
- PyTorch DataLoader integration
- Token embeddings
- Positional embeddings
- Multi-head causal self-attention
- Feed Forward Network (FFN)
- Residual connections
- Layer normalization (Pre-Norm)
- Weight tying
- Cross entropy loss
- AdamW optimizer
- Cosine learning rate scheduler
- Warmup steps
- Gradient clipping
- Validation loop
- Model checkpoint saving
- Temperature sampling
- Top-k sampling
- Autoregressive text generation
Clone the repository:
git clone https://github.com/harikrish2727/SimplestGPT.git
cd SimplestGPTInstall dependencies:
pip install -r requirements.txtCreate a .env file:
url=<dataset_url>python main.pyThe model checkpoint will be saved as:
shakespere_gpt.ptRaw Text
↓
Character Tokenization
↓
Input / Target Sequence Creation
↓
Embedding Layer
↓
Transformer Decoder Blocks
↓
Logits
↓
Cross Entropy Loss
↓
Backpropagation
This repository intentionally avoids:
- High-level training frameworks
- Hidden abstractions
- Excessive optimization tricks
The idea is to understand every tensor and every operation.
- Rotary Positional Embeddings (RoPE)
- KV Cache for inference
- Better tokenizer
- Config system
- Improved generation pipeline
- Subword tokenization (BPE / SentencePiece)
- Mixed precision training
- Flash Attention
- Larger datasets
- Checkpoint loading and resuming
- Instruction tuning
- Fine-tuning pipelines
- Multi-language support
- Scaled training experiments
- Python
- PyTorch
- Transformers (scheduler utilities)
- dotenv
Most tutorials hide important implementation details.
This repository is an attempt to build a GPT-style language model step-by-step while understanding:
- the mathematics,
- the tensor dimensions,
- the training mechanics,
- and the reasoning behind each design choice.
Inspired by:
- GPT architecture papers
- Andrej Karpathy's educational content
- Open-source LLM implementations
MIT License