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
Safetensors
Model size
5.24M params
Tensor type
F32
Β·
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Dataset used to train Brutalsky111/MiniGPT-Scratch-5M