NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Probing Pretrained Models of Source Codes

Sergey Troshin, Nadezhda Chirkova

TL;DR

This paper introduces a suite of diagnostic "probing tasks" to systematically evaluate how well pre-trained code models understand source code beyond simple pattern matching. They find that while models grasp syntax and naming well, they struggle with deeper semantic understanding, suggesting current models are not yet ready for complex, safety-critical applications.

What Problem Does This Paper Address?

Deep learning models have revolutionized code-related tasks like generation and summarization. While general-purpose pre-trained models like CodeBERT and CodeT5 often outperform specialized ones, it's unclear what exactly these models learn about source code. The "black box" nature of these models makes it hard to trust them for critical applications. This paper aims to shed light on this by developing a set of probing tasks, inspired by similar work in Natural Language Processing (NLP), to dissect the internal representations of these code models and understand their strengths and weaknesses.

Methods

The core of this work is a set of auxiliary tasks designed to probe specific aspects of code understanding. These tasks use linear regression or classification models trained on top of the frozen representations extracted from different layers of pre-trained code models. The authors introduce:

  • Syntactic Tasks:
    • Token Path Type: Predicts a token's position within the Abstract Syntax Tree (AST).
  • AST Depth: Predicts the depth of the AST for a code snippet.
  • Namespace/Identifier Tasks:
    • Is Variable Declared: Checks if a variable is used within its declaration scope.
  • Variable Name: Predicts a variable's name based on its usage context.
  • Is Variable Misused: Detects bugs where an identifier is incorrectly substituted.
  • Data Flow Task:
    • Edge Prediction in DFG: Predicts data flow edges between tokens.
  • Semantic/Other Tasks:
    • Algorithm: Distinguishes computationally equivalent code from non-equivalent code.
  • Readability: Assesses how easy code is to understand.

They also investigate the impact of model architecture (encoder vs. decoder), model size, and the effect of fine-tuning on these probing results.

Datasets

The paper utilizes several datasets:

  • Synthetic Data: For the first six probing tasks, synthetic Java function and method examples were used, derived from Ahmad et al. (2021a). These examples were standardized and had comments and newlines removed.
  • CodeForces Competition Data: For the "Algorithm" task, data was collected from "wrong answer" and "accepted" Python submissions for a specific problem on the CodeForces platform.
  • Scalabrino et al. (2018) Dataset: For the "Readability" task, a dataset of 200 Java functions with developer-rated readability scores was used, converted into a binary classification task.
  • Downstream Task Datasets: For fine-tuning experiments, datasets for Code Translation (Java-Python), Code Generation (NL to Java), Code Summarization (Java to NL), Clone Detection, and Defect Prediction were used.

Key Insights and Ideas

  • Syntax is King, Semantics is Tricky: Pre-trained code models are surprisingly good at understanding code syntax (AST structure, variable declarations) and naming conventions. However, they struggle significantly with deeper semantic understanding, such as determining if two pieces of code are computationally equivalent ("Algorithm" task).
  • Code-Specific Pretraining Pays Off: Models explicitly pre-trained with code-specific objectives (like GraphCodeBERT and CodeT5) generally outperform models pre-trained only on general text or with simpler code objectives, especially on tasks related to their pretraining focus.
  • Fine-tuning Can Hurt: Fine-tuning models for specific downstream tasks, particularly discriminative ones, can lead to a significant loss of information about code properties. This suggests that fine-tuning might cause models to rely on superficial features rather than genuine code understanding.
  • Punctuation Matters (A Lot!): Ablation studies reveal that punctuation is a surprisingly crucial component for code models. Masking punctuation severely degrades performance across most tasks, highlighting its importance for syntactic and structural understanding.
  • Encoder vs. Decoder: Encoder representations generally perform better than decoder representations for information extraction tasks, suggesting encoders are better suited for capturing rich input features.

Key Results

  • Overall Performance: Pre-trained models consistently outperform simple baselines across all probing tasks.
  • "Algorithm" and "Readability" Challenges: For semantic tasks like "Algorithm" and "Readability," pre-trained models show performance very close to simple baselines, indicating a lack of deep understanding in these areas.
  • CodeT5 and GraphCodeBERT Lead: These models, with code-specific pretraining objectives, show consistent gains, particularly on tasks aligned with their objectives (e.g., GraphCodeBERT on "Edge Prediction in DFG," CodeT5 and GraphCodeBERT on variable-related tasks).
  • BERT's Limitations: The text-based BERT model performs worse than code-pretrained models on most tasks, except for "Readability," where performance is similar.
  • Fine-tuning Impact: Discriminative fine-tuning leads to the highest information loss, while generative tasks like Code Translation show minimal loss. Models trained from scratch on downstream tasks perform significantly worse than finetuned or pre-trained models.

Limitations and Future Work

  • Limited Scope of Probing: The authors acknowledge that their probing setup doesn't cover all possible aspects of source code, though they aimed for diversity.
  • Language Bias: Experiments were primarily conducted on Java and Python, and it would be beneficial to extend this to low-level languages like C/C++.
  • Linear Probes: While effective and widely used, linear probes might have limitations in capturing complex relationships. The authors did experiment with a 3-layer MLP, finding similar results, but further exploration with more complex probing architectures could be valuable.
  • Model Size: While larger models generally performed slightly better on variable-related tasks, the performance difference was often minor across most tasks, suggesting that model size isn't the sole determinant of understanding.
  • Future Work: The authors suggest exploring more complex probing tasks, investigating models on low-level languages, and comparing a wider range of model sizes. They also highlight the importance of code-specific pretraining objectives for better performance on aligned downstream tasks.

Summary updated

BibTeX
@inproceedings{troshin2022probing,
  author = {Sergey Troshin and Nadezhda Chirkova},
  title = {{Probing Pretrained Models of Source Codes}},
  booktitle = {BlackboxNLP @ EMNLP},
  year = {2022},
  doi = {10.18653/v1/2022.blackboxnlp-1.31},
}
Figure