Treeformer: Dense Gradient Trees for Efficient Attention Computation

Lovish Madaan, Srinadh Bhojanapalli, Himanshu Jain, Prateek Jain

TL;DR

Treeformer introduces a novel attention mechanism that replaces the computationally expensive "all-to-all" comparison of standard Transformers with efficient decision tree-based retrieval. By viewing attention as a nearest-neighbor search problem, the authors achieve up to a 30x reduction in FLOPs in the attention layer and significant inference speedups while maintaining accuracy comparable to baseline models on long-sequence tasks.

What Problem Does This Paper Address?

The primary bottleneck of the standard Transformer architecture is its self-attention mechanism. Its computational cost grows quadratically ($O(n^2)$) with the input sequence length ($n$). While this is manageable for short sentences, it becomes a massive hurdle for long-form content like web pages, books, or high-resolution images.

Existing solutions generally fall into two camps: those that provide massive speedups but lose significant accuracy (like Linformer or Performer), and those that maintain accuracy but offer only modest speed gains (like BigBird). The authors tackle this gap by treating attention as a hierarchical retrieval problem, using decision trees to quickly find the most relevant "keys" for any given "query" token.

Methods

The core innovation of Treeformer is the use of Dense Gradient Trees to navigate the search space of tokens. Instead of comparing a query to every single key, the query "walks" down a decision tree to find a cluster of relevant keys.

Treeformer Architecture

The authors propose two specific variants of this tree-based attention:

  1. TF-ATTENTION (Tree Fine-grained): A query token navigates to a specific leaf node and only performs attention on the keys that landed in that same leaf. This approach is highly sparse and computationally efficient.
  2. TC-ATTENTION (Tree Coarse): To solve the "dead gradient" problem inherent in discrete trees, this version allows the query to pay attention to an average of keys at every node along its path to the leaf. This provides smoother, "denser" gradients during training, improving convergence.

To make these discrete trees trainable via backpropagation, the authors utilize Straight-Through Estimators. Furthermore, they introduce a 2-D Bootstrapping Algorithm: training starts with a standard Transformer, gradually "tree-ifying" the layers and increasing the tree height over time. This curriculum prevents the model from getting stuck in poor local minima early in training.

Datasets

The authors evaluated Treeformer across several standard benchmarks to ensure robustness:

  • BERT Pre-training: Wikipedia, BooksCorpus, CC-News, and OpenWebText (Stories).
  • GLUE & SQuAD: Standard NLP tasks including sentiment analysis, natural language inference, and question answering.
  • Long Range Arena (LRA): Specifically designed to test models on long sequences (up to 8k tokens), including tasks like ListOps, Text classification, and Image retrieval.
  • Summarization: Long-document datasets including PubMed and Arxiv.

Key Insights and Ideas

  • Attention as Retrieval: The foundational insight is realizing that attention is essentially a Maximum Inner Product Search (MIPS). Decision trees are a classical, highly efficient way to solve MIPS in logarithmic time ($O(\log n)$).
  • The Training Hurdle: You cannot simply drop a decision tree into a neural network and expect it to work, as the discrete nature of trees breaks gradient flow. The bootstrapping method—gradually transitioning from "dense" attention to "tree" attention—is the secret sauce that makes the model converge effectively.
  • Balancing the Tree: In TF-ATTENTION, the model naturally wants to "cheat" by putting all keys into one leaf to minimize loss (effectively reverting to standard attention). TC-ATTENTION counteracts this by incentivizing the model to spread keys across the tree to create more distinct, query-dependent representations.

Key Results

Treeformer demonstrates a strong balance between computational efficiency and model accuracy.

Training Dynamics of TC vs TF Attention
  • Efficiency: Treeformer uses up to 30x fewer FLOPs in the attention layer than a standard Transformer and is 9x cheaper than BigBird.
  • Accuracy: On the GLUE benchmark, the $k$-ary TF-A variant actually outperformed the BERT-Base baseline by over 1% while being significantly more efficient.
  • Long Sequences: On the Long Range Arena, TC-ATTENTION matched the baseline accuracy while reducing compute by 33x.
  • Inference Speed: In real-world wall-clock time, Treeformer achieved speedups ranging from 1.8x to 6.7x for sequence lengths between 2048 and 8192 tokens.

Limitations and Future Work

  • Hard Boundaries: Because a query is routed down a specific path, it might miss a highly relevant key that fell into a neighboring leaf node just across a decision boundary.
  • Tree Imbalance: If the keys are not distributed uniformly across leaf nodes, the computational savings diminish. In the worst-case scenario, performance reverts to $O(n^2)$.
  • Future Directions: The authors suggest exploring Forest structures (using multiple trees) to provide multiple paths for a query, which could mitigate the "hard boundary" problem and further improve accuracy. They also see potential in combining decision trees with other types of efficient attention, like low-rank approximations.

Summary updated

BibTeX
@inproceedings{madaan2023treeformer,
  author = {Lovish Madaan and Srinadh Bhojanapalli and Himanshu Jain and Prateek Jain},
  title = {{Treeformer: Dense Gradient Trees for Efficient Attention Computation}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=DWn1TEb2fK},
}
Figure