Project P08

Post-training an open LLM: LoRA, SFT, DPO and GRPO

How can a small open LLM be post-trained for a specific task on an 8 GB GPU, implementing each method and honestly evaluating what improves and what is lost?

stages
8
duration
13 h 50 min
concepts
20
available in English
8 / 8
completed by you
0 of 8

Where it fits

Overview

A complete post-training project using an open model with 0.5 to 3 billion parameters. Plan memory use and establish baseline evaluation, implement LoRA and QLoRA's NF4 quantization from scratch, perform SFT in a chosen domain using public data, generate on-policy preferences, train a Bradley-Terry reward model, implement DPO with its diagnostics, and finish with GRPO and verifiable mathematical reasoning rewards. A comprehensive evaluation measures gains, the alignment tax, and contamination, and the final model is exported for the local agent.

Why it matters

Adapting an open model to a task is a widely sought LLM engineering skill and allows you to own models trained on data that stays on your machine. Implementing LoRA, DPO, and GRPO from scratch and comparing them with libraries helps you understand failures such as forgetting, likelihood displacement, and reward hacking, and read alignment and reasoning papers critically.

Stages 8

  1. 01Memory plan and baseline evaluationSelect an open base model of 0.5B to 3B parameters such as Qwen2.5 or SmolLM2 after reviewing its license, estimate what fits in 8 GB for full fine-tuning, LoRA, and QLoRA, and compare with measured memory. Before training, fix a baseline evaluation harness with general lm-evaluation-harness tasks and a custom evaluation for the chosen domain. GPUAdvanced90 min
  2. 02LoRA from scratchImplement LoRA as an nn.Linear wrapper with zero initialization of B, alpha-over-r scaling, dropout, and merging into base weights, then inject it into a transformer model by module name. Verify that initialization leaves the model unchanged, test merge equivalence, and compare with PEFT. GPUAdvanced90 min
  3. 03QLoRA and NF4 quantizationImplement blockwise NF4 quantization with double quantization and compare it with bitsandbytes. Train a model of up to 3B that would not otherwise fit: a frozen 4-bit base, bf16 adapters, activation recomputation and a paged optimizer, measuring the difference from bf16 LoRA on a model that fits both ways. GPUAdvanced100 min
  4. 04Domain SFT and forgetting measurementBuild an instruction dataset for a domain from public sources, such as explaining and classifying software weaknesses from the CWE catalog and public advisories in Spanish; filter and deduplicate it against evaluation and fine-tune with LoRA or QLoRA. A small hyperparameter sweep measures domain gain and forgetting on general tasks. GPUAdvanced110 min
  5. 05On-policy preferences and reward modelGenerate multiple SFT-model answers per prompt and label them using available verifiers or a larger local judge, controlling for its biases. Train a Bradley-Terry reward model with LoRA on the pairs, measure test-pair accuracy, and establish the best-of-n baseline for DPO and GRPO. GPUAdvanced110 min
  6. 06DPO from scratch and its diagnosticsImplement DPO with masked sequence log-probabilities, a precomputed reference to save memory, beta-scaled loss, and implicit reward metrics. Sweep beta, monitor chosen and rejected log-probabilities for likelihood displacement, and compare the resulting policy with SFT and best-of-n. GPUAdvanced110 min
  7. 07GRPO with verifiable rewardsImplement GRPO to improve a 0.5B to 1.5B model's mathematical reasoning on GSM8K-style problems using verifiable correctness and format rewards. Build the complete loop with group sampling, normalized group advantages, clipped probability ratios, and KL, monitor entropy, length, and groups without signal, and actively test reward shortcuts. GPUResearch120 min
  8. 08Comprehensive evaluation and exportCompare base, SFT, DPO, and GRPO using the harness fixed in the first stage across domain, instruction following, mathematical reasoning, and general tasks, with intervals, length-controlled win rates, and alignment tax. Check contamination, merge adapters, and export the final quantized model for the local agent. GPUAdvanced100 min

Final deliverable

Your own specialist model

An open model post-trained in a chosen domain using SFT, DPO, and GRPO, with a reproducible evaluation report, model card, and quantized version integrated into the local agent.

Check what you already know

  • Calculate memory requirements for full fine-tuning, LoRA, and QLoRA, including activations and optimizer states
  • Implement a LoRA layer with exact merging into the base weights
  • Implement blockwise NF4 quantization with double quantization
  • Train and evaluate a Bradley-Terry reward model using on-policy preferences
  • Implement the DPO loss and diagnose likelihood displacement
  • Implement GRPO with verifiable rewards and monitor reward hacking

References