Skip to main content
QUICK REVIEW

[Paper Review] Learning Program Representations with a Tree-Structured Transformer

Wenhan Wang, Kechi Zhang|arXiv (Cornell University)|Aug 18, 2022
Software Engineering ResearchComputer Science6 citations
TL;DR

This paper proposes Tree-Transformer, a novel tree-structured Transformer model that learns program representations by leveraging multi-head attention to capture dependencies between parent-child and sibling nodes, combined with bidirectional (bottom-up and top-down) information propagation. The method significantly outperforms existing tree-based and graph-based approaches on program classification, bug localization, and type inference tasks by effectively modeling long-range dependencies and learning rich node-level representations.

ABSTRACT

Learning vector representations for programs is a critical step in applying deep learning techniques for program understanding tasks. Various neural network models are proposed to learn from tree-structured program representations, e.g., abstract syntax tree (AST) and concrete syntax tree (CST). However, most neural architectures either fail to capture long-range dependencies which are ubiquitous in programs, or cannot learn effective representations for syntax tree nodes, making them incapable of performing the node-level prediction tasks, e.g., bug localization. In this paper, we propose Tree-Transformer, a novel recursive tree-structured neural network to learn the vector representations for source codes. We propose a multi-head attention mechanism to model the dependency between siblings and parent-children node pairs. Moreover, we propose a bi-directional propagation strategy to allow node information passing in two directions, bottom-up and top-down along trees. In this way, Tree-Transformer can learn the information of the node features as well as the global contextual information. The extensive experimental results show that our Tree-Transformer significantly outperforms the existing tree-based and graph-based program representation learning approaches in both the tree-level and node-level prediction tasks.

Motivation & Objective

  • To address the limitations of existing tree- and graph-based models in capturing long-range dependencies and learning effective node-level representations for code.
  • To overcome the unidirectional information flow in traditional recursive neural networks that hinders context learning for leaf and low-level nodes.
  • To preserve and utilize the ordered structure of programs, especially sibling ordering, which is often ignored by permutation-invariant models like GNNs.
  • To maintain the original syntax tree structure without requiring complex preprocessing or graph augmentation, reducing overhead and preserving structural fidelity.
  • To develop a Transformer-based architecture that effectively models hierarchical and ordered program structures for improved program representation learning.

Proposed method

  • Tree-Transformer employs a multi-head self-attention mechanism tailored for tree structures, explicitly modeling dependencies between parent-child and sibling node pairs.
  • It introduces a bidirectional information propagation strategy using two parallel Transformer units: a bottom-up unit aggregating context from leaves to root, and a top-down unit distributing root-level context to descendants.
  • The model integrates positional encoding to capture the order of siblings, enabling the model to distinguish between different structural positions in the tree.
  • Node representations are learned through recursive aggregation of child representations using attention-based fusion, allowing contextual information to flow across the tree.
  • The architecture is recursive and tree-structured, enabling end-to-end learning of vector representations directly from abstract syntax trees (ASTs).
  • The model is trained via standard supervised objectives on downstream tasks such as program classification, bug localization, and type inference.
Figure 1: The overall pipeline of Tree-Transformer.
Figure 1: The overall pipeline of Tree-Transformer.

Experimental results

Research questions

  • RQ1Can a Transformer-based architecture effectively model long-range dependencies in program syntax trees, especially across distant nodes?
  • RQ2Can bidirectional information propagation (bottom-up and top-down) improve node-level representation learning compared to unidirectional methods?
  • RQ3Does incorporating sibling ordering via positional encoding enhance the model's ability to distinguish syntactic structure in code?
  • RQ4How does Tree-Transformer compare to state-of-the-art tree-based and graph-based models in downstream program understanding tasks?
  • RQ5Can the model maintain performance without requiring complex graph construction or structural preprocessing?

Key findings

  • Tree-Transformer achieves state-of-the-art performance on program classification, outperforming existing tree-based and graph-based models on multiple benchmarks.
  • The model significantly improves node-level prediction tasks such as wrong operator localization and repair, demonstrating superior representation learning at the node level.
  • The bidirectional propagation mechanism enables effective long-range dependency modeling, as evidenced by improved performance on tasks requiring contextual reasoning across distant code elements.
  • The integration of sibling order via positional encoding leads to measurable gains in tasks sensitive to syntactic ordering.
  • Tree-Transformer maintains strong performance without requiring additional graph construction or preprocessing, preserving the original AST structure and reducing computational overhead.
  • Ablation studies confirm that both the bidirectional propagation and multi-head attention components are essential for the model’s performance.
Figure 2: An example of existing tree-structured and graph neural networks for learning on abstract syntax trees.
Figure 2: An example of existing tree-structured and graph neural networks for learning on abstract syntax trees.

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.