Abstract
Automatic code summarization—generating natural language descriptions for code snippets—is essential for improving software documentation and developer productivity. Existing models typically treat code as either a sequence of tokens or a structural tree, often failing to integrate these two modalities effectively. We present BLOCSUM (BLOck scope-based source Code SUMmarization), a novel framework that explicitly models the logical hierarchy of code through functional blocks. By introducing shared block position embeddings and multi-relational AST variants, BLOCSUM aligns sequential and structural representations. Our evaluation on standard Java and Python benchmarks shows that BLOCSUM achieves state-of-the-art performance, surpassing models like CodeBERT in both automated metrics and human evaluation.
Introduction
Programming languages are inherently structured into nested functional units, such as methods, loops, and conditional statements. While sequence-based models (treating code as text) and structure-based models (using Abstract Syntax Trees or ASTs) have made progress, they often struggle with the "sparse" nature of ASTs or the lack of structural context in sequences. Hybrid models that combine both often fail to deeply integrate these representations. BLOCSUM addresses this by recognizing Block Scope as the fundamental unit of code logic, ensuring that tokens and AST nodes belonging to the same logical block are tied together in the model's latent space.
Methodology
The BLOCSUM architecture consists of two primary encoders—a Code Encoder and an AST Encoder—and a Summary Decoder. The core innovations lie in how these components interact and how the AST is represented.
Shared Block Position Embedding
To bridge the gap between the sequential code tokens and the hierarchical AST nodes, BLOCSUM assigns a unique index to every functional block in the source code. These indices are used to generate "Shared Block Position Embeddings," which are fed into both the Code Encoder (a Transformer) and the AST Encoder (Graph Attention Networks). This shared representation forces the model to recognize when a piece of text and a node in the tree belong to the same logical unit, facilitating cross-modality alignment.
Multi-Relational AST Variants
Standard ASTs can be sparse, where logically related variables are separated by many hops. To mitigate this "long path" problem, BLOCSUM utilizes three variants of the AST simultaneously:
- AST-original: The standard hierarchical tree structure.
- AST-block: Adds edges between all nodes within the same code block to capture local dependencies.
- AST-global: A fully connected graph where every node is linked to every other node, providing global context.
Experimental Setup
We evaluated BLOCSUM on two widely used real-world datasets:
- Java Dataset: 69,708 training samples of method-level code and comments.
- Python Dataset: 55,538 training samples. The model was compared against several baselines, including sequence-to-sequence models and large pre-trained models like CodeBERT.
Results and Discussion
BLOCSUM demonstrated superior performance across both quantitative and qualitative evaluations.
Quantitative Performance
On the Java dataset, BLOCSUM achieved a BLEU score of 49.05, a significant improvement over the previous state-of-the-art (46.89). Similar improvements were observed in the Python dataset (35.16 BLEU). Ablation studies confirmed that the sharing of block embeddings was critical; using independent embeddings for code and AST resulted in lower performance, validating the importance of cross-modality alignment.
Human Evaluation
In a blind study involving computer science graduate students, BLOCSUM's summaries were rated higher for Relevance and Coverage than those generated by CodeBERT. While CodeBERT occasionally produced more fluent text due to its massive pre-training, BLOCSUM was more accurate in identifying specific logic and keywords, such as correctly capturing WSGI request parameters in Python functions.
Conclusion
BLOCSUM introduces a powerful paradigm for code summarization by treating code as a hierarchy of scopes rather than just a string or a tree. By aligning sequential and structural information through shared block embeddings and augmented ASTs, the model achieves state-of-the-art results. Future work includes integrating block-scope awareness into large-scale pre-training frameworks and exploring generalizability across a wider array of programming languages.
Summary updated