Papers
Topics
Authors
Recent
Search
2000 character limit reached

SortCut Algorithm for Efficient Sparse Attention

Updated 7 February 2026
  • SortCut is an algorithmic enhancement to Sparse Sinkhorn Attention that dynamically selects and truncates sequence blocks for efficient long-sequence modeling.
  • It employs a learned sorting network with Sinkhorn normalization to rank block importance, balancing global context with local attention.
  • Empirical results show SortCut achieves near-linear time and space complexity while maintaining performance close to full attention with significant memory savings.

SortCut is an algorithmic enhancement to the Sparse Sinkhorn Attention mechanism, designed to enable efficient and scalable self-attention for long sequences by dynamically truncating attention to the most salient regions. Its core principle is the explicit selection, via learned sorting and hard truncation, of a fixed budget of sequence blocks before applying local attention. This process yields near-linear time and space complexity in sequence length, while maintaining empirical performance comparable to full attention on a variety of natural language and sequence modeling tasks (Tay et al., 2020).

1. Conceptual Motivation and Design Principles

Standard Transformer self-attention incurs quadratic memory and computational costs in the sequence length â„“\ell. Local attention reduces this but loses the ability to aggregate global context. Sparse Sinkhorn Attention addresses this by learning a permutation of sequence blocks, allowing local attention to span non-contiguous parts of the original sequence. SortCut introduces an additional compression step: after learning block importance through permutation, it truncates the permuted sequence by retaining only the highest-scoring nn blocks. This approach exploits input-dependent structure, focusing memory and compute resources on the most relevant content per example.

SortCut is especially effective when applied in encoder architectures, where a single learned sort suffices per layer. For decoder/autoregressive applications, the sorting must be performed at each timestep with special causal constraints, increasing computational overhead.

2. Formal Definition and Mathematical Specification

Consider sequence embeddings X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}, partitioned into NB=ℓ/BN_B = \ell / B non-overlapping blocks of size BB. A block-pooling operation ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d} (typically mean or sum pooling) aggregates each block's embeddings.

A small block-sorting network P(⋅):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B} assigns each block a vector of sorting logits Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}. These logits are perturbed (optionally) with Gumbel noise, scaled by a temperature parameter τ\tau, then normalized using NiterN_{\text{iter}} Sinkhorn iterations to yield a soft permutation matrix nn0.

Block-wise key and value representations nn1, nn2 are linearly projected and block-pooled, then permuted:

nn3

SortCut truncates the sorted blocks, retaining only the first nn4:

nn5

Attention is computed using queries nn6 against the unfolded truncated blocks:

nn7

where nn8 denotes block-sorting followed by unpooling.

3. Algorithmic Workflow and Pseudocode

The SortCut variant of Sparse Sinkhorn Attention involves the following pipeline for each attention head:

  1. Compute nn9, X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}0, X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}1 by projecting the input X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}2.
  2. Pool X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}3 and X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}4 into blocks (X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}5, X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}6).
  3. Obtain sorting logits via the feed-forward network X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}7.
  4. Optionally add Gumbel noise, normalize via Sinkhorn iterations to soft permutation matrix X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}8.
  5. Apply X∈Rℓ×dX \in \mathbb{R}^{\ell \times d}9 to block-pooled NB=ℓ/BN_B = \ell / B0 and NB=ℓ/BN_B = \ell / B1 for permutation.
  6. Truncate to top-NB=â„“/BN_B = \ell / B2 blocks (NB=â„“/BN_B = \ell / B3, NB=â„“/BN_B = \ell / B4).
  7. Expand truncated blocks back to token-level representations.
  8. Compute scaled dot-product attention using NB=â„“/BN_B = \ell / B5 and the expanded NB=â„“/BN_B = \ell / B6, yielding outputs NB=â„“/BN_B = \ell / B7.

Block pooling can be adapted (sum, mean) as necessary. For multi-head attention, each head maintains independent sorting networks and budgets.

Step Input(s) Output
Block pooling NB=â„“/BN_B = \ell / B8 NB=â„“/BN_B = \ell / B9
Sorting logits BB0 BB1
Sinkhorn norm. BB2, BB3 BB4
Permutation BB5 BB6
Truncation BB7 BB8

This structured pipeline underpins SortCut’s efficiency and flexibility.

4. Computational Complexity and Efficiency

For sequence length BB9, block size ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}0, number of blocks ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}1, and SortCut budget ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}2:

  • Full attention: ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}3 time, ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}4 memory
  • Block-local attention: ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}5 time, ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}6 memory
  • Sinkhorn block-sort attention: Adds ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}7 Sinkhorn cost, no truncation
  • SortCut (with truncation): ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}8 time, ψP(X)=B(X)∈RNB×d\psi_P(X) = B(X) \in \mathbb{R}^{N_B \times d}9 memory

Since P(⋅):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}0 typically, SortCut achieves near-linear time and space in P(⋅):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}1, aside from the soft permutation (Sinkhorn) cost—amortized over training. Unlike masking-based sparse architectures, SortCut requires only standard matrix operations, simplifying implementation.

5. Hyperparameterization and Influence

Key SortCut hyperparameters include:

  • Block size P(â‹…):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}2: Governs granularity; larger P(â‹…):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}3 yields fewer blocks and cheaper Sinkhorn sorting, at potential loss of block-level detail.
  • Sinkhorn temperature P(â‹…):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}4 and iteration count P(â‹…):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}5: Lower P(â‹…):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}6 and higher P(â‹…):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}7 make the learned permutation more discrete. However, excessively sharp (hard) sorting impairs gradient flow. Empirical ablative analysis shows P(â‹…):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}8 and P(â‹…):Rd→RNBP(\cdot): \mathbb{R}^d \rightarrow \mathbb{R}^{N_B}9 are typically optimal; Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}0 (no Sinkhorn) severely degrades performance.
  • Truncation budget Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}1: The number of blocks retained post-sort. Lower Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}2 saves memory but potentially omits context; in practice, even Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}3 (on long NLP sequences) can nearly match full attention using Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}4 of the memory.
  • Block sorting network Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}5: Its architecture and capacity affect permutation quality.

Ablations confirm that small Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}6 can suffice and that the balance between Sinkhorn softness and truncation aggressiveness is crucial to optimal trade-offs.

6. Integration with Meta-Sorting Networks and Causal Sinkhorn

The meta-sorting network Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}7 emits per-block logits guiding the permutation. In encoder architectures, a single sort suffices per layer, maximizing efficiency. For causal (decoder) attention, the block pooling and Sinkhorn normalization must enforce causality:

  • Block pooling up to position Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}8 for block Rlogits∈RNB×NBR_{\text{logits}} \in \mathbb{R}^{N_B \times N_B}9, ensuring no future context access.
  • Masked Sinkhorn normalization prohibits permutation entries for indices Ï„\tau0.

Causal SortCut thus requires recomputation at every decoding step, making it better suited to encoder use cases.

7. Empirical Results and Comparative Analysis

On standard NLP and sequence tasks (IMDb, SST, SNLI, MultiNLI, up to 2k tokens), SortCut encoders with τ\tau1 blocks demonstrate accuracy within τ\tau2–τ\tau3 of full attention while reducing memory usage by τ\tau4–τ\tau5.

Representative experimental results:

Model IMDb Accuracy (%) SNLI Accuracy (%) Memory Use
Vanilla Transformer 85.12 78.87 Baseline (O(Ï„\tau6))
SortCut (τ\tau7) 84.32 – τ\tau8
SortCut (τ\tau9) 84.43 – NiterN_{\text{iter}}0
SortCut (NiterN_{\text{iter}}1) – 80.30 Reduced

Examinations reveal that Sinkhorn is essential for performance; setting NiterN_{\text{iter}}2 collapses accuracy. Optimal results arise from a soft—but not too hard—sorting regime and moderate block budgets.

Empirically, SortCut sometimes outperforms full attention models on NLI tasks, suggesting that focused content selection can act as a regularizer. The practical utility of hard truncation and learned permutation is consistently confirmed by ablations (Tay et al., 2020).


In summary, SortCut generalizes Sparse Sinkhorn Attention with post-sorting truncation, dynamically selecting the most pertinent subsequence blocks for attention computation. This results in a simple, efficient, and highly scalable mechanism—well-suited for long-sequence modeling without requiring specialized hardware or custom kernels, achieving substantial savings in computational resources while closely matching standard attention's empirical performance.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (1)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to SortCut Algorithm.