fla-core 0.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fla/__init__.py +4 -0
- fla/modules/__init__.py +33 -0
- fla/modules/activations.py +548 -0
- fla/modules/convolution.py +1143 -0
- fla/modules/feature_map.py +300 -0
- fla/modules/fused_bitlinear.py +632 -0
- fla/modules/fused_cross_entropy.py +419 -0
- fla/modules/fused_kl_div.py +324 -0
- fla/modules/fused_linear_cross_entropy.py +620 -0
- fla/modules/fused_norm_gate.py +1253 -0
- fla/modules/grpo.py +411 -0
- fla/modules/l2norm.py +285 -0
- fla/modules/l2warp.py +38 -0
- fla/modules/layernorm.py +1433 -0
- fla/modules/layernorm_gated.py +528 -0
- fla/modules/mlp.py +132 -0
- fla/modules/parallel.py +42 -0
- fla/modules/rotary.py +500 -0
- fla/modules/token_shift.py +543 -0
- fla/ops/__init__.py +53 -0
- fla/ops/abc/__init__.py +7 -0
- fla/ops/abc/chunk.py +1117 -0
- fla/ops/abc/naive.py +96 -0
- fla/ops/attn/__init__.py +7 -0
- fla/ops/attn/decoding.py +182 -0
- fla/ops/attn/parallel.py +730 -0
- fla/ops/based/__init__.py +9 -0
- fla/ops/based/fused_chunk.py +373 -0
- fla/ops/based/naive.py +72 -0
- fla/ops/based/parallel.py +408 -0
- fla/ops/comba/__init__.py +7 -0
- fla/ops/comba/chunk.py +342 -0
- fla/ops/comba/fused_recurrent.py +332 -0
- fla/ops/comba/utils.py +172 -0
- fla/ops/comba/wy_fast.py +423 -0
- fla/ops/common/__init__.py +1 -0
- fla/ops/common/chunk_delta_h.py +527 -0
- fla/ops/common/chunk_h.py +384 -0
- fla/ops/common/chunk_h_parallel.py +551 -0
- fla/ops/common/chunk_h_split.py +596 -0
- fla/ops/common/chunk_o.py +681 -0
- fla/ops/common/chunk_scaled_dot_kkt.py +305 -0
- fla/ops/common/fused_chunk.py +629 -0
- fla/ops/common/fused_recurrent.py +567 -0
- fla/ops/delta_rule/__init__.py +11 -0
- fla/ops/delta_rule/chunk.py +311 -0
- fla/ops/delta_rule/fused_chunk.py +6 -0
- fla/ops/delta_rule/fused_recurrent.py +535 -0
- fla/ops/delta_rule/naive.py +120 -0
- fla/ops/delta_rule/parallel.py +403 -0
- fla/ops/delta_rule/wy_fast.py +294 -0
- fla/ops/forgetting_attn/__init__.py +7 -0
- fla/ops/forgetting_attn/parallel.py +64 -0
- fla/ops/gated_delta_product/__init__.py +5 -0
- fla/ops/gated_delta_product/chunk.py +305 -0
- fla/ops/gated_delta_product/chunk_deltaproduct_h.py +502 -0
- fla/ops/gated_delta_product/chunk_deltaproduct_o.py +154 -0
- fla/ops/gated_delta_product/chunk_ref.py +68 -0
- fla/ops/gated_delta_product/naive.py +36 -0
- fla/ops/gated_delta_rule/__init__.py +7 -0
- fla/ops/gated_delta_rule/chunk.py +340 -0
- fla/ops/gated_delta_rule/fused_recurrent.py +351 -0
- fla/ops/gated_delta_rule/wy_fast.py +296 -0
- fla/ops/generalized_delta_rule/__init__.py +9 -0
- fla/ops/generalized_delta_rule/dplr/__init__.py +7 -0
- fla/ops/generalized_delta_rule/dplr/chunk.py +362 -0
- fla/ops/generalized_delta_rule/dplr/chunk_A_bwd.py +367 -0
- fla/ops/generalized_delta_rule/dplr/chunk_A_fwd.py +198 -0
- fla/ops/generalized_delta_rule/dplr/chunk_h_bwd.py +175 -0
- fla/ops/generalized_delta_rule/dplr/chunk_h_fwd.py +175 -0
- fla/ops/generalized_delta_rule/dplr/chunk_o_bwd.py +430 -0
- fla/ops/generalized_delta_rule/dplr/chunk_o_fwd.py +125 -0
- fla/ops/generalized_delta_rule/dplr/fused_recurrent.py +268 -0
- fla/ops/generalized_delta_rule/dplr/naive.py +96 -0
- fla/ops/generalized_delta_rule/dplr/wy_fast_bwd.py +164 -0
- fla/ops/generalized_delta_rule/dplr/wy_fast_fwd.py +284 -0
- fla/ops/generalized_delta_rule/iplr/__init__.py +7 -0
- fla/ops/generalized_delta_rule/iplr/chunk.py +492 -0
- fla/ops/generalized_delta_rule/iplr/fused_recurrent.py +452 -0
- fla/ops/generalized_delta_rule/iplr/naive.py +69 -0
- fla/ops/generalized_delta_rule/iplr/wy_fast.py +300 -0
- fla/ops/gla/__init__.py +11 -0
- fla/ops/gla/chunk.py +1298 -0
- fla/ops/gla/fused_chunk.py +642 -0
- fla/ops/gla/fused_recurrent.py +111 -0
- fla/ops/gla/naive.py +41 -0
- fla/ops/gsa/__init__.py +9 -0
- fla/ops/gsa/chunk.py +1133 -0
- fla/ops/gsa/fused_recurrent.py +536 -0
- fla/ops/gsa/naive.py +69 -0
- fla/ops/hgrn/__init__.py +9 -0
- fla/ops/hgrn/chunk.py +282 -0
- fla/ops/hgrn/fused_recurrent.py +308 -0
- fla/ops/hgrn/naive.py +63 -0
- fla/ops/lightning_attn/__init__.py +9 -0
- fla/ops/lightning_attn/chunk.py +84 -0
- fla/ops/lightning_attn/fused_recurrent.py +84 -0
- fla/ops/linear_attn/__init__.py +11 -0
- fla/ops/linear_attn/chunk.py +76 -0
- fla/ops/linear_attn/fused_chunk.py +61 -0
- fla/ops/linear_attn/fused_recurrent.py +35 -0
- fla/ops/linear_attn/naive.py +64 -0
- fla/ops/linear_attn/utils.py +10 -0
- fla/ops/log_linear_attn/__init__.py +7 -0
- fla/ops/log_linear_attn/chunk.py +1904 -0
- fla/ops/log_linear_attn/naive.py +50 -0
- fla/ops/mesa_net/__init__.py +5 -0
- fla/ops/mesa_net/chunk.py +371 -0
- fla/ops/mesa_net/chunk_cg_solver_bwd.py +162 -0
- fla/ops/mesa_net/chunk_cg_solver_fwd.py +186 -0
- fla/ops/mesa_net/chunk_h_fwd.py +170 -0
- fla/ops/mesa_net/chunk_h_kk_intra_bwd.py +191 -0
- fla/ops/mesa_net/chunk_h_kv_intra_bwd.py +209 -0
- fla/ops/mesa_net/chunk_h_kv_intra_bwd_separate.py +300 -0
- fla/ops/mesa_net/decoding_one_step.py +175 -0
- fla/ops/mesa_net/naive.py +131 -0
- fla/ops/nsa/__init__.py +9 -0
- fla/ops/nsa/compression.py +536 -0
- fla/ops/nsa/naive.py +103 -0
- fla/ops/nsa/parallel.py +879 -0
- fla/ops/nsa/utils.py +92 -0
- fla/ops/path_attn/__init__.py +7 -0
- fla/ops/path_attn/cumprod_householder_bwd.py +140 -0
- fla/ops/path_attn/cumprod_householder_fwd.py +119 -0
- fla/ops/path_attn/intra_chunk_preprocess_bwd.py +140 -0
- fla/ops/path_attn/intra_chunk_preprocess_bwd_prepare.py +208 -0
- fla/ops/path_attn/intra_chunk_preprocess_fwd.py +168 -0
- fla/ops/path_attn/parallel.py +270 -0
- fla/ops/path_attn/parallel_path_bwd_inter_dkv.py +189 -0
- fla/ops/path_attn/parallel_path_bwd_inter_dqh.py +200 -0
- fla/ops/path_attn/parallel_path_bwd_intra.py +172 -0
- fla/ops/path_attn/parallel_path_fwd.py +195 -0
- fla/ops/path_attn/prepare_k_cache.py +74 -0
- fla/ops/path_attn/transform_q.py +108 -0
- fla/ops/rebased/__init__.py +7 -0
- fla/ops/rebased/naive.py +27 -0
- fla/ops/rebased/parallel.py +464 -0
- fla/ops/retention/__init__.py +13 -0
- fla/ops/retention/chunk.py +77 -0
- fla/ops/retention/fused_chunk.py +78 -0
- fla/ops/retention/fused_recurrent.py +33 -0
- fla/ops/retention/naive.py +15 -0
- fla/ops/retention/parallel.py +71 -0
- fla/ops/rwkv4/__init__.py +7 -0
- fla/ops/rwkv4/fused_recurrent.py +472 -0
- fla/ops/rwkv6/__init__.py +9 -0
- fla/ops/rwkv6/chunk.py +1300 -0
- fla/ops/rwkv6/chunk_naive.py +43 -0
- fla/ops/rwkv6/fused_recurrent.py +673 -0
- fla/ops/rwkv6/recurrent_naive.py +103 -0
- fla/ops/rwkv7/__init__.py +10 -0
- fla/ops/rwkv7/channel_mixing.py +323 -0
- fla/ops/rwkv7/chunk.py +78 -0
- fla/ops/rwkv7/fused_addcmul.py +293 -0
- fla/ops/rwkv7/fused_k_update.py +346 -0
- fla/ops/rwkv7/fused_recurrent.py +334 -0
- fla/ops/rwkv7/gate_output_correction.py +244 -0
- fla/ops/simple_gla/__init__.py +13 -0
- fla/ops/simple_gla/chunk.py +303 -0
- fla/ops/simple_gla/fused_chunk.py +108 -0
- fla/ops/simple_gla/fused_recurrent.py +112 -0
- fla/ops/simple_gla/naive.py +114 -0
- fla/ops/simple_gla/parallel.py +722 -0
- fla/ops/titans/__init__.py +7 -0
- fla/ops/titans/log_impl.py +153 -0
- fla/ops/titans/naive.py +375 -0
- fla/ops/ttt/__init__.py +9 -0
- fla/ops/ttt/chunk.py +1441 -0
- fla/ops/ttt/fused_chunk.py +831 -0
- fla/ops/ttt/naive.py +126 -0
- fla/ops/utils/__init__.py +58 -0
- fla/ops/utils/constant.py +6 -0
- fla/ops/utils/cumsum.py +465 -0
- fla/ops/utils/index.py +133 -0
- fla/ops/utils/logcumsumexp.py +52 -0
- fla/ops/utils/logsumexp.py +80 -0
- fla/ops/utils/matmul.py +245 -0
- fla/ops/utils/op.py +62 -0
- fla/ops/utils/pack.py +208 -0
- fla/ops/utils/pooling.py +207 -0
- fla/ops/utils/softmax.py +106 -0
- fla/ops/utils/solve_tril.py +381 -0
- fla/utils.py +462 -0
- fla_core-0.3.2.dist-info/METADATA +575 -0
- fla_core-0.3.2.dist-info/RECORD +188 -0
- fla_core-0.3.2.dist-info/WHEEL +5 -0
- fla_core-0.3.2.dist-info/licenses/LICENSE +21 -0
- fla_core-0.3.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
import warnings
|
|
5
|
+
from typing import Optional, Tuple
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
from fla.ops.simple_gla.chunk import chunk_simple_gla
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@torch.compiler.disable
|
|
13
|
+
def chunk_lightning_attn(
|
|
14
|
+
q: torch.Tensor,
|
|
15
|
+
k: torch.Tensor,
|
|
16
|
+
v: torch.Tensor,
|
|
17
|
+
layer_idx: int,
|
|
18
|
+
num_layers: int,
|
|
19
|
+
scale: Optional[float] = None,
|
|
20
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
21
|
+
output_final_state: bool = False,
|
|
22
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
23
|
+
head_first: bool = False
|
|
24
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
25
|
+
r"""
|
|
26
|
+
Args:
|
|
27
|
+
q (torch.Tensor):
|
|
28
|
+
queries of shape `[B, T, H, K]`.
|
|
29
|
+
k (torch.Tensor):
|
|
30
|
+
keys of shape `[B, T, H, K]`.
|
|
31
|
+
v (torch.Tensor):
|
|
32
|
+
values of shape `[B, T, H, V]`.
|
|
33
|
+
layer_idx (int):
|
|
34
|
+
The index of the current layer.
|
|
35
|
+
num_layers (int):
|
|
36
|
+
The total number of layers. Both `layer_idx` and `num_layers` are used to compute the decay factor.
|
|
37
|
+
scale (Optional[float]):
|
|
38
|
+
Scale factor for the attention scores.
|
|
39
|
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
|
40
|
+
initial_state (Optional[torch.Tensor]):
|
|
41
|
+
Initial state of shape `[N, H, K, V]` for `N` input sequences.
|
|
42
|
+
For equal-length input sequences, `N` equals the batch size `B`.
|
|
43
|
+
Default: `None`.
|
|
44
|
+
output_final_state (Optional[bool]):
|
|
45
|
+
Whether to output the final state of shape `[N, H, K, V]`. Default: `False`.
|
|
46
|
+
cu_seqlens (torch.LongTensor):
|
|
47
|
+
Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
|
|
48
|
+
consistent with the FlashAttention API.
|
|
49
|
+
head_first (Optional[bool]):
|
|
50
|
+
Whether the inputs are in the head-first format. Default: `False`.
|
|
51
|
+
This argument has been deprecated.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
o (torch.Tensor):
|
|
55
|
+
Outputs of shape `[B, T, H, V]`.
|
|
56
|
+
final_state (torch.Tensor):
|
|
57
|
+
Final state of shape `[N, H, K, V]` if `output_final_state=True` else `None`.
|
|
58
|
+
"""
|
|
59
|
+
if head_first:
|
|
60
|
+
raise DeprecationWarning(
|
|
61
|
+
"head_first is deprecated and will be removed in a future version. "
|
|
62
|
+
"Please use head_first=False for now instead."
|
|
63
|
+
)
|
|
64
|
+
if not head_first and q.shape[1] < q.shape[2]:
|
|
65
|
+
warnings.warn(
|
|
66
|
+
f"Input tensor shape suggests potential format mismatch: seq_len ({q.shape[1]}) < num_heads ({q.shape[2]}). "
|
|
67
|
+
"This may indicate the inputs were passed in head-first format [B, H, T, ...] "
|
|
68
|
+
"when head_first=False was specified. "
|
|
69
|
+
"Please verify your input tensor format matches the expected shape [B, T, H, ...]."
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
H = q.shape[2]
|
|
73
|
+
g_gamma = -(8 / H * (1 - layer_idx / num_layers)) * q.new_tensor(range(H), dtype=torch.float)
|
|
74
|
+
return chunk_simple_gla(
|
|
75
|
+
q=q,
|
|
76
|
+
k=k,
|
|
77
|
+
v=v,
|
|
78
|
+
scale=scale,
|
|
79
|
+
g_gamma=g_gamma,
|
|
80
|
+
initial_state=initial_state,
|
|
81
|
+
output_final_state=output_final_state,
|
|
82
|
+
head_first=head_first,
|
|
83
|
+
cu_seqlens=cu_seqlens
|
|
84
|
+
)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
import warnings
|
|
5
|
+
from typing import Optional, Tuple
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
from fla.ops.simple_gla.fused_recurrent import fused_recurrent_simple_gla
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def fused_recurrent_lightning_attn(
|
|
13
|
+
q: torch.Tensor,
|
|
14
|
+
k: torch.Tensor,
|
|
15
|
+
v: torch.Tensor,
|
|
16
|
+
layer_idx: int,
|
|
17
|
+
num_layers: int,
|
|
18
|
+
scale: Optional[float] = None,
|
|
19
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
20
|
+
output_final_state: bool = False,
|
|
21
|
+
reverse: bool = False,
|
|
22
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
23
|
+
head_first: bool = False
|
|
24
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
25
|
+
r"""
|
|
26
|
+
Args:
|
|
27
|
+
q (torch.Tensor):
|
|
28
|
+
queries of shape `[B, T, H, K]`.
|
|
29
|
+
k (torch.Tensor):
|
|
30
|
+
keys of shape `[B, T, H, K]`.
|
|
31
|
+
v (torch.Tensor):
|
|
32
|
+
values of shape `[B, T, H, V]`.
|
|
33
|
+
layer_idx (int):
|
|
34
|
+
The index of the current layer.
|
|
35
|
+
num_layers (int):
|
|
36
|
+
The total number of layers. Both `layer_idx` and `num_layers` are used to compute the decay factor.
|
|
37
|
+
scale (Optional[float]):
|
|
38
|
+
Scale factor for the attention scores.
|
|
39
|
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
|
40
|
+
initial_state (Optional[torch.Tensor]):
|
|
41
|
+
Initial state of shape `[N, H, K, V]` for `N` input sequences.
|
|
42
|
+
For equal-length input sequences, `N` equals the batch size `B`.
|
|
43
|
+
Default: `None`.
|
|
44
|
+
output_final_state (Optional[bool]):
|
|
45
|
+
Whether to output the final state of shape `[N, H, K, V]`. Default: `False`.
|
|
46
|
+
cu_seqlens (torch.LongTensor):
|
|
47
|
+
Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
|
|
48
|
+
consistent with the FlashAttention API.
|
|
49
|
+
head_first (Optional[bool]):
|
|
50
|
+
Whether the inputs are in the head-first format. Default: `False`.
|
|
51
|
+
This argument has been deprecated.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
o (torch.Tensor):
|
|
55
|
+
Outputs of shape `[B, T, H, V]`.
|
|
56
|
+
final_state (torch.Tensor):
|
|
57
|
+
Final state of shape `[N, H, K, V]` if `output_final_state=True` else `None`.
|
|
58
|
+
"""
|
|
59
|
+
if head_first:
|
|
60
|
+
raise DeprecationWarning(
|
|
61
|
+
"head_first is deprecated and will be removed in a future version. "
|
|
62
|
+
"Please use head_first=False for now instead."
|
|
63
|
+
)
|
|
64
|
+
if not head_first and q.shape[1] < q.shape[2]:
|
|
65
|
+
warnings.warn(
|
|
66
|
+
f"Input tensor shape suggests potential format mismatch: seq_len ({q.shape[1]}) < num_heads ({q.shape[2]}). "
|
|
67
|
+
"This may indicate the inputs were passed in head-first format [B, H, T, ...] "
|
|
68
|
+
"when head_first=False was specified. "
|
|
69
|
+
"Please verify your input tensor format matches the expected shape [B, T, H, ...]."
|
|
70
|
+
)
|
|
71
|
+
H = q.shape[2]
|
|
72
|
+
g_gamma = -(8 / H * (1 - layer_idx / num_layers)) * q.new_tensor(range(H), dtype=torch.float)
|
|
73
|
+
return fused_recurrent_simple_gla(
|
|
74
|
+
q=q,
|
|
75
|
+
k=k,
|
|
76
|
+
v=v,
|
|
77
|
+
g_gamma=g_gamma,
|
|
78
|
+
scale=scale,
|
|
79
|
+
initial_state=initial_state,
|
|
80
|
+
output_final_state=output_final_state,
|
|
81
|
+
reverse=reverse,
|
|
82
|
+
cu_seqlens=cu_seqlens,
|
|
83
|
+
head_first=head_first
|
|
84
|
+
)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .chunk import chunk_linear_attn
|
|
4
|
+
from .fused_chunk import fused_chunk_linear_attn
|
|
5
|
+
from .fused_recurrent import fused_recurrent_linear_attn
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
'chunk_linear_attn',
|
|
9
|
+
'fused_chunk_linear_attn',
|
|
10
|
+
'fused_recurrent_linear_attn'
|
|
11
|
+
]
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
from typing import Optional, Tuple
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
from fla.ops.linear_attn.utils import normalize_output
|
|
9
|
+
from fla.ops.simple_gla import chunk_simple_gla
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@torch.compiler.disable
|
|
13
|
+
def chunk_linear_attn(
|
|
14
|
+
q: torch.Tensor,
|
|
15
|
+
k: torch.Tensor,
|
|
16
|
+
v: torch.Tensor,
|
|
17
|
+
scale: Optional[float] = None,
|
|
18
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
19
|
+
output_final_state: bool = False,
|
|
20
|
+
normalize: bool = True,
|
|
21
|
+
head_first: bool = False
|
|
22
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
23
|
+
r"""
|
|
24
|
+
Args:
|
|
25
|
+
q (torch.Tensor):
|
|
26
|
+
queries of shape `[B, T, H, K]`.
|
|
27
|
+
k (torch.Tensor):
|
|
28
|
+
keys of shape `[B, T, H, K]`.
|
|
29
|
+
v (torch.Tensor):
|
|
30
|
+
values of shape `[B, T, H, V]`.
|
|
31
|
+
scale (Optional[float]):
|
|
32
|
+
Scale factor for the linear attention scores.
|
|
33
|
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
|
34
|
+
initial_state (Optional[torch.Tensor]):
|
|
35
|
+
Initial state of shape `[B, H, K, V]`. Default: `None`.
|
|
36
|
+
output_final_state (Optional[bool]):
|
|
37
|
+
Whether to output the final state of shape `[B, H, K, V]`. Default: `False`.
|
|
38
|
+
normalize (bool):
|
|
39
|
+
Whether to normalize the output. Default: `True`.
|
|
40
|
+
head_first (Optional[bool]):
|
|
41
|
+
Whether the inputs are in the head-first format. Default: `False`.
|
|
42
|
+
This argument has been deprecated.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
o (torch.Tensor):
|
|
46
|
+
Outputs of shape `[B, T, H, V]`.
|
|
47
|
+
final_state (torch.Tensor):
|
|
48
|
+
Final state of shape `[B, H, K, V]` if `output_final_state=True` else `None`.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
if head_first:
|
|
52
|
+
raise DeprecationWarning(
|
|
53
|
+
"head_first is deprecated and will be removed in a future version. "
|
|
54
|
+
"Please use head_first=False for now instead."
|
|
55
|
+
)
|
|
56
|
+
if not head_first:
|
|
57
|
+
if q.shape[1] < q.shape[2]:
|
|
58
|
+
raise DeprecationWarning(
|
|
59
|
+
f"Input tensor shape suggests potential format mismatch: seq_len ({q.shape[1]}) < num_heads ({q.shape[2]}). "
|
|
60
|
+
"This may indicate the inputs were passed in head-first format [B, H, T, ...] "
|
|
61
|
+
"when head_first=False was specified. "
|
|
62
|
+
"Please verify your input tensor format matches the expected shape [B, T, H, ...]."
|
|
63
|
+
)
|
|
64
|
+
if scale is None:
|
|
65
|
+
scale = k.shape[-1] ** -0.5
|
|
66
|
+
o, final_state = chunk_simple_gla(
|
|
67
|
+
q=q,
|
|
68
|
+
k=k,
|
|
69
|
+
v=v,
|
|
70
|
+
scale=scale,
|
|
71
|
+
initial_state=initial_state,
|
|
72
|
+
output_final_state=output_final_state
|
|
73
|
+
)
|
|
74
|
+
if normalize:
|
|
75
|
+
o = normalize_output(q * scale, k, o)
|
|
76
|
+
return o, final_state
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
from typing import Optional, Tuple
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
from fla.ops.linear_attn.utils import normalize_output
|
|
9
|
+
from fla.ops.simple_gla import fused_chunk_simple_gla
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@torch.compiler.disable
|
|
13
|
+
def fused_chunk_linear_attn(
|
|
14
|
+
q: torch.Tensor,
|
|
15
|
+
k: torch.Tensor,
|
|
16
|
+
v: torch.Tensor,
|
|
17
|
+
scale: Optional[float] = None,
|
|
18
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
19
|
+
output_final_state: bool = False,
|
|
20
|
+
normalize: bool = True,
|
|
21
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
22
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
23
|
+
r"""
|
|
24
|
+
Args:
|
|
25
|
+
q (torch.Tensor):
|
|
26
|
+
queries of shape `[B, T, H, K]`.
|
|
27
|
+
k (torch.Tensor):
|
|
28
|
+
keys of shape `[B, T, H, K]`.
|
|
29
|
+
v (torch.Tensor):
|
|
30
|
+
values of shape `[B, T, H, V]`.
|
|
31
|
+
scale (Optional[float]):
|
|
32
|
+
Scale factor for linear attention scores.
|
|
33
|
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
|
34
|
+
initial_state (Optional[torch.Tensor]):
|
|
35
|
+
Initial state of shape `[B, H, K, V]`. Default: `None`.
|
|
36
|
+
output_final_state (Optional[bool]):
|
|
37
|
+
Whether to output the final state of shape `[B, H, K, V]`. Default: `False`.
|
|
38
|
+
normalize (bool):
|
|
39
|
+
Whether to normalize the output. Default: `True`.
|
|
40
|
+
cu_seqlens (torch.LongTensor):
|
|
41
|
+
Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
|
|
42
|
+
consistent with the FlashAttention API.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
o (torch.Tensor):
|
|
46
|
+
Outputs of shape `[B, T, H, V]`.
|
|
47
|
+
final_state (torch.Tensor):
|
|
48
|
+
Final state of shape `[B, H, K, V]` if `output_final_state=True` else `None`
|
|
49
|
+
"""
|
|
50
|
+
o, final_state = fused_chunk_simple_gla(
|
|
51
|
+
q=q,
|
|
52
|
+
k=k,
|
|
53
|
+
v=v,
|
|
54
|
+
scale=scale,
|
|
55
|
+
initial_state=initial_state,
|
|
56
|
+
output_final_state=output_final_state,
|
|
57
|
+
cu_seqlens=cu_seqlens
|
|
58
|
+
)
|
|
59
|
+
if normalize:
|
|
60
|
+
o = normalize_output(q * scale, k, o)
|
|
61
|
+
return o, final_state
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
from typing import Optional, Tuple
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
from fla.ops.linear_attn.utils import normalize_output
|
|
9
|
+
from fla.ops.simple_gla.fused_recurrent import fused_recurrent_simple_gla
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def fused_recurrent_linear_attn(
|
|
13
|
+
q: torch.Tensor,
|
|
14
|
+
k: torch.Tensor,
|
|
15
|
+
v: torch.Tensor,
|
|
16
|
+
scale: Optional[float] = None,
|
|
17
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
18
|
+
output_final_state: bool = False,
|
|
19
|
+
reverse: bool = False,
|
|
20
|
+
normalize: bool = False,
|
|
21
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
22
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
23
|
+
o, final_state = fused_recurrent_simple_gla(
|
|
24
|
+
q=q,
|
|
25
|
+
k=k,
|
|
26
|
+
v=v,
|
|
27
|
+
scale=scale,
|
|
28
|
+
initial_state=initial_state,
|
|
29
|
+
output_final_state=output_final_state,
|
|
30
|
+
reverse=reverse,
|
|
31
|
+
cu_seqlens=cu_seqlens,
|
|
32
|
+
)
|
|
33
|
+
if normalize:
|
|
34
|
+
o = normalize_output(q * scale, k, o)
|
|
35
|
+
return o, final_state
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
from typing import Optional, Tuple
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
from einops import rearrange
|
|
8
|
+
|
|
9
|
+
from fla.ops.linear_attn.utils import normalize_output
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def naive_recurrent_linear_attn(
|
|
13
|
+
q: torch.Tensor,
|
|
14
|
+
k: torch.Tensor,
|
|
15
|
+
v: torch.Tensor,
|
|
16
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
17
|
+
output_final_state: bool = False,
|
|
18
|
+
scale: Optional[float] = None,
|
|
19
|
+
normalize: bool = False
|
|
20
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
21
|
+
dtype = q.dtype
|
|
22
|
+
if scale is None:
|
|
23
|
+
scale = q.shape[-1] ** -0.5
|
|
24
|
+
B, T, H, K, V = *q.shape, v.shape[-1]
|
|
25
|
+
q, k, v = map(lambda x: x.to(torch.float32), (q, k, v))
|
|
26
|
+
o = torch.empty_like(v)
|
|
27
|
+
|
|
28
|
+
S = torch.zeros((B, H, K, V), device=q.device, dtype=torch.float32)
|
|
29
|
+
if initial_state is not None:
|
|
30
|
+
S = S + initial_state
|
|
31
|
+
for t in range(T):
|
|
32
|
+
S = S + torch.einsum('b h k, b h v -> b h k v', k[:, t], v[:, t])
|
|
33
|
+
o[:, t] = torch.einsum('b h k v, b h k -> b h v', S, q[:, t] * scale)
|
|
34
|
+
if normalize:
|
|
35
|
+
o = normalize_output(q * scale, k, o)
|
|
36
|
+
return o.to(dtype), S if output_final_state else None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def naive_chunk_linear_attn(
|
|
40
|
+
q: torch.Tensor,
|
|
41
|
+
k: torch.Tensor,
|
|
42
|
+
v: torch.Tensor,
|
|
43
|
+
scale: Optional[float] = None,
|
|
44
|
+
normalize: bool = False
|
|
45
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
46
|
+
if scale is None:
|
|
47
|
+
scale = q.shape[-1] ** -0.5
|
|
48
|
+
chunk_size = 64
|
|
49
|
+
q = rearrange(q, 'b (n c) h d -> b h n c d', c=chunk_size) * scale
|
|
50
|
+
k = rearrange(k, 'b (n c) h d -> b h n c d', c=chunk_size)
|
|
51
|
+
v = rearrange(v, 'b (n c) h d -> b h n c d', c=chunk_size)
|
|
52
|
+
kv = k.transpose(-1, -2) @ v
|
|
53
|
+
kv = kv.cumsum(2)
|
|
54
|
+
kv = torch.cat([torch.zeros_like(kv[:, :, :1]), kv[:, :, :-1]], dim=2)
|
|
55
|
+
inter = q @ kv
|
|
56
|
+
intra = ((
|
|
57
|
+
q @ k.transpose(-1, -2)).masked_fill_(
|
|
58
|
+
torch.triu(torch.ones(chunk_size, chunk_size, dtype=bool, device=q.device), diagonal=1),
|
|
59
|
+
0
|
|
60
|
+
)) @ v
|
|
61
|
+
o = inter + intra
|
|
62
|
+
if normalize:
|
|
63
|
+
o = normalize_output(q * scale, k, o)
|
|
64
|
+
return rearrange(o, 'b h n c d -> b (n c) h d')
|