📖 TinyStories Mini-GPT (1.3M Parameters)
A compact, decoder-only autoregressive language model built from scratch in pure PyTorch and trained on Microsoft Research's TinyStories dataset.
🔍 Model Overview
- Architecture: Decoder-Only Transformer (Pre-LayerNorm, Causal Multi-Head Attention)
- Parameters: 1,329,408 (~1.33 Million)
- Layers: 4 Transformer Blocks
- Attention Heads: 4 heads ($d_k = 32$)
- Hidden Dimension: $d_{model} = 128$
- Vocabulary: 4,096 tokens (Byte-Level BPE)
- Context Length: 128 tokens
- Training Corpus: roneneldan/TinyStories
🚀 How to Use
Quick Inference via Python:
import os
import torch
from huggingface_hub import hf_hub_download
# 1. Download model files from Hugging Face
repo_id = "mallelamanoj75/tinystories-minigpt"
ckpt_path = hf_hub_download(repo_id=repo_id, filename="best_model.pt")
vocab_path = hf_hub_download(repo_id=repo_id, filename="vocab.json")
merges_path = hf_hub_download(repo_id=repo_id, filename="merges.txt")
model_py = hf_hub_download(repo_id=repo_id, filename="model.py")
tokenizer_py = hf_hub_download(repo_id=repo_id, filename="tokenizer_util.py")
# 2. Load model
from model import TinyStoriesGPT, GPTConfig
from tokenizer_util import StoryTokenizer
os.makedirs("tokenizer_data", exist_ok=True)
import shutil
shutil.copy(vocab_path, "tokenizer_data/vocab.json")
shutil.copy(merges_path, "tokenizer_data/merges.txt")
tok = StoryTokenizer("tokenizer_data")
checkpoint = torch.load(ckpt_path, map_location="cpu", weights_only=False)
model = TinyStoriesGPT(checkpoint["config"])
model.load_state_dict(checkpoint["state_dict"])
model.eval()
# 3. Generate a story!
prompt = "Once upon a time, Lily found a sparkling green stone"
input_ids = torch.tensor([tok.encode(prompt)], dtype=torch.long)
output_ids = model.generate(input_ids, max_new_tokens=80, temperature=0.8, top_k=30, top_p=0.9)
print(tok.decode(output_ids[0].tolist()))
📊 Sample Generation
Prompt: "One sunny morning, Mia and her mom went to the big park"
"One sunny morning, Mia and her mom went to the big park with her mom. They saw a big, red ball and a swing.
"Look, this is a big slide!" Mia said. "But I want to swing very high and fast."
"No, I want to play!" Mia said. "Let's play!"
They ran to the swing and jumped in the slide. They were happy. They had fun.
[End of Story]"
📚 Citation & References
- TinyStories: How Small Can Language Models Be and Still Speak Coherent English? (Eldan & Li, 2023)
- GitHub Repository: mallelamanojkumar90/BuildingLLM