Close Menu
techwavedispatch.com

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Approximating self-attention in linear time and memory via the Nyström method

    November 15, 2026

    A New Way to Build With Machine Learning

    November 15, 2026

    Proximal Policy Optimization (PPO)

    November 15, 2026
    Facebook X (Twitter) Instagram
    Trending
    • Approximating self-attention in linear time and memory via the Nyström method
    • A New Way to Build With Machine Learning
    • Proximal Policy Optimization (PPO)
    • Train and Fine-Tune Sentence Transformers Models
    • Deploying 🤗 ViT on Kubernetes with TF Serving
    • Introducing Skops
    • Hugging Face’s TensorFlow Philosophy
    • A Gentle Introduction to 8-bit Matrix Multiplication for transformers at scale using transformers, accelerate and bitsandbytes
    Facebook X (Twitter) Instagram
    techwavedispatch.com
    Subscribe
    Sunday, November 15
    • Home
    • AI Tools & Software
    • Machine Learning
    • Generative AI
    • Robotics & Automation
    • Future Tech & Hardware
    • AI in Business
    • Ethics & Regulations
    • More
      • Gadgets & Smart Devices
      • Cybersecurity & AI
      • Tech Breakthroughs
    techwavedispatch.com
    Home»AI Tools & Software»Approximating self-attention in linear time and memory via the Nyström method
    AI Tools & Software

    Approximating self-attention in linear time and memory via the Nyström method

    adminBy adminNovember 15, 2026No Comments6 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Approximating self-attention in linear time and memory via the Nyström method
    Share
    Facebook Twitter LinkedIn Pinterest Email



    Introduction

    Transformers have exhibited remarkable performance on various Natural Language Processing and Computer Vision tasks. Their success can be attributed to the self-attention mechanism, which captures the pairwise interactions between all the tokens in an input. However, the standard self-attention mechanism has a time and memory complexity of O(n2)O(n^2)O(n2) (where nnn is the length of the input sequence), making it expensive to train on long input sequences.

    The Nyströmformer is one of many efficient Transformer models that approximates standard self-attention with O(n)O(n)O(n) complexity. Nyströmformer exhibits competitive performance on various downstream NLP and CV tasks while improving upon the efficiency of standard self-attention. The aim of this blog post is to give readers an overview of the Nyström method and how it can be adapted to approximate self-attention.



    Nyström method for matrix approximation

    At the heart of Nyströmformer is the Nyström method for matrix approximation. It allows us to approximate a matrix by sampling some of its rows and columns. Let’s consider a matrix Pn×nP^{n \times n}Pn×n, which is expensive to compute in its entirety. So, instead, we approximate it using the Nyström method. We start by sampling mmm rows and columns from PPP. We can then arrange the sampled rows and columns as follows:

    Representing P as a block matrix

    We now have four submatrices: AP,BP,FP,A_P, B_P, F_P,AP​,BP​,FP​, and CPC_PCP​, with sizes m×m,m×(n−m),(n−m)×mm \times m, m \times (n – m), (n – m) \times mm×m,m×(n−m),(n−m)×m and (n−m)×(n−m)(n – m) \times (n – m)(n−m)×(n−m) respectively. The mmm sampled columns are contained in APA_PAP​ and FPF_PFP​, whereas the mmm sampled rows are contained in APA_PAP​ and BPB_PBP​. So, the entries of AP,BP,A_P, B_P,AP​,BP​, and FPF_PFP​ are known to us, and we will estimate CPC_PCP​. According to the Nyström method, CPC_PCP​ is given by:

    CP=FPAP+BPC_P = F_P A_P^+ B_PCP​=FP​AP+​BP​

    Here, +++ denotes the Moore-Penrose inverse (or pseudoinverse).
    Thus, the Nyström approximation of P,P^P, \hat{P}P,P^ can be written as:

    Nyström approximation of P

    As shown in the second line, P^\hat{P}P^ can be expressed as a product of three matrices. The reason for doing so will become clear later.



    Can we approximate self-attention with the Nyström method?

    Our goal is to ultimately approximate the softmax matrix in standard self attention: S = softmax QKTd \frac{QK^T}{\sqrt{d}} d​QKT​

    Here, QQQ and KKK denote the queries and keys respectively. Following the procedure discussed above, we would sample mmm rows and columns from SSS, form four submatrices, and obtain S^\hat{S}S^:

    Nyström approximation of S

    But, what does it mean to sample a column from SSS? It means we select one element from each row. Recall how S is calculated: the final operation is a row-wise softmax. To find a single entry in a row, we must access all other entries (for the denominator in softmax). So, sampling one column requires us to know all other columns in the matrix. Therefore, we cannot directly apply the Nyström method to approximate the softmax matrix.



    How can we adapt the Nyström method to approximate self-attention?

    Instead of sampling from SSS, the authors propose to sample landmarks (or Nyström points) from queries and keys. We denote the query landmarks and key landmarks as Q~\tilde{Q}Q~​ and K~\tilde{K}K~ respectively. Q~\tilde{Q}Q~​ and K~\tilde{K}K~ can be used to construct three matrices corresponding to those in the Nyström approximation of SSS. We define the following matrices:

    F~=softmax(QK~Td)A~=softmax(Q~K~Td)+B~=softmax(Q~KTd)\tilde{F} = softmax(\frac{Q\tilde{K}^T}{\sqrt{d}}) \hspace{40pt} \tilde{A} = softmax(\frac{\tilde{Q}\tilde{K}^T}{\sqrt{d}})^+ \hspace{40pt} \tilde{B} = softmax(\frac{\tilde{Q}K^T}{\sqrt{d}})F~=softmax(d​QK~T​)A~=softmax(d​Q~​K~T​)+B~=softmax(d​Q~​KT​)

    The sizes of F~\tilde{F}F~, A~\tilde{A}A~, and B~)are(n×m,m×m,\tilde{B}) are \\(n \times m, m \times m,B~)are(n×m,m×m, and m×nm \times nm×n respectively.
    We replace the three matrices in the Nyström approximation of SSS with the new matrices we have defined to obtain an alternative Nyström approximation:

    S^=F~A~B~=softmax(QK~Td)softmax(Q~K~Td)+softmax(Q~KTd)\begin{aligned}\hat{S} &= \tilde{F} \tilde{A} \tilde{B} \\ &= softmax(\frac{Q\tilde{K}^T}{\sqrt{d}}) softmax(\frac{\tilde{Q}\tilde{K}^T}{\sqrt{d}})^+ softmax(\frac{\tilde{Q}K^T}{\sqrt{d}}) \end{aligned}S^​=F~A~B~=softmax(d​QK~T​)softmax(d​Q~​K~T​)+softmax(d​Q~​KT​)​

    This is the Nyström approximation of the softmax matrix in the self-attention mechanism. We multiply this matrix with the values ( VVV) to obtain a linear approximation of self-attention. Note that we never calculated the product QKTQK^TQKT, avoiding the O(n2)O(n^2)O(n2) complexity.



    How do we select landmarks?

    Instead of sampling mmm rows from QQQ and KKK, the authors propose to construct Q~\tilde{Q}Q~​ and K~\tilde{K}K~
    using segment means. In this procedure, nnn tokens are grouped into mmm segments, and the mean of each segment is computed. Ideally, mmm is much smaller than nnn. According to experiments from the paper, selecting just 323232 or 646464 landmarks produces competetive performance compared to standard self-attention and other efficient attention mechanisms, even for long sequences lengths ( n=4096n=4096n=4096 or 819281928192).

    The overall algorithm is summarised by the following figure from the paper:

    Efficient self-attention with the Nyström method

    The three orange matrices above correspond to the three matrices we constructed using the key and query landmarks. Also, notice that there is a DConv box. This corresponds to a skip connection added to the values using a 1D depthwise convolution.



    How is Nyströmformer implemented?

    The original implementation of Nyströmformer can be found here and the HuggingFace implementation can be found here. Let’s take a look at a few lines of code (with some comments added) from the HuggingFace implementation. Note that some details such as normalization, attention masking, and depthwise convolution are avoided for simplicity.

    
    key_layer = self.transpose_for_scores(self.key(hidden_states)) 
    value_layer = self.transpose_for_scores(self.value(hidden_states)) 
    query_layer = self.transpose_for_scores(mixed_query_layer) 
    
    q_landmarks = query_layer.reshape(
        -1,
        self.num_attention_heads,
        self.num_landmarks,
        self.seq_len // self.num_landmarks,
        self.attention_head_size,
    ).mean(dim=-2) 
    
    k_landmarks = key_layer.reshape(
        -1,
        self.num_attention_heads,
        self.num_landmarks,
        self.seq_len // self.num_landmarks,
        self.attention_head_size,
    ).mean(dim=-2) 
    
    kernel_1 = torch.nn.functional.softmax(torch.matmul(query_layer, k_landmarks.transpose(-1, -2)), dim=-1) 
    kernel_2 = torch.nn.functional.softmax(torch.matmul(q_landmarks, k_landmarks.transpose(-1, -2)), dim=-1) 
    
    attention_scores = torch.matmul(q_landmarks, key_layer.transpose(-1, -2)) 
    
    kernel_3 = nn.functional.softmax(attention_scores, dim=-1) 
    attention_probs = torch.matmul(kernel_1, self.iterative_inv(kernel_2)) 
    new_value_layer = torch.matmul(kernel_3, value_layer) 
    context_layer = torch.matmul(attention_probs, new_value_layer) 
    



    Using Nyströmformer with HuggingFace

    Nyströmformer for Masked Language Modeling (MLM) is available on HuggingFace. Currently, there are 4 checkpoints, corresponding to various sequence lengths: nystromformer-512, nystromformer-1024, nystromformer-2048, and nystromformer-4096. The number of landmarks, mmm, can be controlled using the num_landmarks parameter in the NystromformerConfig. Let’s take a look at a minimal example of Nyströmformer for MLM:

    from transformers import AutoTokenizer, NystromformerForMaskedLM
    import torch
    
    tokenizer = AutoTokenizer.from_pretrained("uw-madison/nystromformer-512")
    model = NystromformerForMaskedLM.from_pretrained("uw-madison/nystromformer-512")
    
    inputs = tokenizer("Paris is the [MASK] of France.", return_tensors="pt")
    
    with torch.no_grad():
        logits = model(**inputs).logits
    
    
    mask_token_index = (inputs.input_ids == tokenizer.mask_token_id)[0].nonzero(as_tuple=True)[0]
    
    predicted_token_id = logits[0, mask_token_index].argmax(axis=-1)
    tokenizer.decode(predicted_token_id)
    
    Output:
    ----------------------------------------------------------------------------------------------------
    capital
    

    Alternatively, we can use the pipeline API (which handles all the complexity for us):

    from transformers import pipeline
    unmasker = pipeline('fill-mask', model='uw-madison/nystromformer-512')
    unmasker("Paris is the [MASK] of France.")
    
    Output:
    ----------------------------------------------------------------------------------------------------
    [{'score': 0.829957902431488,
      'token': 1030,
      'token_str': 'capital',
      'sequence': 'paris is the capital of france.'},
    {'score': 0.022157637402415276,
      'token': 16081,
      'token_str': 'birthplace',
      'sequence': 'paris is the birthplace of france.'},
    {'score': 0.01904447190463543,
      'token': 197,
      'token_str': 'name',
      'sequence': 'paris is the name of france.'},
    {'score': 0.017583081498742104,
      'token': 1107,
      'token_str': 'kingdom',
      'sequence': 'paris is the kingdom of france.'},
    {'score': 0.005948934704065323,
      'token': 148,
      'token_str': 'city',
      'sequence': 'paris is the city of france.'}]
    



    Conclusion

    Nyströmformer offers an efficient approximation to the standard self-attention mechanism, while outperforming other linear self-attention schemes. In this blog post, we went over a high-level overview of the Nyström method and how it can be leveraged for self-attention. Readers interested in deploying or fine-tuning Nyströmformer for downstream tasks can find the HuggingFace documentation here.

    Approximating linear Memory Method Nyström selfattention Time
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    admin
    • Website

    Related Posts

    A New Way to Build With Machine Learning

    November 15, 2026

    Proximal Policy Optimization (PPO)

    November 15, 2026

    Train and Fine-Tune Sentence Transformers Models

    November 15, 2026
    Leave A Reply Cancel Reply

    Top Posts

    NVIDIA Is Investigating Reports That CEO Jensen Huang Was The Target Of Chinese Spy Efforts

    November 8, 202619 Views

    ADIndex: Layer-Adaptive Sparse Attention Indexing via Group-Level Distillation

    November 11, 20265 Views

    Anthropic releases new Mythos and Fable models amid RSI concerns

    November 9, 20265 Views

    One Policy for 100 Household Tasks: Our Solution for the 2026 BEHAVIOR Challenge

    November 11, 20264 Views
    Don't Miss
    AI Tools & Software

    Approximating self-attention in linear time and memory via the Nyström method

    By adminNovember 15, 20260

    Introduction Transformers have exhibited remarkable performance on various Natural Language Processing and Computer Vision tasks.…

    A New Way to Build With Machine Learning

    November 15, 2026

    Proximal Policy Optimization (PPO)

    November 15, 2026

    Train and Fine-Tune Sentence Transformers Models

    November 15, 2026
    About Us

    Welcome to TechWave Dispatch

    At TechWave Dispatch, we bring you the latest news, insights, and developments from the ever-evolving world of technology. Our mission is to keep readers informed about the innovations shaping our digital future.

    From artificial intelligence and machine learning to cybersecurity, robotics, smart devices, and groundbreaking technologies, we cover the topics that matter in today's fast-moving tech landscape.

    Our Picks

    Approximating self-attention in linear time and memory via the Nyström method

    November 15, 2026

    A New Way to Build With Machine Learning

    November 15, 2026

    Proximal Policy Optimization (PPO)

    November 15, 2026
    Most Popular

    After blocking Muse, Amazon now lets users text Alexa to help them shop

    November 6, 20260 Views

    A controlled world for robot learning and evaluation

    November 6, 20260 Views

    Perplexity now has its own always-on AI agent

    November 6, 20260 Views
    Facebook X (Twitter) Instagram Pinterest
    • Terms and Conditions
    • Privacy Policy
    • Contact Us
    • About Us
    © 2026 TechWave Dispatch. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.