- A Structural Analysis of Pre-Trained Language Models for Source Code
Abstract
Pre-trained models like CodeBERT have revolutionized "code intelligence," yet they are often viewed as black boxes. This study performs a structural analysis to determine if these models implicitly learn the hierarchical syntax of programming languages—specifically the Abstract Syntax Tree (AST) structure. By analyzing internal attention and embeddings, we demonstrate that middle and deeper layers capture complex structural information, allowing for the unsupervised reconstruction of program syntax.
Introduction
In the field of Software Engineering, pre-trained language models (PLMs) have become indispensable tools for tasks like code completion and summarization. However, unlike the extensive "BERTology" research in Natural Language Processing, the interpretability of code-centric models remains under-explored. Code is not merely a sequence of tokens; it possesses a rigid, nested structure defined by formal grammars. This paper addresses whether PLMs capture these underlying structural properties or simply excel at pattern matching text.
*[What Do They Capture?
- A Structural Analysis of Pre-Trained Language Models for Source Code, page 2](/pdf/art_c4d8bad6c5bd4113aa10e439e061ca7a#page=2)*
Methodology
The authors employ three analytical lenses to evaluate models like CodeBERT and GraphCodeBERT:
- Attention Analysis: Examining self-attention weights to see if high-confidence heads align with AST "motif structures" (parent-child relationships).
- Structural Probing: Training a linear probe to determine if Euclidean distances in the embedding space correlate with distances in the AST.
- Syntax Tree Induction: Attempting to reconstruct the entire syntax tree from internal model distances using a greedy top-down algorithm.
The study utilizes the CodeSearchNet dataset, focusing on Python, Java, and PHP, with detailed analysis performed on a subset of 5,000 code snippets.
Key Insights and Structural Geometry
The analysis reveals that structural "understanding" peaks in the middle and deeper layers of the models. Specifically, layer 5 for embeddings and layer 11 for attention show the strongest alignment with AST structures.
*[What Do They Capture?
- A Structural Analysis of Pre-Trained Language Models for Source Code, page 3](/pdf/art_c4d8bad6c5bd4113aa10e439e061ca7a#page=3)*
The success of linear probing suggests that the geometry of the model's internal vector space is naturally tree-like. Furthermore, attention heads frequently connect tokens sharing a parent in the AST, effectively grouping logical blocks of code.
Results
- Attention Alignment: Layer 11 attention heads in CodeBERT showed up to 67.25% alignment with AST motifs.
- Probing Correlation: Structural probes achieved a Spearman correlation of 0.85 to 0.86 in the 5th layer, indicating strong preservation of hierarchy.
- Tree Induction: Unsupervised reconstruction achieved F1 scores of 45.37 to 51.34, vastly outperforming random (16 F1) or simple branching (18-26 F1) baselines.
- GraphCodeBERT: Models pre-trained with explicit data-flow information generally outperformed standard CodeBERT, suggesting that structural signals during pre-training are highly beneficial.
Conclusion
This research confirms that PLMs for source code implicitly capture hierarchical syntax. While limited to ASTs and BERT architectures, the findings suggest that the internal representations of these models are fundamentally shaped by the formal structures of the code they process. Future work may extend this analysis to control-flow graphs and larger, more modern architectures.
Summary updated