Teaching language models to reason about code execution remains a fundamental challenge. While Chain-of-Thought (CoT) prompting has shown promise, current synthetic training data suffers from a critical weakness: the reasoning steps are often plausible-sounding explanations generated by teacher models, not verifiable accounts of what the code actually does. This creates a troubling failure mode where models learn to mimic superficially convincing but logically flawed reasoning patterns. We address this by grounding CoT generation directly in program execution traces. Our pipeline instruments code to capture its dynamic behavior, then narrates these execution traces into natural language and factually-grounded rationales that are verifiable by design. This execution-grounded approach ensures every reasoning step reflects what the program computes, eliminating logical hallucinations at the source. We evaluate our method on code reasoning tasks, code generation and explanation tasks from HumanEval. Models trained on our bi-directional trace-grounded data achieve substantial improvements on reasoning tasks, with gains of up to 30 points on output prediction and 28 points on input prediction over base models, alongside competitive explanation and code generation performance. https://github.ibm.com/IBM-Research-AI/Verified-Code-CoT
翻译:教导语言模型对代码执行进行推理仍然是一项基础性挑战。尽管思维链(CoT)提示已展现出潜力,但当前的合成训练数据存在一个关键缺陷:其推理步骤通常是教师模型生成的看似合理的解释,而非对代码实际行为的可验证描述。这导致了一种令人担忧的失效模式,即模型学会了模仿表面上有说服力但逻辑有缺陷的推理模式。我们通过将CoT生成直接锚定在程序执行轨迹上来解决这一问题。我们的流程通过插装代码以捕获其动态行为,然后将这些执行轨迹叙述为自然语言且基于事实的推理依据,这些依据在设计上就是可验证的。这种基于执行的方法确保了每个推理步骤都反映了程序的计算过程,从源头上消除了逻辑幻觉。我们在代码推理任务、HumanEval的代码生成和解释任务上评估了我们的方法。使用我们双向的、基于轨迹的数据训练的模型在推理任务上取得了显著改进,在输出预测上比基线模型提升了高达30个百分点,在输入预测上提升了28个百分点,同时在解释和代码生成性能上也具有竞争力。https://github.ibm.com/IBM-Research-AI/Verified-Code-CoT