MiniGPT π
A GPT-style language model built completely from scratch in PyTorch for educational purposes.
This project implements the complete GPT pipeline, including tokenizer training, Transformer architecture, language model training, and text generation without relying on pre-built GPT implementations.
For installation check the repo:
https://github.com/samdoom-coder/MiniGPT.git
Features
- β Custom BPE Tokenizer
- β Token Embeddings
- β Positional Embeddings
- β Multi-Head Self-Attention
- β Feed Forward Network (FFN)
- β Residual Connections
- β Layer Normalization
- β Transformer Decoder Blocks
- β Weight Tying (GPT-2 Style)
- β Cross Entropy Loss
- β AdamW Optimizer
- β Temperature Sampling
- β Top-k Sampling
- β Top-p (Nucleus) Sampling
- β EOS Stopping
- β Model Checkpoint Saving & Loading
Model Architecture
| Parameter | Value |
|---|---|
| Model Type | Decoder-only Transformer |
| Layers | 4 |
| Attention Heads | 4 |
| Embedding Dimension | 256 |
| Feed Forward Dimension | 1024 |
| Context Length | 128 |
| Vocabulary Size | 8000 |
| Total Parameters | ~5.2 Million |
Dataset
The model is trained on:
- TinyStories
- Approximately 1,000 stories (initial experiment)
Dataset:
https://huggingface.co/datasets/roneneldan/TinyStories
Tokenizer
A Byte Pair Encoding (BPE) tokenizer was trained from scratch using the TinyStories dataset.
Special tokens:
<pad><bos><eos><unk>
Vocabulary Size:
8000
Training
Optimizer
AdamW
Loss Function
CrossEntropyLoss
Learning Rate
3e-4
Batch Size
16
Epochs
20
Text Generation
MiniGPT supports multiple decoding strategies:
- Greedy Decoding
- Temperature Sampling
- Top-k Sampling
- Top-p (Nucleus) Sampling
Example:
output = model.generate(
input_ids,
max_new_tokens=200,
temperature=0.8,
top_k=100,
top_p=0.9,
)
Example Output
Prompt
Once upon a time
Output
There was a little girl with dark hair and a smile.
She was very happy.
She ran to her mom and showed her the new dress.
Her mom smiled and said,
"Thank you, Lily.
You are a very good girl."
Lily smiled and said,
"I love you too, mom.
You are very kind."
Project Structure
MiniGPT
β
βββ src/
β βββ attention.py
β βββ config.py
β βββ dataset.py
β βββ embeddings.py
β βββ feedforward.py
β βββ model.py
β βββ trainer.py
β βββ transformer.py
β
βββ tokenizer/
β βββ tokenizer.json
β βββ tokenizer_config.json
β
βββ train.py
βββ generate.py
βββ train_tokenizer.py
βββ README.md
Future Improvements
- Larger model architecture
- More training data
- Validation dataset
- Learning rate scheduler
- Mixed precision (AMP)
- Flash Attention
- KV Cache for faster inference
- Hugging Face Transformers compatibility
- Model quantization
- Fine-tuning support
Purpose
This project was created to understand how GPT-style language models work internally by implementing every major component from scratch instead of using existing GPT implementations.
The goal is educational: to learn the architecture, training process, and text generation pipeline of modern decoder-only Transformer language models.
Tech Stack
- Python
- PyTorch
- Hugging Face Datasets
- Hugging Face Tokenizers
- Transformers
License
MIT License
β If you found this project interesting or helpful, consider giving it a like!
- Downloads last month
- 488