Source: AI-Generated Image
Source: AI-Generated Image

We all know large AI models are huge. If today you decide to use them on your local system, your system would struggle to even load them. Forget about the inference part.

I'm sure you already know about this problem. And that's why I want to introduce a couple of techniques that make it possible to run these models on your own systems.

The techniques that I'm going to talk about are quantization and distillation.

The idea is to get a smaller model to act similarly to the larger one. But they do it in completely different ways.

Most people mix up these 2 techniques. And it's about time we fix that.

Let us understand these two techniques and how they can make a large model run on your system.


1) Quantization

Very simple idea. You have a large model. You cannot use it on your personal system. So we shrink its size.

But hang on! You might wonder, okay, we shrink its size, but won't it affect its performance? How can we do it without breaking the model?

Let me tell you, a model is made up of billions of numbers. These numbers are known as weights. And quantization is not about deleting them. Instead, we store the same weights but with fewer bits. This means we end up reducing precision.

For instance, when you click a photo on your phone, it is 10 MB. When you send it on WhatsApp, it reaches your friend as a file of 200 KB. It's still the same photo. You haven't even cropped it. So why does the size get smaller?

This is because some of the fine details are eliminated. You notice it only when you zoom in. But for most people, that is still good enough. Quantization does exactly that to your model.

If we think of a Llama 3.1 8B model, this is a model having 8 billion parameters. By default, each weight is stored in 16 bits. So that comes to around 16 GB just to load the model. Many laptops won't be able to handle this.

But guess what? If we store each weight in 8 bits, we would need around 8 GB. Take it down to 4 bits, and it's even less.

An interesting thing to know is that if you have ever downloaded a model from Ollama, you have already used quantization. The files with Q4 or Q8 in the name are the quantized models. Those numbers tell you how many bits each weight uses.

The catch is precision.

The answers become slightly less precise. Perhaps at 8 bits you would barely notice that. But push it down to 2 or 3 bits, and the model may start to make mistakes.

It's a trade-off between precision and space.


2) Distillation

Shrinking the model is fine, but what if, instead of shrinking a large model, we train a smaller model that behaves like a large one?

That is distillation.

For instance, when you're preparing for an examination, you most likely won't read the entire 600-page textbook mentioned in the curriculum. You'd rather opt for 100 pages of notes that you have made during the lectures. These notes consist of all the important questions that could be asked.

The catch is that you won't know everything that was present in the original textbook, but you can still clear the exam.

In AI, this small model that we are building is trained on the larger model's output instead of learning everything from scratch.

This is not just theory. Because there are companies out there doing this.

DistilBERT is a popular example where Hugging Face took Google's BERT and distilled it into a smaller model. It is 40% smaller and 60% faster, yet it keeps 97% of BERT's language understanding.

Meta has done the same. They built their 1B and 3B models by using the outputs of the larger Llama 3.1 8B and 70B models.


My Take

These two techniques are the main reasons large AI models aren't locked inside big data centers.

A few years ago, running such a large model on a laptop sounded insane. Today, a student can do it in their hostel room.

The major difference that you need to understand is that quantization keeps the same model. It just stores its weights with fewer bits, whereas distillation creates a new smaller model behaving like the larger one.

Also, quantization does not need training data because you are just shrinking the model. Distillation needs training data and a lot of GPU hours.

Quantization opts for shrinking. Distillation trains a new one.