Beyond 512 Tokens: Siamese Multi-depth Transformer-based Hierarchical Encoder for Document Matching

Liu Yang, Mingyang Zhang, Cheng Li, Michael Bendersky, Marc NajorkarXiv 2020

Introduction

The advent of Transformer-based models like BERT has revolutionized natural language processing. However, these models suffer from a significant limitation: quadratic computational complexity $O(N^2)$ relative to the input sequence length $N$. This "512-token barrier" restricts their application to short text snippets or "short-to-long" retrieval tasks where only the query is short.

In real-world scenarios, such as recommending related news articles or identifying paper citations, models must perform long-to-long matching. This requires understanding the semantic structure of two lengthy documents simultaneously. Previous solutions using Recurrent Neural Networks (RNNs) often failed to capture long-range dependencies over thousands of words. To address this, the Siamese Multi-depth Transformer-based Hierarchical (SMITH) encoder was developed to process documents up to 2048 tokens efficiently.

The SMITH Architecture

The core of the SMITH model is its hierarchical approach to document encoding. Unlike standard Transformers that treat a document as a flat sequence of tokens, SMITH models the natural hierarchy of language (words into sentences, sentences into documents).

The hierarchical architecture of the SMITH model, featuring sentence-level and document-level Transformers.

As shown in the architecture diagram, the model consists of two main levels:

  1. Sentence-level Transformers ($L_1$ layers): These learn contextual representations of words within a "sentence block." By restricting self-attention to these local blocks, the model avoids the global quadratic bottleneck.
  2. Document-level Transformers ($L_2$ layers): These take the learned block representations as input to capture the global thematic context across the entire document.

Furthermore, SMITH employs a Siamese structure. Each document in a pair is processed independently into a dense vector representation. This "two-tower" design is crucial for practical applications; it allows document embeddings to be pre-computed and indexed offline for high-speed retrieval, rather than requiring expensive cross-attention for every possible pair during inference.

Data Processing and Pre-training

To maximize the efficiency of the hierarchical structure, the researchers introduced Greedy Sentence Filling. Standard padding often wastes significant computational resources by filling empty space with null tokens. By packing as many complete natural sentences as possible into a fixed-size block (e.g., 32 tokens), SMITH preserves semantic boundaries while minimizing padding overhead.

Comparison between standard padding and the proposed greedy sentence filling method to optimize block utilization.

Beyond architecture, the pre-training strategy is specifically adapted for long-form text. While standard BERT uses Masked Language Modeling (MLM) at the word level, SMITH introduces the Masked Sentence Block (MSB) task. In this task, entire blocks of text are masked, and the model must reconstruct them using the surrounding document context. This forces the document-level Transformer to learn high-level structural and thematic relationships that word-level masking alone might overlook.

Experimental Evaluation

The model was evaluated on two primary long-to-long matching benchmarks:

  • Wiki65K: A dataset of Wikipedia articles where "relatedness" is defined by link overlap.
  • AAN104K: A citation suggestion dataset from the ACL Anthology Network.

Key Results

The results demonstrate that extending the input length and using a hierarchical structure significantly improves performance on long-document tasks. On the Wiki65K dataset, the full SMITH model (incorporating both word and sentence block pre-training) achieved an F1-score of 0.9591. This represents a substantial improvement over the RNN-based SMASH model (0.9177) and a standard Siamese BERT baseline (0.9319) which is limited to 512 tokens.

The experiments also highlighted the importance of the MSB pre-training task. Models pre-trained with both word-level and block-level masking consistently outperformed those using word-level masking alone, confirming that structural understanding is key for document-level semantics.

Conclusion

The SMITH encoder successfully breaks the 512-token limit, extending the reach of Transformer models to 2048 tokens for document matching tasks. By combining a Siamese hierarchical architecture with specialized pre-training and data-filling techniques, it achieves state-of-the-art results in long-to-long matching. While memory constraints still exist for extremely long documents like full books, SMITH provides a robust framework for the next generation of document-level NLP applications.

Summary updated

BibTeX
@misc{yang2020beyond,
  author = {Liu Yang and Mingyang Zhang and Cheng Li and Michael Bendersky and Marc Najork},
  title = {{Beyond 512 Tokens: Siamese Multi-depth Transformer-based Hierarchical Encoder for Document Matching}},
  year = {2020},
  url = {https://openreview.net/forum?id=E9P-oZcymXx},
}
Figure