Bridging Pre-trained Models and Downstream Tasks for Source Code Understanding

Deze Wang, Zhouyang Jia, Shanshan Li, Yue Yu, Yun Xiong, Wei Dong, Xiangke Liao

TL;DR

Researchers have developed a lightweight approach to fine-tuning pre-trained models for code understanding by combining semantic-preserving data augmentation with curriculum learning. Their method, which trains models on increasingly complex versions of the same code, allows a general-purpose natural language model (RoBERTa) to outperform specialized code models like CodeBERT on several key benchmarks.

What Problem Does This Paper Address?

Large-scale pre-trained models (PTMs) like BERT and RoBERTa have revolutionized NLP, and their successors—CodeBERT and GraphCodeBERT—have brought those gains to software engineering. However, two major hurdles remain:

  1. Semantic Mismatch: Most code PTMs simply "graft" natural language techniques onto code. They often fail to realize that two code snippets with completely different syntax (e.g., a for loop vs. a while loop) can be semantically identical.
  2. Overfitting: These models have millions of parameters. When fine-tuned on small, specialized downstream datasets, they often overfit, making them fragile when faced with slight variations in code structure.

The authors aim to "bridge" the gap between general pre-training and specific code tasks without the massive computational cost of training a new model from scratch.

Methods

The authors propose a three-pronged approach called DACL (Data Augmentation and Curriculum Learning):

  • Semantic-Preserving Data Augmentation (DA): Instead of just changing words, they use a source-to-source compiler to transform code while keeping its logic identical. This includes changing control structures (e.g., if-else to ternary), modifying APIs, and swapping variable declarations.
  • Curriculum Learning (CL): Training on all augmented data at once can be noisy and confusing for a model. The authors introduce a "curriculum" where the model starts with "easy" samples (the original code) and gradually moves to "hard" samples (code that has undergone multiple complex transformations). They use two scoring strategies:
  • Augmentation-based: Difficulty is defined by the number of transformations.
  • Class-based: For classification, they use a "leave-one-out" strategy to identify which classes are naturally harder for the model to learn.
  • Test-Time Augmentation (TTA): During evaluation, the model doesn't just look at one version of the code. It looks at the original and several transformed versions, averaging the predictions to ensure the final result is robust to syntactic "noise."

Datasets

The study evaluates the approach on three diverse benchmarks from the CodeXGLUE suite:

  • POJ104: A C/C++ dataset for algorithm classification (identifying which problem a code snippet solves).
  • BigCloneBench: A Java dataset for code clone detection (finding semantically similar code fragments).
  • CodeSearchNet: A Java dataset for code search (finding code snippets based on natural language queries).

Key Insights and Ideas

  • Augmented Code as Adversarial Examples: The authors experimentally proved that transformed code is significantly harder for models to process than original code. By treating these transformations as a learning challenge, the model learns features that are invariant to syntax.
  • The "RoBERTa Shocker": One of the most surprising findings was that RoBERTa (trained only on natural language) could beat CodeBERT (trained on code) when fine-tuned using the DACL approach. This suggests that the way we fine-tune on code might be just as important as the data used during the massive pre-training phase.
  • Curriculum Matters: Simply adding augmented data to the training set (random order) yielded lower performance than the "easy-to-hard" curriculum. The curriculum helps the model build a stable foundation before tackling confusing syntactic variations.

Key Results

The DACL approach showed significant improvements across the board:

  • Algorithm Classification: Improved Mean Average Precision (MAP) by 10.24% over the previous state-of-the-art.
  • Code Search: Achieved a new state-of-the-art Mean Reciprocal Rank (MRR) of 0.720.
  • Efficiency: In clone detection, CodeBERT trained on just 10% of the data using DACL outperformed the standard CodeBERT trained on 100% of the data.
  • Robustness: Because of Test-Time Augmentation, the models became much more resistant to "adversarial" syntactic changes that usually trip up deep learning models.

Limitations and Future Work

  • Discriminative vs. Generative: The current work focuses on understanding tasks (classification, search). The authors note that applying this to generative tasks (like writing code) is more challenging because TTA cannot be easily applied to output sequences.
  • Transformation Dependency: The success of the method depends on the quality and variety of the source-to-source transformations available for a specific programming language.
  • Future Directions: The authors suggest combining this approach with prompt-based learning to further exploit the potential of generative models in the future.

Summary updated

BibTeX
@inproceedings{wang2022bridging,
  author = {Deze Wang and Zhouyang Jia and Shanshan Li and Yue Yu and Yun Xiong and Wei Dong and Xiangke Liao},
  title = {{Bridging Pre-trained Models and Downstream Tasks for Source Code Understanding}},
  booktitle = {ICSE},
  year = {2022},
  doi = {10.1145/3510003.3510062},
}
Figure