QiMeng-MegaKernel: LLMs Optimizing LLM Inference via Automated MegaKernel Generation

Weiqiang Xiong, Shaohui Peng, Wenyi Li, Hao Lu, Qirui Zhou, Congying Ma, Ziming Ye, Zhenyu Yi, Yunji Chen, Qi Guo, Ling LiNeurIPS 2026

TL;DR

When running a large model, your GPU spends a shocking amount of time just waiting for the CPU to tell it what to do next. Fusing an entire model into one giant "MegaKernel" fixes this bottleneck, but writing one by hand is a nightmare of low-level engineering. QiMeng-MegaKernel teaches LLMs to automatically design, optimize, and verify these massive CUDA kernels from scratch—dramatically accelerating inference across diverse AI models and GPUs without human intervention.

What Problem Does This Paper Address?

Picture a chef trying to cook a meal, except after chopping every single vegetable, they have to stop, wash their hands, walk out of the kitchen, and wait for a manager to hand them the next instruction.

That is roughly how modern inference engines (like vLLM or SGLang) run Large Models. They break a model down into hundreds of tiny operations—called kernels—launched sequentially by the CPU. In latency-sensitive scenarios like robotics or real-time assistants, this creates massive "kernel launch overhead" and forces data to constantly ping-pong between slow off-chip memory and fast on-chip memory. On an NVIDIA A100 running Llama3.2-1B, simply launching kernels eats up 41.8% of total execution time!

Comparison of Discrete Kernels vs. MegaKernels

The antidote is the MegaKernel: fusing the entire model's inference into one persistent GPU kernel. No launch bubbles, and intermediate data stays right on the chip.

The catch? Writing a MegaKernel requires thousands of lines of tangled, hardware-specific CUDA code. Human-crafted MegaKernels don't port to new models or GPUs, rule-based compilers are too rigid, and asking a standard LLM to write thousands of lines of synchronized CUDA code in one shot results in a hallucinated mess.

Methods

Instead of asking an LLM to swallow the whole elephant at once, the authors introduce QiMeng-MegaKernel, a structured framework built on a novel Instruction-Centric Fusion Abstraction.

They observe that transformer operations boil down to three core patterns: GEMM (heavy matrix-matrix math for Tensor Cores), GEMV (skinny matrix-vector math), and Attention. Each pattern is paired with an Optimization Skeleton—a structured code template with "slots" for memory movement, pipelining, and computation.

Figure 2: The QIMENG-MEGAKERNEL workflow consists of four stages: (1) Decomposition and config- uration. Model layers are converted into instruction instances with analyzed configurations. (2) Instruction scheduling. Instruction sequences are mapped onto GPU SMs for optimized execution. (3) Python reference generation. An LLM generates executable Python code for correctness verification. (4) CUDA generation. Guided by skeletons and GPU configs, the LLM produces final CUDA kernels with a closed-loop verification.

Starting from standard Hugging Face Python code, an LLM agent executes a four-stage pipeline:

  1. Decomposition & Configuration: The LLM chops the model into fused "instructions" matching the three patterns, separating pure math logic from hardware-specific settings (like warp specialization).
  2. Instruction Scheduling: The LLM acts as a traffic controller, ordering instructions and mapping them evenly across the GPU’s Streaming Multiprocessors (SMs) to eliminate idle "bubbles."
  3. Python Reference Generation: Using only the math configurations, the LLM writes a clean, serial Python version of each fused instruction.
  4. Skeleton-Guided CUDA Generation & Verification: Filling in the optimization skeletons, the LLM writes __device__ CUDA functions for each instruction and stitches them into a single __global__ MegaKernel.

Crucially, the system uses hierarchical verification: it checks the generated Python against the Hugging Face model layer-by-layer, and then checks each CUDA snippet against its Python twin. If nvcc throws a compiler error or tensors mismatch, an automated feedback loop tells the LLM to debug and try again.

Datasets

This is a pure inference optimization framework (no model training required). To evaluate real-world LLM throughput, the authors sampled up to 200 prompts from six established benchmarks: HumanEval (coding), GSM8K and MultiArith (math), CoinFlip and ReClor (reasoning), and SQuAD (reading comprehension). For Vision-Language-Action (VLA) robotics models, they evaluated standard robotic control setups across varying camera views (1–3) and action chunk sizes (10, 20, 50). No new datasets were introduced.

Key Insights and Ideas

  • Scaffolding turns failing LLMs into CUDA wizards: When asked to write inference code directly ("vanilla"), GPT-5.2, Claude-Sonnet-4.5, and DeepSeek-R1 completely failed to produce working code, and Gemini-3-Pro limped along at ~90 tokens/s. Plugged into QiMeng-MegaKernel, every single one of those models successfully generated MegaKernels hitting ~450+ tokens/s on an A100.
  • Two-stage verification is the secret to autonomy: Debugging a 5,000-line CUDA kernel is nearly impossible. By generating an intermediate Python reference first, the framework isolates strategy bugs (did we slice the model wrong?) from implementation bugs (did we mess up a CUDA pointer?), making self-correction reliable.
  • Small batches love MegaKernels: Theoretical latency analysis confirms MegaKernels shine brightest at small batch sizes and short-to-medium contexts—precisely where launch overheads dominate and register pressure stays low.

Key Results

Evaluated across six diverse models—dense LLMs (Llama3.2, Qwen2.5/3), a Mixture-of-Experts model (Qwen1.5-MoE), and a robotic VLA model ($\pi_0$)— across three NVIDIA GPU architectures (Hopper H100, Ampere A100, Ada L20):

  • Crushing Inference Engines: Achieved up to 5.04$\times$ higher throughput than vLLM, 2.42$\times$ over SGLang, 4.68$\times$ over Ollama, and 2.33$\times$ over TensorRT-LLM.
  • Beating Compilers & Humans: Outperformed the rule-based MegaKernel compiler MPK by up to 1.92$\times$ (and worked on Ada GPUs and MoE/VLA models where MPK couldn't). Remarkably, it even edged out human-handcrafted MegaKernels on Llama3.2-1B and VLA $\pi_0$.
  • Slashing Latency: A case study on Qwen3-0.6B (H100) showed vLLM launching 94 kernels taking 2.792 ms per token; QiMeng-MegaKernel launched 1 kernel, finishing in 0.861 ms.

Limitations and Future Work

  • Large-batch trade-offs: Because MegaKernels pack huge amounts of logic together, they consume more registers and shared memory. As batch sizes and context lengths grow very large, reduced GPU occupancy makes MegaKernels less advantageous compared to traditional engines.
  • Hardware ecosystem: The framework was only verified on NVIDIA GPUs (Hopper, Ampere, Ada). Future work aims to extend this automated generation paradigm to other GPU vendors and specialized AI accelerators.

Summary updated

Abstract

High-performance inference for large models is critical in latency-sensitive applications. Existing inference engines suffer from sequential execution of hundreds of fine-grained kernels, causing kernel launch overheads and redundant memory accesses. A promising solution is fusing multiple kernels into a single large kernel, namely MegaKernel. However, existing MegaKernel implementations are tightly coupled to specific models and GPU architectures and require extensive manual engineering. We propose an LLM-driven framework QiMeng-MegaKernel, which leverages an Instruction-Centric Fusion Abstraction to decompose MegaKernels into tractable fusion instructions, thereby enabling automated generation. Our framework jointly performs fusion strategy reasoning and CUDA code generation while hierarchically verifying correctness. Extensive experiments demonstrate that QiMeng-MegaKernel consistently outperforms state-of-the-art inference engines (up to 5.04$\times$ vLLM and 2.42$\times$ SGLang) and compilers (up to 1.92$\times$ MPK), while supporting a broader range of models (dense, MoE, and VLA) and NVIDIA GPU platforms (Hopper, Ampere, and Ada).
BibTeX
@inproceedings{xiong2026qimeng,
  author = {Weiqiang Xiong and Shaohui Peng and Wenyi Li and Hao Lu and Qirui Zhou and Congying Ma and Ziming Ye and Zhenyu Yi and Yunji Chen and Qi Guo and Ling Li},
  title = {{QiMeng-MegaKernel: LLMs Optimizing LLM Inference via Automated MegaKernel Generation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=NYcBKCjBjb},
}
Figure