AI/MLStarted 2026

TinyStories GPT

A 44.02M-parameter decoder-only transformer implemented in PyTorch and trained on TinyStories for 45,001 optimizer steps.

PythonPyTorchGPT-2 BPECUDAMemory-Mapped Data
TinyStories GPT project preview
Role
Independent ML Engineer
Development Began
2026
Status
Trained Research Prototype

Evidence at a Glance

44.02M
Checkpoint Parameters

Three transformer layers, six attention heads, 384-wide embeddings, and a tied 50,257-token vocabulary.

45,001
Optimizer Steps

The final checkpoint preserves the complete training and validation-loss history.

1.7438
Final Validation Loss

Measured from the checkpoint's held-out TinyStories validation batches.

Overview

TinyStories GPT is a decoder-only language model implemented and trained directly in PyTorch. The published checkpoint contains 44,019,840 parameters across three transformer blocks, six attention heads, 384-wide embeddings, a 256-token context window, and tied input/output embeddings over the GPT-2 BPE vocabulary.

The training pipeline owns more than the network definition. It streams source text in bounded chunks, caches token IDs to a raw binary file, memory-maps the result, creates a 90/10 train-validation split, and supports automatic model sizing, warm-up plus cosine learning-rate decay, gradient clipping, validation sampling, resumable checkpoints, and loss history.

The model was trained on the TinyStories corpus for 45,001 optimizer steps. A CLI and local threaded web demo both load the real checkpoint on CPU and expose prompt, temperature, length, and top-k sampling controls. No hosted model is substituted behind the interface.

The Challenge

The TinyStories corpus is large enough that naïvely reading all text and token IDs into Python objects creates avoidable memory pressure. Training also needs to survive long runs without losing optimizer state or the evidence needed to judge generalisation.

The GPT-2 vocabulary makes the embedding table a large part of a compact model's parameter budget, so architecture size, context, data volume, and tokenizer choice have to be considered together rather than quoting a small layer count as the whole model size.

Key Engineering Decisions

01

Implement the Transformer Directly

Causal self-attention, MLP blocks, residual paths, normalization, weight tying, loss, and autoregressive sampling remain inspectable instead of hiding behind a pretrained-model API.

02

Stream and Memory-Map Tokens

One-megabyte text chunks feed a cached int32 token file, and NumPy memmap pages training slices on demand instead of holding the corpus twice in memory.

03

Train for Recovery and Comparison

Checkpoints retain architecture, weights, optimizer state, step, tokenizer, and loss history; validation runs every 100 steps and resumable snapshots are written every 1,000.

04

Run the Actual Checkpoint in the Demo

The local server loads the trained model on CPU and bounds temperature and output length, keeping the showcase honest and predictable.

What I Owned

Implemented a 44.02M-parameter GPT checkpoint directly in PyTorch.
Built streaming BPE tokenisation, binary caching, memory-mapped batching, automatic model sizing, and device selection.
Trained for 45,001 optimizer steps with AdamW, warm-up, cosine decay, gradient clipping, and held-out validation.
Recorded the full train/validation history in the final checkpoint, ending at 1.7438 validation loss.
Shipped CLI generation and a local browser interface backed by the real CPU-loaded checkpoint.

Results

Produced a working TinyStories model and portable checkpoint whose architecture and training history can be inspected without an external generation API.
Created an end-to-end teaching artifact covering data preparation, transformer internals, optimization, checkpoint recovery, validation, and sampling.

Valuable Lessons

For a compact language model, tokenizer vocabulary can dominate the parameter budget; parameter count needs architectural context to mean anything.

A model demo is credible when it loads the real checkpoint and publishes measured training facts, not when it only presents a curated text sample.