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.
Files changed (188) hide show
  1. fla/__init__.py +4 -0
  2. fla/modules/__init__.py +33 -0
  3. fla/modules/activations.py +548 -0
  4. fla/modules/convolution.py +1143 -0
  5. fla/modules/feature_map.py +300 -0
  6. fla/modules/fused_bitlinear.py +632 -0
  7. fla/modules/fused_cross_entropy.py +419 -0
  8. fla/modules/fused_kl_div.py +324 -0
  9. fla/modules/fused_linear_cross_entropy.py +620 -0
  10. fla/modules/fused_norm_gate.py +1253 -0
  11. fla/modules/grpo.py +411 -0
  12. fla/modules/l2norm.py +285 -0
  13. fla/modules/l2warp.py +38 -0
  14. fla/modules/layernorm.py +1433 -0
  15. fla/modules/layernorm_gated.py +528 -0
  16. fla/modules/mlp.py +132 -0
  17. fla/modules/parallel.py +42 -0
  18. fla/modules/rotary.py +500 -0
  19. fla/modules/token_shift.py +543 -0
  20. fla/ops/__init__.py +53 -0
  21. fla/ops/abc/__init__.py +7 -0
  22. fla/ops/abc/chunk.py +1117 -0
  23. fla/ops/abc/naive.py +96 -0
  24. fla/ops/attn/__init__.py +7 -0
  25. fla/ops/attn/decoding.py +182 -0
  26. fla/ops/attn/parallel.py +730 -0
  27. fla/ops/based/__init__.py +9 -0
  28. fla/ops/based/fused_chunk.py +373 -0
  29. fla/ops/based/naive.py +72 -0
  30. fla/ops/based/parallel.py +408 -0
  31. fla/ops/comba/__init__.py +7 -0
  32. fla/ops/comba/chunk.py +342 -0
  33. fla/ops/comba/fused_recurrent.py +332 -0
  34. fla/ops/comba/utils.py +172 -0
  35. fla/ops/comba/wy_fast.py +423 -0
  36. fla/ops/common/__init__.py +1 -0
  37. fla/ops/common/chunk_delta_h.py +527 -0
  38. fla/ops/common/chunk_h.py +384 -0
  39. fla/ops/common/chunk_h_parallel.py +551 -0
  40. fla/ops/common/chunk_h_split.py +596 -0
  41. fla/ops/common/chunk_o.py +681 -0
  42. fla/ops/common/chunk_scaled_dot_kkt.py +305 -0
  43. fla/ops/common/fused_chunk.py +629 -0
  44. fla/ops/common/fused_recurrent.py +567 -0
  45. fla/ops/delta_rule/__init__.py +11 -0
  46. fla/ops/delta_rule/chunk.py +311 -0
  47. fla/ops/delta_rule/fused_chunk.py +6 -0
  48. fla/ops/delta_rule/fused_recurrent.py +535 -0
  49. fla/ops/delta_rule/naive.py +120 -0
  50. fla/ops/delta_rule/parallel.py +403 -0
  51. fla/ops/delta_rule/wy_fast.py +294 -0
  52. fla/ops/forgetting_attn/__init__.py +7 -0
  53. fla/ops/forgetting_attn/parallel.py +64 -0
  54. fla/ops/gated_delta_product/__init__.py +5 -0
  55. fla/ops/gated_delta_product/chunk.py +305 -0
  56. fla/ops/gated_delta_product/chunk_deltaproduct_h.py +502 -0
  57. fla/ops/gated_delta_product/chunk_deltaproduct_o.py +154 -0
  58. fla/ops/gated_delta_product/chunk_ref.py +68 -0
  59. fla/ops/gated_delta_product/naive.py +36 -0
  60. fla/ops/gated_delta_rule/__init__.py +7 -0
  61. fla/ops/gated_delta_rule/chunk.py +340 -0
  62. fla/ops/gated_delta_rule/fused_recurrent.py +351 -0
  63. fla/ops/gated_delta_rule/wy_fast.py +296 -0
  64. fla/ops/generalized_delta_rule/__init__.py +9 -0
  65. fla/ops/generalized_delta_rule/dplr/__init__.py +7 -0
  66. fla/ops/generalized_delta_rule/dplr/chunk.py +362 -0
  67. fla/ops/generalized_delta_rule/dplr/chunk_A_bwd.py +367 -0
  68. fla/ops/generalized_delta_rule/dplr/chunk_A_fwd.py +198 -0
  69. fla/ops/generalized_delta_rule/dplr/chunk_h_bwd.py +175 -0
  70. fla/ops/generalized_delta_rule/dplr/chunk_h_fwd.py +175 -0
  71. fla/ops/generalized_delta_rule/dplr/chunk_o_bwd.py +430 -0
  72. fla/ops/generalized_delta_rule/dplr/chunk_o_fwd.py +125 -0
  73. fla/ops/generalized_delta_rule/dplr/fused_recurrent.py +268 -0
  74. fla/ops/generalized_delta_rule/dplr/naive.py +96 -0
  75. fla/ops/generalized_delta_rule/dplr/wy_fast_bwd.py +164 -0
  76. fla/ops/generalized_delta_rule/dplr/wy_fast_fwd.py +284 -0
  77. fla/ops/generalized_delta_rule/iplr/__init__.py +7 -0
  78. fla/ops/generalized_delta_rule/iplr/chunk.py +492 -0
  79. fla/ops/generalized_delta_rule/iplr/fused_recurrent.py +452 -0
  80. fla/ops/generalized_delta_rule/iplr/naive.py +69 -0
  81. fla/ops/generalized_delta_rule/iplr/wy_fast.py +300 -0
  82. fla/ops/gla/__init__.py +11 -0
  83. fla/ops/gla/chunk.py +1298 -0
  84. fla/ops/gla/fused_chunk.py +642 -0
  85. fla/ops/gla/fused_recurrent.py +111 -0
  86. fla/ops/gla/naive.py +41 -0
  87. fla/ops/gsa/__init__.py +9 -0
  88. fla/ops/gsa/chunk.py +1133 -0
  89. fla/ops/gsa/fused_recurrent.py +536 -0
  90. fla/ops/gsa/naive.py +69 -0
  91. fla/ops/hgrn/__init__.py +9 -0
  92. fla/ops/hgrn/chunk.py +282 -0
  93. fla/ops/hgrn/fused_recurrent.py +308 -0
  94. fla/ops/hgrn/naive.py +63 -0
  95. fla/ops/lightning_attn/__init__.py +9 -0
  96. fla/ops/lightning_attn/chunk.py +84 -0
  97. fla/ops/lightning_attn/fused_recurrent.py +84 -0
  98. fla/ops/linear_attn/__init__.py +11 -0
  99. fla/ops/linear_attn/chunk.py +76 -0
  100. fla/ops/linear_attn/fused_chunk.py +61 -0
  101. fla/ops/linear_attn/fused_recurrent.py +35 -0
  102. fla/ops/linear_attn/naive.py +64 -0
  103. fla/ops/linear_attn/utils.py +10 -0
  104. fla/ops/log_linear_attn/__init__.py +7 -0
  105. fla/ops/log_linear_attn/chunk.py +1904 -0
  106. fla/ops/log_linear_attn/naive.py +50 -0
  107. fla/ops/mesa_net/__init__.py +5 -0
  108. fla/ops/mesa_net/chunk.py +371 -0
  109. fla/ops/mesa_net/chunk_cg_solver_bwd.py +162 -0
  110. fla/ops/mesa_net/chunk_cg_solver_fwd.py +186 -0
  111. fla/ops/mesa_net/chunk_h_fwd.py +170 -0
  112. fla/ops/mesa_net/chunk_h_kk_intra_bwd.py +191 -0
  113. fla/ops/mesa_net/chunk_h_kv_intra_bwd.py +209 -0
  114. fla/ops/mesa_net/chunk_h_kv_intra_bwd_separate.py +300 -0
  115. fla/ops/mesa_net/decoding_one_step.py +175 -0
  116. fla/ops/mesa_net/naive.py +131 -0
  117. fla/ops/nsa/__init__.py +9 -0
  118. fla/ops/nsa/compression.py +536 -0
  119. fla/ops/nsa/naive.py +103 -0
  120. fla/ops/nsa/parallel.py +879 -0
  121. fla/ops/nsa/utils.py +92 -0
  122. fla/ops/path_attn/__init__.py +7 -0
  123. fla/ops/path_attn/cumprod_householder_bwd.py +140 -0
  124. fla/ops/path_attn/cumprod_householder_fwd.py +119 -0
  125. fla/ops/path_attn/intra_chunk_preprocess_bwd.py +140 -0
  126. fla/ops/path_attn/intra_chunk_preprocess_bwd_prepare.py +208 -0
  127. fla/ops/path_attn/intra_chunk_preprocess_fwd.py +168 -0
  128. fla/ops/path_attn/parallel.py +270 -0
  129. fla/ops/path_attn/parallel_path_bwd_inter_dkv.py +189 -0
  130. fla/ops/path_attn/parallel_path_bwd_inter_dqh.py +200 -0
  131. fla/ops/path_attn/parallel_path_bwd_intra.py +172 -0
  132. fla/ops/path_attn/parallel_path_fwd.py +195 -0
  133. fla/ops/path_attn/prepare_k_cache.py +74 -0
  134. fla/ops/path_attn/transform_q.py +108 -0
  135. fla/ops/rebased/__init__.py +7 -0
  136. fla/ops/rebased/naive.py +27 -0
  137. fla/ops/rebased/parallel.py +464 -0
  138. fla/ops/retention/__init__.py +13 -0
  139. fla/ops/retention/chunk.py +77 -0
  140. fla/ops/retention/fused_chunk.py +78 -0
  141. fla/ops/retention/fused_recurrent.py +33 -0
  142. fla/ops/retention/naive.py +15 -0
  143. fla/ops/retention/parallel.py +71 -0
  144. fla/ops/rwkv4/__init__.py +7 -0
  145. fla/ops/rwkv4/fused_recurrent.py +472 -0
  146. fla/ops/rwkv6/__init__.py +9 -0
  147. fla/ops/rwkv6/chunk.py +1300 -0
  148. fla/ops/rwkv6/chunk_naive.py +43 -0
  149. fla/ops/rwkv6/fused_recurrent.py +673 -0
  150. fla/ops/rwkv6/recurrent_naive.py +103 -0
  151. fla/ops/rwkv7/__init__.py +10 -0
  152. fla/ops/rwkv7/channel_mixing.py +323 -0
  153. fla/ops/rwkv7/chunk.py +78 -0
  154. fla/ops/rwkv7/fused_addcmul.py +293 -0
  155. fla/ops/rwkv7/fused_k_update.py +346 -0
  156. fla/ops/rwkv7/fused_recurrent.py +334 -0
  157. fla/ops/rwkv7/gate_output_correction.py +244 -0
  158. fla/ops/simple_gla/__init__.py +13 -0
  159. fla/ops/simple_gla/chunk.py +303 -0
  160. fla/ops/simple_gla/fused_chunk.py +108 -0
  161. fla/ops/simple_gla/fused_recurrent.py +112 -0
  162. fla/ops/simple_gla/naive.py +114 -0
  163. fla/ops/simple_gla/parallel.py +722 -0
  164. fla/ops/titans/__init__.py +7 -0
  165. fla/ops/titans/log_impl.py +153 -0
  166. fla/ops/titans/naive.py +375 -0
  167. fla/ops/ttt/__init__.py +9 -0
  168. fla/ops/ttt/chunk.py +1441 -0
  169. fla/ops/ttt/fused_chunk.py +831 -0
  170. fla/ops/ttt/naive.py +126 -0
  171. fla/ops/utils/__init__.py +58 -0
  172. fla/ops/utils/constant.py +6 -0
  173. fla/ops/utils/cumsum.py +465 -0
  174. fla/ops/utils/index.py +133 -0
  175. fla/ops/utils/logcumsumexp.py +52 -0
  176. fla/ops/utils/logsumexp.py +80 -0
  177. fla/ops/utils/matmul.py +245 -0
  178. fla/ops/utils/op.py +62 -0
  179. fla/ops/utils/pack.py +208 -0
  180. fla/ops/utils/pooling.py +207 -0
  181. fla/ops/utils/softmax.py +106 -0
  182. fla/ops/utils/solve_tril.py +381 -0
  183. fla/utils.py +462 -0
  184. fla_core-0.3.2.dist-info/METADATA +575 -0
  185. fla_core-0.3.2.dist-info/RECORD +188 -0
  186. fla_core-0.3.2.dist-info/WHEEL +5 -0
  187. fla_core-0.3.2.dist-info/licenses/LICENSE +21 -0
  188. fla_core-0.3.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,362 @@
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
+ import triton
9
+
10
+ from fla.ops.generalized_delta_rule.dplr.chunk_A_bwd import chunk_dplr_bwd_dqk_intra
11
+ from fla.ops.generalized_delta_rule.dplr.chunk_A_fwd import chunk_dplr_fwd_intra
12
+ from fla.ops.generalized_delta_rule.dplr.chunk_h_bwd import chunk_dplr_bwd_dhu
13
+ from fla.ops.generalized_delta_rule.dplr.chunk_h_fwd import chunk_dplr_fwd_h
14
+ from fla.ops.generalized_delta_rule.dplr.chunk_o_bwd import chunk_dplr_bwd_dAu, chunk_dplr_bwd_dv, chunk_dplr_bwd_o
15
+ from fla.ops.generalized_delta_rule.dplr.chunk_o_fwd import chunk_dplr_fwd_o
16
+ from fla.ops.generalized_delta_rule.dplr.wy_fast_bwd import chunk_dplr_bwd_wy
17
+ from fla.ops.generalized_delta_rule.dplr.wy_fast_fwd import prepare_wy_repr_fwd
18
+ from fla.ops.rwkv6.chunk import chunk_rwkv6_fwd_cumsum
19
+ from fla.utils import autocast_custom_bwd, autocast_custom_fwd, input_guard
20
+
21
+
22
+ def chunk_dplr_fwd(
23
+ q: torch.Tensor,
24
+ k: torch.Tensor,
25
+ v: torch.Tensor,
26
+ a: torch.Tensor,
27
+ b: torch.Tensor,
28
+ gk: torch.Tensor,
29
+ scale: float,
30
+ initial_state: torch.Tensor,
31
+ output_final_state: bool,
32
+ cu_seqlens: Optional[torch.LongTensor] = None,
33
+ chunk_size: int = 64
34
+ ):
35
+ T = q.shape[1]
36
+ BT = min(chunk_size, max(triton.next_power_of_2(T), 16))
37
+ gi, ge = chunk_rwkv6_fwd_cumsum(gk, BT, cu_seqlens=cu_seqlens)
38
+
39
+ A_ab, A_qk, A_ak, A_qb, qg, kg, ag, bg = chunk_dplr_fwd_intra(
40
+ q=q,
41
+ k=k,
42
+ a=a,
43
+ b=b,
44
+ gi=gi,
45
+ ge=ge,
46
+ scale=scale,
47
+ cu_seqlens=cu_seqlens,
48
+ chunk_size=BT,
49
+ )
50
+ del ge
51
+
52
+ # A_ab, A_ak, gi, ge torch.float32
53
+ # A_qk, A_qb, qg, kg, ag, bg, dtype=q.dtype, eg: bf16
54
+ w, u, _ = prepare_wy_repr_fwd(
55
+ ag=ag,
56
+ A_ab=A_ab,
57
+ A_ak=A_ak,
58
+ v=v,
59
+ cu_seqlens=cu_seqlens,
60
+ chunk_size=BT
61
+ )
62
+ del A_ab, A_ak
63
+ h, v_new, final_state = chunk_dplr_fwd_h(
64
+ kg=kg,
65
+ bg=bg,
66
+ v=v,
67
+ w=w,
68
+ u=u,
69
+ gk=gi,
70
+ initial_state=initial_state,
71
+ output_final_state=output_final_state,
72
+ cu_seqlens=cu_seqlens,
73
+ chunk_size=BT
74
+ )
75
+ del u, kg, bg, gi
76
+
77
+ o = chunk_dplr_fwd_o(
78
+ qg=qg,
79
+ v=v,
80
+ v_new=v_new,
81
+ A_qk=A_qk,
82
+ A_qb=A_qb,
83
+ h=h,
84
+ cu_seqlens=cu_seqlens,
85
+ chunk_size=BT
86
+ )
87
+ del v_new, h, A_qk, A_qb
88
+
89
+ return o, final_state
90
+
91
+
92
+ class ChunkDPLRDeltaRuleFunction(torch.autograd.Function):
93
+
94
+ @staticmethod
95
+ @input_guard
96
+ @autocast_custom_fwd
97
+ def forward(
98
+ ctx,
99
+ q: torch.Tensor,
100
+ k: torch.Tensor,
101
+ v: torch.Tensor,
102
+ a: torch.Tensor,
103
+ b: torch.Tensor,
104
+ gk: torch.Tensor,
105
+ scale: float,
106
+ initial_state: torch.Tensor,
107
+ output_final_state: bool,
108
+ cu_seqlens: Optional[torch.LongTensor] = None,
109
+ ):
110
+ chunk_size = 16
111
+ o, final_state = chunk_dplr_fwd(
112
+ q=q,
113
+ k=k,
114
+ v=v,
115
+ a=a,
116
+ b=b,
117
+ gk=gk,
118
+ scale=scale,
119
+ initial_state=initial_state,
120
+ output_final_state=output_final_state,
121
+ cu_seqlens=cu_seqlens,
122
+ chunk_size=chunk_size
123
+ )
124
+ ctx.save_for_backward(q, k, v, a, b, gk, initial_state)
125
+ ctx.cu_seqlens = cu_seqlens
126
+ ctx.scale = scale
127
+ ctx.chunk_size = chunk_size
128
+ return o.to(q.dtype), final_state
129
+
130
+ @staticmethod
131
+ @input_guard
132
+ @autocast_custom_bwd
133
+ def backward(
134
+ ctx,
135
+ do: torch.Tensor,
136
+ dht: torch.Tensor
137
+ ):
138
+ q, k, v, a, b, gk, initial_state = ctx.saved_tensors
139
+ BT = ctx.chunk_size
140
+ cu_seqlens = ctx.cu_seqlens
141
+ scale = ctx.scale
142
+
143
+ # ******* start recomputing everything, otherwise i believe the gpu memory will be exhausted *******
144
+ gi, ge = chunk_rwkv6_fwd_cumsum(gk, BT, cu_seqlens=cu_seqlens)
145
+
146
+ A_ab, A_qk, A_ak, A_qb, qg, kg, ag, bg = chunk_dplr_fwd_intra(
147
+ q=q,
148
+ k=k,
149
+ a=a,
150
+ b=b,
151
+ gi=gi,
152
+ ge=ge,
153
+ scale=scale,
154
+ cu_seqlens=cu_seqlens,
155
+ chunk_size=BT,
156
+ )
157
+ w, u, A_ab_inv = prepare_wy_repr_fwd(
158
+ ag=ag,
159
+ A_ab=A_ab,
160
+ A_ak=A_ak,
161
+ v=v,
162
+ cu_seqlens=cu_seqlens,
163
+ chunk_size=BT
164
+ )
165
+ del A_ab
166
+ h, v_new, _ = chunk_dplr_fwd_h(
167
+ kg=kg,
168
+ bg=bg,
169
+ v=v,
170
+ w=w,
171
+ u=u,
172
+ gk=gi,
173
+ initial_state=initial_state,
174
+ cu_seqlens=cu_seqlens,
175
+ chunk_size=BT
176
+ )
177
+ del u
178
+ # ******* end of recomputation *******
179
+ # A_ak, A_ab_inv, gi, ge torch.float32
180
+ # A_qk, A_qb, qg, kg, ag, bg, v_new dtype=q.dtype, eg: bf16
181
+
182
+ dv_new_intra, dA_qk, dA_qb = chunk_dplr_bwd_dAu(
183
+ v=v,
184
+ v_new=v_new,
185
+ do=do,
186
+ A_qb=A_qb,
187
+ scale=scale,
188
+ cu_seqlens=cu_seqlens,
189
+ chunk_size=BT
190
+ )
191
+
192
+ dh, dh0, dv_new = chunk_dplr_bwd_dhu(
193
+ qg=qg,
194
+ bg=bg,
195
+ w=w,
196
+ gk=gi,
197
+ h0=initial_state,
198
+ dht=dht,
199
+ do=do,
200
+ dv=dv_new_intra,
201
+ cu_seqlens=cu_seqlens,
202
+ chunk_size=BT
203
+ )
204
+
205
+ dv = chunk_dplr_bwd_dv(
206
+ A_qk=A_qk,
207
+ kg=kg,
208
+ do=do,
209
+ dh=dh,
210
+ cu_seqlens=cu_seqlens,
211
+ chunk_size=BT
212
+ )
213
+ del A_qk
214
+
215
+ dqg, dkg, dw, dbg, dgk_last = chunk_dplr_bwd_o(
216
+ k=kg,
217
+ b=bg,
218
+ v=v,
219
+ v_new=v_new,
220
+ do=do,
221
+ h=h,
222
+ dh=dh,
223
+ dv=dv_new,
224
+ w=w,
225
+ gk=gi,
226
+ cu_seqlens=cu_seqlens,
227
+ chunk_size=BT,
228
+ scale=scale,
229
+ )
230
+ del v_new
231
+
232
+ dA_ab, dA_ak, dv, dag = chunk_dplr_bwd_wy(
233
+ A_ab_inv=A_ab_inv,
234
+ A_ak=A_ak,
235
+ v=v,
236
+ ag=ag,
237
+ dw=dw,
238
+ du=dv_new,
239
+ dv0=dv,
240
+ cu_seqlens=cu_seqlens,
241
+ chunk_size=BT
242
+ )
243
+ del A_ak
244
+
245
+ dq, dk, da, db, dgk = chunk_dplr_bwd_dqk_intra(
246
+ q=q,
247
+ k=k,
248
+ a=a,
249
+ b=b,
250
+ gi=gi,
251
+ ge=ge,
252
+ dAqk=dA_qk,
253
+ dAqb=dA_qb,
254
+ dAak=dA_ak,
255
+ dAab=dA_ab,
256
+ dgk_last=dgk_last,
257
+ dqg=dqg,
258
+ dkg=dkg,
259
+ dag=dag,
260
+ dbg=dbg,
261
+ chunk_size=BT,
262
+ scale=scale,
263
+ cu_seqlens=cu_seqlens,
264
+ )
265
+
266
+ return dq.to(q), dk.to(k), dv.to(v), da.to(a), db.to(b), dgk.to(gk), None, dh0, None, None
267
+
268
+
269
+ @torch.compiler.disable
270
+ def chunk_dplr_delta_rule(
271
+ q: torch.Tensor,
272
+ k: torch.Tensor,
273
+ v: torch.Tensor,
274
+ a: torch.Tensor,
275
+ b: torch.Tensor,
276
+ gk: torch.Tensor,
277
+ scale: Optional[float] = None,
278
+ initial_state: Optional[torch.Tensor] = None,
279
+ output_final_state: bool = False,
280
+ cu_seqlens: Optional[torch.LongTensor] = None,
281
+ head_first: bool = False,
282
+ ):
283
+ r"""
284
+ Args:
285
+ q (torch.Tensor):
286
+ queries of shape `[B, T, H, K]`.
287
+ k (torch.Tensor):
288
+ keys of shape `[B, T, H, K]`.
289
+ v (torch.Tensor):
290
+ values of shape `[B, T, H, V]`.
291
+ a (torch.Tensor):
292
+ activations of shape `[B, T, H, K]`.
293
+ b (torch.Tensor):
294
+ betas of shape `[B, T, H, K]`.
295
+ gk (torch.Tensor):
296
+ gk of shape `[B, T, H, K]`. decay term in log space!
297
+ scale (Optional[float]):
298
+ Scale factor for the RetNet attention scores.
299
+ If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
300
+ initial_state (Optional[torch.Tensor]):
301
+ Initial state of shape `[N, H, K, V]` for `N` input sequences.
302
+ For equal-length input sequences, `N` equals the batch size `B`.
303
+ Default: `None`.
304
+ output_final_state (Optional[bool]):
305
+ Whether to output the final state of shape `[N, H, K, V]`. Default: `False`.
306
+ cu_seqlens (torch.LongTensor):
307
+ Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
308
+ consistent with the FlashAttention API.
309
+ head_first (Optional[bool]):
310
+ Whether the inputs are in the head-first format. Default: `False`.
311
+ This argument has been deprecated.
312
+
313
+ Returns:
314
+ o (torch.Tensor):
315
+ Outputs of shape `[B, T, H, V]`.
316
+ final_state (torch.Tensor):
317
+ Final state of shape `[N, H, K, V]` if `output_final_state=True` else `None`.
318
+ """
319
+ if head_first:
320
+ raise DeprecationWarning(
321
+ "head_first is deprecated and will be removed in a future version. "
322
+ "Please use head_first=False for now instead."
323
+ )
324
+ if not head_first and q.shape[1] < q.shape[2]:
325
+ warnings.warn(
326
+ f"Input tensor shape suggests potential format mismatch: seq_len ({q.shape[1]}) < num_heads ({q.shape[2]}). "
327
+ "This may indicate the inputs were passed in head-first format [B, H, T, ...] "
328
+ "when head_first=False was specified. "
329
+ "Please verify your input tensor format matches the expected shape [B, T, H, ...]."
330
+ )
331
+ if q.dtype == torch.float32:
332
+ warnings.warn(
333
+ """ChunkDeltaRuleFunction does not support float32 on some platforms. Please use bfloat16/float16.
334
+ If you want to use float32, please solve the issue by yourself.""",
335
+ category=RuntimeWarning,
336
+ stacklevel=2
337
+ )
338
+ if cu_seqlens is not None:
339
+ if q.shape[0] != 1:
340
+ raise ValueError(
341
+ f"The batch size is expected to be 1 rather than {q.shape[0]} when using `cu_seqlens`."
342
+ f"Please flatten variable-length inputs before processing."
343
+ )
344
+ if initial_state is not None and initial_state.shape[0] != len(cu_seqlens) - 1:
345
+ raise ValueError(
346
+ f"The number of initial states is expected to be equal to the number of input sequences, "
347
+ f"i.e., {len(cu_seqlens) - 1} rather than {initial_state.shape[0]}."
348
+ )
349
+ scale = k.shape[-1] ** -0.5 if scale is None else scale
350
+ o, final_state = ChunkDPLRDeltaRuleFunction.apply(
351
+ q,
352
+ k,
353
+ v,
354
+ a,
355
+ b,
356
+ gk,
357
+ scale,
358
+ initial_state,
359
+ output_final_state,
360
+ cu_seqlens,
361
+ )
362
+ return o, final_state