AI for Everyone: Optimization and Putting Models on a Diet (Part 5)

AI model optimization explained simply
How to put an AI on a diet: the art of optimization
date: · 5 min
author: Matteo
series: AI for Everyone/ part 5/6

How artificial intelligence really works, explained without the maths.

  1. AI for Everyone: The Guide You Wish You’d Had Sooner (Part 1)
  2. AI for Everyone: How Machines Learn (Part 2)
  3. AI for Everyone: Architectures and Artificial Brains (Part 3)
  4. AI for Everyone: Advanced Techniques Made Simple (Part 4)
  5. AI for Everyone: Optimization and Putting Models on a Diet (Part 5)
  6. AI for Everyone: How Do We Know If an AI Really Works? (Part 6)
the whole guide >

“Guys, after everything we’ve eaten, I think I’m starting my diet tomorrow!” exclaimed one of my fellow diners (who hasn’t made that post-feast promise?). Well, AIs also need to go on a diet every now and then.

Yes, you read that right. Just like we feel a bit “weighed down” after a big lunch, AI models can also become too “heavy” and need a bit of optimization. And believe me, the analogies between human dieting and AI model optimization are surprisingly fun.

Quantization: The Low-Bit Diet

Quantization is like going from a full 10-course menu to a balanced but lighter lunch. Instead of using numbers with lots of precision (think of a scale that measures down to the milligram), you use more “rounded” values.

It’s like when grandma says “a pinch of salt” instead of specifying “3.14159 grams”. The result is almost identical, but it takes up much less space in the recipe. In the same way, quantization allows AI models to work almost as well using simpler numbers and taking up less memory.

Full Precision vs Half Precision: Choosing Your Portions

Speaking of precision, it’s a bit like the difference between weighing ingredients with a precision scale or using a simple spoon. Full precision is like using a scientific scale in a lab, half precision is like the classic “cup” in American recipes.

“But don’t you lose quality?” asked Claudia, always attentive to details. Well, it’s like with recipes: sometimes you need precision down to the milligram (think of a pastry lab), other times a rougher approach works just fine (like when you cook at home).

Model Distillation: The Art of Reduction

Model distillation is exactly what it sounds like: taking the essence of a large, complex model and transferring it into a smaller, more manageable one. It’s like when you take a sauce that’s been simmering for hours and reduce it into a more concentrated but equally tasty version.

It reminds me of when my grandmother taught me how to make her ragù: “Look, in the end all those steps can be boiled down to the essentials without losing the flavor”. Model distillation does the same thing: it takes an expert “chef” model and teaches a smaller, more efficient model to achieve similar results.

Model Pruning: Trimming the Excess

Model pruning is like cleaning up the kitchen after a big feast. Look at all these leftovers: which ones are worth keeping and which can we throw away? In the world of AI, pruning means removing the connections and parameters that don’t contribute significantly to the final result.

It’s like when you realize that drawer full of kitchen utensils you never use can be slimmed down to a few essential tools. The “pruned” model is lighter and faster, but keeps its core capabilities.

Model Compression: The Digital Tupperware

Model compression is the umbrella term for all the techniques we’ve seen so far: quantization, distillation and pruning are all ways of compressing a model. The shared goal is taking a large model and packing it into a smaller space, a bit like when you try to fit all the lunch leftovers into containers for the fridge.

“It’s like my vacuum-sealing trick!” exclaimed Francesco, our chef friend. “You take something bulky and compress it without losing quality.” Almost! Compression tries to retain as much of the model’s capabilities as possible while taking up less space, but usually something gets lost along the way: the trick is keeping that loss small.

The Art of Balance

Optimizing AI models really is an art of balance, just like a healthy diet. It’s not about drastically cutting everything, but about finding the right balance between size, efficiency and performance.

It’s like when you try to eat healthier: you don’t have to cut desserts out of your life completely (thank heavens!), but find a sustainable balance. In the same way, optimizing an AI model means finding the sweet spot between a smaller size and good performance.

Surprising Results

The surprising thing? The loss is often so small you barely notice it. A model quantized to 4 bits takes up about a quarter of the memory of the 16-bit version and, for most everyday uses, answers almost as well, but faster and more cheaply. It doesn’t get smarter: it gets lighter. It’s like when you feel more nimble after losing a few extra pounds: still you, just quicker on your feet.

Conclusion: Toward a Lighter Future

These optimization techniques are making AI more accessible and efficient. Just as a well-balanced diet makes us feel better, a well-optimized AI model runs faster, costs less and can even work on a laptop or a smartphone.

In the next article we’ll talk about how to assess whether these “diets” actually work, exploring the metrics and methods for evaluating AI models. But that’s another story, perhaps to be told over a nice light salad… or maybe not?

P.S. If you’re reading this article while eating something not-so-healthy, don’t worry: even AI models need a bit of computational “comfort food” now and then! 😉


Useful resources

Copied