The Problem

Training a large language model (LLM) from raw text to a usable assistant normally requires a stack of heavyweight libraries (transformers, trl, peft). This repo eliminates those dependencies and shows how to build the entire pipeline—tokenisation, transformer, loss, fine‑tuning, reward modelling, RL, and chat—in plain PyTorch. The goal is a single‑GPU trainable LLM that can be aligned for reasoning tasks without external tooling.

What This Does

  • Model definition lives in src/models/*.py (e.g., transformer.py, attention.py).
  • Data pipelines are in data_loader/*.py and the generic data_loader.py loads raw text, tokenises it, and builds PromptDataset, SFTDataset, or PreferenceDataset.
  • Training loops are in scripts/*.py (e.g., pretrain_base.py, train_sft.py, train_dpo.py).
  • Post‑training (SFT, reward model, PPO/DPO/GRPO) is implemented in src/post_training/*.py.
  • UI is a Streamlit app (ui/app.py) that orchestrates training, evaluation, and chat.

All configuration is JSON (configs/*.json) and loaded via config/loader.py.

How It Is Wired

Entry point: src/post_training/cli.py.

  1. CLI parses arguments and calls train(), evaluate(), or inference() from src/post_training/*.
  2. train() imports the chosen trainer (e.g., train_sft() from src/post_training/sft.py) which builds a dataset via data_loader.* and a model via src/models/transformer.py.
  3. The model is trained in a PyTorch loop defined in src/post_training/optim.py and src/post_training/rollout.py.
  4. After training, the checkpoint is saved to checkpoints/ and can be resumed (tests/test_checkpoint_resume.py).
  5. Post‑training: src/post_training/reward_train.py trains a Bradley‑Terry reward model, src/post_training/dpo.py runs DPO, src/post_training/ppo.py runs PPO, and src/post_training/grpo.py runs GRPO.
  6. Evaluation (src/post_training/evaluation.py) and inference (src/post_training/inference.py) use the trained checkpoints.

UI wiring: ui/app.py imports ui/stage_page.py and ui/pages/*. Each page imports the corresponding post‑training module and calls the same functions used by the CLI, providing a web interface for training and chat.

External effects:

  • Reads/writes checkpoints in checkpoints/.
  • Generates tokenised datasets in data/.
  • Writes logs to logs/ via src/post_training/logging_utils.py.
  • Serves a web UI on localhost:8501 via Streamlit.

How To Use It

  1. Setup ``bash git clone https://github.com/moses-y/train-llm-from-scratch cd train-llm-from-scratch pip install -r requirements.txt ``
  2. Configuration Edit the JSON files under configs/ (e.g., configs/pretrain.json) to set dataset paths, model hyper‑parameters, and GPU settings.
  3. Training ``bash python scripts/pretrain_base.py --config configs/pretrain.json python scripts/train_sft.py --config configs/sft.json python scripts/train_reward.py --config configs/reward.json python scripts/train_dpo.py --config configs/dpo.json ``
  4. Evaluation / Inference ``bash python scripts/eval_post_training.py --config configs/eval.json python scripts/generate_text.py --prompt "Explain quantum computing." ``
  5. UI ``bash streamlit run ui/app.py ``

Real‑World Use

A research lab can use this repo to prototype a reasoning‑aligned assistant. Train a 13‑M parameter model on a private dataset, fine‑tune with DPO, evaluate with the built‑in tool, and expose the model via the Streamlit UI for quick demos.

Code Health & Issues

  • Low – Missing lockfile (pyproject.toml declares dependencies but no lockfile; reproducibility depends on pip).
  • Low – No Dockerfile or container build; deployment requires manual environment setup.
  • Low – CI limited to documentation (.github/workflows/docs.yml); no automated test runs.
  • Low – License is MIT, clear for reuse.

The repository contains 7 test files (tests/*.py) that cover checkpoint resume and smoke tests, but deeper integration tests for all training stages are absent.

The Bottom Line

This repo is an excellent educational platform for building an LLM from scratch in pure PyTorch. It demonstrates each training stage, from tokenisation to reinforcement learning, and offers a ready‑made UI. Production use would require adding a lockfile, containerisation, and a robust CI pipeline, but for research or teaching it delivers a clear, single‑GPU‑ready workflow.