Abstract
Code summarization generates brief natural language description given a source code snippet, while code retrieval fetches relevant source code given a natural language query. Since both tasks aim to model the association between natural language and programming language, recent studies have combined these two tasks to improve their performance. However, researchers have yet been able to effectively leverage the intrinsic connection between the two tasks as they train these tasks in a separate or pipeline manner, which means their performance can not be well balanced. In this paper, we propose a novel end-to-end model for the two tasks by introducing an additional code generation task. More specifically, we explicitly exploit the probabilistic correlation between code summarization and code generation with dual learning, and utilize the two encoders for code summarization and code generation to train the code retrieval task via multi-task learning. We have carried out extensive experiments on an existing dataset of SQL and Python, and results show that our model can significantly improve the results of the code retrieval task over the-state-of-art models, as well as achieve competitive performance in terms of BLEU score for the code summarization task.
Introduction
Software developers spend a significant portion of their time searching for relevant code snippets and attempting to understand existing codebases. These activities correspond to the tasks of code retrieval and code summarization, respectively. Fundamentally, these tasks are "two sides of the same coin," as they both involve mapping between natural language (NL) and programming language (PL).
Despite this intrinsic connection, most existing research treats these tasks in isolation or via a pipeline approach. In pipeline models, a summary is generated first and then used as a query for retrieval. This often creates a conflict: to optimize retrieval performance, models may generate "retrieval-friendly" summaries that consist of keyword jumbles, sacrificing human readability. The challenge lies in leveraging the connection between NL and PL such that both tasks benefit without compromising the quality of either.
Methodology
The proposed framework, CO3, addresses these challenges by simultaneously learning three related tasks: code summarization, code retrieval, and code generation.
The CO3 Architecture
The model utilizes a shared architecture where encoders for summarization and generation feed into a search scorer for retrieval. This design forces the model to learn a unified semantic space where code and text vectors are closely aligned.
Dual and Multi-Task Learning
- The Dual Learning Mechanism: CO3 treats code summarization ($Code \to NL$) and code generation ($NL \to Code$) as dual tasks. Following the principle of probabilistic duality, the joint probability of a code-description pair should remain consistent regardless of the direction of translation. A dual learning regularization term is introduced in the loss function to enforce this consistency.
- Multi-Task Learning: By training on all three tasks simultaneously, CO3 avoids the error propagation common in pipeline models. The model uses continuous hidden states (vectors) rather than discrete words to pass information to the retrieval module, preserving rich semantic details.
- Parameter Sharing: To maintain efficiency, the model shares LSTM-cell parameters between the code encoder/decoder and the query encoder/decoder. This reduces the parameter count while ensuring a deep understanding of both language structures.
Experimental Setup
The model was evaluated using the StaQC dataset, a large collection of <question title, code> pairs mined from Stack Overflow.
- SQL: 119,519 pairs.
- Python: 147,546 pairs. The data was partitioned into training (75%), validation (10%), and testing (15%) sets.
Results and Discussion
CO3 demonstrated significant improvements over state-of-the-art (SOTA) baselines, including DCS and CoaCor.
Code Retrieval Performance
As shown in the table below, CO3 consistently outperformed existing models in retrieval metrics. On the Python dataset, CO3 achieved a Mean Reciprocal Rank (MRR) of 0.682, a notable improvement over CoaCor (0.636) and DCS (0.617).
Code Summarization and Ablation
In terms of summarization, CO3 more than doubled the performance of CoaCor in human readability, achieving a BLEU4 score of 0.124 for Python. Ablation studies confirmed that the dual learning component and the inclusion of the code generation task are critical; removing either led to a measurable drop in performance.
A key insight from the results is the positive correlation between summarization quality (BLEU) and retrieval quality (MRR). This suggests that improving human readability does not necessarily come at the cost of search accuracy, contradicting previous assumptions about the trade-offs in pipeline models.
Conclusion
The CO3 framework successfully leverages the dual nature of code summarization and generation to enhance code retrieval. By using a unified end-to-end architecture with dual learning regularization, the model achieves superior retrieval results while maintaining high-quality, readable summaries. Future work will focus on optimizing the training overhead and testing the model on more verbose programming languages like Java or C++.
Summary updated