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

Unveiling Code Pre-Trained Models: Investigating Syntax and Semantics Capacities

Wei Ma, Shangqing Liu, Mengjie Zhao, Xiaofei Xie, Wenhan Wang, Qiang Hu, Jie Zhang, Yang Liu

TL;DR

This paper dives deep into how pre-trained code models and large language models (LLMs) understand code syntax and semantics. By developing novel probing tasks, the authors reveal that while models excel at syntax, their grasp of semantics is more varied, with LLMs not always outperforming specialized code models. This work is crucial for building more reliable and capable AI for software engineering.

What Problem Does This Paper Address?

The rapid advancement of AI in software engineering, particularly with pre-trained code models and LLMs, has led to impressive capabilities in tasks like code generation and completion. However, a fundamental question remains: how well do these models truly understand the underlying structure and meaning of code? While prior research has touched upon syntax, a comprehensive investigation into both syntax and semantics, especially for the latest LLMs, has been lacking. Existing analyses often rely on simplifying assumptions (e.g., edge count in AST directly correlating with syntax closeness) that may not hold true, potentially leading to inaccurate conclusions. This paper aims to bridge this gap by rigorously evaluating the syntax and semantic understanding of various code models.

Methods

The authors introduce a suite of four novel probing tasks designed to assess code models' understanding of syntax and semantics. These tasks focus on reconstructing code structures within the models' internal representations:

  • Syntax Probing:
    • Syntax Node Pair Prediction: Aims to determine if code models can capture the syntactic relationships between code tokens by predicting if two nodes in an Abstract Syntax Tree (AST) belong to the same subtree.
  • Token Syntax Tagging: Evaluates if individual code tokens are assigned their correct syntactic roles (e.g., identifier, variable declaration) within the model's representation.
  • Semantic Probing:
    • Semantic Relation Prediction: Focuses on reconstructing semantic structures like Control Dependency Graphs (CDG), Data Dependency Graphs (DDG), and Control Flow Graphs (CFG) from model representations.
  • Semantic Propagation Prediction (inGraph): Assesses the models' ability to understand long-range semantic dependencies, such as how a variable declared early in the code is used much later.

Additionally, the paper includes an attention analysis to understand how different attention heads contribute to learning code semantics. The probing tasks use a fixed probing classifier (an MLP) to evaluate the representations extracted from various layers of the models.

Datasets

The study utilizes two established datasets for evaluation:

  • Java250: A large dataset of Java programs.
  • POJ-104: A dataset of C/C++ programs.

The authors also mention using a dataset of over 10,000 semantic inputs for pre-trained models and randomly sampling 100 for LLMs for the attention analysis.

Key Insights and Ideas

  • Syntax is More Observable: The study consistently finds that syntactic relationships are more readily observable in the shallower layers of code models, suggesting that models learn basic structural properties early on.
  • Semantics are Trickier: Encoding code semantics proves more challenging and shows greater variability across models and semantic types.
  • LLMs Aren't Always Superior: Despite their massive scale, LLMs (CodeLlama, StarCoder, CodeT5+) do not consistently outperform specialized pre-trained code models (CodeBERT, GraphCodeBERT, CodeT5, UnixCoder) on these probing tasks. This suggests that simply having more parameters doesn't automatically translate to better understanding of code structure.
  • Layer-Specific Strengths: For LLMs, shallower layers often show better performance on semantic tasks, with a notable drop in deeper layers. This hints at potential architectural or training strategy differences that impact how semantics are encoded and accessed.
  • Model Specialization: Different models exhibit varying strengths. For instance, CodeT5 and GraphCodeBERT are noted for their ability to capture control and data dependencies, while UnixCoder is less effective.
  • Attention Heads are Specialized: The attention analysis reveals that specific attention heads are dedicated to learning different aspects of code semantics, offering a granular view of how models process information.
  • Syntax vs. Semantics Distance: The paper reinforces the idea that traditional distance metrics (like Euclidean distance in vector space) might not accurately reflect syntactic or semantic closeness in code, challenging prior assumptions.

Key Results

  • Syntax Performance: Pre-trained code models generally achieve good performance (MCC > 70%) on syntax pair node prediction, with optimal performance often found in layers 2-4.
  • Semantic Performance: Semantic relation prediction performance is generally lower than syntax tasks. CodeT5 shows the highest proficiency in CDG and DDG, while GraphCodeBERT is slightly better for CFG. UnixCoder consistently underperforms in semantic tasks, especially in deeper layers.
  • LLM Semantic Performance: LLMs show that shallow layers are better for observing semantic information, but performance drops in deeper layers. StarCoder's performance drops significantly in deep layers.
  • Control Flow Challenges: Models, including LLMs, struggle to capture control flow semantics (CFG) as effectively as other semantic types like CDG and DDG.
  • Attention Head Overlap: Semantic attention patterns are surprisingly shared across different programming languages (Java and C/C++), suggesting a degree of language-agnostic semantic understanding.

Limitations and Future Work

  • Probing Limitations: The authors acknowledge that probing analysis can only reveal if a property is observable in the representation space, not definitively if it's absent. The high-dimensional nature of representations can mix information, making some properties harder to isolate.
  • Dataset Bias: The use of static analysis tools for dataset generation might introduce biases and not fully capture dynamic semantics.
  • Generalizability: While efforts were made to include diverse model architectures and types, the conclusions might not apply to models with vastly different training strategies or datasets.
  • Future Directions:
    • Optimizing Architectures and Training: Developing new neural network architectures (e.g., graph transformers), training objectives, and data augmentation techniques to improve semantic understanding.
  • New Probing Tasks and Metrics: Creating more sophisticated methods to unveil model internals and robustly evaluate code understanding.
  • Robustness and Adversarial Attacks: Investigating how noise and adversarial attacks affect code model performance and developing methods to enhance robustness, especially concerning code semantics and security.
  • Understanding LLM Semantics: Further research is needed to make the semantic representations of LLMs more observable, potentially through prompting techniques.

Summary updated

BibTeX
@article{ma2022unveiling,
  author = {Wei Ma and Shangqing Liu and Mengjie Zhao and Xiaofei Xie and Wenhan Wang and Qiang Hu and Jie Zhang and Yang Liu},
  title = {{Unveiling Code Pre-Trained Models: Investigating Syntax and Semantics Capacities}},
  journal = {ACM Transactions on Software Engineering and Methodology},
  year = {2022},
  url = {https://openreview.net/forum?id=L0EnFg4Fev},
}
Figure