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

Understanding Long Programming Languages with Structure-Aware Sparse Attention

Tingting Liu, Chengyu Wang, Cen Chen, Ming Gao, Aoying Zhou

Introduction

Standard Transformer-based models, such as CodeBERT, have revolutionized code understanding tasks. However, they are fundamentally limited by the "quadratic bottleneck": the computational and memory costs of self-attention scale with the square of the input sequence length ($O(n^2)$). Consequently, most models truncate input code to a mere 512 tokens, discarding critical logic in large software projects. To address this, we present Structure-Aware Sparse Attention (SASA), an architecture that enables efficient processing of long code sequences by exploiting the inherent structural properties of programming languages.

The SASA Architecture

SASA replaces the dense self-attention mechanism with a multi-pattern sparse approach. By focusing only on the most relevant token interactions, SASA achieves linear complexity ($O(n)$) while maintaining high performance. The mechanism integrates four distinct attention patterns:

  1. Sliding Window Attention: Captures local context by allowing tokens to attend to their immediate neighbors.
  2. Global Attention: Uses special tokens (e.g., [CLS]) that attend to the entire sequence, providing a holistic representation.
  3. Top-$k$ Sparse Attention: Based on a pre-calculated frequency matrix derived from the CodeSearchNet corpus, this pattern prioritizes historically significant token pairs, even across long distances.
  4. AST-based Structure-Aware Attention: Leverages the formal syntax of code to link logically related but textually distant elements.
Figure 1: Visualization of local and top-k attention patterns alongside the frequency distribution of top attention scores, highlighting the 'long-tail' nature of code attention.

Leveraging Code Structure

A key insight of this work is that in programming languages, logical structure often trumps textual distance. While natural language models rely heavily on proximity, code logic is hierarchical and defined by an Abstract Syntax Tree (AST). SASA explicitly incorporates this structure, allowing tokens to "jump" across thousands of lines to connect a variable's declaration with its usage.

Figure 2: Mapping code tokens to an Abstract Syntax Tree (AST). This structural representation allows the model to capture long-range logical dependencies that are often lost in sequence truncation.

The authors observed a "long-tail" phenomenon in attention scores: only a tiny fraction of token interactions contribute significantly to the model's output. By focusing on these high-impact connections—identified through both learned frequencies and explicit syntax—SASA avoids the redundant calculations of standard Transformers.

Experimental Results

The researchers evaluated SASA on the CodeXGLUE benchmark, focusing on tasks with sequences exceeding 1,024 tokens.

  • Performance: On the BigCloneBench clone detection task, SASA achieved an F1 score of 0.911, a substantial improvement over CodeBERT's 0.789 and GraphCodeBERT's 0.871.
  • Efficiency: SASA demonstrated a 23% to 33% reduction in memory usage compared to standard attention. While CodeBERT encountered "Out of Memory" (OOM) errors at batch sizes above 10 on a 32GB V100 GPU, SASA scaled linearly, handling larger batches and longer sequences with ease.
  • Ablation Studies: Removing either the Top-$k$ or the AST-based attention components resulted in significant performance degradation, confirming that both statistical frequency and explicit syntax are vital for deep code understanding.

Conclusion

SASA provides a scalable and effective solution for modeling long programming language sequences. By combining sparse attention patterns with structural awareness, it overcomes the quadratic limitations of traditional Transformers. Future work will explore dynamic frequency matrices and the application of SASA to long-form generative tasks, such as class-level code completion.

Summary updated

BibTeX
@inproceedings{liu2022understanding,
  author = {Tingting Liu and Chengyu Wang and Cen Chen and Ming Gao and Aoying Zhou},
  title = {{Understanding Long Programming Languages with Structure-Aware Sparse Attention}},
  booktitle = {International ACM SIGIR Conference on Research and Development in Information Retrieval},
  year = {2022},
}
Figure