Sub topic 12
Language adapters and parameter-efficient adaptation
This chapter assumes you understand Transformer encoders, masked language-model pretraining, and basic Hugging Face fine-tuning. It also assumes you can read Python and interpret accuracy or F1 scores.
Multilingual models provide a shared starting point, but their behavior may still be weak for a particular language or domain. Full fine-tuning updates every model parameter. That can require large GPU memory and creates a separate model copy for each language and task. Parameter-efficient adaptation (PEFT) changes only a small part of the model while keeping the base model frozen.
Language adapters
A language adapter is a small trainable module inserted into a pretrained Transformer. It learns language-specific behavior without replacing the shared multilingual model. A typical adapter contains a down-projection, a nonlinearity, and an up-projection:
- The hidden vector is reduced to a smaller dimension.
- The reduced vector passes through a nonlinear layer.
- The result is expanded back to the original dimension.
- The adapter output is added to the original hidden state.
A language adapter can learn word-order patterns, morphology, or script-specific behavior. A task adapter can then learn the behavior needed for classification, retrieval, or another task.
Adapters make combinations possible. One shared base model can use a German adapter, a Swahili adapter, and a sentiment task adapter. You can switch combinations at inference time instead of storing a full model for every combination.
LoRA and other PEFT methods
Low-Rank Adaptation, usually called LoRA, does not add a complete replacement matrix to a model layer. Instead, it learns two small matrices whose product approximates an update to the original weight matrix:
W' = W + BAThe base matrix \(W\) stays frozen. The trainable matrices \(A\) and \(B\) have a much smaller rank. This greatly reduces trainable parameters and optimizer memory.
Other PEFT methods include:
- Prefix tuning, which learns virtual key and value vectors for attention.
- Prompt tuning, which learns trainable embeddings prepended to the input.
- Bias-only tuning, which updates only bias terms.
- Bottleneck adapters, which insert small neural modules into Transformer layers.
LoRA is often a practical default because it works well for classification and generation and has broad library support.
| Method | What changes | Main advantage |
|---|---|---|
| Full fine-tuning | Every model weight | Maximum flexibility |
| Language adapter | Small inserted modules | Switchable language components |
| LoRA | Low-rank updates to selected layers | Small checkpoints and low memory |
| Prompt tuning | Trainable input embeddings | Very few trainable parameters |
Applying LoRA to a multilingual classifier
The following example freezes xlm-roberta-base and adds LoRA updates to its attention query and value projections. The dummy batch makes the example runnable without downloading a dataset.
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from peft import LoraConfig, TaskType, get_peft_model
name = "xlm-roberta-base"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(name, num_labels=3)
config = LoraConfig(
task_type=TaskType.SEQ_CLS,
r=8,
lora_alpha=16,
lora_dropout=0.1,
target_modules=["query", "value"],
)
model = get_peft_model(model, config)
model.print_trainable_parameters()
batch = tokenizer(
["This film was excellent.", "The service was poor."],
padding=True,
return_tensors="pt",
)
labels = torch.tensor([2, 0])
output = model(**batch, labels=labels)
output.loss.backward()
print("loss:", float(output.loss))For a language-specific adapter, train the adapter on target-language text or labeled examples while freezing the base model. For a task adapter, train on task labels. If both are used, keep their training data and evaluation roles separate so that language adaptation does not accidentally memorize the task test set.
Choosing an adaptation strategy
Use full fine-tuning when you have enough data and need the highest possible task specialization. Use LoRA when you need several inexpensive task variants or limited GPU memory. Use language adapters when the same multilingual base model must support many languages independently. Combine a language adapter with a task adapter when language variation and task variation should remain modular.
Measure each language separately. A strong average score can hide a large low-resource language gap.
Exercise
Why might a language adapter be preferable to a separate fully fine-tuned model for every language?
Show answer
It stores only a small language-specific module while sharing the expensive multilingual base model, making language switching and deployment cheaper.