Back to work
In progressLLMs · PyTorch Internals30M–125M Params · Pure PyTorch

GPT-Style Large Language Model from Scratch

Built a decoder-only GPT architecture from raw PyTorch tensor operations and nn.Module primitives to verify every forward pass, normalization invariant, causal mask, and optimizer step without delegating to high-level model wrappers.

Interactive demo

Demo

Next-token probabilities

A temperature slider reshapes the softmax over a fixed set of logits.

Loading the demo…

Pipeline

  1. 01 · Tokenization

    SimpleTokenizerV1 & SimpleTokenizerV2

    Regex-driven vocabulary construction, unknown-token handling, and sliding-window context/target sequence generation.

  2. 02 · Core Blocks

    Custom LayerNorm & Multi-Head Self-Attention

    Scaled dot-product attention with upper-triangular causal masks, learnable affine LayerNorm, GELU MLP blocks, and residual projections.

  3. 03 · Training Loop

    Autoregressive Cross-Entropy & LR Scheduling

    Explicit gradient accumulation, cosine learning-rate decay with warmup, and step-budget planning per parameter scale.

  4. 04 · Corpus & Scale

    FineWeb-Edu & TinyStories on L4/T4

    Memory-mapped batch streaming and GPU utilization profiling targeting 30M–125M parameter runs on 16–24 GB VRAM hardware.

Engineering decisions

  • Avoided high-level transformer wrappers so causal masking, attention head splitting, and LayerNorm epsilon stability could be unit-checked directly.
  • Planned token-to-parameter step budgets specifically around NVIDIA L4 (24 GB) and T4 (16 GB) VRAM limits on Lightning AI.
  • Implemented progressive tokenizer iterations (SimpleTokenizerV1 → SimpleTokenizerV2) before transitioning to subword pretraining corpora.

Benchmarks

Target Parameter Scale
30M – 125M
Configurable depth, heads, and embedding width
Pretraining Corpora
FineWeb-Edu · TinyStories
Educational and synthetic reasoning corpora
Hardware Envelope
NVIDIA L4 / T4
16–24 GB VRAM step-budget optimization
Abstraction Policy
Zero Model Wrappers
Custom tokenizers, attention, LayerNorm & loop
PyTorchPythonNumPyFineWeb-EduTinyStoriesLightning AI