A straightforward method for training your LLM, from downloading data to generating text.
docs: point line-anchored code links at the current definitions
FareedKhan-deve691b32adc5c
diff --git a/docs/04_reward_model.md b/docs/04_reward_model.mdindex 24c9bcb..a882f3c 100644--- a/docs/04_reward_model.md+++ b/docs/04_reward_model.md@@ -36,7 +36,7 @@ flowchart LR## The model: a scalar head on the backbone-[`RewardModel`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/reward_model.py#L37) wraps a `Transformer`, drops the `lm_head`,+[`RewardModel`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/reward_model.py#L33) wraps a `Transformer`, drops the `lm_head`,and reads the reward off the **last real token's** hidden state (the InstructGPT convention). Becauseattention is causal, that last token has seen the whole sequence and never attends to the right-paddingafter it, so we need no attention mask:diff --git a/docs/05_dpo.md b/docs/05_dpo.mdindex b7ad5d4..6b86c39 100644--- a/docs/05_dpo.md+++ b/docs/05_dpo.md@@ -37,7 +37,7 @@ flowchart LRDPO compares how much *more* likely the policy makes the chosen response vs the rejected one, relativeto the reference. So I need the **summed log-prob of each response** under both models.-[`sequence_logprobs`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/rollout.py#L268) does exactly that (and is reused by PPO/GRPO):+[`sequence_logprobs`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/rollout.py#L250) does exactly that (and is reused by PPO/GRPO):```pythondef sequence_logprobs(model, sequences, response_mask, *, temperature=1.0, requires_grad=True):@@ -48,7 +48,7 @@ def sequence_logprobs(model, sequences, response_mask, *, temperature=1.0, requi## The DPO loss-[`dpo_loss`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/dpo.py#L21) is the canonical objective. The β temperature controls how+[`dpo_loss`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/dpo.py#L42) is the canonical objective. The β temperature controls howhard it pushes away from the reference:```python@@ -62,8 +62,8 @@ def dpo_loss(policy_chosen_logps, policy_rejected_logps, ref_chosen_logps, ref_rreturn loss, chosen_reward, rejected_reward```-The two **variants** ([`orpo_loss`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/dpo.py#L48),-[`kto_loss`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/dpo.py#L71)) live in the same file:+The two **variants** ([`orpo_loss`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/dpo.py#L121),+[`kto_loss`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/dpo.py#L144)) live in the same file:- **ORPO**: reference-free: combines the SFT negative-log-likelihood on the chosen response with anodds-ratio preference term, folding SFT + alignment into one stage (no frozen reference needed).- **KTO**: treats chosen as *desirable* and rejected as *undesirable* against a reference-KL baselinediff --git a/docs/06_ppo.md b/docs/06_ppo.mdindex 16d3431..a86400d 100644--- a/docs/06_ppo.md+++ b/docs/06_ppo.md@@ -53,9 +53,9 @@ def forward(self, idx):## Rollout + log-probs-[`rollout_prompts`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/rollout.py#L180) length-buckets the prompts and samples a-completion for each, and [`generate_with_logprobs`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/rollout.py#L94) records the-sampling log-probs. Log-probs are always taken in **fp32** ([`compute_logprobs`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/rollout.py#L233))+[`rollout_prompts`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/rollout.py#L162) length-buckets the prompts and samples a+completion for each, and [`generate_with_logprobs`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/rollout.py#L76) records the+sampling log-probs. Log-probs are always taken in **fp32** ([`compute_logprobs`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/rollout.py#L215))because PPO subtracts them and bf16 rounding there is harmful.## GAE: Generalized Advantage Estimationdiff --git a/docs/07_grpo.md b/docs/07_grpo.mdindex d0453e8..60a537b 100644--- a/docs/07_grpo.md+++ b/docs/07_grpo.md@@ -36,7 +36,7 @@ flowchart LR## Group-relative advantage-[`group_advantages`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/grpo.py#L17) is the whole idea: standardize rewards *within+[`group_advantages`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/grpo.py#L47) is the whole idea: standardize rewards *withineach group*, so a good answer is one that beats its siblings on the same prompt:```python@@ -52,9 +52,9 @@ groups as a health metric.## The loss: clipped surrogate + KL-[`grpo_loss`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/grpo.py#L37) applies the same PPO-style token-level clipped surrogate+[`grpo_loss`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/grpo.py#L97) applies the same PPO-style token-level clipped surrogate(advantage broadcast across a completion's tokens) plus a per-token KL penalty to the reference, using-Schulman's non-negative **k3** estimator ([`k3_kl`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/grpo.py#L31)):+Schulman's non-negative **k3** estimator ([`k3_kl`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/grpo.py#L65)):```pythonratio = torch.exp(new_logp - old_logp)diff --git a/docs/09_inference.md b/docs/09_inference.mdindex b33a905..a1d26fb 100644--- a/docs/09_inference.md+++ b/docs/09_inference.md@@ -46,7 +46,7 @@ state = {k.removeprefix("module.").removeprefix("transformer."): v for k, v in s## Chat vs raw-[`generate_reply`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/inference.py#L37) has two modes, reusing the same tested+[`generate_reply`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/inference.py#L44) has two modes, reusing the same testedgeneration core as training/eval ([`batched_generate`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/post_training/evaluation.py#L24)):- **chat** (default): wraps your text in the chat template (optionally with a `system` message) anddiff --git a/docs/README.md b/docs/README.mdindex 8f872b5..24b43b6 100644--- a/docs/README.md+++ b/docs/README.md@@ -81,7 +81,7 @@ flowchart TD## The one design rule: *wrap, don't rewrite*Everything here sits on top of the original [`Transformer`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/models/transformer.py). I changed the-educational model in exactly **one** place: I added a [`forward_hidden`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/models/transformer.py#L56)+educational model in exactly **one** place: I added a [`forward_hidden`](https://github.com/FareedKhan-dev/train-llm-from-scratch/blob/main/src/models/transformer.py#L68)method that returns the final hidden states the `lm_head` consumes. Every post-training head (a valuehead for PPO, a scalar reward head for the reward model) and every RL log-prob computation composes*around* that one method, so the from-scratch model you already understand stays intact.