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,311 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
import warnings
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
from fla.modules.l2norm import l2norm_bwd, l2norm_fwd
|
|
10
|
+
from fla.ops.common.chunk_delta_h import chunk_gated_delta_rule_bwd_dhu, chunk_gated_delta_rule_fwd_h
|
|
11
|
+
from fla.ops.common.chunk_o import chunk_bwd_dqkwg, chunk_bwd_dv_local, chunk_fwd_o
|
|
12
|
+
from fla.ops.delta_rule.wy_fast import prepare_wy_repr_bwd, prepare_wy_repr_fwd, recompute_w_u_fwd
|
|
13
|
+
from fla.utils import autocast_custom_bwd, autocast_custom_fwd, input_guard
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def chunk_delta_rule_fwd(
|
|
17
|
+
q: torch.Tensor,
|
|
18
|
+
k: torch.Tensor,
|
|
19
|
+
v: torch.Tensor,
|
|
20
|
+
beta: torch.Tensor,
|
|
21
|
+
scale: float,
|
|
22
|
+
initial_state: torch.Tensor,
|
|
23
|
+
output_final_state: bool,
|
|
24
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
25
|
+
):
|
|
26
|
+
# obtain WY representation. u is actually the new v.
|
|
27
|
+
w, u, A = prepare_wy_repr_fwd(
|
|
28
|
+
k=k,
|
|
29
|
+
v=v,
|
|
30
|
+
beta=beta,
|
|
31
|
+
cu_seqlens=cu_seqlens,
|
|
32
|
+
)
|
|
33
|
+
h, v_new, final_state = chunk_gated_delta_rule_fwd_h(
|
|
34
|
+
k=k,
|
|
35
|
+
w=w,
|
|
36
|
+
u=u,
|
|
37
|
+
g=None,
|
|
38
|
+
initial_state=initial_state,
|
|
39
|
+
output_final_state=output_final_state,
|
|
40
|
+
cu_seqlens=cu_seqlens
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
o = chunk_fwd_o(
|
|
44
|
+
q=q,
|
|
45
|
+
k=k,
|
|
46
|
+
v=v_new,
|
|
47
|
+
h=h,
|
|
48
|
+
g=None,
|
|
49
|
+
scale=scale,
|
|
50
|
+
cu_seqlens=cu_seqlens
|
|
51
|
+
)
|
|
52
|
+
return o, A, final_state
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def chunk_delta_rule_bwd(
|
|
56
|
+
q: torch.Tensor,
|
|
57
|
+
k: torch.Tensor,
|
|
58
|
+
v: torch.Tensor,
|
|
59
|
+
beta: torch.Tensor,
|
|
60
|
+
A: torch.Tensor,
|
|
61
|
+
scale: float,
|
|
62
|
+
initial_state: torch.Tensor,
|
|
63
|
+
do: torch.Tensor,
|
|
64
|
+
dht: torch.Tensor,
|
|
65
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
66
|
+
):
|
|
67
|
+
w, u = recompute_w_u_fwd(
|
|
68
|
+
k=k,
|
|
69
|
+
v=v,
|
|
70
|
+
beta=beta,
|
|
71
|
+
A=A,
|
|
72
|
+
cu_seqlens=cu_seqlens
|
|
73
|
+
)
|
|
74
|
+
h, v_new, _ = chunk_gated_delta_rule_fwd_h(
|
|
75
|
+
k=k,
|
|
76
|
+
w=w,
|
|
77
|
+
u=u,
|
|
78
|
+
g=None,
|
|
79
|
+
initial_state=initial_state,
|
|
80
|
+
output_final_state=False,
|
|
81
|
+
cu_seqlens=cu_seqlens,
|
|
82
|
+
)
|
|
83
|
+
dv = chunk_bwd_dv_local(
|
|
84
|
+
q=q,
|
|
85
|
+
k=k,
|
|
86
|
+
do=do,
|
|
87
|
+
g=None,
|
|
88
|
+
scale=scale,
|
|
89
|
+
cu_seqlens=cu_seqlens
|
|
90
|
+
)
|
|
91
|
+
dh, dh0, dv = chunk_gated_delta_rule_bwd_dhu(
|
|
92
|
+
q=q,
|
|
93
|
+
k=k,
|
|
94
|
+
w=w,
|
|
95
|
+
g=None,
|
|
96
|
+
h0=initial_state,
|
|
97
|
+
dht=dht,
|
|
98
|
+
do=do,
|
|
99
|
+
dv=dv,
|
|
100
|
+
scale=scale,
|
|
101
|
+
cu_seqlens=cu_seqlens
|
|
102
|
+
)
|
|
103
|
+
dq, dk, dw, _ = chunk_bwd_dqkwg(
|
|
104
|
+
q=q,
|
|
105
|
+
k=k,
|
|
106
|
+
v=v_new,
|
|
107
|
+
h=h,
|
|
108
|
+
w=w,
|
|
109
|
+
dv=dv,
|
|
110
|
+
do=do,
|
|
111
|
+
dh=dh,
|
|
112
|
+
g=None,
|
|
113
|
+
scale=scale,
|
|
114
|
+
cu_seqlens=cu_seqlens
|
|
115
|
+
)
|
|
116
|
+
dk2, dv, db = prepare_wy_repr_bwd(
|
|
117
|
+
k=k,
|
|
118
|
+
v=v,
|
|
119
|
+
beta=beta,
|
|
120
|
+
A=A,
|
|
121
|
+
dw=dw,
|
|
122
|
+
du=dv,
|
|
123
|
+
cu_seqlens=cu_seqlens
|
|
124
|
+
)
|
|
125
|
+
dk.add_(dk2)
|
|
126
|
+
return dq, dk, dv, db, dh0
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class ChunkDeltaRuleFunction(torch.autograd.Function):
|
|
130
|
+
|
|
131
|
+
@staticmethod
|
|
132
|
+
@input_guard
|
|
133
|
+
@autocast_custom_fwd
|
|
134
|
+
def forward(
|
|
135
|
+
ctx,
|
|
136
|
+
q: torch.Tensor,
|
|
137
|
+
k: torch.Tensor,
|
|
138
|
+
v: torch.Tensor,
|
|
139
|
+
beta: torch.Tensor,
|
|
140
|
+
scale: float,
|
|
141
|
+
initial_state: torch.Tensor,
|
|
142
|
+
output_final_state: bool,
|
|
143
|
+
use_qk_l2norm_in_kernel: bool = False,
|
|
144
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
145
|
+
):
|
|
146
|
+
if use_qk_l2norm_in_kernel:
|
|
147
|
+
q, q_rstd = l2norm_fwd(q)
|
|
148
|
+
k, k_rstd = l2norm_fwd(k)
|
|
149
|
+
else:
|
|
150
|
+
q_rstd, k_rstd = None, None
|
|
151
|
+
|
|
152
|
+
o, A, final_state = chunk_delta_rule_fwd(
|
|
153
|
+
q=q,
|
|
154
|
+
k=k,
|
|
155
|
+
v=v,
|
|
156
|
+
beta=beta,
|
|
157
|
+
scale=scale,
|
|
158
|
+
initial_state=initial_state,
|
|
159
|
+
output_final_state=output_final_state,
|
|
160
|
+
cu_seqlens=cu_seqlens,
|
|
161
|
+
)
|
|
162
|
+
ctx.save_for_backward(q, q_rstd, k, k_rstd, v, beta, A, initial_state)
|
|
163
|
+
ctx.scale = scale
|
|
164
|
+
ctx.cu_seqlens = cu_seqlens
|
|
165
|
+
ctx.use_qk_l2norm_in_kernel = use_qk_l2norm_in_kernel
|
|
166
|
+
return o.to(q.dtype), final_state
|
|
167
|
+
|
|
168
|
+
@staticmethod
|
|
169
|
+
@input_guard
|
|
170
|
+
@autocast_custom_bwd
|
|
171
|
+
def backward(
|
|
172
|
+
ctx,
|
|
173
|
+
do: torch.Tensor,
|
|
174
|
+
dht: torch.Tensor
|
|
175
|
+
):
|
|
176
|
+
q, q_rstd, k, k_rstd, v, beta, A, initial_state = ctx.saved_tensors
|
|
177
|
+
|
|
178
|
+
dq, dk, dv, db, dh0 = chunk_delta_rule_bwd(
|
|
179
|
+
q=q,
|
|
180
|
+
k=k,
|
|
181
|
+
v=v,
|
|
182
|
+
beta=beta,
|
|
183
|
+
A=A,
|
|
184
|
+
scale=ctx.scale,
|
|
185
|
+
initial_state=initial_state,
|
|
186
|
+
do=do,
|
|
187
|
+
dht=dht,
|
|
188
|
+
cu_seqlens=ctx.cu_seqlens
|
|
189
|
+
)
|
|
190
|
+
if ctx.use_qk_l2norm_in_kernel:
|
|
191
|
+
dq = l2norm_bwd(q, q_rstd, dq)
|
|
192
|
+
dk = l2norm_bwd(k, k_rstd, dk)
|
|
193
|
+
return dq.to(q.dtype), dk.to(k.dtype), dv.to(v.dtype), db.to(beta.dtype), None, dh0, None, None, None, None, None
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@torch.compiler.disable
|
|
197
|
+
def chunk_delta_rule(
|
|
198
|
+
q: torch.Tensor,
|
|
199
|
+
k: torch.Tensor,
|
|
200
|
+
v: torch.Tensor,
|
|
201
|
+
beta: torch.Tensor,
|
|
202
|
+
scale: float = None,
|
|
203
|
+
initial_state: torch.Tensor = None,
|
|
204
|
+
output_final_state: bool = False,
|
|
205
|
+
use_qk_l2norm_in_kernel: bool = False,
|
|
206
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
207
|
+
head_first: bool = False,
|
|
208
|
+
):
|
|
209
|
+
r"""
|
|
210
|
+
Args:
|
|
211
|
+
q (torch.Tensor):
|
|
212
|
+
queries of shape `[B, T, H, K]`.
|
|
213
|
+
k (torch.Tensor):
|
|
214
|
+
keys of shape `[B, T, H, K]`.
|
|
215
|
+
v (torch.Tensor):
|
|
216
|
+
values of shape `[B, T, H, V]`.
|
|
217
|
+
beta (torch.Tensor):
|
|
218
|
+
betas of shape `[B, T, H]`.
|
|
219
|
+
scale (Optional[float]):
|
|
220
|
+
Scale factor for the RetNet attention scores.
|
|
221
|
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
|
222
|
+
initial_state (Optional[torch.Tensor]):
|
|
223
|
+
Initial state of shape `[N, H, K, V]` for `N` input sequences.
|
|
224
|
+
For equal-length input sequences, `N` equals the batch size `B`.
|
|
225
|
+
Default: `None`.
|
|
226
|
+
output_final_state (Optional[bool]):
|
|
227
|
+
Whether to output the final state of shape `[N, H, K, V]`. Default: `False`.
|
|
228
|
+
use_qk_l2norm_in_kernel (Optional[bool]):
|
|
229
|
+
Whether to use qk l2norm within the kernel for saving GPU memory.
|
|
230
|
+
Default: `False`.
|
|
231
|
+
cu_seqlens (torch.LongTensor):
|
|
232
|
+
Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
|
|
233
|
+
consistent with the FlashAttention API.
|
|
234
|
+
head_first (Optional[bool]):
|
|
235
|
+
Whether the inputs are in the head-first format. Default: `False`.
|
|
236
|
+
This argument has been deprecated.
|
|
237
|
+
|
|
238
|
+
Returns:
|
|
239
|
+
o (torch.Tensor):
|
|
240
|
+
Outputs of shape `[B, T, H, V]`.
|
|
241
|
+
final_state (torch.Tensor):
|
|
242
|
+
Final state of shape `[N, H, K, V]` if `output_final_state=True` else `None`.
|
|
243
|
+
|
|
244
|
+
Examples::
|
|
245
|
+
>>> import torch
|
|
246
|
+
>>> import torch.nn.functional as F
|
|
247
|
+
>>> from einops import rearrange
|
|
248
|
+
>>> from fla.ops.delta_rule import chunk_delta_rule
|
|
249
|
+
# inputs with equal lengths
|
|
250
|
+
>>> B, T, H, K, V = 4, 2048, 4, 512, 512
|
|
251
|
+
>>> q = torch.randn(B, T, H, K, dtype=torch.bfloat16, device='cuda')
|
|
252
|
+
>>> k = F.normalize(torch.randn(B, T, H, K, dtype=torch.bfloat16, device='cuda'), p=2, dim=-1)
|
|
253
|
+
>>> v = torch.randn(B, T, H, V, dtype=torch.bfloat16, device='cuda')
|
|
254
|
+
>>> beta = torch.rand(B, T, H, dtype=torch.bfloat16, device='cuda').sigmoid()
|
|
255
|
+
>>> h0 = torch.randn(B, H, K, V, dtype=torch.bfloat16, device='cuda')
|
|
256
|
+
>>> o, ht = chunk_delta_rule(
|
|
257
|
+
q, k, v, beta,
|
|
258
|
+
initial_state=h0,
|
|
259
|
+
output_final_state=True
|
|
260
|
+
)
|
|
261
|
+
# for variable-length inputs, the batch size `B` is expected to be 1 and `cu_seqlens` is required
|
|
262
|
+
>>> q, k, v, beta = map(lambda x: rearrange(x, 'b t ... -> 1 (b t) ...'), (q, k, v, beta))
|
|
263
|
+
# for a batch with 4 sequences, `cu_seqlens` with 5 start/end positions are expected
|
|
264
|
+
>>> cu_seqlens = q.new_tensor([0, 2048, 4096, 6144, 8192], dtype=torch.long)
|
|
265
|
+
>>> o, ht = chunk_delta_rule(
|
|
266
|
+
q, k, v, beta,
|
|
267
|
+
initial_state=h0,
|
|
268
|
+
output_final_state=True,
|
|
269
|
+
cu_seqlens=cu_seqlens
|
|
270
|
+
)
|
|
271
|
+
"""
|
|
272
|
+
assert q.dtype == k.dtype == v.dtype
|
|
273
|
+
assert q.dtype != torch.float32, "ChunkDeltaRuleFunction does not support float32. Please use bfloat16."
|
|
274
|
+
assert len(beta.shape) == 3, "beta must be of shape (batch size, num of head, seq len)."
|
|
275
|
+
|
|
276
|
+
if head_first:
|
|
277
|
+
raise DeprecationWarning(
|
|
278
|
+
"head_first is deprecated and will be removed in a future version. "
|
|
279
|
+
"Please use head_first=False for now instead."
|
|
280
|
+
)
|
|
281
|
+
if not head_first and q.shape[1] < q.shape[2]:
|
|
282
|
+
warnings.warn(
|
|
283
|
+
f"Input tensor shape suggests potential format mismatch: seq_len ({q.shape[1]}) < num_heads ({q.shape[2]}). "
|
|
284
|
+
"This may indicate the inputs were passed in head-first format [B, H, T, ...] "
|
|
285
|
+
"when head_first=False was specified. "
|
|
286
|
+
"Please verify your input tensor format matches the expected shape [B, T, H, ...]."
|
|
287
|
+
)
|
|
288
|
+
if cu_seqlens is not None:
|
|
289
|
+
if q.shape[0] != 1:
|
|
290
|
+
raise ValueError(
|
|
291
|
+
f"The batch size is expected to be 1 rather than {q.shape[0]} when using `cu_seqlens`."
|
|
292
|
+
f"Please flatten variable-length inputs before processing."
|
|
293
|
+
)
|
|
294
|
+
if initial_state is not None and initial_state.shape[0] != len(cu_seqlens) - 1:
|
|
295
|
+
raise ValueError(
|
|
296
|
+
f"The number of initial states is expected to be equal to the number of input sequences, "
|
|
297
|
+
f"i.e., {len(cu_seqlens) - 1} rather than {initial_state.shape[0]}."
|
|
298
|
+
)
|
|
299
|
+
scale = k.shape[-1] ** -0.5 if scale is None else scale
|
|
300
|
+
o, final_state = ChunkDeltaRuleFunction.apply(
|
|
301
|
+
q,
|
|
302
|
+
k,
|
|
303
|
+
v,
|
|
304
|
+
beta,
|
|
305
|
+
scale,
|
|
306
|
+
initial_state,
|
|
307
|
+
output_final_state,
|
|
308
|
+
use_qk_l2norm_in_kernel,
|
|
309
|
+
cu_seqlens,
|
|
310
|
+
)
|
|
311
|
+
return o, final_state
|