← 开源
lucidrains

local-attention

An implementation of local windowed attention for language modeling

Model DevelopmentArchitecturePython
在 GitHub 打开
增长势头
+-124 小时新增 Star-0.2%
504
Star
52
Fork
+-1
本周
3
贡献者
创建于 2020-07-05 · 更新于 2026-10-16 · 今日第 19238 名
主要开发者
lucidrainsminhnh-falcongieoon

lucidrains/local-attention 排名第 19,238,有 504 个星标。

同类别比较

lucidrains/local-attention 排名第 19,238,此处有 8 个同类条目;PaddlePaddle/Paddle 排名第 19,247。

同类别比较
名称排名指标
GT-RIPL/Continual-Learning-Benchmark19,217
语言:
Python
星标数:
530
分叉数:
90
简介:
Evaluate three types of task shifting with popular continual learning algorithms.
gerstung-lab/Delphi19,219
语言:
Jupyter Notebook
星标数:
528
分叉数:
126
简介:
Modelling human health trajectories using generative transformers
mit-han-lab/tiny-training19,220
语言:
Python
星标数:
528
分叉数:
73
简介:
On-Device Training Under 256KB Memory [NeurIPS'22]
fkodom/fft-conv-pytorch19,225
语言:
Python
星标数:
524
分叉数:
61
简介:
Implementation of 1D, 2D, and 3D FFT convolutions in PyTorch. Much faster than direct convolutions for large kernel sizes.
PaddlePaddle/Paddle19,247
语言:
C++
星标数:
24,125
分叉数:
6,019
简介:
PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
openai/parameter-golf19,287
语言:
Python
星标数:
5,198
分叉数:
3,248
简介:
Train the smallest LM you can that fits in 16MB. Best model wins!
Cartucho/mAP19,301
语言:
Python
星标数:
2,969
分叉数:
916
简介:
mean Average Precision - This code evaluates the performance of your neural net for object recognition.
openai/weak-to-strong19,304
语言:
Python
星标数:
2,545
分叉数:
314
关键参数与规格
关键参数与规格
参数值
语言Python
类别Model Development
子类别Architecture
星标数504
分叉数52
贡献者3
创建日期2020-07-05
更新日期2026-10-16
1 天星标数-1
7 天星标数-1
排名19,238
上次排名17,968
排名变化-1,270
7 天星标排名18,073
上次 7 天星标排名16,546
7 天星标排名变化-1,527
总星标排名19,266
上次总星标排名19,201
总星标排名变化-65
国家USA
README 摘录

Local attention An implementation of local windowed attention, which sets an incredibly strong baseline for language modeling. It is becoming apparent that a transformer needs local attention in the bottom layers, with the top layers reserved for global attention to integrate the findings of previous layers. This repository makes it easy to immediately employ local window attention. This code has been battletested in multiple repositories already, alongside different implementations of sparse long-range attention. Install Usage This library also allows

for local attention in the setting of shared query/key space (Reformer architecture). The normalization of the keys, as well as the masking of tokens to itself, will be taken care of. If you wish for the module to automagically pad your query / key / values as well as the mask, simply set the keyword to Local Attention Transformer A full local attention transformer Enwik8 at 4096 window size of 256, lookback of 1, total receptive field of 512 Citation

常见问题

记录了多少星标和分叉?

lucidrains/local-attention 有 504 个星标和 52 个分叉。

仓库排名是多少?

lucidrains/local-attention 排名第 19,238。

记录的语言和类别是什么?

lucidrains/local-attention 有 504 个星标,语言为 Python,类别为 Model Development。

哪个仓库排名最接近?

lucidrains/local-attention 与 PaddlePaddle/Paddle 比较,后者排名第 19,247,有 24,125 个星标。

README

Local attention

An implementation of local windowed attention, which sets an incredibly strong baseline for language modeling. It is becoming apparent that a transformer needs local attention in the bottom layers, with the top layers reserved for global attention to integrate the findings of previous layers. This repository makes it easy to immediately employ local window attention.

This code has been battletested in multiple repositories already, alongside different implementations of sparse long-range attention.

Install

$ pip install local-attention

Usage

import torch
from local_attention import LocalAttention

q = torch.randn(2, 8, 2048, 64)
k = torch.randn(2, 8, 2048, 64)
v = torch.randn(2, 8, 2048, 64)

attn = LocalAttention(
    dim = 64,                # dimension of each head (you need to pass this in for relative positional encoding)
    window_size = 512,       # window size. 512 is optimal, but 256 or 128 yields good enough results
    causal = True,           # auto-regressive or not
    look_backward = 1,       # each window looks at the window before
    look_forward = 0,        # for non-auto-regressive case, will default to 1, so each window looks at the window before and after it
    dropout = 0.1,           # post-attention dropout
    exact_windowsize = False # if this is set to true, in the causal setting, each query will see at maximum the number of keys equal to the window size
)

mask = torch.ones(2, 2048).bool()
out = attn(q, k, v, mask = mask) # (2, 8, 2048, 64)

This library also allows for local attention in the setting of shared query/key space (Reformer architecture). The normalization of the keys, as well as the masking of tokens to itself, will be taken care of.

import torch
from local_attention import LocalAttention

qk = torch.randn(2, 8, 2048, 64)
v  = torch.randn(2, 8, 2048, 64)

attn = LocalAttention(
    dim = 64,
    window_size = 512,
    shared_qk = True,
    causal = True
)

mask = torch.ones(2, 2048).bool()
out = attn(qk, qk, v, mask = mask) # (2, 8, 2048, 64)

If you wish for the module to automagically pad your query / key / values as well as the mask, simply set the autopad keyword to True

import torch
from local_attention import LocalAttention

q = torch.randn(8, 2057, 64)
k = torch.randn(8, 2057, 64)
v = torch.randn(8, 2057, 64)

attn = LocalAttention(
    window_size = 512,
    causal = True,
    autopad = True      # auto pads both inputs and mask, then truncates output appropriately
)

mask = torch.ones(1, 2057).bool()
out = attn(q, k, v, mask = mask) # (8, 2057, 64)

Local Attention Transformer

A full local attention transformer

import torch
from local_attention import LocalTransformer

model = LocalTransformer(
    num_tokens = 256,
    dim = 512,
    depth = 6,
    max_seq_len = 8192,
    causal = True,
    local_attn_window_size = 256
).cuda()

x = torch.randint(0, 256, (1, 8192)).cuda()

logits = model(x) # (1, 8192, 256)

Enwik8 at 4096

window size of 256, lookback of 1, total receptive field of 512

$ python train.py

Citation

@inproceedings{rae-razavi-2020-transformers,
    title   = "Do Transformers Need Deep Long-Range Memory?",
    author  = "Rae, Jack  and Razavi, Ali",
    booktitle = "Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics",
    month   = jul,
    year    = "2020",
    address = "Online",
    publisher = "Association for Computational Linguistics",
    url     = "https://www.aclweb.org/anthology/2020.acl-main.672"
}
@misc{roy*2020efficient,
    title   = {Efficient Content-Based Sparse Attention with Routing Transformers},
    author  = {Aurko Roy* and Mohammad Taghi Saffar* and David Grangier and Ashish Vaswani},
    year    = {2020},
    url     = {https://arxiv.org/pdf/2003.05997.pdf}
}
@misc{beltagy2020longformer,
    title   = {Longformer: The Long-Document Transformer},
    author  = {Iz Beltagy and Matthew E. Peters and Arman Cohan},
    year    = {2020},
    eprint  = {2004.05150},
    archivePrefix = {arXiv},
    primaryClass = {cs.CL}
}
@inproceedings{Sun2022ALT,
    title     = {A Length-Extrapolatable Transformer},
    author    = {Yutao Sun and Li Dong and Barun Patra and Shuming Ma and Shaohan Huang and Alon Benhaim and Vishrav Chaudhary and Xia Song and Furu Wei},
    year      = {2022}
}
@article{Bondarenko2023QuantizableTR,
    title   = {Quantizable Transformers: Removing Outliers by Helping Attention Heads Do Nothing},
    author  = {Yelysei Bondarenko and Markus Nagel and Tijmen Blankevoort},
    journal = {ArXiv},
    year    = {2023},
    volume  = {abs/2306.12929},
    url     = {https://api.semanticscholar.org/CorpusID:259224568}
}
排名相邻
同类项目