GN-Transformer: Fusing Sequence and Graph Representation for Improved Code Summarization

Junyan Cheng, Iordanis Fostiropoulos, Barry W. BoehmarXiv 2021

Abstract

As opposed to natural languages, source code understanding is influenced by grammatical relationships between tokens regardless of their identifier name. Graph representations of source code such as Abstract Syntax Tree (AST) can capture relationships between tokens that are not obvious from the source code. We propose a novel method, GN-Transformer to learn end-to-end on a fused sequence and graph modality we call Syntax-Code-Graph (SCG). GN-Transformer expands on Graph Networks (GN) framework using a self-attention mechanism. SCG is the result of the early fusion between a source code snippet and the AST representation. We perform experiments on the structure of SCG, an ablation study on the model design, and the hyper-parameters to conclude that the performance advantage is from the fused representation. The proposed methods achieve state-of-the-art performance in two code summarization datasets and across three automatic code summarization metrics (BLEU, METEOR, ROUGE-L). We further evaluate the human perceived quality of our model and previous work with an expert-user study. Our model outperforms the state-of-the-art in human perceived quality and accuracy.

Introduction

Code summarization—the task of generating natural language descriptions for code snippets—is a critical benchmark for machine understanding of programming logic. Unlike natural language, source code possesses a rigid, hierarchical structure defined by formal grammar. While standard Transformer models excel at sequence processing, they often overlook the underlying structural "shape" of the code.

Previous research attempted to incorporate structure through "late fusion," where separate models process text and structure independently before merging. However, this approach fails to capture deep, early interactions between semantic meaning and structural context. The GN-Transformer addresses this by treating code as a unified graph, leveraging a "relational inductive bias" to focus attention on logical hierarchies rather than just linear sequences.

Methods: The GN-Transformer and SCG

The core innovation lies in the early fusion of source code and its Abstract Syntax Tree (AST) into a single modality: the Syntax-Code-Graph (SCG). In an SCG, every token (e.g., variable names, operators) is linked directly to its corresponding parent node in the AST, creating a unified representation of both content and structure.

GN-Transformer Architecture: The model fuses sequence and graph modalities using a GN-Transformer encoder and a standard Transformer decoder.

The GN-Transformer architecture adapts the Graph Networks (GN) framework to incorporate self-attention. Unlike standard Transformers that assume a fully connected graph (where every token attends to every other token), the GN-Transformer restricts information flow to the edges defined in the SCG. It utilizes Multi-Head Attention (MHA) and Feed-Forward Networks (FFN) within an encoder-decoder framework, where the GN-Transformer encoder processes the graph and a standard Transformer decoder generates the natural language summary.

Experimental Setup and Results

The model was evaluated on two standard benchmarks: a Java dataset of GitHub methods and a Python corpus of functions and documentation. Preprocessing was standardized to ensure fair comparison with existing state-of-the-art (SOTA) models.

The GN-Transformer achieved SOTA results across multiple automated metrics. In Java, it reached a 45.48 BLEU score (surpassing the previous 44.84). In Python, it achieved 33.31 BLEU (compared to 32.79).

Human Evaluation

Recognizing that automated metrics like BLEU often fail to capture the qualitative nuances of human-readable documentation, the authors conducted an expert-user study with 330 participants.

Human Evaluation Results: Distribution of responses across different expertise levels and programming experience, showing a strong preference for the GN-Transformer (Ours).

The study revealed a significant preference for the GN-Transformer. Its summaries were selected as the "best description" 37% of the time, nearly doubling the preference for standard Transformers (20%) and significantly outperforming the previous SOTA, GREAT (17%). This suggests that the structural awareness of the GN-Transformer leads to more logically coherent and useful summaries for developers.

Discussion and Conclusion

The success of the GN-Transformer highlights the importance of early fusion and relational inductive bias in code understanding. By forcing tokens to interact through the AST structure, the model avoids noise in long snippets and maintains focus on the logical hierarchy.

However, challenges remain. Information propagation in graphs is limited by the number of layers (one "hop" per layer), making it difficult for distant nodes to communicate. Furthermore, effectively representing linear positional order within a 2D graph structure remains an open research question. Future work will explore preserving AST-node embeddings during the decoding phase to further leverage high-level structural information.

Summary updated

BibTeX
@misc{cheng2021gn,
  author = {Junyan Cheng and Iordanis Fostiropoulos and Barry W. Boehm},
  title = {{GN-Transformer: Fusing Sequence and Graph Representation for Improved Code Summarization}},
  year = {2021},
  eprint = {2111.08874},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2111.08874},
}
Figure