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

Learning to Represent Programs with Code Hierarchies

Minh Nguyen, Nghi D. Q. Bui

Abstract

We propose a novel method for code summarization utilizing Heterogeneous Code Representations (HCRs) and our specially designed HierarchyNet. HCRs effectively capture essential code features at lexical, syntactic, and semantic levels by abstracting coarse-grained code elements and incorporating fine-grained program elements in a hierarchical structure. Our HierarchyNet method processes each layer of the HCR separately through a unique combination of the Heterogeneous Graph Transformer, a Tree-based CNN, and a Transformer Encoder. This approach preserves dependencies between code elements and captures relations through a novel Hierarchical-Aware Cross Attention layer. Our method surpasses current state-of-the-art techniques, such as PA-Former, CAST, and NeuralCodeSum.

Introduction

Automated code summarization is a critical task for software maintenance, yet it remains difficult because programming languages are inherently more structured and logical than natural language. Current AI approaches typically fall into two categories: sequence-based models that treat code like text, often missing nested structures, and structure-based models using Abstract Syntax Trees (ASTs), which can suffer from long-term dependency issues in large trees. HierarchyNet addresses these gaps by viewing code through three simultaneous lenses: lexical (words), syntactic (structure), and semantic (logic/dependencies).

Methodology: Heterogeneous Code Representation and HierarchyNet

The core innovation of this work is the Heterogeneous Code Representation (HCR) and the accompanying HierarchyNet architecture. Instead of a single representation, HCR decomposes source code into a three-layered hierarchy to capture different levels of abstraction:

  1. Low Level (AST Sequence): Captures raw tokens and their types via a Transformer Encoder.
  2. Middle Level (Subtree-level): Groups tokens into meaningful syntactic units (e.g., statements) processed by a Tree-based CNN (TBCNN) to capture local patterns.
  3. High Level (Graph-level): Represents the function as a graph where nodes are statements and edges represent Data Flow and Control Flow. This is handled by a Heterogeneous Graph Transformer (HGT).
Overview of the HCR construction process (left) and the HierarchyNet architecture (right).

To integrate these layers, the model employs Hierarchy-Aware Cross Attention (HACA) to align specific code tokens with corresponding logical nodes in the graph. A Gating Layer then dynamically balances textual and structural information before final decoding. This multi-modal approach allows the model to "collapse" complex logic into manageable nodes, significantly reducing the distance between related semantic elements.

Experimental Results

HierarchyNet was evaluated against several benchmarks, including DeepCom, FunCom-50, TL-CodeSum, and FunCom. The model consistently outperformed state-of-the-art baselines like CAST and PA-Former across all metrics.

Performance comparison of HierarchyNet against baseline models on the DeepCom and FunCom-50 datasets.

On the DeepCom dataset, HierarchyNet achieved a BLEU score of 43.64, representing a 4.79-point improvement over PA-Former. On TL-CodeSum, it outperformed CAST by 2.82 BLEU points. Ablation studies confirmed the value of the hierarchy: using only the token layer resulted in a BLEU score of 40.63, while the full three-layer HCR pushed the score to 48.01 on the same dataset. Case studies on the gating mechanism revealed that structural and semantic information contributes at least 37% to the model's final understanding, proving that structural logic is essential for code comprehension.

Conclusion and Future Work

HierarchyNet demonstrates that representing code through multiple levels of abstraction is significantly more effective than monolithic representations. By using specialized encoders for each level, the model captures complex dependencies that flat models miss. Future work will explore "cross-layer" edges to connect fine-grained tokens directly to high-level logical blocks and investigate the use of attention scores to provide explainable summaries for developers.

Summary updated

BibTeX
@inproceedings{nguyen2024learning,
  author = {Minh Nguyen and Nghi D. Q. Bui},
  title = {{Learning to Represent Programs with Code Hierarchies}},
  booktitle = {EACL (Findings)},
  year = {2024},
}
Figure