A straightforward method for training your LLM, from downloading data to generating text.
docs(pipeline): repo-relative paths in every command
FareedKhan-devf5e3ef7b7fd5
diff --git a/docs/01_data_pipeline.md b/docs/01_data_pipeline.mdindex ca75f66..b26b742 100644--- a/docs/01_data_pipeline.md+++ b/docs/01_data_pipeline.md@@ -44,7 +44,7 @@ flowchart TD</details>-Everything lands on the big `/ephemeral` disk and uses the OpenAI **`r50k_base`** tokenizer+Everything lands in `data/` (pass other paths to use a bigger disk) and uses the OpenAI **`r50k_base`** tokenizer(`vocab_size = 50304`, the only special token is `<|endoftext|>` = id `50256`).## 1 · Pretraining data (Pile → flat-token HDF5)@@ -62,8 +62,8 @@ for ids in enc.encode_ordinary_batch(docs):``````bash-PYTHONPATH=. python scripts/prepare_pretrain_data.py --split val --out /ephemeral/data/pile_dev.h5-PYTHONPATH=. python scripts/prepare_pretrain_data.py --split train --num_shards 1 --out /ephemeral/data/pile_train.h5+python scripts/prepare_pretrain_data.py --split val --out data/pile_dev.h5+python scripts/prepare_pretrain_data.py --split train --num_shards 1 --out data/pile_train.h5```The base [`get_batch_iterator`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/data_loader/data_loader.py) then slices random@@ -101,7 +101,7 @@ concatenates everything and slices it into fixed `1024`-token rows, writing two`tokens` and `loss_mask`.```bash-PYTHONPATH=. python scripts/prepare_sft_data.py --context_length 1024 --out_dir /ephemeral/data+python scripts/prepare_sft_data.py --context_length 1024 --out_dir data```I verified on the real file that the mask covers exactly `<answer>4</answer>` and excludes the user@@ -126,7 +126,7 @@ the same chat template and right-pads a batch, which is safe because the model's**causal**, so the last real token never attends to padding after it (no attention mask needed).```bash-PYTHONPATH=. python scripts/prepare_preference_data.py --source both --max_per_source 40000+python scripts/prepare_preference_data.py --source both --max_per_source 40000```## 4 · RL prompt data (→ `{prompt, gold}` JSONL)@@ -141,7 +141,7 @@ rows.append({"prompt": ex["question"].strip(), "gold": gold})``````bash-PYTHONPATH=. python scripts/prepare_rl_prompts.py --out_dir /ephemeral/data+python scripts/prepare_rl_prompts.py --out_dir data```I cross-checked the emitted gold answers 50/50 against the live GSM8K dataset. They match exactly,diff --git a/docs/02_pretraining.md b/docs/02_pretraining.mdindex 7faa5b5..90c5663 100644--- a/docs/02_pretraining.md+++ b/docs/02_pretraining.md@@ -77,9 +77,9 @@ A few choices worth calling out:```bash# single GPU-PYTHONPATH=. python scripts/pretrain_base.py+python scripts/pretrain_base.py# both H100s (effective batch = batch_size * grad_accum * num_gpus)-PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True PYTHONPATH=. \+PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True \torchrun --standalone --nproc_per_node=2 scripts/pretrain_base.py \--batch_size 8 --grad_accum 12 --train_steps 50000```@@ -96,7 +96,7 @@ PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True PYTHONPATH=. \- **eval train/dev**: averaged loss on held-out windows, printed every `eval_steps`; watch the devloss to spot overfitting.-Checkpoints are written to `/ephemeral/ckpts/base_pretrained.pt` every `save_every` steps and carry the+Checkpoints are written to `models/base_pretrained.pt` every `save_every` steps and carry theconfig, so every later stage can rebuild the exact model with [`load_backbone_from_ckpt`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/utils.py).➡️ Next: [Stage 2: SFT](03_sft.md).diff --git a/docs/03_sft.md b/docs/03_sft.mdindex bbb686d..b422f8f 100644--- a/docs/03_sft.md+++ b/docs/03_sft.md@@ -75,8 +75,8 @@ rows across DDP ranks and yields `(tokens, loss_mask, epoch)`.## Run it```bash-PYTHONPATH=. python scripts/train_sft.py # single GPU-PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_sft.py # both GPUs+python scripts/train_sft.py # single GPU+torchrun --standalone --nproc_per_node=2 scripts/train_sft.py # both GPUs# tune: --lr 1e-5 --epochs 3 --batch_size 16```@@ -89,7 +89,7 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_sft.py # bot- **GSM8K dev accuracy**: after SFT the model both follows instructions *and* emits the`<answer>…</answer>` format, so this should rise above the base model (see [08_evaluation.md](08_evaluation.md)).-The result is saved to `/ephemeral/ckpts/sft.pt` and becomes the starting point for the reward model,+The result is saved to `models/sft.pt` and becomes the starting point for the reward model,DPO, PPO and GRPO.➡️ Next: [Stage 3: Reward Model](04_reward_model.md) or jump to [DPO](05_dpo.md).diff --git a/docs/04_reward_model.md b/docs/04_reward_model.mdindex db4969a..24c9bcb 100644--- a/docs/04_reward_model.md+++ b/docs/04_reward_model.md@@ -87,8 +87,8 @@ batch (safe under causal attention) and tracks the true length of each side.## Run it```bash-PYTHONPATH=. python scripts/train_reward.py-PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_reward.py+python scripts/train_reward.py+torchrun --standalone --nproc_per_node=2 scripts/train_reward.py# tune: --lr 1e-5 --max_len 768```@@ -99,7 +99,7 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_reward.pyHH-RLHF / UltraFeedback expect roughly **0.65 to 0.75**. That's normal: human preferences are noisy.- **margin**: mean `r_chosen − r_rejected`; a useful "is it still separating them" signal.-Saved to `/ephemeral/ckpts/reward.pt`; PPO loads it with+Saved to `models/reward.pt`; PPO loads it with[`load_reward_model`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/reward_model.py) when `--reward_source rm`.➡️ Next: [Stage 5: PPO](06_ppo.md) (which consumes this), or the RM-free path: [DPO](05_dpo.md).diff --git a/docs/05_dpo.md b/docs/05_dpo.mdindex cd7165e..b7ad5d4 100644--- a/docs/05_dpo.md+++ b/docs/05_dpo.md@@ -86,9 +86,9 @@ loss.backward()## Run it```bash-PYTHONPATH=. python scripts/train_dpo.py --loss_type dpo --beta 0.1-PYTHONPATH=. python scripts/train_dpo.py --loss_type orpo --orpo_lambda 1.0-PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_dpo.py+python scripts/train_dpo.py --loss_type dpo --beta 0.1+python scripts/train_dpo.py --loss_type orpo --orpo_lambda 1.0+torchrun --standalone --nproc_per_node=2 scripts/train_dpo.py```> DPO uses a **small** learning rate (`5e-7` by default). It's easy to over-push away from the@@ -102,6 +102,6 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_dpo.py- **r_chosen / r_rejected**: the implicit rewards `β·(logπ − logref)`; the gap (margin) should widen.- **GSM8K dev accuracy**: the real downstream check.-Saved to `/ephemeral/ckpts/dpo.pt`.+Saved to `models/dpo.pt`.➡️ Next: the RL path, [PPO](06_ppo.md) and [GRPO](07_grpo.md).diff --git a/docs/06_ppo.md b/docs/06_ppo.mdindex e215a1a..16d3431 100644--- a/docs/06_ppo.md+++ b/docs/06_ppo.md@@ -94,9 +94,9 @@ log-probs / values, build rewards, GAE, then run `ppo_epochs` of minibatched cli## Run it```bash-PYTHONPATH=. python scripts/train_ppo.py --reward_source verifier # GSM8K checker as reward-PYTHONPATH=. python scripts/train_ppo.py --reward_source rm # use the trained reward.pt-PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_ppo.py+python scripts/train_ppo.py --reward_source verifier # GSM8K checker as reward+python scripts/train_ppo.py --reward_source rm # use the trained reward.pt+torchrun --standalone --nproc_per_node=2 scripts/train_ppo.py```## What the numbers mean@@ -111,6 +111,6 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_ppo.py> PPO is the touchy one: small LR (`1e-6`), `clip 0.2`, grad-clip 1.0, and watch KL. I verified the loop> truly *optimizes* by giving it a learnable synthetic reward: reward climbed `0.10 → 1.00`.-Saved to `/ephemeral/ckpts/ppo.pt`.+Saved to `models/ppo.pt`.➡️ Next: [Stage 6: GRPO](07_grpo.md), which drops the critic entirely.diff --git a/docs/07_grpo.md b/docs/07_grpo.mdindex a0b9d46..d0453e8 100644--- a/docs/07_grpo.md+++ b/docs/07_grpo.md@@ -80,8 +80,8 @@ adv = group_advantages(rewards, G)## Run it```bash-PYTHONPATH=. python scripts/train_grpo.py --group_size 8-PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_grpo.py+python scripts/train_grpo.py --group_size 8+torchrun --standalone --nproc_per_node=2 scripts/train_grpo.py# tune: --curriculum_iters 100 --kl_coef 0.04 --temperature 1.0```@@ -97,6 +97,6 @@ PYTHONPATH=. torchrun --standalone --nproc_per_node=2 scripts/train_grpo.py> **0.10 → 0.69 → 1.00** in ~15 iterations and saturated. PPO and GRPO share the same rollout/log-prob> core, so this also exercises the common machinery.-Saved to `/ephemeral/ckpts/grpo.pt`.+Saved to `models/grpo.pt`.➡️ Next: [measure all stages on GSM8K](08_evaluation.md) and [chat with the result](09_inference.md).diff --git a/docs/08_evaluation.md b/docs/08_evaluation.mdindex 8c2d1f2..f27a371 100644--- a/docs/08_evaluation.md+++ b/docs/08_evaluation.md@@ -66,10 +66,10 @@ the stored `cfg`), scores it, and appends a row to a JSONL you can render as a t```bashfor s in base_pretrained sft dpo ppo grpo; do- PYTHONPATH=. python scripts/eval_post_training.py --ckpt /ephemeral/ckpts/$s.pt \- --label $s --limit 200 --append /ephemeral/logs/stage_table.jsonl+ python scripts/eval_post_training.py --ckpt models/$s.pt \+ --label $s --limit 200 --append logs/stage_table.jsonldone-PYTHONPATH=. python scripts/eval_post_training.py --table /ephemeral/logs/stage_table.jsonl+python scripts/eval_post_training.py --table logs/stage_table.jsonl``````@@ -84,7 +84,7 @@ grpo ... 200## In-training metrics-Each trainer also writes a metrics JSONL under `/ephemeral/logs/` (via+Each trainer also writes a metrics JSONL under `logs/` (via[`MetricsLogger`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/logging_utils.py)): train/dev loss for SFT, preference accuracyfor the reward model, implicit-reward accuracy for DPO, and reward/KL/clip-fraction + GSM8K accuracy forPPO/GRPO. Pass `--use_wandb true` to also mirror to Weights & Biases; the JSONL is always written so youdiff --git a/docs/09_inference.md b/docs/09_inference.mdindex c566cdf..b33a905 100644--- a/docs/09_inference.md+++ b/docs/09_inference.md@@ -72,12 +72,12 @@ any padding-vocab ids (the model's vocab is padded to 50304 but r50k_base only d```bash# instruction-tuned models (chat template applied automatically)-PYTHONPATH=. python scripts/chat.py --ckpt /ephemeral/ckpts/sft.pt --prompt "What is 13 + 29?"-PYTHONPATH=. python scripts/chat.py --ckpt /ephemeral/ckpts/grpo.pt --prompt "..." --greedy+python scripts/chat.py --ckpt models/sft.pt --prompt "What is 13 + 29?"+python scripts/chat.py --ckpt models/grpo.pt --prompt "..." --greedy# base-model continuation-PYTHONPATH=. python scripts/chat.py --ckpt /ephemeral/ckpts/base_pretrained.pt --raw --prompt "Once upon a time"+python scripts/chat.py --ckpt models/base_pretrained.pt --raw --prompt "Once upon a time"# interactive REPL (omit --prompt)-PYTHONPATH=. python scripts/chat.py --ckpt /ephemeral/ckpts/sft.pt+python scripts/chat.py --ckpt models/sft.pt```Sampling controls: `--temperature`, `--top_p`, `--top_k`, or `--greedy` for deterministic argmax. Runs