Microsoft LoRA is an open-source Python library from Microsoft that implements "LoRA: Low-Rank Adaptation of Large Language Models" technology. This project provides a revolutionary solution for the efficient fine-tuning of large language models.
Project Address: https://github.com/microsoft/LoRA
Paper Address: https://arxiv.org/abs/2106.09685
LoRA (Low-Rank Adaptation) is a parameter-efficient fine-tuning method. Its core idea is:
LoRA demonstrates excellent performance in the GLUE benchmark:
Model | Number of Training Parameters | MNLI Accuracy | SST-2 Accuracy | MRPC Accuracy |
---|---|---|---|---|
RoBERTa Base Full Fine-tuning | 125M | 87.6 | 94.8 | 90.2 |
RoBERTa Base LoRA | 0.8M | 87.6±.1 | 95.1±.2 | 89.7±.7 |
DeBERTa XXL Full Fine-tuning | 1.5B | 91.1 | 96.8 | 92.9 |
DeBERTa XXL LoRA | 4.7M | 91.9±.1 | 96.9±.2 | 92.6±.6 |
LoRA also performs excellently on text generation tasks such as E2E, DART, and WebNLG:
Method | Training Parameters | E2E (BLEU) | DART (BLEU) | WebNLG (BLEU) |
---|---|---|---|---|
GPT-2 M Full Fine-tuning | 354.92M | 68.2 | 46.0 | 47.6 |
GPT-2 M LoRA | 0.35M | 70.4±.1 | 47.1±.2 | 55.3±.2 |
microsoft/LoRA/
├── loralib/ # Core library source code
├── examples/
│ ├── NLG/ # GPT-2 Natural Language Generation Example
│ └── NLU/ # RoBERTa/DeBERTa Natural Language Understanding Example
├── README.md
└── setup.py
pip install loralib
# Or install from source
pip install git+https://github.com/microsoft/LoRA
# ===== Before Modification =====
# layer = nn.Linear(in_features, out_features)
# ===== After Modification =====
import loralib as lora
layer = lora.Linear(in_features, out_features, r=16)
import loralib as lora
model = BigModel()
# Only set parameters containing "lora_" as trainable
lora.mark_only_lora_as_trainable(model)
# Training loop
for batch in dataloader:
# Normal training process
pass
# Save LoRA parameters
torch.save(lora.lora_state_dict(model), 'lora_checkpoint.pt')
# Load pre-trained model
model.load_state_dict(torch.load('pretrained.pt'), strict=False)
# Load LoRA parameters
model.load_state_dict(torch.load('lora_checkpoint.pt'), strict=False)
Currently, the LoRA library supports the following layer types:
nn.Linear
→ lora.Linear
nn.Embedding
→ lora.Embedding
nn.Conv2d
→ lora.Conv2d
lora.MergedLinear
(for merged linear layers such as QKV projections)# For scenarios such as QKV projections
qkv_proj = lora.MergedLinear(
d_model, 3*d_model,
r=8,
enable_lora=[True, False, True] # Apply LoRA only to Q and V
)
# Train LoRA-related biases
lora.mark_only_lora_as_trainable(model, bias='lora_only')
# Train all biases
lora.mark_only_lora_as_trainable(model, bias='all')
# Evaluation mode: merge weights, eliminate inference latency
model.eval()
# Training mode: restore separated state
model.train()
The Microsoft LoRA project provides a breakthrough solution for the efficient fine-tuning of large language models. Through clever low-rank adaptation technology, it maintains excellent model performance while significantly reducing computational and storage costs. This project not only has important academic value but also provides a feasible technical path for practical industrial applications, and is a milestone work in the field of parameter-efficient fine-tuning.