Sub topic 11
Evaluating against the base model
This chapter assumes you have trained an adapter with SFTTrainer (previous chapter) and can run a Hugging Face causal language model for inference. You should also know what a held-out split is.
A falling training loss only shows that the adapter fits the training data. It does not show that the model got better at your task. To find out, compare the fine-tuned model with the base model on examples neither has seen.
Build a held-out set first
Split your data before training and never train on the held-out part. Keep it small but representative, for example 50 to 200 examples. Include the kinds of inputs you expect in production, not only easy ones.
Compare on identical conditions
The comparison is only fair if everything except the adapter is the same: prompts, chat template, generation settings, and scoring. Use greedy decoding (do_sample=False) so differences come from the model and not from sampling noise.
PEFT lets you run the same model object with the adapter on and off. The disable_adapter() context manager gives you base-model behavior without loading a second copy.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
base_id = "Qwen/Qwen2.5-0.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(base_id)
base = AutoModelForCausalLM.from_pretrained(base_id)
model = PeftModel.from_pretrained(base, "lora-out/adapter")
model.eval()
def generate(prompt: str) -> str:
messages = [{"role": "user", "content": prompt}]
inputs = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
)
with torch.no_grad():
out = model.generate(**inputs, max_new_tokens=64, do_sample=False)
new_tokens = out[0][inputs["input_ids"].shape[1]:]
return tokenizer.decode(new_tokens, skip_special_tokens=True)
prompt = "Summarize: LoRA trains small low-rank matrices."
tuned_answer = generate(prompt)
with model.disable_adapter():
base_answer = generate(prompt)
print("BASE :", base_answer)
print("TUNED:", tuned_answer)Note that add_generation_prompt=True appends the assistant header so the model starts a reply. If you leave it out, the model may continue the user turn instead.
Choose a metric that matches the task
| Task type | Useful measure | Caveat |
|---|---|---|
| Classification or extraction | Exact match, accuracy, F1 | Normalize whitespace and case before comparing |
| Structured output | Fraction that parses as valid JSON, schema checks | Valid does not mean correct |
| Free-form text | Human or model-based rating on a rubric | Judges have biases, so randomize answer order |
| Language modeling quality | Held-out loss or perplexity | Lower loss does not guarantee better answers |
Held-out loss is cheap to compute: call trainer.evaluate() with an eval_dataset. Use it as an early warning for overfitting. Training loss keeps falling while held-out loss turns upward.
Check for regressions
Fine-tuning can make the model worse at things you did not train on. Add a small set of general prompts, such as simple questions, instructions with constraints, and tasks in other formats. Run them on both models. If the tuned model loses ability here, you may be seeing forgetting (a later chapter covers fixes).
Also read outputs directly. Numbers hide problems like repeated phrases, a missing end-of-turn token, or answers that copy training examples word for word.
Common mistakes
- Evaluating on training examples, which rewards memorization.
- Using a different prompt format for the base model than for the tuned one.
- Sampling with temperature for one model and greedy decoding for the other.
- Judging from three or four examples. Small samples swing widely, so report the count alongside the score.
Exercise
Your tuned model scores 92% on the training prompts and 61% on held-out prompts, while the base model scores 58% on held-out prompts. What does this suggest, and what would you try?
Show answer
The adapter has overfit: it fits training data much better than unseen data, and the held-out gain over the base model is small (3 points). Try fewer epochs or a lower learning rate, add more varied training data, increase lora_dropout, or lower the rank. Re-evaluate on the held-out set after each change.
Exercise
Why use model.disable_adapter() for the baseline instead of loading a different checkpoint?
Show answer
It runs the exact same base weights, tokenizer, and generation code with only the adapter switched off, so any difference in output comes from the adapter.