ClawSAT: Towards Both Robust and Accurate Code Models

Jinghan Jia, Shashank Srikant, Tamara Mitrovska, Chuang Gan, Shiyu Chang, Sijia Liu, Una-May O'Reilly

TL;DR

CLAWSAT is a new framework that breaks the traditional trade-off between accuracy and robustness in code models. By integrating adversarial code obfuscations into contrastive pre-training (CLAW) and employing a staggered schedule for fine-tuning (SAT), the authors achieve state-of-the-art performance that is both more accurate on clean code and significantly more resilient to adversarial attacks.

What Problem Does This Paper Address?

Large Language Models (LLMs) for code are increasingly used for tasks like code summarization and completion. However, these models are surprisingly fragile. Small, semantics-preserving changes—known as code obfuscations—can easily fool them. For example, renaming a variable or inserting a "dead" print statement shouldn't change what a program does, but it can completely derail a model's prediction.

Two types of semantics-preserving transformations: replace and insert.

In machine learning, there is a well-known "robustness-accuracy trade-off": making a model resistant to these adversarial attacks usually degrades its performance on normal, "clean" data. This paper investigates whether we can pre-train code models to be robust from the start and, crucially, whether we can maintain or even improve that robustness during task-specific fine-tuning without sacrificing accuracy.

Methods

The authors propose a two-stage framework called CLAWSAT:

  1. CLAW (Contrastive Learning with Adversarial Views): During self-supervised pre-training, the model learns by comparing different "views" of the same code. While standard methods use random transformations, CLAW uses a bi-level optimization process to generate adversarial views. These are "hard" examples specifically designed to be difficult for the current model to recognize as the same code. By forcing the model to map these adversarial versions to the same representation as the original code, it learns a much more robust feature space.
The CLAW optimization process generates 'hard' positive examples to enforce representation invariance.
  1. SAT (Staggered Adversarial Training): The authors observed that aggressive adversarial training during fine-tuning often "overwrites" the useful features learned during pre-training. SAT introduces a staggered schedule (a frequency parameter $\tau$) for adversarial updates. Instead of attacking the model at every training step, it only does so periodically. This "sweet spot" allows the model to adapt to the specific downstream task while retaining its foundational robustness.
Schematic overview of the CLAWSAT framework combining CLAW and SAT.

Datasets

The models were pre-trained and evaluated on large-scale, standard benchmarks:

  • Pre-training: The CodeSearchNet (CSN) dataset for Python (~500K methods) and Java (~600K methods).
  • Fine-tuning/Evaluation:
    • PY150 and JAVA-C2S for code summarization.
  • Py-CSN for code completion.
  • BigCloneBench (Java) for code clone detection.

Key Insights and Ideas

  • Adversarial Views as "Hard Positives": In contrastive learning, not all positive pairs are created equal. Adversarial obfuscations act as "hard positives" that push the model to ignore superficial syntactic changes and focus on deeper semantic meaning.
  • Loss Landscape Flatness: The authors found that CLAW leads to a significantly "flatter" loss landscape compared to baselines like ContraCode. In deep learning, a flatter landscape is a strong indicator of better generalization and easier fine-tuning.
Comparison of loss landscapes: CLAW (left) shows a much flatter profile than ContraCode (right).
  • Interpretability through EBE: Using "Explanation-by-Example" (EBE), the researchers showed that CLAW-trained models are more likely to map a perturbed code snippet back to its correct, unperturbed training example (68% success) compared to standard contrastive models (57%).
  • The Fine-tuning "Sweet Spot": Robustness isn't just about the data; it's about the schedule. Staggering the adversarial attacks during fine-tuning prevents the model from "forgetting" its robust pre-trained representations.

Key Results

CLAWSAT consistently outperformed the state-of-the-art baseline (ContraCode) across all tested tasks:

  • Code Summarization (Python): Achieved an 11% absolute gain in robustness (ROB-F1) and a 6% gain in accuracy (GEN-F1).
  • Code Completion: Showed a 3% improvement in robustness and a 2% improvement in accuracy.
  • Architecture Agnostic: The benefits were consistent whether using LSTM-based decoders or Transformers.
  • Stability: The performance gains remained stable even as the strength of the adversarial attacks (number of sites modified in the code) increased.

Limitations and Future Work

The authors note that while CLAWSAT is highly effective, the discrete nature of code (unlike continuous image data) presents unique challenges. They suggest that:

  • Stronger Attacks: Future work could explore even more sophisticated adversarial attack methods for code to further test these models.
  • Theoretical Grounding: While the empirical results are strong, establishing a more rigorous theoretical framework for why staggered training works so well in the discrete code domain is an open area for research.
  • Vision vs. Code: The paper hints that code models might benefit from "milder" adversarial training compared to vision models, a phenomenon that warrants further investigation.

Summary updated

BibTeX
@inproceedings{jia2023clawsat,
  author = {Jinghan Jia and Shashank Srikant and Tamara Mitrovska and Chuang Gan and Shiyu Chang and Sijia Liu and Una-May O'Reilly},
  title = {{ClawSAT: Towards Both Robust and Accurate Code Models}},
  booktitle = {SANER},
  year = {2023},
  doi = {10.1109/saner56733.2023.00029},
}
Figure