CUDA-L1: Improving CUDA Optimization via Contrastive Reinforcement Learning

📄 arXiv: 2507.14111v9 📥 PDF

作者: Xiaoya Li, Xiaofei Sun, Albert Wang, Jiwei Li, Chris Shum

分类: cs.AI, cs.DC, cs.LG

发布日期: 2025-07-18 (更新: 2025-11-25)

备注: Project Page: https://deepreinforce-ai.github.io/cudal1_blog/


💡 一句话要点

CUDA-L1:利用对比强化学习提升CUDA代码优化

🎯 匹配领域: 支柱二:RL算法与架构 (RL & Architecture)

关键词: CUDA优化 强化学习 对比学习 GPU计算 代码生成

📋 核心要点

  1. 现有CUDA优化方法依赖人工经验,效率低且难以扩展,大型语言模型在CUDA优化任务中成功率低。
  2. CUDA-L1采用对比强化学习,通过奖励信号引导LLM学习CUDA优化策略,无需人工干预。
  3. 实验表明,CUDA-L1在KernelBench上实现了显著的加速,并优于多种基线方法,同时具备跨架构的可移植性。

📝 摘要(中文)

GPU计算资源需求的指数级增长迫切需要自动化的CUDA优化策略。尽管LLM的最新进展在代码生成方面显示出希望,但当前SOTA模型在提高CUDA速度方面的成功率较低。本文介绍了一种用于CUDA优化的自动化强化学习框架CUDA-L1,该框架采用了一种新颖的对比强化学习算法。在A100上训练的CUDA-L1在KernelBench的所有250个CUDA内核上,针对默认基线实现了平均3.12倍、中位数1.42倍的加速,峰值加速达到120倍。除了KernelBench提供的默认基线外,CUDA-L1还优于Torch Compile(2.77倍)、Torch Compile with reduce overhead(2.88倍)、CUDA Graph实现(2.81倍)以及cuDNN库(7.72倍)。此外,该模型还展示了跨不同GPU架构的可移植性。研究表明,仅通过基于加速的奖励信号,强化学习可以将最初表现不佳的LLM转变为有效的CUDA优化器,无需人工专业知识或领域知识。这种范例为CUDA操作的自动优化开辟了可能性,并有望大大提高GPU效率,缓解GPU计算资源日益增长的压力。

🔬 方法详解

问题定义:论文旨在解决CUDA代码自动优化的问题。现有方法,包括人工优化和基于LLM的优化,都存在局限性。人工优化耗时耗力,且依赖专家知识;基于LLM的优化虽然有潜力,但在实际CUDA优化任务中的成功率较低,难以达到理想的性能提升。

核心思路:论文的核心思路是利用强化学习(RL)来训练一个能够自动优化CUDA代码的智能体。通过将CUDA代码优化过程建模为一个马尔可夫决策过程(MDP),智能体可以学习在不同的CUDA代码片段上应用不同的优化策略,以最大化性能提升(加速比)。对比学习被用于提高RL训练的效率和稳定性。

技术框架:CUDA-L1框架主要包含以下几个模块:1) LLM代码生成器:用于生成候选的CUDA代码优化版本。2) 性能评估器:用于测量不同CUDA代码版本的执行速度,并计算加速比。3) 强化学习智能体:基于对比强化学习算法,学习选择合适的优化策略。4) 奖励函数:基于性能评估器提供的加速比,为智能体提供奖励信号。整体流程是:LLM生成多个优化后的CUDA代码版本,性能评估器评估这些版本的性能,强化学习智能体根据性能反馈调整优化策略,不断迭代,最终找到最佳的优化方案。

关键创新:该论文的关键创新在于将对比强化学习应用于CUDA代码优化。传统的强化学习方法在CUDA优化任务中可能面临奖励稀疏、探索空间大等问题,导致训练困难。对比强化学习通过引入对比损失,鼓励智能体学习区分不同优化策略的优劣,从而提高学习效率和稳定性。此外,该框架无需人工干预或领域知识,仅通过基于加速比的奖励信号即可实现有效的CUDA优化。

关键设计:在对比强化学习方面,论文可能采用了以下关键设计:1) 对比损失函数:用于衡量不同优化策略之间的相似性和差异性,鼓励智能体学习区分优劣策略。2) 奖励函数设计:奖励函数直接基于加速比,引导智能体最大化性能提升。3) 状态表示:如何将CUDA代码片段表示为强化学习智能体可以理解的状态向量是一个关键问题。论文可能采用了某种代码嵌入技术,将CUDA代码片段转换为向量表示。

🖼️ 关键图片

img_0

📊 实验亮点

CUDA-L1在KernelBench数据集上取得了显著的性能提升,平均加速比达到3.12倍,中位数加速比为1.42倍,峰值加速比高达120倍。此外,CUDA-L1还优于Torch Compile (2.77x), Torch Compile with reduce overhead (2.88x), CUDA Graph implementations (2.81x), 和 cuDNN libraries (7.72x)。这些结果表明,CUDA-L1是一种有效的CUDA代码优化方法。

🎯 应用场景

CUDA-L1具有广泛的应用前景,可用于自动优化各种CUDA应用程序,提高GPU利用率,降低计算成本。该技术可应用于深度学习、科学计算、图像处理等领域,加速相关任务的执行速度。未来,CUDA-L1有望成为CUDA开发人员的强大工具,帮助他们更高效地开发高性能的GPU应用程序。

📄 摘要(原文)

The exponential growth in demand for GPU computing resources has created an urgent need for automated CUDA optimization strategies. While recent advances in LLMs show promise for code generation, current SOTA models achieve low success rates in improving CUDA speed. In this paper, we introduce CUDA-L1, an automated reinforcement learning framework for CUDA optimization that employs a novel contrastive RL algorithm. CUDA-L1 achieves significant performance improvements on the CUDA optimization task: trained on A100, it delivers an average speedup of x3.12 with a median speedup of x1.42 against default baselines over across all 250 CUDA kernels of KernelBench, with peak speedups reaching x120. In addition to the default baseline provided by KernelBench, CUDA-L1 demonstrates x2.77 over Torch Compile, x2.88 over Torch Compile with reduce overhead, x2.81 over CUDA Graph implementations, and remarkably x7.72 over cuDNN libraries. Furthermore, the model also demonstrates portability across different GPU architectures. Beyond these benchmark results, CUDA-L1 demonstrates several properties: it 1) discovers a variety of CUDA optimization techniques and learns to combine them strategically to achieve optimal performance; 2) uncovers fundamental principles of CUDA optimization, such as the multiplicative nature of optimizations; 3) identifies non-obvious performance bottlenecks and rejects seemingly beneficial optimizations that actually harm performance. The capabilities demonstrate that, RL can transform an initially poor-performing LLM into an effective CUDA optimizer through speedup-based reward signals alone, without human expertise or domain knowledge. This paradigm opens possibilities for automated optimization of CUDA operations, and holds promise to substantially promote GPU efficiency and alleviate the rising pressure on GPU computing resources.