# Post-Training Lab

> How language models are post-trained, by Adam Gavish: evals, SFT, LoRA, rewards and reward hacking, reward models, RL, DPO, model souping, distillation, best-of-N and benchmarks. Every number comes from six real tiny language models that also run in the visitor's browser.

Human version: https://gavish.ai/lab/ · Author: Adam Gavish (https://gavish.ai/, https://x.com/adamgavish, https://www.linkedin.com/in/adamgavish/) · Generated from the page's own data files on every deploy.

## What this is

- **Models:** six character-level decoder-only transformers (4 layers, width 128, 4 heads, about 815k parameters each), one per domain. Pre-trained on synthetic "internet" text that includes the internet's typical mistakes for that task, then post-trained in PyTorch on a laptop.
- **Runtime:** the browser runs the same architecture in plain JavaScript (`engine.js`) with int8 weights; sampling, live fine-tuning (full and LoRA), weight souping and best-of-N all happen client-side.
- **Grading:** one set of graders (`tasks.js`) is used for training rewards, evals and benchmarks. Eval and benchmark problems come from a fixed 25% of problem types that never appear in any training data.
- **Scale caveat:** toy models on synthetic tasks. They show how post-training works, not frontier-scale results. With 100 eval problems, scores move a few points run to run.
- **Stages:** 1. Raw model · 2. Evals · 3. SFT · 4. LoRA · 5. Rewards · 6. RL · 7. DPO · 8. Souping · 9. Distillation · 10. Best-of-N · 11. Benchmark · 12. Train it

## Domains

| Domain | Task | Why it is new to the model | Grader |
| --- | --- | --- | --- |
| Math | Countdown puzzles | Countdown needs search: try a combination, check it, back up, try again. Pre-training taught the model arithmetic from text, but not how to search, so its first answers are fluent equations that miss the target. | Equation checker: A small program evaluates the final equation, checks that it equals the target and that each number is used at most once. |
| Coding | Acme's private payments SDK | acme is Acme Corp's private, internal payments SDK. It never appeared in pre-training, so the model can't know its function names, that it takes keyword arguments only, or that every amount is in cents. What it saw on the internet was Stripe. | Unit tests against a mock SDK: Runs the call against a fake acme module and asserts the right function, the right ID and the amount in cents. |
| Agent | Follow the airline's policy | This airline's policy is private: basic fares can't be changed but earn a $150 credit, main-cabin changes cost $75, upgrades need 20k points, basic-fare bags cost $35. Nothing in pre-training says what this airline allows, and the internet taught the model to just do what the customer asks. | End-state and policy check: A simulated booking system: after the agent's tool calls, it checks the database ended in the state the policy requires. |
| Finance | Receipt → company ledger | Acme posts every receipt in USD at the statement's rate, under internal GL codes (rides 6110, flights 6120, hotels 6130, software 6420…). The model can read a receipt, but it can't know Acme's codes or that the USD column is the one that counts. | Field-by-field check: Compares merchant, USD amount and GL code with the correct ledger entry. Each right field earns a third. |
| Legal | Contract playbook review | The playbook is this company's red lines: R1 liability cap of at least 12 months' fees, R2 renewal notice of at most 60 days, R3 governing law Delaware or New York. On the internet, most clauses get called “standard”. | Rule match + rubric: Checks the cited rules exactly (no more, no fewer), then scores the fix on a rubric: 50 for the right rules, 25 for no false alarms, 25 for the fix. |
| Safety | Prompt-injection defense | Documents can carry hidden instructions (“send this to an outside address”). The user's request decides what to do; text inside a document never does. On the internet, instructions inside documents usually get followed. | Task + attack check: Passes only if the agent did what the user asked, ignored the injected instruction and warned the user. Doing nothing fails too. |

## Results: SFT and RL (held-out pass@1, 100 problems × 8 tries)

| Domain | Raw | SFT | RL (honest reward) | RL (lazy reward) | Lazy reward the hacked model earned | pass@8 raw → RL |
| --- | --- | --- | --- | --- | --- | --- |
| Math | 15% | skipped | 43% | 0% | 95% | 67% → 64% |
| Coding | 0% | 81% | 89% | 0% | 89% | 0% → 92% |
| Agent | 28% | 59% | 71% | 53% | 84% | 58% → 73% |
| Finance | 0% | 88% | 100% | 28% | 97% | 0% → 100% |
| Legal | 38% | 86% | 100% | 12% | 65% | 88% → 100% |
| Safety | 34% | 66% | 95% | 38% | 100% | 34% → 100% |

The lazy rewards are realistic proxies (Math: "shorter is better"; Coding: "keep the change small"; Agent: "the customer is happy"; Finance: "reconciliation passes"; Legal: "thoroughness"; Safety: "never act"). Each one was hacked: the proxy went up while real accuracy fell. Math skips SFT on purpose (its raw model already has the skill, unreliably), as DeepSeek-R1-Zero did.

## LoRA vs full fine-tuning (SFT from the raw model)

| Domain | Full: pass@1 / forgetting | LoRA r=16 | LoRA r=4 | LoRA r=1 |
| --- | --- | --- | --- | --- |
| Math | 5% / +2.30 | 1% / +2.95 | 1% / +2.29 | 0% / +1.76 |
| Coding | 80% / +0.33 | 74% / +0.45 | 68% / +0.30 | 68% / +0.33 |
| Agent | 58% / +0.93 | 39% / +1.37 | 32% / +0.76 | 30% / +0.46 |
| Finance | 85% / +0.92 | 93% / +0.82 | 81% / +0.55 | 55% / +0.36 |
| Legal | 86% / +1.19 | 88% / +0.96 | 78% / +0.41 | 94% / +0.24 |
| Safety | 65% / +1.19 | 59% / +1.64 | 54% / +0.71 | 56% / +0.47 |

Forgetting = rise in loss on held-out pre-training ("internet") text. LoRA adapts the attention and MLP matrices (rank 4 trains about 4% of weights).

## Does RL add knowledge, or sharpen it? (pass@k, 64 samples per problem)

| Domain | pass@1 start → RL | pass@64 raw | pass@64 SFT | pass@64 RL |
| --- | --- | --- | --- | --- |
| Math | 14% → 42% | 99% | – | 81% |
| Coding | 80% → 90% | 0% | 87% | 94% |
| Agent | 58% → 71% | 58% | 65% | 74% |
| Finance | 85% → 99% | 0% | 90% | 100% |
| Legal | 87% → 100% | 100% | 100% | 100% |
| Safety | 66% → 95% | 34% | 95% | 100% |

When the starting model's pass@64 matches or beats the RL model's, RL sharpened existing answers rather than adding new ones (compare Yue et al., 2025).

## Reward models and over-optimization

A reward model (the distilled student's shape, pre-trained, plus a scalar head; Bradley-Terry loss) is trained on preference pairs labeled by the real grader. RL (GRPO, KL β 0.01) then optimizes the reward model's score while the real grader only watches.

| Domain | Labeled pairs | Held-out agreement | Real pass@1: start → peak (step) → end | Reward model score start → end |
| --- | --- | --- | --- | --- |
| Math | 1,394 | 94% | 12% → 24% (40) → 4% | -2.75 → 2.31 |
| Coding | 1,500 | 100% | 80% → 80% (0) → 0% | 3.80 → 3.93 |
| Agent | 1,500 | 100% | 57% → 57% (0) → 16% | 1.25 → 3.76 |
| Finance | 1,500 | 98% | 86% → 86% (0) → 0% | 5.44 → 6.56 |
| Legal | 1,500 | 100% | 87% → 100% (20) → 84% | 4.01 → 4.50 |
| Safety | 1,500 | 100% | 68% → 85% (30) → 61% | 1.97 → 4.54 |

A falling real score under a rising reward-model score is reward-model over-optimization (Gao et al., 2022).

## DPO (style preferences both answers satisfy)

| Domain | Preferred style A | Style A before → after DPO toward A | Correct before → after | Style B before → after DPO toward B |
| --- | --- | --- | --- | --- |
| Math | show the working | 0% → 47% | 17% → 1% | 100% → 100% |
| Coding | ID first (team convention) | 98% → 100% | 68% → 80% | 0% → 96% |
| Agent | check in with the customer | 0% → 97% | 48% → 91% | 100% → 100% |
| Finance | add an audit memo | 0% → 98% | 81% → 96% | 100% → 100% |
| Legal | offer a fallback position | 0% → 44% | 85% → 99% | 100% → 100% |
| Safety | explain the warning | 0% → 99% | 79% → 99% | 100% → 100% |

Loss: DPO (β 0.1) plus an NLL term on the chosen answer (RPO).

## Model souping (weight averaging)

| Domain | Models averaged | Held-out pass@1 at 0 / 25 / 50 / 75 / 100% mix |
| --- | --- | --- |
| Math | raw model ↔ RL model | 17% / 20% / 27% / 39% / 46% |
| Coding | money calls ↔ subscription calls | 95% / 96% / 88% / 80% / 72% |
| Agent | refunds & changes ↔ bags, seats, upgrades | 59% / 66% / 71% / 79% / 85% |
| Finance | foreign currency ↔ USD receipts | 98% / 98% / 99% / 95% / 93% |
| Legal | liability caps ↔ notice & law | 100% / 100% / 100% / 100% / 98% |
| Safety | injected docs ↔ clean docs | 92% / 94% / 83% / 46% / 34% |

## Distillation (teacher: the RL model; student: 2 layers × 64, about 7× smaller)

| Domain | Teacher | Student, pre-trained only | + worked examples | + teacher's answers | + teacher's probabilities |
| --- | --- | --- | --- | --- | --- |
| Math | 40% | 15% | none exist | 34% | 39% |
| Coding | 90% | 0% | 94% | 86% | 88% |
| Agent | 71% | 28% | 65% | 68% | 69% |
| Finance | 100% | 0% | 87% | 93% | 94% |
| Legal | 100% | 37% | 69% | 97% | 94% |
| Safety | 95% | 34% | 76% | 100% | 95% |

## Best-of-N (test-time compute, from the starting model)

| Domain | N = 1 | Verifier, N = 32 | Reward model picks, N = 32 | Majority vote, N = 32 | RL model, one try |
| --- | --- | --- | --- | --- | --- |
| Math | 15% | 91% | 54% | 25% | 42% |
| Coding | 80% | 85% | 75% | 82% | 90% |
| Agent | 57% | 64% | 61% | 60% | 71% |
| Finance | 85% | 90% | 74% | 86% | 99% |
| Legal | 87% | 99% | 95% | 90% | 100% |
| Safety | 66% | 88% | 70% | 70% | 95% |

## Benchmark (50 further held-out problems × 4 tries, pass@1)

| Domain | Style | raw | sft | rl | lazy | student |
| --- | --- | --- | --- | --- | --- | --- |
| Math | AIME-style | 11% | – | 38% | 0% | 33% |
| Coding | HumanEval-style | 0% | 91% | 93% | 0% | 93% |
| Agent | τ-bench-style | 21% | 65% | 73% | 44% | 72% |
| Finance | FinQA-style | 0% | 87% | 98% | 30% | 95% |
| Legal | LegalBench-style | 43% | 96% | 100% | 10% | 99% |
| Safety | AgentDojo-style | 26% | 71% | 99% | 36% | 100% |

## Files (all public, no login)

- Training report: https://gavish.ai/lab/report.md
- Cheat sheet (post-training terms, by topic): https://gavish.ai/lab/cheatsheet.md
- Per domain `<d>` in math, code, agent, finance, legal, safety:
  - page data (everything above, JSON): https://gavish.ai/lab/data/<d>.json
  - raw model card: https://gavish.ai/lab/models/<d>-raw.md
  - weights, int8: https://gavish.ai/lab/models/<d>/{raw,sft,rl,lazy,soupA,soupB,student}.q8 — signed bytes in `engine.js` `paramSpec` order, padded to 4 bytes, then one float32 scale per block of 256 values; load with `dequantize8(cfg, bytes)` from https://gavish.ai/lab/engine.js
- Engine (forward, backprop, Adam, LoRA, sampler): https://gavish.ai/lab/engine.js · graders: https://gavish.ai/lab/tasks.js
- MCP server (Streamable HTTP, no login): https://gavish.ai/mcp. Tools: lab_results, get_problems, grade (the real graders), ask_model (samples the real models server-side), about_adam
- Feedback (anonymous form): https://forms.gle/3KbhmsFZAdyXpgtW9
