Skip to main content
QUICK REVIEW

[Paper Review] Unveiling Code Pre-Trained Models: Investigating Syntax and Semantics Capacities

Wei Ma, Shangqing Liu|arXiv (Cornell University)|Dec 20, 2022
Natural Language Processing TechniquesComputer Science5 citations
TL;DR

This paper investigates the syntax and semantics learning capabilities of CodeBERT and GraphCodeBERT through a set of novel probing tasks. It designs syntax-specific tasks using ASTs and semantic tasks using control flow, data dependency, and control dependency graphs, revealing that both models learn syntax effectively and GraphCodeBERT outperforms CodeBERT in capturing control flow and data dependency semantics.

ABSTRACT

Past research has examined how well these models grasp code syntax, yet their understanding of code semantics still needs to be explored. We extensively analyze seven code models to investigate how code models represent code syntax and semantics. This includes four prominent code pre-trained models (CodeBERT, GraphCodeBERT, CodeT5, and UnixCoder) and three large language models (StarCoder, CodeLlama, and CodeT5+). We have developed four probing tasks to evaluate the models' abilities to learn code syntax and semantics. These tasks focus on reconstructing code syntax and semantic structures-such as AST, CFG, CDG, and DDG - within the models' representation spaces. These structures are fundamental to understanding code. Additionally, we explore the role of syntax tokens in each token representation and the extended dependencies among code tokens. Furthermore, we examine the distribution of attention weights concerning code semantic structures. Through detailed analysis, our results emphasize the strengths and weaknesses of various code models in mastering code syntax and semantics. The findings reveal that these models are proficient in grasping code syntax, effectively capturing the relationships and roles of syntax tokens. However, their ability to encode code semantics shows more variability. This study enriches our understanding of the capabilities of code models in analyzing syntax and semantics. Our findings offer valuable insights for future code model enhancements, helping optimize their application across a range of code-related tasks.

Motivation & Objective

  • To investigate whether code pre-trained models like CodeBERT and GraphCodeBERT can effectively learn program syntax and semantics.
  • To address the limitations of prior work that rely on distance-based methods in high-dimensional spaces, which suffer from the curse of dimensionality.
  • To provide a systematic analysis of the semantic knowledge encoded in pre-trained models, particularly focusing on control flow, data dependency, and control dependency.
  • To examine the role of attention heads and layers in encoding semantic information, offering insights into model interpretability.
  • To validate the probing tasks' external validity using ASTs and static analysis graphs to ensure they represent true syntax and semantics.

Proposed method

  • Design two syntax probing tasks: (1) syntax node pair prediction using AST structure, and (2) token syntax tagging prediction to assess grammar rule encoding.
  • Construct control flow graphs (CFG), data dependency graphs (DDG), and control dependency graphs (CDG) for semantic probing tasks.
  • Develop three semantic probing tasks: (1) semantic relationship prediction, (2) semantic propagation prediction (inGraph), and (3) type inference.
  • Train linear classifiers on contextual representations from CodeBERT and GraphCodeBERT to evaluate their ability to recover syntax and semantic facts.
  • Use Spearman’s rank correlation (ρ) to analyze layer-behavior consistency across models and layers.
  • Perform statistical analysis on attention weights across heads and layers to identify their roles in encoding semantic information.

Experimental results

Research questions

  • RQ1Can code pre-trained models like CodeBERT and GraphCodeBERT learn program syntax effectively, and how does their performance compare across different syntax structures?
  • RQ2To what extent can CodeBERT and GraphCodeBERT learn program semantics, particularly control flow, data dependency, and control dependency information?
  • RQ3How do attention heads and layers contribute differently to the encoding of semantic knowledge in these models?
  • RQ4Are the probing tasks externally valid in representing true program syntax and semantics, and do they avoid high-dimensional distance artifacts?
  • RQ5How consistent are the representations of CodeBERT and GraphCodeBERT across layers in capturing syntax and semantic facts?

Key findings

  • Both CodeBERT and GraphCodeBERT demonstrate strong ability to learn program syntax, as evidenced by high performance on syntax node pair prediction and token tagging tasks.
  • GraphCodeBERT outperforms CodeBERT in learning control flow and data dependency semantics, as shown by superior performance on CFG and DDG probing tasks.
  • Both models show similar capability in learning control dependency information, with no significant performance difference on CDG-based probing tasks.
  • The final layer representations of both models contain rich semantic information, but different attention heads and layers play distinct roles in encoding various semantic facts.
  • Statistical analysis of attention weights reveals that specific heads and layers are specialized for different semantic types, indicating heterogeneous attention mechanisms for semantic understanding.
  • The probing tasks are externally valid, as they are grounded in AST and static analysis graphs, avoiding the pitfalls of high-dimensional distance-based evaluation methods.

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.