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/*.pyand the genericdata_loader.pyloads raw text, tokenises it, and buildsPromptDataset,SFTDataset, orPreferenceDataset. - 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.
- CLI parses arguments and calls
train(),evaluate(), orinference()fromsrc/post_training/*. train()imports the chosen trainer (e.g.,train_sft()fromsrc/post_training/sft.py) which builds a dataset viadata_loader.*and a model viasrc/models/transformer.py.- The model is trained in a PyTorch loop defined in
src/post_training/optim.pyandsrc/post_training/rollout.py. - After training, the checkpoint is saved to
checkpoints/and can be resumed (tests/test_checkpoint_resume.py). - Post‑training:
src/post_training/reward_train.pytrains a Bradley‑Terry reward model,src/post_training/dpo.pyruns DPO,src/post_training/ppo.pyruns PPO, andsrc/post_training/grpo.pyruns GRPO. - 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/viasrc/post_training/logging_utils.py. - Serves a web UI on
localhost:8501via Streamlit.
How To Use It
- Setup ``
bash git clone https://github.com/moses-y/train-llm-from-scratch cd train-llm-from-scratch pip install -r requirements.txt`` - Configuration Edit the JSON files under
configs/(e.g.,configs/pretrain.json) to set dataset paths, model hyper‑parameters, and GPU settings. - 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`` - Evaluation / Inference ``
bash python scripts/eval_post_training.py --config configs/eval.json python scripts/generate_text.py --prompt "Explain quantum computing."`` - 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.tomldeclares 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.