Code summarization aims to generate natural language descriptions of source code, facilitating programmers to understand and maintain it rapidly. While previous code summarization efforts have predominantly focused on method-level, this paper studies file-level code summarization, which can assist programmers in understanding and maintaining large source code projects. Unlike method-level code summarization,file-level code summarization typically involves long source code within a single file, which makes it challenging for Transformer-based models to understand the code semantics for the maximum input length of these models is difficult to set to a large number that can handle long code input well, due to the quadratic scaling of computational complexity with the input sequence length. To address this challenge, we propose SparseCoder, an identifier-aware sparse transformer for effectively handling long code sequences. Specifically, the SparseCoder employs a sliding window mechanism for self-attention to model short-term dependencies and leverages the structure message of code to capture long-term dependencies among source code identifiers by introducing two types of sparse attention patterns named global and identifier attention. To evaluate the performance of SparseCoder, we construct a new dataset FILE-CS for file-level code summarization in Python. Experimental results show that our SparseCoder model achieves state-of-the-art performance compared with other pre-trained models, including full self-attention and sparse models. Additionally, our model has low memory overhead and achieves comparable performance with models using full self-attention mechanism.
翻译:代码摘要旨在生成源代码的自然语言描述,帮助程序员快速理解和维护代码。尽管以往的代码摘要研究大多聚焦于方法级别,但本文研究的是文件级代码摘要,这有助于程序员理解和维护大型源代码项目。与方法级代码摘要不同,文件级代码摘要通常涉及单个文件中的长代码片段,这使得基于Transformer的模型难以理解代码语义,因为这类模型的最大输入长度难以设定为能良好处理长代码输入的大数值——其计算复杂度随输入序列长度呈二次方增长。为解决这一挑战,我们提出SparseCoder——一种能够有效处理长代码序列的标识符感知稀疏Transformer。具体而言,SparseCoder采用滑动窗口机制进行自注意力建模,以捕捉短期依赖关系,同时利用代码的结构信息,通过引入两种稀疏注意力模式(全局注意力和标识符注意力)来捕获源代码标识符间的长期依赖关系。为评估SparseCoder的性能,我们构建了一个新的数据集FILE-CS,专用于Python语言的代码文件级摘要任务。实验结果表明,与其他预训练模型(包括全自注意力模型和稀疏模型)相比,我们的SparseCoder模型达到了最先进的性能。此外,该模型内存开销较低,且性能可与使用全自注意力机制的模型相媲美。