File size: 38,353 Bytes
eafbe80 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | <div align="center">
# ๐ฅ Flash Linear Attention
[](https://huggingface.co/fla-hub) [](https://discord.gg/vDaJTmKNcS)
</div>
This repo aims at providing a collection of efficient Triton-based implementations for state-of-the-art linear attention models. **All implementations are written purely in PyTorch and Triton, making them platform-agnostic.** Currently verified platforms include NVIDIA, AMD, and Intel. **Any pull requests are welcome!**
<div align="center">
<img width="400" alt="image" src="https://github.com/fla-org/flash-linear-attention/assets/18402347/02ff2e26-1495-4088-b701-e72cd65ac6cf">
</div>
* [News](#news)
* [Models](#models)
* [Installation](#installation)
* [Usage](#usage)
* [Token Mixing](#token-mixing)
* [Fused Modules](#fused-modules)
* [Generation](#generation)
* [Hybrid Models](#hybrid-models)
* [Training](#training)
* [Evaluation](#evaluation)
* [Benchmarks](#benchmarks)
* [Citation](#citation)
* [Star History](#star-history)
* [Acknowledgements](#acknowledgements)
## News
- **$\texttt{[2025-10]}$:** ๐ Add Kimi Delta Attention implementation to `fla` ([paper](https://arxiv.org/abs/2510.26692)).
- **$\texttt{[2025-09]}$:** ๐ฒ Add DeltaFormer implementation to `fla` ([paper](https://arxiv.org/abs/2505.19488v1)).
- **$\texttt{[2025-09]}$:** ๐ป Thrilled to announce that [GDN](fla/ops/gated_delta_rule) has been integrated into Qwen3-Next. Check out their [blog post](https://qwen.ai/blog?id=4074cca80393150c248e508aa62983f9cb7d27cd&from=research.latest-advancements-list) for more infos!
- **$\texttt{[2025-08]}$:** ๐ฒ Add Log-Linear Attention implementation to `fla` ([paper](https://arxiv.org/abs/2506.04761)).
- **$\texttt{[2025-08]}$:** ๐ Add MoM implementation to `fla` ([paper](https://arxiv.org/abs/2502.13685)).
- **$\texttt{[2025-07]}$:** ๐ณ Add MLA implementation to `fla` ([paper](https://arxiv.org/abs/2405.04434)).
- **$\texttt{[2025-07]}$:** ๐ฃ๏ธ Added PaTH Attention to fla ([paper](https://arxiv.org/abs/2505.16381)).
- **$\texttt{[2025-06]}$:** ๐ Added MesaNet to fla ([paper](https://arxiv.org/abs/2506.05233)).
- **$\texttt{[2025-06]}$:** ๐ Add Comba implementation to `fla` ([paper](https://arxiv.org/abs/2506.02475)).
- **$\texttt{[2025-05]}$:** ๐ Add Rodimus* implementation to `fla` ([paper](https://arxiv.org/abs/2410.06577)).
- **$\texttt{[2025-04]}$:** ๐ Add DeltaProduct implementation to `fla` ([paper](https://arxiv.org/abs/2502.10297)).
- **$\texttt{[2025-04]}$:** ๐ Add FoX implementation to `fla` ([paper](https://arxiv.org/abs/2503.02130)).
- **$\texttt{[2025-03]}$:** ~~We have changed the default `initializer_range` to the magic ๐ณ 0.006~~ The `initializer_range` was rolled back to the default value of 0.02. For actual training, we recommend trying both.
- **$\texttt{[2025-02]}$:** ๐ณ Add NSA implementations to `fla`. See kernels [here](fla/ops/nsa).
- **$\texttt{[2025-01]}$:** ๐ฅ We are migrating to `torchtitan`-based training framework. Check out the [flame](https://github.com/fla-org/flame) repo for more details.
- **$\texttt{[2025-01]}$:** ๐ฆ
Add RWKV7 implementations (both kernels and models) to `fla`.
- **$\texttt{[2024-12]}$:** Integrated `flash-bidirectional-attention` to `fla-org` ([repo](https://github.com/fla-org/flash-bidirectional-linear-attention))
- **$\texttt{[2024-12]}$:** ๐ Add Gated DeltaNet implementation to `fla` ([paper](https://arxiv.org/abs/2412.06464)).
- **$\texttt{[2024-12]}$:** ๐ `fla` now officially supports kernels with variable-length inputs.
- **$\texttt{[2024-11]}$:** The inputs are now switched from head-first to seq-first format.
- **$\texttt{[2024-11]}$:** ๐ฅ `fla` now provides a flexible way for training hybrid models.
- **$\texttt{[2024-10]}$:** ๐ฅ Announcing `flame`, a minimal and scalable framework for training `fla` models. Check out the details [here](training/README.md).
- **$\texttt{[2024-09]}$:** `fla` now includes a fused linear and cross-entropy layer, significantly reducing memory usage during training.
- **$\texttt{[2024-09]}$:** ๐ Add GSA implementation to `fla` ([paper](https://arxiv.org/abs/2409.07146)).
- **$\texttt{[2024-05]}$:** ๐ Add DeltaNet implementation to `fla` ([paper](https://arxiv.org/abs/2102.11174)).
- **$\texttt{[2024-05]}$:** ๐ฅ `fla` v0.1: a variety of subquadratic kernels/layers/models integrated (RetNet/GLA/Mamba/HGRN/HGRN2/RWKV6, etc., see [Models](#models)).
- **$\texttt{[2023-12]}$:** ๐ฅ Launched `fla`, offering a collection of implementations for state-of-the-art linear attention models.
## Models
Roughly sorted according to the timeline supported in `fla`. The recommended training mode is `chunk` when available.
| Year | Venue | Model | Paper | Code | |
| :--- | :------ | :------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------: |
| 2023 | | RetNet | [Retentive network: a successor to transformer for large language models](https://arxiv.org/abs/2307.08621) | [official](https://github.com/microsoft/torchscale/tree/main) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/multiscale_retention.py) |
| 2024 | ICML | GLA | [Gated Linear Attention Transformers with Hardware-Efficient Training](https://arxiv.org/abs/2312.06635) | [official](https://github.com/berlino/gated_linear_attention) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/gla.py) |
| 2024 | ICML | Based | [Simple linear attention language models balance the recall-throughput tradeoff](https://arxiv.org/abs/2402.18668) | [official](https://github.com/HazyResearch/based) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/based.py) |
| 2024 | ACL | Rebased | [Linear Transformers with Learnable Kernel Functions are Better In-Context Models](https://arxiv.org/abs/2402.10644) | [official](https://github.com/corl-team/rebased/) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/rebased.py) |
| 2024 | NeurIPS | DeltaNet | [Parallelizing Linear Transformers with Delta Rule over Sequence Length](https://arxiv.org/abs/2406.06484) | [official](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/delta_net.py) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/delta_net.py) |
| 2022 | ACL | ABC | [ABC: Attention with Bounded-memory Control](https://arxiv.org/abs/2110.02488) | | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/abc.py) |
| 2023 | NeurIPS | HGRN | [Hierarchically Gated Recurrent Neural Network for Sequence Modeling](https://openreview.net/forum?id=P1TCHxJwLB) | [official](https://github.com/OpenNLPLab/HGRN) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/hgrn.py) |
| 2024 | COLM | HGRN2 | [HGRN2: Gated Linear RNNs with State Expansion](https://arxiv.org/abs/2404.07904) | [official](https://github.com/OpenNLPLab/HGRN2) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/hgrn2.py) |
| 2024 | COLM | RWKV6 | [Eagle and Finch: RWKV with Matrix-Valued States and Dynamic Recurrence](https://arxiv.org/abs/2404.05892) | [official](https://github.com/RWKV/RWKV-LM) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/rwkv6.py) |
| 2024 | | LightNet | [You Only Scan Once: Efficient Multi-dimension Sequential Modeling with LightNet](https://arxiv.org/abs/2405.21022) | [official](https://github.com/OpenNLPLab/LightNet) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/lightnet.py) |
| 2025 | ICLR | Samba | [Samba: Simple Hybrid State Space Models for Efficient Unlimited Context Language Modeling](https://arxiv.org/abs/2406.07522) | [official](https://github.com/microsoft/Samba) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/models/samba) |
| 2024 | ICML | Mamba2 | [Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality](https://arxiv.org/abs/2405.21060) | [official](https://github.com/state-spaces/mamba) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/models/mamba2) |
| 2024 | NeurIPS | GSA | [Gated Slot Attention for Efficient Linear-Time Sequence Modeling](https://arxiv.org/abs/2409.07146) | [official](https://github.com/fla-org/flash-linear-attention/tree/main/fla/models/gsa) | [fla](https://github.com/fla-org/flash-linear-attention/tree/main/fla/models/gsa) |
| 2025 | ICLR | Gated DeltaNet | [Gated Delta Networks: Improving Mamba2 with Delta Rule](https://arxiv.org/abs/2412.06464) | [official](https://github.com/NVlabs/GatedDeltaNet) | [fla](https://github.com/fla-org/flash-linear-attention/tree/main/fla/ops/gated_delta_rule) |
| 2025 | | RWKV7 | [RWKV-7 "Goose" with Expressive Dynamic State Evolution](https://arxiv.org/abs/2503.14456) | [official](https://github.com/BlinkDL/RWKV-LM/tree/main/RWKV-v7) | [fla](https://github.com/fla-org/flash-linear-attention/tree/main/fla/ops/rwkv7) |
| 2025 | | NSA | [Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention](https://arxiv.org/abs/2502.11089) | | [fla](https://github.com/fla-org/flash-linear-attention/tree/main/fla/ops/nsa) |
| 2025 | ICLR | FoX | [Forgetting Transformer: Softmax Attention with a Forget Gate](https://arxiv.org/abs/2503.02130) | [official](https://github.com/zhixuan-lin/forgetting-transformer) | [fla](https://github.com/fla-org/flash-linear-attention/tree/main/fla/ops/forgetting_attn) |
| 2025 | | DeltaProduct | [DeltaProduct: Improving State-Tracking in Linear RNNs via Householder Products](https://arxiv.org/abs/2502.10297) | | [fla](https://github.com/fla-org/flash-linear-attention/tree/main/fla/layers/gated_deltaproduct.py) |
| 2025 | ICLR | Rodimus* | [Rodimus*: Breaking the Accuracy-Efficiency Trade-Off with Efficient Attentions](https://arxiv.org/abs/2410.06577) | [official](https://github.com/codefuse-ai/rodimus) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/rodimus.py) |
| 2025 | | MesaNet | [MesaNet: Sequence Modeling by Locally Optimal Test-Time Training](https://arxiv.org/abs/2506.05233) | | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/mesa_net.py) |
| 2025 | | Comba | [Comba: Improving Bilinear RNNs with Closed-loop Control](https://arxiv.org/abs/2506.02475) | [official](https://github.com/AwesomeSeq/Comba-triton) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/comba.py) |
| 2025 | | PaTH | [PaTH Attention: Position Encoding via Accumulating Householder Transformations](https://arxiv.org/abs/2505.16381) | | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/path_attn.py) |
| 2025 | | MoM | [MoM: Linear Sequence Modeling with Mixture-of-Memories](https://arxiv.org/abs/2502.13685) | [official](https://github.com/OpenSparseLLMs/MoM) | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/mom.py) |
| 2025 | | Log-Linear Attention | [Log-Linear Attention](https://arxiv.org/abs/2506.04761) | [official](https://github.com/HanGuo97/log-linear-attention) | [fla](https://github.com/fla-org/flash-linear-attention/tree/main/fla/ops/log_linear_attn) |
| 2025 | | DeltaFormer | [Understanding Transformer from the Perspective of Associative Memory](https://arxiv.org/abs/2505.19488v1) | | [fla](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/deltaformer.py) |
| 2025 | | KDA | [Kimi Linear: An Expressive, Efficient Attention Architecture](https://arxiv.org/abs/2510.26692) | | [fla](https://github.com/fla-org/flash-linear-attention/tree/main/fla/ops/kda) |
## Installation
[](https://github.com/fla-org/flash-linear-attention/actions/workflows/nvidia-4090.yml) [](https://github.com/fla-org/flash-linear-attention/actions/workflows/nvidia-a100.yml) [](https://github.com/fla-org/flash-linear-attention/actions/workflows/nvidia-h100.yml) [](https://github.com/fla-org/flash-linear-attention/actions/workflows/intel-b580.yml)
The following requirements should be satisfied
- [PyTorch](https://pytorch.org/) >= 2.5
- [Triton](https://github.com/openai/triton) >=3.0 (or nightly version, see [FAQs](FAQs.md))
- [einops](https://einops.rocks/)
- [transformers](https://github.com/huggingface/transformers) >=4.45.0
- [datasets](https://github.com/huggingface/datasets) >=3.3.0
Starting from v0.3.2, the packages published on PyPI are `fla-core` and `flash-linear-attention`. The former contains all our customized kernels and only depends on PyTorch, Triton, and einops. The latter is an extension package of the former, containing `fla/layers` and `fla/models`, and depends on transformers. We also provide Triton implementations for conv1d operations, so causal-conv1d is not required.
You can install `fla` with pip:
```sh
pip install flash-linear-attention
```
As `fla` is actively developed now, for the latest features and updates, an alternative way is to install the package from source. Note that installing from git uses the default mode, so you need to uninstall both `fla-core` and `flash-linear-attention` first:
```sh
# uninstall both packages first to ensure a successful upgrade
pip uninstall fla-core flash-linear-attention -y && pip install -U git+https://github.com/fla-org/flash-linear-attention
```
or manage `fla` with submodules
```sh
git submodule add https://github.com/fla-org/flash-linear-attention.git 3rdparty/flash-linear-attention
ln -s 3rdparty/flash-linear-attention/fla fla
```
If you have installed `triton-nightly` and `torch` pre version, please use the following command:
```sh
pip install einops ninja datasets transformers numpy
# uninstall both packages first to ensure a successful upgrade
pip uninstall fla-core flash-linear-attention -y && pip install -U --no-use-pep517 git+https://github.com/fla-org/flash-linear-attention --no-deps
```
## Usage
### Token Mixing
We provide ``token mixing'' linear attention layers in `fla.layers` for you to use.
You can replace the standard multihead attention layer in your model with other linear attention layers.
Example usage is as follows:
```py
>>> import torch
>>> from fla.layers import MultiScaleRetention
>>> batch_size, num_heads, seq_len, hidden_size = 32, 4, 2048, 1024
>>> device, dtype = 'cuda:0', torch.bfloat16
>>> retnet = MultiScaleRetention(hidden_size=hidden_size, num_heads=num_heads).to(device=device, dtype=dtype)
>>> retnet
MultiScaleRetention(
(q_proj): Linear(in_features=1024, out_features=1024, bias=False)
(k_proj): Linear(in_features=1024, out_features=1024, bias=False)
(v_proj): Linear(in_features=1024, out_features=2048, bias=False)
(g_proj): Linear(in_features=1024, out_features=2048, bias=False)
(o_proj): Linear(in_features=2048, out_features=1024, bias=False)
(g_norm_swish_gate): FusedRMSNormGated(512, eps=1e-05, activation=swish)
(rotary): RotaryEmbedding(dim=256, base=10000.0, interleaved=False, pos_idx_in_fp32=True)
)
>>> x = torch.randn(batch_size, seq_len, hidden_size).to(device=device, dtype=dtype)
>>> y, *_ = retnet(x)
>>> y.shape
torch.Size([32, 2048, 1024])
```
We provide the implementations of models that are compatible with ๐ค Transformers library.
Here's an example of how to initialize a GLA model from the default configs in `fla`:
```py
>>> from fla.models import GLAConfig
>>> from transformers import AutoModelForCausalLM
>>> config = GLAConfig()
>>> config
GLAConfig {
"attn": null,
"attn_mode": "chunk",
"bos_token_id": 1,
"clamp_min": null,
"conv_size": 4,
"elementwise_affine": true,
"eos_token_id": 2,
"expand_k": 0.5,
"expand_v": 1,
"feature_map": null,
"fuse_cross_entropy": true,
"fuse_norm": true,
"fuse_swiglu": true,
"hidden_act": "swish",
"hidden_ratio": 4,
"hidden_size": 2048,
"initializer_range": 0.006,
"intermediate_size": null,
"max_position_embeddings": 2048,
"model_type": "gla",
"norm_eps": 1e-06,
"num_heads": 4,
"num_hidden_layers": 24,
"num_kv_heads": null,
"tie_word_embeddings": false,
"transformers_version": "4.50.1",
"use_cache": true,
"use_gk": true,
"use_gv": false,
"use_output_gate": true,
"use_short_conv": false,
"vocab_size": 32000
}
>>> AutoModelForCausalLM.from_config(config)
GLAForCausalLM(
(model): GLAModel(
(embeddings): Embedding(32000, 2048)
(layers): ModuleList(
(0-23): 24 x GLABlock(
(attn_norm): RMSNorm(2048, eps=1e-06)
(attn): GatedLinearAttention(
(q_proj): Linear(in_features=2048, out_features=1024, bias=False)
(k_proj): Linear(in_features=2048, out_features=1024, bias=False)
(v_proj): Linear(in_features=2048, out_features=2048, bias=False)
(g_proj): Linear(in_features=2048, out_features=2048, bias=False)
(gk_proj): Sequential(
(0): Linear(in_features=2048, out_features=16, bias=False)
(1): Linear(in_features=16, out_features=1024, bias=True)
)
(o_proj): Linear(in_features=2048, out_features=2048, bias=False)
(g_norm_swish_gate): FusedRMSNormGated(512, eps=1e-06, activation=swish)
)
(mlp_norm): RMSNorm(2048, eps=1e-06)
(mlp): GatedMLP(
(gate_proj): Linear(in_features=2048, out_features=5632, bias=False)
(up_proj): Linear(in_features=2048, out_features=5632, bias=False)
(down_proj): Linear(in_features=5632, out_features=2048, bias=False)
(swiglu_linear): SwiGLULinear()
)
)
)
(norm): RMSNorm(2048, eps=1e-06)
)
(lm_head): Linear(in_features=2048, out_features=32000, bias=False)
)
```
### Fused Modules
We offer a collection of fused modules in `fla.modules` to facilitate faster training:
* [`Rotary Embedding`](fla/modules/rotary.py): rotary positional embeddings as adopted by the Llama architecture, a.k.a., Transformer++.
* [`Norm Layers`](fla/modules/layernorm.py):
* `RMSNorm`, `LayerNorm` and `GroupNorm`
* `RMSNormLinear`, `LayerNormLinear` and `GroupNormLinear` to reduce memory usage of intermediate tensors for improved memory efficiency.
* [`Norm Layers with Gating`](fla/modules/fused_norm_gate.py): combine norm layers with element-wise sigmoid or swish gating, as used by RetNet/GLA.
* [`Cross Entropy`](fla/modules/fused_cross_entropy.py): faster Triton implementation of cross entropy loss.
* [`Linear Cross Entropy`](fla/modules/fused_linear_cross_entropy.py): fused linear layer and cross entropy loss to avoid the materialization of large logits tensors. Also refer to implementations by [mgmalek](https://github.com/mgmalek/efficient_cross_entropy) and [Liger-Kernel](https://github.com/linkedin/Liger-Kernel/blob/main/src/liger_kernel/ops/fused_linear_cross_entropy.py).
* [`Linear KL Divergence`](fla/modules/fused_kl_div.py): fused linear layer and KL divergence loss in a similar vein as CE loss.
> [!IMPORTANT]
> You can control using `fuse_linear_cross_entropy` in the model configuration to enable/disable the fused linear cross entropy loss.
>
> This fused implementation is more memory-efficient but may reduce numerical precision. Due to this trade-off, it is disabled by default.
> If you enable this feature and encounter training instability (e.g., loss divergence), we recommend disabling it to see if the issue is resolved.
### Generation
Upon successfully pretraining a model, it becomes accessible for generating text using the ๐ค text generation APIs.
In the following, we give a generation example:
```py
>>> import fla
>>> from transformers import AutoModelForCausalLM, AutoTokenizer
>>> name = 'fla-hub/gla-1.3B-100B'
>>> tokenizer = AutoTokenizer.from_pretrained(name)
>>> model = AutoModelForCausalLM.from_pretrained(name).cuda()
>>> input_prompt = "Power goes with permanence. Impermanence is impotence. And rotation is castration."
>>> input_ids = tokenizer(input_prompt, return_tensors="pt").input_ids.cuda()
>>> outputs = model.generate(input_ids, max_length=64)
>>> tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
```
We also provide a simple script [here](benchmarks/benchmark_generation.py) for benchmarking the generation speed.
Simply run it by:
```sh
$ python -m benchmarks.benchmark_generation \
--path 'fla-hub/gla-1.3B-100B' \
--repetition_penalty 2. \
--prompt="Hello everyone, I'm Songlin Yang"
Prompt:
Hello everyone, I'm Songlin Yang
Generated:
Hello everyone, I'm Songlin Yang.
I am a 20 year old girl from China who is currently studying in the United States of America for my Master degree and also working as an English teacher at school here on campus since last summer (1st semester). My main goal to be able do well with this course so that we can have
Prompt length: 10, generation length: 64
Total prompt processing + decoding time: 4593ms
```
All of the pretrained models currently available can be found in [`fla-hub`](https://huggingface.co/fla-hub).
```py
>>> from huggingface_hub import list_models
>>> for model in list_models(author='fla-hub'): print(model.id)
```
### Hybrid Models
`fla` provides a flexible method to incorporate standard attention layers into existing linear attention models.
This is easily achieved by specifying the `attn` argument in the model configuration.
For example, to create a 2-layer Samba model with interleaved Mamba and local attention layers, using a sliding window size of 2048:
```py
>>> from fla.models import SambaConfig
>>> from transformers import AutoModelForCausalLM
>>> config = SambaConfig(num_hidden_layers=2)
>>> config.attn = {
'layers': [1],
'num_heads': 18,
'num_kv_heads': 18,
'qkv_bias': False,
'rope_theta': 10000.,
'window_size': 2048
}
>>> config
SambaConfig {
"attn": {
"layers": [
1
],
"num_heads": 18,
"num_kv_heads": 18,
"qkv_bias": false,
"rope_theta": 10000.0,
"window_size": 2048
},
"bos_token_id": 1,
"conv_kernel": 4,
"eos_token_id": 2,
"expand": 2,
"fuse_cross_entropy": true,
"fuse_norm": true,
"fuse_swiglu": true,
"hidden_act": "swish",
"hidden_ratio": 4,
"hidden_size": 2304,
"initializer_range": 0.02,
"intermediate_size": 4608,
"max_position_embeddings": 2048,
"model_type": "samba",
"norm_eps": 1e-05,
"num_hidden_layers": 2,
"pad_token_id": 0,
"rescale_prenorm_residual": false,
"residual_in_fp32": false,
"state_size": 16,
"tie_word_embeddings": false,
"time_step_floor": 0.0001,
"time_step_init_scheme": "random",
"time_step_max": 0.1,
"time_step_min": 0.001,
"time_step_rank": 144,
"time_step_scale": 1.0,
"transformers_version": "4.50.1",
"use_bias": false,
"use_cache": true,
"use_conv_bias": true,
"vocab_size": 32000
}
>>> AutoModelForCausalLM.from_config(config)
SambaForCausalLM(
(backbone): SambaModel(
(embeddings): Embedding(32000, 2304)
(layers): ModuleList(
(0): SambaBlock(
(mixer_norm): RMSNorm(2304, eps=1e-05)
(mixer): Mamba(
(conv1d): Conv1d(4608, 4608, kernel_size=(4,), stride=(1,), padding=(3,), groups=4608)
(in_proj): Linear(in_features=2304, out_features=9216, bias=False)
(x_proj): Linear(in_features=4608, out_features=176, bias=False)
(dt_proj): Linear(in_features=144, out_features=4608, bias=True)
(out_proj): Linear(in_features=4608, out_features=2304, bias=False)
)
(mlp_norm): RMSNorm(2304, eps=1e-05)
(mlp): GatedMLP(
(gate_proj): Linear(in_features=2304, out_features=6144, bias=False)
(up_proj): Linear(in_features=2304, out_features=6144, bias=False)
(down_proj): Linear(in_features=6144, out_features=2304, bias=False)
(swiglu_linear): SwiGLULinear()
)
)
(1): SambaBlock(
(mixer_norm): RMSNorm(2304, eps=1e-05)
(mixer): Attention(
(q_proj): Linear(in_features=2304, out_features=2304, bias=False)
(k_proj): Linear(in_features=2304, out_features=2304, bias=False)
(v_proj): Linear(in_features=2304, out_features=2304, bias=False)
(o_proj): Linear(in_features=2304, out_features=2304, bias=False)
(rotary): RotaryEmbedding(dim=128, base=10000.0, interleaved=False, pos_idx_in_fp32=True)
)
(mlp_norm): RMSNorm(2304, eps=1e-05)
(mlp): GatedMLP(
(gate_proj): Linear(in_features=2304, out_features=6144, bias=False)
(up_proj): Linear(in_features=2304, out_features=6144, bias=False)
(down_proj): Linear(in_features=6144, out_features=2304, bias=False)
(swiglu_linear): SwiGLULinear()
)
)
)
(norm_f): RMSNorm(2304, eps=1e-05)
)
(lm_head): Linear(in_features=2304, out_features=32000, bias=False)
)
```
During inference, you **DO NOT** need to revise anything for generation!
The model will produce output as-is, without any need for additional configurations or modifications.
## Training
We provide a minimal framework called [๐ฅ `flame`](https://github.com/fla-org/flame) built on top of `torchtitan`, for efficient training of `fla` models.
Checkout [the GLA example](https://github.com/fla-org/flash-linear-attention/blob/main/examples/training.md) for more details.
## Evaluation
The [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness) library allows you to easily perform (zero-shot) model evaluations.
Follow the steps below to use this library:
1. Install `lm_eval` following [their instructions](https://github.com/EleutherAI/lm-evaluation-harness/blob/main/README.md).
2. Run evaluation with:
```sh
$ MODEL='fla-hub/gla-1.3B-100B'
$ python -m evals.harness --model hf \
--model_args pretrained=$MODEL,dtype=bfloat16 \
--tasks wikitext,lambada_openai,piqa,hellaswag,winogrande,arc_easy,arc_challenge,boolq,sciq,copa,openbookqa \
--batch_size 64 \
--num_fewshot 0 \
--device cuda \
--show_config
```
We've made `fla` compatible with hf-style evaluations, you can call [evals.harness](evals/harness.py) to finish the evaluations.
Running the command above will provide the task results reported in the GLA paper.
3. Multi-GPU Evaluation with Hugging Face accelerate ๐
To perform data-parallel evaluation (where each GPU loads a separate full copy of the model), we leverage the accelerate launcher as follows:
```sh
$ MODEL='fla-hub/gla-1.3B-100B'
$ accelerate launch -m evals.harness --model hf \
--model_args pretrained=$MODEL,dtype=bfloat16,trust_remote_code=True \
--tasks wikitext,lambada_openai,piqa,hellaswag,winogrande,arc_easy,arc_challenge,boolq,sciq,copa,openbookqa \
--batch_size 64 \
--num_fewshot 0 \
--device cuda \
--show_config \
--trust_remote_code
```
4. ๐ RULER Benchmark suite
The RULER benchmarks are commonly used for evaluating model performance on long-context tasks.
You can evaluate `fla` models on RULER directly using `lm-evaluation-harness`. RULER is only available in a relatively recent version of `lm-evaluation-harness`, so make sure you have the latest version installed.
```
git clone --depth 1 https://github.com/EleutherAI/lm-evaluation-harness
cd lm-evaluation-harness
pip install -e .
```
Then, install the necessary dependencies for RULER:
```sh
pip install lm_eval["ruler"]
```
and run evaluation by (e.g., 32k contexts):
```sh
$ accelerate launch -m evals.harness \
--output_path $OUTPUT \
--tasks niah_single_1,niah_single_2,niah_single_3,niah_multikey_1,niah_multikey_2,niah_multikey_3,niah_multiquery,niah_multivalue,ruler_vt,ruler_cwe,ruler_fwe,ruler_qa_hotpot,ruler_qa_squad \
--model_args pretrained=$MODEL,dtype=bfloat16,max_length=32768,trust_remote_code=True \
--metadata='{"max_seq_lengths":[4096,8192,16384,32768]}' \
--batch_size 2 \
--show_config \
--trust_remote_code
```
If a GPU can't load a full copy of the model, please refer to [this link](https://github.com/EleutherAI/lm-evaluation-harness?tab=readme-ov-file#multi-gpu-evaluation-with-hugging-face-accelerate) for FSDP settings.
> [!Tip]
> If you are using `lm-evaluation-harness` as an external library and can't find (almost) any tasks available, before calling `lm_eval.evaluate()` or `lm_eval.simple_evaluate()`, simply run the following to load the library's stock tasks!
```py
>>> from lm_eval.tasks import TaskManager; TaskManager().initialize_tasks()
```
## Benchmarks
We compared our Triton-based RetNet implementation with CUDA-based FlashAttention2, using a batch size of 8, 32 heads, and a head dimension of 128, across different sequence lengths.
These tests were conducted on a single H100 80GB GPU, as illustrated in the following graph
```py
# you might have to first install `fla` to enable its import via `pip install -e .`
$ python benchmark_retention.py
Performance:
T chunk_fwd parallel_fwd flash_fwd chunk_fwdbwd parallel_fwdbwd flash_fwdbwd
0 128.0 0.264032 0.243536 0.083488 1.301856 1.166784 0.320704
1 256.0 0.273472 0.252848 0.094304 1.345872 1.300608 0.807936
2 512.0 0.303600 0.278896 0.098112 1.503168 1.433184 0.857216
3 1024.0 0.357248 0.367360 0.156528 1.773552 2.303424 1.160864
4 2048.0 0.454624 0.605616 0.340928 2.283728 4.483360 1.955936
5 4096.0 0.638960 1.378016 1.004992 3.374720 12.271215 4.813776
6 8192.0 1.012352 4.201344 3.625008 5.581808 40.833618 15.023697
7 16384.0 1.748512 14.489664 13.710080 10.191552 153.093765 54.336864
```
<div align="center">
<img width="500" alt="image" src="https://github.com/user-attachments/assets/c2607015-63af-43d1-90d1-ad5fe1670a03">
</div>
## Citation
If you find this repository helpful, please cite our work:
```bib
@software{yang2024fla,
title = {FLA: A Triton-Based Library for Hardware-Efficient Implementations of Linear Attention Mechanism},
author = {Yang, Songlin and Zhang, Yu},
url = {https://github.com/fla-org/flash-linear-attention},
month = jan,
year = {2024}
}
@misc{zhang2025kda,
title = {Kimi Linear: An Expressive, Efficient Attention Architecture},
author = {Zhang, Yu and Lin, Zongyu and Yao, Xingcheng and Hu, Jiaxi and Meng, Fanqing and Liu, Chengyin and Men, Xin and Yang, Songlin and Li, Zhiyuan and Li, Wentao and Lu, Enzhe and Liu, Weizhou and Chen, Yanru and Xu, Weixin and Yu, Longhui and Wang, Yejie and Fan, Yu and Zhong, Longguang and Yuan, Enming and Zhang, Dehao and Zhang, Yizhi and T. Liu, Y. and Wang, Haiming and Fang, Shengjun and He, Weiran and Liu, Shaowei and Li, Yiwei and Su, Jianlin and Qiu, Jiezhong and Pang, Bo and Yan, Junjie and Jiang, Zhejun and Huang, Weixiao and Yin, Bohong and You, Jiacheng and Wei, Chu and Wang, Zhengtao and Hong, Chao and Chen, Yutian and Chen, Guanduo and Wang, Yucheng and Zheng, Huabin and Wang, Feng and Liu, Yibo and Dong, Mengnan and Zhang, Zheng and Pan, Siyuan and Wu, Wenhao and Wu, Yuhao and Guan, Longyu and Tao, Jiawen and Fu, Guohong and Xu, Xinran and Wang, Yuzhi and Lai, Guokun and Wu, Yuxin and Zhou, Xinyu and Yang, Zhilin and Du, Yulun},
year = {2025},
eprint = {2510.26692},
archivePrefix = {arXiv},
primaryClass = {cs.CL}
}
@inproceedings{yang2025path,
title = {PaTH Attention: Position Encoding via Accumulating Householder Transformations},
author = {Yang, Songlin and Shen, Yikang and Wen, Kaiyue and Tan, Shawn and Mishra, Mayank and Ren, Liliang and Panda, Rameswar and Kim, Yoon},
booktitle = {Proceedings of NeurIPS},
year = {2025}
}
@inproceedings{yang2024gdn,
title = {Gated Delta Networks: Improving Mamba2 with Delta Rule},
author = {Yang, Songlin and Kautz, Jan and Hatamizadeh, Ali},
booktitle = {Proceedings of ICLR},
year = {2025}
}
@inproceedings{yang2024deltanet,
title = {Parallelizing Linear Transformers with the Delta Rule over Sequence Length},
author = {Yang, Songlin and Wang, Bailin and Zhang, Yu and Shen, Yikang and Kim, Yoon},
booktitle = {Proceedings of NeurIPS},
year = {2024}
}
@inproceedings{zhang2024gsa,
title = {Gated Slot Attention for Efficient Linear-Time Sequence Modeling},
author = {Zhang, Yu and Yang, Songlin and Zhu, Ruijie and Zhang, Yue and Cui, Leyang and Wang, Yiqiao and Wang, Bolun and Shi, Freda and Wang, Bailin and Bi, Wei and Zhou, Peng and Fu, Guohong},
booktitle = {Proceedings of NeurIPS},
year = {2024}
}
@inproceedings{qin2024hgrn2,
title = {HGRN2: Gated Linear RNNs with State Expansion},
author = {Qin, Zhen and Yang, Songlin and Sun, Weixuan and Shen, Xuyang and Li, Dong and Sun, Weigao and Zhong, Yiran},
booktitle = {Proceedings of COLM},
year = {2024}
}
@inproceedings{yang2024gla,
title = {Gated Linear Attention Transformers with Hardware-Efficient Training},
author = {Yang, Songlin and Wang, Bailin and Shen, Yikang and Panda, Rameswar and Kim, Yoon},
booktitle = {Proceedings of ICML},
year = {2024}
}
```
## Star History
[](https://github.com/fla-org/flash-linear-attention/stargazers)
[](https://star-history.com/#fla-org/flash-linear-attention&Date)
## Acknowledgements
We extend our gratitude to [Bitdeer](https://www.bitdeer.com/) and [Moonshot AI](https://www.moonshot.ai/) for their support in maintaining and powering our project infrastructure.
|