Skip to content

Commit 6c48949

Browse files
Randlora documentation and some example usage (#2524)
This is a follow up to #2464 and issue #2441. Entails documentation for RandLora and slightly updated example usage in the model.py docstring. Also adds RandLoRA to method comparison. --------- Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
1 parent 6c054d0 commit 6c48949

File tree

8 files changed

+8510
-3
lines changed

8 files changed

+8510
-3
lines changed

docs/source/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
title: Bone
125125
- local: package_reference/trainable_tokens
126126
title: Trainable Tokens
127+
- local: package_reference/randlora
128+
title: RandLora
127129

128130
title: Adapters
129131
- sections:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!--Copyright 2025 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
12+
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
13+
rendered properly in your Markdown viewer.
14+
15+
-->
16+
17+
# RandLora: Full-rank parameter-efficient fine-tuning of large models
18+
[RandLora](https://huggingface.co/papers/2502.00987) is a parameter-efficient fine-tuning technique that is similar to [LoRA](https://huggingface.co/papers/2106.09685) and [VeRA](https://huggingface.co/papers/2310.11454) but performs full rank updates to improve performance. RandLora can be particulary usefull when adapting large model to hard tasks that require complex updates while preserving the parameter efficiency of LoRA. The full rank update of RandLora is achieved by linearly scaling random bases. The random bases are a collection of multiple low rank matrices such that the summation of their ranks if greater or equal to the full rank of the parameter matrices. The trainable parameters of RandLora are two diagonal matrices (vectors) that get multiplied with the right hand low rank random bases, in a similar way to VeRA's update. To maintain low memory usage, RandLora uses a custom function that prevents storing unnecessary bases in memory for backpropagation.
19+
20+
RandLora presents the noteworthy difference that contrary to other LoRA-like PEFT algorithm, increasing RandLora's random base ranks increases the amount of trainable parameters. Because number of bases x bases rank is constant in RandLora, reducing the rank will increase the number of random bases, hence the number of base-specific trainable diagonal bases.
21+
22+
Because reducing the rank of RandLora's random bases will increase their number, RandLora can become slower to train than LoRA for very small ranks where typically, ranks below 4 with result in a large training time increase. This does not affect inference though as the RandLora adapters can be merged into the pretrained weight matrices.
23+
24+
RandLora additionally supports training with sparse, ternary random bases (only containing -1, 0 and 1). These bases are as described in [Bingham et al.](https://cs-people.bu.edu/evimaria/cs565/kdd-rp.pdf) and [Ping et al.](https://hastie.su.domains/Papers/Ping/KDD06_rp.pdf) and could theoretically be used to reduce compute needs by performing aggregations instead of matrix multiplications to create the weight update. This is not currently supported. Although it does not currently reduce compute, using sparse random bases in RandLora can reduce overfitting in some cases. For users intersted in using sparse ternary bases, the `sparse` option is recommended over the `very_sparse` one that can reduce perfromance.
25+
26+
Similarly to VeRA, when saving the RandLora's parameters, it's possible to eschew storing the low rank matrices by setting `save_projection=False` on the `VeraConfig`. In that case, these matrices will be restored based on the fixed random seed from the `projection_prng_key` argument. This cuts down on the size of the checkpoint, but we cannot guarantee reproducibility on all devices and for all future versions of PyTorch. If you want to ensure reproducibility, set `save_projection=True` (which is the default).
27+
28+
As in Vera and to handle different shapes of adapted layers, RandLora initializes shared A and B matrices with the largest required size for each dimension. During the forward pass, submatrices A and B for a given layer are sliced out from these shared matrices and used as described in the paper. For example, adapting two linear layers of shapes (100, 20) and (80, 50) will create A and B matrices of shapes (rank, 50) and (100, rank) respectively. Then, to adapt a layer of shape (100, 20), submatrices A and B of shapes (rank, 20) and (100, rank) will be extracted.
29+
30+
RandLora currently has the following constraint:
31+
32+
- Only `nn.Linear` layers are supported.
33+
34+
The abstract from the paper is:
35+
36+
> Low-Rank Adaptation (LoRA) and its variants have shown impressive results in reducing the number of trainable parameters and memory requirements of large transformer networks while maintaining fine-tuning performance. The low-rank nature of the weight update inherently limits the representation power of fine-tuned models, however, thus potentially compromising performance on complex tasks. This raises a critical question: when a performance gap between LoRA and standard fine-tuning is observed, is it due to the reduced number of trainable parameters or the rank deficiency?
37+
This paper aims to answer this question by introducing RandLora, a parameter-efficient method that performs full-rank updates using a learned linear combinations of low-rank, non-trainable random matrices. Our method limits the number of trainable parameters by restricting optimization to diagonal scaling matrices applied to the fixed random matrices. This allows us to effectively overcome the low-rank limitations while maintaining parameter and memory efficiency during training. Through extensive experimentation across vision, language, and vision-language benchmarks, we systematically evaluate the limitations of LoRA and existing random basis methods. Our findings reveal that full-rank updates are beneficial across vision and language tasks individually, and even more so for vision-language tasks, where RandLora significantly reduces---and sometimes eliminates---the performance gap between standard fine-tuning and LoRA, demonstrating its efficacy.
38+
39+
## RandLoraConfig
40+
41+
[[autodoc]] tuners.randlora.config.RandLoraConfig
42+
43+
## RandLoraModel
44+
45+
[[autodoc]] tuners.randlora.model.RandLoraModel
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# RandLora: Full-rank parameter-efficient fine-tuning of large models
2+
3+
## Introduction
4+
[RandLora](https://huggingface.co/papers/2502.00987) is a parameter-efficient fine-tuning technique that is similar to LoRA and VeRA but performs full rank updates to improve performance. RandLora can be particulary usefull when adapting large model to hard tasks that require complex updates while preserving the parameter efficiency of LoRA. The full rank update of RandLora is acheived by linearly scaling random bases. The random bases are a collection of multiple low rank matrices such that the summation of their ranks if greater or equal to the full rank of the parameter matrices. The trainable parameters of RandLora are two diagonal matrices (vectors) that get multiplied with the right hand low rank random bases, in a similar way to VeRA's update. To maintain low memory usage, RandLora uses a custom function that prevents storing unnecessary bases in memory for backpropagation.
5+
6+
## Quick start
7+
```python
8+
import torch
9+
from peft import RandLoraConfig, get_peft_model
10+
from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer
11+
from datasets import load_dataset
12+
13+
model = AutoModelForCausalLM.from_pretrained("huggyllama/llama-7b", device_map="cuda")
14+
tokenizer = AutoTokenizer.from_pretrained("huggyllama/llama-7b")
15+
dataset = load_dataset("timdettmers/openassistant-guanaco", split="train")
16+
randlora_config = RandLoraConfig()
17+
18+
peft_model = get_peft_model(model, lora_config)
19+
trainer = transformers.Trainer(
20+
model=peft_model,
21+
train_dataset=dataset,
22+
dataset_text_field="text",
23+
max_seq_length=2048,
24+
tokenizer=tokenizer,
25+
)
26+
trainer.train()
27+
peft_model.save_pretrained("randlora-llama-7b")
28+
```
29+
30+
There is no additional change needed to your standard PEFT training procedure, simply swap your `LoraConfig` for a `RandLoraConfig`. Note however that RandLora's trainable parameter count is **inversely proportional** to the rank parameter `r`. Lower `r` to increase and increase it to reduce trainable parameters of RandLora.
31+
32+
Run the finetuning script simply by running:
33+
```bash
34+
python examples/randlora_finetuning/randlora_finetuning.py --base_model meta-llama/Meta-Llama-3-8B --data_path timdettmers/openassistant-guanaco
35+
```
36+
This 👆🏻 by default will load the model in peft set up with RandLora config. Now if you wanna quickly compare it with Lora, all you need to do is to input ` --use_lora` in the command line and reduce `--randlora_alpha` to 2x the rank. So same above example would be 👇🏻;
37+
38+
```bash
39+
python examples/randlora_finetuning/randlora_finetuning.py --base_model meta-llama/Meta-Llama-3-8B --data_path timdettmers/openassistant-guanaco --use_lora --rank 32 --randlora_alpha 64
40+
```
41+
42+
RandLora can be made to use sparse or very sparse random bases. These sparse matrices can help reduce overfitting. Add `--very_sparse` to run with very sparse matrices or `--sparse` for sparse matrices:
43+
44+
```bash
45+
python examples/randlora_finetuning/randlora_finetuning.py --base_model meta-llama/Meta-Llama-3-8B --sparse
46+
```
47+
48+
RandLora also supports quantization. To use 4-bit quantization try:
49+
50+
```bash
51+
python examples/randlora_finetuning/randlora_finetuning.py --base_model meta-llama/Meta-Llama-3-8B --quantize
52+
```
53+
54+
By default the RandLora layers are the key and value layers of LLama model. Adding adapters on more layers will increase memory usage. If you wish to choose a different set of layers for RandLora to be applied on, you can simply define it using:
55+
```bash
56+
python examples/randlora_finetuning/randlora_finetuning.py --randlora_target_modules "q_proj,k_proj,v_proj"
57+
```
58+
59+
### Full example of the script
60+
```bash
61+
python randlora_finetuning.py \
62+
--base_model "PATH_TO_MODEL" \
63+
--data_path "PATH_TO_DATASET" \
64+
--output_dir "PATH_TO_OUTPUT_DIR" \
65+
--batch_size 1 \
66+
--num_epochs 3 \
67+
--learning_rate 3e-4 \
68+
--cutoff_len 512 \
69+
--val_set_size 500 \
70+
--quantize \
71+
--eval_step 10 \
72+
--save_step 100 \
73+
--device "cuda:0" \
74+
--rank 32 \
75+
--randlora_alpha 640 \
76+
--randlora_dropout 0.05 \
77+
--randlora_target_modules "k_proj,v_proj" \
78+
--hub_model_id "YOUR_HF_REPO" \
79+
--push_to_hub
80+
```
81+
82+
## RandLora vs. LoRA
83+
RandLora differs from LoRA and other related low rank approximation algorithms by chanllenging the low rank paradigm. RandLora adapters learn **full-rank** updates as the [paper](https://huggingface.co/papers/2502.00987) shows that the low rank constraint of LoRA can constrain performance gains as trainable parameters increase (with higher ranks). As a result, using RandLora is specifically recommended for difficult tasks that are underfit by LoRA. RandLoRA however also often improves performance for common tasks. If increasing LoRA's rank improves performance for your task, RandLora will most likely outperform.
84+
85+
RandLora is expected to increase performance over LoRA for equivalent amounts of trainable parameters, mostly for larger equivalent amounts (> LoRA rank 4).
86+
87+
RandLora's performance increase comes with two limitations:
88+
89+
1. Performance is dependent on using a large `randlora_alpha` scaling parameter (usually 20x the basis rank). This large parameter can sometimes make training the update unstable, reduce the learning rate or the scaling parameter if this is the case.
90+
91+
2. Increase training time over LoRA when using very low RandLora basis ranks.
92+
93+
## RandLora vs. VeRA
94+
RandLora shares similarities with VeRA in that both algorithms use random basis combinations to address some of LoRA's limitations. The limitations addressed by each algorithm is however different.
95+
VeRA aims to reduce trainable parameters beyond rank 1 LoRAs while RandLoRA reduces the performance limitation due to the low rank of the update as the trainable parameter count increases.
96+
97+
RandLora is expected to:
98+
99+
1. Improve performance over VeRA when more trainable parameters are required (hard tasks)
100+
101+
2. Reduce memory usage over VeRA thanks to RandLora's random base sharing strategy
102+
103+
104+
## Citation
105+
```
106+
@inproceedings{2025_ICLR_RandLoRA,
107+
title="{RandLoRA: Full rank parameter-efficient fine-tuning of large models}",
108+
author="Albert, Paul and Zhang, Frederic Z. and Saratchandran, Hemanth and Rodriguez-Opazo, Cristian and van den Hengel, Anton and Abbasnejad, Ehsan",
109+
booktitle="{International Conference on Learning Representations (ICLR)}",
110+
year="2025"
111+
}
112+
```

0 commit comments

Comments
 (0)