(Source) code summarization is the task of automatically generating natural language summaries for given code snippets. Such summaries play a key role in helping developers understand and maintain source code. Recently, with the successful application of large language models (LLMs) in numerous fields, software engineering researchers have also attempted to adapt LLMs to solve code summarization tasks. The main adaptation schemes include instruction prompting and task-oriented fine-tuning. However, instruction prompting involves designing crafted prompts for zero-shot learning or selecting appropriate samples for few-shot learning and requires users to have professional domain knowledge, while task-oriented fine-tuning requires high training costs. In this paper, we propose a novel prompt learning framework for code summarization called PromptCS. PromptCS trains a prompt agent that can generate continuous prompts to unleash the potential for LLMs in code summarization. Compared to the human-written discrete prompt, the continuous prompts are produced under the guidance of LLMs and are therefore easier to understand by LLMs. PromptCS freezes the parameters of LLMs when training the prompt agent, which can greatly reduce the requirements for training resources. We evaluate PromptCS on the CodeSearchNet dataset involving multiple programming languages. The results show that PromptCS significantly outperforms instruction prompting schemes on all four widely used metrics. In some base LLMs, e.g., CodeGen-Multi-2B and StarCoderBase-1B and -3B, PromptCS even outperforms the task-oriented fine-tuning scheme. More importantly, the training efficiency of PromptCS is faster than the task-oriented fine-tuning scheme, with a more pronounced advantage on larger LLMs. The results of the human evaluation demonstrate that PromptCS can generate more good summaries compared to baselines.
翻译:(源)代码摘要是为给定代码片段自动生成自然语言摘要的任务。此类摘要在帮助开发者理解和维护源代码方面发挥关键作用。近年来,随着大语言模型(LLMs)在众多领域的成功应用,软件工程研究者也开始尝试将LLMs适配到代码摘要任务中。主要的适配方案包括指令提示和面向任务的微调方法。然而,指令提示方案需要为零样本学习设计精心构建的提示,或为少样本学习选择合适样本,这要求用户具备专业领域知识;而面向任务的微调方案则需要高昂的训练成本。本文提出一种新颖的面向代码摘要的提示学习框架PromptCS。PromptCS训练一个能够生成连续提示的提示代理,以释放LLMs在代码摘要任务中的潜力。与人工编写的离散提示相比,连续提示是在LLMs指导下生成的,因此更易于被LLMs理解。PromptCS在训练提示代理时冻结了LLMs的参数,这能够大幅降低对训练资源的需求。我们在涵盖多种编程语言的CodeSearchNet数据集上评估了PromptCS。结果表明,PromptCS在四个广泛使用的指标上显著优于指令提示方案。在某些基础LLMs(如CodeGen-Multi-2B、StarCoderBase-1B和-3B)上,PromptCS甚至优于面向任务的微调方案。更重要的是,PromptCS的训练效率高于面向任务的微调方案,且在更大规模的LLMs上优势更为明显。人工评估结果表明,与基线方法相比,PromptCS能生成更优质的摘要。