Abstract
In the field of source code processing, transformer-based representation models have shown great powerfulness and have achieved state-of-the-art (SOTA) performance in many tasks. Although transformer models process sequential source code, evidence shows they may capture structural information (e.g., syntax trees, data flow, control flow) as well. We propose the aggregated attention score to investigate the structural information learned by the transformer and the aggregated attention graph to automatically extract program graphs from pre-trained models. Our experimental results show that these automatically extracted semantic graphs are highly effective, performing nearly as well as manually designed graphs in software engineering tasks like Variable Misuse detection.
Introduction
While Transformers have achieved state-of-the-art performance in code-related tasks, they are often treated as "black boxes." Unlike natural language, source code is governed by strict, formal rules of syntax (Abstract Syntax Trees) and logic (Control/Data Flow). Traditionally, researchers have manually designed complex graphs to represent these structures to help models "understand" code. This research asks a fundamental question: Does the Transformer learn these structures on its own during pre-training? If so, can we "harvest" them directly from the model’s internal attention mechanisms instead of building them by hand?
Methodology: Extracting the Program Graph
The authors propose two primary tools to analyze the model's internal structural knowledge:
- Aggregated Attention Score: Instead of looking at a single attention head, they calculate the maximum attention score across all heads in a specific layer. This provides a unified view of which tokens the model considers most "connected."
- Aggregated Attention Graph: To turn these scores into a usable structure, they treat the attention scores as weights in a dense graph and apply the Chu-Liu/Edmonds (CLE) algorithm. This algorithm finds the "Maximum Spanning Tree," effectively pruning noise and leaving behind a sparse, hierarchical skeleton of the code.
By analyzing these graphs across different layers and comparing them to "ground truth" compiler-generated syntax trees, the researchers can measure exactly how much structural knowledge the model has acquired.
Key Insights and Ideas
- From Linear to Logical: In early layers (e.g., Layer 1), attention is primarily "diagonal," focusing on immediate neighbors in a linear sequence. As the data progresses to deeper layers (e.g., Layer 11), the patterns become "heterogeneous," focusing on semantically related tokens that might be far apart in the text but close in logic.
- Fine-tuning as a Specialist: While a base model like CodeBERT learns general programming patterns, fine-tuning it on a specific language (like Python) dramatically increases its sensitivity to language-specific structures, such as the relationship between a function's
defand itsreturnstatement. - Head Specialization: Specific attention heads specialize in different types of relations. Some heads focus on "data flow" (tracking variable values), while others focus on "code structure" (the skeleton of the function).
Experimental Results
The authors tested their automatically extracted graphs against manually designed ones in the Variable Misuse (VarMisuse) task using the ETH Py150 dataset. The model must identify if a wrong variable was used and suggest the correct one.
| Graph Type | Joint Localization & Repair Accuracy |
|---|---|
| Manual (Origin) Graphs | 78.18% |
| Extracted (Ours - GraphCodeBERT) | 77.81% |
| Simple Sequence Baseline | 72.12% |
The fact that the automatically extracted graphs performed within 0.37% of the manual ones—despite requiring no human-defined rules—is a significant win for model interpretability and automated tool building.
Limitations and Future Work
While promising, the authors note that models still struggle to capture very complex control flows (like intricate branching logic) as well as a dedicated compiler can. Additionally, the process of fine-tuning can sometimes "wash out" certain pre-trained structural advantages if the downstream task doesn't explicitly require them. Future work will explore how to better utilize these "implicit" graphs to build more efficient code intelligence tools that don't rely on external heavy-duty parsers.
Summary updated