[Paper Review] Towards Understanding What Code Language Models Learned
This paper investigates whether code pre-trained language models learn computational semantics beyond surface-level patterns by testing their ability to reconstruct code after meaning-preserving transformations such as block swapping and operand reordering. Results show that models like GraphCodeBERT and CodeBERT maintain high accuracy (F-score >0.73) on transformed code, outperforming frequency-based guessing, indicating robust semantic understanding from form alone.
Pre-trained language models are effective in a variety of natural language tasks, but it has been argued their capabilities fall short of fully learning meaning or understanding language. To understand the extent to which language models can learn some form of meaning, we investigate their ability to capture semantics of code beyond superficial frequency and co-occurrence. In contrast to previous research on probing models for linguistic features, we study pre-trained models in a setting that allows for objective and straightforward evaluation of a model's ability to learn semantics. In this paper, we examine whether such models capture the semantics of code, which is precisely and formally defined. Through experiments involving the manipulation of code fragments, we show that code pre-trained models of code learn a robust representation of the computational semantics of code that goes beyond superficial features of form alone
Motivation & Objective
- To investigate whether pre-trained code language models learn computational semantics beyond lexical and syntactic patterns.
- To evaluate if models can generalize to semantically equivalent but syntactically different code forms.
- To determine whether model predictions are driven by memorization of token frequencies or by deeper semantic understanding.
- To provide an objective, automated evaluation of semantic robustness in code models using formal program transformations.
Proposed method
- Applying meaning-preserving code transformations—such as block swapping and operand reordering—while preserving computational semantics.
- Measuring model performance on masked operator reconstruction before and after transformation using F-score and log-likelihood.
- Using Monte Carlo simulations with frequency-based random guessing to establish a baseline for comparison.
- Analyzing embedding space distances between semantically equivalent and syntactically similar but semantically different code pairs.
- Evaluating models on restricted context and variable renaming to test robustness to syntactic unnaturalness.
- Using pre-trained models directly via API without fine-tuning to assess intrinsic semantic encoding.
Experimental results
Research questions
- RQ1Can pre-trained code language models correctly reconstruct code after meaning-preserving syntactic transformations?
- RQ2Does model performance on transformed code exceed what would be expected from token frequency alone?
- RQ3Are model predictions driven by memorization of surface-level patterns or by deeper semantic representations?
- RQ4How do embedding representations reflect semantic equivalence versus syntactic similarity in code?
Key findings
- Models like GraphCodeBERT and CodeBERT maintain high F-scores (over 0.73) on masked operator prediction after block swapping, indicating robust semantic understanding.
- For the "<=" and ">=" operators, the model's F-score was 0.10 before transformation, but this was still statistically superior to frequency-based guessing in 23 out of 24 cases (p < 0.01).
- The log-likelihood of predicted operators increased significantly after transformation (e.g., from 0.17 to 0.35 for GraphCodeBERT), showing improved confidence without relying on lexical frequency.
- Semantically equivalent code pairs had the smallest embedding distance, while syntactically similar but semantically different pairs were farther apart, confirming semantic encoding in representations.
- Even under variable renaming and repositioning of conditionals—transformations that make code more unnatural—the models preserved high prediction accuracy.
- The model’s performance consistently exceeded random frequency-based guessing, suggesting it captures functional semantics rather than memorizing token co-occurrence patterns.
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.