-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathchunk_h.py
More file actions
527 lines (482 loc) 路 18.9 KB
/
Copy pathchunk_h.py
File metadata and controls
527 lines (482 loc) 路 18.9 KB
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
# Copyright (c) 2023-2026, Songlin Yang, Yu Zhang, Zhiyuan Li
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# For a list of all contributors, visit:
# https://github.com/fla-org/flash-linear-attention/graphs/contributors
import torch
import triton
import triton.language as tl
from fla.ops.utils import prepare_chunk_offsets
from fla.ops.utils.op import exp2
from fla.utils import autotune_cache_kwargs, check_shared_mem
BKV_LIST = [32, 64] if check_shared_mem() else [16, 32]
@triton.heuristics({
'USE_INITIAL_STATE': lambda args: args['h0'] is not None,
'STORE_FINAL_STATE': lambda args: args['ht'] is not None,
'STORE_MMA_STATE': lambda args: args['h_mma'] is not None,
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
})
@triton.autotune(
configs=[
triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
for BK in BKV_LIST
for BV in BKV_LIST
for num_warps in [1, 2, 4, 8]
for num_stages in [2, 3, 4]
],
key=['BT', 'USE_G', 'USE_GK', 'USE_GV', 'STATE_V_FIRST'],
**autotune_cache_kwargs,
)
@triton.jit(do_not_specialize=['T'])
def chunk_fwd_kernel_h(
k,
v,
h,
h_mma,
g,
g_gamma,
gk,
gv,
h0,
ht,
cu_seqlens,
split_offsets,
T,
H: tl.constexpr,
K: tl.constexpr,
V: tl.constexpr,
BT: tl.constexpr,
BS: tl.constexpr,
BK: tl.constexpr,
BV: tl.constexpr,
USE_G: tl.constexpr,
USE_G_GAMMA: tl.constexpr,
USE_GK: tl.constexpr,
USE_GV: tl.constexpr,
USE_INITIAL_STATE: tl.constexpr,
STORE_FINAL_STATE: tl.constexpr,
STORE_MMA_STATE: tl.constexpr,
IS_VARLEN: tl.constexpr,
STATE_V_FIRST: tl.constexpr,
):
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2).to(tl.int64)
i_n, i_h = i_nh // H, i_nh % H
if IS_VARLEN:
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int64), tl.load(cu_seqlens + i_n + 1).to(tl.int64)
T = eos - bos
NT, NS = tl.cdiv(T, BT), tl.cdiv(T, BS)
boh = tl.load(split_offsets + i_n).to(tl.int64)
else:
bos, eos = i_n * T, i_n * T + T
NT, NS = tl.cdiv(T, BT), tl.cdiv(T, BS)
boh = i_n * NS
NTS = BS // BT
if USE_G_GAMMA:
# decay rate given the head index
b_gamma = tl.load(g_gamma + i_h)
b_g = b_gamma * (tl.arange(0, BT) + 1)
# [BK, BV] accumulator; STATE_V_FIRST only flips the stored state's HBM layout to [V, K], applied at the load/store below.
b_h = tl.zeros([BK, BV], dtype=tl.float32)
o_k = i_k * BK + tl.arange(0, BK)
o_v = i_v * BV + tl.arange(0, BV)
if USE_INITIAL_STATE:
if STATE_V_FIRST:
p_h0 = h0 + i_nh * K*V + o_v[:, None] * K + o_k[None, :]
b_h = tl.trans(tl.load(p_h0, mask=(o_v[:, None] < V) & (o_k[None, :] < K), other=0.0)).to(tl.float32)
else:
p_h0 = h0 + i_nh * K*V + o_k[:, None] * V + o_v[None, :]
b_h = tl.load(p_h0, mask=(o_k[:, None] < K) & (o_v[None, :] < V), other=0.0).to(tl.float32)
for i_t in range(NT):
i_s = i_t // NTS
o_t = i_t * BT + tl.arange(0, BT)
m_t = o_t < T
p_k = k + (bos*H + i_h) * K + o_k[:, None] + o_t[None, :] * (H*K)
p_v = v + (bos*H + i_h) * V + o_t[:, None] * (H*V) + o_v[None, :]
o_h = ((boh + i_s) * H + i_h).to(tl.int64) * K*V
if STATE_V_FIRST:
p_h = h + o_h + o_v[:, None] * K + o_k[None, :]
m_h = (o_v[:, None] < V) & (o_k[None, :] < K)
if STORE_MMA_STATE:
p_h_mma = h_mma + o_h + o_v[:, None] * K + o_k[None, :]
else:
p_h = h + o_h + o_k[:, None] * V + o_v[None, :]
m_h = (o_k[:, None] < K) & (o_v[None, :] < V)
if STORE_MMA_STATE:
p_h_mma = h_mma + o_h + o_k[:, None] * V + o_v[None, :]
if i_t % NTS == 0:
tl.store(p_h, (tl.trans(b_h) if STATE_V_FIRST else b_h).to(p_h.dtype.element_ty), mask=m_h)
if STORE_MMA_STATE:
tl.store(
p_h_mma,
(tl.trans(b_h) if STATE_V_FIRST else b_h).to(p_h_mma.dtype.element_ty),
mask=m_h,
)
# [BK, BT]
b_k = tl.load(p_k, mask=(o_k[:, None] < K) & m_t[None, :], other=0.0)
# [BT, BV]
b_v = tl.load(p_v, mask=m_t[:, None] & (o_v < V)[None, :], other=0.0)
last_idx = min((i_t + 1) * BT, T) - 1
# scalar decay
if USE_G:
b_g_last = tl.load(g + bos * H + last_idx * H + i_h)
p_g = g + bos*H + (i_t * BT + tl.arange(0, BT)) * H + i_h
b_g = tl.load(p_g, mask=(i_t * BT + tl.arange(0, BT) < T), other=0.)
b_h *= exp2(b_g_last)
b_v = (b_v * exp2(b_g_last - b_g)[:, None]).to(b_v.dtype)
if USE_G_GAMMA:
b_g_last = b_gamma * min(BT, T - i_t * BT)
b_h *= exp2(b_g_last)
b_v = (b_v * exp2(b_g_last - b_g)[:, None]).to(b_v.dtype)
# vector decay, h = Diag(gk) @ h
if USE_GK:
p_gk = gk + (bos*H + i_h) * K + o_k[:, None] + o_t[None, :] * (H*K)
p_gk_last = gk + (bos + last_idx) * H*K + i_h * K + i_k * BK + tl.arange(0, BK)
b_gk_last = tl.load(p_gk_last, mask=(i_k * BK + tl.arange(0, BK) < K), other=0.)
b_gk = tl.load(p_gk, mask=(o_k[:, None] < K) & m_t[None, :], other=0.0)
b_h *= exp2(b_gk_last)[:, None]
b_k = (b_k * exp2(b_gk_last[:, None] - b_gk)).to(b_k.dtype)
# vector decay, h = h @ Diag(gv)
if USE_GV:
p_gv = gv + (bos*H + i_h) * V + o_t[:, None] * (H*V) + o_v[None, :]
p_gv_last = gv + (bos + last_idx) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
b_gv_last = tl.load(p_gv_last, mask=(i_v * BV + tl.arange(0, BV) < V), other=0.)
b_gv = tl.load(p_gv, mask=m_t[:, None] & (o_v < V)[None, :], other=0.0)
b_h *= exp2(b_gv_last)[None, :]
b_v = (b_v * exp2(b_gv_last[None, :] - b_gv)).to(b_v.dtype)
b_h += tl.dot(b_k, b_v)
if STORE_FINAL_STATE:
if STATE_V_FIRST:
p_ht = ht + i_nh * K*V + o_v[:, None] * K + o_k[None, :]
tl.store(p_ht, tl.trans(b_h).to(p_ht.dtype.element_ty), mask=(o_v[:, None] < V) & (o_k[None, :] < K))
else:
p_ht = ht + i_nh * K*V + o_k[:, None] * V + o_v[None, :]
tl.store(p_ht, b_h.to(p_ht.dtype.element_ty), mask=(o_k[:, None] < K) & (o_v[None, :] < V))
@triton.heuristics({
'STORE_INITIAL_STATE_GRADIENT': lambda args: args['dh0'] is not None,
'USE_FINAL_STATE_GRADIENT': lambda args: args['dht'] is not None,
'STORE_MMA_STATE': lambda args: args['dh_mma'] is not None,
'FUSE_HDH_LAST': lambda args: args['h_for_hdh'] is not None and args['hdh_last'] is not None,
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
})
@triton.autotune(
configs=[
triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
for BK in BKV_LIST
for BV in BKV_LIST
for num_warps in [1, 2, 4, 8]
for num_stages in [2, 3, 4]
],
key=['BT', 'USE_G', 'USE_GK', 'USE_GV', 'STATE_V_FIRST'],
**autotune_cache_kwargs,
)
@triton.jit(do_not_specialize=['T'])
def chunk_bwd_kernel_dh(
q,
g,
g_gamma,
gk,
gv,
do,
dh,
dh_mma,
h_for_hdh,
hdh_last,
dht,
dh0,
cu_seqlens,
split_offsets,
scale,
T,
HQ: tl.constexpr,
H: tl.constexpr,
K: tl.constexpr,
V: tl.constexpr,
BT: tl.constexpr,
BS: tl.constexpr,
BK: tl.constexpr,
BV: tl.constexpr,
NG: tl.constexpr,
USE_G: tl.constexpr,
USE_G_GAMMA: tl.constexpr,
USE_GK: tl.constexpr,
USE_GV: tl.constexpr,
STORE_INITIAL_STATE_GRADIENT: tl.constexpr,
USE_FINAL_STATE_GRADIENT: tl.constexpr,
STORE_MMA_STATE: tl.constexpr,
FUSE_HDH_LAST: tl.constexpr,
IS_VARLEN: tl.constexpr,
STATE_V_FIRST: tl.constexpr,
):
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2).to(tl.int64)
i_n, i_hq = i_nh // HQ, i_nh % HQ
i_h = i_hq // NG
if IS_VARLEN:
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int64), tl.load(cu_seqlens + i_n + 1).to(tl.int64)
T = eos - bos
NT = tl.cdiv(T, BT)
NS = tl.cdiv(T, BS)
boh = tl.load(split_offsets + i_n).to(tl.int64)
else:
bos, eos = i_n * T, i_n * T + T
NT = tl.cdiv(T, BT)
NS = tl.cdiv(T, BS)
boh = i_n * NS
if USE_G_GAMMA:
b_gamma = tl.load(g_gamma + i_h)
b_g = b_gamma * (tl.arange(0, BT) + 1)
# [BK, BV] accumulator; STATE_V_FIRST only flips the stored state's HBM layout to [V, K], applied at the load/store below.
b_dh = tl.zeros([BK, BV], dtype=tl.float32)
o_k = i_k * BK + tl.arange(0, BK)
o_v = i_v * BV + tl.arange(0, BV)
if USE_FINAL_STATE_GRADIENT:
if STATE_V_FIRST:
p_dht = dht + i_nh * K*V + o_v[:, None] * K + o_k[None, :]
b_dh += tl.trans(tl.load(p_dht, mask=(o_v[:, None] < V) & (o_k[None, :] < K), other=0.0)).to(tl.float32)
else:
p_dht = dht + i_nh * K*V + o_k[:, None] * V + o_v[None, :]
b_dh += tl.load(p_dht, mask=(o_k[:, None] < K) & (o_v[None, :] < V), other=0.0).to(tl.float32)
for i_t in range(NT - 1, -1, -1):
i_s = i_t // (BS // BT)
o_dh = ((boh + i_s) * H + i_h).to(tl.int64) * K*V
if STATE_V_FIRST:
p_dh = dh + o_dh + o_v[:, None] * K + o_k[None, :]
m_dh = (o_v[:, None] < V) & (o_k[None, :] < K)
if STORE_MMA_STATE:
p_dh_mma = dh_mma + o_dh + o_v[:, None] * K + o_k[None, :]
if FUSE_HDH_LAST:
p_h_for_hdh = h_for_hdh + o_dh + o_v[:, None] * K + o_k[None, :]
else:
p_dh = dh + o_dh + o_k[:, None] * V + o_v[None, :]
m_dh = (o_k[:, None] < K) & (o_v[None, :] < V)
if STORE_MMA_STATE:
p_dh_mma = dh_mma + o_dh + o_k[:, None] * V + o_v[None, :]
if FUSE_HDH_LAST:
p_h_for_hdh = h_for_hdh + o_dh + o_k[:, None] * V + o_v[None, :]
if i_t % (BS // BT) == 0:
tl.store(p_dh, (tl.trans(b_dh) if STATE_V_FIRST else b_dh).to(p_dh.dtype.element_ty), mask=m_dh)
if STORE_MMA_STATE:
tl.store(
p_dh_mma,
(tl.trans(b_dh) if STATE_V_FIRST else b_dh).to(p_dh_mma.dtype.element_ty),
mask=m_dh,
)
if FUSE_HDH_LAST:
b_h_for_hdh = tl.load(p_h_for_hdh, mask=m_dh, other=0.0).to(tl.float32)
if STATE_V_FIRST:
b_h_for_hdh = tl.trans(b_h_for_hdh)
tl.atomic_add(
hdh_last + (boh + i_s) * H + i_h,
tl.sum(b_h_for_hdh * b_dh),
sem='relaxed',
)
last_idx = min(i_t * BT + BT, T) - 1
o_t = i_t * BT + tl.arange(0, BT)
m_t = o_t < T
# [BK, BT]
p_q = q + (bos*HQ + i_hq) * K + o_k[:, None] + o_t[None, :] * (HQ*K)
p_do = do + (bos*HQ + i_hq) * V + o_t[:, None] * (HQ*V) + o_v[None, :]
b_q = tl.load(p_q, mask=(o_k[:, None] < K) & m_t[None, :], other=0.0)
b_q = (b_q * scale).to(b_q.dtype)
# [BT, BV]
b_do = tl.load(p_do, mask=m_t[:, None] & (o_v < V)[None, :], other=0.0)
if USE_G:
p_g = g + (bos + i_t * BT + tl.arange(0, BT)) * H + i_h
b_g_last = tl.load(g + (bos + last_idx) * H + i_h)
b_g = tl.load(p_g, mask=(i_t * BT + tl.arange(0, BT) < T), other=0.)
b_q = (b_q * exp2(b_g)[None, :]).to(b_q.dtype)
b_dh *= exp2(b_g_last)
if USE_G_GAMMA:
b_g_last = b_gamma * min(BT, T - i_t * BT)
b_q = (b_q * exp2(b_g)[None, :]).to(b_q.dtype)
b_dh *= exp2(b_g_last)
if USE_GK:
p_gk = gk + (bos*H + i_h) * K + o_k[:, None] + o_t[None, :] * (H*K)
p_gk_last = gk + (bos + last_idx) * H*K + i_h * K + i_k * BK + tl.arange(0, BK)
b_gk = tl.load(p_gk, mask=(o_k[:, None] < K) & m_t[None, :], other=0.0)
b_gk_last = tl.load(p_gk_last, mask=(i_k * BK + tl.arange(0, BK) < K), other=0.)
b_q = (b_q * exp2(b_gk)).to(b_q.dtype)
b_dh *= exp2(b_gk_last)[:, None]
if USE_GV:
p_gv = gv + (bos*H + i_h) * V + o_t[:, None] * (H*V) + o_v[None, :]
p_gv_last = gv + (bos + last_idx) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
b_gv = tl.load(p_gv, mask=m_t[:, None] & (o_v < V)[None, :], other=0.0)
b_gv_last = tl.load(p_gv_last, mask=(i_v * BV + tl.arange(0, BV) < V), other=0.)
b_do = (b_do * exp2(b_gv))
b_dh *= exp2(b_gv_last)[None, :]
b_dh += tl.dot(b_q, b_do.to(b_q.dtype))
if STORE_INITIAL_STATE_GRADIENT:
if STATE_V_FIRST:
p_dh0 = dh0 + i_nh * K*V + o_v[:, None] * K + o_k[None, :]
tl.store(p_dh0, tl.trans(b_dh).to(p_dh0.dtype.element_ty), mask=(o_v[:, None] < V) & (o_k[None, :] < K))
else:
p_dh0 = dh0 + i_nh * K*V + o_k[:, None] * V + o_v[None, :]
tl.store(p_dh0, b_dh.to(p_dh0.dtype.element_ty), mask=(o_k[:, None] < K) & (o_v[None, :] < V))
@triton.jit
def _chunk_hdh_last_kernel(
h,
dh,
hdh_last,
D: tl.constexpr,
BLOCK: tl.constexpr,
):
i_h = tl.program_id(0)
offs = tl.arange(0, BLOCK)
acc = tl.zeros((BLOCK,), dtype=tl.float32)
for base in range(0, D, BLOCK):
idx = base + offs
mask = idx < D
b_h = tl.load(h + i_h * D + idx, mask=mask, other=0.).to(tl.float32)
b_dh = tl.load(dh + i_h * D + idx, mask=mask, other=0.).to(tl.float32)
acc += b_h * b_dh
tl.store(hdh_last + i_h, tl.sum(acc, axis=0))
def chunk_hdh_last(h: torch.Tensor, dh: torch.Tensor) -> torch.Tensor:
h_flat = h.reshape(-1, h.shape[-2] * h.shape[-1])
dh_flat = dh.reshape(-1, dh.shape[-2] * dh.shape[-1])
D = h_flat.shape[1]
block = min(triton.next_power_of_2(D), 1024)
hdh_last = torch.empty(h_flat.shape[0], dtype=torch.float32, device=h.device)
_chunk_hdh_last_kernel[(h_flat.shape[0],)](
h_flat,
dh_flat,
hdh_last,
D,
BLOCK=block,
num_warps=8 if block >= 1024 else 4,
)
return hdh_last
def chunk_fwd_h(
k: torch.Tensor,
v: torch.Tensor,
g: torch.Tensor | None = None,
g_gamma: torch.Tensor | None = None,
gk: torch.Tensor | None = None,
gv: torch.Tensor | None = None,
h0: torch.Tensor | None = None,
output_final_state: bool = False,
state_v_first: bool = False,
cu_seqlens: torch.Tensor | None = None,
chunk_size: int = 64,
split_size: int | None = None,
states_in_fp32: bool = False,
output_mma_state: bool = False,
) -> tuple[torch.Tensor, torch.Tensor] | tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
B, T, H, K, V = *k.shape, v.shape[-1]
BT = chunk_size
BS = BT if split_size is None else split_size
assert BS % BT == 0, f"The `split_size` (got {BS}) must be a multiple of `chunk_size` {BT}"
# N: the actual number of sequences in the batch with either equal or variable lengths
if cu_seqlens is None:
N, NS, split_offsets = B, triton.cdiv(T, BS), None
else:
split_offsets = prepare_chunk_offsets(cu_seqlens, BS)
N, NS = len(cu_seqlens) - 1, split_offsets[-1].item()
# `state_v_first` stores the states in V-first `[V, K]` layout instead of `[K, V]`
state_shape = (V, K) if state_v_first else (K, V)
h = k.new_empty(B, NS, H, *state_shape, dtype=k.dtype if not states_in_fp32 else torch.float)
h_mma = k.new_empty(B, NS, H, *state_shape, dtype=k.dtype) if output_mma_state else None
ht = k.new_empty(N, H, *state_shape, dtype=torch.float) if output_final_state else None
def grid(meta): return (triton.cdiv(K, meta['BK']), triton.cdiv(V, meta['BV']), N * H)
chunk_fwd_kernel_h[grid](
k=k,
v=v,
h=h,
h_mma=h_mma,
g=g,
g_gamma=g_gamma,
gk=gk,
gv=gv,
h0=h0,
ht=ht,
cu_seqlens=cu_seqlens,
split_offsets=split_offsets,
T=T,
H=H,
K=K,
V=V,
BT=BT,
BS=BS,
USE_G=g is not None,
USE_G_GAMMA=g_gamma is not None,
USE_GK=gk is not None,
USE_GV=gv is not None,
STATE_V_FIRST=state_v_first,
)
if output_mma_state:
return h, ht, h_mma
return h, ht
def chunk_bwd_dh(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
do: torch.Tensor,
h0: torch.Tensor,
dht: torch.Tensor,
scale: float,
g: torch.Tensor | None = None,
g_gamma: torch.Tensor | None = None,
gk: torch.Tensor | None = None,
gv: torch.Tensor | None = None,
state_v_first: bool = False,
cu_seqlens: torch.Tensor | None = None,
chunk_size: int = 64,
split_size: int | None = None,
states_in_fp32: bool = False,
output_mma_state: bool = False,
h_for_hdh: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor] | tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
B, T, H, K, V = *k.shape, v.shape[-1]
HQ = q.shape[2]
BT = chunk_size
BS = BT if split_size is None else split_size
assert BS % BT == 0, f"The `split_size` (got {BS}) must be a multiple of `chunk_size` {BT}"
# N: the actual number of sequences in the batch with either equal or variable lengths
# NG: number of groups in GQA
if cu_seqlens is None:
N, NS, split_offsets = B, triton.cdiv(T, BS), None
else:
split_offsets = prepare_chunk_offsets(cu_seqlens, BS)
N, NS = len(cu_seqlens) - 1, split_offsets[-1].item()
NG = HQ // H
# `state_v_first` stores the states in V-first `[V, K]` layout instead of `[K, V]`
state_shape = (V, K) if state_v_first else (K, V)
dh = k.new_empty(B, NS, HQ, *state_shape, dtype=k.dtype if not states_in_fp32 else torch.float)
dh_mma = k.new_empty(B, NS, HQ, *state_shape, dtype=q.dtype) if output_mma_state else None
hdh_last = q.new_zeros(B, NS, H, dtype=torch.float32) if h_for_hdh is not None else None
dh0 = torch.empty_like(h0, dtype=torch.float) if h0 is not None else None
def grid(meta): return (triton.cdiv(K, meta['BK']), triton.cdiv(V, meta['BV']), N * H)
chunk_bwd_kernel_dh[grid](
q=q,
g=g,
g_gamma=g_gamma,
gk=gk,
gv=gv,
do=do,
dh=dh,
dh_mma=dh_mma,
h_for_hdh=h_for_hdh,
hdh_last=hdh_last,
dht=dht,
dh0=dh0,
cu_seqlens=cu_seqlens,
split_offsets=split_offsets,
scale=scale,
T=T,
HQ=HQ,
H=H,
K=K,
V=V,
BT=BT,
BS=BS,
NG=NG,
USE_G=g is not None,
USE_G_GAMMA=g_gamma is not None,
USE_GK=gk is not None,
USE_GV=gv is not None,
STATE_V_FIRST=state_v_first,
)
if output_mma_state:
if hdh_last is not None:
return dh, dh0, dh_mma, hdh_last
return dh, dh0, dh_mma
return dh, dh0