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
fla/ops/comba/utils.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
import triton
|
|
5
|
+
import triton.language as tl
|
|
6
|
+
|
|
7
|
+
from fla.ops.utils.index import prepare_chunk_indices
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@triton.heuristics({
|
|
11
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
12
|
+
})
|
|
13
|
+
@triton.autotune(
|
|
14
|
+
configs=[
|
|
15
|
+
triton.Config({}, num_warps=num_warps)
|
|
16
|
+
for num_warps in [1, 2, 4, 8]
|
|
17
|
+
],
|
|
18
|
+
key=['B', 'H', 'BT', 'IS_VARLEN']
|
|
19
|
+
)
|
|
20
|
+
@triton.jit(do_not_specialize=['T'])
|
|
21
|
+
def chunk_comba_cumsum_scalar_fwd_kernel(
|
|
22
|
+
g,
|
|
23
|
+
g0,
|
|
24
|
+
g1,
|
|
25
|
+
cu_seqlens,
|
|
26
|
+
chunk_indices,
|
|
27
|
+
T,
|
|
28
|
+
B: tl.constexpr,
|
|
29
|
+
H: tl.constexpr,
|
|
30
|
+
BT: tl.constexpr,
|
|
31
|
+
IS_VARLEN: tl.constexpr,
|
|
32
|
+
HEAD_FIRST: tl.constexpr,
|
|
33
|
+
):
|
|
34
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
35
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
36
|
+
if IS_VARLEN:
|
|
37
|
+
i_n, i_t = tl.load(chunk_indices + i_t * 2).to(tl.int32), tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32)
|
|
38
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
39
|
+
T = eos - bos
|
|
40
|
+
else:
|
|
41
|
+
bos, eos = i_b * T, i_b * T + T
|
|
42
|
+
|
|
43
|
+
if HEAD_FIRST:
|
|
44
|
+
p_g = tl.make_block_ptr(g + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
45
|
+
p_g0 = tl.make_block_ptr(g0 + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
46
|
+
p_g1 = tl.make_block_ptr(g1 + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
47
|
+
else:
|
|
48
|
+
p_g = tl.make_block_ptr(g + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
49
|
+
p_g0 = tl.make_block_ptr(g0 + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
50
|
+
p_g1 = tl.make_block_ptr(g1 + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
51
|
+
# [BT]
|
|
52
|
+
b_g = tl.load(p_g, boundary_check=(0,)).to(tl.float32)
|
|
53
|
+
b_g1 = tl.cumsum(b_g, axis=0)
|
|
54
|
+
b_g0 = b_g1 - b_g
|
|
55
|
+
tl.store(p_g0, b_g0.to(p_g0.dtype.element_ty), boundary_check=(0,))
|
|
56
|
+
tl.store(p_g1, b_g1.to(p_g1.dtype.element_ty), boundary_check=(0,))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def chunk_comba_cumsum_scalar_fwd(
|
|
60
|
+
g: torch.Tensor,
|
|
61
|
+
chunk_size: int,
|
|
62
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
63
|
+
head_first: bool = False,
|
|
64
|
+
output_dtype: Optional[torch.dtype] = torch.float
|
|
65
|
+
) -> torch.Tensor:
|
|
66
|
+
if head_first:
|
|
67
|
+
B, H, T = g.shape
|
|
68
|
+
else:
|
|
69
|
+
B, T, H = g.shape
|
|
70
|
+
assert chunk_size == 2**(chunk_size.bit_length()-1), "chunk_size must be a power of 2"
|
|
71
|
+
BT = chunk_size
|
|
72
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
73
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
74
|
+
g0, g1 = torch.empty_like(g, dtype=output_dtype or g.dtype), torch.empty_like(g, dtype=output_dtype or g.dtype)
|
|
75
|
+
grid = (NT, B * H)
|
|
76
|
+
chunk_comba_cumsum_scalar_fwd_kernel[grid](
|
|
77
|
+
g,
|
|
78
|
+
g0,
|
|
79
|
+
g1,
|
|
80
|
+
cu_seqlens,
|
|
81
|
+
chunk_indices,
|
|
82
|
+
T=T,
|
|
83
|
+
B=B,
|
|
84
|
+
H=H,
|
|
85
|
+
BT=BT,
|
|
86
|
+
HEAD_FIRST=head_first
|
|
87
|
+
)
|
|
88
|
+
return g0, g1
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@triton.heuristics({
|
|
92
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
93
|
+
})
|
|
94
|
+
@triton.autotune(
|
|
95
|
+
configs=[
|
|
96
|
+
triton.Config({}, num_warps=num_warps)
|
|
97
|
+
for num_warps in [1, 2, 4, 8]
|
|
98
|
+
],
|
|
99
|
+
key=['B', 'H', 'BT', 'IS_VARLEN']
|
|
100
|
+
)
|
|
101
|
+
@triton.jit(do_not_specialize=['T'])
|
|
102
|
+
def chunk_comba_cumsum_scalar_bwd_kernel(
|
|
103
|
+
dg0,
|
|
104
|
+
dgr,
|
|
105
|
+
cu_seqlens,
|
|
106
|
+
chunk_indices,
|
|
107
|
+
T,
|
|
108
|
+
B: tl.constexpr,
|
|
109
|
+
H: tl.constexpr,
|
|
110
|
+
BT: tl.constexpr,
|
|
111
|
+
IS_VARLEN: tl.constexpr,
|
|
112
|
+
HEAD_FIRST: tl.constexpr,
|
|
113
|
+
):
|
|
114
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
115
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
116
|
+
if IS_VARLEN:
|
|
117
|
+
i_n, i_t = tl.load(chunk_indices + i_t * 2).to(tl.int32), tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32)
|
|
118
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
119
|
+
T = eos - bos
|
|
120
|
+
else:
|
|
121
|
+
bos, eos = i_b * T, i_b * T + T
|
|
122
|
+
|
|
123
|
+
if HEAD_FIRST:
|
|
124
|
+
p_dg0 = tl.make_block_ptr(dg0 + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
125
|
+
p_dgr = tl.make_block_ptr(dgr + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
126
|
+
else:
|
|
127
|
+
p_dg0 = tl.make_block_ptr(dg0 + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
128
|
+
p_dgr = tl.make_block_ptr(dgr + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
129
|
+
# [BT]
|
|
130
|
+
"""
|
|
131
|
+
b_dg: 1,2,3,4
|
|
132
|
+
b_dg0: 0,1,2,3
|
|
133
|
+
b_temp: 0,1,3,6
|
|
134
|
+
b_dz: 6
|
|
135
|
+
b_dgr: 6,5,3,0
|
|
136
|
+
"""
|
|
137
|
+
b_dg0 = tl.load(p_dg0, boundary_check=(0,)).to(tl.float32)
|
|
138
|
+
b_temp = tl.cumsum(b_dg0, axis=0)
|
|
139
|
+
b_dz = tl.sum(b_dg0, axis=0)
|
|
140
|
+
b_dgr = -b_temp + b_dz[None]
|
|
141
|
+
tl.store(p_dgr, b_dgr.to(p_dgr.dtype.element_ty), boundary_check=(0,))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def chunk_comba_cumsum_scalar_bwd(
|
|
145
|
+
dg0: torch.Tensor,
|
|
146
|
+
chunk_size: int,
|
|
147
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
148
|
+
head_first: bool = False,
|
|
149
|
+
output_dtype: Optional[torch.dtype] = torch.float
|
|
150
|
+
) -> torch.Tensor:
|
|
151
|
+
if head_first:
|
|
152
|
+
B, H, T = dg0.shape
|
|
153
|
+
else:
|
|
154
|
+
B, T, H = dg0.shape
|
|
155
|
+
assert chunk_size == 2**(chunk_size.bit_length()-1), "chunk_size must be a power of 2"
|
|
156
|
+
BT = chunk_size
|
|
157
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
158
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
159
|
+
dg = torch.empty_like(dg0, dtype=output_dtype or dg0.dtype)
|
|
160
|
+
grid = (NT, B * H)
|
|
161
|
+
chunk_comba_cumsum_scalar_bwd_kernel[grid](
|
|
162
|
+
dg0,
|
|
163
|
+
dg,
|
|
164
|
+
cu_seqlens,
|
|
165
|
+
chunk_indices,
|
|
166
|
+
T=T,
|
|
167
|
+
B=B,
|
|
168
|
+
H=H,
|
|
169
|
+
BT=BT,
|
|
170
|
+
HEAD_FIRST=head_first,
|
|
171
|
+
)
|
|
172
|
+
return dg
|
fla/ops/comba/wy_fast.py
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
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
|
+
import triton
|
|
8
|
+
import triton.language as tl
|
|
9
|
+
|
|
10
|
+
from fla.ops.utils import prepare_chunk_indices
|
|
11
|
+
from fla.ops.utils.op import exp
|
|
12
|
+
from fla.utils import check_shared_mem
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@triton.heuristics({
|
|
16
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
|
|
17
|
+
'USE_G': lambda args: args['g'] is not None
|
|
18
|
+
})
|
|
19
|
+
@triton.autotune(
|
|
20
|
+
configs=[
|
|
21
|
+
triton.Config({'BK': BK}, num_warps=num_warps, num_stages=num_stages)
|
|
22
|
+
for BK in [32, 64, 128]
|
|
23
|
+
for num_warps in [2, 4, 8]
|
|
24
|
+
for num_stages in [2, 3, 4]
|
|
25
|
+
],
|
|
26
|
+
key=['H', 'K', 'BT', 'IS_VARLEN', 'USE_G'],
|
|
27
|
+
)
|
|
28
|
+
@triton.jit(do_not_specialize=['T'])
|
|
29
|
+
def chunk_scaled_dot_comba_pkt_fwd_kernel(
|
|
30
|
+
k,
|
|
31
|
+
p,
|
|
32
|
+
beta,
|
|
33
|
+
g0,
|
|
34
|
+
g,
|
|
35
|
+
A,
|
|
36
|
+
cu_seqlens,
|
|
37
|
+
chunk_indices,
|
|
38
|
+
T,
|
|
39
|
+
H: tl.constexpr,
|
|
40
|
+
K: tl.constexpr,
|
|
41
|
+
BT: tl.constexpr,
|
|
42
|
+
BK: tl.constexpr,
|
|
43
|
+
IS_VARLEN: tl.constexpr,
|
|
44
|
+
USE_G: tl.constexpr,
|
|
45
|
+
):
|
|
46
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
47
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
48
|
+
if IS_VARLEN:
|
|
49
|
+
i_n, i_t = tl.load(chunk_indices + i_t * 2).to(tl.int32), tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32)
|
|
50
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
51
|
+
T = eos - bos
|
|
52
|
+
else:
|
|
53
|
+
bos, eos = i_b * T, i_b * T + T
|
|
54
|
+
o_t = i_t * BT + tl.arange(0, BT)
|
|
55
|
+
m_t = o_t < T
|
|
56
|
+
|
|
57
|
+
p_beta = tl.make_block_ptr(beta + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
58
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
59
|
+
|
|
60
|
+
b_A = tl.zeros([BT, BT], dtype=tl.float32)
|
|
61
|
+
for i_k in range(tl.cdiv(K, BK)):
|
|
62
|
+
p_k = tl.make_block_ptr(k + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
63
|
+
p_p = tl.make_block_ptr(p + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
64
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
65
|
+
b_p = tl.load(p_p, boundary_check=(0, 1))
|
|
66
|
+
b_pb = b_p * b_beta[:, None]
|
|
67
|
+
b_A += tl.dot(b_pb.to(b_k.dtype), tl.trans(b_k))
|
|
68
|
+
|
|
69
|
+
if USE_G:
|
|
70
|
+
p_g0 = tl.make_block_ptr(g0 + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
71
|
+
p_g = tl.make_block_ptr(g + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
72
|
+
b_g0 = tl.load(p_g0, boundary_check=(0,))
|
|
73
|
+
b_g = tl.load(p_g, boundary_check=(0,))
|
|
74
|
+
b_A = b_A * exp(b_g0[:, None] - b_g[None, :])
|
|
75
|
+
|
|
76
|
+
m_A = (o_t[:, None] > o_t[None, :]) & (m_t[:, None] & m_t)
|
|
77
|
+
b_A = tl.where(m_A, b_A, 0)
|
|
78
|
+
p_A = tl.make_block_ptr(A + (bos*H + i_h) * BT, (T, BT), (BT*H, 1), (i_t * BT, 0), (BT, BT), (1, 0))
|
|
79
|
+
tl.store(p_A, b_A.to(p_A.dtype.element_ty), boundary_check=(0, 1))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def chunk_scaled_dot_comba_pkt_fwd(
|
|
83
|
+
k: torch.Tensor,
|
|
84
|
+
p: torch.Tensor,
|
|
85
|
+
beta: torch.Tensor,
|
|
86
|
+
g0: Optional[torch.Tensor] = None,
|
|
87
|
+
g: Optional[torch.Tensor] = None,
|
|
88
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
89
|
+
chunk_size: int = 64,
|
|
90
|
+
output_dtype: torch.dtype = torch.float32
|
|
91
|
+
) -> torch.Tensor:
|
|
92
|
+
r"""
|
|
93
|
+
Compute beta \mathcal{A}(i-1/j) * P * K^T.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
k (torch.Tensor):
|
|
97
|
+
The key tensor of shape `[B, T, H, K]`.
|
|
98
|
+
p (torch.Tensor):
|
|
99
|
+
The auxiliary key tensor of shape `[B, T, H, K]`.
|
|
100
|
+
beta (torch.Tensor):
|
|
101
|
+
The beta tensor of shape `[B, T, H]`.
|
|
102
|
+
g0 (torch.Tensor):
|
|
103
|
+
The cumulative sum minus the original one of the gate tensor of shape `[B, T, H]`.
|
|
104
|
+
Default: None
|
|
105
|
+
g (torch.Tensor):
|
|
106
|
+
The cumulative sum of the gate tensor of shape `[B, T, H]`.
|
|
107
|
+
Default: None
|
|
108
|
+
cu_seqlens (torch.LongTensor):
|
|
109
|
+
The cumulative sequence lengths of the input tensor.
|
|
110
|
+
Default: None
|
|
111
|
+
chunk_size (int):
|
|
112
|
+
The chunk size. Default: 64.
|
|
113
|
+
output_dtype (torch.dtype):
|
|
114
|
+
The dtype of the output tensor. Default: `torch.float32`
|
|
115
|
+
|
|
116
|
+
Returns:
|
|
117
|
+
beta * K * K^T of shape `[B, T, H, BT]` where `BT` is the chunk size.
|
|
118
|
+
"""
|
|
119
|
+
B, T, H, K = k.shape
|
|
120
|
+
BT = chunk_size
|
|
121
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
122
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
123
|
+
A = torch.empty(B, T, H, BT, device=k.device, dtype=output_dtype)
|
|
124
|
+
chunk_scaled_dot_comba_pkt_fwd_kernel[(NT, B * H)](
|
|
125
|
+
k=k,
|
|
126
|
+
p=p,
|
|
127
|
+
beta=beta,
|
|
128
|
+
g0=g0,
|
|
129
|
+
g=g,
|
|
130
|
+
A=A,
|
|
131
|
+
cu_seqlens=cu_seqlens,
|
|
132
|
+
chunk_indices=chunk_indices,
|
|
133
|
+
T=T,
|
|
134
|
+
H=H,
|
|
135
|
+
K=K,
|
|
136
|
+
BT=BT,
|
|
137
|
+
)
|
|
138
|
+
return A
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@triton.heuristics({
|
|
142
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
143
|
+
})
|
|
144
|
+
@triton.autotune(
|
|
145
|
+
configs=[
|
|
146
|
+
triton.Config({}, num_warps=num_warps, num_stages=num_stages)
|
|
147
|
+
for num_warps in [2, 4]
|
|
148
|
+
for num_stages in [2, 3, 4]
|
|
149
|
+
],
|
|
150
|
+
key=['H', 'K', 'V', 'BT', 'BK', 'BV', 'IS_VARLEN']
|
|
151
|
+
)
|
|
152
|
+
@triton.jit(do_not_specialize=['T'])
|
|
153
|
+
def prepare_wy_repr_bwd_kernel(
|
|
154
|
+
k,
|
|
155
|
+
v,
|
|
156
|
+
p,
|
|
157
|
+
beta,
|
|
158
|
+
g0,
|
|
159
|
+
g,
|
|
160
|
+
A,
|
|
161
|
+
dw,
|
|
162
|
+
du,
|
|
163
|
+
dk,
|
|
164
|
+
dv,
|
|
165
|
+
dp,
|
|
166
|
+
dbeta,
|
|
167
|
+
dg0,
|
|
168
|
+
dg,
|
|
169
|
+
cu_seqlens,
|
|
170
|
+
chunk_indices,
|
|
171
|
+
T,
|
|
172
|
+
H: tl.constexpr,
|
|
173
|
+
K: tl.constexpr,
|
|
174
|
+
V: tl.constexpr,
|
|
175
|
+
BT: tl.constexpr,
|
|
176
|
+
BK: tl.constexpr,
|
|
177
|
+
BV: tl.constexpr,
|
|
178
|
+
IS_VARLEN: tl.constexpr
|
|
179
|
+
):
|
|
180
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
181
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
182
|
+
if IS_VARLEN:
|
|
183
|
+
i_n, i_t = tl.load(chunk_indices + i_t * 2).to(tl.int32), tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32)
|
|
184
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
185
|
+
T = eos - bos
|
|
186
|
+
else:
|
|
187
|
+
bos, eos = i_b * T, i_b * T + T
|
|
188
|
+
|
|
189
|
+
p_beta = tl.make_block_ptr(beta + (bos*H + i_h), (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
190
|
+
p_g0 = tl.make_block_ptr(g0 + (bos*H + i_h), (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
191
|
+
p_g = tl.make_block_ptr(g + (bos*H + i_h), (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
192
|
+
p_A = tl.make_block_ptr(A + (bos*H + i_h) * BT, (BT, T), (1, H*BT), (0, i_t * BT), (BT, BT), (0, 1))
|
|
193
|
+
|
|
194
|
+
b_A = tl.load(p_A, boundary_check=(0, 1))
|
|
195
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
196
|
+
b_g0 = tl.load(p_g0, boundary_check=(0,))
|
|
197
|
+
b_g0_exp = tl.exp(b_g0)
|
|
198
|
+
b_g = tl.load(p_g, boundary_check=(0,))
|
|
199
|
+
|
|
200
|
+
b_dbeta = tl.zeros([BT], dtype=tl.float32)
|
|
201
|
+
b_dA = tl.zeros([BT, BT], dtype=tl.float32)
|
|
202
|
+
b_dg0 = tl.zeros([BT], dtype=tl.float32)
|
|
203
|
+
|
|
204
|
+
for i_k in range(tl.cdiv(K, BK)):
|
|
205
|
+
p_p = tl.make_block_ptr(p + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
206
|
+
p_dp = tl.make_block_ptr(dp + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
207
|
+
p_dw = tl.make_block_ptr(dw + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
208
|
+
b_p = tl.load(p_p, boundary_check=(0, 1))
|
|
209
|
+
b_p_beta_g0 = (b_p * b_beta[:, None] * b_g0_exp[:, None]).to(b_p.dtype)
|
|
210
|
+
b_dw = tl.load(p_dw, boundary_check=(0, 1))
|
|
211
|
+
b_dA += tl.dot(b_dw, tl.trans(b_p_beta_g0))
|
|
212
|
+
b_dp_beta_g0 = tl.dot(b_A, b_dw)
|
|
213
|
+
b_dp = b_dp_beta_g0 * b_beta[:, None] * b_g0_exp[:, None]
|
|
214
|
+
b_dbeta += tl.sum(b_dp_beta_g0 * b_p * b_g0_exp[:, None], 1)
|
|
215
|
+
b_dg0 += tl.sum(b_dp * b_p, 1)
|
|
216
|
+
tl.store(p_dp, b_dp.to(p_dp.dtype.element_ty), boundary_check=(0, 1))
|
|
217
|
+
|
|
218
|
+
for i_v in range(tl.cdiv(V, BV)):
|
|
219
|
+
p_v = tl.make_block_ptr(v + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
|
220
|
+
p_dv = tl.make_block_ptr(dv + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
|
221
|
+
p_du = tl.make_block_ptr(du + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
|
222
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
223
|
+
b_v_beta = (b_v * b_beta[:, None]).to(b_v.dtype)
|
|
224
|
+
b_du = tl.load(p_du, boundary_check=(0, 1))
|
|
225
|
+
b_dA += tl.dot(b_du, tl.trans(b_v_beta))
|
|
226
|
+
b_dv_beta = tl.dot(b_A, b_du)
|
|
227
|
+
b_dv = b_dv_beta * b_beta[:, None]
|
|
228
|
+
b_dbeta += tl.sum(b_dv_beta * b_v, 1)
|
|
229
|
+
tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
|
|
230
|
+
|
|
231
|
+
o_t = i_t * BT + tl.arange(0, BT)
|
|
232
|
+
m_t = o_t < T
|
|
233
|
+
m_A = (o_t[:, None] > o_t[None, :]) & (m_t[:, None] & m_t)
|
|
234
|
+
b_dA = tl.where(m_A, b_dA, 0)
|
|
235
|
+
b_dA = tl.dot(b_dA.to(b_A.dtype), b_A)
|
|
236
|
+
b_dA = tl.dot(b_A, b_dA.to(b_A.dtype))
|
|
237
|
+
b_dA = tl.where(m_A, -b_dA * exp(b_g0[:, None] - b_g[None, :]), 0).to(k.dtype.element_ty)
|
|
238
|
+
b_dA = b_dA.to(k.dtype.element_ty)
|
|
239
|
+
b_A = tl.zeros([BT, BT], dtype=tl.float32)
|
|
240
|
+
|
|
241
|
+
for i_k in range(tl.cdiv(K, BK)):
|
|
242
|
+
p_k = tl.make_block_ptr(k + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
243
|
+
p_p = tl.make_block_ptr(p + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
244
|
+
p_dk = tl.make_block_ptr(dk + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
245
|
+
p_dp = tl.make_block_ptr(dp + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
246
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
247
|
+
b_p = tl.load(p_p, boundary_check=(0, 1))
|
|
248
|
+
b_dp = tl.load(p_dp, boundary_check=(0, 1))
|
|
249
|
+
b_p_beta = (b_p * b_beta[:, None]).to(b_p.dtype)
|
|
250
|
+
b_A += tl.dot(b_p_beta, tl.trans(b_k))
|
|
251
|
+
b_dp_beta = tl.dot(b_dA, b_k)
|
|
252
|
+
b_dbeta += tl.sum(b_dp_beta * b_p, 1)
|
|
253
|
+
b_dk = tl.dot(tl.trans(b_dA), b_p_beta)
|
|
254
|
+
b_dp += b_dp_beta * b_beta[:, None]
|
|
255
|
+
tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
|
|
256
|
+
tl.store(p_dp, b_dp.to(p_dp.dtype.element_ty), boundary_check=(0, 1))
|
|
257
|
+
|
|
258
|
+
b_dA_A = b_dA * b_A
|
|
259
|
+
b_dg0 += tl.sum(b_dA_A, axis=1)
|
|
260
|
+
b_dg = - tl.sum(b_dA_A, axis=0)
|
|
261
|
+
p_dg = tl.make_block_ptr(dg + (bos*H + i_h), (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
262
|
+
p_dg0 = tl.make_block_ptr(dg0 + (bos*H + i_h), (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
263
|
+
p_dbeta = tl.make_block_ptr(dbeta + (bos*H + i_h), (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
264
|
+
tl.store(p_dg, b_dg.to(p_dg.dtype.element_ty), boundary_check=(0,))
|
|
265
|
+
tl.store(p_dg0, b_dg0.to(p_dg0.dtype.element_ty), boundary_check=(0,))
|
|
266
|
+
tl.store(p_dbeta, b_dbeta.to(p_dbeta.dtype.element_ty), boundary_check=(0,))
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
@triton.heuristics({
|
|
270
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
271
|
+
})
|
|
272
|
+
@triton.autotune(
|
|
273
|
+
configs=[
|
|
274
|
+
triton.Config({}, num_warps=num_warps, num_stages=num_stages)
|
|
275
|
+
for num_warps in [2, 4, 8]
|
|
276
|
+
for num_stages in [2, 3, 4]
|
|
277
|
+
],
|
|
278
|
+
key=['H', 'K', 'V', 'BT', 'BK', 'BV', 'IS_VARLEN'],
|
|
279
|
+
)
|
|
280
|
+
@triton.jit(do_not_specialize=['T'])
|
|
281
|
+
def recompute_w_u_fwd_kernel(
|
|
282
|
+
k,
|
|
283
|
+
v,
|
|
284
|
+
beta,
|
|
285
|
+
w,
|
|
286
|
+
u,
|
|
287
|
+
A,
|
|
288
|
+
g,
|
|
289
|
+
cu_seqlens,
|
|
290
|
+
chunk_indices,
|
|
291
|
+
T,
|
|
292
|
+
H: tl.constexpr,
|
|
293
|
+
K: tl.constexpr,
|
|
294
|
+
V: tl.constexpr,
|
|
295
|
+
BT: tl.constexpr,
|
|
296
|
+
BK: tl.constexpr,
|
|
297
|
+
BV: tl.constexpr,
|
|
298
|
+
IS_VARLEN: tl.constexpr
|
|
299
|
+
):
|
|
300
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
301
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
302
|
+
if IS_VARLEN:
|
|
303
|
+
i_n, i_t = tl.load(chunk_indices + i_t * 2).to(tl.int32), tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32)
|
|
304
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
305
|
+
T = eos - bos
|
|
306
|
+
else:
|
|
307
|
+
bos, eos = i_b * T, i_b * T + T
|
|
308
|
+
p_beta = tl.make_block_ptr(beta + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
309
|
+
p_g = tl.make_block_ptr(g + (bos*H + i_h), (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
310
|
+
p_A = tl.make_block_ptr(A + (bos*H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
|
|
311
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
312
|
+
b_A = tl.load(p_A, boundary_check=(0, 1))
|
|
313
|
+
b_g = tl.exp(tl.load(p_g, boundary_check=(0,)))
|
|
314
|
+
|
|
315
|
+
for i_v in range(tl.cdiv(V, BV)):
|
|
316
|
+
p_v = tl.make_block_ptr(v + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
|
317
|
+
p_u = tl.make_block_ptr(u + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
|
318
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
319
|
+
b_vb = (b_v * b_beta[:, None]).to(b_v.dtype)
|
|
320
|
+
b_u = tl.dot(b_A, b_vb, allow_tf32=False)
|
|
321
|
+
tl.store(p_u, b_u.to(p_u.dtype.element_ty), boundary_check=(0, 1))
|
|
322
|
+
|
|
323
|
+
for i_k in range(tl.cdiv(K, BK)):
|
|
324
|
+
p_k = tl.make_block_ptr(k + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
325
|
+
p_w = tl.make_block_ptr(w + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
326
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
327
|
+
b_kb = (b_k * b_beta[:, None] * b_g[:, None]).to(b_k.dtype)
|
|
328
|
+
b_w = tl.dot(b_A, b_kb)
|
|
329
|
+
tl.store(p_w, b_w.to(p_w.dtype.element_ty), boundary_check=(0, 1))
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def recompute_w_u_fwd(
|
|
333
|
+
k: torch.Tensor,
|
|
334
|
+
v: torch.Tensor,
|
|
335
|
+
beta: torch.Tensor,
|
|
336
|
+
g_cumsum: torch.Tensor,
|
|
337
|
+
A: torch.Tensor,
|
|
338
|
+
cu_seqlens: Optional[torch.LongTensor],
|
|
339
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
340
|
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
|
341
|
+
BT = A.shape[-1]
|
|
342
|
+
|
|
343
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
344
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
345
|
+
BK = 64
|
|
346
|
+
BV = 64
|
|
347
|
+
|
|
348
|
+
u = torch.empty_like(v)
|
|
349
|
+
w = torch.empty_like(k)
|
|
350
|
+
recompute_w_u_fwd_kernel[(NT, B*H)](
|
|
351
|
+
k=k,
|
|
352
|
+
v=v,
|
|
353
|
+
beta=beta,
|
|
354
|
+
w=w,
|
|
355
|
+
u=u,
|
|
356
|
+
A=A,
|
|
357
|
+
g=g_cumsum,
|
|
358
|
+
cu_seqlens=cu_seqlens,
|
|
359
|
+
chunk_indices=chunk_indices,
|
|
360
|
+
T=T,
|
|
361
|
+
H=H,
|
|
362
|
+
K=K,
|
|
363
|
+
V=V,
|
|
364
|
+
BT=BT,
|
|
365
|
+
BK=BK,
|
|
366
|
+
BV=BV,
|
|
367
|
+
)
|
|
368
|
+
return w, u
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def prepare_wy_repr_bwd(
|
|
372
|
+
k: torch.Tensor,
|
|
373
|
+
v: torch.Tensor,
|
|
374
|
+
p: torch.Tensor,
|
|
375
|
+
g0: torch.Tensor,
|
|
376
|
+
g: torch.Tensor,
|
|
377
|
+
beta: torch.Tensor,
|
|
378
|
+
A: torch.Tensor,
|
|
379
|
+
dw: torch.Tensor,
|
|
380
|
+
du: torch.Tensor,
|
|
381
|
+
cu_seqlens: Optional[torch.LongTensor],
|
|
382
|
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
383
|
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
|
384
|
+
BT = 64
|
|
385
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
386
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
387
|
+
CONST_TILING = 64 if check_shared_mem() else 32
|
|
388
|
+
BK = min(max(triton.next_power_of_2(K), 16), CONST_TILING)
|
|
389
|
+
BV = min(max(triton.next_power_of_2(V), 16), CONST_TILING)
|
|
390
|
+
|
|
391
|
+
dk = torch.empty_like(k)
|
|
392
|
+
dv = torch.empty_like(v)
|
|
393
|
+
dp = torch.empty_like(p)
|
|
394
|
+
dbeta = torch.empty_like(beta)
|
|
395
|
+
dg0 = torch.empty_like(g0)
|
|
396
|
+
dg = torch.empty_like(g)
|
|
397
|
+
prepare_wy_repr_bwd_kernel[(NT, B * H)](
|
|
398
|
+
k=k,
|
|
399
|
+
v=v,
|
|
400
|
+
p=p,
|
|
401
|
+
beta=beta,
|
|
402
|
+
g0=g0,
|
|
403
|
+
g=g,
|
|
404
|
+
A=A,
|
|
405
|
+
dw=dw,
|
|
406
|
+
du=du,
|
|
407
|
+
dk=dk,
|
|
408
|
+
dv=dv,
|
|
409
|
+
dp=dp,
|
|
410
|
+
dbeta=dbeta,
|
|
411
|
+
dg0=dg0,
|
|
412
|
+
dg=dg,
|
|
413
|
+
cu_seqlens=cu_seqlens,
|
|
414
|
+
chunk_indices=chunk_indices,
|
|
415
|
+
T=T,
|
|
416
|
+
H=H,
|
|
417
|
+
K=K,
|
|
418
|
+
V=V,
|
|
419
|
+
BT=BT,
|
|
420
|
+
BK=BK,
|
|
421
|
+
BV=BV,
|
|
422
|
+
)
|
|
423
|
+
return dk, dv, dp, dbeta, dg0, dg
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|