A straightforward method for training your LLM, from downloading data to generating text.
docs(howto): drop em dashes from the how-to pages, POST_TRAINING.md and the diagrams README
FareedKhan-devaf8561d9122c
diff --git a/POST_TRAINING.md b/POST_TRAINING.mdindex 5f2c10e..fdc4334 100644--- a/POST_TRAINING.md+++ b/POST_TRAINING.md@@ -1,6 +1,6 @@# Post-Training from Scratch: SFT · Reward Model · PPO · DPO · GRPO-This adds a complete, **from-scratch** (pure PyTorch — no `trl`/`peft`/`transformers`)+This adds a complete, **from-scratch** (pure PyTorch, no `trl`/`peft`/`transformers`)post-training suite on top of the repo's own `Transformer`, covering the full modernpipeline used to turn a base LM into an aligned, reasoning model:@@ -16,7 +16,7 @@ metric is **greedy GSM8K accuracy across stages**.> **Expectation:** a ~400M model pretrained from scratch on 2×H100 is coherent and> instruction-followable and shows real before/after gains at each stage, but its-> *absolute* GSM8K score stays modest — frontier numbers need far more pretraining+> *absolute* GSM8K score stays modest; frontier numbers need far more pretraining> compute. The value here is the authentic end-to-end pipeline on real data with real eval.---@@ -55,7 +55,7 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/pretrain_base.py[scripts/pretrain_base.py](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/scripts/pretrain_base.py) upgrades the original`train_transformer.py` recipe with DDP, bf16 autocast, gradient accumulation, a cosine LR-schedule with warmup, and periodic checkpointing — everything needed to train a mid-size+schedule with warmup, and periodic checkpointing: everything needed to train a mid-sizemodel on 2×H100. The original training script is untouched.---@@ -89,7 +89,7 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_reward.py #([src/post_training/reward_model.py](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/reward_model.py)), trained with the**Bradley-Terry** pairwise loss in[src/post_training/reward_train.py](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/reward_train.py). The reward is read- off the last real token (causal attention makes right-padding safe — no attention mask+ off the last real token (causal attention makes right-padding safe, no attention maskneeded). **Eval:** held-out preference accuracy (expect ~0.65–0.75 on noisy real data).## 4. DPO / ORPO / KTO@@ -128,7 +128,7 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_grpo.py --gr```- **From-scratch:** group-relative advantages + token-level clipped surrogate with a k3 KL- penalty in [src/post_training/grpo.py](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/grpo.py). **No critic** — the+ penalty in [src/post_training/grpo.py](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/grpo.py). **No critic**: thebaseline is each prompt's own group of G samples. An **arithmetic curriculum** runs forthe first `curriculum_iters` iterations so the policy has non-zero reward variance beforefull GSM8K. **Eval:** mean group reward, informative-group fraction, KL, GSM8K test accuracy.@@ -137,8 +137,8 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_grpo.py --gr## 7. Inference / chat (any stage checkpoint)-[scripts/chat.py](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/scripts/chat.py) loads **any** checkpoint (base/sft/dpo/ppo/grpo) —-reading the model dims from the checkpoint itself — and generates with the chat template+[scripts/chat.py](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/scripts/chat.py) loads **any** checkpoint (base/sft/dpo/ppo/grpo),+reading the model dims from the checkpoint itself, and generates with the chat template(instruction models) or as raw continuation (the base model):```bashdiff --git a/docs/diagrams/README.md b/docs/diagrams/README.mdindex f449e11..4befabe 100644--- a/docs/diagrams/README.md+++ b/docs/diagrams/README.md@@ -3,14 +3,14 @@The documentation diagrams are **hand-drawn, colour-coded Mermaid sketches**, pre-rendered to PNG andembedded as images in the docs. We pre-render (rather than rely on live ```` ```mermaid ```` blocks)because GitHub's live Mermaid renderer does not reliably support the `look: handDrawn` style, and some-markdown viewers (e.g. the VS Code preview) block SVGs — an embedded **PNG** shows the hand-drawn look+markdown viewers (e.g. the VS Code preview) block SVGs, so an embedded **PNG** shows the hand-drawn lookeverywhere. Each doc also keeps the editable Mermaid source in a collapsible *"Mermaid source"* blockunder its image.## Files-- `src/*.mmd` — the canonical hand-drawn Mermaid sources (with `look: handDrawn` + the colour palette).-- `*.png` — the rendered images embedded by the docs (and `README.png` for the top-level README).+- `src/*.mmd`: the canonical hand-drawn Mermaid sources (with `look: handDrawn` + the colour palette).+- `*.png`: the rendered images embedded by the docs (and `README.png` for the top-level README).## Regenerate after editingdiff --git a/docs/howto/configs.md b/docs/howto/configs.mdindex 97746f8..bb22cf5 100644--- a/docs/howto/configs.md+++ b/docs/howto/configs.md@@ -2,13 +2,13 @@Every training stage is configured by a small, human-editable JSON file under[`configs/`](https://github.com/FareedKhan-dev/train-llm-from-scratch/tree/main/configs). You edit one-file and see only that stage's knobs — the model architecture and runtime live in the shared+file and see only that stage's knobs; the model architecture and runtime live in the shared`configs/base.json`.```configs/base.json # shared model + runtime (vocab, n_embed, device, amp_dtype, ...)- pretrain.json # one file per stage — only that stage's hyperparameters+ pretrain.json # one file per stage, only that stage's hyperparameterssft.json reward.json dpo.json ppo.json grpo.jsonsmoke/ # tiny CPU variant (small model + few steps) for quick testsbase.json pretrain.json sft.json ...@@ -29,7 +29,7 @@ source; the CLI wins for quick one-offs. JSON `null` maps to Python `None` (the!!! tip "Smoke configs auto-shrink the model"A stage JSON in a sub-folder uses that folder's `base.json`. So `configs/smoke/sft.json` picks up- `configs/smoke/base.json` (tiny model, CPU) automatically — handy for a fast end-to-end test.+ `configs/smoke/base.json` (tiny model, CPU) automatically, handy for a fast end-to-end test.## Editing & inspectingdiff --git a/docs/howto/train.md b/docs/howto/train.mdindex dc43a06..db1ae83 100644--- a/docs/howto/train.md+++ b/docs/howto/train.md@@ -1,13 +1,13 @@# Train (UI & CLI)You can run every stage two ways: from the **command line** (full control, best for long jobs) or from-the **Streamlit control panel** (forms, one-click launch, live logs, charts — see [The UI](ui.md)).+the **Streamlit control panel** (forms, one-click launch, live logs, charts; see [The UI](ui.md)).## Install```bash-pip install -e ".[train]" # editable install — no more PYTHONPATH=.-export HF_HOME=/ephemeral/hf_cache+pip install -e ".[train]" # editable install, no more PYTHONPATH=.+# optional: export HF_HOME=/path/to/big/disk/hf_cache to keep Hugging Face downloads elsewhere```## The pipeline, end to end (CLI)@@ -55,7 +55,7 @@ bash scripts/run_posttraining.sh # SFT → RM → DPO → PPO → GRPO- `torchrun --standalone --nproc_per_node=N` launches N data-parallel ranks (DDP + bf16). Only rank 0logs and checkpoints.- On the dev box (2× H100, no NVLink) the educational attention materializes a `(B, n_head, T, T)` tensor- per block, so memory scales with sequence length — at context 1024 use `--batch_size 8 --grad_accum 12`+ per block, so memory scales with sequence length: at context 1024 use `--batch_size 8 --grad_accum 12`and recover the effective batch via accumulation. Set `PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True`.## Where outputs godiff --git a/docs/howto/ui.md b/docs/howto/ui.mdindex 679705e..6a0e38d 100644--- a/docs/howto/ui.md+++ b/docs/howto/ui.md@@ -1,7 +1,7 @@# The control-panel UI-A polished **Streamlit** app that wraps the whole pipeline — theory, one-click training, live logs,-metric charts, evaluation, and chat — so you can drive everything without memorizing commands.+A polished **Streamlit** app that wraps the whole pipeline (theory, one-click training, live logs,+metric charts, evaluation, and chat) so you can drive everything without memorizing commands.```bashpip install -e ".[ui]"@@ -10,20 +10,20 @@ streamlit run ui/app.py## What's inside-- **Home** — the pipeline diagram, live job status, and GPU status at a glance.-- **Data** — launch the `prepare_*` scripts and watch the files appear under `/ephemeral/data`.+- **Home**: the pipeline diagram, live job status, and GPU status at a glance.+- **Data**: launch the `prepare_*` scripts and watch the files appear under `/ephemeral/data`.- **A page per stage** (Pretrain, SFT, Reward, DPO, PPO, GRPO). Each page gives you:1. the **theory + hand-drawn diagram** for that stage (pulled straight from these docs),2. a **config form** that writes the stage's `configs/*.json`,3. a **Launch** button that runs the real training script as a background job, and4. a **live log tail** + **metric charts** (loss / reward / KL / accuracy) read from the JSONL.-- **Evaluate** — run GSM8K accuracy on any checkpoint and inspect sample generations.-- **Chat** — talk to any checkpoint in-process, with temperature / top-p / top-k controls.+- **Evaluate**: run GSM8K accuracy on any checkpoint and inspect sample generations.+- **Chat**: talk to any checkpoint in-process, with temperature / top-p / top-k controls.## How launching worksThe app builds the same command you'd type (`torchrun … scripts/train_sft.py --config …`), runs it as a-detached background process, and streams its log file into the page — so jobs keep running even if you+detached background process, and streams its log file into the page, so jobs keep running even if younavigate away or refresh. A **Stop** button cleanly terminates the job (and all `torchrun` workers).!!! warning "One GPU job at a time"