Tech / [18]

What Is LoRA and QLoRA? Let Me Explain, No Fluff

What is LoRA and QLoRA? Here's how to fine-tune an AI model by training under 1% of its parameters, even on a home GPU or a free Google Colab.

Hi friends! Every time I write here about an open model, like Xiaomi MiMo 2.6, the same question pops into my head: fine, the model is open… but can I teach it to do what I need without a data center at home? The short answer is yes, and the trick is called LoRA. Today I’ll explain what LoRA is, what QLoRA is, and why these two acronyms changed the game for anyone who wants to customize AI.

Hang on, I’ll explain it with a trip to the tailor.

1. The problem: fine-tuning a whole model costs a fortune

When we want to specialize a language model (to write in a brand’s voice, answer questions on a specific subject, or stick to a fixed format), the classic route is full fine-tuning. Every weight in the model gets updated.

The problem is the bill. A 7-billion-parameter model in 16 bits takes around 14 GB just to sit in memory. To train it, once you add gradients and optimizer states, you easily need more than 100 GB. And every tuned version is a full copy of the model: ten specializations, ten 14 GB files.

It’s like having a brand-new suit sewn from scratch every time the pants are a little too long. Does it work? Sure. But nobody does that!

2. What is LoRA: the tailor who just hems the pants

LoRA stands for Low-Rank Adaptation, proposed by Microsoft researchers in 2021 (here’s the paper). The idea is simple: the original model stays frozen, untouched, and we only train two tiny matrices that sit “next to” the chosen layers.

Back to the suit: the tailor doesn’t make a new one. He hems the pants, takes in the waist, and the original suit is still there. The adjustment lives apart from the suit.

The magic is a mathematical bet. When you fine-tune a model, the difference between the old and new weights tends to be “low rank” (that’s the name). In plain terms, that difference can be described with far fewer numbers than the full matrix. So instead of learning a giant matrix of changes, LoRA learns two thin matrices, A and B, whose product gives you that change.

For a sense of scale: in a 4,096 × 4,096 matrix, full fine-tuning touches 16.8 million numbers. With rank-8 LoRA, it’s only 65,536, about 0.4%. In the original paper, applied to the 175-billion-parameter GPT-3, LoRA cut trainable parameters by 10,000 times and GPU memory by 3 times.

Two more perks I really like. The training output is a small file (the “adapter”) of a few dozen MB, not a 14 GB copy, so one base model can have several adapters, one per task. And after training you can merge the adapter into the model, so responses are just as fast as the original’s.

3. What is QLoRA: the suit in a vacuum bag

LoRA solved training, but one problem remained: the base model, even frozen, still takes those 14 GB in 16 bits. For a lot of people, that no longer fits on their graphics card.

Enter QLoRA (Quantized LoRA), presented in 2023 by Tim Dettmers and colleagues at the University of Washington (here’s the paper). They fine-tuned a 65-billion-parameter model on a single 48 GB GPU. Seriously, stop and think about how big that is!

The trick is compressing the base model down to 4 bits while the adapters keep training at higher precision. It’s the suit stored in a vacuum bag: it takes a fraction of the closet space, and when you need it, you take it out, adjust it, and put it back.

Three ideas make it work without wrecking quality:

  1. NF4 (4-bit NormalFloat): a compression format designed around how neural network weights are distributed, so it loses less information than a plain 4-bit format.
  2. Double quantization: even the “rulers” used for compression get compressed, saving a few more GB on large models.
  3. Paged optimizers: when GPU memory spikes, some data moves temporarily to the computer’s RAM, avoiding that out-of-memory error halfway through training (it happens lol).

In practice, a 7B model in 4 bits takes around 4 GB, and with QLoRA you can fine-tune it on a 12 to 16 GB consumer card, or even on free Google Colab.

4. LoRA vs QLoRA: which one should you use?

Use QLoRA when memory is the bottleneck. A home graphics card, Colab, or the urge to play with a big model without renting an expensive server.

Use LoRA when you have memory to spare and want faster training. QLoRA pays a speed toll: at every step it has to decompress the weights to do the math.

Now, an honest warning: neither one is the right tool for cramming lots of new knowledge into a model. Fine-tuning is great for teaching style, format, and behavior. For facts that change all the time (prices, inventory, internal docs), the usual answer is RAG (in Portuguese), which looks up the information at question time.

5. How to start using LoRA in practice

If you code in Python, the most common path is Hugging Face’s PEFT library together with bitsandbytes for the 4-bit part. Unsloth and Axolotl package all of this with ready-made configs. A QLoRA skeleton looks roughly like this:

import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training

# Base model in 4 bits (NF4 + double quantization)
bnb = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
)
model = AutoModelForCausalLM.from_pretrained("model-name", quantization_config=bnb)
model = prepare_model_for_kbit_training(model)

# LoRA adapters
config = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05,
                    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
                    task_type="CAUSAL_LM")
model = get_peft_model(model, config)
model.print_trainable_parameters()  # shows the % that will be trained

For plain LoRA, just drop the BitsAndBytesConfig and load the model normally.

Three tips before you hit play:

  1. Start with rank (r) 8 or 16. Going much higher rarely pays off on a small dataset.
  2. Take care of your dataset. A few hundred well-made examples beat thousands of messy ones.
  3. Set aside examples for validation. With little data, the model memorizes instead of learning, and you only notice if you have something to compare against.

6. Frequently asked questions

Do LoRA and QLoRA work with any model?

They work with pretty much any open-weights model (Llama, Qwen, Mistral, MiMo and friends). With a closed model, only if the provider itself offers fine-tuning.

Does QLoRA make the model worse?

In the paper’s tests, quality came out very close to 16-bit fine-tuning. There’s a tiny loss, but for most uses you won’t even notice it.

Do I need a graphics card to use LoRA?

To train, you need a GPU (yours or in the cloud, like Colab). To run the tuned model, it depends on its size: a small quantized model runs even on a laptop.

What’s the difference between LoRA and fine-tuning?

LoRA is a type of fine-tuning. The difference is that instead of updating every weight, it trains only a small adapter and leaves the original model intact.

To fix the suit, call the tailor.

Cheers, Fellipe Soares

Leave a Reply

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