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/ttt/naive.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang, Yuqi Pan
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
import torch.nn.functional as F
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def ttt_linear(
|
|
9
|
+
q: torch.Tensor,
|
|
10
|
+
k: torch.Tensor,
|
|
11
|
+
v: torch.Tensor,
|
|
12
|
+
w: torch.Tensor,
|
|
13
|
+
b: torch.Tensor,
|
|
14
|
+
eta: torch.Tensor,
|
|
15
|
+
scale: float,
|
|
16
|
+
eps: float,
|
|
17
|
+
mini_batch_size: int,
|
|
18
|
+
initial_state: torch.Tensor,
|
|
19
|
+
initial_state_bias: torch.Tensor,
|
|
20
|
+
output_final_state: bool
|
|
21
|
+
):
|
|
22
|
+
B, H, T, D = q.shape
|
|
23
|
+
BT = mini_batch_size
|
|
24
|
+
NT = T // BT
|
|
25
|
+
# [NT, B, H, mini_batch_size, D]
|
|
26
|
+
_q = q.reshape(B, H, NT, BT, D).permute(2, 0, 1, 3, 4)
|
|
27
|
+
_k = k.reshape(B, H, NT, BT, D).permute(2, 0, 1, 3, 4)
|
|
28
|
+
_v = v.reshape(B, H, NT, BT, D).permute(2, 0, 1, 3, 4)
|
|
29
|
+
# [NT, B, H, BT, 1]
|
|
30
|
+
_eta = eta.reshape(B, H, NT, BT, 1).permute(2, 0, 1, 3, 4)
|
|
31
|
+
# [H, 1, D]
|
|
32
|
+
w = w.reshape(H, 1, D).to(torch.float32)
|
|
33
|
+
b = b.reshape(H, 1, D).to(torch.float32)
|
|
34
|
+
|
|
35
|
+
h = torch.zeros((B, H, D, D), device=v.device, dtype=torch.float32) if initial_state is None else initial_state
|
|
36
|
+
hb = torch.zeros((B, H, 1, D), device=v.device, dtype=torch.float32) if initial_state_bias is None else initial_state_bias
|
|
37
|
+
q *= scale
|
|
38
|
+
# [NT, B, H, BT, D]
|
|
39
|
+
o = torch.empty_like(_v)
|
|
40
|
+
|
|
41
|
+
for i in range(NT):
|
|
42
|
+
q_i, k_i, v_i, eta_i = [x[i] for x in [_q, _k, _v, _eta]]
|
|
43
|
+
kh = k_i @ h + hb
|
|
44
|
+
reconstruction_target = v_i - k_i
|
|
45
|
+
|
|
46
|
+
mean = kh.mean(-1, True)
|
|
47
|
+
var = kh.var(-1, unbiased=False, keepdim=True).to(torch.float32)
|
|
48
|
+
rstd = torch.sqrt(var + eps).to(torch.float32)
|
|
49
|
+
kh_hat = (kh - mean) / rstd
|
|
50
|
+
|
|
51
|
+
g = w * kh_hat + b - reconstruction_target
|
|
52
|
+
g *= w
|
|
53
|
+
v_new = (D * g - g.sum(-1, True) - kh_hat * (g * kh_hat).sum(-1, True)) / (rstd * D)
|
|
54
|
+
|
|
55
|
+
Attn = torch.tril(q_i @ k_i.transpose(-2, -1))
|
|
56
|
+
o_i = q_i @ h - (eta_i * Attn) @ v_new + hb - torch.tril(eta_i.expand_as(Attn)) @ v_new
|
|
57
|
+
h = h - (eta_i[:, :, -1, :, None] * k_i).transpose(-1, -2) @ v_new
|
|
58
|
+
hb = hb - torch.sum(eta_i[:, :, -1, :, None] * v_new, dim=-2, keepdim=True)
|
|
59
|
+
# layer norm with residuals
|
|
60
|
+
|
|
61
|
+
mean = o_i.mean(dim=-1, keepdim=True)
|
|
62
|
+
var = o_i.var(dim=-1, unbiased=False, keepdim=True).to(torch.float32)
|
|
63
|
+
rstd = torch.sqrt(var + eps).to(torch.float32)
|
|
64
|
+
o[i] = o_i + (o_i - mean) / rstd * w + b
|
|
65
|
+
|
|
66
|
+
# [B, H, T, D]
|
|
67
|
+
o = o.permute(1, 2, 0, 3, 4).reshape(B, H, T, D)
|
|
68
|
+
h = h if output_final_state else None
|
|
69
|
+
hb = hb if output_final_state else None
|
|
70
|
+
return o, h, hb
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def chunk_ttt_linear_ref(
|
|
74
|
+
q: torch.Tensor,
|
|
75
|
+
k: torch.Tensor,
|
|
76
|
+
v: torch.Tensor,
|
|
77
|
+
w: torch.Tensor,
|
|
78
|
+
b: torch.Tensor,
|
|
79
|
+
eta: torch.Tensor,
|
|
80
|
+
scale: float = None,
|
|
81
|
+
eps: float = 1e-6,
|
|
82
|
+
mini_batch_size: int = 16,
|
|
83
|
+
initial_state: torch.Tensor = None,
|
|
84
|
+
initial_state_bias: torch.Tensor = None,
|
|
85
|
+
output_final_state: bool = False,
|
|
86
|
+
head_first: bool = False,
|
|
87
|
+
):
|
|
88
|
+
assert q.dtype == k.dtype == v.dtype
|
|
89
|
+
assert k.shape[-1] == v.shape[-1], "The key and value dimension must be the same."
|
|
90
|
+
if isinstance(eta, float):
|
|
91
|
+
eta = torch.full_like(q[:, :, :, :1], eta)
|
|
92
|
+
if scale is None:
|
|
93
|
+
scale = k.shape[-1] ** -0.5
|
|
94
|
+
if not head_first:
|
|
95
|
+
q = q.transpose(1, 2)
|
|
96
|
+
k = k.transpose(1, 2)
|
|
97
|
+
v = v.transpose(1, 2)
|
|
98
|
+
eta = eta.transpose(1, 2)
|
|
99
|
+
T = q.shape[-2]
|
|
100
|
+
padded = (mini_batch_size - (T % mini_batch_size)) % mini_batch_size
|
|
101
|
+
if padded > 0:
|
|
102
|
+
q = F.pad(q, (0, 0, 0, padded))
|
|
103
|
+
k = F.pad(k, (0, 0, 0, padded))
|
|
104
|
+
v = F.pad(v, (0, 0, 0, padded))
|
|
105
|
+
eta = F.pad(eta, (0, 0, 0, padded))
|
|
106
|
+
eta[:, :, -1, :] = eta[:, :, -(padded+1), :]
|
|
107
|
+
assert q.shape[-2] % mini_batch_size == 0, "Sequence length should be a multiple of mini_batch_size."
|
|
108
|
+
q, k, v, eta, w, b = map(lambda x: x.to(torch.float32), [q, k, v, eta, w, b])
|
|
109
|
+
o, final_state, final_state_bias = ttt_linear(
|
|
110
|
+
q,
|
|
111
|
+
k,
|
|
112
|
+
v,
|
|
113
|
+
w,
|
|
114
|
+
b,
|
|
115
|
+
eta,
|
|
116
|
+
scale,
|
|
117
|
+
eps,
|
|
118
|
+
mini_batch_size,
|
|
119
|
+
initial_state,
|
|
120
|
+
initial_state_bias,
|
|
121
|
+
output_final_state,
|
|
122
|
+
)
|
|
123
|
+
o = o[:, :, :T, :].contiguous()
|
|
124
|
+
if not head_first:
|
|
125
|
+
o = o.transpose(1, 2)
|
|
126
|
+
return o, final_state, final_state_bias
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .cumsum import (
|
|
4
|
+
chunk_global_cumsum,
|
|
5
|
+
chunk_global_cumsum_scalar,
|
|
6
|
+
chunk_global_cumsum_vector,
|
|
7
|
+
chunk_local_cumsum,
|
|
8
|
+
chunk_local_cumsum_scalar,
|
|
9
|
+
chunk_local_cumsum_vector
|
|
10
|
+
)
|
|
11
|
+
from .index import (
|
|
12
|
+
get_max_num_splits,
|
|
13
|
+
prepare_chunk_indices,
|
|
14
|
+
prepare_chunk_offsets,
|
|
15
|
+
prepare_cu_seqlens_from_lens,
|
|
16
|
+
prepare_cu_seqlens_from_mask,
|
|
17
|
+
prepare_lens,
|
|
18
|
+
prepare_lens_from_cu_seqlens,
|
|
19
|
+
prepare_lens_from_mask,
|
|
20
|
+
prepare_position_ids,
|
|
21
|
+
prepare_sequence_ids,
|
|
22
|
+
prepare_token_indices
|
|
23
|
+
)
|
|
24
|
+
from .logsumexp import logsumexp_fwd
|
|
25
|
+
from .matmul import addmm, matmul
|
|
26
|
+
from .pack import pack_sequence, unpack_sequence
|
|
27
|
+
from .pooling import mean_pooling
|
|
28
|
+
from .softmax import softmax_bwd, softmax_fwd
|
|
29
|
+
from .solve_tril import solve_tril
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
'chunk_global_cumsum',
|
|
33
|
+
'chunk_global_cumsum_scalar',
|
|
34
|
+
'chunk_global_cumsum_vector',
|
|
35
|
+
'chunk_local_cumsum',
|
|
36
|
+
'chunk_local_cumsum_scalar',
|
|
37
|
+
'chunk_local_cumsum_vector',
|
|
38
|
+
'pack_sequence',
|
|
39
|
+
'unpack_sequence',
|
|
40
|
+
'prepare_chunk_indices',
|
|
41
|
+
'prepare_chunk_offsets',
|
|
42
|
+
'prepare_cu_seqlens_from_lens',
|
|
43
|
+
'prepare_cu_seqlens_from_mask',
|
|
44
|
+
'prepare_lens',
|
|
45
|
+
'prepare_lens_from_cu_seqlens',
|
|
46
|
+
'prepare_lens_from_mask',
|
|
47
|
+
'prepare_position_ids',
|
|
48
|
+
'prepare_sequence_ids',
|
|
49
|
+
'prepare_token_indices',
|
|
50
|
+
'logsumexp_fwd',
|
|
51
|
+
'addmm',
|
|
52
|
+
'matmul',
|
|
53
|
+
'mean_pooling',
|
|
54
|
+
'softmax_bwd',
|
|
55
|
+
'softmax_fwd',
|
|
56
|
+
'solve_tril',
|
|
57
|
+
'get_max_num_splits'
|
|
58
|
+
]
|
fla/ops/utils/cumsum.py
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
import triton
|
|
8
|
+
import triton.language as tl
|
|
9
|
+
|
|
10
|
+
from fla.ops.utils.index import prepare_chunk_indices
|
|
11
|
+
from fla.utils import check_shared_mem, input_guard
|
|
12
|
+
|
|
13
|
+
BS_LIST = [32, 64] if check_shared_mem() else [16, 32]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@triton.heuristics({
|
|
17
|
+
'HAS_SCALE': lambda args: args['scale'] is not None,
|
|
18
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
19
|
+
})
|
|
20
|
+
@triton.autotune(
|
|
21
|
+
configs=[
|
|
22
|
+
triton.Config({}, num_warps=num_warps)
|
|
23
|
+
for num_warps in [1, 2, 4, 8]
|
|
24
|
+
],
|
|
25
|
+
key=['B', 'H', 'BT', 'IS_VARLEN', 'REVERSE']
|
|
26
|
+
)
|
|
27
|
+
@triton.jit(do_not_specialize=['T'])
|
|
28
|
+
def chunk_local_cumsum_scalar_kernel(
|
|
29
|
+
s,
|
|
30
|
+
o,
|
|
31
|
+
scale,
|
|
32
|
+
cu_seqlens,
|
|
33
|
+
chunk_indices,
|
|
34
|
+
T,
|
|
35
|
+
B: tl.constexpr,
|
|
36
|
+
H: tl.constexpr,
|
|
37
|
+
BT: tl.constexpr,
|
|
38
|
+
REVERSE: tl.constexpr,
|
|
39
|
+
HAS_SCALE: tl.constexpr,
|
|
40
|
+
IS_VARLEN: tl.constexpr,
|
|
41
|
+
HEAD_FIRST: tl.constexpr,
|
|
42
|
+
):
|
|
43
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
44
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
45
|
+
if IS_VARLEN:
|
|
46
|
+
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)
|
|
47
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
48
|
+
T = eos - bos
|
|
49
|
+
else:
|
|
50
|
+
bos, eos = i_b * T, i_b * T + T
|
|
51
|
+
|
|
52
|
+
if HEAD_FIRST:
|
|
53
|
+
p_s = tl.make_block_ptr(s + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
54
|
+
p_o = tl.make_block_ptr(o + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
55
|
+
else:
|
|
56
|
+
p_s = tl.make_block_ptr(s + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
57
|
+
p_o = tl.make_block_ptr(o + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
58
|
+
# [BT]
|
|
59
|
+
b_s = tl.load(p_s, boundary_check=(0,)).to(tl.float32)
|
|
60
|
+
b_o = tl.cumsum(b_s, axis=0)
|
|
61
|
+
if REVERSE:
|
|
62
|
+
b_z = tl.sum(b_s, axis=0)
|
|
63
|
+
b_o = -b_o + b_z[None] + b_s
|
|
64
|
+
if HAS_SCALE:
|
|
65
|
+
b_o *= scale
|
|
66
|
+
tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0,))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@triton.heuristics({
|
|
70
|
+
'HAS_SCALE': lambda args: args['scale'] is not None,
|
|
71
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
72
|
+
})
|
|
73
|
+
@triton.autotune(
|
|
74
|
+
configs=[
|
|
75
|
+
triton.Config({'BS': BS}, num_warps=num_warps)
|
|
76
|
+
for BS in BS_LIST
|
|
77
|
+
for num_warps in [2, 4, 8]
|
|
78
|
+
],
|
|
79
|
+
key=['B', 'H', 'S', 'BT', 'IS_VARLEN', 'REVERSE']
|
|
80
|
+
)
|
|
81
|
+
@triton.jit(do_not_specialize=['T'])
|
|
82
|
+
def chunk_local_cumsum_vector_kernel(
|
|
83
|
+
s,
|
|
84
|
+
o,
|
|
85
|
+
scale,
|
|
86
|
+
cu_seqlens,
|
|
87
|
+
chunk_indices,
|
|
88
|
+
T,
|
|
89
|
+
B: tl.constexpr,
|
|
90
|
+
H: tl.constexpr,
|
|
91
|
+
S: tl.constexpr,
|
|
92
|
+
BT: tl.constexpr,
|
|
93
|
+
BS: tl.constexpr,
|
|
94
|
+
REVERSE: tl.constexpr,
|
|
95
|
+
HAS_SCALE: tl.constexpr,
|
|
96
|
+
IS_VARLEN: tl.constexpr,
|
|
97
|
+
HEAD_FIRST: tl.constexpr,
|
|
98
|
+
):
|
|
99
|
+
i_s, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
100
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
101
|
+
if IS_VARLEN:
|
|
102
|
+
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)
|
|
103
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
104
|
+
T = eos - bos
|
|
105
|
+
else:
|
|
106
|
+
bos, eos = i_b * T, i_b * T + T
|
|
107
|
+
|
|
108
|
+
o_i = tl.arange(0, BT)
|
|
109
|
+
if REVERSE:
|
|
110
|
+
m_s = tl.where(o_i[:, None] <= o_i[None, :], 1., 0.)
|
|
111
|
+
else:
|
|
112
|
+
m_s = tl.where(o_i[:, None] >= o_i[None, :], 1., 0.)
|
|
113
|
+
|
|
114
|
+
if HEAD_FIRST:
|
|
115
|
+
p_s = tl.make_block_ptr(s + (bos * H + i_h*T)*S, (T, S), (S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0))
|
|
116
|
+
p_o = tl.make_block_ptr(o + (bos * H + i_h*T)*S, (T, S), (S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0))
|
|
117
|
+
else:
|
|
118
|
+
p_s = tl.make_block_ptr(s + (bos * H + i_h) * S, (T, S), (H*S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0))
|
|
119
|
+
p_o = tl.make_block_ptr(o + (bos * H + i_h) * S, (T, S), (H*S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0))
|
|
120
|
+
# [BT, BS]
|
|
121
|
+
b_s = tl.load(p_s, boundary_check=(0, 1)).to(tl.float32)
|
|
122
|
+
b_o = tl.dot(m_s, b_s, allow_tf32=False)
|
|
123
|
+
if HAS_SCALE:
|
|
124
|
+
b_o *= scale
|
|
125
|
+
tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@triton.heuristics({
|
|
129
|
+
'HAS_SCALE': lambda args: args['scale'] is not None,
|
|
130
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
131
|
+
})
|
|
132
|
+
@triton.autotune(
|
|
133
|
+
configs=[
|
|
134
|
+
triton.Config({'BT': BT}, num_warps=num_warps, num_stages=num_stages)
|
|
135
|
+
for BT in [32, 64, 128, 256]
|
|
136
|
+
for num_warps in [2, 4, 8]
|
|
137
|
+
for num_stages in [1, 2, 3, 4]
|
|
138
|
+
],
|
|
139
|
+
key=['B', 'H', 'IS_VARLEN', 'REVERSE']
|
|
140
|
+
)
|
|
141
|
+
@triton.jit(do_not_specialize=['T'])
|
|
142
|
+
def chunk_global_cumsum_scalar_kernel(
|
|
143
|
+
s,
|
|
144
|
+
o,
|
|
145
|
+
scale,
|
|
146
|
+
cu_seqlens,
|
|
147
|
+
T,
|
|
148
|
+
B: tl.constexpr,
|
|
149
|
+
H: tl.constexpr,
|
|
150
|
+
BT: tl.constexpr,
|
|
151
|
+
REVERSE: tl.constexpr,
|
|
152
|
+
HAS_SCALE: tl.constexpr,
|
|
153
|
+
IS_VARLEN: tl.constexpr,
|
|
154
|
+
HEAD_FIRST: tl.constexpr,
|
|
155
|
+
):
|
|
156
|
+
i_nh = tl.program_id(0)
|
|
157
|
+
i_n, i_h = i_nh // H, i_nh % H
|
|
158
|
+
if IS_VARLEN:
|
|
159
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
160
|
+
else:
|
|
161
|
+
bos, eos = i_n * T, i_n * T + T
|
|
162
|
+
T = eos - bos
|
|
163
|
+
|
|
164
|
+
b_z = tl.zeros([], dtype=tl.float32)
|
|
165
|
+
NT = tl.cdiv(T, BT)
|
|
166
|
+
for i_c in range(NT):
|
|
167
|
+
i_t = NT - 1 - i_c if REVERSE else i_c
|
|
168
|
+
if HEAD_FIRST:
|
|
169
|
+
p_s = tl.make_block_ptr(s + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
170
|
+
p_o = tl.make_block_ptr(o + bos*H + i_h*T, (T,), (1,), (i_t * BT,), (BT,), (0,))
|
|
171
|
+
else:
|
|
172
|
+
p_s = tl.make_block_ptr(s + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
173
|
+
p_o = tl.make_block_ptr(o + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
174
|
+
b_s = tl.load(p_s, boundary_check=(0,)).to(tl.float32)
|
|
175
|
+
b_o = tl.cumsum(b_s, axis=0)
|
|
176
|
+
b_ss = tl.sum(b_s, 0)
|
|
177
|
+
if REVERSE:
|
|
178
|
+
b_o = -b_o + b_ss + b_s
|
|
179
|
+
b_o += b_z
|
|
180
|
+
if i_c >= 0:
|
|
181
|
+
b_z += b_ss
|
|
182
|
+
if HAS_SCALE:
|
|
183
|
+
b_o *= scale
|
|
184
|
+
tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0,))
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
@triton.heuristics({
|
|
188
|
+
'HAS_SCALE': lambda args: args['scale'] is not None,
|
|
189
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
|
|
190
|
+
})
|
|
191
|
+
@triton.autotune(
|
|
192
|
+
configs=[
|
|
193
|
+
triton.Config({'BT': BT}, num_warps=num_warps, num_stages=num_stages)
|
|
194
|
+
for BT in [16, 32, 64, 128]
|
|
195
|
+
for num_warps in [2, 4, 8]
|
|
196
|
+
for num_stages in [1, 2, 3, 4]
|
|
197
|
+
],
|
|
198
|
+
key=['B', 'H', 'S', 'IS_VARLEN', 'REVERSE']
|
|
199
|
+
)
|
|
200
|
+
@triton.jit(do_not_specialize=['T'])
|
|
201
|
+
def chunk_global_cumsum_vector_kernel(
|
|
202
|
+
s,
|
|
203
|
+
o,
|
|
204
|
+
scale,
|
|
205
|
+
cu_seqlens,
|
|
206
|
+
T,
|
|
207
|
+
B: tl.constexpr,
|
|
208
|
+
H: tl.constexpr,
|
|
209
|
+
S: tl.constexpr,
|
|
210
|
+
BT: tl.constexpr,
|
|
211
|
+
BS: tl.constexpr,
|
|
212
|
+
REVERSE: tl.constexpr,
|
|
213
|
+
HAS_SCALE: tl.constexpr,
|
|
214
|
+
IS_VARLEN: tl.constexpr,
|
|
215
|
+
HEAD_FIRST: tl.constexpr,
|
|
216
|
+
):
|
|
217
|
+
i_s, i_nh = tl.program_id(0), tl.program_id(1)
|
|
218
|
+
i_n, i_h = i_nh // H, i_nh % H
|
|
219
|
+
if IS_VARLEN:
|
|
220
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
221
|
+
else:
|
|
222
|
+
bos, eos = i_n * T, i_n * T + T
|
|
223
|
+
T = eos - bos
|
|
224
|
+
|
|
225
|
+
o_i = tl.arange(0, BT)
|
|
226
|
+
if REVERSE:
|
|
227
|
+
m_s = tl.where(o_i[:, None] <= o_i[None, :], 1., 0.)
|
|
228
|
+
else:
|
|
229
|
+
m_s = tl.where(o_i[:, None] >= o_i[None, :], 1., 0.)
|
|
230
|
+
|
|
231
|
+
b_z = tl.zeros([BS], dtype=tl.float32)
|
|
232
|
+
NT = tl.cdiv(T, BT)
|
|
233
|
+
for i_c in range(NT):
|
|
234
|
+
i_t = NT - 1 - i_c if REVERSE else i_c
|
|
235
|
+
if HEAD_FIRST:
|
|
236
|
+
p_s = tl.make_block_ptr(s + (bos * H + i_h*T)*S, (T, S), (S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0))
|
|
237
|
+
p_o = tl.make_block_ptr(o + (bos * H + i_h*T)*S, (T, S), (S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0))
|
|
238
|
+
else:
|
|
239
|
+
p_s = tl.make_block_ptr(s + (bos * H + i_h) * S, (T, S), (H*S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0))
|
|
240
|
+
p_o = tl.make_block_ptr(o + (bos * H + i_h) * S, (T, S), (H*S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0))
|
|
241
|
+
# [BT, BS]
|
|
242
|
+
b_s = tl.load(p_s, boundary_check=(0, 1)).to(tl.float32)
|
|
243
|
+
b_c = b_z[None, :] + tl.dot(m_s, b_s, allow_tf32=False)
|
|
244
|
+
if HAS_SCALE:
|
|
245
|
+
b_c *= scale
|
|
246
|
+
tl.store(p_o, b_c.to(p_o.dtype.element_ty), boundary_check=(0, 1))
|
|
247
|
+
b_z += tl.sum(b_s, 0)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def chunk_local_cumsum_scalar(
|
|
251
|
+
g: torch.Tensor,
|
|
252
|
+
chunk_size: int,
|
|
253
|
+
reverse: bool = False,
|
|
254
|
+
scale: float = None,
|
|
255
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
256
|
+
head_first: bool = False,
|
|
257
|
+
output_dtype: Optional[torch.dtype] = torch.float
|
|
258
|
+
) -> torch.Tensor:
|
|
259
|
+
if head_first:
|
|
260
|
+
B, H, T = g.shape
|
|
261
|
+
else:
|
|
262
|
+
B, T, H = g.shape
|
|
263
|
+
assert chunk_size == 2**(chunk_size.bit_length()-1), "chunk_size must be a power of 2"
|
|
264
|
+
BT = chunk_size
|
|
265
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
266
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
267
|
+
g_org, g = g, torch.empty_like(g, dtype=output_dtype or g.dtype)
|
|
268
|
+
grid = (NT, B * H)
|
|
269
|
+
chunk_local_cumsum_scalar_kernel[grid](
|
|
270
|
+
s=g_org,
|
|
271
|
+
o=g,
|
|
272
|
+
scale=scale,
|
|
273
|
+
cu_seqlens=cu_seqlens,
|
|
274
|
+
chunk_indices=chunk_indices,
|
|
275
|
+
T=T,
|
|
276
|
+
B=B,
|
|
277
|
+
H=H,
|
|
278
|
+
BT=BT,
|
|
279
|
+
HEAD_FIRST=head_first,
|
|
280
|
+
REVERSE=reverse
|
|
281
|
+
)
|
|
282
|
+
return g
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def chunk_local_cumsum_vector(
|
|
286
|
+
g: torch.Tensor,
|
|
287
|
+
chunk_size: int,
|
|
288
|
+
reverse: bool = False,
|
|
289
|
+
scale: float = None,
|
|
290
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
291
|
+
head_first: bool = False,
|
|
292
|
+
output_dtype: Optional[torch.dtype] = torch.float
|
|
293
|
+
) -> torch.Tensor:
|
|
294
|
+
if head_first:
|
|
295
|
+
B, H, T, S = g.shape
|
|
296
|
+
else:
|
|
297
|
+
B, T, H, S = g.shape
|
|
298
|
+
BT = chunk_size
|
|
299
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, chunk_size) if cu_seqlens is not None else None
|
|
300
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
301
|
+
assert chunk_size == 2**(chunk_size.bit_length()-1), "chunk_size must be a power of 2"
|
|
302
|
+
|
|
303
|
+
g_org, g = g, torch.empty_like(g, dtype=output_dtype or g.dtype)
|
|
304
|
+
def grid(meta): return (triton.cdiv(meta['S'], meta['BS']), NT, B * H)
|
|
305
|
+
# keep cummulative normalizer in fp32
|
|
306
|
+
# this kernel is equivalent to
|
|
307
|
+
# g = g.view(B, H, NT, BT, -1).cumsum(-2).view(B, H, T, -1)
|
|
308
|
+
chunk_local_cumsum_vector_kernel[grid](
|
|
309
|
+
s=g_org,
|
|
310
|
+
o=g,
|
|
311
|
+
scale=scale,
|
|
312
|
+
cu_seqlens=cu_seqlens,
|
|
313
|
+
chunk_indices=chunk_indices,
|
|
314
|
+
T=T,
|
|
315
|
+
B=B,
|
|
316
|
+
H=H,
|
|
317
|
+
S=S,
|
|
318
|
+
BT=BT,
|
|
319
|
+
HEAD_FIRST=head_first,
|
|
320
|
+
REVERSE=reverse
|
|
321
|
+
)
|
|
322
|
+
return g
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
@input_guard
|
|
326
|
+
def chunk_global_cumsum_scalar(
|
|
327
|
+
s: torch.Tensor,
|
|
328
|
+
reverse: bool = False,
|
|
329
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
330
|
+
scale: float = None,
|
|
331
|
+
head_first: bool = False,
|
|
332
|
+
output_dtype: Optional[torch.dtype] = torch.float
|
|
333
|
+
) -> torch.Tensor:
|
|
334
|
+
if head_first:
|
|
335
|
+
B, H, T = s.shape
|
|
336
|
+
else:
|
|
337
|
+
B, T, H = s.shape
|
|
338
|
+
N = len(cu_seqlens) - 1 if cu_seqlens is not None else B
|
|
339
|
+
|
|
340
|
+
z = torch.empty_like(s, dtype=output_dtype or s.dtype)
|
|
341
|
+
grid = (N * H,)
|
|
342
|
+
chunk_global_cumsum_scalar_kernel[grid](
|
|
343
|
+
s=s,
|
|
344
|
+
o=z,
|
|
345
|
+
scale=scale,
|
|
346
|
+
cu_seqlens=cu_seqlens,
|
|
347
|
+
T=T,
|
|
348
|
+
B=B,
|
|
349
|
+
H=H,
|
|
350
|
+
HEAD_FIRST=head_first,
|
|
351
|
+
REVERSE=reverse
|
|
352
|
+
)
|
|
353
|
+
return z
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
@input_guard
|
|
357
|
+
def chunk_global_cumsum_vector(
|
|
358
|
+
s: torch.Tensor,
|
|
359
|
+
reverse: bool = False,
|
|
360
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
361
|
+
scale: float = None,
|
|
362
|
+
head_first: bool = False,
|
|
363
|
+
output_dtype: Optional[torch.dtype] = torch.float
|
|
364
|
+
) -> torch.Tensor:
|
|
365
|
+
if head_first:
|
|
366
|
+
B, H, T, S = s.shape
|
|
367
|
+
else:
|
|
368
|
+
B, T, H, S = s.shape
|
|
369
|
+
N = len(cu_seqlens) - 1 if cu_seqlens is not None else B
|
|
370
|
+
BS = min(32, triton.next_power_of_2(S))
|
|
371
|
+
|
|
372
|
+
z = torch.empty_like(s, dtype=output_dtype or s.dtype)
|
|
373
|
+
grid = (triton.cdiv(S, BS), N * H)
|
|
374
|
+
chunk_global_cumsum_vector_kernel[grid](
|
|
375
|
+
s=s,
|
|
376
|
+
o=z,
|
|
377
|
+
scale=scale,
|
|
378
|
+
cu_seqlens=cu_seqlens,
|
|
379
|
+
T=T,
|
|
380
|
+
B=B,
|
|
381
|
+
H=H,
|
|
382
|
+
S=S,
|
|
383
|
+
BS=BS,
|
|
384
|
+
HEAD_FIRST=head_first,
|
|
385
|
+
REVERSE=reverse
|
|
386
|
+
)
|
|
387
|
+
return z
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
@input_guard
|
|
391
|
+
def chunk_global_cumsum(
|
|
392
|
+
s: torch.Tensor,
|
|
393
|
+
reverse: bool = False,
|
|
394
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
395
|
+
scale: float = None,
|
|
396
|
+
head_first: bool = False,
|
|
397
|
+
output_dtype: Optional[torch.dtype] = torch.float
|
|
398
|
+
) -> torch.Tensor:
|
|
399
|
+
if cu_seqlens is not None:
|
|
400
|
+
assert s.shape[0] == 1, "Only batch size 1 is supported when cu_seqlens are provided"
|
|
401
|
+
if len(s.shape) == 3:
|
|
402
|
+
return chunk_global_cumsum_scalar(
|
|
403
|
+
s=s,
|
|
404
|
+
reverse=reverse,
|
|
405
|
+
cu_seqlens=cu_seqlens,
|
|
406
|
+
scale=scale,
|
|
407
|
+
head_first=head_first,
|
|
408
|
+
output_dtype=output_dtype
|
|
409
|
+
)
|
|
410
|
+
elif len(s.shape) == 4:
|
|
411
|
+
return chunk_global_cumsum_vector(
|
|
412
|
+
s=s,
|
|
413
|
+
reverse=reverse,
|
|
414
|
+
cu_seqlens=cu_seqlens,
|
|
415
|
+
scale=scale,
|
|
416
|
+
head_first=head_first,
|
|
417
|
+
output_dtype=output_dtype
|
|
418
|
+
)
|
|
419
|
+
else:
|
|
420
|
+
raise ValueError(
|
|
421
|
+
f"Unsupported input shape {s.shape}, "
|
|
422
|
+
f"which should be [B, T, H]/[B, T, H, D] if `head_first=False` "
|
|
423
|
+
f"or [B, H, T]/[B, H, T, D] otherwise"
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
@input_guard
|
|
428
|
+
def chunk_local_cumsum(
|
|
429
|
+
g: torch.Tensor,
|
|
430
|
+
chunk_size: int,
|
|
431
|
+
reverse: bool = False,
|
|
432
|
+
scale: float = None,
|
|
433
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
434
|
+
head_first: bool = False,
|
|
435
|
+
output_dtype: Optional[torch.dtype] = torch.float,
|
|
436
|
+
**kwargs
|
|
437
|
+
) -> torch.Tensor:
|
|
438
|
+
if cu_seqlens is not None:
|
|
439
|
+
assert g.shape[0] == 1, "Only batch size 1 is supported when cu_seqlens are provided"
|
|
440
|
+
if len(g.shape) == 3:
|
|
441
|
+
return chunk_local_cumsum_scalar(
|
|
442
|
+
g=g,
|
|
443
|
+
chunk_size=chunk_size,
|
|
444
|
+
reverse=reverse,
|
|
445
|
+
scale=scale,
|
|
446
|
+
cu_seqlens=cu_seqlens,
|
|
447
|
+
head_first=head_first,
|
|
448
|
+
output_dtype=output_dtype
|
|
449
|
+
)
|
|
450
|
+
elif len(g.shape) == 4:
|
|
451
|
+
return chunk_local_cumsum_vector(
|
|
452
|
+
g=g,
|
|
453
|
+
chunk_size=chunk_size,
|
|
454
|
+
reverse=reverse,
|
|
455
|
+
scale=scale,
|
|
456
|
+
cu_seqlens=cu_seqlens,
|
|
457
|
+
head_first=head_first,
|
|
458
|
+
output_dtype=output_dtype
|
|
459
|
+
)
|
|
460
|
+
else:
|
|
461
|
+
raise ValueError(
|
|
462
|
+
f"Unsupported input shape {g.shape}, "
|
|
463
|
+
f"which should be (B, T, H, D) if `head_first=False` "
|
|
464
|
+
f"or (B, H, T, D) otherwise"
|
|
465
|
+
)
|