Introduction
Neural Language Models of Code (NCMs) have transitioned from academic curiosities to the backbone of modern developer tools like GitHub Copilot and OpenAI’s Codex. Despite their impressive performance, these models remain largely "black boxes." Traditional evaluation metrics like BLEU or raw accuracy provide a snapshot of performance but fail to explain why a model makes a specific prediction. This lack of transparency poses risks, particularly regarding spurious correlations—where a model appears to understand a high-level concept (like "bugginess") but is actually relying on irrelevant statistical artifacts (like code length or complexity).
To address this, we introduce $do_{code}$, a post hoc interpretability framework grounded in causal inference. By moving beyond simple associations to estimate actual causal effects, $do_{code}$ provides a more robust understanding of NCM behavior.
The $do_{code}$ Framework
The $do_{code}$ framework is built upon Judea Pearl’s "Ladder of Causation," specifically focusing on interventions—the ability to ask "what happens if I change X?" in a controlled manner. It situates itself within the broader landscape of interpretability as a post hoc, causal method that utilizes interventions to probe model logic.
The framework follows a rigorous four-step pipeline:
- Modeling: Researchers define a Structural Causal Model (SCM), a directed acyclic graph representing assumed relationships between the code property (treatment), the model's output (outcome), and potential confounders.
- Identification: The framework uses rules like the "back-door criterion" to determine if the causal effect can be uniquely estimated from the data.
- Estimation: It calculates the Average Treatment Effect (ATE), measuring the change in performance directly caused by the treatment while holding confounders constant.
- Refutation: The results are validated through stress tests, such as replacing the treatment with a "placebo" (random noise) to ensure the detected effect isn't a fluke.
Case Study: Disentangling "Buggy" Code
A common observation is that NCMs perform worse on buggy code. A purely correlational view might suggest the model "understands" bugs. However, $do_{code}$ allows us to test competing hypotheses to see if this performance drop is truly caused by the presence of a bug.
In the figure above, we see three possibilities: (a) Bugginess causes lower accuracy; (b) Accuracy causes bugginess (unlikely); or (c) A hidden confounder (like code complexity or the number of subwords) causes both the bugginess and the lower accuracy. Our analysis revealed that while there is a high correlation between buggy code and low accuracy, the ATE was near zero. This confirms hypothesis (c): the models aren't struggling with the "bug" itself, but rather with the increased complexity that often accompanies buggy code.
Key Insights
- Structural Bias: NCMs are significantly better at predicting "structural" tokens (brackets, semicolons) than logical ones. They learn the "shape" of code with much less confounding bias than they learn its logic.
- Syntactic Sensitivity: Models are highly sensitive to "Type II" clones (renaming variables or changing whitespace). These minor syntactic shifts causally explain significant changes in model cross-entropy, suggesting models rely heavily on surface-level patterns.
- BERT’s Limitations: Surprisingly, BERT-like models showed less sensitivity to specific grammar-based masking than to random masking. This suggests they may not capture the underlying tree structure of programming languages as deeply as previously assumed.
Conclusion
The $do_{code}$ framework demonstrates that causal inference is essential for truly interpreting NCMs. By distinguishing between what a model sees (correlation) and what actually drives its decisions (causation), we can build more reliable and less biased AI tools for software engineering. Future work will focus on automating the creation of causal models and integrating these checks directly into the model training pipeline.
Summary updated