Do LMs Know What Variables Do? Interpreting Code Representations via Variable Roles
Organizations: Sorbonne Université · MBZUAI · RIKEN AIP · Tohoku University
Abstract
Variable roles describe recurring behaviors such as accumulation, traversal, and retaining the best candidate encountered. We use Sajaniemi's independently defined taxonomy to study which role distinctions can be recovered from large language models, how sparse autoencoders (SAEs) retain those distinctions, and whether associated directions support control of generation. We introduce static annotation rules for all eleven roles and a corpus of 126,926 natural-code files across five languages. Across five model-SAE systems, residual probes outperform individual SAE features and tested lexical baselines, with modest losses after neutral target renaming. Combining SAE features closes much of the single-feature gap, but probes on SAE reconstructions are less sensitive to role-changing edits reviewed by LLMs. High paired ranking accuracy coexists with frequent false positives at fixed thresholds. During generation, interventions often change role annotations at the cost of correctness; changes accompanied by passing tests show no clear aggregate advantage over random controls. The benchmark separates role-related recoverability, SAE fidelity, and correctness-preserving control through matched prediction, source edits, and generation interventions.
Figures & tables
| Work | Signal | Languages | Roles | Corpus | Labeled data |
| Gerdt and Sajaniemi (2004) | Static analysis | Pascal | 3 | NR | NR |
| Bishop and Johnson (2005) | Static analysis | Java | Checking | 17 programs | NR |
| Gerdt and Sajaniemi (2006) | Static + learning | Pascal | 9 | 87 programs | 305 vars. |
| Demyanova et al. (2013) | Static analysis | C | 14 custom | 5.2 KLOC † | NR |
| Lui et al. (2020) | Traces + NN | C | 4 classes | 110 programs | 8,907 cases |
| Hussain et al. (2023) | Syntax rules | Java | 2 | 14,022,318 methods | 1,438,035 vars. |
| Predictor | Matched AUROC | Counterfactual paired accuracy |
|---|---|---|
| Residual probe | 0.906–0.913 | 0.813–0.877 |
| SAE reconstruction probe | 0.857–0.881 | 0.739–0.784 |
| Top-64 SAE probe | 0.853–0.878 | 0.722–0.753 |
| Single SAE feature | 0.722–0.782 | 0.622–0.713 |
| Gemma 2 2B | Gemma 3 4B | ||||
|---|---|---|---|---|---|
| Role | Representative syntax | Pos. steer. | Orth. | Pos. steer. | Orth. |
| Gatherer | Augmented assignment | (107) | (17) | (44) | (4) |
| Most-wanted holder | if statement | (92) | (20) | (17) | (4) |
| One-way flag | Boolean literal | (17) | – | (18) | – |
| Stepper | range call | (59) | (40) | (54) | (11) |
| Walker | Subscript expression | (40) | (8) | (39) | (10) |
Appendix figures & tables25 assets
Supplementary material from the paper’s appendix.
Appendix
| Study / coverage | Corpus and labeled examples | Scope and qualifications |
|---|---|---|
| Gerdt and Sajaniemi (2004) Pascal; 3 roles | Prototype program and variable counts: NR. | Fixed Value, Stepper, and Most-Recent Holder, selected using an earlier textbook survey. |
| Bishop and Johnson (2005) Java; role checking | 17 canonical teaching programs; variable count: NR. | Checks supplied role assignments in procedural Java. |
| Gerdt and Sajaniemi (2006) Pascal; 9 roles | Learning: 43 programs, 175 variables. Evaluation: 44 programs, 130 variables. Total: 87 programs, 305 variables. | Filtered, human-annotated textbook sample; arrays omitted and role-changing variables split by role. |
| Demyanova et al. (2013) C; 14 custom roles | 5.2 KLOC of cBench for role design. Downstream file and variable totals: NR. | Custom roles evaluated on SV-COMP file-category prediction, not role-label accuracy. |
| Lui et al. (2020) C; 4 classes | 110 educational programs; 8,907 labeled trace cases. | Manually annotated programs; cases are not stated to be distinct declarations. One-Way Flag and Constant are merged. |
| Hussain et al. (2023) Java; 2 roles | 14,022,318 methods; 1,438,035 role-augmented variables (624,701 steppers; 813,334 walkers). | Automatic syntax rules; pool includes methods without either role. Evaluated on method-name prediction. |
| Distribution | Performance | ||||
|---|---|---|---|---|---|
| Role | Extractor | Human | Precision | Recall | F1 |
| Container | 30 | 27 | 0.733 | 0.815 | 0.772 |
| Fixed value | 106 | 145 | 0.830 | 0.607 | 0.701 |
| Follower | 20 | 22 | 0.900 | 0.818 | 0.857 |
| Gatherer | 20 | 25 | 0.900 | 0.720 | 0.800 |
| Most-recent holder | 21 | 30 | 0.667 | 0.467 | 0.549 |
| Partition | Files | Variables | Role pairs |
|---|---|---|---|
| Train | 76,155 | 2,116,300 | 1,048,604 |
| Validation | 25,386 | 705,213 | 349,496 |
| Test | 25,385 | 704,017 | 349,527 |
| All | 126,926 | 3,525,530 | 1,747,627 |
| Role | Train | Valid. | Test | Total | % | Test files |
|---|---|---|---|---|---|---|
| Fixed value | 780,012 | 259,968 | 260,007 | 1,299,987 | 36.87 | 24,387 |
| Stepper | 22,201 | 7,403 | 7,402 | 37,006 | 1.05 | 4,079 |
| Gatherer | 2,877 | 959 | 958 | 4,794 | 0.14 | 677 |
| Most-recent holder | 6,500 | 2,166 | 2,168 | 10,834 | 0.31 | 1,300 |
| Most-wanted holder | 901 | 298 | 300 | 1,499 | 0.04 | 217 |
| One-way flag | 4,321 | 1,439 | 1,437 | 7,197 | 0.20 | 1,132 |
| Predictor | Macro AP | Macro F1 |
|---|---|---|
| Binary majority | 0.045 | 0.000 |
| Most frequent role | 0.045 | 0.049 |
| Always positive | 0.045 | 0.071 |
| Random (uniform) | ||
| Random (prior-matched) | ||
| Language + kind | 0.100 [0.098, 0.101] | 0.112 [0.109, 0.114] |
| Role | Prior AP | Name AP | Code AP | Combined AP | Combined F1 |
|---|---|---|---|---|---|
| Fixed value | 0.3693 | 0.594 | 0.785 | 0.822 | 0.752 |
| Stepper | 0.0105 | 0.363 | 0.649 | 0.679 | 0.690 |
| Gatherer | 0.0014 | 0.075 | 0.423 | 0.479 | 0.535 |
| Most-recent holder | 0.0031 | 0.011 | 0.222 | 0.223 | 0.317 |
| Most-wanted holder | 0.0004 | 0.056 | 0.388 | 0.458 | 0.488 |
| One-way flag | 0.0020 | 0.052 | 0.516 | 0.665 | 0.658 |
| Model | SAE release | SAE ID | Layers |
|---|---|---|---|
| Gemma 2 2B | gemma-scope-2b-pt-res-canonical | layer_{l}/width_16k/canonical | 6, 13, 17, 22 |
| Gemma 3 4B | gemma-scope-2-4b-pt-res | layer_{l}_width_65k_l0_medium | 9, 17, 22, 29 |
| Gemma 3 12B | gemma-scope-2-12b-pt-res | layer_{l}_width_65k_l0_medium | 12, 24, 31, 41 |
| Llama 3.1 8B | llama_scope_lxr_32x | l{l}r_32x | 8, 16, 21, 27 |
| Qwen3.5 9B | qwen-scope-3.5-9b-base-w64k-l100 | layer{l} | 8, 16, 21, 27 |
| Comparison | Population and split | Selection and outcome |
|---|---|---|
| Corpus baselines | 3,525,530 variables; three training folds, one validation fold, one Test fold | Automatic annotations; metadata and lexical prediction on the full consolidated corpus. |
| Representation fits | 5,321 training and 9,547 validation pairs; source groups separated from Test | Training fixes normalization and feature ranking; validation chooses regularization, single feature, and threshold. |
| Matched Test | 4,238 positive/negative pairs after exclusions; four languages | Shared examples across predictors; AP and AUROC measure rankings against automatic labels. |
| Counterfactual Test | 1,698 accepted pairs from 948 source groups | Static source review of original and edited behavior; paired ranking and frozen-threshold decisions. |
| Neutral-name Test | 100 positive/negative pairs per role | Both bindings renamed, with fits fixed; paired change in AUROC and binary decisions. |
| Feature-scale steering | Union of 121 MBPP tasks; role-specific cohorts | Eleven SAE targets each for Gemma 2B/4B and nine for 12B; ten same-layer random features per target. |
| Study | Analysis and target | Key finding |
|---|---|---|
| Behavioral robustness and semantic priors | ||
| Yang et al. (2022) | Input edits: semantics-preserving names | Plausible renaming induces failures; adversarial training improves robustness. |
| Gao et al. (2023) | Input counterfactuals: names | Names provide semantics and shortcuts; counterfactual inference improves robustness. |
| Wang et al. (2023) | Input edits: ReCode | Generation degrades under identifier, syntax, formatting, and docstring perturbations. |
| Ahmed et al. (2023) † | Input edits: masked code | Masked-operator predictions reflect code edits beyond frequency effects. |
| Hooda et al. (2024) | Input counterfactuals: predicates | Counterfactual code edits expose inconsistent handling of control/data flow and names. |
LLM Interpretability
Large language model (LLM) interpretability studies how internal representations and computations produce outputs, and how they can be explained or controlled.
See all 778 papers