Lesson 2 of 3 Fine-Tuning Foundation Models

Advanced Fine-Tuning Methods

This lesson explains supervised fine-tuning, direct preference optimization and reinforcement fine-tuning, the training types and hyperparameters, and parameter-efficient fine-tuning of open models.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between SFT, DPO and RFT.
  • Describe the DPO data format and the role of graders in RFT.
  • Compare the training types.
  • Explain how LoRA and QLoRA reduce training cost.

Supervised fine-tuning

Supervised fine-tuning (SFT) trains the model to reproduce the assistant responses in the training conversations. It is the most widely supported method and the right starting point whenever the desired answer for each input can be written down. SFT teaches formats, styles, domain conventions and tasks such as classification and extraction. Its limit is the quality of the examples: the model learns the inconsistencies and errors in them as faithfully as the correct behaviour.

SFT can also train tool calling. Training conversations include tool definitions and assistant messages that call tools with the right arguments, which teaches a model to select tools and fill parameters more reliably for a specific set of functions.

Direct preference optimization

Direct preference optimization (DPO) trains on pairs of responses. Each example contains an input, which holds the conversation messages, a preferred_output and a non_preferred_output. The model learns to make the preferred kind of response more likely and the non-preferred kind less likely, without a separate reward model.

{"input": {"messages": [{"role": "user", "content": "Explain why my claim was declined."}]}, "preferred_output": [{"role": "assistant", "content": "Your policy excludes gradual leaks. The adjuster found ... You can appeal by ..."}], "non_preferred_output": [{"role": "assistant", "content": "Your claim was declined per policy terms."}]}

DPO suits qualities that reviewers can judge by comparison more easily than they can write a perfect answer: tone, helpfulness, completeness and safety. Preference pairs often come from production, where users or reviewers compare two responses. A common pattern is SFT first, to teach the task and format, followed by DPO on the fine-tuned model to refine the quality of its responses. A DPO job that continues from an SFT model uses the fine-tuned model's identifier, not its deployment name.

Reinforcement fine-tuning and graders

Reinforcement fine-tuning (RFT) trains a reasoning model, such as o4-mini or, by invitation, gpt-5, by letting it generate answers and scoring them with a grader. The model is rewarded for reasoning that leads to high scores. Training examples contain the prompt and the reference information the grader needs, not a complete answer to imitate, so RFT suits tasks with many valid answers or reasoning paths, where correctness can be checked: calculations, code, policy decisions and structured extraction with verifiable fields.

The grader defines what the model learns, so it must reward the real goal. Foundry provides several grader types, described in the following table.

Grader typeAPI typeHow it scores
String checkstring_checkExact or pattern comparison (eq, ne, like, ilike) with a reference value
Text similaritytext_similarityMetrics such as fuzzy match, BLEU, GLEU, METEOR or ROUGE against a reference
Model graderscore_modelA model scores the answer against a rubric
Python graderpythonA grade(sample, item) function computes a score in code
Multi-gradermultiCombines several graders with a formula

RFT jobs report training and validation reward rather than loss. A grader that can be satisfied without solving the task, such as one that checks only the JSON structure, leads to reward hacking: rewards rise while real quality does not. Graders should be tested on known good and bad answers before training, and the fine-tuned model must still be evaluated on held-out data with independent metrics.

Choosing the method

The choice of method follows from the kind of training signal the team can produce. The following comparison summarizes the three methods.

SFT
Training signalDemonstrated ideal responses
TeachesFormat, style, narrow tasks, tool calls
Modelsgpt-4o-mini, gpt-4o, the gpt-4.1 family and open models such as Llama, Qwen and gpt-oss
Main riskLearning errors in the examples
Choose when You can write the right answer
DPO
Training signalPreferred and non-preferred response pairs
TeachesTone, helpfulness, completeness, safety preferences
Modelsgpt-4o and the gpt-4.1 family, often after SFT
Main riskInconsistent preferences
Choose when You can say which answer is better
RFT
Training signalA grader that scores generated responses
TeachesReasoning towards verifiably correct answers
ModelsReasoning models such as o4-mini and gpt-5 (by invitation)
Main riskReward hacking by a weak grader
Choose when You can check whether an answer is right

Training types and hyperparameters

When a job is created, a training type decides where and on what capacity it runs. Standard training runs in the resource's region, which keeps training data in that region for data residency requirements. Global training can use capacity in other regions at a lower price, and Developer training uses idle capacity at the lowest price, with no latency or SLA guarantees, so jobs can be preempted and resumed later. Data zone training keeps data within a data zone, and currently supports only the United States data zone. The available types depend on the model and region.

The main SFT hyperparameters are set under the job's method. Leaving them at auto is the recommended starting point; they are then adjusted based on the training metrics, as the next module describes.

HyperparameterEffectAdjust when
n_epochsNumber of passes through the training dataIncrease if the model underfits; decrease if validation loss rises
batch_sizeExamples per training stepLarger batches give more stable updates on large datasets
learning_rate_multiplierScales the learning rateLower it if training is unstable or overfits; raise it if learning is slow
from openai import OpenAI

client = OpenAI(base_url=f"{endpoint}/openai/v1/", api_key=token_provider())

train = client.files.create(file=open("train.jsonl", "rb"), purpose="fine-tune")
valid = client.files.create(file=open("valid.jsonl", "rb"), purpose="fine-tune")

job = client.fine_tuning.jobs.create(
    model="gpt-4.1-mini",
    training_file=train.id,
    validation_file=valid.id,
    suffix="claims",
    method={"type": "supervised",
            "supervised": {"hyperparameters": {"n_epochs": "auto"}}},
)
print(job.id, job.status)

Parameter-efficient fine-tuning of open models

Open-weight models from the model catalog, such as Llama, Mistral and Phi models, can also be fine-tuned, either through Foundry or in Azure Machine Learning jobs on GPU compute. Full fine-tuning updates every weight, which needs a great deal of GPU memory and produces a full copy of the model. Parameter-efficient fine-tuning (PEFT) trains only a small number of additional parameters.

  • LoRA (low-rank adaptation) freezes the base weights and trains small low-rank matrices added to selected layers. The trained adapter is a small file that is loaded with the base model.
  • QLoRA combines LoRA with a quantized base model, which reduces GPU memory further so that larger models fit on smaller GPUs.
  • Several adapters can share one base model, one per task or customer, which reduces storage and serving cost.

The LoRA rank sets the adapter's capacity: a higher rank can learn more, but trains more parameters. Managed fine-tuning of Azure OpenAI models handles these choices internally; for open models trained in Azure Machine Learning, they are part of the training script and its tracked parameters.

Worked example

Selecting a method

A tax firm wants a model that works out the deductible amount from a client's expense details, applying rules with several exceptions. Different reasoning paths can reach the same correct figure. The firm has 800 cases with the correct deductible amount but no written explanations, and it can compute the correct amount for any case with an existing rules engine.

This calls for reinforcement fine-tuning of a reasoning model such as o4-mini. The task requires multi-step reasoning, there are many valid reasoning paths, and correctness can be checked: a Python grader or a string check grader compares the model's final amount with the reference amount, with partial credit if useful. The training examples need only the case details and the reference amount.

SFT would need a full worked answer for each case, which the firm does not have, and would teach the model to imitate one path rather than to reach correct answers. DPO needs pairs of responses ranked by preference, which is an indirect signal for a task with a verifiable answer. The grader should be tested to ensure it cannot be satisfied by an answer that only looks well formed.

Check yourself

Reviewers can reliably say which of two responses has a better tone, but find it hard to write ideal responses. Which method fits?

Show answer

Direct preference optimization, which trains on preferred and non-preferred response pairs, so it learns from comparisons rather than from demonstrated ideal answers.

Exam Tips

Remember for the exam

  • SFT when you can demonstrate the answer, DPO when you can compare answers, RFT when you can grade them.
  • DPO examples have input, preferred_output and non_preferred_output; DPO often follows SFT.
  • RFT trains reasoning models such as o4-mini with string check, text similarity, model, Python or multi-graders.
  • Fine-tuning support depends on the model; check the supported methods and training types for each model and version.
  • Standard training keeps data in region; Data zone keeps it in a zone; Global is cheaper; Developer uses idle capacity without an SLA.
  • LoRA trains small adapters on a frozen base model; QLoRA adds quantization.