ReCode: Robustness Evaluation of Code Generation Models

Shiqi Wang, Zheng Li, Haifeng Qian, Chenghao Yang, Zijian Wang, Mingyue Shang, Varun Kumar, Samson Tan, Baishakhi Ray, Parminder Bhatia, Ramesh Nallapati, Murali Krishna Ramanathan, Dan Roth, Bing Xiang

Introduction

Large Language Models (LLMs) specialized for code, such as CodeGen and InCoder, have revolutionized software development by powering intelligent completion tools like GitHub Copilot. Despite their impressive performance on standard benchmarks, these models often exhibit "brittle" behavior: minor, semantically irrelevant changes to a prompt—such as a different variable name or a slight rephrasing of a docstring—can lead to drastically different and often incorrect outputs. While robustness has been a central theme in natural language processing research, it has remained largely unexplored in the context of code generation. This paper introduces ReCode, a comprehensive framework designed to quantitatively evaluate the robustness of code generation models against natural perturbations.

The ReCode Framework

The core of ReCode is a suite of over 30 semantic-preserving transformations applied to existing code generation datasets. These transformations are designed to mimic the varied ways humans write code and comments, ensuring that the underlying logic required to solve the task remains unchanged.

Semantic-Preserving Transformations

The perturbations are categorized into four main areas:

  1. Docstrings: Techniques include paraphrasing via back-translation, altering character casing, and substituting synonyms.
  2. Function and Variable Names: Transformations involve switching naming conventions (e.g., snake_case to camelCase) or using models like CodeBERT to suggest contextually appropriate alternative names.
  3. Code Syntax: This includes structural changes like replacing for loops with while loops or reordering operands in logical expressions.
  4. Code Format: Adjustments to indentation, line splitting, and the placement of comments.
Examples of docstring perturbations used in the ReCode benchmark.

Robustness Metrics

To move beyond standard accuracy metrics like Pass@k, ReCode introduces three specialized metrics:

  • Robust Pass ($RP_s@k$): A strict metric where a model is considered successful only if it generates a correct solution for the original prompt and all $s$ perturbed versions.
  • Robust Drop ($RD_s@k$): Measures the relative percentage decrease in performance when moving from clean to perturbed prompts, highlighting the model's sensitivity.
  • Robust Relative ($RR_s@k$): Quantifies general instability by tracking how frequently a model's correctness "flips" under perturbation.

Experimental Setup

ReCode is evaluated using two prominent Python benchmarks: HumanEval (164 hand-written problems) and MBPP (Mostly Basic Python Problems, ~500 crowd-sourced tasks). The evaluation covers both docstring-to-code generation and function completion tasks, where the model must robustly finish a partially written solution.

Analysis and Key Findings

The evaluation reveals significant vulnerabilities in state-of-the-art models. One of the most striking findings is the sensitivity to syntax. When syntax perturbations (like loop transformations) were applied to the MBPP dataset, the Robust Drop ($RD_5@1$) often exceeded 90%. This suggests that models may rely more on surface-level pattern matching than on a deep understanding of program logic.

Example of a model failure triggered by a simple function name perturbation.

As illustrated in the figure above, changing a function name from remove_lowercase to removeLowercase caused the model to generate a completely incorrect implementation that converted the string to lowercase instead of removing lowercase characters.

Key Insights:

  • Scale vs. Stability: While larger models (e.g., CodeGen-16B) generally achieve higher absolute scores, they still exhibit significant relative performance drops under perturbation. Scaling improves capability but does not inherently solve the robustness problem.
  • Training Diversity: Models like CodeGen, trained on more diverse datasets, showed better overall robustness compared to models like InCoder, likely due to exposure to a wider variety of coding styles during pre-training.
  • Dataset Difficulty: The MBPP dataset proved more challenging for robustness than HumanEval, likely due to the higher natural variance in its crowd-sourced prompts.

Conclusion and Future Work

ReCode highlights a critical gap between the accuracy and the reliability of current code generation models. While these models are powerful, their extreme sensitivity to minor prompt variations poses risks for real-world deployment. Future research should leverage the ReCode framework not only for evaluation but also for mitigation, such as through robust training via data augmentation. Expanding the benchmark to include strictly typed languages and more complex, repository-level tasks remains an important next step in building truly dependable AI for software engineering.

Summary updated

BibTeX
@inproceedings{wang2023recode,
  author = {Shiqi Wang and Zheng Li and Haifeng Qian and Chenghao Yang and Zijian Wang and Mingyue Shang and Varun Kumar and Samson Tan and Baishakhi Ray and Parminder Bhatia and Ramesh Nallapati and Murali Krishna Ramanathan and Dan Roth and Bing Xiang},
  title = {{ReCode: Robustness Evaluation of Code Generation Models}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-long.773},
}
Figure