Buckets:
name: llm-alignment-training
description: >-
Implement post-training alignment (SFT, DPO, RLHF) for language models — from
base model to conversational assistant. Covers masked instruction loss,
preference optimization, reward modeling, and integration with custom training
loops.
tags:
- llm
- alignment
- sft
- dpo
- rlhf
- fine-tuning
- post-training
LLM Alignment Training
Implement SFT (Supervised Fine-Tuning), DPO (Direct Preference Optimization), and RLHF (Reinforcement Learning from Human Feedback) for language models. This skill covers the full pipeline from base pre-trained model to conversational, aligned assistant.
When to Use
- User wants to add conversational ability to a base language model
- User wants to implement instruction-following training
- User wants to add preference-based alignment (DPO/RLHF)
- User has a custom model implementation and needs alignment training code
- User wants to understand masked loss strategies for SFT
Core Concepts
Training Stages
Pretrained Model → SFT → DPO/RLHF → Aligned Model
(next-token) (instruction) (preference)
| Stage | Data | Loss Function | Purpose |
|---|---|---|---|
| SFT | (instruction, response) pairs | Masked cross-entropy | Learn to follow instructions |
| DPO | (instruction, chosen, rejected) triples | DPO loss | Learn to prefer better responses |
| RLHF | (instruction, chosen, rejected) + reward model | PPO/REINFORCE | Optimize for human preference |
SFT: Masked Instruction Loss
The key insight: only compute loss on the response tokens, not the instruction tokens. This prevents the model from "re-learning" the question itself.
Input: [bos] Question [ANS] Answer tokens...[eos]
Mask: [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1]
↑ loss starts here
Implementation:
- Tokenize
instruction + responseas a single sequence - Build loss mask: 0 for instruction positions, 1 for response positions
- Compute cross-entropy only where mask = 1
- Normalize by number of response tokens
DPO: Direct Preference Optimization
DPO eliminates the need for a separate reward model by using the policy model itself as the reward signal relative to a frozen reference model.
L_DPO = -log(σ(β × (log π(y_w)/π_ref(y_w) - log π(y_l)/π_ref(y_l))))
Where:
π= policy model (being optimized)π_ref= reference model (frozen copy of SFT model)y_w= chosen (preferred) responsey_l= rejected responseβ= temperature parameter (typically 0.1-0.5)
Key implementation details:
- Reference model is a frozen copy — never updated
- Both models share the same architecture
- Log probabilities are computed per-token and summed over the response
- The implicit reward is
r(y) = β × (log π(y) - log π_ref(y))
RLHF: Full Pipeline (when DPO is insufficient)
- Train reward model on preference data
- Optimize policy against reward model using PPO
- KL penalty to prevent policy from diverging too far from reference
Implementation Patterns
For Custom C++ Implementations
When implementing alignment for a custom model (not PyTorch/HuggingFace):
Reuse existing infrastructure:
- Model forward pass
- Optimizer (AdamW)
- Learning rate scheduler
- Model serialization format
Add alignment-specific components:
- Loss mask computation for SFT
- Reference model copy for DPO
- Log probability computation for DPO
Data format:
- JSONL is the standard:
{"instruction": "...", "response": "..."} - For DPO:
{"instruction": "...", "chosen": "...", "rejected": "..."} - Alternative: plain text with delimiter (e.g.,
|||)
- JSONL is the standard:
Checkpoint format:
- Reuse existing model serialization
- Save both policy and reference models for DPO
Training Hyperparameters
| Parameter | SFT | DPO | RLHF |
|---|---|---|---|
| Learning rate | 1e-5 to 5e-5 | 1e-6 to 5e-6 | 1e-6 to 1e-5 |
| Epochs | 1-5 | 1-5 | 1-3 |
| Batch size | 4-32 | 4-32 | 4-16 |
| Max seq length | 512-4096 | 512-4096 | 512-2048 |
| Warmup ratio | 0.03-0.1 | 0.03-0.1 | 0.03-0.1 |
| β (DPO temp) | — | 0.1-0.5 | — |
| KL penalty | — | — | 0.01-0.1 |
Common Pitfalls
SFT Pitfalls
- Not masking instruction loss: Model learns to "repeat" the question rather than answer it
- Too high learning rate: Destroys pre-trained knowledge (catastrophic forgetting)
- Insufficient data diversity: Model overfits to specific response patterns
- Wrong tokenization: Instruction and response must use the same tokenizer as pre-training
DPO Pitfalls
- Reference model not frozen: Both models update, destroying the preference signal
- β too large: Policy diverges too far from reference, quality degrades
- β too small: No meaningful preference learning occurs
- Poor quality preference data: Chosen/rejected pairs must have clear quality difference
- Not using SFT model as starting point: DPO works best when initialized from SFT, not base model
General Pitfalls
- Forgetting to call
model.train()/model.eval(): Batch norm and dropout behave differently - Not saving checkpoints: Alignment training can be unstable; save frequently
- No evaluation: Always hold out eval data to monitor overfitting
- Ignoring sequence length: Long sequences need more memory; consider gradient accumulation
Verification Checklist
After implementing alignment training:
- SFT loss decreases over training
- DPO loss decreases (or accuracy of preference prediction increases)
- Model generates coherent responses to unseen instructions
- Model prefers chosen over rejected responses (DPO)
- Reference model remains unchanged (DPO)
- Checkpoints can be loaded and used for inference
- No NaN/Inf in loss or gradients
- Eval loss tracks training loss (no severe overfitting)
Related Skills
huggingface-hub— for using HuggingFace models/datasetsevaluating-llms-harness— for benchmarking aligned modelsserving-llms-vllm— for serving aligned modelsdspy— for declarative LM program optimization
References
- See
references/neuroflow-sft-dpo-implementation.mdfor a concrete example of implementing SFT+DPO for a custom C++ LLM (NeuroFlow model with SN/ECN/DMN architecture)
Xet Storage Details
- Size:
- 6.42 kB
- Xet hash:
- 10bd8819db608d77553a6b1d0f4deb83f2ca9d12b034f13c622bb35e6fdc449d
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.