TL;DR
CodeGenGuard is a robust watermarking framework designed to protect the intellectual property of code generation models. By leveraging semantic-preserving code transformations and a novel "dual-LoRA" shadow training scheme, it embeds hidden signatures that survive model theft and extraction attacks without degrading the quality of the generated code.
The Threat to Code Language Models
Training high-performing code language models (LMs) like GitHub Copilot or DeepSeek-Coder requires massive investments in compute, curated datasets, and specialized human annotations. This makes the resulting models highly valuable intellectual property. However, once released, these models face two major threats:
- Unauthorized Redistribution: An adversary makes minor tweaks to the model and claims ownership.
- Model Extraction (Distillation): An adversary queries the model to train a "student" model that mimics the original's behavior, effectively stealing the "knowledge" without having access to the underlying weights.
Traditional watermarking—often used in image classification—fails here because code must adhere to strict syntactic and semantic rules. You cannot simply "corrupt" a label or add imperceptible noise; you must change the code's style or structure without breaking its functionality. Existing code watermarks often struggle with a "utility-robustness dilemma," where the watermark either hurts the model's coding ability or is easily erased by fine-tuning.
How CodeGenGuard Works
To resolve these issues, the authors propose CodeGenGuard, which treats watermarking as a controlled "backdoor" behavior.
Its architecture relies on several key innovations:
- Semantic-Preserving Transformations (SPTs): Instead of injecting arbitrary text, the watermark is a specific coding pattern that doesn't change what the code does. For example, changing an empty list initialization from
x = []tox = list().
- Dead-Code Data Augmentation: Many SPTs are rare in the wild. To ensure the model learns them well, CodeGenGuard uses a Probabilistic Context-Free Grammar (PCFG) to inject "dead code" (non-executing blocks) containing the target SPTs into the training data.
- Optimizable Triggers: Rather than a fixed string, the framework uses the PEZ algorithm to find a discrete sequence of tokens that acts as a "secret key" to trigger the watermark.
- Dual-LoRA Shadow Training: This is the core robustness feature. To defend against extraction attacks, the authors train a "shadow model" (a student) alongside the watermarked model. They optimize the trigger so that the watermark is inherited by the student during distillation. By using LoRA (Low-Rank Adaptation), they can train both models simultaneously with only a ~1.5% increase in trainable parameters, making it feasible for large models.
- Auxiliary Prompts: During verification, the system inserts comments (e.g.,
# initialize an empty list) to provide context, guiding the generative model to produce the specific watermarked pattern more reliably.
Key Insights and Performance
The researchers evaluated CodeGenGuard using CodeSearchNet (CSN-Python) for primary experiments, HumanEval and MBPP for utility benchmarks, and MultiPL-E to test generalizability across languages like Java and JavaScript.
- The "Sticky" Watermark: The most significant insight is that for a watermark to survive model extraction, it must be part of the model's fundamental output distribution. By using shadow training, the authors "pre-distill" the watermark, ensuring it transfers to any student model an attacker might try to train.
- Context is King: Generative models are highly sensitive to prompts. The use of "Auxiliary Prompts" during verification narrows the search space for the model, significantly boosting the detection rate (reducing false negatives) without needing to change the model's weights further.
- High Confidence & Resilience: CodeGenGuard achieved p-values as low as $10^{-31}$, indicating near-certainty in ownership verification. The watermark successfully survived logits-based distillation and fine-tuning attacks. In scenarios where baseline watermarks vanished after a few epochs of fine-tuning, CodeGenGuard's signature remained statistically significant.
- Superior Utility: Unlike baselines like CodeMark (which saw up to a 10% drop in performance), CodeGenGuard maintained nearly identical Pass@1 scores on HumanEval and MBPP compared to "clean" unwatermarked models.
Limitations and Future Work
While highly effective, the authors identify a few areas for improvement. The optimized triggers can sometimes look like unnatural "gibberish" code, which might be detectable by a human auditor (though they evade automated detection). Furthermore, while robust against extraction, the model remains potentially vulnerable to emerging "backdoor cleaning" techniques like weight merging (WAG). Finally, as a learning-based approach, it lacks the formal mathematical proofs of robustness found in some cryptographic watermarks.
Future work will focus on making triggers more "natural" and scaling the system to even larger models with tens of billions of parameters.
Summary updated