Abstract
Code summaries help developers comprehend programs and reduce their time to infer the program functionalities during software maintenance. Recent efforts resort to deep learning techniques such as sequence-to-sequence models for generating accurate code summaries, among which Transformer-based approaches have achieved promising performance. However, effectively integrating the code structure information into the Transformer is under-explored in this task domain. In this article, we propose a novel approach named SG-Trans to incorporate code structural properties into Transformer. Specifically, we inject the local symbolic information (e.g., code tokens and statements) and global syntactic structure (e.g., dataflow graph) into the self-attention module of Transformer as inductive bias. To further capture the hierarchical characteristics of code, the local information and global structure are designed to distribute in the attention heads of lower layers and high layers of Transformer. Extensive evaluation shows the superior performance of SG-Trans over the state-of-the-art approaches. Compared with the best-performing baseline, SG-Trans still improves 1.4% and 2.0% on two benchmark datasets, respectively, in terms of METEOR score, a metric widely used for measuring generation quality.
Introduction
Program comprehension is a massive bottleneck in software engineering, often consuming over 50% of a developer's time during maintenance. While natural language summaries (comments) are invaluable for understanding code, they are frequently missing, incomplete, or outdated. Existing automated tools often fall short because they treat source code like plain text—a flat sequence of tokens. Programming languages, however, are inherently structural. While standard Transformers are powerful, they are purely data-driven and may fail to learn critical syntactic and semantic relationships from limited training data without explicit structural guidance.
The SG-Trans Framework
The authors propose SG-Trans (Code Structure Guided Transformer), which modifies the standard Transformer architecture to inject code structural properties as inductive biases.
Structure-Guided Self-Attention
Instead of allowing every token to attend to every other token equally, SG-Trans introduces three specialized attention heads based on the code's underlying structure:
- Token-guided: Restricts attention to sub-tokens that originally belonged to the same code token (e.g., "Is" and "Prime" in
IsPrime). - Statement-guided: Restricts attention to tokens within the same code statement.
- Data flow-guided: Biases attention towards variables that have direct data dependencies, as defined by a Data Flow Graph (DFG).
Research found that Data Flow Graphs (DFGs) provide a more concise and effective signal for global dependencies than full Abstract Syntax Trees (ASTs), which can be redundant and excessively deep.
Hierarchical Structure-Variant Attention (HSVA)
A core innovation of SG-Trans is the distribution of these attention heads across the Transformer's layers. Inspired by the "principle of compositionality"—the idea that complex meanings are built from simpler parts—the authors distribute these heads hierarchically. Lower layers are assigned more local (token and statement) attention heads, while higher layers transition to more global (data flow and standard) attention heads. This mirrors how humans read code, learning local syntax before global semantics. For local structures, the authors found that "hard masks" (completely blocking attention to unrelated tokens) were more effective than the "soft" relative position embeddings used in previous work.
Copy Mechanism
To handle the "out-of-vocabulary" (OOV) problem common in code (like unique variable names), the model includes a pointer-generator network. This mechanism allows the model to choose between generating a token from a fixed vocabulary or copying it directly from the source code.
Evaluation and Results
The model was evaluated on two widely used benchmark datasets: a Java dataset of 87,136 method-comment pairs and a Python dataset of 92,545 function-documentation pairs. The authors performed rigorous de-duplication to ensure performance wasn't inflated by memorizing repeated code snippets.
SG-Trans outperformed all state-of-the-art baselines across multiple metrics:
- Quantitative Performance: On the Java dataset, it improved the METEOR score by 1.4% over the best baseline. On the Python dataset, the improvement was 2.0%.
- Human Evaluation: In a study with professional developers, SG-Trans summaries were rated significantly higher for Adequacy, Conciseness, and Fluency compared to vanilla Transformers.
- Efficiency: Despite the added structural information, SG-Trans maintains lower GPU memory usage and faster training times than many comparable models because it avoids complex graph convolutions.
Conclusion
SG-Trans demonstrates that the placement of structural information is as important as the information itself. By guiding the model to focus on local syntax in lower layers and global data flow in higher layers, it achieves state-of-the-art performance. Future work includes extending this structural guidance to inter-procedural dependencies and other tasks like code generation and bug detection.
Summary updated