Transformers meet Stochastic Block Models: Attention with Data-Adaptive Sparsity and Cost

Sungjun Cho, Seonwoo Min, Jinwoo Kim, Moontae Lee, Honglak Lee, Seunghoon Hong

TL;DR

The SBM-Transformer introduces a novel way for attention heads to "learn" their own sparsity patterns using Stochastic Block Models (SBMs). Unlike previous efficient Transformers that rely on fixed patterns or computationally expensive sparse masks, this model adaptively samples attention edges based on the input data. This approach achieves linear-to-quadratic complexity while maintaining, and often exceeding, the performance of full-attention models.

The Quadratic Bottleneck

The standard Transformer architecture suffers from a well-documented "quadratic bottleneck": as the sequence length ($n$) grows, the cost of computing all-to-all attention scores scales by $O(n^2)$. While numerous "efficient" Transformers have been proposed to mitigate this, they typically fall into two flawed categories:

  1. Hand-crafted Sparsity: Models like BigBird or Longformer utilize fixed patterns (e.g., sliding windows, global tokens). While efficient, they are rigid and cannot adapt to the specific semantic structure of the input data.
  2. Learnable Sparsity: Models using functions like $\alpha$-entmax can learn sparse patterns, but they often require computing the full $n \times n$ attention matrix before sparsifying it. This means they don't actually save computation or memory during training.

The authors propose a "middle way": a model that flexibly chooses between sparse and dense attention based on the input, with computational costs scaling only with the number of edges actually sampled.

Methods: The SBM-Transformer

The core innovation of the SBM-Transformer is treating the attention mask as a bipartite graph sampled from a mixed-membership Stochastic Block Model (SBM).

Forward and backward passes of the SBM-Transformer, highlighting the Straight-Through Estimator (STE) for gradient flow.
  • The SBM Mechanism: Each attention head is equipped with its own SBM. It maps queries and keys into a latent "node representation space" and defines a set of trainable "cluster embeddings." The probability of an attention edge existing between a query and a key depends on their respective memberships in these latent clusters.
  • Efficient Sampling: Instead of computing all $n^2$ probabilities, the model leverages the fastRG algorithm. This allows it to sample $m$ edges in $O(m)$ time. The number of edges is sampled from a Poisson distribution whose mean is determined dynamically by the input data.
  • Differentiability via STE: Because graph sampling is a discrete, non-differentiable operation, the authors employ a Straight-Through Estimator (STE). As shown in the architecture diagram, this allows gradients to flow back to the SBM parameters during backpropagation, effectively "teaching" the model which edges are useful for minimizing the predictive loss.
  • Exploration: To prevent the model from getting stuck in a state where it never samples certain edges, a small perturbation ($\delta$) is added to the edge probabilities, encouraging the model to occasionally explore new attention patterns.

Key Insights: Adaptive Sparsity

The SBM-Transformer offers several unique advantages over prior methods:

  • Data-Adaptive Cost: This is the first Transformer capable of dynamically adjusting its computational budget. For simple tasks, it might only use 1% density; for complex ones requiring dense interactions, it can scale up to 100% (full attention).
The SBM can adaptively generate dense, block-diagonal, or sparse attention patterns based on the learned cluster assignments of query and key nodes.
  • Sparsity as a Feature, Not a Bug: Interestingly, the SBM-Transformer often outperforms full-attention models while using only 20-30% of the edges. The authors suggest that this structural sparsity acts as a form of regularization, filtering out "noise" in the attention scores that typically plagues dense models.
  • Universal Approximation: The authors provide a theoretical proof that the SBM-Transformer remains a universal approximator for sequence-to-sequence functions, even when the number of connections is restricted to $O(n)$.
  • Emergent Hierarchy: Layer-wise analysis revealed that different layers learn different densities. Lower layers often remain denser to capture global context, while upper layers become highly sparse to focus on specific syntactic or task-related patterns.

Datasets and Key Results

The model was rigorously evaluated across three distinct fronts:

  1. Synthetic Task: A "Repeated Tokens" binary classification task designed to stress-test whether the model can learn to use full attention when strictly necessary. While low-rank models (like Linformer or Performer) failed because they couldn't capture necessary pairwise interactions, SBM-Transformer successfully "dialed up" its density to 100% to solve the task perfectly.
  2. Long Range Arena (LRA): On this standard benchmark for efficient Transformers (involving long sequences across text, images, etc.), SBM-Transformer achieved the highest average score, outperforming both the vanilla Transformer and popular variants.
  3. GLUE Benchmark: On downstream NLP tasks, the model maintained competitive F1 scores while using an average attention density of only 13.5%.

Limitations and Future Work

The primary hurdle for the SBM-Transformer is hardware alignment. While the model significantly reduces theoretical floating-point operations (FLOPs), modern GPUs are heavily optimized for dense matrix multiplications. The "unstructured" sparsity (where edges are scattered randomly) generated by the SBM is currently harder to accelerate than "block" sparsity.

Consequently, while memory usage is drastically lower, actual wall-clock runtimes can currently be longer than dense models. Future work will likely focus on developing specialized GPU kernels to make this fine-grained, adaptive sparsity as fast in practice as it is in theory.

Summary updated

BibTeX
@inproceedings{cho2022transformers,
  author = {Sungjun Cho and Seonwoo Min and Jinwoo Kim and Moontae Lee and Honglak Lee and Seunghoon Hong},
  title = {{Transformers meet Stochastic Block Models: Attention with Data-Adaptive Sparsity and Cost}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=HW8tX7_u4QC},
}
Figure