Skip to content

What Is LoRA? Low-Rank Adaptation Explained (With QLoRA, VRAM Needs and Settings)

LoRA lets you fine-tune a huge AI model by training less than 1% of its parameters. A complete guide: what LoRA is, how the low-rank maths works, worked parameter counts, LoRA vs full fine-tuning vs QLoRA vs DoRA, VRAM requirements by model size, recommended hyperparameters, code with Hugging Face PEFT, tools, LoRA for Stable Diffusion and common mistakes.

6 min read

What Is LoRA? — what is lora

Short answer

What is LoRA? LoRA (Low-Rank Adaptation) is a technique for fine-tuning large AI models cheaply. Instead of updating all of a model's billions of weights, LoRA freezes the original model and trains two small matrices per layer whose product represents the change. Introduced by Microsoft researchers (Hu et al.) in 2021, it cut trainable parameters for GPT-3 175B by about 10,000× and GPU memory by about 3×, with quality comparable to full fine-tuning. Typical LoRA adapters are just megabytes to a few hundred megabytes, can be swapped like plugins, and add no inference latency once merged. Combined with 4-bit quantization (QLoRA), you can fine-tune an 8B model on a single consumer GPU with about 8–12 GB of VRAM.

Why LoRA exists

Fine-tuning adapts a pretrained model to a task or style. Full fine-tuning updates every weight, which is expensive:

  • Memory: with the Adam optimizer in mixed precision, full fine-tuning needs roughly 16 bytes per parameter (weights, gradients and optimizer states), so an 8B model needs over 120 GB before activations.
  • Storage: every fine-tuned version is a full copy of the model.
  • Compute: updating all weights is slow.

LoRA's insight: the *change* needed to adapt a model usually has a low "intrinsic rank," so it can be represented with far fewer numbers.

How LoRA works

For a weight matrix W (size d × k), LoRA learns an update ΔW = B·A, where:

  • A has size r × k
  • B has size d × r
  • r (the rank) is small, typically 8–64

The layer's output becomes:

h = W·x + (alpha / r) · B·A·x
  • W stays frozen.
  • B is initialised to zero, so training starts from the original model's behaviour.
  • alpha scales the update.
  • After training, B·A can be merged into W, so inference speed is unchanged.

Worked example: how many parameters?

For one attention projection with d = k = 4,096:

MethodTrainable parametersShare
Full fine-tuning4,096 × 4,096 = 16,777,216100%
LoRA r = 82 × 4,096 × 8 = 65,5360.39%
LoRA r = 162 × 4,096 × 16 = 131,0720.78%
LoRA r = 642 × 4,096 × 64 = 524,2883.1%

Applied across all attention and MLP layers of an 8B model, LoRA at r = 16 typically trains roughly 0.5% of parameters, tens of millions instead of billions.

LoRA vs other fine-tuning methods

MethodWhat is trainedMemoryQualityBest for
Full fine-tuningAll weightsVery highHighest ceilingLarge budgets, major domain shifts
LoRALow-rank adapters (16-bit base)ModerateClose to full fine-tuning for most tasksMost fine-tuning on capable GPUs
QLoRALoRA on a 4-bit quantized baseLowClose to LoRAConsumer GPUs, large models
DoRA (2024)Decomposes weights into magnitude and direction, LoRA on directionSlightly above LoRAOften slightly better than LoRAWhen you want extra quality at low rank
Prefix / prompt tuningSmall learned prompt vectorsVery lowLower on hard tasksLightweight task adaptation
Adapters (bottleneck layers)Small inserted layersLowGoodOlder PEFT approach; adds latency

Other variants include rsLoRA (better scaling at high ranks), LoRA+ (different learning rates for A and B), PiSSA (initialising from principal components) and VeRA (even fewer parameters).

QLoRA: fine-tuning big models on small GPUs

QLoRA (Dettmers et al., 2023) combines LoRA with a 4-bit quantized base model. Its key ideas:

  • NF4 (4-bit NormalFloat) data type designed for normally distributed weights.
  • Double quantization of the quantization constants to save more memory.
  • Paged optimizers to handle memory spikes.

The paper showed a 65B model could be fine-tuned on a single 48 GB GPU. See LLM quantization explained.

How much VRAM do you need?

Approximate requirements for fine-tuning with a short sequence length (around 2K tokens), batch size 1–2 and gradient checkpointing. Longer sequences and bigger batches need more.

Model sizeFull fine-tuning (16-bit + Adam)LoRA (16-bit base)QLoRA (4-bit base)Example GPUs for QLoRA
1–3B24–60 GB8–12 GB4–6 GBRTX 3060 12 GB
7–8B120+ GB18–24 GB8–12 GBRTX 4060 Ti 16 GB, RTX 3090
13–14B220+ GB32–40 GB12–18 GBRTX 4090 / 5090
30–32B500+ GB70–80 GB22–30 GBRTX 5090 32 GB, RTX PRO 6000
70B1+ TB150+ GB42–50 GB2 × 24 GB, RTX PRO 6000 96 GB, H100 80 GB

Use our VRAM calculator for inference sizing and see best GPU for deep learning. Renting is often easiest for one-off runs; see the GPU cloud price tracker.

SettingTypical starting valueNotes
Rank (r)16 (try 8–64)Higher rank = more capacity, more memory; complex tasks may need 64+
AlphaEqual to r or 2 × rControls update strength; changing r without alpha changes effective learning rate
Dropout0–0.1Small datasets benefit from some dropout
Target modulesAll linear layers (q, k, v, o, gate, up, down)Targeting all layers usually beats attention-only
Learning rate1e-4 to 2e-4Much higher than full fine-tuning (around 1e-5)
Epochs1–3More risks overfitting
Batch sizeEffective 16–64 via gradient accumulation
SchedulerCosine or linear with warmup

Example: LoRA with Hugging Face PEFT

from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model

model_id = "your-base-model"
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="bfloat16", device_map="auto")

config = LoraConfig(
    r=16,
    lora_alpha=32,
    lora_dropout=0.05,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
    task_type="CAUSAL_LM",
)
model = get_peft_model(model, config)
model.print_trainable_parameters()
# then train with the Trainer or TRL's SFTTrainer

For QLoRA, load the model with BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4") and call prepare_model_for_kbit_training before adding the adapter.

After training, merge for deployment:

model = model.merge_and_unload()
model.save_pretrained("merged-model")

Tools for LoRA fine-tuning

ToolBest for
Hugging Face PEFT + TRLStandard, flexible Python workflows
UnslothFast, memory-efficient fine-tuning on a single GPU
AxolotlConfig-driven training, multi-GPU
LLaMA-FactoryWeb UI and many models
torchtunePyTorch-native recipes
kohya_ss / AI ToolkitLoRAs for image models (Stable Diffusion, FLUX)

LoRA for image models

LoRA is hugely popular for Stable Diffusion and FLUX, where small adapters teach a model a new character, object or style from a few dozen images. Image LoRAs are often only 10–200 MB and can be combined with weighting. See What is a diffusion model? and best GPU for Stable Diffusion.

Serving many LoRAs efficiently

Because adapters are small, one base model can serve many customers or tasks. Inference engines such as vLLM and SGLang support multi-LoRA serving, loading different adapters per request. See LLM inference engines compared.

When LoRA is not enough

  • Teaching large amounts of new knowledge (LoRA is better at style, format and behaviour than memorising facts; consider RAG).
  • Major changes such as a new language or modality may benefit from higher ranks or full fine-tuning.
  • Very small datasets may overfit; quality data matters more than settings.

Common mistakes

1. Learning rate too low (copying full fine-tuning values). 2. Targeting only attention layers when all linear layers work better. 3. Wrong chat template during training versus inference. 4. Training too long and overfitting. 5. Poor data: a few hundred high-quality examples often beat thousands of noisy ones. 6. Forgetting to evaluate against the base model.

Frequently asked questions

What does LoRA stand for?

Low-Rank Adaptation, a parameter-efficient fine-tuning method introduced in the 2021 paper "LoRA: Low-Rank Adaptation of Large Language Models."

Is LoRA as good as full fine-tuning?

For most instruction tuning and style adaptation tasks, LoRA gets close. Full fine-tuning can still win when learning large amounts of new information.

What rank should I use for LoRA?

Start with r = 16 and alpha = 16–32. Increase rank for complex tasks or large datasets.

Can I fine-tune a 70B model with LoRA on one GPU?

With QLoRA, yes, on a GPU with roughly 48 GB or more (for example, an RTX PRO 6000 or H100), at short sequence lengths.

Does LoRA slow down inference?

Not once merged into the base weights. Unmerged adapters add a small overhead.

Sources and further reading

Think you understand it now? Prove it.

Solve real Tensor Core math in the TensorCore.si Challenge and earn a certificate with your name.

Take the challenge

Leave a Reply

Your email address will not be published. Required fields are marked *