CoolFace
Modelpublic

cannizaroo/tiny-llm-50m

sourceHugging Facemitupdated 7h agoView on Hugging Face
2likes60downloads
Model Card

Tiny-LLM-From-Scratch (~50M)

A ~50M parameter GPT-2-style decoder-only Transformer built entirely from scratch in PyTorch and pre-trained on FineWeb-Edu.

This model was created for educational purposes to demonstrate how a GPT-style LLM works at the tensor/code level. Every component—tokenizer, data pipeline, attention, MLP, training loop, and generation—is implemented from scratch without using the transformers library.

Architecture Details

ParameterValue
ArchitectureDecoder-only Transformer (Pre-LayerNorm, GPT-2 style)
Total parameters50,677,760 (50.68 M)
Context length256 tokens
Vocabulary16,384 Byte-Level BPE tokens
Embedding dimension (C)640
Attention heads (H)10 (head_dim = 64)
Transformer layers (N)6
FFN hidden dim2,560 (4 x C)
ActivationGELU
Weight tyingNo

Training Details

SettingValue
DatasetFineWeb-Edu sample-10BT
Training tokens~20 M
TokenizerCustom Byte-Level BPE (trained on 80k docs)
OptimiserAdamW (lr=3e-4, weight_decay=0.1, betas=(0.9, 0.95))
LR scheduleCosine with 500-step linear warm-up
Effective batch size128 sequences (32 × 4 grad accum)
Steps5,000
AMPFP16 (PyTorch autocast + GradScaler)
HardwareGoogle Colab T4 (16 GB)

Sampling Capabilities

Supports autoregressive text generation with:

  • Temperature scaling
  • Top-K filtering
  • Nucleus (Top-P) sampling

Usage (PyTorch)

python
from huggingface_hub import hf_hub_download
import torch, json

# Download files
config_path = hf_hub_download(repo_id="tiny-llm-50m", filename="config.json")
weights_path = hf_hub_download(repo_id="tiny-llm-50m", filename="pytorch_model.bin")

# Rebuild model (copy GPTConfig and GPT classes from the training notebook)
with open(config_path) as f:
    cfg_dict = json.load(f)
config = GPTConfig(**cfg_dict)
model  = GPT(config)
model.load_state_dict(torch.load(weights_path, map_location="cpu"))
model.eval()

Source Code

Full project source: Tiny-LLM-From-Scratch