designwithfocus

AI & ML development

What Is LoRA Fine-Tuning? Low-Rank Updates for a Model

By Design With Focus Team 5 min read
A large weight grid with two small matrices drawn beside it

Short answer

LoRA fine-tuning freezes a pretrained model and trains small low-rank matrices instead of every weight. The 2021 paper calls this Low-Rank Adaptation. It injects trainable rank-decomposition matrices into Transformer layers while the original weights stay fixed. PEFT describes the same idea: two update matrices adapt to new data, then combine with the frozen weights.

Last verified against the sources listed below.

Key takeaways

  • LoRA means Low-Rank Adaptation. The paper was submitted on 17 June 2021 and revised on 16 October 2021.
  • The original weights stay frozen. Trainable pieces are low-rank matrices added at layers, often attention blocks.
  • PEFT's rank setting, r, sets the size of those update matrices. A lower rank means fewer trainable parameters.
  • The abstract says LoRA adds no extra inference latency once adapter weights merge into the base model.
  • The PEFT docs label the stable line as v0.22.0 and say the main version installs from source.

What is LoRA fine tuning is a question about which weights move. The paper, “LoRA: Low-Rank Adaptation of Large Language Models,” was submitted on 17 June 2021 and revised on 16 October 2021. The abstract says full fine-tuning, which retrains all parameters, gets less practical as models grow.

LoRA freezes the pretrained weights and injects trainable rank-decomposition matrices into each Transformer layer. The authors say this greatly reduces the number of trainable parameters for a downstream task. Their reference package is for PyTorch models. PyTorch’s site says PyTorch is well supported on major cloud platforms.

How does low-rank adaptation work?

Two small matrices stand in for the weight update. Hugging Face PEFT calls them update matrices, produced by low-rank decomposition. You train those matrices on the new data. The original weight matrix stays frozen.

At the end, the original weights and the adapted weights are combined. PEFT says that during training the matrices, called A and B in its diagram, stay separate. After training they can be merged into one matrix that matches the separate computation. The merge is how you avoid loading two models at inference time.

PEFT also says LoRA can be combined with many other parameter-efficient methods. The bias setting is one of three strings: none, all, or lora_only. modules_to_save keeps extra pieces, such as a new head, in the checkpoint with the adapter. Those switches do not change the frozen base weights.

The paper’s abstract says that, unlike adapters, LoRA adds no extra inference latency. PEFT repeats that: adapter weights can be merged into the base model, so inference does not keep a separate adapter path. The docs page labels stable PEFT as v0.22.0.

What does the paper claim?

The abstract uses GPT-3 175B as the expensive case. Deploying a separate fully fine-tuned copy, each with 175B parameters, is described as prohibitively expensive.

The comment on the abstract page says draft version 2 adds better baselines, GLUE experiments, and more on adapter latency. The subjects are computation and language, artificial intelligence, and machine learning.

The authors say they release implementations and checkpoints for RoBERTa, DeBERTa, and GPT-2, plus a package that connects LoRA to PyTorch models. The abstract also says the work looks at rank-deficiency in language-model adaptation. It does not print a rank that every model should use. PEFT leaves that choice to r.

Compared with GPT-3 175B fine-tuned with Adam, the abstract says LoRA can reduce trainable parameters by 10,000 times and the GPU memory requirement by 3 times. It also says LoRA performs on par or better than fine-tuning on RoBERTa, DeBERTa, GPT-2, and GPT-3, with fewer trainable parameters and higher training throughput. Those are the paper’s claims. This page does not add a newer measurement.

The code release named in the abstract is the integration package for PyTorch. The microsoft/LoRA repository describes loralib as that implementation. huggingface/peft is the later library whose guide this article follows for the settings.

Which settings does PEFT expose?

PEFT exposes rank, alpha, target modules, and a bias choice. You load a base model, build a LoraConfig, wrap it with get_peft_model(), and train that PeftModel.

The docs say the main version of the guide is the source install, and that the latest stable label is v0.22.0.

Setting What the PEFT guide says
r Rank of the update matrices. Lower rank, fewer trainable parameters
lora_alpha Scaling factor. Default scale is alpha divided by r
use_rslora When true, scale is alpha divided by the square root of r
target_modules Where to attach the matrices, often attention blocks
bias none, all, or lora_only
modules_to_save Extra modules, such as a new head, saved with the adapter

PEFT says LoRA is often limited to attention blocks for simplicity, even though it can apply to any subset of dense layers. The number of trainable parameters depends on r and on the shape of the original matrix. Rank-stabilized scaling is the square-root form, not a different matrix layout.

When do you merge the weights?

Merge when you want one set of weights at inference. merge_and_unload() writes the adapter into the base model and gives you that standalone model.

Keep the adapter separate when you want several tasks on one frozen base. PEFT lists that as a reason to use LoRA: multiple lightweight adapters, one set of original weights. merge_adapter() merges while leaving the PeftModel in place so you can unmerge. unload() drops back to the pretrained base without merging, which the docs illustrate with a case of returning to the base model after trying LoRAs.

The guide also says the method is not only for language models. Any dense layer can take it, including diffusion models, and the page points at a Dreambooth fine-tune as the image example.

Documenting the r and alpha you chose is ordinary writing. The PX to REM converter is a size conversion for that write-up, not part of training.

How this sits next to other learning methods is covered in what machine learning is.

Frequently asked questions

What does the rank setting change?

PEFT's r is the rank of the update matrices, an integer. A lower rank means smaller matrices and fewer trainable parameters. lora_alpha is the scaling factor. The default scale is lora_alpha divided by r. If use_rslora is true, the scale is lora_alpha divided by the square root of r, which the docs attribute to rank-stabilized LoRA. target_modules chooses where the matrices attach. If layers_to_transform is omitted, every layer in target_modules is transformed.

How are the matrices initialized?

PEFT's default matches the reference code: Kaiming uniform for weight A, and zeros for weight B, so the adapter starts as an identity transform. A gaussian setting draws A from a Gaussian and still zeros B, which the docs say is how diffusers initializes LoRA. Setting the flag false skips that identity start and is marked for debugging. LoftQ is the option aimed at quantization error. The docs say not to quantize the base model first, then to set init_lora_weights to loftq.

What does merge_and_unload do?

PEFT says training keeps the small matrices separate, which can add latency if you load the base model and the adapter as two pieces at inference. merge_and_unload folds the adapter into the base weights so the result is one model. The docs say the merged matrix matches what the separate pieces computed. merge_adapter keeps the PeftModel so you can unmerge later. unload returns the base model without merging. delete_adapter removes one adapter. add_weighted_adapter combines several LoRAs with a weighting scheme you provide.

Where does the reference code live?

The paper's GitHub repository, microsoft/LoRA, describes loralib as an implementation of the paper. PEFT's repository describes the library as parameter-efficient fine-tuning, and the conceptual guide is the page that defines r, lora_alpha, and the merge helpers. The guide also says LoRA can attach to any subset of weight matrices, and that diffusion models can use it even though the paper focused on language models. A Dreambooth image example is linked from that guide. This article does not restate a speedup beyond the abstract's own claims.

Sources