Enhancing the Code Reasoning Capabilities of LLMs via Consistency-based Reinforcement Learning

📄 arXiv: 2605.17958v1 📥 PDF

作者: Zhanyue Qin, Jia Feng, Yibo Lyu, Yun Peng, Dianbo Sui, Cuiyun Gao, Qing Liao

分类: cs.LG, cs.PL

发布日期: 2026-05-18

备注: Under review


💡 一句话要点

提出CodeThinker,通过一致性强化学习提升LLM的代码推理能力

🎯 匹配领域: 支柱二:RL算法与架构 (RL & Architecture) 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 代码推理 强化学习 一致性学习 大型语言模型 奖励机制

📋 核心要点

  1. 现有代码推理强化学习方法奖励设计不足,忽略了推理过程的一致性,导致奖励稀疏和奖励利用漏洞。
  2. CodeThinker通过一致性追踪范式、动态束搜索和一致性奖励机制,提升LLM在代码推理任务中的性能。
  3. 实验表明,CodeThinker在多个基准测试和下游任务中均取得了显著的性能提升,验证了其有效性。

📝 摘要(中文)

本文研究代码推理任务,即在给定源代码和输入的情况下预测程序的输出,以此评估大型语言模型(LLM)的推理能力,并促进代码生成和数学推理等下游任务。现有强化学习方法仅基于最终输出或粗粒度信号设计奖励,忽略了代码推理过程中逐步推理的内在一致性,导致奖励稀疏或奖励利用漏洞,限制了学习能力的发挥。为此,本文提出了CodeThinker,一个基于一致性驱动的代码推理强化学习框架。CodeThinker包含三个关键组件:(1) 逐步推理感知模型训练模块,利用一致性追踪范式合成训练数据,捕捉逐步推理过程;(2) 动态束搜索策略,在固定采样预算下提高采样输出的质量;(3) 一致性奖励机制,有效缓解奖励利用漏洞。在三个流行基准上的实验表明,CodeThinker在多个LLM上实现了最先进的性能。例如,在Qwen2.5-Coder-7B-Instruct上部署时,其准确率比最强的基线高出4.3%。本文还验证了CodeThinker在下游任务上的有效性,结果表明,在没有额外训练的情况下,CodeThinker在数学推理和覆盖17种编程语言的代码推理任务上分别获得了平均5.33和3.11个百分点的准确率提升。

🔬 方法详解

问题定义:论文旨在提升大型语言模型在代码推理任务中的能力,即根据给定的代码和输入预测输出。现有方法主要依赖强化学习,但其奖励机制设计存在缺陷,通常只关注最终结果或粗粒度的中间步骤,忽略了代码推理过程本身所蕴含的逻辑一致性,导致奖励信号稀疏,模型容易利用奖励机制的漏洞,而非真正理解代码的逻辑。

核心思路:论文的核心思路是利用代码推理过程中的一致性作为强化学习的指导信号。具体来说,如果模型在推理的每一步都遵循正确的逻辑,那么最终的输出结果更有可能是正确的。因此,论文设计了一种基于一致性的奖励机制,鼓励模型在推理过程中保持逻辑一致性,从而提高代码推理的准确性。

技术框架:CodeThinker框架包含三个主要模块:(1) 逐步推理感知模型训练模块:该模块通过一致性追踪范式生成训练数据,模拟代码推理的逐步过程,使模型能够学习到推理过程中的逻辑关系。(2) 动态束搜索策略:该策略在推理过程中动态调整搜索范围,提高采样输出的质量,从而更好地探索潜在的正确推理路径。(3) 一致性奖励机制:该机制根据模型在推理过程中是否保持逻辑一致性来给予奖励,避免模型利用奖励机制的漏洞。

关键创新:论文的关键创新在于提出了一种基于一致性的强化学习方法,将代码推理过程中的逻辑一致性作为强化学习的指导信号。与现有方法相比,该方法能够更有效地利用强化学习来提升LLM的代码推理能力,避免了奖励稀疏和奖励利用漏洞的问题。

关键设计:在逐步推理感知模型训练模块中,使用了Consistency Tracing Paradigm来合成训练数据,模拟代码推理的步骤。动态束搜索策略的具体实现未知,但其目标是在固定计算资源下提升采样质量。一致性奖励机制的设计细节未知,但其核心是衡量模型在推理过程中是否保持逻辑一致性。

📊 实验亮点

CodeThinker在三个流行的代码推理基准测试上取得了最先进的性能。例如,在Qwen2.5-Coder-7B-Instruct模型上,CodeThinker的准确率比最强的基线高出4.3%。此外,在下游任务中,CodeThinker在数学推理和代码推理任务上分别获得了平均5.33和3.11个百分点的准确率提升,证明了其泛化能力。

🎯 应用场景

CodeThinker具有广泛的应用前景,可用于提升LLM在软件开发、程序调试、安全漏洞检测等领域的性能。通过提高代码推理能力,可以帮助开发者更高效地编写、测试和维护代码,并减少软件缺陷和安全风险。此外,该方法还可以应用于教育领域,帮助学生更好地理解和掌握编程知识。

📄 摘要(原文)

Code reasoning refers to the task of predicting the output of a program given its source code and specific inputs. It can measure the reasoning capability of large language models (LLMs) and also benefit downstream tasks such as code generation and mathematical reasoning. Existing work has verified the effectiveness of reinforcement learning on the task. However, these methods design rewards solely based on final outputs or coarse-grained signals, and neglect the inherent consistency of the stepwise reasoning process in the task. Therefore, these methods often result in sparse reward or reward hacking, which limits the full play of enhanced learning capabilities. To alleviate these issues, we propose CodeThinker, a consistency-driven reinforcement learning framework for code reasoning. Specifically, CodeThinker has three key components: (1) a stepwise reasoning-aware model training module, which utilizes a consistency tracing paradigm as a template to synthesize training data that captures the stepwise reasoning process; (2) a dynamic beam sampling strategy, which aims to improve the quality of sampled outputs under a fixed sampling budget; and (3) a consistency reward mechanism that can effectively alleviate reward hacking. Experiments on three popular benchmarks show that CodeThinker achieves state-of-the-art performance across multiple LLMs. For instance, it outperforms the strongest baseline by 4.3% in accuracy when deployed on Qwen2.5-Coder-7B-Instruct. We also validate the effectiveness of CodeThinker on downstream tasks. Results show that, without additional training, CodeThinker obtains average accuracy gains of 5.33 and 3.11 percentage points on mathematical reasoning and code reasoning tasks covering 17 programming languages, respectively.