Skip to content

AI & LoRA vs QLoRA: Fine-Tuning LLMs Guide

CoreConceptAugust 1, 20263 min read

Full parameter fine-tuning of Large Language Models (such as Llama 3 70B or Qwen 2.5) requires updating billions of weights, demanding massive GPU clusters with thousands of gigabytes of VRAM. Parameter-Efficient Fine-Tuning (PEFT) techniques eliminate this hardware barrier.

The two standard PEFT algorithms are LoRA (Low-Rank Adaptation) and QLoRA (Quantized Low-Rank Adaptation). This guide explains how low-rank matrix decomposition, 4-bit NormalFloat (NF4) quantization, and double quantization enable developers to fine-tune 70B parameter models on a single consumer GPU.

Three levers controlling three different things
Three levers controlling three different things

LoRA Mechanics: Low-Rank Matrix Decomposition

LoRA (Low-Rank Adaptation) freezes the original pre-trained model weight matrices $W_0 \in \mathbb{R}^{d \times k}$ and injects trainable rank-decomposition matrices $A \in \mathbb{R}^{r \times k}$ and $B \in \mathbb{R}^{d \times r}$, where rank $r \ll \min(d, k)$.

Instead of updating all 70 billion parameters during backpropagation, LoRA updates only the small rank matrices $A$ and $B$ (typically <0.5% of total model weights), drastically reducing GPU VRAM gradient memory storage.

Quick reference

  • LoRA freezes base weights $W_0$ and trains low-rank adapter matrices $A$ and $B$, reducing trainable parameters by >99%.
  • QLoRA quantizes base model weights to 4-bit NormalFloat (NF4) while maintaining 16-bit BrainFloating Point (BF16) compute precision.
  • Double Quantization in QLoRA quantizes the quantization constants themselves, saving an additional 0.37 bits per parameter.
LoRA Layer Injection in PyTorch (Hugging Face PEFT)
1from peft import LoraConfig, get_peft_model2from transformers import AutoModelForCausalLM3 4# Load 16-bit base model5model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B")6 7# Configure LoRA adapter rank and target modules8peft_config = LoraConfig(9    r=16,                       # Rank dimension10    lora_alpha=32,              # Scaling factor11    target_modules=["q_proj", "v_proj"], # Attention layers12    lora_dropout=0.05,13    bias="none",14    task_type="CAUSAL_LM"15)16 17lora_model = get_peft_model(model, peft_config)18lora_model.print_trainable_parameters()19# Output: trainable params: 4,194,304 || all params: 8,034,455,552 || trainable%: 0.052
QLoRA 4-Bit NormalFloat Quantization Setup
1import torch2from transformers import BitsAndBytesConfig, AutoModelForCausalLM3 4# QLoRA configuration: 4-bit NF4 quantization + double quantization5bnb_config = BitsAndBytesConfig(6    load_in_4bit=True,7    bnb_4bit_quant_type="nf4",8    bnb_4bit_use_double_quant=True,9    bnb_4bit_compute_dtype=torch.bfloat1610)11 12# Base model memory footprint reduced by 4x13model = AutoModelForCausalLM.from_pretrained(14    "meta-llama/Meta-Llama-3-70B",15    quantization_config=bnb_config,16    device_map="auto"17)

Remember this

LoRA reduces trainable parameter memory; QLoRA reduces base model VRAM by 4x through 4-bit NF4 quantization.

QLoRA Breakthrough: 4-Bit NF4 & Paged Optimizers

QLoRA extends LoRA by introducing 4-bit NormalFloat (NF4) quantization—an information-theoretically optimal data type for normally distributed neural network weights.

QLoRA also introduces Paged Optimizers, which leverage CUDA Unified Memory to automatically page optimizer state memory between GPU VRAM and CPU RAM during memory spikes.

Quick reference

  • NF4 quantization achieves equal empirical model performance to 16-bit FP16 models while using a fraction of the VRAM.
  • Paged Optimizers prevent Out-Of-Memory (OOM) errors during long-sequence gradient updates.
  • Compare with LLM Inference Engine Benchmarks and On-Prem vs Cloud Inference.

Remember this

QLoRA enables developers to fine-tune 70B parameter models on a single 48GB GPU (such as an NVIDIA A6000 or RTX 4090 cluster).

Fine-Tuning Strategy Decision Matrix

Selecting between Full Fine-Tuning, LoRA, and QLoRA depends on your hardware budget and target domain adaptation.

For related guides, see our articles on Fine-Tuning vs RAG vs Prompting and LLM Dataset Curation.

Quick reference

  • Choose QLoRA when fine-tuning 8B–70B models on limited consumer or single-node GPU hardware.
  • Choose LoRA when training on server-grade H100 GPUs where maximum training throughput is required.
  • Merge trained LoRA/QLoRA adapter weights back into base model checkpoints for zero-latency inference serving.

Remember this

Adopt QLoRA as your default fine-tuning approach for domain adaptation on accessible GPU hardware.

Key takeaway

LoRA low-rank adaptation and QLoRA 4-bit NF4 quantization democratize LLM fine-tuning, allowing developers to adapt 70B foundation models on accessible hardware.

Share:

Related Articles

This guide is for Python developers who can write functions and run pytest but have not structured an agent service. By

Read

As autonomous AI coding agents (such as Claude Code, Gemini CLI, and Cursor) take on complex software tasks, measuring t

Read

Large Language Model inference is notoriously memory-bandwidth bound. Generating tokens autoregressively requires loadin

Read

Explore this topic

Keep learning

Follow a structured path or browse all courses to go deeper.