Skip to main content
QUICK REVIEW

[Paper Review] What does Transformer learn about source code?

Kechi Zhang, Ge Li|arXiv (Cornell University)|Jul 18, 2022
Software Engineering ResearchComputer Science6 citations
TL;DR

This paper introduces the aggregated attention score and aggregated attention graph to automatically extract structural information—such as syntax, control flow, and data flow—from pre-trained transformer models like CodeBERT and GraphCodeBert. The method enables effective, rule-free construction of program representation graphs that match or exceed manually designed graphs in performance on the Variable Misuse task, demonstrating that transformers learn rich, semantically meaningful structural patterns in source code.

ABSTRACT

In the field of source code processing, the transformer-based representation models have shown great powerfulness and have achieved state-of-the-art (SOTA) performance in many tasks. Although the transformer models process the sequential source code, pieces of evidence show that they may capture the structural information (\eg, in the syntax tree, data flow, control flow, \etc) as well. We propose the aggregated attention score, a method to investigate the structural information learned by the transformer. We also put forward the aggregated attention graph, a new way to extract program graphs from the pre-trained models automatically. We measure our methods from multiple perspectives. Furthermore, based on our empirical findings, we use the automatically extracted graphs to replace those ingenious manual designed graphs in the Variable Misuse task. Experimental results show that the semantic graphs we extracted automatically are greatly meaningful and effective, which provide a new perspective for us to understand and use the information contained in the model.

Motivation & Objective

  • To investigate what structural information transformers learn about source code, especially beyond linear sequence modeling.
  • To address the lack of effective interpretation methods for structural patterns in code-specific transformer models.
  • To develop an automated, data-driven method to extract program representation graphs from attention mechanisms without relying on handcrafted rules.
  • To evaluate whether automatically extracted graphs can match or outperform manually designed graphs in downstream code intelligence tasks.

Proposed method

  • Propose the aggregated attention score as a method to quantify and aggregate attention weights across all heads and layers to reveal structural dependencies in source code.
  • Use the Chu-Liu/Edmonds (CLE) algorithm to transform aggregated attention scores into a directed acyclic graph (DAG), forming the aggregated attention graph.
  • Apply the method to pre-trained models such as CodeBERT and GraphCodeBert to extract semantic and structural relationships from attention patterns.
  • Measure graph quality using quantitative metrics like precision, recall, and F1 on dependency parsing benchmarks.
  • Replace manually designed graphs in the Variable Misuse task with automatically extracted aggregated attention graphs and evaluate performance using standard metrics.

Experimental results

Research questions

  • RQ1What kind of structural information (e.g., syntax, control/data flow) do transformer-based models learn about source code?
  • RQ2Can attention mechanisms in pre-trained models like CodeBERT and GraphCodeBert implicitly encode program structure without explicit structural pre-training?
  • RQ3How effective is the aggregated attention graph in capturing meaningful program relationships compared to manually designed program graphs?
  • RQ4Can automatically extracted graphs from attention scores achieve competitive performance in downstream code intelligence tasks?

Key findings

  • The aggregated attention score successfully captures syntactic and semantic dependencies in source code, such as if-else and variable-use relationships.
  • The aggregated attention graph extracted via the CLE algorithm maintains strong structural fidelity and achieves comparable performance to manually designed graphs on the Variable Misuse task.
  • On the VarMisuse task, graphs extracted from CodeBERT achieved 77.33% location and representation accuracy, and from GraphCodeBert, 77.81%, matching or exceeding baseline performance.
  • The method outperforms sequence-based and origin-based graph baselines, demonstrating that attention mechanisms encode rich, task-relevant structural information.
  • The results validate that transformers learn and retain complex program structures, even when not explicitly pre-trained on such features.
  • The approach provides a new, scalable, and interpretable way to extract and utilize structural knowledge from pre-trained code models without relying on external tools or rule-based systems.

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.