AI Hints
Home›Fine-Tuning LLMs Tutorial›Evaluation Loops, Judges and Forgetting
Fine-Tuning LLMs · Lesson 87 of 120

Catastrophic Forgetting

What else changed?.

⏱ 2 min read▶ Video at 2:12:27🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

When we teach a model a new task, does it forget what it knew before? Sometimes, yes. It's called catastrophic forgetting. Let's test our model.

  • New task learned
  • Old skills forgotten?
  • Catastrophic forgetting

What is it?

Catastrophic forgetting means that training on a new task damages old skills. It's worst with full fine-tuning, many epochs, and a high learning rate. LoRA usually forgets less, because the original weights never change.

keyvalue
Forgettingnew task hurts old skills
Worst withfull fine-tuning, many epochs
LoRAforgets less
Whyoriginal weights are frozen

Test general skills

We load our ticket model. Then we ask two general questions, with no system message. Disable adapter switches LoRA off for a moment, so we see the model before fine-tuning. Then we ask again, with LoRA switched on. Run it. The model still knows Paris, and can still write about rain. But notice the extra quotation marks. Our JSON training left a small habit.

app.pypython
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel

NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
tok = AutoTokenizer.from_pretrained(NAME)
model = PeftModel.from_pretrained(AutoModelForCausalLM.from_pretrained(NAME), "ticket-lora").eval()
def chat(question):
    ids = tok.apply_chat_template([{"role": "user", "content": question}],
                                  add_generation_prompt=True, return_dict=True, return_tensors="pt")
    out = model.generate(**ids, max_new_tokens=25, do_sample=False)
    return tok.decode(out[0][ids["input_ids"].shape[1]:], skip_special_tokens=True)

for q in ["What is the capital of France?", "Write a haiku about rain."]:
    with model.disable_adapter():
        print("before:", chat(q).replace("\n", " / ")[:75])
    print("after: ", chat(q).replace("\n", " / ")[:75])
Output
before: The capital of France is Paris.
after:  "Paris" is the capital of France.
before: In the dawn's early light, / Rain falls like a gentle breeze, / A soothing
after:  "Rain falls like a gentle breeze, / A soothing melody that whispers softly,
Also uses _setup.py
import shutil
shutil.copytree('/Users/hussain/Docker Complete Course/shared-data/ft_cache/ticket-lora', 'ticket-lora')

Protecting old skills

To protect old skills, use LoRA, with few epochs, and a modest learning rate. Mix in some general examples with your task data. And always test a few general questions after training, just like we did.

keyvalue
UseLoRA, few epochs
Learning ratemodest
Mix ingeneral examples
Testgeneral questions too

Summary

Let's recap. Fine-tuning can damage old skills. LoRA forgets much less. Mix in general data, and test general questions. Next, comparing training runs.

Key takeaways
  • Catastrophic forgetting
  • LoRA forgets less
  • Test general skills
Next up: Comparing Runs — which settings won?