Inferensys

Glossary

Longformer

A Transformer model employing a sparse attention mechanism that scales linearly with sequence length, enabling the processing of long legal documents.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
SPARSE ATTENTION ARCHITECTURE

What is Longformer?

Longformer is a Transformer model that employs a sparse attention mechanism combining local sliding window and global task-specific patterns to process sequences up to 4,096 tokens with linear memory scaling, eliminating the quadratic bottleneck of standard self-attention for long-document legal NLP tasks.

The Longformer architecture replaces the standard O(n²) self-attention of the original Transformer with a composite sparse attention pattern. It uses a sliding window of fixed size (typically 512 tokens) for local context, combined with pre-specified global attention positions—such as [CLS] tokens—that attend to the entire sequence. This hybrid design scales linearly with sequence length, enabling direct processing of full legal briefs, multi-page contracts, and consolidated case law without truncation or chunking.

For multi-document legal reasoning, Longformer's global attention mechanism is critical. By designating query tokens or section headers as global, the model builds task-specific representations that bridge distant sections of a single document or link related passages across a corpus. This capability supports cross-document alignment and factual consistency verification in legal summarization pipelines, where a generated statement must be grounded against evidence scattered across hundreds of pages of source material.

ARCHITECTURE DEEP DIVE

Key Features of Longformer

The Longformer introduces a sparse attention mechanism that scales linearly with sequence length, enabling Transformer models to process entire legal documents without truncation.

01

Sliding Window Attention

A fixed-size window of w tokens surrounding each position attends locally, capturing immediate context. This reduces complexity from O(n²) to O(n × w).

  • Default window size is typically 512 tokens
  • Each token attends to w/2 tokens on each side
  • Stacks multiple layers to build a large receptive field
  • Critical for processing dense legal paragraphs where local context defines meaning
02

Dilated Sliding Windows

Increases the receptive field without increasing computation by inserting gaps between attended tokens. The dilation factor grows exponentially across layers.

  • Lower layers use small dilation for fine-grained syntax
  • Upper layers use large dilation for document-level semantics
  • Enables a theoretical receptive field spanning thousands of tokens
  • Essential for tracking obligations defined across distant sections of a contract
03

Global Attention Tokens

Designates specific tokens—such as [CLS] or task-specific markers—to attend to every token in the sequence. These act as information hubs for sequence-level tasks.

  • Used for classification tokens and question tokens in QA
  • Global tokens receive attention from all positions
  • Symmetric: global tokens also attend to all positions
  • Enables effective multi-document fusion by using global tokens to aggregate cross-document evidence
04

Linear Memory Scaling

Full self-attention requires memory proportional to , making it infeasible for long legal documents. Longformer's memory footprint grows linearly with sequence length.

  • Processes sequences up to 4,096 tokens in base configuration
  • Extended variants handle 16,384+ tokens
  • Enables end-to-end processing of full contracts without chunking
  • Preserves cross-reference integrity that chunking strategies destroy
05

Task-Specific Global Patterns

The global attention pattern is configurable per task. For legal text summarization, global tokens can be placed on section headers and defined terms to anchor the summary.

  • Classification: global attention on [CLS]
  • QA: global attention on question tokens
  • Summarization: global attention on key structural markers
  • Allows the model to learn which positions require document-wide context
06

Pre-Training Objective Compatibility

Longformer uses the same masked language modeling (MLM) objective as BERT and RoBERTa, enabling initialization from existing pre-trained checkpoints.

  • Initialized from RoBERTa weights for general language understanding
  • Further pre-trained on legal corpora for domain adaptation
  • Compatible with standard fine-tuning pipelines
  • Supports both extractive and abstractive summarization heads
SPARSE ATTENTION ARCHITECTURE COMPARISON

Longformer vs. Other Long-Context Transformers

A technical comparison of the Longformer's sliding window and global attention mechanism against other prominent sparse and efficient Transformer architectures designed for long-document processing.

FeatureLongformerBigBirdReformer

Attention Mechanism

Sliding Window + Dilated Sliding Window + Global

Random + Window + Global

Locality-Sensitive Hashing (LSH)

Complexity Class

O(n)

O(n)

O(n log n)

Max Sequence Length (Reported)

4,096 tokens

4,096 tokens

64,000 tokens

Global Attention on Special Tokens

Autoregressive Decoding Support

Pre-trained Checkpoints Available

Dilated Sliding Window

Compatible with Standard Transformer Layers

LONGFORMER INSIGHTS

Frequently Asked Questions

Clarifying the mechanics, applications, and advantages of the Longformer model for processing extended legal documents.

Longformer is a modified Transformer model designed to process long documents efficiently by using a sparse attention mechanism that scales linearly with sequence length. Unlike standard Transformers, which use full self-attention with a quadratic O(n²) memory cost, Longformer introduces an attention pattern combining a sliding window of local context with global attention on pre-selected tokens. The sliding window allows each token to attend to a fixed number of neighbors on either side, capturing local context. Simultaneously, task-specific tokens—like the [CLS] token for classification or question tokens for QA—are designated as global, attending to all tokens and being attended to by all tokens. This hybrid approach reduces the computational complexity to O(n), enabling the processing of documents up to 4,096 tokens in the base model, with variants scaling to even longer sequences, making it ideal for multi-page legal contracts and lengthy judicial opinions.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.