WhyGen: Explaining ML-powered Code Generation by Referring to Training Examples

Weixiang Yan, Yuanchun Li

TL;DR

WhyGen is a tool designed to pull back the curtain on AI code generators by linking generated snippets to the specific training examples that influenced them. By using a novel "inference fingerprint"—a vector of internal model activations—it can identify potential copyright recitations with over 81% accuracy while adding negligible latency to the generation process.

What Problem Does This Paper Address?

Large Language Models (LLMs) like GitHub Copilot have revolutionized coding, but they come with a significant "black box" problem. These models often memorize their training data, leading to "recitation"—where the AI generates code that is a verbatim or near-verbatim copy of copyrighted source material.

For developers, this creates a legal and ethical minefield: how do you know if the code your AI just suggested is original or a direct lift from a GPL-licensed repository? Current solutions often involve sacrificing model accuracy to reduce memorization, which isn't ideal. WhyGen proposes a different path: keep the high-performance models, but provide transparency by showing the user exactly which training examples the model is "referring to" during generation.

Methods

The core innovation of WhyGen is the inference fingerprint.

Instead of using computationally expensive "influence functions" to trace a model's output back to its training data, the authors look at the internal state of the neural network. An inference fingerprint is a vector of activation values produced by a specific set of "critical" neurons during a single pass through the network.

The process works in two stages:

  1. Offline Indexing: The authors run the entire training dataset through the model, extract fingerprints for each example, and store them in a searchable database (using the Faiss library for high-speed similarity search).
  2. Online Retrieval: When a user requests code, WhyGen captures the fingerprint of the model's decision process for the first generated token. It then queries the database to find the training examples with the most similar fingerprints using Euclidean distance.
The workflow of WhyGen to explain DNN-powered code generation by examples.

To make this efficient, the authors focus on neurons in the activation layers following Transformer blocks. They discovered that neurons with the highest variance across the training set are the most representative of the model's decision logic.

Illustration of an inference step with Transformer-based code generator.

Datasets

The researchers evaluated WhyGen using:

  • CodeGPT: An open-source code generator based on the GPT-2 architecture.
  • PY150: A standard dataset consisting of 150,000 Python repositories from GitHub.
  • Evaluation Set: They randomly selected 10,000 test snippets and identified 3,842 specific cases where the model "recited" training data (generated an exact match) to serve as a ground-truth benchmark for retrieval.

Key Insights and Ideas

  • Activation as Identity: The paper's fundamental "aha moment" is that internal activation patterns are a reliable proxy for a model's "thought process." If two different inputs trigger similar internal neuron patterns, the model is likely treating them as functionally similar.
  • The First Token Matters Most: The researchers found that they only needed to fingerprint the first generated token. Because the first token's generation involves processing the entire user prompt, its activations effectively encode the context of the whole request.
  • Efficiency is Feasible: By choosing a small subset of high-variance neurons (only 100 in their implementation), the authors turned a complex interpretability problem into a simple vector search problem that is fast enough for real-time use in an IDE.

Key Results

WhyGen demonstrates that transparency doesn't have to come at the cost of performance:

  • High Accuracy: In tests to find the exact training example being recited, WhyGen achieved a top-10 accuracy of 81.21% and a top-1 accuracy of 73.84%.
  • Superior to Baselines: As shown in the table below, the "high-variance" neuron selection strategy significantly outperformed random selection or selecting neurons based on minimum/maximum activation values.
The accuracy of WhyGen and its variants to include the recited code in the retrieved training examples.
  • Minimal Overhead: While code generation took roughly 360ms, WhyGen’s retrieval process added only 6ms of latency. This makes it highly practical for integration into developer tools.

Limitations and Future Work

While promising, the authors acknowledge several areas for growth:

  • Scalability to LLMs: The study used GPT-2; further testing is needed to see how these fingerprints behave in massive models like GPT-4 or specialized code models with billions of parameters.
  • Beyond Transformers: The current implementation is tailored for Transformer architectures. The authors plan to extend support to other structures like CNNs and RNNs.
  • Causal Links: Currently, WhyGen finds similar examples. Future work aims to investigate the direct causal relationship—proving that a specific training example is the reason a certain snippet was generated.

Summary updated

BibTeX
@inproceedings{yan2022whygen,
  author = {Weixiang Yan and Yuanchun Li},
  title = {{WhyGen: Explaining ML-powered Code Generation by Referring to Training Examples}},
  booktitle = {ICSE-Companion},
  year = {2022},
  doi = {10.1145/3510454.3516866},
}

Loading related content...

Figure