Towards Efficient Fine-Tuning of Pre-trained Code Models: An Experimental Study and Beyond

Ensheng Shi, Yanlin Wang, Hongyu Zhang, Lun Du, Shi Han, Dongmei Zhang, Hongbin Sun

Abstract

Fine-tuning large-scale pre-trained models has become the standard approach for achieving state-of-the-art performance in software engineering tasks. However, the computational cost of full-parameter fine-tuning is increasingly prohibitive. This study investigates the internal dynamics of pre-trained code models (PCMs) during the fine-tuning process. By analyzing the "knowledge geography" of these models, we demonstrate that lower layers, which capture lexical and syntactic information, remain largely static. Based on these findings, we propose Telly, a layer-freezing strategy that reduces training parameters and time by up to 90% while maintaining or improving performance through effective regularization.

Introduction

The "Pre-train then Fine-tune" paradigm has revolutionized tasks such as code search, summarization, and bug detection. Despite its success, the process is often treated as a black box, requiring massive GPU resources—fine-tuning a model like CodeT5 can exceed 40 hours on high-end hardware. This approach is not only computationally expensive but also runs contrary to the goals of low-carbon deep learning.

Researchers from Xi’an Jiaotong University and Microsoft recently pulled back the curtain on this process. Their work addresses two fundamental questions:

  1. How is specific code knowledge (syntax vs. logic) distributed across model layers?
  2. Which layers actually evolve when adapting to a specific downstream task?

Methodology

To map the internal state of PCMs, the researchers employed two diagnostic frameworks:

1. Probing Tasks

Four diagnostic tasks were designed to identify where specific types of information are stored:

  • Lexical Probing: Identifying token types (e.g., distinguishing keywords from identifiers).
  • Syntactic Probing: Matching code snippets to their corresponding Abstract Syntax Trees (ASTs).
  • Semantic Probing: Identifying code clones (different implementations with identical logic).
  • Structural Probing: Predicting Cyclomatic Complexity to assess the model's grasp of execution pathways.

2. Representational Similarity Analysis (RSA)

RSA was used to compare the internal representations of a model before and after fine-tuning. By calculating the correlation between layers across different stages of training, the researchers could pinpoint exactly which layers were being modified to accommodate new tasks.

The Telly Strategy

The analysis revealed a "Knowledge Hierarchy" similar to a compiler: lexical information resides in the bottom layers, syntactic information in the middle, and structural information at the top. Crucially, RSA showed that the bottom 9 layers of a standard 12-layer model maintain a correlation higher than 0.8 after fine-tuning, suggesting that most of the "learning" is concentrated in the final two layers.

Based on this, the authors introduced Telly-$K$, a strategy where the bottom $K$ layers of the model are frozen. Only the top $12-K$ layers and the task-specific head are updated during training.

Experimental Evaluation

The study evaluated Telly across five diverse downstream tasks using industry-standard benchmarks, including CodeSearchNet (Python/Ruby), BigCloneBench (Java), and the Github Java Corpus.

Key Results

  • Efficiency Gains: Freezing the bottom 9 layers (Telly-9) reduced trainable parameters by 83% and decreased training time by approximately 84%.
  • Performance Stability: On tasks like Code Search, Telly-9 achieved Mean Reciprocal Rank (MRR) scores nearly identical to full fine-tuning. In some instances, performance actually improved, suggesting that freezing layers acts as a form of regularization that prevents overfitting on smaller datasets.
  • The Optimal Configuration: The researchers identified Telly-7 as the ideal "sweet spot" for general-purpose use, offering a significant reduction in compute costs with negligible impact on accuracy across all tested languages and tasks.

Discussion and Limitations

While Telly offers a clear path toward more sustainable AI development, some limitations remain. The current study focused primarily on Transformer encoders like UniXcoder and GraphCodeBERT; further research is required to determine if these findings apply to massive decoder-only models such as GPT-4 or CodeLlama. Additionally, the current method requires the user to select the hyperparameter $K$ manually. Future iterations could involve dynamic freezing algorithms that automatically detect layer convergence.

Conclusion

The internal layers of pre-trained code models already possess a robust understanding of code syntax and structure. By focusing fine-tuning efforts on the top layers—where task-specific logic is refined—developers can drastically reduce the environmental and financial costs of model deployment without sacrificing quality. The message for the industry is clear: stop retraining the foundations and start teaching the task.

Summary updated

BibTeX
@inproceedings{shi2023towards,
  author = {Ensheng Shi and Yanlin Wang and Hongyu Zhang and Lun Du and Shi Han and Dongmei Zhang and Hongbin Sun},
  title = {{Towards Efficient Fine-Tuning of Pre-trained Code Models: An Experimental Study and Beyond}},
  booktitle = {ISSTA},
  year = {2023},
  doi = {10.1145/3597926.3598036},
}
Figure