Preserving In-Context Learning ability in Large Language Model Fine-tuning
TL;DR Highlight
A study on how to preserve ICL (in-context learning — performing new tasks with just a few examples) ability that breaks during fine-tuning.
Who Should Read
Researchers and engineers who need to fine-tune LLMs for specific tasks while preserving their general few-shot learning capabilities.
Core Mechanics
- Fine-tuning on task-specific data often catastrophically degrades a model's in-context learning (ICL) ability — a form of catastrophic forgetting
- ICL capability is stored in a distributed way across attention layers — task-specific fine-tuning overwrites these distributed representations
- Key finding: fine-tuning with diversity in the fine-tuning data preserves ICL better than fine-tuning on homogeneous task-specific data
- LoRA fine-tuning preserves ICL significantly better than full fine-tuning because it limits the parameter update space
- Adding a small fraction (~10%) of general instruction-following examples to the task-specific fine-tuning data substantially preserves ICL ability
- The paper proposes an ICL preservation regularization term that can be added to any fine-tuning objective
Evidence
- Full fine-tuning on task-specific data: ICL performance dropped by 31% on average across held-out tasks
- LoRA fine-tuning: ICL performance dropped by only 9% — significantly better preservation
- Adding 10% general data to fine-tuning mix: ICL performance preserved within 5% of baseline while maintaining task-specific performance
How to Apply
- When fine-tuning: always use LoRA or PEFT methods rather than full fine-tuning if preserving ICL matters for your use case — the rank constraint acts as a natural regularizer.
- Mix in ~10% of diverse general instruction-following data (e.g., from FLAN or Alpaca datasets) into your task-specific fine-tuning data — this simple trick significantly preserves ICL.
- Evaluate ICL preservation explicitly in your fine-tuning pipeline: before and after fine-tuning, test the model on held-out few-shot tasks unrelated to your fine-tuning domain.
Try It
# Example of mixing ICL format samples into fine-tuning data (based on HuggingFace datasets)
from datasets import concatenate_datasets, load_dataset
# Target task data
task_dataset = load_dataset("your_task_dataset")
# Function to convert to ICL format
def to_icl_format(examples, num_shots=3):
"""
Convert to ICL format including few-shot examples
e.g., [Example1 Q&A] [Example2 Q&A] [Example3 Q&A] [Actual Question]
"""
icl_samples = []
data = examples # Modify to match actual data structure
for i in range(num_shots, len(data)):
shots = data[i-num_shots:i]
shot_text = "\n".join([f"Q: {s['input']}\nA: {s['output']}" for s in shots])
query = f"{shot_text}\nQ: {data[i]['input']}\nA:"
icl_samples.append({"text": query, "label": data[i]['output']})
return icl_samples
# Mix ICL format data at 20% ratio of the total dataset
# (Adjust to match data structure in actual implementation)
print("ICL data mixing ratio: 20% recommended")
print("LoRA rank: 8~16 (LoRA recommended over full fine-tuning)")
# LoRA configuration example
from peft import LoraConfig, get_peft_model
lora_config = LoraConfig(
r=16, # rank
lora_alpha=32,
target_modules=["q_proj", "v_proj"],
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM"
)
# model = get_peft_model(base_model, lora_config)Terminology
Related Papers
Quadrillion-parameter transformers and how to train them
On fixed data, much bigger models with scaled weight decay and ensembling keep loss falling as compute grows.
Astra can read and write weights to improve Qwen
An LLM edited Qwen3.5-0.8B's weights directly, lifting MATH-500 from 7.9% to 48.1%.
Show HN: Qwen3-4B with the AI writing tics trained out
Distilling Qwen3-4B with LoRA cut its AI-writing slop score from 121 to 12.5 per 1000 words
Dust: Pretraining Transformers Without Backpropagation
Adding per-token noise to activations lets transformers pretrain without backpropagation, rivaling backprop.
Show HN: TurboGPT: train 22KiB transformer in 13s
A CUDA C++ project trains a tiny byte-level GPT in under a minute.
I had Gemini train its own replacement for $9
Fine-tuning GLiNER on $9 of Gemini labels reached 0.83 F1 locally.
Learning to solve hard problems in RL for LLMs by never giving up
Cutting compute on easy problems and retrying hard ones in RL raised accuracy on the hardest problems
Training a 3.8B LLM to 0.384 CORE for $998
A 3.8B LLM trained on 8 B200s in 43 hours for $998 scored 0.384 on CORE.