Hello! In our last lesson, we walked through a full fine-tuning project where we used QLoRA to adapt a Llama 3 model on a custom dataset. You saw firsthand how we could load a massive 8-billion parameter model onto a single GPU by using a BitsAndBytesConfig to load it in 4-bit precision.
This lesson dives into the theory behind that practical step. We're going to unpack the "Q" in QLoRA: quantization. Our goal is to explain the concept and benefits of quantization for reducing model size and memory usage. Understanding this is essential for any AI engineer, as it's one of the most powerful tools we have for making large models practical to deploy and run.
1. The Core Problem: Why LLMs Are So Big
Large Language Models, by their very nature, are defined by a massive number of parameters—the weights and biases that form the neural network. A typical pre-trained model stores each of these parameters as a 32-bit floating-point number (FP32), which requires 4 bytes of memory per parameter.
Given your background as a software engineer, you're familiar with how data types determine memory allocation. Let's do some quick math for a "small" 7-billion parameter model:
Just loading this model's weights into memory requires 28 GB of VRAM, exceeding the capacity of most consumer and many professional-grade GPUs. This is before even accounting for the memory needed for activations during inference or gradients during training.
This is where quantization comes in.
2. What is Quantization?
Quantization is a compression technique that reduces the numerical precision of a model's parameters. Instead of storing each weight as a high-precision 32-bit float, we convert them to a lower-precision format, such as an 8-bit integer (INT8) or even a 4-bit integer (INT4).

This reduction has a dramatic effect on model size:
- FP32 (32-bit): 4 bytes per parameter
- INT8 (8-bit): 1 byte per parameter (a 75% reduction in size)
- INT4 (4-bit): 0.5 bytes per parameter (an 87.5% reduction in size)
Suddenly, our 28 GB model could become 7 GB (at 8-bit) or even 3.5 GB (at 4-bit), making it feasible to run on a wide range of hardware.
3. How Quantization Works Without Breaking the Model
You might be wondering: how can we throw away so much precision without catastrophically degrading the model's performance? To understand this, we first need to appreciate how floating-point numbers are stored and why quantization is not as lossy as it might seem.
The following video provides an excellent technical explanation that will connect well with your software engineering background.
Quantizing LLMs - How & Why (8-Bit, 4-Bit, GGUF & More)
This video from Adam Lucek explains the fundamentals of quantization. The first part delves into the binary representation of floating-point numbers, which is key to understanding why they are so memory-intensive.
Please watch from the beginning until 12:15. Focus on these key points: The Motivation (0:00 - 2:20): How quantization allows massive models to run on consumer hardware. Floating-Point Precision (2:20 - 12:15): Pay attention to the breakdown of FP32 numbers into a sign, exponent, and mantissa. This section explains why a format like FP32, while precise, is expensive in terms of memory.
As the video explains, high-precision formats like FP16 or FP32 are crucial during training to capture the subtle updates from gradients. However, for inference (i.e., generating text), it turns out that the absolute value of each individual weight is less important than the overall relationship between all the weights in a layer.
Let's continue with the video to see this concept explained.
Quantizing LLMs - How & Why (8-Bit, 4-Bit, GGUF & More)
Now, let's explore why this precision reduction is effective. This next segment explains the core insight that makes quantization work.
Please watch from 12:15 to 14:48. The key takeaway here is the idea that we can map the vast range of floating-point numbers to a much smaller set of integers while preserving the model's predictive power because the relative differences between weights are maintained.
The process of converting a floating-point number to a lower-bit integer involves a simple mapping. For each group of weights, we find their minimum and maximum values. This range is then mapped to the target integer range (e.g., -128 to 127 for INT8). Two values are calculated and stored:
- A scale factor to convert the numbers between the ranges.
- A zero-point to ensure that the value 0.0 in the original floating-point range maps perfectly to an integer in the new range.
During inference, the model performs calculations using the fast, low-precision integers and then uses the scale factor and zero-point to de-quantize the final result back into a floating-point number if needed.
4. The Tangible Benefits of Quantization
The advantages of quantization extend beyond just fitting a model into VRAM. They impact performance, cost, and energy efficiency, all of which are critical concerns in a production environment.

Here’s a breakdown of the main benefits:
-
Reduced Memory Footprint: As we've established, this is the primary benefit. It reduces both storage requirements (disk space) and runtime memory requirements (VRAM or RAM), enabling deployment on edge devices, mobile phones, and cheaper cloud instances.
-
Faster Inference Speed: Modern CPUs and GPUs can perform integer arithmetic much faster than floating-point arithmetic. By operating on 8-bit or 4-bit integers, quantized models can achieve significantly lower latency. For example, 4-bit quantization can lead to a 100-200% speed improvement over the FP16 baseline.
-
Lower Energy Consumption and Cost: Faster computations and reduced memory access translate directly to lower power consumption. For large-scale deployments, this results in substantial cost savings on cloud bills and a smaller environmental footprint. According to the "Complete Guide to LLM Quantization," 4-bit quantization can cut inference energy by 45-80%.
Test your understanding!
A 13B parameter model is released. Your machine has a GPU with 12GB of VRAM. Can you run this model without quantization? Can you run it with 4-bit quantization? Explain your reasoning.
Show answer
Without quantization (in FP32), the model would require 13B * 4 bytes = 52 GB of VRAM. Even at half-precision (FP16), it would need 13B * 2 bytes = 26 GB. In both cases, it would not fit in 12GB of VRAM.
With 4-bit quantization, the model requires 13B * 0.5 bytes = 6.5 GB of VRAM. This would comfortably fit within the 12GB available, leaving room for activations and other overhead.
5. Quantization in Practice: A Demonstration
Let's bridge theory and practice. In the last lesson, you used BitsAndBytesConfig from the bitsandbytes library. Now, let's watch a demonstration that shows exactly what this library does and measures the impact.
Quantizing LLMs - How & Why (8-Bit, 4-Bit, GGUF & More)
This final video segment puts everything together. It demonstrates how to use bitsandbytes to load a model in 4-bit and 8-bit precision and evaluates the trade-offs.
Please watch from 14:48 to 21:51. Pay close attention to: The code for creating an 8-bit and 4-bit BitsAndBytesConfig. The reported reduction in memory footprint and VRAM usage. The comparison of text generation quality and the perplexity scores, which show how little performance is lost despite the aggressive compression.
The video also touches upon another popular format: GGUF (GPT-Generated Unified Format). This format is highly optimized for running quantized models efficiently on CPUs, which is what powers popular local LLM tools like Ollama and LM Studio. It's another example of how quantization makes powerful AI accessible to everyone.
Conclusion
You now have a solid conceptual understanding of quantization. You've gone from using it as a black box in the previous lesson to understanding the fundamental principles that make it work.
Key Takeaways:
- Quantization is Precision Reduction: It's the process of converting model weights from high-precision formats (like 32-bit floats) to low-precision ones (like 8-bit or 4-bit integers).
- It Solves the Size Problem: This drastically reduces model size and memory usage, making it possible to run large models on resource-constrained hardware.
- Benefits Are More Than Size: Quantization also leads to faster inference, lower energy consumption, and reduced operational costs.
- It Works by Preserving Relationships: The technique is effective because the relative patterns and relationships between weights are more critical for model performance than their absolute high-precision values.
- Practical Tools Make It Easy: Libraries like
bitsandbytesand formats like GGUF provide the practical tools to apply quantization easily.
Next Up:
In the last lesson, you saw QLoRA used within a larger project. In this lesson, we've explored the theory of quantization. In our next lesson, we will combine these ideas and focus specifically on the implementation of QLoRA. We'll examine how it masterfully interleaves quantization and Low-Rank Adaptation to achieve the most memory-efficient fine-tuning possible.