Taylor Series Linear Attention
Explorations into the Taylor Series Linear Attention proposed in the paper Zoology: Measuring and Improving Recall in Efficient Language Models
This repository will offer full self attention, cross attention, and autoregressive via CUDA kernel from pytorch-fast-transformers.
Be aware that in linear attention, the quadratic is pushed to the attention head dimension. With the second taylor expansion, this becomes O(D^3), so more research needed.
Update: It works! Strongest formulation of linear attention I've come across in the literature
Appreciation
- A16Z Open Source AI Grant Program and 🤗 Huggingface for the generous sponsorships, as well as my other sponsors, for affording me the independence to open source current artificial intelligence research
Install
$ pip install taylor-series-linear-attention
Usage
import torch
from taylor_series_linear_attention import TaylorSeriesLinearAttn
attn = TaylorSeriesLinearAttn(
dim = 512,
dim_head = 16,
heads = 16
)
x = torch.randn(1, 4096, 512)
mask = torch.ones((1, 4096)).bool()
out = attn(x, mask = mask)
assert x.shape == out.shape
Cross attention
import torch
from taylor_series_linear_attention import TaylorSeriesLinearAttn
attn = TaylorSeriesLinearAttn(
dim = 512,
dim_head = 16,
heads = 16
)
x = torch.randn(1, 1024, 512)
context = torch.randn(1, 65536, 512)
context_mask = torch.ones((1, 65536)).bool()
out = attn(x, context = context, mask = context_mask)
assert x.shape == out.shape
For autoregressive, first pip install pytorch-fast-transformers. Then set causal = True
import torch
from taylor_series_linear_attention import TaylorSeriesLinearAttn
attn = TaylorSeriesLinearAttn(
dim = 512,
dim_head = 16,
heads = 16,
causal = True, # set this to True
rotary_emb = True # rotary embeddings
)
x = torch.randn(1, 8192, 512)
out = attn(x)
assert x.shape == out.shape
Taylor Series Poly Attention
Higher-order (order-2) linear attention from Poly-Attention, with the taylor series feature map on both passes
import torch
from taylor_series_linear_attention import TaylorSeriesPolyAttention
attn = TaylorSeriesPolyAttention(
dim = 512,
heads = 16,
dim_head = 32,
remove_even_power_dups = False,
prenorm = True,
multiply_root_value = True,
use_root_value_as_attn_gate = True
)
x = torch.randn(1, 1024, 512)
mask = torch.ones((1, 1024)).bool()
out = attn(x, mask = mask)
assert x.shape == out.shape
The function composition demonstration is included in the project root, runnable as
$ python train_function_composition.py
Todo
- take care of caching for causal variant
Citations
@inproceedings{Arora2023ZoologyMA,
title = {Zoology: Measuring and Improving Recall in Efficient Language Models},
author = {Simran Arora and Sabri Eyuboglu and Aman Timalsina and Isys Johnson and Michael Poli and James Zou and Atri Rudra and Christopher R'e},
year = {2023},
url = {https://api.semanticscholar.org/CorpusID:266149332}
}
@inproceedings{chakrabarti2026poly,
title = {Poly-attention: a general scheme for higher-order self-attention},
author = {Chakrabarti, Sayak and Pitassi, Toniann and Alman, Josh},
booktitle = {International Conference on Learning Representations (ICLR)},
year = {2026}
}
@inproceedings{Keles2022OnTC,
title = {On The Computational Complexity of Self-Attention},
author = {Feyza Duman Keles and Pruthuvi Maheshakya Wijewardena and Chinmay Hegde},
booktitle = {International Conference on Algorithmic Learning Theory},
year = {2022},
url = {https://api.semanticscholar.org/CorpusID:252198880}
}
@article{Shazeer2019FastTD,
title = {Fast Transformer Decoding: One Write-Head is All You Need},
author = {Noam M. Shazeer},
journal = {ArXiv},
year = {2019},
volume = {abs/1911.02150}
}
@inproceedings{Peng2023RWKVRR,
title = {RWKV: Reinventing RNNs for the Transformer Era},
author = {Bo Peng and Eric Alcaide and Quentin G. Anthony and Alon Albalak and Samuel Arcadinho and Stella Biderman and Huanqi Cao and Xin Cheng and Michael Chung and Matteo Grella and G Kranthikiran and Xuming He and Haowen Hou and Przemyslaw Kazienko and Jan Kocoń and Jiaming Kong and Bartlomiej Koptyra and Hayden Lau and Krishna Sri Ipsit Mantri and Ferdinand Mom and Atsushi Saito and Xiangru Tang and Bolun Wang and Johan Sokrates Wind and Stansilaw Wozniak and Ruichong Zhang and Zhenyuan Zhang and Qihang Zhao and Peng Zhou and Jian Zhu and Rui Zhu},
booktitle = {Conference on Empirical Methods in Natural Language Processing},
year = {2023},
url = {https://api.semanticscholar.org/CorpusID:258832459}
}
@inproceedings{Katharopoulos2020TransformersAR,
title = {Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention},
author = {Angelos Katharopoulos and Apoorv Vyas and Nikolaos Pappas and Franccois Fleuret},
booktitle = {International Conference on Machine Learning},
year = {2020},
url = {https://api.semanticscholar.org/CorpusID:220250819}
}
@misc{buckman2024,
author = {Buckman, Jacob and Gelada, Carles and Zhang, Sean},
publisher = {Manifest AI},
title = {Symmetric {Power} {Transformers}},
date = {2024-08-15},
langid = {en}
}
The greatest shortcoming of the human race is man’s inability to understand the exponential function. - Albert A. Bartlett
Metadata
Release files for taylor-series-linear-attention 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| taylor_series_linear_attention-0.2.0.tar.gz | 10.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| taylor_series_linear_attention-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.8 kB
Release files / taylor_series_linear_attention-0.2.0.tar.gz
| Download URL | taylor_series_linear_attention-0.2.0.tar.gz |
|---|---|
| Size | 10.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a4827b1f7f0e33c7ac4a89dea18bb9e03c997d2370050c2a001ff49537a6bd58
|
|
BLAKE2b-256 checksum How to use checksums |
6f993369232e11672f38faf21a661fd050e47c75a6a47623e22c99c3166c5e9c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.17
|
Release files / taylor_series_linear_attention-0.2.0-py3-none-any.whl
| Download URL | taylor_series_linear_attention-0.2.0-py3-none-any.whl |
|---|---|
| Size | 12.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fc07535f154ebdc14ab4b56a412473ad7e0d24d6767a7984b38e435f5cf51112
|
|
BLAKE2b-256 checksum How to use checksums |
df33beaf044b7c6bfcd3bc47ae3bac67b8d7b7f1e9cccda26ee85bd7e309920d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.17
|