论文标题
同步:从预训练的语言模型中生成可靠的代码
Synchromesh: Reliable code generation from pre-trained language models
论文作者
论文摘要
大型预训练的语言模型已用于生成代码,为从自然语言规范中综合程序提供了灵活的界面。但是,他们经常违反其输出语言的句法和语义规则,从而限制其实际可用性。在本文中,我们提出了同步:一个框架,用于实质上提高预先训练模型的代码生成的可靠性。同步物包括两个组成部分。首先,它使用目标相似性调整(TST)从训练库中检索了几个示例,这是一种新颖的语义示例选择方法。 TST学会了识别尽管表面自然语言特征差异的差异,但可以识别描述类似目标程序的话语。然后,同步使用受限的语义解码(CSD)将示例馈送到预训练的语言模型中,并将程序示例程序:将输出限制为目标语言中一组有效程序的一般框架。 CSD利用部分输出的约束来采样完整的正确程序,并且不需要重新训练,也不需要对语言模型进行微调。我们通过使用GPT-3和Codex中的三种现实世界语言中合成自然语言描述的代码来评估我们的方法:SQL查询,Vega-Lite可视化和SMCALFLOW程序。这些域展示了CSD能够执行的丰富约束,包括语法,范围,打字规则和上下文逻辑。我们在预测准确性方面观察到了CSD和TST的大量互补收益,并有效防止运行时错误。
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.