[论文解读] Guiding Language Models of Code with Global Context using Monitors
本文提出监控引导解码(MGD),一种通过监控集成静态分析以在代码生成过程中提供全局上下文的代码语言模型增强方法。通过使用仓库级别的类型和语义分析,利用logit掩码引导解码,MGD提升了编译成功率和与真实标签的一致性,使较小的模型(如SantaCoder-1.1B)能够超越更大的模型(如text-davinci-003)。
Language models of code (LMs) work well when the surrounding code provides sufficient context. This is not true when it becomes necessary to use types, functionality or APIs defined elsewhere in the repository or a linked library, especially those not seen during training. LMs suffer from limited awareness of such global context and end up hallucinating. Integrated development environments (IDEs) assist developers in understanding repository context using static analysis. We extend this assistance, enjoyed by developers, to LMs. We propose monitor-guided decoding (MGD) where a monitor uses static analysis to guide the decoding. We construct a repository-level dataset PragmaticCode for method-completion in Java and evaluate MGD on it. On models of varying parameter scale, by monitoring for type-consistent object dereferences, MGD consistently improves compilation rates and agreement with ground truth. Further, LMs with fewer parameters, when augmented with MGD, can outperform larger LMs. With MGD, SantaCoder-1.1B achieves better compilation rate and next-identifier match than the much larger text-davinci-003 model. We also conduct a generalizability study to evaluate the ability of MGD to generalize to multiple programming languages (Java, C# and Rust), coding scenarios (e.g., correct number of arguments to method calls), and to enforce richer semantic constraints (e.g., stateful API protocols). Our data and implementation are available at https://github.com/microsoft/monitors4codegen .
研究动机与目标
- 解决代码语言模型在缺少全局上下文(如其他文件或库中的类型)时幻觉化标识符和类型的问题。
- 将开发者在IDE中使用的静态分析优势扩展至代码生成模型,无需重新训练。
- 设计一种模块化、推理时的方法,通过仓库级别的分析强制执行类型和语义一致性,以提升代码生成质量。
- 评估在多种编程语言(Java、C#、Rust)、编码任务(如方法参数数量)和语义约束(如API协议)下的泛化能力。
- 证明经过MGD增强的小型模型在代码生成质量上可超越更大但未经调优的模型。
提出的方法
- 提出监控引导解码(MGD),一种在解码阶段增强的方法,通过logit掩码利用静态分析引导标记生成。
- 将监控定义为有状态接口,用于观察生成的代码,并在触发点(如对象解引用)查询静态分析结果。
- 将静态分析结果转换为标记掩码,以在解码过程中重塑语言模型的下一个标记概率分布。
- 使用语言服务器协议(LSP)访问完整仓库及依赖项(包括Lombok和Protobuf等构建产物)中的类型和语义信息。
- 构建一个新的数据集PragmaticCode,用于Java方法补全任务,包含真实类型一致的补全结果和全局上下文标注。
- 在不改变模型架构或进行微调的前提下,将MGD集成至现有代码LM(如SantaCoder、text-davinci-003)中。
实验结果
研究问题
- RQ1MGD能否通过引入来自静态分析的全局类型和语义上下文,提升代码生成的准确性和编译成功率?
- RQ2经过MGD增强的小型代码LM是否能在代码生成质量上超越更大但未经调优的模型?
- RQ3MGD是否能在多种编程语言(Java、C#、Rust)和编码场景(如正确参数数量、API协议合规)中实现泛化?
- RQ4MGD在减少与错误方法名、字段访问或返回类型相关的幻觉方面效果如何?
- RQ5MGD能否与其它提示技术(如FIM、RLPG)有效结合,进一步提升与真实标签的匹配率?
主要发现
- SantaCoder-1.1B在使用MGD后,其编译成功率和下一个标识符匹配率均优于参数量大得多的text-davinci-003模型。
- 在PragmaticCode数据集上,MGD在不同参数规模的模型中均持续提升了编译成功率和与真实标签的一致性。
- MGD有效减少了标识符生成中的幻觉现象——例如,将错误的名称如'host'和'port'替换为正确的名称如'describe'和'of'。
- 在模型生成可编译但错误的代码时,若结合FIM或RLPG等提示增强技术,MGD可实现与真实标签的完全一致。
- MGD在C#和Rust中也表现出良好的泛化能力,有效提升了在参数数量不匹配和状态化API协议强制执行等场景下的正确性。
- 该方法使小型模型能够通过获取原本在本地自回归解码中不可用的仓库级上下文,超越大型模型。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。