TL;DR
This paper introduces GenCode, a novel framework for data augmentation in deep learning for code understanding. By generating diverse code candidates and intelligently selecting the most informative ones based on model loss, GenCode significantly boosts the accuracy and robustness of code models, outperforming existing methods.
What Problem Does This Paper Address?
The field of code intelligence has seen tremendous progress with pre-trained deep learning models for code understanding. However, a critical bottleneck remains: effectively augmenting code data to improve model training. While data augmentation is common in other domains like computer vision and NLP, its application to code is less explored. Existing methods often struggle to significantly improve model performance or robustness, and the process of manually preparing high-quality training data is expensive and time-consuming, requiring domain expertise. This paper addresses the need for an automated, generic, and effective data augmentation framework for code understanding tasks.
Methods
GenCode employs a two-stage "generation-and-selection" paradigm:
- Generation: It utilizes a diverse set of atomic code augmentation operators. These operators are categorized into two types:
- Semantic/Syntax-Preserving: These methods, like API renaming and duplication, modify code while maintaining its original functionality and syntax.
- Syntax-Breaking: These methods, inspired by NLP techniques like random swap and synonym replacement, can alter the code's syntax, potentially introducing challenges that force models to learn more robust representations.
- Selection: After generating a pool of augmented code candidates, GenCode selects the most valuable data for training. This selection is guided by an "influence score," which is defined as the loss value of the code sample produced by the current code model. The core assumption is that samples with higher loss are more informative and contribute more to model generalization. The framework then trains the model using this curated subset of data.
Datasets
The paper evaluates GenCode on four code understanding tasks using the following datasets:
- Authorship Attribution: Google Code Jam (GCJ)
- Bug Detection: Refactory
- Problem Classification: Java250
- Clone Detection: BigCloneBench
Key Insights and Ideas
- High-loss samples are gold: The fundamental idea that samples with higher loss during training are more informative for model generalization is crucial. This challenges the intuition that only "easy" samples are useful.
- Generic applicability: GenCode's generation-and-selection approach is designed to be model-agnostic and task-agnostic, making it broadly applicable to various code understanding tasks and models.
- Syntax-breaking can be beneficial: Contrary to expectations, methods that slightly break code syntax can still be useful for training robust code models, provided they are selected intelligently.
- Influence score matters: The choice of how to measure "importance" (e.g., maximum loss vs. minimum loss vs. random selection) significantly impacts performance, with maximum loss proving most effective.
Key Results
GenCode demonstrates significant improvements across various code understanding tasks and models:
- Accuracy:
- For pre-trained code models, GenCode achieves an average accuracy improvement of 3.06% over models trained without data augmentation and 2.92% over the SOTA method MixCode.
- For code-specific LLMs (StarCoder2 and Qwen2.5-Coder), GenCode yields average accuracy improvements of 0.93% and 0.84%, respectively.
- Robustness:
- GenCode achieves an average adversarial robustness improvement of 8.42% over models trained without data augmentation and 4.90% over MixCode.
- For LLMs, robustness gains are more modest, with improvements of less than 1%.
- Convergence: GenCode consistently outperforms other methods in terms of convergence speed, reaching higher accuracy earlier in training.
- Code Embeddings: Visualizations using PCA show that GenCode trains code embeddings that are more clearly distributed, leading to better discrimination between classes (e.g., buggy vs. correct code).
Limitations and Future Work
- Computational Cost: GenCode incurs the highest computational cost among the tested data augmentation methods, primarily due to its two-stage process. While text-oriented methods are more efficient, GenCode's performance benefits outweigh this cost in many scenarios.
- LLM Robustness: While GenCode improves LLM accuracy, its impact on robustness for these larger models is limited.
- Gradient-based methods for robustness: For natural robustness, gradient-based data selection methods (like BADGE) sometimes outperform GenCode, suggesting that exploring combinations of GenCode's approach with gradient-based uncertainty might be a promising future direction.
- Clone Detection for LLMs: The paper notes that clone detection remains a challenging task for LLMs, and further research is needed in this area.
Summary updated