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

AST-T5: Structure-Aware Pretraining for Code Generation and Understanding

Linyuan Gong, Mostafa Elhoushi, Alvin Cheung

Introduction

Large Language Models (LLMs) typically treat source code as a linear sequence of subword tokens, mirroring the approach used for natural language. However, source code is inherently hierarchical and governed by formal grammars. While a human developer recognizes logical blocks like function bodies or conditional statements, a standard T5 model—using greedy segmentation—often processes arbitrary spans of text that may break across logical boundaries.

AST-T5 introduces a new pretraining paradigm that incorporates the "shape" of code into the model's learning process. By leveraging Abstract Syntax Trees (ASTs) to guide data segmentation and masking, AST-T5 achieves state-of-the-art performance in code generation and transpilation for its size, frequently outperforming models with significantly larger parameter counts.

Methodology

The core innovation of AST-T5 lies in two structural modifications to the pretraining pipeline, both powered by the Tree-sitter parsing library. These methods ensure that the model respects the logical structure of code without requiring changes to the underlying Transformer architecture.

1. AST-Aware Segmentation

Standard models utilize "Greedy Segmentation," filling training blocks until a token limit (e.g., 512 tokens) is reached. This often results in logical units being split mid-statement. AST-T5 replaces this with a Dynamic Programming (DP) algorithm designed to identify optimal partition boundaries. The algorithm minimizes the number of broken AST subtrees, ensuring that the model primarily trains on complete, logically coherent blocks of code.

2. AST-Aware Span Corruption

In traditional T5 pretraining, the model learns by predicting randomly deleted text spans. AST-T5 evolves this into Subtree Corruption. Instead of masking random tokens, the model masks entire subtrees—such as a complete expression or a full loop statement. A tunable hyperparameter ($\theta$) allows researchers to control the granularity of this masking, ranging from individual variables to entire function bodies. This forces the model to learn the structural relationships between different components of a program.

Experimental Setup

Datasets

AST-T5 was pretrained on a 588GB subset of The Stack Dedup, encompassing multiple languages including Python, Java, C, C++, C#, and documentation formats like Markdown.

Evaluation Benchmarks

The model was evaluated across three primary categories:

  • Code Generation: HumanEval, MBPP, and Concode.
  • Code Transpilation: Bugs2Fix and CodeXGLUE (Java-to-C#).
  • Code Understanding: BigCloneBench (clone detection) and Defect Detection.

Key Insights and Results

Efficiency and Scale

One of the most significant findings is the efficiency of structural awareness. The 277M parameter AST-T5 model achieved a 14.0 Pass@1 score on HumanEval. This performance significantly exceeds the baseline T5 (5.2) and outperforms much larger models, such as the 6B-parameter InCoder. This suggests that structural data preparation can be as impactful as raw parameter scaling.

The Transpilation Advantage

AST-T5 demonstrated superior performance in transpilation tasks, outperforming CodeT5 by 2 points in exact match scores for Bugs2Fix and 3 points for Java-to-C# translation. Because the underlying logic (the AST) often remains consistent across different programming languages, a model trained on subtree structures is better equipped to translate the intent of the code rather than just the syntax.

Optimization of Masking Density

While standard T5 models typically mask 15% of tokens, the authors discovered a "sweet spot" at 25% masking for AST-T5. This higher density significantly boosted generation capabilities without degrading the model's performance on classification or understanding tasks.

Limitations and Future Work

Because AST-T5 is specifically optimized for code structure, it may exhibit suboptimal performance in natural language generation tasks, such as writing docstrings or comments, where AST structures are absent. Future research will explore more effective ways to mask documentation and investigate whether the advantages of structural awareness persist when scaled to multi-billion parameter models.

Summary updated

BibTeX
@inproceedings{gong2024ast,
  author = {Linyuan Gong and Mostafa Elhoushi and Alvin Cheung},
  title = {{AST-T5: Structure-Aware Pretraining for Code Generation and Understanding}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=cBWVJh5Fvf},
}
Figure