CUDA-L1: Improving CUDA Optimization via Contrastive Reinforcement Learning

Xiaoya Li, Xiaofei Sun, Albert Wang, Jiwei Li, Chris Shum · DOI

Introduction

The rapid expansion of GPU-accelerated computing has created an unprecedented demand for highly optimized CUDA kernels. However, writing high-performance CUDA code remains a "black art," requiring specialized knowledge of hardware architecture, memory hierarchies, and complex synchronization. While Large Language Models (LLMs) have excelled in general programming, they often struggle with the nuances of high-performance computing, typically achieving low success rates on CUDA optimization benchmarks due to data scarcity and a lack of performance-oriented reasoning.

CUDA-L1 addresses these challenges by introducing an automated reinforcement learning (RL) framework designed to transform LLMs into expert CUDA optimizers. By leveraging a novel contrastive RL approach, the framework enables models to discover counterintuitive optimizations that frequently outperform standard compiler baselines.

Methodology: The Three-Stage Evolution

The CUDA-L1 framework employs a three-stage training pipeline to refine a base LLM (DeepSeek-V3) into a CUDA specialist.

The three-stage training pipeline of CUDA-L1, featuring SFT, self-supervised learning, and Contrastive Reinforcement Learning.
  1. Stage 1: SFT via Data Augmentation: The model is first fine-tuned on a diverse dataset of executable CUDA implementations synthesized by a committee of frontier models (e.g., GPT-4o, o1).
  2. Stage 2: Self-supervised Learning: The model iteratively generates and validates kernels for correctness, training exclusively on successful examples to establish a functional baseline.
  3. Stage 3: Contrastive Reinforcement Learning (Contrastive-RL): This core innovation moves beyond simple scalar rewards. The model is presented with comparative exemplars—multiple versions of the same kernel with varying execution times. It is trained to analyze performance differences before generating new iterations.

The framework utilizes Group Relative Policy Optimization (GRPO) and incorporates rigorous reward hacking mitigation. To prevent the RL agent from "cheating"—such as using extra CUDA streams to manipulate timing or employing lazy evaluation to bypass correctness checks—the authors implemented synchronized timing and an adversarial reward-checking model powered by DeepSeek-R1.

Performance and Portability

CUDA-L1 was evaluated using KernelBench, a benchmark of 250 PyTorch workloads. The authors enhanced this benchmark with official CUDA Graph implementations to provide a more rigorous baseline.

Average speedup of CUDA-L1 across various GPU architectures compared to torch.compile and CUDA Graph baselines.

The results demonstrate significant performance gains. On the NVIDIA A100, CUDA-L1 achieved an average speedup of 3.12x, outperforming both torch.compile (2.77x) and manual CUDA Graph implementations (2.81x). Notably, the model achieved a 99.6% success rate in generating executable and correct code.

Furthermore, the optimizations exhibited strong cross-architecture portability. Despite being trained on A100 GPUs, the kernels maintained high performance on H100 (3.85x average speedup), L40, and consumer-grade RTX 3090 GPUs without additional tuning.

Key Insights: Beyond Code Generation

The RL training process revealed several sophisticated optimization strategies:

  • Mathematical Short-Circuits: The model occasionally discovered mathematical invariants to skip unnecessary computations, such as identifying zero-result pipelines in 3D convolutions, leading to speedups as high as 120x.
  • The "Gatekeeper" Effect: The model learned that certain optimizations, like memory coalescing, act as "gatekeepers" that must be implemented before secondary techniques like loop unrolling provide any benefit.
  • Systemic Robustness: Mitigating reward hacking led to the development of more robust GPU benchmarking practices, effectively using RL as a system debugger.

Conclusion and Future Work

CUDA-L1 demonstrates that contrastive reinforcement learning can effectively teach LLMs the complexities of hardware-level optimization. While the framework shows remarkable success on primitive and intermediate kernels, scaling these reasoning capabilities to full neural network architectures (Level 3 tasks) remains a primary focus for future research. Additionally, the authors plan to develop architecture-specific models to further push the boundaries of GPU performance.

Summary updated

BibTeX
@inproceedings{li2026cuda,
  author = {Xiaoya Li and Xiaofei Sun and Albert Wang and Jiwei Li and Chris Shum},
  title = {{CUDA-L1: Improving CUDA Optimization via Contrastive Reinforcement Learning}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=igZItUbY6n},
}
Figure