Skip to main content
QUICK REVIEW

[Paper Review] What Do They Capture? -- A Structural Analysis of Pre-Trained Language Models for Source Code

Yao Wan, Wei Zhao|arXiv (Cornell University)|Feb 14, 2022
Software Engineering ResearchComputer Science16 citations
TL;DR

This paper conducts a structural analysis of pre-trained code language models (CodeBERT and GraphCodeBERT) through attention alignment, word embedding probing, and syntax tree induction to reveal how they capture code structure. It finds that attention mechanisms align strongly with code syntax, intermediate representations preserve syntactic structure, and models can induce valid ASTs—suggesting that incorporating syntax during pre-training enhances code representation learning.

ABSTRACT

Recently, many pre-trained language models for source code have been proposed to model the context of code and serve as a basis for downstream code intelligence tasks such as code completion, code search, and code summarization. These models leverage masked pre-training and Transformer and have achieved promising results. However, currently there is still little progress regarding interpretability of existing pre-trained code models. It is not clear why these models work and what feature correlations they can capture. In this paper, we conduct a thorough structural analysis aiming to provide an interpretation of pre-trained language models for source code (e.g., CodeBERT, and GraphCodeBERT) from three distinctive perspectives: (1) attention analysis, (2) probing on the word embedding, and (3) syntax tree induction. Through comprehensive analysis, this paper reveals several insightful findings that may inspire future studies: (1) Attention aligns strongly with the syntax structure of code. (2) Pre-training language models of code can preserve the syntax structure of code in the intermediate representations of each Transformer layer. (3) The pre-trained models of code have the ability of inducing syntax trees of code. Theses findings suggest that it may be helpful to incorporate the syntax structure of code into the process of pre-training for better code representations.

Motivation & Objective

  • To understand why pre-trained code language models achieve strong performance in code intelligence tasks.
  • To investigate what structural features these models capture, particularly syntactic structure.
  • To analyze whether attention, word embeddings, and model representations preserve or induce code syntax.
  • To evaluate the capability of pre-trained models to reconstruct abstract syntax trees (ASTs) without explicit training.
  • To provide interpretability insights that can guide future design of code representation models.

Proposed method

  • Conducting self-attention visualization and alignment with AST structures to assess attention's focus on syntactic components.
  • Designing a structural probing method to test whether intermediate representations in each Transformer layer encode syntactic structure.
  • Applying a top-down recursive binary tree induction algorithm to recover ASTs from model representations.
  • Using a right-skewness bias in tree construction to improve alignment with ASTs, adapted from natural language parsing.
  • Evaluating the induced trees against ground-truth ASTs using standard tree similarity metrics.
  • Analyzing models on code snippets from large-scale code repositories to ensure generalization.

Experimental results

Research questions

  • RQ1How well do attention heads in pre-trained code models align with the syntactic structure of source code?
  • RQ2To what extent do the intermediate representations in each Transformer layer preserve syntactic structure?
  • RQ3Can pre-trained code models induce valid abstract syntax trees (ASTs) from code representations without explicit training?
  • RQ4How does incorporating syntax into pre-training (e.g., via GraphCodeBERT) affect structural preservation in representations?
  • RQ5What is the relationship between attention patterns, word embeddings, and syntactic structure in code models?

Key findings

  • Attention heads in pre-trained code models show strong alignment with syntactic constituents, particularly function calls, control structures, and variable declarations.
  • Intermediate representations across all Transformer layers preserve syntactic structure, with deeper layers showing more refined syntactic awareness.
  • Pre-trained code models can induce plausible abstract syntax trees from code embeddings, with F1 scores on tree similarity indicating meaningful structural recovery.
  • Models like GraphCodeBERT, which incorporate data-flow graphs during pre-training, demonstrate enhanced syntactic structure preservation compared to standard CodeBERT.
  • The induced syntax trees are structurally coherent and reflect the hierarchical nature of code, suggesting that syntactic structure is implicitly encoded in the representations.
  • The study reveals that syntactic structure is a key inductive bias captured by pre-trained code models, supporting the integration of syntax into future pre-training objectives.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.