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

A Transformer-based Approach for Source Code Summarization

Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang

Abstract

Source code summarization is essential for program comprehension and maintenance, yet traditional Recurrent Neural Networks (RNNs) struggle with the non-sequential logic and long-range dependencies inherent in programming languages. This paper proposes a Transformer-based model enhanced with relative position representations and a copy mechanism. Experimental results on Java and Python datasets demonstrate that this architecture significantly outperforms existing state-of-the-art models. A key finding is that relative structural information is more critical than absolute positioning, which can actually hinder code understanding.

Introduction

Automatically generating natural language descriptions for code blocks—source code summarization—is a critical task for modern software development. It assists developers in navigating complex codebases and understanding legacy systems. Historically, this was handled by RNNs, which suffer from two major flaws: sequential bias and difficulty managing long-range dependencies. Because code often involves non-linear logic (such as function calls jumping across files), a more flexible architecture is required.

The Transformer architecture, successful in natural language processing, offers a solution through self-attention. However, code possesses unique structural properties that require specific architectural adjustments to the standard Transformer to be truly effective.

Methodology

The proposed approach introduces two primary modifications to the standard Transformer:

  1. Relative Position Representations: Unlike natural language where absolute position (token index) is often useful, code semantics are frequently invariant to absolute position. For instance, the logic of an operation remains the same regardless of its line number. By modeling the relative distance between tokens, the Transformer focuses on structural interactions rather than arbitrary indices.
  2. Copy Attention Mechanism: Code contains many "rare" tokens, such as specific variable names. A standard vocabulary cannot store them all. The copy mechanism allows the model to "point" at a token in the source code and copy it directly into the summary, which is essential for generating accurate technical descriptions.

Additionally, the authors employ a sub-tokenization strategy, splitting CamelCase and snake_case identifiers to reduce vocabulary size and better expose the semantic intent behind variable names.

Experimental Setup

The model was evaluated on two large-scale benchmarks curated from GitHub: a Java dataset and a Python dataset. These datasets provide a diverse range of real-world coding patterns and summary pairs.

Table 1: Statistics of the Java and Python datasets used for evaluation.

Results and Discussion

The proposed "Full Model" set a new standard for the field, achieving significant performance gains over previous state-of-the-art methods like CODE-NN and the Dual Model.

Table 2: Performance comparison of the proposed model against state-of-the-art methods and internal ablation studies.

Key Insights

  • Absolute Position is a Distraction: One of the most surprising findings was that removing absolute positional encoding altogether actually improved performance. This suggests that for code, the specific index of a token is less important than its structural context.
  • Implicit Structure: While many researchers attempt to incorporate Abstract Syntax Trees (ASTs) to provide structural hints, this study found that a Transformer with relative positions can implicitly learn these structures. Explicitly adding AST information did not yield significant gains and increased computational overhead.
  • Depth over Breadth: Analysis of model hyperparameters indicated that increasing model depth (number of layers) was more effective than increasing width (hidden dimensions). This suggests that mapping complex code logic to human language requires high-level hierarchical abstraction.

Conclusion

This research demonstrates that a properly tuned Transformer, focusing on relative token relationships and a copy mechanism, is highly effective for code summarization. The model achieved a 44.58 BLEU score on Java and a 32.52 BLEU score on Python, representing a substantial leap over previous benchmarks. Future work will explore the application of these insights to other software engineering tasks, such as automatic commit message generation or code translation.

Summary updated

BibTeX
@inproceedings{ahmad2020transformer,
  author = {Wasi Uddin Ahmad and Saikat Chakraborty and Baishakhi Ray and Kai-Wei Chang},
  title = {{A Transformer-based Approach for Source Code Summarization}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2020},
  doi = {10.18653/v1/2020.acl-main.449},
}
Figure