Abstract
Developing high-performance GPU kernels is critical for AI and scientific computing, but remains challenging due to its reliance on expert crafting and poor portability. While large language models (LLMs) offer promise for automation, both general-purpose and finetuned LLMs suffer from two fundamental and conflicting limitations: correctness and efficiency. The key reason is that existing LLM-based approaches directly generate the entire optimized low-level programs, requiring exploration of an extremely vast space encompassing both optimization policies and implementation codes. To address the challenge of exploring an intractable space, we propose Macro Thinking Micro Coding (MTMC), a hierarchical framework inspired by the staged optimization strategy of human experts. It decouples optimization strategy from implementation details, ensuring efficiency through high-level strategy and correctness through low-level implementation. Specifically, Macro Thinking employs reinforcement learning to guide lightweight LLMs in efficiently exploring and learning semantic optimization strategies that maximize hardware utilization. Micro Coding leverages general-purpose LLMs to incrementally implement the stepwise optimization proposals from Macro Thinking, avoiding full-kernel generation errors. Together, they effectively navigate the vast optimization space and intricate implementation details, enabling LLMs for high-performance GPU kernel generation. Comprehensive results on widely adopted benchmarks demonstrate the superior performance of MTMC on GPU kernel generation in both accuracy and running time. On KernelBench, MTMC achieves near 100% and 70% accuracy at Levels 1-2 and 3, over 50% than SOTA general-purpose and domain-finetuned LLMs, with up to 7.3× speedup over LLMs, and 2.2× over expert-optimized PyTorch Eager kernels. On the more challenging TritonBench, MTMC attains up to 59.64% accuracy and 34× speedup. All models and datasets will be made publicly available.
Introduction
Writing high-performance GPU kernels—the low-level programs that handle operations like matrix multiplication or attention—is a notorious bottleneck in AI development. It typically requires human experts to spend months manually tuning code for specific hardware architectures, such as NVIDIA’s Hopper or Ampere. While Large Language Models (LLMs) show promise in general coding tasks, they struggle with GPU kernels due to two primary factors: correctness and efficiency. GPU code is inherently brittle, where minor memory management errors lead to system crashes. Furthermore, the search space for optimizations like tiling, fusion, and pipelining is astronomical, often reaching $10^9$ possible configurations for a single subgraph.
Existing approaches typically attempt to generate entire optimized kernels in a single pass, which frequently results in buggy code or performance that falls significantly short of human-expert standards. To overcome these hurdles, we introduce QiMeng-Kernel and its core paradigm: Macro-Thinking Micro-Coding (MTMC).
Methodology: The MTMC Framework
The MTMC framework adopts a "divide and conquer" strategy inspired by the staged optimization process used by human experts. It separates the high-level strategic reasoning from the low-level implementation details.
1. Macro Thinking (The Strategist)
In this stage, a lightweight LLM (such as DeepSeek-Coder-1.3B) acts as a high-level architect. Instead of writing code directly, it analyzes hardware features (e.g., Streaming Multiprocessors, Shared Memory) and the initial unoptimized code to propose Semantic Optimization Actions. These actions include tiling, operator fusion, pipelining, and loop reordering. The "strategist" is trained using Reinforcement Learning (PPO) within a tree-structured environment, receiving rewards based on the speedup and correctness of the resulting code.
2. Micro Coding (The Implementer)
Once a macro action is selected (e.g., "Fuse the linear and max layers"), a powerful general-purpose LLM (such as Gemini 2.5 Pro) takes over. Its role is strictly defined: implement only that specific optimization. By focusing on incremental, atomic changes, the system minimizes the risk of introducing compilation errors and ensures the final kernel remains functional.
Experimental Evaluation
We evaluated MTMC using two primary benchmarks: KernelBench, which includes 250 tasks ranging from single operators to full neural networks (GPT, ViT), and TritonBench, featuring 184 real-world kernels. To support the training of the Macro Thinking model, we curated an offline dataset of 60,000 optimization trajectories.
Key Results
- Superior Accuracy: MTMC achieved near 100% accuracy on KernelBench Levels 1 and 2, outperforming state-of-the-art LLM-based agents by more than 50%.
- Outperforming Experts: On complex Level 3 tasks, MTMC generated kernels that were 2.2× faster than expert-optimized PyTorch Eager kernels.
- Significant Speedups: Compared to vanilla LLMs, MTMC achieved up to a 7.3× speedup on KernelBench and a remarkable 34× speedup on the more challenging TritonBench.
- Cross-Hardware Robustness: The system demonstrated consistent performance gains across multiple NVIDIA GPU generations, including V100, A100, and H100.
Conclusion
QiMeng-Kernel demonstrates that decoupling optimization strategy from implementation syntax allows LLMs to navigate the complex GPU kernel design space effectively. By combining the strategic reasoning of lightweight, RL-tuned models with the implementation capabilities of general-purpose LLMs, MTMC achieves performance that rivals or exceeds human-expert libraries. Future work will explore expanding this framework to end-to-end network-level optimizations and broader hardware architectures.
Summary updated