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

TreeBERT: A tree-based pre-trained model for programming language

Xue Jiang, Zhuoran Zheng, Chen Lyu, Liang Li, Lei Lyu

TL;DR

TreeBERT is a tree-based pre-trained encoder-decoder model that learns programming language representations directly from Abstract Syntax Trees (ASTs). By decomposing trees into root-to-terminal paths, introducing recursive node position embeddings, and training on custom tree-centric objectives, it significantly outperforms sequence-only models like CodeBERT on code summarization and documentation, even transferring effectively to programming languages unseen during pre-training.

What Problem Does This Paper Address?

Most pre-trained models for programming languages (such as CuBERT and CodeBERT) treat code as if it were natural language—a flat sequence of tokens. However, program semantics are intrinsically bound to rigorous hierarchical syntax. While source code can be parsed into Abstract Syntax Trees (ASTs), standard Transformer architectures struggle to consume tree structures natively without losing structural context or resorting to arbitrary traversals (e.g., pre-order traversal).

Furthermore, standard sequence-level pre-training objectives (like standard Masked Language Modeling) do not fit the statistical realities of trees, where top-level syntactic nodes repeat excessively across the tree. TreeBERT bridges this gap by designing an AST serialization method, position embedding, and pre-training objectives tailored specifically to tree structures.

Methods

TreeBERT employs a standard 6-layer Transformer encoder-decoder architecture with minimal architectural modifications, focusing innovation on representation and objectives:

  • Path-Based AST Serialization: Instead of graph convolutions or sequential traversals, TreeBERT represents an AST as a set of constituent paths running from the root node to each terminal (value) node: $A = \{p_1, p_2, \dots, p_N\}$.
  • Recursive Node Position Embedding: To capture hierarchy and sibling relationships without exploding parameters, a node's position embedding linearly combines its parent’s position embedding and a tree-level embedding. This requires roughly 1/200th of the parameters of standard learned position embeddings and extrapolates gracefully to deeper trees.
  • Tree Masked Language Modeling (TMLM): Because root-level type nodes recur frequently across paths, standard random masking disproportionately masks repeated structural nodes. TMLM preferentially masks nodes closer to the leaves (terminal tokens holding rich variable/identifier semantics). The decoder is then tasked with generating the complete token sequence from the masked AST encoder inputs.
  • Node Order Prediction (NOP): A binary classification task that randomly swaps node positions within a path (e.g., swapping a parent statement and child block). The model predicts whether the path adheres to valid structural order constraints.

The model is trained via a weighted hybrid loss: $\mathcal{L} = 0.75 \cdot \mathcal{L}_{\text{TMLM}} + 0.25 \cdot \mathcal{L}_{\text{NOP}}$.

Datasets

  • Pre-training: Python (7.2 million files, ~2 billion tokens) and Java (14.1 million files, ~4.5 billion tokens) sourced from the CuBERT corpus, converted into over 1.6 billion total AST paths.
  • Downstream Evaluation:
  • Code Summarization (Function name prediction): ETH Py150 (Python) and Java-small, Java-med, and Java-large datasets.
  • Code Documentation (Comment generation): Java dataset from DeepCom.
  • Cross-Language Generalization: CodeNN’s C# dataset from StackOverflow (used exclusively at test time without C# pre-training).

Key Insights and Ideas

  • Terminal Nodes Carry the Semantics: Randomly masking AST nodes fails because high-level syntax nodes (like Module or Expr) repeat across dozens of paths. Weighting masks toward deeper nodes forces the model to learn user-defined identifiers and values.
  • Path Sets Preserve Uniqueness: Modeling a tree as a set of root-to-leaf paths uniquely serializes the tree while bypassing the ordering bias introduced by linear graph traversals.
  • Syntactic Regularity Generalizes Across Languages: TreeBERT transfers remarkably well to C# despite only being pre-trained on Python and Java. Because AST abstractions share underlying logic across imperative and object-oriented languages, syntactic pre-training provides strong cross-lingual transferability.

Key Results

  • Code Summarization: TreeBERT establishes state-of-the-art results across all four benchmarks. On ETH Py150, TreeBERT achieves an F1 score of 39.04 (compared to 34.45 for Code+Gnn+GRU and 29.58 for CodeBERT). On Java datasets, it achieves F1 scores of 51.99 (Java-small), 61.22 (Java-med), and 67.25 (Java-large), showing the largest relative margins on smaller training splits (+13.29% relative gain on Py150).
  • Code Documentation: TreeBERT scores 20.49 BLEU on the Java comment generation task, surpassing CodeBERT (17.87 BLEU) and specialized models like Code+Gnn+GRU (19.73 BLEU).
  • Zero-Shot Language Transfer (C#): When fine-tuned on C#, TreeBERT scores 17.94 BLEU, noticeably higher than CodeBERT (15.31 BLEU) and MASS (16.84 BLEU).
  • Ablations: Removing TMLM causes the largest performance drop, but replacing the tree-aware masking strategy with standard random masking collapses performance nearly to the no-pre-training baseline.

Limitations and Future Work

  • Parse Dependency: Code snippets that fail syntax parsing or cannot be converted into ASTs must be discarded.
  • Context Limits on Massive Trees: Deep or sprawling ASTs require truncation (pre-training capped paths at 100 paths of length 20).
  • Future Directions: The authors propose extending TreeBERT beyond code summarization/documentation to other PL tasks and integrating multimodal representations that combine tokens, AST paths, and control-flow graphs simultaneously.

Summary updated

BibTeX
@inproceedings{jiang2021treebert,
  author = {Xue Jiang and Zhuoran Zheng and Chen Lyu and Liang Li and Lei Lyu},
  title = {{TreeBERT: A tree-based pre-trained model for programming language}},
  booktitle = {Conference on Uncertainty in Artificial Intelligence},
  year = {2021},
  url = {https://openreview.net/forum?id=0rFfz21FFv},
}
Figure