4103: Double Quantization - BitsAndBytes (bnb) Logic¶
Difficulty: โญโญโญ Advanced
Estimated Time: 4 hours
Table of Contents¶
- Learning Objectives
- Abstract
- The Problem with Standard Quantization
- Double Quantization Solution
- BitsAndBytes Implementation
- Memory Savings
- Quality Impact
- Advanced DQ Techniques
- BitsAndBytes Training
- Troubleshooting
- Summary
- References
Learning Objectives¶
After completing this lesson, you will be able to:
- Compute the standard 4-bit metadata overhead from the group formulas โ weights 0.5mn, fp16 scales 2mn/g, int8 zero-points mn/g bytes โ landing at 8 MB + 256 KB + 128 KB โ 8.4 MB (~5%) for a 4096ร4096 matrix at g=128
- Derive the scales-of-scales scheme โ scales re-quantized to 8-bit
round(s/c) + 128against one per-tensor fp16 constantc = max(|scales|)/127โ by walking thedouble_quantize/double_dequantizeround trip and its ~50% scale-storage saving - Configure
BitsAndBytesConfig(load_in_4bit,bnb_4bit_quant_type="nf4",bnb_4bit_use_double_quant=True,bnb_4bit_compute_dtype=torch.float16) and read the 16 NF4 levels as the normal-distribution-matched codebook - Quantify DQ's footprint from the model tables โ Llama 2 7B scales 0.1 โ 0.05 GB plus ~0.001 GB meta (3.6 โ 3.55 GB, ~0.5 GB at 70B) โ against its <0.5% quality cost (WikiText-2: 7B 5.65 โ 5.67, 70B 3.78 โ 3.79)
- Decide when DQ pays off (VRAM-constrained, 70B+, inference) versus skipping it (max quality, ample VRAM, 7B/13B), and reject triple quantization for <0.01 GB more savings at compounding quality loss
- Wire DQ into QLoRA โ
prepare_model_for_kbit_trainingplus LoraConfig (r=16, alpha=32, q_proj/v_proj) training ~1% of parameters โ and route the OOM / NaN-loss / slow-inference troubleshooting paths
Abstract¶
Double Quantization (DQ) is a technique introduced by bitsandbytes to further compress quantization parameters. It quantizes the quantization scales themselves, providing additional memory savings with minimal quality loss.
The Problem with Standard Quantization¶
Standard 4-bit Quantization Memory¶
For a weight matrix W โ โ^(mรn):
Standard 4-bit:
- Weights: m ร n ร 0.5 bytes = 0.5mn bytes
- Scales: m ร n/g ร 2 bytes (fp16) = 2mn/g bytes
- Zero points: m ร n/g ร 1 byte = mn/g bytes
Total: 0.5mn + 2mn/g + mn/g bytes
For g=128 (group size), m=4096, n=4096:
- Weights: 0.5 ร 16M = 8 MB
- Scales: 2 ร 16M / 128 = 256 KB
- Zero points: 16M / 128 = 128 KB
- Total: ~8.4 MB
The scales and zero points add overhead!
Visualizing Memory Layout¶
Weight Matrix (4096 ร 4096):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Weights (4-bit): 8 MB โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Scales (fp16): 256 KB โ โ Overhead
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Zero points (8-bit): 128 KB โ โ Overhead
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Total: 8.4 MB (~5% overhead for metadata)
Double Quantization Solution¶
What is Double Quantized?¶
Key idea: The scales themselves can be quantized!
Standard:
W โ Q โ (4-bit weights, fp16 scales)
Double Quantization:
W โ Q โ (4-bit weights, 8-bit scales, 8-bit scale scales)
Meta-meta: scales of scales!
Double Quantization Formula¶
Let c be the quantization constant for scales:
c = max(|scales|) / 127
For each scale s:
s_quantized = round(s / c)
s_dequantized = s_quantized ร c
Memory saved:
- fp16 scales: 2 bytes per group
- 8-bit quantized scales: 1 byte per group
- Plus: one 8-bit constant c per tensor
Savings: ~50% on scale storage
DQ Implementation¶
import torch
def double_quantize(weights, group_size=128):
"""
Double quantization: quantize weights and scales
"""
m, n = weights.shape
# Reshape into groups
weights_reshaped = weights.reshape(-1, group_size)
# First quantization: weights to 4-bit
scales_fp16 = weights_reshaped.abs().max(dim=-1).values / 8.0
zeros = torch.zeros_like(scales_fp16)
# Quantize weights
weights_q = torch.clamp(
torch.round(weights_reshaped / scales_fp16[:, None]) + 128,
0, 255
).to(torch.uint8)
# Second quantization: scales to 8-bit
# Compute scale for scales
scale_scale = scales_fp16.abs().max() / 127.0
scale_zero = 128
# Quantize scales
scales_q = torch.clamp(
torch.round(scales_fp16 / scale_scale) + scale_zero,
0, 255
).to(torch.uint8)
return {
'weights_q': weights_q,
'scales_q': scales_q, # Now 8-bit!
'scale_scale': scale_scale.to(torch.float16),
'scale_zero': scale_zero,
}
def double_dequantize(weights_q, scales_q, scale_scale, scale_zero, group_size=128):
"""
Dequantize double-quantized weights
"""
# Dequantize scales
scales_fp16 = (scales_q.float() - scale_zero) * scale_scale.float()
# Dequantize weights
weights = (weights_q.float() - 128) * scales_fp16[:, None]
return weights.reshape(-1)
BitsAndBytes Implementation¶
NF4 (4-bit NormalFloat)¶
# BitsAndBytes NF4 quantization
import torch
from bitsandbytes.nn import Linear4bit
# Create a quantized layer
layer = Linear4bit(
in_features=4096,
out_features=4096,
compute_dtype=torch.float16,
quant_type='nf4', # or 'fp4'
)
# NF4 properties:
# - NormalFloat distribution
# - Optimized for normally distributed weights
# - Better than uniform quantization
NF4 Distribution¶
def pack_4bit(quantized_values, levels):
"""Pack nearest-level indices two-per-byte into uint8."""
idx = torch.searchsorted(levels, quantized_values)
idx = torch.clamp(idx, 0, len(levels) - 1).to(torch.uint8)
low, high = idx[0::2], idx[1::2]
return low | (high << 4)
def nf4_quantize(weights):
"""
NF4: Quantized weights follow normal distribution
"""
# Normalize weights to [-1, 1]
max_val = weights.abs().max()
weights_norm = weights / max_val
# NF4 quantization levels (the real bitsandbytes constants)
# 16 levels for 4 bits, matched to the normal distribution
nf4_levels = torch.tensor([
-1.0, -0.6962, -0.5251, -0.3949, -0.2844, -0.1848,
-0.0911, 0.0, 0.0796, 0.1609, 0.2461, 0.3379,
0.4407, 0.5626, 0.7229, 1.0
])
# Find closest level
# Clamp: |w| = 1.0 would return index 16, one past the last level
indices = torch.searchsorted(nf4_levels, weights_norm)
indices = torch.clamp(indices, 0, len(nf4_levels) - 1)
quantized = nf4_levels[indices]
# Pack into 4-bit (2 values per byte)
packed = pack_4bit(quantized, nf4_levels)
return packed
BNB Configuration¶
# bitsandbytes quantization config
from transformers import BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
# 4-bit quantization
load_in_4bit=True,
# Quantization type
bnb_4bit_quant_type="nf4", # or "fp4"
# Double quantization
bnb_4bit_use_double_quant=True,
# Compute dtype
bnb_4bit_compute_dtype=torch.float16,
# Storage dtype for the packed 4-bit values
bnb_4bit_quant_storage=torch.uint8,
)
# Load model with BNB quantization
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
quantization_config=bnb_config,
device_map="auto",
)
Memory Savings¶
Standard vs Double Quantization¶
Llama-2-7B (4096 ร 4096 weights, 32 layers):
Standard 4-bit (no DQ):
- Weights: 7B ร 0.5 bytes = ~3.5 GB
- Scales: ~0.1 GB
- Total: ~3.6 GB
Double Quantization:
- Weights: 7B ร 0.5 bytes = ~3.5 GB
- Quantized scales: ~0.05 GB (50% savings!)
- Meta scales: ~0.001 GB (negligible)
- Total: ~3.55 GB
Savings: ~0.05 GB per model
For 70B model: ~0.5 GB savings!
Visual Comparison¶
Without DQ:
Weights โโโโโโโโโโโโโโโโโโโโ 3.5 GB
Scales โโโ 0.1 GB
โโโโโโโโโโโโโโโโโโโโโ
Total: 3.6 GB
With DQ:
Weights โโโโโโโโโโโโโโโโโโโโ 3.5 GB
Scales โโ 0.05 GB
Meta 0.001 GB
โโโโโโโโโโโโโโโโโโโโโ
Total: 3.55 GB
Quality Impact¶
Perplexity Comparison¶
WikiText-2 Perplexity (lower is better):
Model fp16 4-bit 4-bit+DQ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Llama-2-7B 5.45 5.65 5.67
Llama-2-13B 4.92 5.08 5.10
Llama-2-70B 3.65 3.78 3.79
Quality loss from DQ: <0.5% (negligible!)
Memory savings: ~0.5-1%
When to Use DQ¶
Use Double Quantization:
โ Limited VRAM (saving 0.5GB matters)
โ Large models (70B+)
โ Inference (not training)
Skip Double Quantization:
โ Maximum quality required
โ Plenty of VRAM
โ Small models (7B, 13B)
Advanced DQ Techniques¶
Triple Quantization?¶
Can we quantize the meta-scales?
Theoretically: Yes!
Practically: Diminishing returns
- Additional savings: <0.01 GB
- Quality loss: Starts to compound
- Complexity: Much higher
Conclusion: Not worth it
Adaptive DQ¶
# Different quantization per layer
adaptive_dq_config = {
# Early layers: full precision scales
"layers.0-10": {"use_double_quant": False},
# Middle layers: double quantization
"layers.11-25": {"use_double_quant": True},
# Late layers: triple quantization (if desperate)
"layers.26-31": {"use_double_quant": True, "quantize_meta": True},
}
BitsAndBytes Training¶
QLoRA with BNB¶
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
# Quantization config (double quantization enabled)
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True, # Enable DQ
bnb_4bit_compute_dtype=torch.bfloat16,
)
# Load model
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
quantization_config=bnb_config,
device_map="auto",
)
# Prepare for training
model = prepare_model_for_kbit_training(model)
# LoRA config
lora_config = LoraConfig(
r=16,
lora_alpha=32,
target_modules=["q_proj", "v_proj"],
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
)
# Apply LoRA
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# Now you can fine-tune a 4-bit quantized model!
# Only LoRA parameters are trained (~1% of total)
Troubleshooting¶
Common Issues¶
Issue: "CUDA out of memory"
Solution:
- Reduce batch size
- Enable gradient checkpointing
- Use CPU offloading for some layers
Issue: "NaN loss during training"
Solution:
- Reduce learning rate
- Use bfloat16 instead of float16
- Check gradient clipping
Issue: "Slow inference"
Solution:
- Use torch.compile
- Enable fused kernels
- Batch inference requests
Summary¶
Double quantization squeezes the leftover bytes: the per-block scales that ordinary quantization stores in fp32 get quantized themselves, cutting parameter-adjacent overhead at almost no quality cost. This lesson set up the problem - scales are real memory at scale - walked the bitsandbytes implementation and its measured savings, then the honest parts: quality impact, the advanced DQ techniques, and how DQ interacts with bitsandbytes training. The takeaway is small and durable: when 4-bit runs through bitsandbytes, double quantization is nearly free memory, so the question is rarely whether but how much it buys in your config.
References¶
Related Minder Academy Documents¶
- 4101: GGUF Physics - CPU/GPU Hybrid Offloading
- 4102: EXL2 and AWQ - Extreme Quantization
- CHEAT-SHEET-043: 4-Bit Residuals - the Metadata Squeeze and the Hardware-Native Grid
Next Steps¶
- Continue with: 4104: MXFP4 and NVFP4
- Assessment: 4100: Low-bit Quantization - Quiz
Related Documents:
- 4101: GGUF Physics
- 4102: EXL2 and AWQ
-
Experiment Template: EXP_4103: Double Quantization