Longformer › Memory and Speed Analysis

Michael BrenndoerferUpdated June 25, 20253 min read
Language AI HandbookAll chapters
Part 17 of 67Efficient Attention
Chapter 121Longformer › Memory and Speed Analysis
Reading section 11 of 16 · Memory and Speed Analysis · 3 minSection 11 of 16 · 3 min

Book contents

Section 11 of 16 · 3 min read

Longformer › Memory and Speed AnalysisOne section · Sections in this chapter11 / 16 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Understanding memory usage in precise terms helps you make informed decisions about batch size, sequence length, and hardware requirements when deploying Longformer. The theoretical complexity analysis tells us attention memory scales linearly, but the actual megabytes on a GPU depend on the specific configuration. Let's measure the savings achieved by Longformer compared to full attention across a range of sequence lengths.

In[22]:
Code
def measure_attention_memory(
    seq_lengths, embed_dim=768, num_heads=12, window_size=512
):
    """Estimate memory usage for attention matrices."""
    results = []

    for seq_len in seq_lengths:
        # Full attention: n x n attention matrix per head per layer
        full_attention_size = seq_len * seq_len * num_heads

        # Longformer: n x w for local + n x g for global (approximate)
        # Assuming 1 global token
        num_global = 1
        longformer_size = seq_len * (window_size + 2 * num_global) * num_heads

        # Convert to MB (assuming float32)
        bytes_per_float = 4
        full_mb = full_attention_size * bytes_per_float / (1024**2)
        longformer_mb = longformer_size * bytes_per_float / (1024**2)

        results.append(
            {
                "seq_len": seq_len,
                "full_attention_mb": full_mb,
                "longformer_mb": longformer_mb,
                "savings_ratio": longformer_mb / full_mb,
            }
        )

    return results


seq_lengths = [512, 1024, 2048, 4096, 8192]
memory_results = measure_attention_memory(seq_lengths)
Out[23]:
Visualization
Log-log line plot comparing memory usage of full attention versus Longformer.
Memory usage comparison between full attention and Longformer. Full attention grows quadratically while Longformer grows linearly, shown on log-log scale.
Bar chart showing Longformer memory as percentage of full attention at each sequence length.
Memory ratio showing Longformer memory as percentage of full attention. At 8,192 tokens, Longformer uses only 6% of full attention memory.

The memory comparison reveals the dramatic difference between full attention and Longformer. At 512 tokens, Longformer uses nearly as much memory as full attention because the window size is comparable to the sequence length. But as sequences grow, the gap widens exponentially. At 8,192 tokens, Longformer uses only about 6% of the memory that full attention would require.

This difference is why Longformer can process 4,096-token documents on hardware that would run out of memory with standard BERT-style attention at just 512 tokens.