Reinforcement Learning for Code Optimization

AK's Picks LLM RLHF ML RL PGPO AI Systems and Hardware Text-to-Code
基于强化学习(RL)提升代码正确性目前已成共识:让模型生成程序,将其在隐藏测试用例上运行,并对通过测试的解给予奖励。将该范式拓展至代码优化看似顺理成章——只需在奖励函数中加入执行时间即可。然而在实践中,一旦执行时间成为主导性奖励信号,测量噪声、奖励稀疏性或GRPO算法本身不稳定性等细微问题便会迅速掩盖有效学习信号,导致RL训练失效:所生成的解提速效果微乎其微,且失败率反而上升。为此,我们提出一套三阶段方法,使执行时间真正可学习:(1)重构代码测试方式——构建DMC-Optim基准,配备大规模优化型测试集及经过校准的沙箱执行环境;(2)重构速度到奖励的映射方式——在RL环境中协同建模正确性与执行速度,并借助离线模拟器预测最具潜力的超参配置;(3)重构模型从该类奖励中学习的方式——针对计时执行场景下更稀疏、更嘈杂的奖励特性,专门适配GRPO算法及评估流程。在DMC-Optim基准上,表现最优的优化感知型配置显著提升了严格定义下的top-50% pass@1指标:Qwen 2.5 7B模型由18.0%提升至31.3%,CWM 32B模型则由30.7%跃升至50.4%。当采用更严格的评估标准(如top-30%)时,增益进一步扩大——CWM 32B模型实现125%的相对提升,同时完全保持原有纯正确性指标不变。即便在计时沙箱性能退化的情况下,本方法所实现的鲁棒优化型RL仍较标准RLVR基线取得100%–200%的提升,具体幅度取决于所采用的评估准则。在LCB基准上,CWM 32B模型在中位样本速度对比中,胜出率高达83%(相较标准RLVR)。此外,相对于每道题目中人类提交解的最快正确版本,该模型在复杂度类别层面的改进速率约为人类水平的一半(14% vs. 28%)。
RL for code correctness is now established: have the model generate a program, run it against hidden test cases, and reward solutions that pass. Extending this to code optimization seems straightforward: just add execution time to the reward. But in practice, once timing drives the reward, small problems in measurement noise, reward sparsity, or GRPO instability overwhelm the signal and make RL fail: generated solutions are barely faster, and more of them can fail. We make execution time learnable through three stages: (1) how code is tested, by building DMC-Optim with large optimization tests and a calibrated sandbox; (2) how speed is turned into reward, by composing correctness and speed in the RL environment and using an offline simulator to predict the most promising configurations; and (3) how the model learns from that reward, by adapting GRPO and evaluation to the sparser, noisier timed-execution setting. On DMC-Optim, the strongest optimization-aware configurations improve strict top-50% pass@1 from 18.0% to 31.3% on Qwen 2.5 7B and from 30.7% to 50.4% on CWM 32B. These gains further increase at stricter percentiles such as top-30%, with 125% relative improvement for CWM 32B, while preserving pure-correctness scores. When the timing sandbox is degraded, robust optimization RL reaches 100% to 200% improvement over standard RLVR, depending on the evaluation criterion. On LCB, CWM 32B wins up to 83% of median-sample speed comparisons against standard RLVR. Relative to the fastest correct human submissions per problem, it reaches about half the human rate of complexity-class improvements (14% vs. 28%).
许愿