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

Graph Conditioned Sparse-Attention for Improved Source Code Understanding

Junyan Cheng, Iordanis Fostiropoulos, Barry W. BoehmarXiv 2021

Abstract

Transformer architectures have been successfully used in learning source code representations. The fusion between a graph representation like Abstract Syntax Tree (AST) and a source code sequence makes the use of current approaches computationally intractable for large input sequence lengths. Source code can have long-range dependencies that require larger sequence lengths to model effectively. Current approaches have a quadratic growth in computational and memory costs with respect to the sequence length. Using such models in practical scenarios is difficult. In this work, we propose the conditioning of a source code snippet with its graph modality by using the graph adjacency matrix as an attention mask for a sparse self-attention mechanism and the use of a graph diffusion mechanism to model longer-range token dependencies. Our model reaches state-of-the-art results in BLEU, METEOR, and ROUGE-L metrics for the code summarization task and near state-of-the-art accuracy in the variable misuse task. The memory use and inference time of our model have linear growth with respect to the input sequence length as compared to the quadratic growth of previous works.

Introduction

Applying deep learning to source code understanding presents unique challenges. While Transformers are the standard for sequence modeling, they typically treat code as a flat string, ignoring the hierarchical structure inherent in programming languages. Previous models, such as GREAT, have attempted to inject structural "inductive bias" by fusing code sequences with graph representations like Abstract Syntax Trees (ASTs). However, standard Transformers compute dense attention, where every token attends to every other token, leading to quadratic $O(N^2)$ growth in memory and compute. Given that real-world code files often exceed several hundred lines, these dense models become computationally intractable. This work addresses the need for a graph-aware model that scales linearly with sequence length.

Methodology

The proposed approach utilizes two primary mechanisms to achieve efficiency without sacrificing structural awareness:

Graph Conditioned Sparse Masking

Rather than learning attention weights between all token pairs, we utilize the code's AST as a sparse mask. Attention weights are forced to zero for tokens that do not share an edge in the AST. Since ASTs are naturally sparse—with edges growing linearly relative to nodes—this reduces the attention mechanism's complexity from quadratic to linear.

Figure 1: Comparison of sparse AST-based attention masking (a) versus traditional dense attention (b).

Self-Attention Diffusion Mechanism

Sparse attention can isolate tokens that are distant in the AST. To mitigate this, we apply an approximated graph diffusion mechanism to the attention matrix. This simulates multi-hop message passing, allowing distant nodes to share context across layers without adding new trainable parameters.

Experimental Results

We evaluated our model on code summarization (Java and Python datasets) and variable misuse detection.

Performance Metrics

In code summarization, our model achieved state-of-the-art (SOTA) results. On the Java dataset, it reached a BLEU score of 45.55, surpassing the GREAT model's 44.05. Similar gains were observed in Python (33.41 BLEU vs. 31.19). In the variable misuse task, the model achieved 75.36% Joint accuracy, significantly outperforming vanilla Transformers (66.05%) while maintaining a lower computational footprint.

Efficiency and Scaling

The most significant advantage of our model is its linear scaling. As shown in the memory usage benchmarks, our approach maintains a low memory profile even as sequence lengths reach 10,000 tokens.

Figure 3: Memory usage comparison showing linear scaling of the proposed model versus quadratic growth in Transformer and GREAT baselines.

For a 10,000-token sequence, our model requires less than 4GB of memory, whereas the GREAT baseline would exceed 70GB, making it unusable on standard hardware.

Conclusion and Future Work

By leveraging the inherent structure of source code through AST-based sparse masking and graph diffusion, we demonstrate that it is possible to achieve state-of-the-art performance with linear computational complexity. While current GPU hardware is optimized for dense operations, the theoretical and practical memory gains are substantial. Future research will explore richer graph representations, such as Control Flow Graphs, to further capture semantic meaning while preserving sparsity.

Summary updated

BibTeX
@misc{cheng2021graph,
  author = {Junyan Cheng and Iordanis Fostiropoulos and Barry W. Boehm},
  title = {{Graph Conditioned Sparse-Attention for Improved Source Code Understanding}},
  year = {2021},
  eprint = {2112.00663},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2112.00663},
}
Figure