Home Knowledge Base QLoRA (Quantized Low-Rank Adaptation)

QLoRA (Quantized Low-Rank Adaptation) is a parameter-efficient fine-tuning technique that combines 4-bit quantization of the base model with LoRA adapters trained in higher precision — enabling fine-tuning of 65B+ parameter models on a single consumer GPU (48GB VRAM) by reducing the base model's memory footprint by 75% (16-bit → 4-bit) while training only the small LoRA adapter weights in BFloat16, achieving performance that matches full 16-bit fine-tuning with no quality degradation.

What Is QLoRA?

Three Key Innovations

InnovationWhat It DoesMemory Savings
4-bit NormalFloat (NF4)A new data type optimized for normally-distributed neural network weights (which follow a Gaussian distribution)75% reduction vs FP16
Double QuantizationQuantize the quantization constants (the scaling factors) themselvesAdditional ~0.4 bits/param savings
Paged OptimizersUse CPU RAM to handle GPU memory spikes during gradient checkpointingPrevents OOM during training

Memory Comparison (65B Model)

MethodGPU Memory RequiredHardware NeededCost
Full Fine-Tuning (FP16)~780 GB10× A100 80GB~$300K hardware
LoRA Fine-Tuning (FP16)~160 GB2× A100 80GB~$60K hardware
QLoRA (4-bit base + BF16 adapters)~48 GB1× A100 80GB or 1× A6000 48GB~$15K hardware
QLoRA (4-bit) RTX 4090~33 GB (7B model)1× RTX 4090 24GB~$1,600 hardware

How QLoRA Works

StepProcessPrecision
1. Load base modelQuantize pretrained weights to NF44-bit
2. Attach LoRA adaptersAdd small rank-r matrices to attention layersBFloat16
3. Forward passDequantize 4-bit → compute → LoRA modifies outputMixed
4. Backward passCompute gradients through quantized modelBFloat16
5. UpdateOnly update LoRA adapter weights (frozen base)BFloat16
6. SaveSave only the small LoRA adapter file (~100MB)BFloat16

Implementation

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

# 4-bit quantization config
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",          # NormalFloat4
    bnb_4bit_compute_dtype="bfloat16",   # Compute in BF16
    bnb_4bit_use_double_quant=True       # Double quantization
)

# Load quantized model
model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-2-70b-hf",
    quantization_config=bnb_config
)

# Attach LoRA adapters
lora_config = LoraConfig(r=16, lora_alpha=32, target_modules=["q_proj", "v_proj"])
model = get_peft_model(model, lora_config)

QLoRA democratized LLM fine-tuning — proving that consumer-grade GPUs can customize the largest open-source language models with zero quality loss by combining 4-bit NormalFloat quantization, double quantization, and paged optimizers, reducing the hardware barrier from multi-GPU server clusters to a single GPU card.

qlorafine-tuning

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.