AdaFuse: An Adaptive Retrieval-and-Generation Framework for Code Summarization
摘要
Code summarization aims to automatically generate concise natural language descriptions to assist developers in understanding and maintaining source code. Existing retrieval-augmented approaches often reuse summaries from similar code snippets to improve the accuracy and relevance of generated content. However, their effectiveness heavily depends on the quality and diversity of the retrieval corpus. When retrieved summaries are semantically misaligned with the input code, the generation process may be misled, resulting in inaccurate outputs. To address this issue, we propose an adaptive retrieval-and-generation framework. First, a generation module based on an encoder-decoder architecture models the mapping between source code and natural language. Then, a semantic similarity-driven summary selection module retrieves reference summaries from the corpus. In cases where retrieved summaries exhibit low semantic relevance to the input code, the content generated by an LLM is incorporated to overcome the limitations of the retrieval corpus. To ensure effective integration of retrieved and generated content, a high-similarity sparse attention mechanism is introduced during the fusion stage, along with a learnable copy gate, to extract key information highly relevant to the generation context. Experimental results show that the proposed method significantly improves generated summaries’ semantic accuracy and relevance on multiple datasets.