CodeFort: Robust Training for Code Generation Models

Yuhao Zhang, Shiqi Wang, Haifeng Qian, Zijian Wang, Mingyue Shang, Linbo Liu, Sanjay Krishna Gouda, Baishakhi Ray, Murali Krishna Ramanathan, Xiaofei Ma, Anoop Deoras

Abstract

Large Language Models (LLMs) for code generation exhibit significant brittleness when faced with minor, semantic-preserving perturbations in input prompts. We present CodeFort, a training framework designed to enhance the robustness of these models. By integrating paired data augmentation, specialized masking, and adversarial/contrastive learning objectives, CodeFort significantly improves model reliability across various perturbation types—such as docstring changes and syntax variations—without degrading performance on clean inputs.

Introduction: The Brittleness of Code LLMs

While models like StarCoder and CodeGen demonstrate impressive coding capabilities, they are often sensitive to trivial changes. A model may correctly solve a prompt but fail if a variable name is changed or a typo is introduced in the docstring. These "semantic-preserving perturbations" highlight a gap between a model's nominal performance and its real-world reliability.

Example of semantic-preserving perturbations where variable names are changed (e.g., G to Y) while maintaining the underlying logic.

The challenge in addressing this for code generation is twofold. First, robustness in generation is about coherence rather than invariance; if a prompt renames a variable, the output must follow suit. Second, standard robustness techniques are difficult to apply to Causal Language Modeling (CLM) because perturbations often alter sequence lengths, complicating token-by-token comparisons.

The CodeFort Framework

CodeFort employs a multi-faceted approach to "fortify" generative models:

  • Paired Dataset Generation & Batch Augmentation: Every clean code snippet is paired with a perturbed version. These pairs are included in the same training batch (MBA), allowing the model to learn the relationship between clean and noisy contexts.
  • Masking Unnatural Tokens: To prevent the model from learning to generate noise (like typos), a masking mechanism ensures the model understands noisy input without being penalized for not reproducing it in the output.
  • Adversarial Logits Pairing (ALP & ALPD): These methods minimize the KL divergence between predictions for clean and noisy inputs. A specialized variant, ALPD, uses "name-dropout" to encourage the model to prioritize logic over specific variable names.
  • Fine-Grained Contrastive Learning: Unlike sequence-level methods, ContraToken and ContraName operate at the token and variable-name levels, matching the way generative models actually predict code.

Evaluation and Results

The framework was evaluated using the ReCode benchmark, which applies perturbations (Docstring, Function-name, Code-syntax, and Code-format) to the HumanEval and MBPP datasets.

Detailed performance comparison of CodeFort (Ours) against baselines across different model sizes and perturbation categories.

Key findings include:

  • Significant Robustness Gains: Across CodeGen models (350M to 6B), the average robust pass rate (RP10@1) improved from 14.79 to 21.74.
  • Resilience to Syntax Changes: CodeFort achieved its most notable success in the "Syntax" category, where the performance drop rate was reduced from 95.02% to 54.95%.
  • Preservation of Nominal Performance: The models maintained their high performance on clean ("nominal") prompts, avoiding the "robustness tax" often seen in other domains.
  • Superiority over Simple Augmentation: CodeFort's specialized loss functions (ALP/ALPD) provided significantly better results than standard data augmentation alone.

Conclusion

CodeFort demonstrates that robustness in code generation requires a shift from simple invariance to context-aware coherence. By leveraging fine-grained alignment and specialized training objectives, it is possible to build models that are not only powerful but also resilient to the noise inherent in real-world programming tasks. Future work will explore scaling these strategies to larger model architectures and broadening the scope of context-sensitive perturbations.

Summary updated

BibTeX
@inproceedings{zhang2024codefort,
  author = {Yuhao Zhang and Shiqi Wang and Haifeng Qian and Zijian Wang and Mingyue Shang and Linbo Liu and Sanjay Krishna Gouda and Baishakhi Ray and Murali Krishna Ramanathan and Xiaofei Ma and Anoop Deoras},
  title = {{CodeFort: Robust Training for Code Generation Models}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.findings-emnlp.303},
}
Figure