A Prompt Learning Framework for Source Code Summarization

📄 arXiv: 2312.16066v2 📥 PDF

作者: Tingting Xu, Yun Miao, Chunrong Fang, Hanwei Qian, Xia Feng, Zhenpeng Chen, Chong Wang, Jian Zhang, Weisong Sun, Zhenyu Chen, Yang Liu

分类: cs.SE, cs.AI

发布日期: 2023-12-26 (更新: 2024-12-07)

备注: Under review


💡 一句话要点

提出PromptCS框架,通过训练prompt agent为代码摘要生成连续提示,提升大语言模型性能。

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 代码摘要 提示学习 大语言模型 Prompt Agent 连续提示

📋 核心要点

  1. 现有代码摘要方法存在提示工程依赖领域知识、全参数微调成本高、缺乏高效PEFT方法等问题。
  2. PromptCS训练一个prompt agent生成连续提示,引导大语言模型进行代码摘要,无需人工设计离散提示。
  3. 实验表明,PromptCS显著优于指令提示,与全参数微调相当,在小模型上甚至超越,且训练效率更高。

📝 摘要(中文)

本文提出了一种有效的代码摘要提示学习框架PromptCS,旨在解决现有代码摘要方法中提示工程需要专业知识、全参数微调成本高昂以及缺乏高效的参数高效微调(PEFT)方法的问题。PromptCS不再需要人工设计提示,而是训练一个提示代理来生成连续提示,从而释放大语言模型在代码摘要任务中的潜力。与人工编写的离散提示相比,连续提示是在大语言模型的指导下生成的,因此更容易被大语言模型理解。PromptCS对大语言模型是非侵入式的,并在训练提示代理时冻结大语言模型的参数,从而大大降低了对训练资源的要求。实验结果表明,PromptCS在所有四个广泛使用的指标上都显著优于指令提示方案(包括零样本学习和少样本学习),并且与面向任务的微调方案相当。在某些基础大语言模型(例如,StarCoderBase-1B和-3B)中,PromptCS甚至优于面向任务的微调方案。更重要的是,PromptCS的训练效率比面向任务的微调方案更快,并且在更大的大语言模型上具有更明显的优势。

🔬 方法详解

问题定义:代码摘要旨在为给定的代码片段自动生成自然语言摘要(也称为注释)。现有方法,如指令提示,需要人工设计提示,依赖专业领域知识;全参数微调成本高昂;参数高效微调(PEFT)方法在代码摘要任务上效果不佳或缺乏针对性优化。

核心思路:核心思路是训练一个prompt agent,使其能够自动生成连续的提示(continuous prompts)。这些连续提示能够更好地被大语言模型理解,从而提升代码摘要的质量。与人工设计的离散提示相比,连续提示由模型生成,更贴合模型的内在表示。

技术框架:PromptCS框架包含一个Prompt Agent和一个预训练的大语言模型(LLM)。Prompt Agent负责生成连续提示,LLM则利用这些提示生成代码摘要。训练过程中,LLM的参数被冻结,只训练Prompt Agent。整体流程是:输入代码片段,Prompt Agent生成连续提示,LLM结合代码片段和连续提示生成摘要。

关键创新:关键创新在于使用Prompt Agent自动生成连续提示,避免了人工设计提示的繁琐和对领域知识的依赖。此外,冻结LLM参数,只训练Prompt Agent,显著降低了训练成本。连续提示的设计更易于被LLM理解,从而提升了摘要质量。

关键设计:Prompt Agent的具体实现细节未知,但可以推测其可能是一个小型神经网络,输入是代码片段的某种表示(例如,代码的token embedding),输出是连续的提示向量。损失函数的设计目标是使生成的摘要尽可能接近人工编写的摘要。具体的参数设置、网络结构和损失函数细节在论文中可能有所描述,但摘要中未提及。

📊 实验亮点

实验结果表明,PromptCS在代码摘要任务上显著优于指令提示方法,包括零样本和少样本学习。在StarCoderBase-1B和-3B等模型上,PromptCS甚至超越了全参数微调方法。更重要的是,PromptCS的训练效率更高,尤其是在更大的大语言模型上,训练速度优势更加明显。

🎯 应用场景

PromptCS可应用于软件开发领域,自动生成代码注释,提高代码可读性和可维护性,降低开发人员理解代码的成本。该技术还可用于代码文档自动生成、代码搜索和代码推荐等场景,提升软件开发的效率和质量。未来,该方法可以扩展到其他自然语言生成任务中。

📄 摘要(原文)

(Source) code summarization is the task of automatically generating natural language summaries (also called comments) for given code snippets. 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, task-oriented (full-parameter) fine-tuning, and parameter-efficient fine-tuning (PEFT). However, instruction prompting involves designing crafted prompts and requires users to have professional domain knowledge, while task-oriented fine-tuning requires high training costs, and effective, tailored PEFT methods for code summarization are still lacking. This paper proposes an effective prompt learning framework for code summarization called PromptCS. It no longer requires users to rack their brains to design effective prompts. Instead, 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 is non-invasive to LLMs and freezes the parameters of LLMs when training the prompt agent, which can greatly reduce the requirements for training resources. Our comprehensive experimental results show that PromptCS significantly outperforms instruction prompting schemes (including zero-shot learning and few-shot learning) on all four widely used metrics, and is comparable to the task-oriented fine-tuning scheme. In some base LLMs, e.g., 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.