[Paper Review] Explainable AI for Pre-Trained Code Models: What Do They Learn? When They Do Not Work?
This paper uses attention mechanisms in CodeBERT and GraphCodeBERT to explain what pre-trained code models learn and when they fail on code generation and transformation tasks. It finds that models focus on method signatures in code summarization, syntax in code translation, and balance both in code refinement, with performance drops linked to poor attention distribution on complex inputs.
In recent years, there has been a wide interest in designing deep neural network-based models that automate downstream software engineering tasks on source code, such as code document generation, code search, and program repair. Although the main objective of these studies is to improve the effectiveness of the downstream task, many studies only attempt to employ the next best neural network model, without a proper in-depth analysis of why a particular solution works or does not, on particular tasks or scenarios. In this paper, using an example eXplainable AI (XAI) method (attention mechanism), we study two recent large language models (LLMs) for code (CodeBERT and GraphCodeBERT) on a set of software engineering downstream tasks: code document generation (CDG), code refinement (CR), and code translation (CT). Through quantitative and qualitative studies, we identify what CodeBERT and GraphCodeBERT learn (put the highest attention on, in terms of source code token types), on these tasks. We also show some of the common patterns when the model does not work as expected (performs poorly even on easy problems) and suggest recommendations that may alleviate the observed challenges.
Motivation & Objective
- To understand what token types pre-trained code models like CodeBERT and GraphCodeBERT focus on during code generation and transformation tasks.
- To identify failure patterns in model predictions by analyzing attention distributions, especially in low-performing cases.
- To provide actionable recommendations for improving model robustness and interpretability in software engineering applications.
- To bridge the gap between high model accuracy and trustworthiness by diagnosing why models make correct or incorrect predictions.
- To support open science by releasing a replication package for reproducibility and future research.
Proposed method
- The study employs the intrinsic attention mechanism of the Transformer architecture as an Explainable AI (XAI) method to interpret model predictions.
- Attention scores across all six layers of the models are extracted and normalized to analyze focus on three token type categories: naming, structural, and others.
- The analysis compares attention distributions between high- and low-performing samples (e.g., Easy-Low vs. all samples) to identify failure patterns.
- Quantitative metrics such as BLEU are used to evaluate model performance, while qualitative inspection validates attention patterns.
- The study evaluates three downstream tasks: code document generation (CDG), code refinement (CR), and code translation (CT) on Java and Python code.
- Recommendations are derived from observed attention failures, including using token-type tagging, subjective evaluation metrics, and attention score amplification.
Experimental results
Research questions
- RQ1What do CodeBERT and GraphCodeBERT learn in terms of source code token types during downstream tasks?
- RQ2How does the attention distribution differ between high- and low-performing predictions?
- RQ3What are the common failure patterns in model predictions, and how are they reflected in attention behavior?
- RQ4Can attention-based explanations reveal why models underperform on long or complex code samples?
- RQ5What recommendations can improve model reliability and interpretability based on attention analysis?
Key findings
- In code document generation (CDG), both models focus most on naming tokens (e.g., method names and parameters), with GraphCodeBERT showing 73.68% attention on naming tokens for Python CDG.
- In code translation (CT), models prioritize structural tokens (e.g., syntax keywords), with CodeXGLUE and GraphCodeBERT allocating 48.85% and 49.51% respectively to structural tokens.
- For code refinement (CR), attention is more balanced, with CodeXGLUE and GraphCodeBERT assigning 56.64% and 55.82% to naming tokens, and 46.29% and 48.35% to structural tokens.
- Models fail more frequently on long or complex inputs, and these failures are consistently reflected in attention distributions that underweight task-critical token types.
- The attention shift between easy-low and all samples shows that performance drops correlate with reduced attention on relevant token types—e.g., a 7.92% drop in structural attention for CT in CodeXGLUE.
- GraphCodeBERT shows stronger focus on structural elements than CodeBERT, likely due to its code-flow graph integration, which enhances structural awareness.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.