Sub topic 14
Common failure modes and fixes
This chapter assumes you have trained an adapter with SFTTrainer, as in the earlier chapters. It covers the problems that most often appear afterward, and how to tell them apart. Check the cheap causes first: data and template problems are far more common than bad hyperparameters.
A triage order
When a run goes wrong, work through the causes in this order.
- The chat template and data format.
- Overfitting or underfitting, read from the loss curves.
- Forgetting of general ability.
- Memory and precision errors.
- Adapter loading and merging mistakes.
Wrong chat template
Symptoms: the model ignores the instruction, repeats the prompt, or never stops generating. The usual cause is a mismatch between the format used in training and the format used at inference. Special tokens such as the end-of-turn marker must appear in both.
Fix it by always building prompts with the tokenizer's own template, rather than writing strings by hand.
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
messages = [{"role": "user", "content": "Summarize: LoRA trains small matrices."}]
prompt = tok.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
print(prompt)add_generation_prompt=True appends the opening of the assistant turn, so the model starts writing the answer. Leaving it out is a common cause of odd continuations.
Overfitting and underfitting
Overfitting means the model memorizes the training examples. Training loss keeps falling while loss on held-out data rises. Typical causes are a small dataset, too many epochs, or a rank that is too high for the task.
Underfitting means both losses stay high. Typical causes are a learning rate that is too low, too few target modules, or a rank that is too small.
| Observation | Likely cause | Fix |
|---|---|---|
| Train loss falls, eval loss rises | Overfitting | Fewer epochs, lower rank, more data, higher dropout |
| Both losses stay high | Underfitting | Higher learning rate, more target modules, higher rank |
| Loss becomes NaN | Learning rate too high or fp16 overflow | Lower the learning rate, use bf16 |
| Loss near zero from the start | Labels leak or data duplicated | Check for duplicate or trivial examples |
Forgetting
The base weights are frozen, but the adapter still changes the model's behavior. A narrow dataset can make the model worse at everything else. Test this by running a few general prompts on both the base model and the adapted model. Mitigate it with a lower learning rate, fewer steps, a smaller rank, or by mixing some general instruction data into the training set.
Memory and precision errors
Out-of-memory errors are the most common hardware failure. Apply these in order of how little they change the result:
- Lower
per_device_train_batch_sizeand raisegradient_accumulation_stepsto keep the same effective batch size. - Shorten the maximum sequence length.
- Enable gradient checkpointing.
- Switch to QLoRA from the earlier chapter.
Adapter loading and merging mistakes
- Loading an adapter onto a different base model than the one it was trained on gives garbage or a shape error. Record the base model ID with every adapter.
- Merging into a quantized base can lose precision. Merge into a base model loaded in 16-bit.
- A merged model saved without its tokenizer cannot be loaded on its own. Save the tokenizer alongside it.
Exercise
Training loss falls steadily for three epochs, but eval loss reaches its lowest value after epoch one and then climbs. Name the problem and give two fixes.
Show answer
The model is overfitting. Two fixes: train for one epoch, or lower the rank. Adding more data or raising dropout also helps.
Exercise
After training, the model answers well in your notebook but rambles and never stops in production. The notebook used applychattemplate, and production builds prompts by string concatenation. What is the likely cause?
Show answer
The production prompt does not match the training template. It probably lacks the special tokens and the generation prompt, so the model does not see the expected turn boundaries. Use applychattemplate with addgenerationprompt=True in production too.