Skip to main content
QUICK REVIEW

[论文解读] SkCoder: A Sketch-based Approach for Automatic Code Generation

Jia Li, Yongmin Li|arXiv (Cornell University)|Feb 13, 2023
Software Engineering Research被引用 5
一句话总结

SkCoder 是一种基于草图的代码生成框架,通过模仿人类开发者复用代码的行为来提升自动代码生成性能:它检索一个相似的代码片段,提取其中相关的代码草图(可复用的模式),并根据自然语言需求对其进行修改。该方法在多个模型和数据集上,精确匹配指标上相比SOTA模型最高提升30.30%,Pass@1指标最高提升120.1%。

ABSTRACT

Recently, deep learning techniques have shown great success in automatic code generation. Inspired by the code reuse, some researchers propose copy-based approaches that can copy the content from similar code snippets to obtain better performance. Practically, human developers recognize the content in the similar code that is relevant to their needs, which can be viewed as a code sketch. The sketch is further edited to the desired code. However, existing copy-based approaches ignore the code sketches and tend to repeat the similar code without necessary modifications, which leads to generating wrong results. In this paper, we propose a sketch-based code generation approach named SkCoder to mimic developers' code reuse behavior. Given a natural language requirement, SkCoder retrieves a similar code snippet, extracts relevant parts as a code sketch, and edits the sketch into the desired code. Our motivations are that the extracted sketch provides a well-formed pattern for telling models "how to write". The post-editing further adds requirement-specific details to the sketch and outputs the complete code. We conduct experiments on two public datasets and a new dataset collected by this work. We compare our approach to 20 baselines using 5 widely used metrics. Experimental results show that (1) SkCoder can generate more correct programs, and outperforms the state-of-the-art - CodeT5-base by 30.30%, 35.39%, and 29.62% on three datasets. (2) Our approach is effective to multiple code generation models and improves them by up to 120.1% in Pass@1. (3) We investigate three plausible code sketches and discuss the importance of sketches. (4) We manually evaluate the generated code and prove the superiority of our SkCoder in three aspects.

研究动机与目标

  • 解决基于复制的代码生成模型存在的局限性,即重复整个代码片段而未进行必要修改。
  • 建模人类开发者在代码复用时的行为,即仅提取并适配相关部分(代码草图)。
  • 通过结合检索、草图提取和需求感知编辑,提升代码生成的准确性。
  • 在不同模型和数据集上评估代码草图的有效性,包括一个新构建的20万样本Java数据集。
  • 证明基于草图的编辑方法相比直接复制能显著提升代码生成性能。

提出的方法

  • 检索模块使用密集检索方法从语料库中选择语义相似的代码片段。
  • 草图模块根据自然语言需求,从检索到的代码片段中识别并提取出相关代码部分。
  • 提取出的代码草图作为生成目标代码的结构模板。
  • 编辑模块通过插入与需求相关的细节来修改草图,从而生成最终代码。
  • 将该框架集成到多种代码生成模型(如CodeT5、基于GPT的模型)中,以评估其泛化能力。
  • 采用标准指标(EM、BLEU、CodeBLEU、Pass@1、AvgPassRatio)在公开数据集和新收集的数据集上进行评估。

实验结果

研究问题

  • RQ1基于草图的方法是否能在代码生成中超越现有的基于复制的方法?
  • RQ2代码草图的提取效果如何——具体而言,草图的设计选择中哪些能带来最佳性能?
  • RQ3基于草图的编辑机制在不同模型上的代码生成性能提升程度如何?
  • RQ4所提出的方法是否能泛化到多种编程语言和代码生成任务?
  • RQ5引入草图在单位测试评估下对模型鲁棒性和正确性有何影响?

主要发现

  • 在三个数据集上,SkCoder 在精确匹配指标上分别比SOTA模型CodeT5-base提升30.30%、35.39%和29.62%。
  • 在新收集的AixBench-L数据集上,SkCoder 相比SOTA基线模型,Pass@1提升12.9%,AvgPassRatio提升8.49%。
  • 消融实验表明,添加草图模块和检索模块后,Pass@1在不同模型上最高提升120.1%。
  • 评估了三种不同的草图设计选择,结果证实草图的相关性和结构对性能至关重要。
  • 人工评估确认,SkCoder生成的代码在正确性、可读性以及与需求的语义一致性方面均优于基线模型。
  • 该框架展现出强大的泛化能力,可有效提升包括CodeT5、基于GPT以及编码器-解码器架构在内的多种模型性能。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。