Back to directory

Train LLM From Scratch

A hands-on tutorial and codebase that implements a transformer language model from scratch in plain PyTorch, walking from raw text through pretraining to a fine-tuned, reasoning-style chat model.

Visit website (opens in a new tab)fareedkhan-dev.github.io
Train LLM From Scratch

Train LLM From Scratch is a tutorial and codebase that implements a transformer language model from scratch in plain PyTorch, based on the original 'Attention Is All You Need' paper, letting a reader train their own model from a few million up to billions of parameters on a single GPU. It began as a pretraining tutorial and now covers the whole path from raw text to an aligned, reasoning-style assistant, with every algorithm written by hand rather than relying on existing training libraries.

The path it walks goes from raw text to tokens, through a transformer, to a next-token loss and a base model, and then through post-training stages: supervised fine-tuning, a reward model, proximal and direct preference optimization, and group relative policy optimization, ending in evaluation and a chat interface. Each building block, including the multilayer perceptron, single- and multi-head attention, and the full transformer, is explained in plain language before its code, with the expected output shown afterward.

It includes a Streamlit control panel for interacting with a trained model and a companion documentation site, and is written to work for students, developers and researchers alike.

More in Models

See all 405
  • mini-AGI

    github.com

    A continual learning language model that trains from scratch on a single 8 GB VRAM GPU, assembling its own architecture and adapting its size to the hardware and data available rather than shipping as a fixed pretrained model.

  • An open-source contrastive-embedding pipeline that maps technical CVE vulnerability descriptions to formal cybersecurity compliance controls in NIST SP 800-53 and CMMC, published with its model generation, fine-tuning and evaluation code.

  • peekaboolean

    github.com

    A small vision-language model, fine-tuned with LoRA on a 500-million-parameter backbone, that answers typed multiple-choice, ordinal-score and yes/no questions about an image with calibrated probabilities, in about 400 milliseconds on a laptop.