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

Source Code Summarization with Structural Relative Position Guided Transformer

Zi Gong, Cuiyun Gao, Yasheng Wang, Wenchao Gu, Yun Peng, Zenglin Xu

Introduction

In the modern software development lifecycle, developers spend approximately 60% of their time on program comprehension. High-quality natural language summaries, such as docstrings, are vital for maintaining productivity and ensuring code maintainability. However, these summaries are often missing, incomplete, or outdated in large-scale repositories.

While Transformer-based models have significantly advanced the field of automatic code summarization, they typically treat source code as a linear sequence of tokens. This approach ignores the inherent structural nature of programming languages. In a source file, two tokens might be separated by dozens of lines (long sequential distance) while remaining logically adjacent in the program's execution flow (short structural distance). To bridge this gap, we present SCRIPT (StruCtural RelatIve Position guided Transformer), a model designed to integrate the structural hierarchy of code directly into the Transformer architecture.

The SCRIPT Architecture

SCRIPT enhances the standard Transformer encoder by incorporating structural information derived from the Abstract Syntax Tree (AST). The model utilizes two specialized encoder layers to process code structure:

  1. Relative Distance Weighted Transformer (RDW-Transformer): This layer modifies input representations before they enter the attention mechanism. It calculates the shortest path between any two tokens in the AST and applies a normalized version of this distance to weight the input. This ensures that tokens close within the tree structure are treated as more semantically related.
  2. Structural RPE-Induced Transformer (SRPEi-Transformer): Building on Relative Positional Encoding (RPE), this layer injects structural distance directly into the self-attention calculation. It allows the model to adjust attention scores based on the number of "hops" between nodes in the AST, effectively masking or boosting context based on structural proximity.
The SCRIPT Encoder architecture, featuring RDW and SRPEi Transformer layers.

Quantifying Structural Proximity

The core innovation of SCRIPT lies in its use of the shortest path distance within the AST to guide the model's focus. By treating the AST as a graph, the model can identify relationships that are invisible to sequence-based models.

For example, in a complex if-else if block, the keywords for different branches might be separated by many lines of logic. However, in the AST, these keywords are often sibling nodes, separated by only one or two hops. SCRIPT leverages this "Structural Proximity" to capture the syntax-aware context that human programmers use to understand code logic.

An Abstract Syntax Tree (AST) of a Java method, illustrating the hierarchical relationships between code elements.

Experimental Evaluation

Datasets and Preprocessing

SCRIPT was evaluated on two large-scale parallel datasets:

  • Java: Over 69,000 method-summary pairs from GitHub.
  • Python: Approximately 55,000 method-summary pairs.

To address the "Out-of-Vocabulary" (OOV) problem, we employed CamelCase tokenization and replaced literal strings and numbers with generic placeholders (STR, NUM).

Results

SCRIPT achieved state-of-the-art performance across BLEU-4, ROUGE-L, and METEOR metrics. On the Java dataset, SCRIPT improved BLEU scores by 2.6% over the previous leading model (SiT), while achieving a 1.6% improvement on Python.

A blind human evaluation involving 10 volunteers (including PhD and Masters students) further validated these results. SCRIPT's summaries were consistently rated higher for relevance, similarity to ground truth, and naturalness compared to existing methods.

Conclusion and Future Work

SCRIPT demonstrates that teaching Transformers to "see" the tree structure of code significantly improves the quality of generated summaries. By integrating AST shortest path distances into the attention mechanism, the model captures logical relationships that linear models miss.

Future research will explore incorporating additional structural views, such as Data Flow Graphs (DFGs) and Control Flow Graphs (CFGs), to provide even deeper insights into program logic. Additionally, we plan to extend the SCRIPT architecture to related tasks such as code search and automated code generation.

Summary updated

BibTeX
@inproceedings{gong2022source,
  author = {Zi Gong and Cuiyun Gao and Yasheng Wang and Wenchao Gu and Yun Peng and Zenglin Xu},
  title = {{Source Code Summarization with Structural Relative Position Guided Transformer}},
  booktitle = {SANER},
  year = {2022},
  url = {https://openreview.net/forum?id=Rj8S1jvvx3},
}
Figure