[论文解读] Synchromesh: Reliable code generation from pre-trained language models
Synchromesh 引入 Target Similarity Tuning (TST) 用于选择语义相关的少量示例,以及 Constrained Semantic Decoding (CSD) 来在代码生成过程中强制执行语言特定约束,从而在不进行微调的情况下提升 GPT-3、Codex 在 SQL、Vega-Lite 和 SMCalFlow 等领域的可靠性。
Large pre-trained language models have been used to generate code,providing a flexible interface for synthesizing programs from natural language specifications. However, they often violate syntactic and semantic rules of their output language, limiting their practical usability. In this paper, we propose Synchromesh: a framework for substantially improving the reliability of pre-trained models for code generation. Synchromesh comprises two components. First, it retrieves few-shot examples from a training bank using Target Similarity Tuning (TST), a novel method for semantic example selection. TST learns to recognize utterances that describe similar target programs despite differences in surface natural language features. Then, Synchromesh feeds the examples to a pre-trained language model and samples programs using Constrained Semantic Decoding (CSD): a general framework for constraining the output to a set of valid programs in the target language. CSD leverages constraints on partial outputs to sample complete correct programs, and needs neither re-training nor fine-tuning of the language model. We evaluate our methods by synthesizing code from natural language descriptions using GPT-3 and Codex in three real-world languages: SQL queries, Vega-Lite visualizations and SMCalFlow programs. These domains showcase rich constraints that CSD is able to enforce, including syntax, scope, typing rules, and contextual logic. We observe substantial complementary gains from CSD and TST in prediction accuracy and in effectively preventing run-time errors.
研究动机与目标
- 动机并解决大型语言模型在从自然语言描述生成语法上和语义上有效代码时的失败问题。
- 提出 Target Similarity Tuning (TST),以选择与预期程序结构对齐的语义相关的少量示例。
- 提出 Constrained Semantic Decoding (CSD),在解码过程中在不重新训练模型的情况下强制执行丰富的语言特定约束。
- 在实际语言(SQL、Vega-Lite、SMCalFlow)上演示该框架,展示在准确性和有效性方面的改进。
提出的方法
- 开发 Target Similarity Tuning (TST):对相似性模型进行微调,以从描述中预测目标程序之间的相似性,优化程序结构相似性(基于 AST 树编辑距离)。
- 引入 Completion Engines (CEs) 作为对目标语言的句法和语义约束进行编码的抽象,并推导有效的下一个标记完成。
- 形式化 Constrained Semantic Decoding (CSD):从保持部分程序在语言完成集内的标记集合中抽取下一个标记,使用 Brzozowski 导数来确定部分程序的前缀闭包成员资格。
- 从语法推导完成:使用 ANTLR 推导的解析器状态枚举允许的下一个标记,并在 CE 中构建上下文无关和上下文相关的约束层。
- 提供解码过程:为语言的前缀闭包 L^c 构建一个判定过程,然后将 LLM 的标记采样限制为 V_M(s) = {t | st ∈ L^c},确保生成的程序满足约束。
实验结果
研究问题
- RQ1动态的、具语义感知的少量示例选择(TST)是否能改善用户话述与目标程序之间的语义对齐?
- RQ2一个以约束为驱动的解码框架(CSD)是否能够在不重新训练 LLM 的情况下强制实现语法、作用域、类型和领域特定语义,以减少运行时和语义错误?
- RQ3在使用 GPT-3 和 Codex 时,TST 和 CSD 是否在多种真实世界目标语言(SQL、Vega-Lite、SMCalFlow)上提供互补的增益?
- RQ4与 generate-then-test 方法相比,受约束解码对语言生成代码的可靠性与准确性提升程度如何?
主要发现
- TST 通过基于目标程序相似性而非表层语言相似性来检索语义相关的示例,显著提升 GPT-3 和 Codex 的性能。
- CSD 在解码过程中强制执行约束,显著提升输出的有效性并降低三个领域(SQL、Vega-Lite、SMCalFlow)的运行时错误。
- 结合 TST 和 CSD 能获得最佳结果,具有互补优势:TST 引导朝向结构上相似的目标,而 CSD 保证满足约束的完成。
- CSD 在采样阶段增加了适度的开销(约 8%),但显著提高了有效性和执行成功率,特别是对于较长的程序。
- 在模型和领域之间,系统增强的结果接近有监督基线,并优于 generate-then-test 方法。
- 较长的程序最能从系统获益,准确性下降减慢且有效性相对于基线保持较高。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。