Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Generative AI›Supervised Fine-tuning (SFT)

Fine-tuning and Model Training

Supervised Fine-tuning (SFT)

Supervised Fine-tuning (SFT) is the process of taking a pre-trained base model and further training it on a curated dataset of prompt-response pairs to specialize its behavior. It is the primary method for transforming generic foundation models into task-specific assistants that follow instructions or adopt specific formatting constraints. You reach for SFT when prompting alone fails to elicit the desired consistency, tone, or structural accuracy required for your specific domain application.

The Logic of SFT

At its core, SFT functions by updating the model's internal weights through backpropagation to minimize the divergence between the model's predicted output and the ground truth contained in your training data. A base model is typically trained on vast, uncurated internet text, which makes it an excellent 'next-token predictor' but a poor 'instruction follower.' By feeding the model specific input-output pairs, we shift the probability distribution of the weights so that the model becomes biased toward generating tokens that align with the structure and content of your target data. This is essentially supervised learning: you provide the expected label (the desired response) for every input (the prompt). Unlike raw pre-training, SFT focuses the model on high-quality patterns, teaching it that certain inputs demand specific, formatted responses. Understanding this allows you to reason that SFT is not about adding new knowledge to the model, but about steering the model's existing reasoning capabilities toward a specific conversational style or functional output format.

# SFT works by adjusting model weights to align with target distributions
import torch

def calculate_loss(logits, labels):
    # Standard cross-entropy loss against the desired response tokens
    loss_fn = torch.nn.CrossEntropyLoss()
    # We calculate the loss only on the assistant's response part
    return loss_fn(logits.view(-1, logits.size(-1)), labels.view(-1))

Data Formatting and Prompt Templates

Data preparation is the most critical stage of SFT because the model learns exactly what it is given. You must encapsulate your data in a template that mirrors the structure the model will see during inference. A standard SFT prompt often follows a structure like 'Instruction: [Task]\nResponse: [Answer]'. If you provide inconsistent formatting, the model will struggle to generalize the 'end-of-sequence' signals or the 'instruction-following' trigger. You must ensure that your dataset consistently defines roles, such as user and assistant. When you train on these patterns, you are teaching the model to recognize the specific delimiter tokens that separate instructions from responses. Reasoning about this process helps you realize that if you use a prompt template during training that is completely alien to the model's pre-training architecture, it will fail to converge. Always use the same template for training and deployment to ensure the model recognizes the input structure it was trained to complete.

# Typical prompt template formatting for SFT data preparation
raw_data = {'instruction': 'Summarize this.', 'output': 'Short summary.'}

def format_prompt(data):
    # Consistent template is mandatory for successful fine-tuning
    return f"### Instruction: {data['instruction']}\n### Response: {data['output']}<|endoftext|>"

Training Parameters and Learning Rates

Choosing the right hyperparameters is a delicate balancing act in SFT, primarily because pre-trained models are highly sensitive to weight updates. If your learning rate is too high, you risk 'catastrophic forgetting,' where the model destroys its general language capabilities to obsessively overfit to your specific training dataset. Conversely, a learning rate that is too low will result in the model never truly learning the new patterns you are trying to impart. Generally, SFT requires a learning rate significantly lower than that used in original pre-training—often in the range of 1e-5 to 5e-6. You should also consider the number of epochs; unlike training from scratch, SFT often converges within one to three passes over the data. Reasoning about these parameters allows you to troubleshoot training failures: if your model starts outputting gibberish, your learning rate is likely too high; if it simply ignores your new instructions, it likely needs more epochs or better quality data.

# Setting a conservative learning rate for model stability
learning_rate = 2e-5
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)
# Small steps prevent catastrophic forgetting of pre-trained knowledge

Preventing Overfitting

Overfitting is a common failure mode in SFT where the model memorizes the training data verbatim instead of learning the underlying logic. This manifests as the model producing identical or near-identical responses to inputs that were in the training set while performing poorly on unseen, real-world prompts. To combat this, you should employ regularization techniques such as weight decay or dropout, and maintain a held-out validation set to monitor for signs of memorization. If the loss on your validation set starts increasing while the training loss decreases, you have hit the point of diminishing returns and should stop training immediately. Understanding this principle allows you to reason that model size matters: smaller models have less capacity and are more prone to rote memorization, whereas larger models are more robust but require more diverse datasets to avoid rigid adherence to training examples. Your goal is to foster generalization, not to build a simple look-up table.

# Adding weight decay as regularization to reduce overfitting
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5, weight_decay=0.01)
# The weight_decay parameter discourages reliance on any single set of weights

Evaluating SFT Success

Evaluation for SFT is notoriously difficult because standard metrics like BLEU or ROUGE are poor at capturing the quality, tone, or usefulness of generative responses. Because SFT aims to modify model behavior, your evaluation strategy must be functional. You should create a test suite of 'golden prompts' that represent the specific use cases the model is intended to handle. Additionally, using a more capable model to evaluate the outputs of your fine-tuned model—a technique often called 'LLM-as-a-judge'—can provide much more nuance than statistical similarity metrics. You must test for edge cases, such as the model's response to malicious inputs or prompts outside its training domain. Reasoning through evaluation means recognizing that a low loss value during training does not equate to a model that is actually 'good.' You are building a tool for a specific human-centric purpose, and therefore human verification remains the ultimate benchmark for your SFT deployment success.

# Simple verification function to check model performance on test prompts
def evaluate_model(model, test_prompt):
    input_ids = tokenizer.encode(test_prompt, return_tensors='pt')
    output = model.generate(input_ids, max_new_tokens=50)
    # Check if the output follows the expected style/format
    return tokenizer.decode(output[0])

Key points

  • SFT adjusts pre-trained model weights to better align with target instruction-following tasks.
  • Data quality is more important than quantity in SFT, as the model mirrors the input-output patterns provided.
  • Consistent prompt templating is essential to ensure the model recognizes instructions during both training and inference.
  • Learning rates must be kept low to prevent the model from forgetting its general pre-trained knowledge.
  • Overfitting occurs when a model memorizes training data rather than generalizing the underlying task logic.
  • Regularization techniques like weight decay are necessary to ensure the model produces flexible, usable responses.
  • Statistical metrics are often insufficient for evaluating SFT, making functional testing and human review essential.
  • The ultimate goal of SFT is to steer model behavior toward specific requirements without damaging its foundational language understanding.

Common mistakes

  • Mistake: Using a dataset that is too small. Why it's wrong: SFT requires diverse, high-quality examples to generalize; small datasets lead to overfitting. Fix: Ensure at least several hundred to thousands of high-quality, diverse instruction-response pairs.
  • Mistake: Including noisy or incorrect data. Why it's wrong: Models learn the style and errors of the training set; poor data degrades performance. Fix: Rigorously clean and curate the dataset, ensuring high-quality formatting and accurate labels.
  • Mistake: Overfitting to specific prompts. Why it's wrong: The model loses its ability to generalize to new inputs. Fix: Use regularization techniques like weight decay or dropout, and incorporate validation sets to monitor loss.
  • Mistake: Failing to use proper prompt templates. Why it's wrong: Inconsistent input formats confuse the model's structure. Fix: Standardize the input format consistently between training and inference (e.g., specific user/assistant tokens).
  • Mistake: Neglecting the catastrophic forgetting of base capabilities. Why it's wrong: Aggressive fine-tuning can make the model lose its pre-trained general knowledge. Fix: Mix in original pre-training data or use parameter-efficient fine-tuning (PEFT) methods.

Interview questions

What is the fundamental goal of Supervised Fine-tuning (SFT) in the context of large language models?

The fundamental goal of Supervised Fine-tuning is to take a foundational model that has already been pre-trained on a massive corpus of general data and adapt it to follow specific instructions or perform well on a particular task. While pre-training teaches the model the structure of language, SFT uses a curated dataset of prompt-response pairs to align the model's output behavior with human expectations, effectively teaching the model how to act like a helpful assistant rather than just a document completion engine.

How do we prepare a dataset for the Supervised Fine-tuning process?

Preparing a dataset for SFT requires creating high-quality, instruction-based pairs. Each data point usually consists of a 'prompt' (the user instruction) and a 'completion' (the desired ideal response). You must ensure the data is diverse and representative of the tasks you want the model to excel at. The formatting often follows a strict schema, such as adding special tokens like [INST] or <user> and <assistant> markers, so the model learns the boundary between the input prompt and its own generated response during the training phase.

Why is it important to use a smaller learning rate during SFT compared to pre-training?

Using a smaller learning rate during SFT is critical because the model has already learned general linguistic patterns. If you use a learning rate that is too high, you risk 'catastrophic forgetting,' where the model overwrites the valuable general knowledge it acquired during pre-training in favor of the specific patterns in your smaller fine-tuning dataset. A smaller rate ensures that the weights only receive subtle updates, allowing the model to refine its instruction-following capabilities without losing its foundational comprehension of grammar, reasoning, and world knowledge.

Compare Full Fine-tuning with Parameter-Efficient Fine-tuning (PEFT) techniques like LoRA.

Full Fine-tuning involves updating every single weight parameter in the model, which is computationally expensive and requires massive VRAM to handle gradients and optimizer states. In contrast, PEFT techniques like LoRA, or Low-Rank Adaptation, keep the original model weights frozen. Instead, they inject small, trainable rank-decomposition matrices into the model layers. This significantly reduces memory usage and training time because you are only updating a tiny fraction of the parameters, making it much more feasible to run fine-tuning on consumer-grade hardware while achieving performance comparable to full fine-tuning.

What role does the loss function play during the Supervised Fine-tuning process?

During SFT, the loss function used is typically standard Cross-Entropy Loss, applied only to the completion part of the training sequence. We mask the prompt tokens so the model does not try to learn how to predict the input itself. The loss function calculates the difference between the model's predicted token probabilities and the actual tokens in our 'gold' response. By backpropagating this error, the model adjusts its weights to increase the likelihood of generating tokens that match the desired output, effectively steering the model's probability distribution toward human-approved responses.

Explain the concept of Packing in SFT and why it is used for memory efficiency.

Packing is a technique used during SFT to handle sequences of varying lengths efficiently. Instead of having many short sequences padded with zeros—which wastes computation on processing irrelevant tokens—multiple training samples are concatenated into a single, long sequence separated by an end-of-sequence (EOS) token. This ensures that the GPU is constantly performing dense matrix multiplications on actual data rather than padding. By using packing, we maximize hardware utilization and ensure the model learns to identify individual dialogue turns within the context window, drastically reducing the total training time for large datasets.

All Generative AI interview questions →

Check yourself

1. What is the primary goal of Supervised Fine-tuning (SFT) in the context of Generative AI?

  • A.To increase the total number of parameters in the model
  • B.To teach the model how to follow specific user instructions
  • C.To pre-train the model on vast amounts of unlabeled text
  • D.To replace the model's underlying neural architecture
Show answer

B. To teach the model how to follow specific user instructions
SFT focuses on aligning a pre-trained base model to follow human-like instructions. Option 0 describes model scaling, Option 2 is the pre-training stage, and Option 3 describes architectural modification, which is not the goal of SFT.

2. Why is data quality more critical in SFT than in pre-training?

  • A.SFT datasets are much larger than pre-training datasets
  • B.Pre-training does not care about data quality
  • C.SFT sets the style and behavior, and poor data directly leads to poor behavioral outputs
  • D.SFT training requires significantly more computational power than pre-training
Show answer

C. SFT sets the style and behavior, and poor data directly leads to poor behavioral outputs
SFT acts as the behavioral tuning phase; if the examples provided are poor, the model will learn incorrect behaviors. Pre-training is much larger (Option 0 is false), both stages require high quality (Option 1 is false), and pre-training is far more compute-intensive (Option 3 is false).

3. If your model performs well on training data but fails on new, unseen tasks, what is the most likely cause?

  • A.The model is underfitting the training data
  • B.The model is overfitting, likely due to a narrow or redundant dataset
  • C.The learning rate was set too low to capture patterns
  • D.The dataset lacked enough negative examples
Show answer

B. The model is overfitting, likely due to a narrow or redundant dataset
High performance on training data but poor generalization is the definition of overfitting. Underfitting (Option 0) would show poor performance on training data too. Low learning rate (Option 2) would lead to slow convergence, and lack of negative examples (Option 3) relates more to safety alignment.

4. Which of the following describes the role of the validation set during the SFT process?

  • A.It is used to update the model weights based on the loss calculated
  • B.It provides a way to evaluate generalization performance without the model seeing the data
  • C.It serves as a secondary training set to increase volume
  • D.It acts as a substitute for human evaluation in the final deployment
Show answer

B. It provides a way to evaluate generalization performance without the model seeing the data
The validation set is used to monitor performance on unseen data to detect overfitting. Option 0 describes the training set. Option 2 defeats the purpose of validation. Option 3 is incorrect as human evaluation is usually a separate, final step.

5. When fine-tuning a model, what happens if the learning rate is set too high?

  • A.The model parameters remain static and do not change
  • B.The model takes too long to learn the training data
  • C.The model may overshoot the global minimum and diverge, leading to unstable performance
  • D.The model will exclusively learn the first example in the dataset
Show answer

C. The model may overshoot the global minimum and diverge, leading to unstable performance
A high learning rate causes large steps in parameter space, preventing convergence. Option 0 describes a zero learning rate. Option 1 describes a learning rate that is too low. Option 3 is a specific failure mode but not the general result of a high learning rate.

Take the full Generative AI quiz →

← PreviousWhen to Fine-tune vs RAG vs PromptNext →LoRA and QLoRA — Parameter Efficient Fine-tuning

Generative AI

41 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app