Skip to main content
QUICK REVIEW

[Paper Review] Learning to Represent Programs with Heterogeneous Graphs

Kechi Zhang, Wenhan Wang|arXiv (Cornell University)|Dec 8, 2020
Software Engineering ResearchComputer Science37 references9 citations
TL;DR

This paper proposes a heterogeneous program graph (HPG) that explicitly models node and edge types in source code, enabling more accurate code representations. By leveraging the heterogeneous graph transformer (HGT) on HPG, the approach outperforms SOTA baselines on method name prediction and code classification tasks, with significant gains from heterogeneous type information and manually crafted edges.

ABSTRACT

Program source code contains complex structure information, which can be represented in structured data forms like trees or graphs. To acquire the structural information in source code, most existing researches use abstract syntax trees (AST). A group of works add additional edges to ASTs to convert source code into graphs and use graph neural networks to learn representations for program graphs. Although these works provide additional control or data flow information to ASTs for downstream tasks, they neglect an important aspect of structure information in AST itself: the different types of nodes and edges. In ASTs, different nodes contain different kinds of information like variables or control flow, and the relation between a node and all its children can also be different. To address the information of node and edge types, we bring the idea of heterogeneous graphs to learning on source code and present a new formula of building heterogeneous program graphs from ASTs with additional type information for nodes and edges. We use the ASDL grammar of programming language to define the node and edge types of program graphs. Then we use heterogeneous graph neural networks to learn on these graphs. We evaluate our approach on two tasks: code comment generation and method naming. Both tasks require reasoning on the semantics of complete code snippets. Experiment results show that our approach outperforms baseline models, including homogeneous graph-based models, showing that leveraging the type information of nodes and edges in program graphs can help in learning program semantics.

Motivation & Objective

  • To address the limitation of homogeneous graphs in code representation, which discard semantic type information of nodes and edges.
  • To improve code representation learning by explicitly modeling node and edge types in program structures.
  • To develop a framework that captures complex structural and semantic relationships in source code using heterogeneous graph modeling.
  • To demonstrate the effectiveness of heterogeneous types and manually crafted edges in enhancing model performance on code processing tasks.

Proposed method

  • Constructs a heterogeneous program graph (HPG) using ASDL grammar to encode node and edge types from abstract syntax trees (ASTs).
  • Introduces manually crafted edges such as NextSib and NextToken to preserve structural and syntactic order in code.
  • Employs the heterogeneous graph transformer (HGT) architecture to process HPG, enabling type-aware message passing across different node and edge types.
  • Applies HGT with a decoding strategy that operates only over subtoken nodes to improve generation tasks like method name prediction.
  • Uses node and edge type embeddings to guide attention mechanisms in HGT, allowing the model to distinguish semantic roles in code.
  • Trains the HPG+HGT model end-to-end on code representation tasks using standard benchmarks.

Experimental results

Research questions

  • RQ1How does incorporating heterogeneous node and edge types in program graphs affect code representation quality?
  • RQ2What is the contribution of manually crafted edges (e.g., NextSib, NextToken) to model performance?
  • RQ3How does the HGT architecture benefit from heterogeneous type information compared to homogeneous graph models?
  • RQ4What is the impact of the decoding strategy over subtoken nodes on generation tasks?
  • RQ5To what extent does removing node or edge types degrade model performance?

Key findings

  • The HPG+HGT model outperforms SOTA baselines on both code classification and method name prediction tasks across four benchmark datasets.
  • Removing either node types or edge types from HPG causes a drop of approximately 7% in F1-score, demonstrating their critical role in representation learning.
  • When both node and edge types are removed, turning HPG into a homogeneous graph, the F1-score drops by 8%, confirming the importance of heterogeneity.
  • The removal of manually crafted NextSib and NextToken edges leads to a 1.4% and 1.0% accuracy drop respectively, with p-values < 0.0002, indicating strong statistical significance.
  • Decoding over all nodes instead of only subtoken nodes reduces the F1-score by about 4.6%, validating the design choice of subtoken-only decoding.
  • GGNN benefits from edge type information, with a 3% F1-score improvement when heterogeneous edges are used, showing that even baseline GNNs gain from type-aware graphs.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.