Abstract
Study patterns that models have learned has long been a focus of pattern recognition research. Explaining what patterns are discovered from training data, and how patterns are generalized to unseen data are instrumental to understanding and advancing the pattern recognition methods. Unfortunately, the vast majority of the application domains deal with continuous data out of which extracted patterns can not be formally defined. Unlike the aforementioned data format, programs are a unique data structure with a well-defined syntax and semantics, which creates a golden opportunity to formalize what models have learned from source code. This paper presents the first formal definition of patterns discovered by code summarization models (i.e. models that predict the name of a method given its body), and gives a sound algorithm to infer a context-free grammar (CFG) that formally describes the learned patterns. We realize our approach in PATIC which produces CFGs for summarizing the patterns discovered by code summarization models. In particular, we pick two prominent instances, code2vec and code2seq, to evaluate PATIC. PATIC shows that the patterns extracted by each model are heavily restricted to local, and syntactic code structures with little to none semantic implication. Based on these findings, we present two example uses of the formal definition of patterns: a new method for evaluating the robustness and a new technique for improving the accuracy of code summarization models.
Introduction: The Black Box of Code Summarization
Deep learning models for source code have achieved impressive results in tasks like code summarization—predicting a method's name from its body. However, these models remain "black boxes." In domains like image recognition, defining a "pattern" is difficult because pixels are continuous and statistical. Source code is different: it has a well-defined, discrete syntax and semantics. This research addresses the lack of explainability in AI for code by leveraging this structure to answer a fundamental question: What specific code patterns trigger a model's prediction, and can we formalize them?
Methodology: The PATIC Framework
The authors introduce PATIC, a tool that provides the first formal definition of what code summarization models actually "learn" by representing their internal patterns as Context-Free Grammars (CFGs). The core of PATIC is a four-step algorithm: Abstract, Mutate, Concretize, and Summarize.
- Abstract: The algorithm identifies a "seed"—the minimal subset of statements (often $\le 2$) that allows the model to maintain its original prediction.
- Mutate: The seed is modified using tree-edit operations (renaming, insertion, deletion) to find "mutants" that still trigger the same prediction.
- Concretize: These seeds and mutants are embedded into thousands of diverse, synthesized program contexts to ensure prediction stability regardless of the surrounding code.
- Summarize: PATIC takes all valid programs that preserve the prediction and infers a Context-Free Grammar (CFG). This CFG serves as a formal "trademark" of what the model has learned for a specific label.
Key Insights: The Fragility of Learned Patterns
The study reveals a startling reality: state-of-the-art models like code2vec and code2seq often ignore the global logic of a program, instead relying on tiny, local syntactic snippets to "cheat" their way to a prediction.
- The "Local Window" Trap: Models don't learn global semantic representations. Instead, they focus on a very small, local window of code. The average "seed" responsible for a prediction contains only about 15 tokens.
- Syntactic Proxies for Memorization: Models often use specific syntactic structures (like a particular log message or a specific API call) as a proxy for the method name, rather than understanding what the code actually does.
- Fragility to Simple Edits: The authors found that swapping the operands of a multiplication ($a * b$ to $b * a$)—a change that is semantically null—could completely break a model's prediction.
Evaluation and Results
The study utilized three standard, large-scale public benchmarks for Java (Java-small, Java-med, and Java-large). Key findings include:
- Robustness Evaluation: Using PATIC's insights, the authors created adversarial attacks that were significantly more efficient than previous baselines. They found adversarial examples with an average tree-edit distance of 1.12 to 3.02, compared to the baseline's 2.39 to 3.50.
- Improving Accuracy: The authors used PATIC to identify "clashing" seeds (where two different method names shared similar triggers). By augmenting the training data to "neutralize" these misleading seeds, they improved the accuracy of code2seq, helping it achieve state-of-the-art results on the Java-med and Java-large datasets.
- Seed Minimality: In almost all cases, the vast majority of a method's body could be deleted without changing the model's prediction, proving that the models are "over-attending" to specific tokens.
Conclusion and Future Work
By using CFGs, the authors move beyond "heatmaps" or "attention weights" to provide a mathematically sound description of a model's learned behavior. While the current framework focuses on method-level summarization and assumes a certain monotonicity in model behavior, it opens an exciting new direction for studying what models learn from source code. Future work aims to provide even more granular explanations and apply this framework to tasks like bug detection or code clone detection.
Summary updated