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

AST-Transformer: Encoding Abstract Syntax Trees Efficiently for Code Summarization

Ze Tang, Chuanyi Li, Jidong Ge, Xiaoyu Shen, Zheling Zhu, Bin Luo

Introduction

Code summarization—the automated generation of natural language descriptions for source code—is a critical task for improving software documentation and developer productivity. Modern approaches often leverage Abstract Syntax Trees (ASTs) to capture the structural and logical nuances of code. However, standard Transformer models face significant challenges when processing ASTs. Because ASTs are typically much longer than the raw source code, the quadratic scaling of self-attention leads to a "computational explosion." Furthermore, global attention often introduces noise, as nodes may attend to thousands of irrelevant structural elements, diluting the signal of meaningful dependencies.

Methodology

The AST-Transformer addresses these challenges by introducing a specialized attention mechanism that respects the hierarchical nature of code. The model's architecture is built upon three core innovations:

  • Simplified Linearization: Rather than using complex flattening techniques like Path Decomposition (PD) or Structure-Based Traversal (SBT), the authors utilize Pre-order Traversal (POT). This method is computationally efficient and, when combined with structural attention, matches or exceeds the performance of more complex alternatives.
  • Relation Matrices: The model explicitly defines structural relationships between node pairs. Specifically, it tracks Ancestor-Descendant relationships to capture hierarchy and Sibling relationships to capture temporal flow.
  • Tree-Structure Attention: Instead of a global "all-to-all" attention, the AST-Transformer employs a constrained mechanism. Nodes only attend to their structural neighbors within a defined distance $K$. This "locality" filter prevents the model from being overwhelmed by generic, repetitive AST structures (e.g., MethodDeclare), preserving the unique semantic information of each node.

Experimental Setup

The model was evaluated on two benchmark datasets:

  1. Java Dataset: Curated by Hu et al., containing Java methods and their corresponding comments.
  2. Python Dataset: Curated by Wan et al., providing a similar corpus for Python code.

Performance was measured using standard machine translation metrics: BLEU, METEOR, and ROUGE-L.

Results and Discussion

The AST-Transformer demonstrates superior performance across all evaluated metrics and datasets. By focusing on local structural dependencies, the model effectively filters out noise that typically plagues global attention models.

Performance comparison of AST-Transformer against baseline methods on Java and Python datasets.

As shown in the results table, the AST-Transformer (using POT + R) achieved a state-of-the-art BLEU score of 46.64 on the Java dataset, significantly outperforming the standard Transformer (44.58) and specialized tree-based models like Tree2Seq. Similar gains were observed in the Python dataset.

Beyond accuracy, the efficiency gains are substantial. The combination of POT and constrained attention reduced the computational complexity of the encoding process by 90–95% compared to methods using more complex traversals. These results suggest that the architecture of the attention mechanism—specifically its ability to act as a structural filter—is more critical than the complexity of the input preprocessing.

Conclusion

The AST-Transformer represents a significant step forward in code representation learning. By aligning the attention mechanism with the inherent structure of ASTs, the model achieves higher accuracy with a fraction of the computational cost. Future work may explore the sensitivity of the distance threshold $K$ across different programming languages and the applicability of Tree-Structure Attention to other downstream tasks such as bug detection and code generation.

Summary updated

BibTeX
@inproceedings{tang2021ast,
  author = {Ze Tang and Chuanyi Li and Jidong Ge and Xiaoyu Shen and Zheling Zhu and Bin Luo},
  title = {{AST-Transformer: Encoding Abstract Syntax Trees Efficiently for Code Summarization}},
  booktitle = {ASE},
  year = {2021},
  url = {https://openreview.net/forum?id=dwkEQey9uYs},
}
Figure