Skip to main content
QUICK REVIEW

[Paper Review] cosFormer: Rethinking Softmax in Attention

Zhen Qin, Weixuan Sun|arXiv (Cornell University)|Feb 17, 2022
Advanced Neural Network ApplicationsComputer Science65 citations
TL;DR

cosFormer replaces softmax in attention with a linear, non-negative, cosine-reweighted mechanism, achieving linear time/space complexity while matching or surpassing vanilla Transformer performance on language modeling, downstream tasks, and long-range benchmarks.

ABSTRACT

Transformer has shown great successes in natural language processing, computer vision, and audio processing. As one of its core components, the softmax attention helps to capture long-range dependencies yet prohibits its scale-up due to the quadratic space and time complexity to the sequence length. Kernel methods are often adopted to reduce the complexity by approximating the softmax operator. Nevertheless, due to the approximation errors, their performances vary in different tasks/corpus and suffer crucial performance drops when compared with the vanilla softmax attention. In this paper, we propose a linear transformer called cosFormer that can achieve comparable or better accuracy to the vanilla transformer in both casual and cross attentions. cosFormer is based on two key properties of softmax attention: i). non-negativeness of the attention matrix; ii). a non-linear re-weighting scheme that can concentrate the distribution of the attention matrix. As its linear substitute, cosFormer fulfills these properties with a linear operator and a cosine-based distance re-weighting mechanism. Extensive experiments on language modeling and text understanding tasks demonstrate the effectiveness of our method. We further examine our method on long sequences and achieve state-of-the-art performance on the Long-Range Arena benchmark. The source code is available at https://github.com/OpenNLPLab/cosFormer.

Motivation & Objective

  • Investigate whether softmax attention can be replaced by a linear operator without sacrificing performance.
  • Develop a linear attention variant that preserves non-negativity and introduces recency/locality bias through cosine-based re-weighting.
  • Evaluate cosFormer on autoregressive and bidirectional language modeling, downstream classification tasks, and long-range sequence benchmarks.

Proposed method

  • Replace the softmax-based similarity with a linear kernel using ReLU to ensure non-negativity.
  • Decompose the attention computation to achieve linear time/space via the trick (phi(Q) and phi(K)) with a linear projection.
  • Introduce a cosine-based re-weighting mechanism to mimic softmax locality and stabilization effects, using a Ptolemy-based decomposition.
  • Derive the cosFormer formulation so that O = Q'K'^{T} cos(...)V decomposes into a sum of two linear terms for efficient computation.
  • Compare against kernels-based and pattern-based efficient transformers across autoregressive, bidirectional, and long-range tasks.

Experimental results

Research questions

  • RQ1Can softmax attention be effectively replaced by a linear operation while preserving attention properties like non-negativity and locality?
  • RQ2Does a cosine-based re-weighting mechanism provide stability and locality similar to softmax in various NLP tasks?
  • RQ3How does cosFormer perform on autoregressive and bidirectional language modeling, as well as downstream classification and long-range benchmarks?

Key findings

  • cosFormer achieves comparable or better accuracy than vanilla Transformer in language modeling and downstream tasks, with linear time/space complexity.
  • cosFormer demonstrates faster convergence and competitive perplexity on WikiText-103 autoregressive tasks compared to several baselines.
  • On downstream classification tasks (GLUE, IMDB, AMAZON), cosFormer matches or approaches the performance of the vanilla Transformer and outperforms many efficient transformers.
  • In the Long-Range Arena benchmark, cosFormer achieves state-of-the-art or competitive results, including best overall average across tasks.
  • Ablation shows the cos-based re-weighting improves convergence speed and final performance, indicating locality bias benefits natural language modeling.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.