Trained Without My Consent: Detecting Code Inclusion In Language Models Trained on Code

Vahid Majdinasab, Amin Nikanjam, Foutse KhomharXiv 2024 · DOI

TL;DR

Researchers have developed TraWiC (Trained Without My Consent), a new tool that detects if specific code was included in a Large Language Model's (LLM) training set with over 83% accuracy. By leveraging membership inference attacks and the "Fill-In-the-Middle" technique, TraWiC significantly outperforms traditional code clone detection tools, providing a much-needed auditing mechanism for intellectual property protection in the age of AI coding assistants.

What Problem Does This Paper Address?

The rise of LLMs like GitHub Copilot and Amazon CodeWhisperer has revolutionized software development, but it has also opened a legal and ethical Pandora’s box. These models are trained on massive datasets scraped from public repositories, often including code with restrictive licenses or proprietary logic.

The core challenge is auditing. Because training datasets are rarely disclosed, developers have no way to prove their code was used without permission. Traditional "Code Clone Detection" (CCD) tools—which look for near-identical copies of code—fail here because LLMs don't just copy-paste; they memorize patterns and generalize. If a model generates a snippet that looks like yours, is it a "clone," or did the model simply learn a common way to solve a problem? The authors aim to bridge this gap by creating a reliable way to detect "membership"—proving a specific script was part of the training data.

Methods

The authors introduce TraWiC, a model-agnostic approach based on Membership Inference Attacks (MIA). The intuition is simple: a model will be much better at predicting "masked" parts of a code snippet if it has seen that exact snippet thousands of times during training.

The TraWiC pipeline follows four stages:

  1. Pre-processing: It parses a code script and extracts two types of identifiers:
  2. Syntactic: Variable, function, and class names.
  3. Semantic: Strings, statement-level comments, and docstrings.
  4. Inference: It uses the Fill-In-the-Middle (FIM) objective. For each identifier, it provides the model with the code before (prefix) and after (suffix) the identifier and asks the model to predict the missing piece.
  5. Comparison: It compares the model's prediction to the original.
  6. For syntactic identifiers, it looks for an exact match.
  7. For semantic identifiers (which are closer to natural language), it uses Levenshtein distance (fuzzy matching) to account for slight variations in wording.
  8. Classification: Finally, it feeds the "hit rates" (how often the model guessed correctly) into a Random Forest classifier to determine if the script or the entire project was likely in the training set.

Datasets

The study evaluated TraWiC across three prominent LLMs: SantaCoder, Llama-2 (7B), and Mistral (7B).

  • TheStack: Used to evaluate SantaCoder. Since the training criteria for SantaCoder are public, the authors could accurately recreate the "ground truth" of what was and wasn't included.
  • Custom GitHub Dataset: To test Llama-2 and Mistral (whose training data is secret), the authors collected Python repositories published after September 2023. They then fine-tuned these models on a subset of this new data, creating a controlled environment where they knew exactly which files were "members" of the training set.
  • The evaluation involved over 10,700 code files from 314 projects.

Key Insights and Ideas

  • Semantic Fingerprints: One of the most important findings is that comments and docstrings are high-signal indicators. While code logic might be generic, the way a specific developer explains that logic in natural language is often unique. TraWiC exploits this "individual coding style."
  • Efficiency over CCD: Traditional clone detection requires pair-wise comparisons (comparing every generated snippet against every known snippet), which is computationally explosive. TraWiC only requires querying the model itself, making it much faster for large-scale auditing.
  • Rare Elements Matter: The model's ability to predict rare or infrequent variable names is a much stronger signal of memorization than its ability to predict common ones like i or temp.
  • Project-Level Granularity: Even if a model fails to "remember" a single file perfectly, analyzing an entire project (multiple files) allows TraWiC to detect inclusion with near-certainty.

Key Results

  • Superior Accuracy: TraWiC achieved an accuracy of 83.87% in detecting code inclusion for SantaCoder.
  • Outperforming Baselines: In the same scenario, the industry-standard clone detector NiCad only managed 47.64%, and JPlag reached 55%.
  • High Recall: For project-level detection, TraWiC achieved a sensitivity (recall) of up to 99.19%, meaning it almost never misses a project that was actually included.
  • Robustness: The system remained effective even when moderate levels of "noise" (simulated code obfuscation) were introduced, maintaining an F-score of ~83% even when half of the identifiers were changed.

Limitations and Future Work

  • Obfuscation: While robust to moderate changes, TraWiC’s performance degrades significantly under heavy, systematic data obfuscation (e.g., changing nearly all variable names and comments).
  • FIM Requirement: The method currently requires the model to support the Fill-In-the-Middle objective. While common in code LLMs, some models might require fine-tuning to support this task.
  • Ground Truth for Training: The classifier itself needs some ground truth (known members and non-members) to be trained initially.
  • Future Directions: The authors suggest using Deep Reinforcement Learning to compare Abstract Syntax Trees (AST) rather than just text, which could make the tool even more resistant to code obfuscation.

Summary updated

BibTeX
@misc{majdinasab2024trained,
  author = {Vahid Majdinasab and Amin Nikanjam and Foutse Khomh},
  title = {{Trained Without My Consent: Detecting Code Inclusion In Language Models Trained on Code}},
  year = {2024},
  eprint = {2402.09299},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.09299},
}
Figure