[论文解读] An Empirical Study of the Non-determinism of ChatGPT in Code Generation
本论文在 CodeContests、APPS 和 HumanEval 等基准上实证地表明 ChatGPT 的代码生成存在高度非确定性,显示零温度并不保证确定性,并分析与任务特征的相关性。
There has been a recent explosion of research on Large Language Models (LLMs) for software engineering tasks, in particular code generation. However, results from LLMs can be highly unstable; nondeterministically returning very different codes for the same prompt. Non-determinism is a potential menace to scientific conclusion validity. When non-determinism is high, scientific conclusions simply cannot be relied upon unless researchers change their behaviour to control for it in their empirical analyses. This paper conducts an empirical study to demonstrate that non-determinism is, indeed, high, thereby underlining the need for this behavioural change. We choose to study ChatGPT because it is already highly prevalent in the code generation research literature. We report results from a study of 829 code generation problems from three code generation benchmarks (i.e., CodeContests, APPS, and HumanEval). Our results reveal high degrees of non-determinism: the ratio of coding tasks with zero equal test output across different requests is 75.76%, 51.00%, and 47.56% for CodeContests, APPS, and HumanEval, respectively. In addition, we find that setting the temperature to 0 does not guarantee determinism in code generation, although it indeed brings less non-determinism than the default configuration (temperature=1). These results confirm that there is, currently, a significant threat to scientific conclusion validity. In order to put LLM-based research on firmer scientific foundations, researchers need to take into account non-determinism in drawing their conclusions.
研究动机与目标
- 在多个基准上量化 ChatGPT 代码生成的非确定性程度。
- 评估温度如何影响代码生成的非确定性。
- 比较不同提示配置(多请求 vs. 单请求)下的非确定性。
- 探索编码任务特征(例如指令长度)与非确定性之间的相关性。
- 为研究人员在实证软件工程研究中考虑非确定性提供指南。
提出的方法
- 使用 ChatGPT(gpt-3.5-turbo 和 gpt-4)在三个基准(CodeContests、APPS、HumanEval)中为每个编码任务生成五个代码候选。
- 以 Markdown 格式对 Python3 代码进行提示,以从 API 响应中提取代码。
- 通过测试通过率、输出等价率(OER)以及排除异常的 OER 来评估语义。
- 使用 LCS 和 Levenshtein 编辑距离衡量代码候选之间的句法相似性。
- 使用基于 AST 的方法(pycode_similar,结合 Unified_Diff 与 Tree_Diff)测量结构相似性。
- 研究温度(0、1、2)的影响,并比较 GPT-3.5 与 GPT-4。

实验结果
研究问题
- RQ1RQ1: 在语义、句法和结构视角下,ChatGPT 的代码生成有多非确定性?
- RQ2RQ2: 温度如何影响代码生成的非确定性程度?
- RQ3RQ3: 来自不同请求的五个候选与来自一个请求的五个 top-1 候选之间的非确定性有何差异?
- RQ4RQ4: 编码任务特征(例如指令长度、难度)与非确定性指标之间存在哪些相关性?
- RQ5RQ5: GPT-4 的非确定性与 GPT-3.5 的在代码生成中相比如何?
主要发现
- 观察到高度非确定性威胁:在 CodeContests、APPS、HumanEval 的任务中,前五个候选中没有一个相同的测试输出,比例分别为 72.73%、60.40%、65.85%。
- 在所有数据集,测试通过率的最大差异达到 1.00,且有 48.17% 的 HumanEval 问题显示了这个最大差异。
- 零温度并不保证确定性;然而,温度 0 比温度 1 更具确定性,而温度 2 往往导致代码普遍更差。
- 更长的编码指令与句法相似性、结构相似性以及平均正确性呈负相关。
- 在调查的文献中,只有 21.1%–22.4% 的基于 LLM 的代码生成研究在实验中考虑非确定性。
- 结构相似性在候选之间仍相对较高(United_Diff: CodeContests 0.46, APPS 0.52, HumanEval 0.69;Tree_Diff: CodeContests 0.60, APPS 0.68, HumanEval 0.72)。

更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。