TL;DR
Writing custom, high-performance code ("kernels") for AI chips like Google's TPUs is notoriously difficult, requiring deep hardware expertise that standard Large Language Models (LLMs) struggle to match on their own. MaxKernel is an open-source, multi-agent AI system that automates TPU kernel engineering by combining specialized sub-agents—for planning, coding, testing, autotuning, and profiling—into interactive or fully autonomous optimization loops. On a 50-task TPU benchmark, MaxKernel achieves 100% correctness and even outperforms expert human-written code on production workloads.
What Problem Does This Paper Address?
To squeeze every drop of performance out of modern AI accelerators, engineers often bypass standard compilers (like XLA) and write custom kernels by hand—using CUDA for GPUs, or JAX/Pallas for TPUs. However, writing these kernels is a dark art: it demands intimate knowledge of memory hierarchies, rigid APIs, and opaque low-level hardware errors.
While LLMs have shown promise in generating code, standard "zero-shot" prompting falls flat on TPUs. Without real-time compiler feedback, numerical verification, and hardware profiling, LLMs frequently produce code that doesn't even compile, let alone run fast. The authors tackle this gap by asking: Can we build a modular, multi-agent system that mirrors how human experts actually engineer, debug, and profile TPU kernels?
Methods
MaxKernel breaks the complex art of kernel optimization into a team of specialized sub-agents:
- Plan & Implementation Agents: Draft high-level optimization strategies and translate them into Pallas code, using a Retrieval-Augmented Generation (RAG) pipeline fed with Pallas, Mosaic, and XLA documentation (explicitly excluding hand-tuned kernel code so the agents must discover optimizations themselves).
- Compilation & Testing Agents: Iteratively compile the code, fix syntax/hardware errors, and run a strictly frozen test suite on a real TPU to prevent "reward hacking."
- Autotuning & Profiling Agents: Systematically tune parameters (like block and tile sizes) and capture low-level execution traces using XProf to measure true on-device latency and memory bandwidth.
These sub-agents are orchestrated in three distinct modes:
- Human-in-the-Loop (HITL): An interactive mode where the system pauses after each sub-agent finishes, letting a human developer inspect plans, tweak code, or steer the trajectory.
- Autonomous Loop (Auto): A fully automated, closed-loop pipeline (Plan $\rightarrow$ Implement $\rightarrow$ Compile $\rightarrow$ Test $\rightarrow$ Autotune $\rightarrow$ Profile) that iteratively refines a kernel and rolls back to the fastest valid version.
- Graph-Based Autonomous Search: To escape local optima, this mode wraps the Auto agent in a global search graph, running either Parallel Search (multiple independent Auto trajectories) or Beam Search (pruning underperforming paths while exploring multiple depths).
Datasets
The authors evaluate MaxKernel on JAXBench, a benchmark suite of 50 diverse TPU kernel tasks. They also test it on a subset of 8 complex, real-world production workloads from state-of-the-art open-source models—including Flash Attention, GQA, MLA, Sparse Attention, Paged Attention, Ragged Paged Attention, GEMM, and Megablox GMM—where expert hand-tuned Pallas baselines exist.
All experiments are run on a single TPU v6e chip using Gemini 3.1 Pro as the underlying LLM, with correctness verified via jnp.allclose against reference implementations.
Key Insights and Ideas
- Zero-shot LLMs hit a wall on TPUs: Generating 100 independent zero-shot completions (Best-of-N) only yields a 20% correctness rate (10/50), proving that iterative compiler and hardware feedback is non-negotiable.
- Single-trajectory loops get stuck: While the single-pass Auto agent dramatically boosts correctness to 48/50, its performance varies wildly across runs because it can easily get trapped in local optima.
- Parallel exploration unlocks superhuman tuning: Running just 5 concurrent Auto trajectories (Parallel Search) eliminates all failures (50/50 correct) and discovers optimizations that beat even human experts.
Key Results
| Strategy | Compilation Rate | Correctness Rate | Geomean Speedup (vs. XLA) | $\text{fast}_1$ (Faster than XLA) |
|---|---|---|---|---|
| Best-of-N (N=100) | 10/50 | 10/50 | 1.08× | 6/50 |
| MK Auto (Median) | 49/50 | 48/50 | 1.39× | 22/50 |
| MK Beam | 50/50 | 50/50 | 1.48× | 31/50 |
| MK Parallel | 50/50 | 50/50 | 1.62× | 34/50 |
On the 8 production kernels with human-written references, MK Parallel achieves a 2.69× geometric mean speedup over the standard XLA compiler—outperforming the 2.43× geometric mean speedup of hand-tuned human baselines (notably reaching 6.74× on Paged Attention and 5.03× on Sparse Attention).
Limitations and Future Work
- Single LLM evaluation: All experiments were conducted exclusively using Gemini 3.1 Pro; testing how other LLMs perform within the MaxKernel framework is left for future work.
- Variance in single-pass runs: The standalone Auto agent still exhibits noticeable performance variance depending on its initial trajectory, making multi-trajectory search (Parallel or Beam) necessary for peak reliability.
Summary updated