Skip to main content

An Implementation of Nystromformer, Nyström based algorithm to approximate standard self-attention.

Project description

Nystromformer Twitter

PyPI Run Tests Upload Python Package Code style: black codecov

GitHub License GitHub stars GitHub followers Twitter Follow

An implementation of the Nyströmformer: A Nyström-Based Algorithm for Approximating Self-Attention paper by Xiong et al. The self-attention mechanism that encodes the influence or dependence of other tokens on each specific token is a key component of the performance of Transformers. This uses the Nyström method to approximate standard self-attention with O(n) complexity allowing to exhibit scalability as a function of sequence length.

Installation

Run the following to install:

pip install nystromformer

Developing nystromformer

To install nystromformer, along with tools you need to develop and test, run the following in your virtualenv:

git clone https://github.com/Rishit-dagli/Nystromformer.git
# or clone your own fork

cd Nystromformer
pip install -e .[dev]

To run rank and shape tests run the following:

pytest -v --disable-warnings --cov

Usage

Nystrom Attention

import tensorflow as tf
from nystromformer import NystromAttention

attn = NystromAttention(
    dim = 512,
    dim_head = 64,
    heads = 8,
    num_landmarks = 256,    # number of landmarks
    pinv_iterations = 6,    # number of moore-penrose iterations for approximating pinverse. 6 was recommended by the paper
    residual = True         # whether to do an extra residual with the value or not. supposedly faster convergence if turned on
)

x = tf.random.normal((1, 16384, 512))
mask = tf.ones((1, 16384), dtype=tf.bool)

attn(x, mask = mask) # (1, 16384, 512)

Nystromformer

import tensorflow as tf
from nystromformer import Nystromformer

model = Nystromformer(
    dim = 512,
    dim_head = 64,
    heads = 8,
    depth = 6,
    num_landmarks = 256,
    pinv_iterations = 6
)

x = tf.random.normal((1, 16384, 512))
mask = tf.ones((1, 16384), dtype=tf.bool)

model(x, mask = mask) # (1, 16384, 512)

Want to Contribute 🙋‍♂️?

Awesome! If you want to contribute to this project, you're always welcome! See Contributing Guidelines. You can also take a look at open issues for getting more information about current or upcoming tasks.

Want to discuss? 💬

Have any questions, doubts or want to present your opinions, views? You're always welcome. You can start discussions.

Citation

@misc{xiong2021nystromformer,
    title   = {Nyströmformer: A Nyström-Based Algorithm for Approximating Self-Attention},
    author  = {Yunyang Xiong and Zhanpeng Zeng and Rudrasis Chakraborty and Mingxing Tan and Glenn Fung and Yin Li and Vikas Singh},
    year    = {2021},
    eprint  = {2102.03902},
    archivePrefix = {arXiv},
    primaryClass = {cs.CL}
}

Yannic Kilcher's Video PyTorch Implementation PyTorch Implementation

License

Copyright 2020 Rishit Dagli

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nystromformer-0.1.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

nystromformer-0.1.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file nystromformer-0.1.0.tar.gz.

File metadata

  • Download URL: nystromformer-0.1.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for nystromformer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e873e39fe263947f7eed7acde14835291991f33b7ba98bc5fa2e31fdb0d50bf2
MD5 383a438aa0d8b286fbb5c2025593500f
BLAKE2b-256 a500c4d98c4e0d32fcacbf32409e22bd12b827d8e4f297c3018de0f3234fdb8c

See more details on using hashes here.

File details

Details for the file nystromformer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nystromformer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40fa2541131847b5cbf22e38d6b6aeac19e9071fe9a3a17f47f0461db65b5d74
MD5 5ff5d293574d021a13cd6e0bda7e8588
BLAKE2b-256 c9b08fa9ad7a379bd161802222b2edef19728ed7c36be57a8d4e7c66eb103600

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page