LoRA and QLoRA: Parameter-Efficient Fine-Tuning Explained with Memory and Parameter Calculations

LLM Development 60 min min read Updated: Nov 21, 2026 Advanced
LoRA and QLoRA: Parameter-Efficient Fine-Tuning Explained with Memory and Parameter Calculations
Advanced Topic 2 of 3

The Problem with Full Fine-Tuning

Training every weight of a large model needs huge memory for the weights, gradients and optimizer states. Many people cannot afford that hardware.

The LoRA Idea

LoRA (Low-Rank Adaptation) freezes the original weights. For a weight matrix of size d x k it adds two small matrices, A and B, with a small rank r. Only these are trained. The update is their product, which is added to the original output.

python def lora_params(d, k, r): return r * (d + k) d = k = 4096 full = d * k for r in [4, 8, 16, 64]: small = lora_params(d, k, r) print("rank", r, "-> trainable", small, "=", round(100 * small / full, 2), "% of full matrix")
Result

rank 4 -> trainable 32768 = 0.2 % of full matrix | rank 8 -> trainable 65536 = 0.39 % of full matrix | rank 16 -> trainable 131072 = 0.78 % of full matrix | rank 64 -> trainable 524288 = 3.12 % of full matrix

At rank 8 the adapter trains well under 1% of that matrix. The saved adapter file is small and can be swapped for different tasks on the same base model.

QLoRA

QLoRA goes further. It loads the frozen base model in 4-bit precision to save memory, then trains LoRA adapters in higher precision on top. The Hugging Face PEFT documentation shows this with the bitsandbytes library and the nf4 4-bit data type.

python def weights_gb(params_billion, bits): return params_billion * 1e9 * bits / 8 / 1e9 for bits in [16, 8, 4]: print(bits, "bit weights for a 7B model ~", round(weights_gb(7, bits), 1), "GB")
Result

16 bit weights for a 7B model ~ 14.0 GB | 8 bit weights for a 7B model ~ 7.0 GB | 4 bit weights for a 7B model ~ 3.5 GB

This counts only the weights. Training also needs memory for activations, adapters and optimizer states, so real usage is higher.

Typical Settings (Shape of the Code)

python from transformers import AutoModelForCausalLM, BitsAndBytesConfig from peft import LoraConfig, get_peft_model import torch bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16) model = AutoModelForCausalLM.from_pretrained("YOUR_BASE_MODEL", quantization_config=bnb) lora = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05, target_modules="all-linear", task_type="CAUSAL_LM") model = get_peft_model(model, lora) model.print_trainable_parameters()
Library versions Transformers v5 is a major release that focuses on PyTorch and removes some older TensorFlow and Flax support, and library options keep changing. Check the current PEFT and Transformers documentation and pin your package versions.

Key Settings

SettingMeaning
r (rank)Adapter size. Higher can learn more but costs more
lora_alphaScaling of the adapter's effect
target_modulesWhich layers get adapters
lora_dropoutHelps against overfitting
learning rate and epochsStart small, watch validation loss
Needs a GPU Running the training code needs a suitable GPU and the right libraries. This lesson's calculations run anywhere, but the code block is a template.

Practice

  1. Compute adapter size for d = 2048, k = 8192, r = 16
  2. Compare 4-bit and 16-bit weight size for a 13B model
  3. Write why you must keep a validation set

Conclusion

LoRA trains small adapters and QLoRA also compresses the base model, so large models can be tuned on modest hardware.

Quick Summary: LoRA math; QLoRA; settings; template code.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators