An illustrated guide to fine-tuning: how CPT, SFT, DPO, GRPO, SDPO and model distillation work, where LoRA fits, and how to evaluate quality, latency and cost.
Your data can teach a model more than a prompt can.Follow six animated training flows, from domain textand expert demonstrations to preferences, rewardsand distillation. Choose the signal your task needs.
Companies with demanding, repeatable AI use cases can build an advantage by improving the models behind their products. Prompts, retrieval and context engineering provide essential instructions and information. Training adds another lever: it changes the model’s learned behavior, so the capability can carry across many requests.
The opportunity is practical. A domain specialist can understand unfamiliar terminology more readily. A tool-using agent can learn the schemas and workflows of your environment. A smaller model can learn a focused capability from a larger teacher. Those benefits must be measured on your workload; fine-tuning is a way to pursue them, not an automatic guarantee.
The most useful question is what evidence you can provide. A corpus teaches domain language. An excellent response demonstrates behavior. A preference pair expresses a judgment. A verifier rewards a successful outcome. Detailed feedback explains where an attempt went wrong. Teacher guidance transfers a capability to a student.
The flows below follow that evidence through a training step. They are conceptual illustrations, not recorded model runs or performance benchmarks. The Python documentation image and GSM8K question are real sources; preference pairs, code attempts and reward displays are constructed examples. Library names identify implementation options, not endorsements or customer deployments.
Continued pretraining extends a pretrained model’s language-modeling objective on a new corpus. A manufacturer might curate technical manuals; a developer-tools team might use approved code and documentation. Text becomes tokens, the model predicts each next token, and the observed continuation supplies the target. You do not need to write an ideal response for every document.
CPT is useful when the gap is domain fluency: terminology, recurring structures and relationships that appear throughout a body of text. It does not automatically produce a helpful assistant or a reliable tool user. Those behaviors may require later instruction training. Curate the corpus, manage duplication and mix in general data where appropriate; specializing too narrowly can erode existing capabilities.
Keep the distinction between stable knowledge and changing facts. A model may benefit from learning the language of inventory management. Today’s stock count should still come from the inventory system. Training is also a poor mechanism for making a single fact easy to remove later.
Implementation reference: Transformers causal language modeling
Supervised fine-tuning teaches from examples of the desired response. These can be expert-written answers, structured JSON, or successful tool interactions. For an agent, capture the real tool names, arguments, observations and next actions. A polished answer alone cannot demonstrate all the steps that made it reliable.
In the completion-only setup illustrated here, the prompt is context and its tokens are excluded from the loss. Earlier gold response tokens also provide context as the model predicts the next response token: this is teacher forcing. The current target is not visible ahead of its prediction. Other SFT configurations train on different token subsets, so inspect the actual loss mask and chat template.
SFT is often a useful starting point when you can show what success looks like. Include difficult and varied examples, including correct refusals and recovery from tool errors where relevant. Hold out tasks that test generalization; near-duplicates of training examples can make a weak model look strong.
Implementation reference: TRL SFTTrainer
Dataset shown in the walkthrough: GSM8K, released by OpenAI
Direct preference optimization starts with two responses to the same prompt: one chosen, one rejected. A pair can express a preference for grounded tool use, a clearer explanation or adherence to an internal policy. The labels need a coherent rubric. A longer answer is not necessarily a better answer.
The trainable policy and a frozen reference score both responses. DPO optimizes the chosen-versus-rejected log-probability margin relative to that reference. Standard offline DPO works on existing pairs without generating new rollouts during each training step or fitting a separate reward model.
Use it when comparison is easier to collect than a single perfect answer. SFT can also teach tone and judgment; DPO is a different supervision format, not the exclusive route to those behaviors. Evaluate whether learned preferences carry over to unseen situations and whether the model preserves factual and task performance.
Research: Direct Preference Optimization
Implementation reference: TRL DPOTrainer
Group relative policy optimization lets the current model attempt the task several ways. A verifier or reward function scores the attempts. The update compares each score with its group baseline and reinforces relatively successful behavior. Unlike actor–critic approaches, GRPO does not require a separate learned critic to estimate the baseline.
This is attractive when success is checkable: a program passes tests, a calculation is correct, or an agent completes a task in a controlled environment. The quality of the reward becomes central. An incomplete test suite can reward a shortcut; a weak judge can prefer plausible prose over a correct result.
Generation is part of the training cost. Plan for rollout throughput, sequence length, environment execution and weight synchronization alongside the optimizer. Libraries such as TRL and verl can coordinate training, while vLLM can serve generation. A reliable evaluation set remains separate from the reward used for learning.
Research: DeepSeekMath, which introduced GRPO
Implementation reference: TRL GRPOTrainer
A failed test often tells you more than “incorrect.” It can identify an exception, a missing edge case or a violated contract. Self-distillation policy optimization uses this richer information to form a teaching signal. The student attempts the task; a self-teacher conditions on useful feedback; the student then learns from the teacher’s feedback-informed token predictions.
The important separation is between the teacher’s information and the student’s starting point. The teacher can see hindsight that the student did not originally have. Training tries to transfer the benefit of that hindsight into the student’s initial policy, so later attempts may need less correction.
SDPO is an emerging approach rather than a universal replacement for SFT or reinforcement learning. Feedback quality, the teacher construction and the distillation objective matter. TRL exposes it in an experimental module, with options for successful-rollout context, different teacher updates and blended losses. Pin versions and validate the specific implementation you use.
Research: Reinforcement Learning via Self-Distillation
Implementation reference: TRL experimental SDPO
Conventional teacher–student distillation can make a strong capability more practical to serve. Choose a teacher that performs well on your target tasks, collect its guidance, and train a student to reproduce the useful behavior. Accepted teacher responses can become SFT examples. When token distributions are available, a distillation loss can transfer a richer signal.
The student need not match every capability of the teacher. It needs to meet your product’s requirements on its intended workload. That focus can create room to reduce serving resources or latency, but a smaller model can also require extra retries. Compare cost per successful task, including failures and fallbacks, rather than model size alone. Respect the teacher’s license and service terms when collecting training material.
Implementation reference: TRL Generalized Knowledge Distillation
These techniques answer a different question. Full fine-tuning updates the model’s weights directly. LoRA keeps the original weights frozen and trains low-rank updates. QLoRA adds a quantized frozen base to reduce memory requirements while training adapters. They change the parameter and memory strategy; the learning signal can still come from demonstrations, preferences or another compatible objective.
An adapter approach can make experiments easier to run and store. It does not replace data preparation, evaluation or a deployment plan. Check the supported architecture, target modules and serving path, and measure the resulting behavior instead of assuming adapters or full fine-tuning will always win.
For Agent RFT, the environment is part of the task. A general model may have learned tool use on different schemas and workflows. Training on your actual tool contracts and representative observations can improve how consistently it selects tools, supplies arguments and recovers from errors. The benefit to pursue is a better user experience: fewer avoidable retries, shorter completion time and lower cost for successful work.
Build the evaluation suite before scaling training. Measure task success, invalid calls, grounded answers, regressions outside the target domain, end-to-end latency and cost. Separate customer or task families across training and evaluation when leakage is possible. Run the base model and candidate through the same harness so you can attribute a difference to the model.
Use industry data and benchmarks to understand what your users need. TensorOps works with data partners to help teams identify relevant data and evaluate coverage. Combine that external perspective with your own reviewed examples and production failure analysis. A useful training set reflects the tasks you want to become good at, not simply the data that is easiest to export.
Start with the smallest experiment that can establish an advantage. You may need one method, or a sequence such as domain adaptation followed by SFT and preference or reward-based refinement. Budget for data work, training, rollouts, evaluation and ongoing serving. Expand when the improvement survives held-out tests and a realistic production trial.