QLoRA (2023) › The Solution

Michael BrenndoerferAugust 12, 20254 min read
History of Language AIAll chapters
Part 8 of 9Open Models & Alignment
Chapter 91QLoRA (2023) › The Solution
Reading section 3 of 7 · The Solution · 4 minSection 3 of 7 · 4 min

Book contents

Section 3 of 7 · 4 min read

QLoRA (2023) › The SolutionOne section · Sections in this chapter3 / 7 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

QLoRA solved the memory and computational barriers by combining two complementary techniques: 4-bit quantization and Low-Rank Adaptation. Quantization reduced memory requirements by storing model weights at lower precision, while LoRA enabled parameter-efficient fine-tuning by only training small adapter matrices. The key innovation was demonstrating that these techniques could be combined effectively without sacrificing fine-tuning quality, enabling researchers to fine-tune quantized models and recover full-precision performance through careful adapter design.

4-Bit NormalFloat Quantization

QLoRA used a novel 4-bit quantization scheme called NormalFloat (NF4) that was specifically designed for normally distributed weights, which characterize neural network parameters. Most quantization schemes at the time used uniform quantization, which distributed quantization levels evenly across the value range. However, neural network weights typically follow approximately normal distributions, with most values clustered near zero and fewer values in the tails. NormalFloat quantization recognized this distribution and allocated more quantization levels to values near zero, where most weights were concentrated, and fewer levels to the tails of the distribution.

The NormalFloat scheme achieved this by quantizing weights into a set of levels that followed the expected normal distribution. By matching the quantization distribution to the actual weight distribution, NormalFloat could represent weights more accurately with the same number of bits compared to uniform quantization. This meant that 4-bit NormalFloat quantization could achieve better reconstruction quality than 4-bit uniform quantization for neural network weights. The scheme required carefully computing the quantization levels based on theoretical normal distribution properties, but this overhead was negligible compared to the memory savings and quality improvements.

Double Quantization

QLoRA further reduced memory usage through double quantization, which quantized both the model weights and the quantization constants. Quantization schemes typically require storing scaling factors or constants that define how to map between quantized values and full-precision values. These constants themselves consumed memory, particularly for large models. Double quantization applied quantization to these constants as well, further reducing memory overhead. This optimization seemed small, but it made meaningful differences at scale, especially when combined with other memory-saving techniques.

Low-Rank Adaptation (LoRA)

QLoRA combined quantization with Low-Rank Adaptation, which froze the quantized base model and only trained small adapter matrices. LoRA works by approximating weight updates as low-rank matrix products. Instead of directly updating weight matrices WW, LoRA learns two smaller matrices AA and BB such that the weight update ΔW\Delta W is approximated as BABA. If the original weight matrix has dimensions d×dd \times d, the low-rank factorization uses matrices of dimensions d×rd \times r and r×dr \times d, where rr is the rank of the decomposition, typically much smaller than dd.

During inference, QLoRA computed activations using the quantized base weights and the LoRA adapters: xout=(Wq+BA)xinx_{\text{out}} = (W_q + BA)x_{\text{in}}, where WqW_q represents the quantized base weights and BABA represents the learned adapter matrices. This approach meant that fine-tuning only required storing and updating the small adapter matrices, dramatically reducing the number of trainable parameters. For a 7-billion-parameter model, LoRA might only require training a few million adapter parameters, representing less than one percent of the original model size.

Paged Optimizers

QLoRA also incorporated paged optimizers to handle memory spikes during gradient checkpointing. Gradient checkpointing reduced memory usage during training by recomputing activations instead of storing them, but this created temporary memory spikes when optimizer states were updated. Paged optimizers used CPU memory as a buffer to temporarily store optimizer states during these spikes, preventing out-of-memory errors while maintaining training efficiency. This technique was particularly important for fine-tuning large models on GPUs with limited memory.

The combination of these techniques enabled QLoRA to fine-tune large models on consumer hardware. A 7-billion-parameter model that previously required over 50 gigabytes of memory for fine-tuning could now be fine-tuned on a single consumer GPU with 24 gigabytes of VRAM. The quantized base model weights consumed approximately 4 gigabytes of memory at 4-bit precision, and the LoRA adapter matrices added only a few hundred megabytes. This dramatic reduction in memory requirements opened up fine-tuning to researchers who previously could not afford the computational resources.