๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #324

4-Bit Quantization

This note zooms into 4-bit quantization specifically โ€” the aggressive precision reduction that made QLoRA's single-GPU fine-tuning of huge models possible, and the specialized techniques that keep it surprisingly accurate.

Why 4-Bit Is a Meaningfully Different Regime

With only \(2^4=16\) discrete levels available, naive uniform 4-bit quantization would introduce substantial rounding error for a wide range of real-valued weights. This is exactly why 4-bit quantization in practice (as used in QLoRA) relies on more sophisticated techniques than the simple uniform scheme from Model Quantization.

NF4 โ€” NormalFloat 4-Bit

Rather than spacing the 16 quantization levels uniformly across the weight range, NF4 spaces them according to the quantiles of a standard normal distribution โ€” since pretrained neural network weights empirically tend to follow an approximately normal (Gaussian) distribution (see Probability Distributions), placing more quantization levels near zero (where most weight values actually concentrate) and fewer levels far from zero (where few weights land) produces noticeably better accuracy than uniform spacing for this specific, realistic data distribution.

Double Quantization

Standard quantization stores a scale factor per block of weights (to handle varying value ranges across the model). Double quantization takes this one step further: quantizing those scale factors themselves, squeezing out additional memory savings from what would otherwise be a relatively minor but non-trivial overhead at large model scale.

Code

from transformers import BitsAndBytesConfig
import torch

quant_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",           # NormalFloat 4-bit, not naive uniform
    bnb_4bit_use_double_quant=True,        # quantize the scale factors too
    bnb_4bit_compute_dtype=torch.bfloat16   # computations still happen in higher precision internally
)
# When loading a model with this config, weights are STORED in 4-bit,
# but de-quantized on the fly to bfloat16 for actual forward-pass computation

Common Mistakes

  • Assuming 4-bit weights are used directly in matrix multiplications โ€” in practice, they're de-quantized back to a higher-precision format (like bfloat16) on the fly for the actual computation, with only the storage staying at 4-bit; this is what keeps compute numerically stable while still saving memory.
  • Using naive uniform quantization at 4-bit instead of a distribution-aware scheme like NF4 โ€” the accuracy difference between the two becomes much more significant at this aggressive a bit-width than it is at 8-bit.

Interview Relevance

Q: "Why does NF4 quantization outperform naive uniform 4-bit quantization for neural network weights specifically?" Pretrained weights typically follow an approximately normal distribution, concentrated near zero. NF4 places its 16 quantization levels according to the quantiles of a standard normal distribution rather than spacing them uniformly โ€” putting more precision where most actual weight values fall, and less where few values are, producing lower overall quantization error for this specific, realistic weight distribution than uniform spacing would.

Practice Question

Why does storing weights in 4-bit but computing in bfloat16 (via on-the-fly de-quantization) achieve memory savings without sacrificing computational numerical stability?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

4-Bit Quantization โ€“ FAQs

Quick answers about learning 4-Bit Quantization in Deep Learning.

This free note from CodingNow 2.0 explains 4-Bit Quantization in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including 4-Bit Quantization, is 100% free with no signup required.
With focused practice, most students grasp 4-Bit Quantization in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now