Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Longformer

Longformer pairs sliding-window attention with a few global tokens, so its cost grows linearly with length and it reads 4,096-token documents.

Longformer is a transformer for long documents that replaces full self-attention with sliding-window attention around every token plus global attention on a few task-chosen tokens, so its cost grows linearly with input length instead of quadratically. Iz Beltagy, Matthew Peters, and Arman Cohan of the Allen Institute for AI introduced it in the 2020 paper Longformer: The Long-Document Transformer. Encoders such as BERT and RoBERTa read at most 512 tokens, so longer texts had to be cut into chunks that lose the links between them. The released Longformer models read 4,096 tokens in one pass, a context window eight times longer.

How Longformer Attention Works#

Each layer combines local and global patterns inside one attention mechanism:

  • Sliding window attention: Every token attends to a fixed window of neighbors, 256 on each side in the released models.
  • Dilated sliding window: Gaps between the attended positions let a head see further at the same cost. The authors used dilation on a few heads in the upper layers of their character-level language models.
  • Global attention: Selected tokens attend to every position and every position attends to them, through separate query, key, and value projections. The task picks them: the [CLS] token for classification and the question tokens for question answering.

Stacking layers widens the reach of the window the way stacked convolutions grow the receptive field of a convolutional neural network (CNN): l layers with window w cover l × w tokens. Each layer computes about n × w attention scores instead of n × n. Counting the pairs in a Longformer mask makes the difference visible:

import torch

window = 512  # Longformer-base: 256 tokens on each side of every token

for n in (1024, 2048, 4096):
    pos = torch.arange(n)
    # Sliding window: each token attends to neighbors within window / 2 positions
    mask = (pos[:, None] - pos[None, :]).abs() <= window // 2
    # Global attention on token 0 ([CLS]): it sees every token and every token sees it
    mask[0, :] = True
    mask[:, 0] = True
    print(f"{n} tokens: full attention {n * n / 1e6:.1f}M scores, Longformer {mask.sum().item() / 1e6:.1f}M")

It prints 0.5M, 1.0M, and 2.0M Longformer scores against 1.0M, 4.2M, and 16.8M for full attention: each doubling of the input doubles Longformer's attention work and quadruples the work of full attention. Real implementations compute only the band of scores, which is where the memory savings come from.

Training and Results#

Longformer starts from the RoBERTa checkpoint. The authors copied its 512 learned position embeddings eight times to cover 4,096 positions and continued masked language model pretraining on long documents. After fine-tuning, it consistently outperformed RoBERTa on long-document tasks and set state-of-the-art results at the time on the WikiHop and TriviaQA question answering benchmarks, while the character-level language model set new results on text8 and enwik8.

The Longformer-Encoder-Decoder (LED) adds a decoder for text summarization and other text generation tasks, demonstrated on summarizing full scientific papers from arXiv. Hugging Face Transformers implements both models and takes the global tokens as a global_attention_mask. Domain versions followed, such as Clinical-Longformer, which was pretrained on large clinical text corpora and evaluated on 10 tasks including named entity recognition, question answering, and document classification.

  • Transformer-XL: Reads a long text segment by segment and lets each segment attend to cached hidden states of the previous one, extending context through recurrence for left-to-right generation.
  • BigBird: A Google model from the same year that adds random attention links to window and global attention and also reads 4,096 tokens.
  • Sparse attention: The broader family of fixed attention patterns that Longformer belongs to.
  • Flash attention: Computes exact full attention in on-chip memory tiles, so memory grows linearly with length while the number of scores stays quadratic.

Local and Global Attention in Vision#

High-resolution images pose the same problem, because a vision transformer (ViT) turns an image into thousands of patch tokens. The Swin Transformer answers it with shifted local windows, and Meta's Segment Anything Model (SAM), which runs in the Ultralytics package, mixes windowed and global attention block by block for image segmentation: its ViT-B image encoder attends within non-overlapping 14 × 14 windows of its 64 × 64 patch grid in 8 of its 12 blocks and globally across all 4,096 patches in blocks 2, 5, 8, and 11.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning