Kernel Forge: An Agent Harness for LLM-based Generation and Optimization of CUDA Kernels
Kernel Forge is an open-source, end-to-end agentic harness that leverages Large Language Models and Monte Carlo Tree Search to automatically generate and optimize CUDA kernels for diverse PyTorch workloads, achieving significant speedups over PyTorch eager mode while providing a graphical interface for inspection and debugging.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Imagine you are trying to run a video game on your computer. Sometimes, the game runs smoothly, but other times it stutters or lags. This happens because the computer has to perform millions of tiny math calculations every second to draw the pictures and handle the physics. In the world of artificial intelligence (AI), these calculations are even more intense. To make an AI "think," it relies on special, high-speed instructions called kernels. Think of a kernel as a specific, super-efficient recipe a chef uses to chop vegetables. If the chef uses a dull knife and a slow method, the whole dinner is late. If they use a sharp, specialized knife, the food is ready instantly.
For years, writing these "recipes" for AI has been like trying to write a novel in a language you barely speak. It requires highly skilled experts to hand-code complex instructions for computer chips (GPUs) to make them run faster. But now, we have a new kind of helper: Large Language Models (LLMs). You might know them as the smart chatbots that can write stories or solve math problems. Scientists are now teaching these chatbots to write the "recipes" (kernels) for AI, hoping they can do it faster and better than humans. However, there's a catch: just because a chatbot writes a recipe that looks good on paper doesn't mean it will actually cook the meal faster in a real kitchen.
This is where a new project called Kernel Forge comes in. Researchers from the University of Michigan built a smart system that doesn't just ask a chatbot to write code; it acts like a full-time coach, a quality inspector, and a project manager all rolled into one. They wanted to see if AI could actually take over the job of optimizing these recipes for real-world AI models, not just for fake, made-up test cases.
The Problem: The "Isolated" Trap
Imagine you are training a robot to run. If you only test it on a perfectly flat, empty track, it might look like a world-class sprinter. But the moment you put it on a bumpy, crowded city street, it might trip and fall. This is exactly what happened with previous attempts to use AI to write GPU kernels. Most tools tested the AI by giving it random, made-up math problems on an empty track. They would generate a piece of code, test it in isolation, and say, "Look how fast this is!"
But in the real world, AI models are like busy cities. The "recipes" (kernels) have to work with specific shapes of data, specific amounts of memory, and they have to play nicely with the other recipes right next to them. A recipe that is fast on a random test might be slow or even break when used inside a real AI model like the ones that generate images or chat with you. Previous tools often left it up to human developers to figure out how to fit these new, AI-written recipes back into the complex machine, which was tedious and error-prone.
The Solution: Kernel Forge
The researchers created Kernel Forge, an open-source tool that acts as a bridge between the AI writer and the real world. Instead of asking the AI to guess, Kernel Forge first watches a real AI model (like one that recognizes faces or writes stories) running on a computer. It takes notes on exactly which "recipes" are being used, how big the data is, and how long they take to run.
Once it has this real-world data, it hands the job to an AI agent (a smart bot powered by a large language model). But this isn't a one-shot deal. The system uses a clever strategy called Monte Carlo Tree Search. Imagine a detective solving a mystery. Instead of just following one hunch, the detective tries many different paths. If one path leads to a dead end, they don't give up; they go back and try a different angle. Similarly, Kernel Forge doesn't just write one version of a recipe and hope for the best. It generates many versions, tests them, and if one is slow, it tries to fix it or tries a completely different approach. It keeps a "family tree" of all the attempts, remembering which ones were promising even if they weren't perfect at first.
The Results: Speedy Wins, But Not Everywhere
The team tested Kernel Forge on four different types of AI models: one for recognizing images (ResNet-50), one for generating art (Stable Diffusion 3.5 Medium), and two for chatting and reasoning (Gemma 4 and Qwen 3.5). They ran these tests on a powerful computer with an NVIDIA GB10 GPU.
Here is what they found:
- It works, but it's picky: The system successfully generated new, faster recipes for many parts of the AI. In some cases, the AI-written code was significantly faster than the standard code the computer usually uses. For example, in the image generator, the new code for "group normalization" was 1.70 times faster. In the chatbot Gemma 4, the new code for "softmax" (a math step used to make decisions) was a massive 2.83 times faster.
- The "Guarded" Safety Net: The system is very careful. It has a "guard" that checks every new recipe. If the new AI-written recipe is slower or makes a mistake, the system immediately switches back to the original, reliable code. It never forces a bad recipe on the user. This means that even if the AI tries to optimize a part of the code and fails, the computer doesn't crash or slow down; it just uses the old, safe method.
- The "Big Players" are Hard to Beat: The researchers noticed something interesting. The parts of the AI that take up the most time (the "big players") are often already handled by very mature, expert-written code from companies like NVIDIA. The AI struggled to beat these experts. For instance, in the image generator, the "linear" operation (which takes up over 50% of the time) was actually slower when the AI tried to rewrite it. The system wisely decided to stick with the original code for that part.
- Small Wins Add Up: The biggest speedups happened on smaller, less critical parts of the code. While the AI couldn't beat the experts on the biggest tasks, it found clever ways to speed up the smaller tasks by 1.5 to 2.8 times.
The Cost of Curiosity
The researchers also looked at how much this "thinking" cost. They used a powerful AI model to generate the code, and every time the AI wrote a line of code or checked a result, it cost a tiny bit of money (based on API usage). They found that as they let the AI try more and more variations (up to 50 rounds of trying and fixing), the cost went up. However, the extra speed they got didn't always match the extra money spent. This suggests that while AI can find great shortcuts, we need to be smart about when to stop searching, especially if the part of the code we are trying to fix isn't very important to the overall speed.
The Bottom Line
Kernel Forge shows that we are entering a new era where AI can help write the low-level code that makes other AI run faster. It's not a magic wand that solves everything instantly; it can't easily beat the world's best human experts on the biggest tasks yet. But it is a powerful tool that can find hidden speed boosts in the smaller details, all while keeping the system safe and stable.
The researchers released their tool as open-source, meaning anyone can use it to try and make their own AI models faster. They proved that by combining the creativity of AI with the safety of real-world testing, we can start to optimize the engines of our digital world without needing a PhD in computer science to do it. It's a step toward a future where our computers are not just smarter, but also much more efficient.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.