Introduction
Automated code summarization—generating natural language descriptions of source code—is essential for improving developer productivity and software maintenance. However, code possesses unique characteristics that distinguish it from natural language: it is inherently hierarchical and contains complex semantic dependencies. Effective summarization requires a model to simultaneously grasp lexical tokens, syntactic structures (like Abstract Syntax Trees), and semantic flows (data and control dependencies).
Existing models often fall short by focusing on only one or two of these aspects. Sequence-based models ignore structure, while graph-based models often lose the hierarchical context. HierarchyNet addresses these limitations through a novel Heterogeneous Code Representation (HCR) and a specialized multi-layer architecture.
Heterogeneous Code Representation (HCR)
The core innovation of HierarchyNet is HCR, which organizes source code into three distinct layers to capture its multi-faceted nature:
- Lexical Layer: Represents the code as a linearized sequence of AST nodes, capturing basic textual information.
- Syntactic Layer: Groups related tokens into meaningful entities, such as statements or expressions, abstracted as subtrees.
- Semantic Layer: Constructs a graph where nodes are the syntactic subtrees, connected by edges representing data flow, control dependence, and sequential execution.
By abstracting fine-grained tokens into coarser syntactic units and then into a high-level dependency graph, the model can understand complex logic without getting "lost in the weeds" of individual tokens.
HierarchyNet Architecture
To process this multi-layered representation, HierarchyNet employs a modular architecture with specialized encoders for each layer:
- Transformer Encoder: Processes the lexical token sequence.
- Tree-based CNN (TBCNN): Encodes the syntactic subtrees.
- Heterogeneous Graph Transformer (HGT): Processes the semantic dependency graph.
The "glue" of the system is the Hierarchy-Aware Cross Attention (HACA) mechanism. HACA allows the model to attend to information across different layers, ensuring that fine-grained token information is contextualized by high-level semantic dependencies. Finally, a gating layer fuses this hierarchical information for a Transformer-based decoder to generate the final natural language summary.
Evaluation and Results
HierarchyNet was evaluated on four major benchmarks: TL-CodeSum, FunCom, DeepCom, and FunCom-50. It consistently outperformed state-of-the-art specialized models, fine-tuned pre-trained models, and large language models (LLMs).
Comparison with Specialized and Pre-trained Models
HierarchyNet surpassed previous leading hierarchical models like CAST and PA-former by significant margins, averaging over 3.4 BLEU points. It also outperformed fine-tuned versions of CodeT5 and CodeBERT, demonstrating that structural awareness is more effective than raw pre-training scale for this task.
Comparison with Large Language Models
Notably, HierarchyNet significantly outperformed much larger models such as CodeLlama-13B and StarCoder in zero-shot and few-shot settings. While LLMs are powerful generalists, HierarchyNet’s specialized structural understanding provides a decisive advantage in capturing the intricate logic required for accurate code summarization.
Conclusion
HierarchyNet demonstrates that a hierarchical, heterogeneous approach to code representation is superior for summarization tasks. By explicitly modeling lexical, syntactic, and semantic layers and using specialized encoders to process them, the model achieves state-of-the-art performance. Future work may involve extending this hierarchy to cross-layer edges and scaling the approach to summarize entire software repositories.
Summary updated