Skip to content

Repository files navigation

MiniGPT

MiniGPT is a decoder-only GPT-style Transformer implemented entirely from scratch in PyTorch. It recreates the core building blocks behind modern large language models including causal self-attention, multi-head attention, Pre-LayerNorm transformer blocks, sinusoidal positional encoding, weight tying, and autoregressive text generation without relying on Hugging Face model abstractions.

MiniGPT Architecture


Highlights

Decoder-only GPT · Multi-head causal self-attention · Pre-LayerNorm · Sinusoidal PE · Weight tying · AdamW + cosine LR · FP16 AMP · Top-K / Top-P sampling · Instruction fine-tuning · Gradio UI


Architecture

Token IDs → Embedding (×√d) → Positional Encoding
→ N × [LayerNorm → Masked MHA → +residual → LayerNorm → FFN(GELU) → +residual]
→ LayerNorm → lm_head (weight-tied) → logits → next-token prediction
flowchart TD
A[Token IDs] --> B[Embedding + Sinusoidal PE]
B --> C[Decoder Block × N]
C --> D[LayerNorm → lm_head]
D --> E{Mode}
E -->|Train| F[Cross-Entropy Loss]
E -->|Infer| G[Temperature · Top-K · Top-P · Sample]
Loading
ComponentImplementationWhy
Attentionsoftmax(QKᵀ/√d_k) · V, fused QKV projectionStandard scaled dot-product; multi-head captures parallel patterns
Causal maskLower-triangular; future tokens → -∞Autoregressive — no peeking at future tokens
FFNLinear → GELU → Linear (4× expansion)GPT-2/3 activation choice
Pre-LNNormalize before attention/FFNStable training without heavy warmup
Weight tyinglm_head.weight = embedding.weightFewer params; shared input/output space

Training objective: shifted next-token prediction with cross-entropy. Input x predicts target y one position ahead. Perplexity = exp(loss).

Default config: 4 layers · 8 heads · 256 embed · 256 seq_len · 12K vocab (config.py)


Pipelines

Data:corpus.txt → tokenize → build vocab → encode → sliding windows (x, y) → DataLoader

Train: forward → loss → backward → grad clip → AdamW → val eval → checkpoint (model_best.pt, vocab.json)

Generate: prompt → encode → autoregressive loop (repetition penalty → temperature → top-k → top-p → sample) → decode

Fine-tune: load model_best.pt + existing vocab → train on User: …\nAssistant: … pairs → save model_ft.pt

TrainingChoice
OptimizerAdamW (lr=1e-4, weight_decay=0.01)
SchedulerCosine annealing (per-step)
PrecisionFP16 AMP on CUDA
Gradient clipmax_norm=1.0
LossCross-entropy

Project Structure

MiniGPT/
├── model.py # Transformer architecture + generate()
├── tokenizer.py # WordTokenizer, CharTokenizer
├── dataset.py # Sliding-window TextDataset
├── train.py # Training, checkpointing, fine-tuning
├── generate.py # CLI inference / REPL
├── config.py # Hyperparameters + presets
├── demo.py # CPU demo (no external data)
├── download_hf_corpus.py # HF datasets → corpus
├── app.py # Gradio playground
├── data/ # corpus.txt, instruction.txt
└── checkpoints/ # vocab.json, *.pt (gitignored)

Model Presets

ConfigLayersHeadsEmbedSeq LenVocab
tiny_config()2264643K
small_config()4825625612K
medium_config()4825625612K

Quick Start

pip install torch # core
pip install gradio datasets # optional: UI + HF download
python demo.py # train + generate (no data needed)
python train.py --data data/corpus.txt --epochs 10
python train.py --finetune --data data/instruction.txt
python generate.py --prompt "Once upon a time" --interactive
python app.py # Gradio UI → http://127.0.0.1:7860

Download instruction data:

python download_hf_corpus.py --dataset dolly --out data/corpus.txt

Engineering Notes

ChoiceRationale
Decoder-onlyPure language modeling; matches GPT
Sinusoidal PEParameter-free baseline (vs RoPE in modern LLMs)
Word-level tokenizerSimple, readable — BPE would handle OOV better
Top-K + Top-PPractical decode quality without beam search

Future: RoPE, BPE tokenizer, Flash Attention, LoRA, KV-cache, quantization


License

MIT — see LICENSE. Copyright (c) 2026 Piyush Bafna.

About

MiniGPT is a decoder-only GPT-style Transformer implemented entirely from scratch

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages