TL;DR
Kevin (Kernel Devin) is the first Large Language Model (LLM) specifically trained using multi-turn Reinforcement Learning (RL) to write and optimize CUDA kernels. By mimicking the iterative "write-execute-refine" workflow of human engineers, Kevin achieves an 82% correctness rate and a 1.10x mean speedup over PyTorch Eager, significantly outperforming frontier models like OpenAI’s o4-mini.
The Challenge: Writing High-Performance GPU Kernels
Writing high-performance GPU kernels is often considered a "black art" that requires deep domain expertise. While LLMs have become highly proficient at general code generation, writing in CUDA remains a frontier challenge because it is sparsely represented in training data (comprising less than 0.1% of datasets like the Stack).
Furthermore, kernel engineering is inherently iterative. An engineer rarely writes a perfect, fully optimized kernel on the first try. Instead, they write a baseline, profile its execution, and refine the code based on performance feedback. Most existing RL approaches for code generation are "single-turn," rewarding the model only for its final output. This creates a learning gap: models struggle to learn the long-term strategies needed to transform a slow, correct kernel into a highly optimized one.
Methodology: Multi-Turn Reinforcement Learning
To address this, the researchers developed a multi-turn RL training recipe based on Group Relative Policy Optimization (GRPO). This approach explicitly incorporates the iterative nature of kernel development into the training loop.
Key innovations in this methodology include:
- Turn-by-Turn Training: Instead of treating an entire multi-turn session as one monolithic sample, trajectories are split into individual turns. This allows the model to receive credit for intermediate steps that eventually lead to success.
- Context Management: Reasoning models generate massive "Chains of Thought" (CoT). To prevent context explosion over multiple turns, Kevin summarizes its previous reasoning steps while keeping the actual code and execution feedback intact.
- Discounted Reward Aggregation: Utilizing a classic RL technique—discounted future rewards—a kernel produced in Turn 1 is rewarded not just for its immediate performance, but for its contribution to a highly optimized kernel achieved in Turn 4. Suboptimal steps (like a turn resulting in a compilation error) are tolerated if they serve as necessary precursors to breakthrough optimizations.
- Anti-Hacking Constraints: To prevent the model from "cheating" (e.g., calling PyTorch functions inside a supposedly raw CUDA kernel), strict rule-based checks assign zero reward to "lazy" implementations.
Datasets and Evaluation
The study utilizes KernelBench, a benchmark designed for evaluating LLMs on GPU kernel generation.
- Training: 180 tasks from KernelBench Levels 1 (basic operators) and 2 (fused operators).
- Evaluation: A set of 100 tasks, including 80 novel tasks created by the authors to ensure the model wasn't simply memorizing the benchmark.
- Out-of-Distribution: The model was also tested on Level 3 tasks (full model architectures like Vision Transformers), which it never saw during training.
Key Insights
- Serial Scaling Beats Parallel Scaling: For complex engineering tasks, "thinking longer" (more refinement turns) is more effective than "sampling more" (generating many independent attempts). Kevin demonstrates a higher rate of improvement as it is given more turns to iterate serially.
- The "Not Okay Ratio": The authors identified a quirky proxy signal for model instability. Before the model starts outputting "junk" (repetitive or nonsensical text), it begins its CoT with erratic phrases like "Okay Amigos" or "Okay Holy crap." Tracking this "Not Okay Ratio" allowed the team to predict and mitigate model collapse during training.
Key Results
Kevin was trained starting from the QwQ-32B base model. The performance improvements are stark, particularly when compared to single-turn RL approaches and frontier models.
| Metric | Base (QwQ-32B) | OpenAI o4-mini | Kevin (Multi-Turn RL) |
|---|---|---|---|
| Correctness (best@16) | 56% | 38% | 82% |
| Mean Speedup (vs Eager) | 0.53x | 0.78x | 1.10x |
| fast$_{1.5}$ (Speedup > 1.5x) | 10% | 13% | 20% |
Qualitatively, Kevin demonstrated the ability to implement advanced optimizations like loop unrolling and warp-level reductions—techniques that even many human developers find difficult to implement correctly.
Limitations and Future Work
While Kevin represents a significant leap forward, the authors note several limitations:
- Hardware Specificity: Speedups were measured on NVIDIA H200 GPUs. Because GPU architectures differ, a kernel optimized for an H200 might not be optimal for an A100.
- Compute Intensity: Training Kevin is expensive, requiring roughly 650 H200 GPU hours for a full run.
- Task Diversity: KernelBench currently contains ~250 robust tasks. Expanding the training set to thousands of diverse kernels is a necessary next step.
Future directions include incorporating a learned value network (similar to AlphaGo) to help the model better estimate which code changes are likely to yield the highest speedups before actually executing them.
Summary updated