What QLoRA is
LoRA trains only small adapters, but the frozen base still has to sit in memory at 16 or 32 bits per weight for the forward and backward passes. QLoRA stores the frozen base in 4 bits instead and trains LoRA adapters on top in higher precision. In each forward pass a layer dequantizes its 4-bit weights to the compute precision, multiplies, and adds the adapter's output; gradients flow through the dequantized weights into and , while the 4-bit codes never change. Dettmers et al. introduced it with three pieces: the 4-bit NormalFloat data type, double quantization of the scaling constants, and paged optimizers that move optimizer state to CPU memory during spikes. They fine-tuned a 65 B model on a single 48 GB GPU while matching 16-bit fine-tuning on their benchmarks.
This page quantizes SmolLM2-135M-Instruct with bitsandbytes (NF4, blocks of 64, double quantization, float32 compute) and repeats the LoRA tab's run on it: the same eight text-to-SQL rows, rank 8 on q_proj and v_proj, the same initial . Paged optimizers matter only on a GPU and are not shown.
4-bit NormalFloat
Four bits give 16 values. NF4 spends them where trained weights are: weights are roughly normally distributed, so its levels are quantiles of a standard normal, rescaled to . Each level then covers about the same share of the weights, the property that makes a quantile code optimal for normal data. The levels are built from two half-codes so that 0 is exactly representable: 7 negative, 0, and 8 positive, from −1 through −0.696, …, 0.080, … to 0.723 and 1.
To use them, the weights are cut into blocks of 64 consecutive values. Each block is divided by its largest magnitude, its absmax , and every weight is stored as the index of the nearest level:
The 4-bit NormalFloat panel shows the histogram of for every weight of layer 15's q_proj with the 16 levels over it, and how often each code is used. One block of 64 weights follows a single block: each weight, the level it snaps to, and the codes that are stored, two per byte. Small blocks keep one outlier from stretching the scale of many weights.
What 4 bits cost
Rounding to 16 levels is lossy. What 4 bits cost shows a corner of , of and of the error magnified ten times. Over all 210 linear matrices the relative error lies between 9.2 % and 10.2 %, as the Every quantized matrix grid shows. The model still works: before any training, the held-out answer's loss goes from 2.411 in float32 to 2.430 in NF4, although the largest single logit moves by 15.5.
Double quantization · memory
Each block of 64 weights needs its float32 absmax, which adds bits per weight. Double quantization quantizes those constants too. Layer 15's q_proj has 5,184 blocks and so 5,184 constants: their mean (0.503) is subtracted, and they are stored as 8-bit codes in blocks of 256, each block with one float32 scale (21 scales here). Restored, the constants differ from the originals by at most 0.013. The cost per weight drops from
The model in bytes sums up the saved state: 513.1 MiB in float32, 256.6 MiB in bfloat16, 165.1 MiB in NF4 and 160.6 MiB with double quantization. Only the 106,168,320 weights of the 30 × 7 linear layers go to 4 bits. The 28,311,552-weight embedding, which is also the LM head, stays in float32 at 108.0 MiB, so in a model this small the whole shrinks much less than its linear layers do.
Training on a 4-bit base
LoRA vs QLoRA · 40 steps, same A at the start trains both runs with AdamW at a learning rate of on the same 460,800 adapter weights; the LoRA run repeats the LoRA tab's exactly. QLoRA's batch loss starts a little higher (1.518 against 1.459, the cost of NF4) and both fall to about 0.002. On the held-out row both memorise: LoRA's loss is lowest at step 8 (1.107) and QLoRA's at step 6 (1.589), and they end at 2.471 and 2.770. Starting from a slightly different model, the adapters learn something slightly different, and on eight rows the difference shows. Greedy replies after step … puts the two runs' SQL beside the dataset's.
Merging into 4 bits
A LoRA adapter can be merged into its weight, . On a 4-bit base, peft's merge dequantizes each weight, adds the adapter and rounds the sum back to the 16 levels. A change smaller than half the gap between two levels rounds away. Merging into 4 bits shows the result: the NF4 base has a held-out loss of 2.430 before training, QLoRA reaches 2.770 with its adapters kept apart, and the merged, re-quantized model has 2.396, near the untrained base rather than the trained model (its logits differ from the trained model's by up to 31.4). Keeping the adapters separate, or merging them into a 16-bit copy of the base, avoids this.
Where QLoRA came from
QLoRA builds on Dettmers and colleagues' earlier work in the bitsandbytes library: block-wise quantization with a per-block absmax, introduced for 8-bit optimizer states, and LLM.int8(), 8-bit inference that keeps outlier features in 16 bits. Their study of k-bit inference found 4-bit precision almost always the best trade-off between total model bits and zero-shot accuracy. QLoRA brought 4-bit storage to fine-tuning, and 4-bit LoRA training became a common way to adapt large models on one GPU. The GPTQ, AWQ and GGUF tabs of the Efficiency Explainer quantize models after training, for inference rather than fine-tuning.
Papers
- QLoRA: Efficient Finetuning of Quantized LLMs Introduces QLoRA: LoRA on a frozen 4-bit base, with the NF4 data type, double quantization and paged optimizers.
- LoRA: Low-Rank Adaptation of Large Language Models The low-rank adapters QLoRA trains on top of the quantized base (see the LoRA tab).
- 8-bit Optimizers via Block-wise Quantization Block-wise quantization with a per-block absmax, the scheme NF4 blocks of 64 follow.
- LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale 8-bit inference for large transformers in bitsandbytes, the library this page quantizes with.
- The case for 4-bit precision: k-bit Inference Scaling Laws Compares bit widths, data types and block sizes across model scales and finds 4 bits almost always the best trade-off.
- SmolLM2: When Smol Goes Big -- Data-Centric Training of a Small Language Model SmolLM2-135M-Instruct, the model this page quantizes and adapts.