Adapter
A small trainable module inserted between model layers
A small trainable module inserted between model layers
An Adapter is a small trainable module inserted between Transformer layers to efficiently adapt a large pre-trained model to a specific task. Because the parameters of the original model are frozen and only the Adapter is trained, it achieves comparable performance while updating only 0.5–4% of the parameters compared with full fine-tuning.
It was first proposed in the 2019 Google Research paper "Parameter-Efficient Transfer Learning for NLP". It has since developed into a core technique of PEFT (Parameter-Efficient Fine-Tuning) and became the basis for various variant techniques such as LoRA and Prefix Tuning.
The structure of an Adapter is a bottleneck consisting of a down-projection, a nonlinear activation, and an up-projection. For example, a 768-dimensional input is reduced to 64 dimensions and then restored to 768 dimensions. This bottleneck dimension determines the capacity of the Adapter, and generally 1–10% of the original dimension is used.
In multilingual/multi-domain services, multiple Adapters can be swapped per task on top of a single base model. If you prepare domain-specific Adapters for fields such as medicine, law, and finance, you can load only the required Adapter at inference time and switch quickly, which is effective for building services that cover a variety of specialized fields.
from transformers import AutoModelForSequenceClassification
from peft import get_peft_model, LoraConfig, TaskType
# 기본 모델 로드
model_name = "bert-base-uncased"
model = AutoModelForSequenceClassification.from_pretrained(
model_name, num_labels=2
)
# LoRA (Low-Rank Adaptation) 설정 - Adapter의 인기 변형
lora_config = LoraConfig(
task_type=TaskType.SEQ_CLS,
r=8, # 보틀넥 차원 (rank)
lora_alpha=32, # 스케일링 팩터
lora_dropout=0.1, # 드롭아웃
target_modules=["query", "value"] # 적용할 레이어
)
# Adapter가 적용된 PEFT 모델 생성
peft_model = get_peft_model(model, lora_config)
# 학습 가능한 파라미터 확인
trainable_params = sum(p.numel() for p in peft_model.parameters() if p.requires_grad)
total_params = sum(p.numel() for p in peft_model.parameters())
print(f"학습 파라미터: {trainable_params:,} / {total_params:,}")
print(f"학습 비율: {100 * trainable_params / total_params:.2f}%")
# 학습 후 Adapter만 저장
peft_model.save_pretrained("./my_adapter")
# 다른 태스크용 Adapter 로드하여 교체
from peft import PeftModel
base_model = AutoModelForSequenceClassification.from_pretrained(model_name)
medical_model = PeftModel.from_pretrained(base_model, "./medical_adapter")
legal_model = PeftModel.from_pretrained(base_model, "./legal_adapter")
"If we do full fine-tuning for each client, the model storage cost is too high. With the Adapter approach, we can store just a separate Adapter per client alongside a single base model, so we can cut storage space by more than 90%."
"Please explain the difference between Adapter and LoRA. Adapter adds a new module between layers, while LoRA adds a low-rank matrix to the existing weights. LoRA is more widely used because it can be merged without overhead at inference time."
"Full fine-tuning of a 7B model requires four A100 80GB GPUs, but with LoRA it's possible with a single 24GB GPU. Training time also drops to one-third."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
If the rank is too small, expressiveness is insufficient, and if it is too large, efficiency drops. Generally, start with r=4~16 and adjust according to task complexity. Complex tasks may require up to r=64.
Applying Adapters to all layers increases the number of parameters. Applying them only to Attention's Query/Value is mostly sufficient, and depending on the task, consider additionally including FFN layers.
An Adapter is tied to a specific base model. If the base model is updated, existing Adapters may stop working, so record the base model version together.