Skip to content

Understanding ML Compilation and Intermediate Representation (IR) Through Cooking

For Beginners (and Chefs)

Imagine you're a young chef with a recipe for a delicious dish (this is your machine learning model). Now, you want to cook this dish in different kitchens (these are like various types of computers and devices). Each kitchen has different appliances and tools, and there are many ways to do a single action. The recipe will dictate what one needs to do, and not necessarily how one should carry out the step, e.g. you could peel an apple with a paring knife or with a peeler. Machine learning compilation is like tweaking your recipe (the what, algorithm) so that it turns out perfect, no matter which kitchen (the how, device) you use.

Why Use an Intermediate Representation (IR)?

Now, imagine if you had to write a new recipe for every single kitchen in the world. That would be a lot of work! Instead, what if you could write a general recipe that could be easily changed for each kitchen? This general recipe is like an Intermediate Representation (IR) in machine learning. It’s a universal recipe that can be translated for any kitchen's tools and appliances, i.e. easily adapted to different kitchens without starting from scratch every time.

Bridging Recipes and Kitchens

For Advanced Learners

1. High-Level Recipes and Low-Level Cooking Techniques:

  • High-Level Recipes: Think of your original machine learning algorithms as high-level recipes. These recipes are written in a language that chefs (or programmers, data-scientists, and ML engineers) can easily understand, much like a cookbook with instructions for making various dishes.

  • Low-Level Cooking Techniques: The actual cooking, however, involves precise techniques and varies depending on the kitchen tools and appliances you have (like different stoves, ovens, or even outdoor grills). These represent the low-level hardware where the algorithms actually run.

  • The Gap: There's a big difference between the recipe in the cookbook and what you need to do to cook the dish on a specific type of stove or oven. This is similar to the gap between high-level programming languages and low-level hardware instructions.

2. Role of IR: The Universal Cooking Guide

  • Universal Cooking Guide: IR acts like a universal cooking guide. It takes your high-level recipe and translates it into a form that can be adapted for various types of cooking appliances and techniques. It's more detailed than the recipe but not as specific as the actual cooking instructions for a specific type of stove.

  • Optimization: Before you start cooking on a specific appliance, you optimize your recipe for that appliance using the universal guide. This might involve adjusting cooking times, temperatures, or techniques, much like optimizing the IR for different hardware.

  • Appliance Agnostic: The universal cooking guide (IR) doesn't care whether you're using a gas stove, an electric oven, or a charcoal grill. It provides a basis that can be adjusted to fit any cooking situation, just like how IR abstracts away from hardware specifics.

3. Simplifying the Cooking (Compilation) Process:

  • Easier Multi-Appliance Cooking: With the universal cooking guide (IR), chefs can prepare their dishes to be cooked on any appliance without rewriting the entire recipe from scratch for each one. This is akin to using IR to adapt algorithms for various hardware platforms.

  • Maintaining Recipe Integrity: While being more adaptable to different cooking methods, the universal guide still maintains the essence and flavors of the original recipe, ensuring that the dish turns out as intended, regardless of the cooking surface. Similarly, IR maintains the high-level intent of the algorithm while being closer to execution efficiency.

Wrapping Up

In this analogy, the Intermediate Representation is like a universal cooking guide that bridges the gap between your high-level recipes (algorithms) and the actual cooking techniques required for different types of kitchen appliances (hardware platforms). It simplifies the process, making sure that your dishes (machine learning models) can be prepared efficiently and effectively, no matter where you're cooking them. In Forge, we leverage Relay - TVM's Intermediate Representation.