Processing Long Legal Documents with Pre-trained Transformers: Modding LegalBERT and Longformer

Dimitris Mamakas, Petros Tsotsi, Ion Androutsopoulos, Ilias ChalkidisarXiv 2022

This paper addresses a fundamental bottleneck in applying modern AI to the legal field: the "long document problem." While Transformer models like BERT have revolutionized NLP, their rigid input limits are a poor fit for the sprawling nature of legal texts.

TL;DR

Researchers extended the Longformer architecture to handle up to 8,192 tokens and "warm-started" it using domain-specific LegalBERT, setting a new state-of-the-art for legal document classification. They also introduced efficient "Bag-of-Words" Transformer variants that trade some accuracy for significantly lower computational costs.

What Problem Does This Paper Address?

Standard Transformers like BERT are limited to 512 sub-word tokens because their attention mechanism scales quadratically—doubling the text length quadruples the computational effort. In the legal domain, where a single court opinion or contract can easily span thousands of words, this leads to massive data loss through truncation.

Even "sparse-attention" models like the Longformer, which extends the limit to 4,096 tokens, still cut off significant portions of documents in benchmarks like LexGLUE. The authors set out to bridge this gap, seeking models that can "read" more of the document without becoming computationally prohibited.

Figure 2: Distribution of input text length across LexGLUE datasets. Note how SCOTUS and ECtHR documents frequently exceed standard Transformer limits.

Methods

The authors explored two distinct architectural directions:

  1. LegalLongformer-8192: They pushed the Longformer's boundaries to 8,192 tokens. To make this work effectively, they "warm-started" the model from LegalBERT (a BERT variant already pre-trained on legal corpora) rather than generic models. They also experimented with placing global attention tokens ([sep]) at the end of every paragraph to act as discourse markers, helping information flow across long distances.
  2. TFIDF-BERT (Bag-of-Words Variants): For scenarios where speed is critical, they modified LegalBERT to act more like a traditional classifier. They deduplicated tokens and sorted them by their TF-IDF (Term Frequency-Inverse Document Frequency) scores. By putting the most "important" words first, they could fit the essence of a long document into a standard 512-token window. They also added a novel TF-IDF bucket embedding layer to tell the model exactly how statistically significant each word is.

Datasets

The models were evaluated on the LexGLUE (Legal Extraction, Classification, and Guidance for Ultimate Evaluation) benchmark. The study focused on three "Long Document" tasks:

  • ECtHR (Tasks A & B): Classifying violations of the European Convention on Human Rights.
  • SCOTUS: Classifying US Supreme Court opinions into issue areas.

They also tested on shorter document tasks (EUR-LEX, LEDGAR, UNFAIR-ToS) to ensure the modifications didn't break standard performance.

Key Insights and Ideas

  • Domain Knowledge is Non-Negotiable: Warm-starting the Longformer from LegalBERT provided a +2.0 percentage point boost in F1 score compared to starting from generic RoBERTa. Domain-specific pre-training is the foundation for performance.
  • Structure as a Signal: Using paragraph breaks as global attention anchors (-PAR variants) improved results in SCOTUS, the dataset with the longest documents, suggesting that respecting document structure helps the model navigate long-range dependencies.
  • Positional Embeddings as Importance Rankings: In the BoW variants, sorting words by TF-IDF essentially repurposes BERT’s positional embeddings. Instead of learning "where" a word is, the model learns "how important" it is based on its rank.
  • The Efficiency Frontier: There is a clear trade-off between "deep" understanding and "fast" processing. The BoW-BERT variants are much lighter on memory but struggle when local word order (like the difference between "rights of civil" and "civil rights") is crucial.

Key Results

The LegalLongformer-8192-PAR emerged as the new champion, outperforming the previous state-of-the-art (a hierarchical version of LegalBERT) across long-document tasks.

  • Performance: LegalLongformer-8192 achieved a harmonic mean macro-F1 of 69.4 on long documents, compared to 68.1 for the previous best hierarchical model.
  • Efficiency: While the 8k Longformer is the most accurate, it comes at a cost: it requires 2x the inference time and 30% more GPU memory than the 4k version.
  • BoW Success: The TFIDF-SRT-EMB-LegalBERT variant proved to be a strong middle ground, outperforming traditional SVMs while using 25-50% less memory than standard LegalBERT.
Figure 1: Comparison of examined models presented in Section 3, considering averaged down-stream per- formance and efficiency (inference time) in LexGLUE long document classification tasks (ECtHR, SCOTUS).

Limitations and Future Work

The primary limitation was computational: the authors could not perform extensive pre-training on the 8,192-token models due to resource constraints. They hypothesize that further pre-training on long legal documents would yield even better results.

Future work includes exploring these "modded" Transformers in other domains with long-form text, such as business reports or scientific literature, and refining the BoW variants to better capture multi-word expressions (n-grams) without losing their efficiency edge.

Summary updated

BibTeX
@misc{mamakas2022processing,
  author = {Dimitris Mamakas and Petros Tsotsi and Ion Androutsopoulos and Ilias Chalkidis},
  title = {{Processing Long Legal Documents with Pre-trained Transformers: Modding LegalBERT and Longformer}},
  year = {2022},
  eprint = {2211.00974},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2211.00974},
}
Figure