Sentence Transformers has been integrated with PEFT (Parameter-Efficient Fine-Tuning), allowing you to finetune multi-vector (late-interaction) models without fine-tuning all…
Training with PEFT Adapters
Sentence Transformers has been integrated with PEFT (Parameter-Efficient Fine-Tuning), allowing you to finetune multi-vector (late-interaction) models without fine-tuning all of the model parameters. Instead, with PEFT methods you are only finetuning a fraction of (extra) model parameters with only a minor hit in performance compared to full model finetuning.
Compatibility Methods
Adding a New Adapter
from peft import LoraConfig, TaskType
from sentence_transformers import MultiVectorEncoder
# 1. Load a model to finetune
model = MultiVectorEncoder("answerdotai/ModernBERT-base")
# 2. Create a LoRA adapter for the model & add it
peft_config = LoraConfig(
task_type=TaskType.FEATURE_EXTRACTION,
inference_mode=False,
target_modules=["Wqkv", "Wo", "Wi"], # ModernBERT attention and MLP linear layers
r=64,
lora_alpha=128,
lora_dropout=0.1,
)
model.add_adapter(peft_config)
# Proceed as usual... See https://sbert.net/docs/multi_vector_encoder/training_overview.html
Training Script
See the following example file for a full example of how PEFT can be used with Multi-Vector Encoder models:
- training_msmarco_lora.py: This is the same recipe as training_contrastive.py, finetuning answerdotai/ModernBERT-base on MS MARCO triplets with MultiVectorMultipleNegativesRankingLoss, but it has been adapted to use a LoRA adapter from PEFT. Only the LoRA matrices and the projection layer are trained (8.4% of the parameters), which considerably reduces the gradient and optimizer memory.
Source and license
Source: Sentence Transformers documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.