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.
Code Example
# 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
Is One Layer Enough? A Single Transformer Layer Matches Full-Parameter RL Train
LLM의 RL 후처리 학습(post-training)에서 성능 향상의 대부분이 중간 레이어 소수에 집중되며, 단 하나의 레이어만 학습해도 전체 파라미터 학습과 비슷하거나 더 나은 결과를 낼 수 있다는 연구 결과. 이는 RL 학습 비용을 대폭 줄일 수 있는 가능성을 시사한다.
Knowledge Distillation of Black-Box Large Language Models (2024)
GPT-4 같은 내부 구조에 접근할 수 없는 독점 LLM에서 작은 모델로 지식을 효과적으로 전달하는 Proxy-KD 기법을 소개하는 논문으로, 전통적인 White-Box 방식보다 성능이 높다는 점에서 주목할 만하다.
Show HN: NanoEuler – GPT-2 scale model in pure C/CUDA from scratch
PyTorch나 autograd 없이 C와 CUDA만으로 GPT-2 수준의 LLM을 처음부터 구현한 교육용 프로젝트로, 역전파·BPE 토크나이저·FlashAttention까지 직접 손으로 작성했다.
Show HN: Neural Particle Automata
고정된 격자 대신 움직이는 파티클 위에서 동작하는 Neural Cellular Automata의 확장 버전으로, 형태 생성·포인트 클라우드 분류·텍스처 합성 등 다양한 작업에서 자기조직화 동작을 학습할 수 있다.
The annotated PyTorch training loop
PyTorch 학습 루프의 각 코드 줄이 왜 그 위치에 있어야 하는지, 순서를 바꾸거나 빠뜨렸을 때 어떤 문제가 생기는지를 단계별로 설명한 심층 가이드다.
When Good Verifiers Go Bad: Self-Improving VLMs Can Regress on New Tasks
VLM 자가학습 루프에서 verifier가 특정 태스크에 맞지 않으면 학습할수록 오히려 성능이 떨어지는데, DPO 손실값은 멀쩡히 내려가서 눈치채기도 어렵다.