Sub topic 9
Quantization basics for fine-tuning
This chapter assumes you know how a transformer stores its weights as matrices of floating-point numbers. It also assumes you can run a Hugging Face model for inference. It prepares you for QLoRA, which comes later in this tutorial.
What quantization does
Quantization stores numbers with fewer bits. A 16-bit weight takes 2 bytes. A 4-bit weight takes half a byte. Each saved bit shrinks the memory needed to hold the model.
A rough rule: memory for weights equals parameter count times bytes per parameter.
| Format | Bytes per weight | Weights of a 7B model |
|---|---|---|
| float32 | 4 | about 28 GB |
| bfloat16 | 2 | about 14 GB |
| int8 | 1 | about 7 GB |
| 4-bit | 0.5 | about 3.5 GB |
These figures cover weights only. Activations, gradients, optimizer state and the adapter add more during training.
How a weight is quantized
Quantization maps a range of real values onto a small set of levels. The basic recipe for int8 is:
- Take a block of weights.
- Find the largest absolute value in the block.
- Divide by it to get a scale, then round each weight to the nearest integer level.
- Keep the scale next to the integers so you can reverse the mapping.
Reversing the mapping is called dequantization. It gives back an approximation of the original weight. The difference between the two is the quantization error.
import torch
w = torch.tensor([0.12, -0.80, 0.33, 0.05])
scale = w.abs().max() / 127
q = torch.round(w / scale).to(torch.int8)
w_hat = q.float() * scale
print(q)
print(w_hat)
print((w - w_hat).abs().max())The error is small for int8. It grows as you use fewer bits, because there are fewer levels to choose from.
Block-wise scaling and outliers
One large outlier in a block forces a large scale, and then the small weights all round to the same level. To limit this, libraries quantize in small blocks, such as 64 weights, each with its own scale. This costs a little extra memory for the scales. It keeps the error low.
The 4-bit formats used for fine-tuning
The bitsandbytes library offers two 4-bit types.
- fp4 uses evenly designed floating-point levels.
- nf4 (NormalFloat 4) places its 16 levels where trained weights tend to fall. Trained weights are roughly bell-shaped, so nf4 usually loses less accuracy than fp4. QLoRA uses nf4.
Double quantization quantizes the block scales as well. This saves a further fraction of a bit per weight.
Storage type and compute type
This distinction matters in practice. Weights are stored in 4 bits, but the GPU does not multiply 4-bit numbers directly. For each layer, the library dequantizes the weights to a compute dtype such as bfloat16, runs the matrix multiplication, and discards the temporary copy. So there are two dtypes:
- the storage dtype (4-bit) sets how much memory the model holds.
- the compute dtype (for example bfloat16) sets the precision of the math.
Dequantizing costs time, so a 4-bit model is often somewhat slower per step than a bfloat16 one. You trade speed for memory.
Why quantization fits LoRA
Training a quantized weight directly is a problem. Rounded integers have no useful gradient, and small updates would vanish in the rounding. LoRA avoids this. The quantized base weights stay frozen, and only the small adapter matrices A and B train, in higher precision. Gradients flow through the dequantized values to the adapter. The base model contributes a small memory footprint, and the adapter holds all the learning.
Common mistakes
- Expecting the quantized model to match the original exactly. It is an approximation, so evaluate it.
- Forgetting that quantization with bitsandbytes needs a CUDA GPU.
- Counting only weights when planning memory. Activations and optimizer state still need room.
- Choosing float16 compute on hardware where bfloat16 is available. bfloat16 has a wider range and overflows less often.
Exercise
A 13B-parameter model is stored in bfloat16. Roughly how much memory do its weights need, and how much after 4-bit quantization? Why will real training use more than the second number?
Show answer
In bfloat16, 13B x 2 bytes is about 26 GB. In 4-bit, 13B x 0.5 bytes is about 6.5 GB. Training also needs memory for activations, the adapter's weights and gradients, optimizer state, and the quantization scales.