Skip to main content
QUICK REVIEW

[Paper Review] Emergent Representations of Program Semantics in Language Models Trained on Programs

Charles Jin, Martin Rinard|arXiv (Cornell University)|May 18, 2023
Software Engineering ResearchComputer Science9 citations
TL;DR

The paper shows that a language model trained only on next-token prediction over programs develops linear, meaningful representations of program semantics, including future states, and that semantics are attributed to model states rather than the probe.

ABSTRACT

We present evidence that language models (LMs) of code can learn to represent the formal semantics of programs, despite being trained only to perform next-token prediction. Specifically, we train a Transformer model on a synthetic corpus of programs written in a domain-specific language for navigating 2D grid world environments. Each program in the corpus is preceded by a (partial) specification in the form of several input-output grid world states. Despite providing no further inductive biases, we find that a probing classifier is able to extract increasingly accurate representations of the unobserved, intermediate grid world states from the LM hidden states over the course of training, suggesting the LM acquires an emergent ability to interpret programs in the formal sense. We also develop a novel interventional baseline that enables us to disambiguate what is represented by the LM as opposed to learned by the probe. We anticipate that this technique may be generally applicable to a broad range of semantic probing experiments. In summary, this paper does not propose any new techniques for training LMs of code, but develops an experimental framework for and provides insights into the acquisition and representation of formal semantics in statistical models of code. Our code is available at https://github.com/charlesjin/emergent-semantics.

Motivation & Objective

  • Explore whether meaning can emerge in LMs trained solely on next-token prediction using a program synthesis setting.
  • Define a precise semantic framework (program tracing and abstract interpretation) to test for semantic encodings in LM representations.
  • Demonstrate that LM hidden states contain linear encodings of semantic state and future program states, correlated with synthesis accuracy.
  • Differentiate whether semantics reside in model states or are learned by probes through interventional experiments.

Proposed method

  • Train a 350M parameter Transformer (CodeGen architecture) on a corpus of one million Karel programs with textual specifications (input-output examples).
  • Construct trace datasets by recording the LM hidden state and the corresponding program state after partial execution for each generated token.
  • Train linear probes to predict semantic states (e.g., facing direction) from model states and assess semantic content.
  • Intervene on semantics with an alternative semantics to distinguish whether model states encode semantic content or only a syntactic transcript.
  • Compare generated programs to training distribution by analyzing program lengths and perplexities.
  • Use greedy decoding to generate programs and evaluate generative accuracy on test specifications.
Figure 1 : An overview of the Karel domain. We construct training examples by sampling a random reference program, then sampling 5 random inputs and executing the program to obtain the corresponding 5 outputs. The LM is trained to perform next token prediction on a corpus of examples. At test time,
Figure 1 : An overview of the Karel domain. We construct training examples by sampling a random reference program, then sampling 5 random inputs and executing the program to obtain the corresponding 5 outputs. The LM is trained to perform next token prediction on a corpus of examples. At test time,

Experimental results

Research questions

  • RQ1Can LMs trained only on next token prediction learn semantics when trained on program specifications?
  • RQ2Do LM hidden states contain linear, transferable representations of program semantics, including future states?
  • RQ3Are semantic representations in model states or learned by the probes?
  • RQ4Do LM outputs differ semantically from the training distribution, even when achieving high accuracy?

Key findings

  • Semantic content emerges during training and correlates with generative accuracy (R^2 = 0.968, p < 0.001).
  • Probes can extract abstract semantics and future program states from LM states, indicating learned meaning.
  • An interventional test shows semantics are attributable to model states rather than the probe, rejecting the syntactic transcript hypothesis.
  • The LM tends to generate shorter correct programs than those in the training set, while perplexity on training-set programs remains high, indicating semantic differences from the training distribution.
  • The semantic content in future states can be predicted from current states, with strong correlation to generative progress (R^2 around 0.9 for 1–2 steps into the future).
  • Alternative semantics degrade the probe’s ability to recover meaning, supporting that model states encode original semantics.
Figure 2 : Plotting generative accuracy (blue line) and semantic content (green line) over time. The dotted line at 25% plots the baseline semantic content for random guessing.
Figure 2 : Plotting generative accuracy (blue line) and semantic content (green line) over time. The dotted line at 25% plots the baseline semantic content for random guessing.

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.