NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Structure-aware Fine-tuning for Code Pre-trained Models

Jiayi Wu, Renyu Zhu, Nuo Chen, Qiushi Sun, Xiang Li, Ming Gao

Introduction

Programming languages are fundamentally different from natural languages due to their strict, hierarchical structures. These structures, typically represented as Abstract Syntax Trees (ASTs), dictate the logic and flow of code. While modern Code Pre-trained Models (CodePTMs) like CodeBERT and CodeT5 have shown remarkable capabilities in understanding code, they often struggle to fully leverage these structural properties during the fine-tuning phase for specific downstream tasks.

Existing methods to incorporate structural awareness often involve complex architectural changes or are restricted to the pre-training phase. To address this, researchers have introduced Structure-aware Fine-tuning (SAT), a plug-and-play approach designed to inject structural knowledge directly into the fine-tuning process without requiring architectural modifications.

Methodology: The SAT Framework

The core objective of SAT is to align a model's internal attention mechanisms with the actual syntactic structure of the code. This is achieved through a multi-task learning objective that incorporates a novel Structure Loss.

1. Extracting Ground Truth Structure

The process begins by parsing the source code into an AST. From this tree, the researchers calculate the shortest path distance between leaf nodes (tokens). This results in a Distance Matrix that serves as the "ground truth" for how tokens should relate to one another based on the language's syntax.

2. Capturing Learned Attention

Simultaneously, the model’s internal Attention Matrix is extracted from the first Transformer layer. This matrix represents the relationships the model has learned to prioritize during processing. The authors identified the first layer as the most critical for capturing structural information.

3. Aligning Structures via Sinkhorn Divergence

To bridge the gap between the mathematical representation of the AST and the model's attention scores, SAT employs Sinkhorn Divergence. This is a computationally efficient version of the Wasserstein distance, used to measure the discrepancy between the two matrices. Because attention scores and tree distances exist on different scales, a small, learnable linear layer—the Structure Encoder—is introduced to align them.

4. Multi-task Fine-tuning

During training, the model optimizes a joint objective that combines the standard task loss (such as cross-entropy) with the new structure-aware loss. This forces the model to maintain structural integrity while learning the specific requirements of the downstream task.

Experimental Evaluation

The efficacy of SAT was evaluated using the CodeXGLUE benchmark across two primary categories:

  • Code Summarization: Generating natural language descriptions for snippets in Ruby, JavaScript, Go, Python, and Java using the CodeSearchNet dataset.
  • Code Translation: Bidirectional translation between Java and C# using a dataset of 10,000 parallel snippets.

The researchers tested SAT across four prominent models: RoBERTa, CodeBERT, GraphCodeBERT, and CodeT5. Additionally, they conducted "low-resource" experiments, training models on varying fractions (20% to 80%) of the available data to test the method's robustness.

Key Findings

Performance Gains Across the Board

SAT consistently improved performance for all tested models. One of the most notable results was seen with RoBERTa, a model originally pre-trained on natural language. When augmented with SAT for JavaScript summarization, RoBERTa saw a +3.78 BLEU-4 improvement, allowing it to outperform the standard CodeBERT model.

The "Weak Model" Equalizer

The study suggests that SAT acts as a powerful equalizer. Models that were not specifically pre-trained on code benefit the most, as the structure loss effectively "teaches" them the syntactic rules they missed during their initial training phase.

Robustness in Data-Scarce Scenarios

Perhaps the most significant finding is that the benefits of SAT become more pronounced as the amount of training data decreases. In low-resource settings, the structural "prior" provided by the AST acts as a vital guide, preventing the model from overfitting to noise and helping it maintain logical consistency when examples are scarce.

Qualitative Improvements

Beyond metrics, case studies revealed that SAT-enhanced models produce more logically sound code. They are significantly better at maintaining correct variable assignments and preserving the original logic flow, whereas standard models are more prone to "hallucinating" incorrect identifiers or breaking the logic of the snippet.

Conclusion and Future Work

Structure-aware Fine-tuning (SAT) provides a simple yet effective way to enhance CodePTMs by grounding their attention mechanisms in the inherent syntax of programming languages. While highly effective for code-to-code and code-to-text tasks, the current implementation is limited by its reliance on source-side ASTs. This means it cannot yet be applied to text-to-code generation, where the input (natural language) lacks a formal syntax tree. Future research will focus on bridging this gap to bring structural awareness to the full spectrum of code intelligence tasks.

Summary updated

BibTeX
@inproceedings{wu2024structure,
  author = {Jiayi Wu and Renyu Zhu and Nuo Chen and Qiushi Sun and Xiang Li and Ming Gao},
  title = {{Structure-aware Fine-tuning for Code Pre-trained Models}},
  booktitle = {International Conference on Computational Linguistics},
  year = {2024},
}
Figure