fla-core 0.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fla/__init__.py +4 -0
- fla/modules/__init__.py +33 -0
- fla/modules/activations.py +548 -0
- fla/modules/convolution.py +1143 -0
- fla/modules/feature_map.py +300 -0
- fla/modules/fused_bitlinear.py +632 -0
- fla/modules/fused_cross_entropy.py +419 -0
- fla/modules/fused_kl_div.py +324 -0
- fla/modules/fused_linear_cross_entropy.py +620 -0
- fla/modules/fused_norm_gate.py +1253 -0
- fla/modules/grpo.py +411 -0
- fla/modules/l2norm.py +285 -0
- fla/modules/l2warp.py +38 -0
- fla/modules/layernorm.py +1433 -0
- fla/modules/layernorm_gated.py +528 -0
- fla/modules/mlp.py +132 -0
- fla/modules/parallel.py +42 -0
- fla/modules/rotary.py +500 -0
- fla/modules/token_shift.py +543 -0
- fla/ops/__init__.py +53 -0
- fla/ops/abc/__init__.py +7 -0
- fla/ops/abc/chunk.py +1117 -0
- fla/ops/abc/naive.py +96 -0
- fla/ops/attn/__init__.py +7 -0
- fla/ops/attn/decoding.py +182 -0
- fla/ops/attn/parallel.py +730 -0
- fla/ops/based/__init__.py +9 -0
- fla/ops/based/fused_chunk.py +373 -0
- fla/ops/based/naive.py +72 -0
- fla/ops/based/parallel.py +408 -0
- fla/ops/comba/__init__.py +7 -0
- fla/ops/comba/chunk.py +342 -0
- fla/ops/comba/fused_recurrent.py +332 -0
- fla/ops/comba/utils.py +172 -0
- fla/ops/comba/wy_fast.py +423 -0
- fla/ops/common/__init__.py +1 -0
- fla/ops/common/chunk_delta_h.py +527 -0
- fla/ops/common/chunk_h.py +384 -0
- fla/ops/common/chunk_h_parallel.py +551 -0
- fla/ops/common/chunk_h_split.py +596 -0
- fla/ops/common/chunk_o.py +681 -0
- fla/ops/common/chunk_scaled_dot_kkt.py +305 -0
- fla/ops/common/fused_chunk.py +629 -0
- fla/ops/common/fused_recurrent.py +567 -0
- fla/ops/delta_rule/__init__.py +11 -0
- fla/ops/delta_rule/chunk.py +311 -0
- fla/ops/delta_rule/fused_chunk.py +6 -0
- fla/ops/delta_rule/fused_recurrent.py +535 -0
- fla/ops/delta_rule/naive.py +120 -0
- fla/ops/delta_rule/parallel.py +403 -0
- fla/ops/delta_rule/wy_fast.py +294 -0
- fla/ops/forgetting_attn/__init__.py +7 -0
- fla/ops/forgetting_attn/parallel.py +64 -0
- fla/ops/gated_delta_product/__init__.py +5 -0
- fla/ops/gated_delta_product/chunk.py +305 -0
- fla/ops/gated_delta_product/chunk_deltaproduct_h.py +502 -0
- fla/ops/gated_delta_product/chunk_deltaproduct_o.py +154 -0
- fla/ops/gated_delta_product/chunk_ref.py +68 -0
- fla/ops/gated_delta_product/naive.py +36 -0
- fla/ops/gated_delta_rule/__init__.py +7 -0
- fla/ops/gated_delta_rule/chunk.py +340 -0
- fla/ops/gated_delta_rule/fused_recurrent.py +351 -0
- fla/ops/gated_delta_rule/wy_fast.py +296 -0
- fla/ops/generalized_delta_rule/__init__.py +9 -0
- fla/ops/generalized_delta_rule/dplr/__init__.py +7 -0
- fla/ops/generalized_delta_rule/dplr/chunk.py +362 -0
- fla/ops/generalized_delta_rule/dplr/chunk_A_bwd.py +367 -0
- fla/ops/generalized_delta_rule/dplr/chunk_A_fwd.py +198 -0
- fla/ops/generalized_delta_rule/dplr/chunk_h_bwd.py +175 -0
- fla/ops/generalized_delta_rule/dplr/chunk_h_fwd.py +175 -0
- fla/ops/generalized_delta_rule/dplr/chunk_o_bwd.py +430 -0
- fla/ops/generalized_delta_rule/dplr/chunk_o_fwd.py +125 -0
- fla/ops/generalized_delta_rule/dplr/fused_recurrent.py +268 -0
- fla/ops/generalized_delta_rule/dplr/naive.py +96 -0
- fla/ops/generalized_delta_rule/dplr/wy_fast_bwd.py +164 -0
- fla/ops/generalized_delta_rule/dplr/wy_fast_fwd.py +284 -0
- fla/ops/generalized_delta_rule/iplr/__init__.py +7 -0
- fla/ops/generalized_delta_rule/iplr/chunk.py +492 -0
- fla/ops/generalized_delta_rule/iplr/fused_recurrent.py +452 -0
- fla/ops/generalized_delta_rule/iplr/naive.py +69 -0
- fla/ops/generalized_delta_rule/iplr/wy_fast.py +300 -0
- fla/ops/gla/__init__.py +11 -0
- fla/ops/gla/chunk.py +1298 -0
- fla/ops/gla/fused_chunk.py +642 -0
- fla/ops/gla/fused_recurrent.py +111 -0
- fla/ops/gla/naive.py +41 -0
- fla/ops/gsa/__init__.py +9 -0
- fla/ops/gsa/chunk.py +1133 -0
- fla/ops/gsa/fused_recurrent.py +536 -0
- fla/ops/gsa/naive.py +69 -0
- fla/ops/hgrn/__init__.py +9 -0
- fla/ops/hgrn/chunk.py +282 -0
- fla/ops/hgrn/fused_recurrent.py +308 -0
- fla/ops/hgrn/naive.py +63 -0
- fla/ops/lightning_attn/__init__.py +9 -0
- fla/ops/lightning_attn/chunk.py +84 -0
- fla/ops/lightning_attn/fused_recurrent.py +84 -0
- fla/ops/linear_attn/__init__.py +11 -0
- fla/ops/linear_attn/chunk.py +76 -0
- fla/ops/linear_attn/fused_chunk.py +61 -0
- fla/ops/linear_attn/fused_recurrent.py +35 -0
- fla/ops/linear_attn/naive.py +64 -0
- fla/ops/linear_attn/utils.py +10 -0
- fla/ops/log_linear_attn/__init__.py +7 -0
- fla/ops/log_linear_attn/chunk.py +1904 -0
- fla/ops/log_linear_attn/naive.py +50 -0
- fla/ops/mesa_net/__init__.py +5 -0
- fla/ops/mesa_net/chunk.py +371 -0
- fla/ops/mesa_net/chunk_cg_solver_bwd.py +162 -0
- fla/ops/mesa_net/chunk_cg_solver_fwd.py +186 -0
- fla/ops/mesa_net/chunk_h_fwd.py +170 -0
- fla/ops/mesa_net/chunk_h_kk_intra_bwd.py +191 -0
- fla/ops/mesa_net/chunk_h_kv_intra_bwd.py +209 -0
- fla/ops/mesa_net/chunk_h_kv_intra_bwd_separate.py +300 -0
- fla/ops/mesa_net/decoding_one_step.py +175 -0
- fla/ops/mesa_net/naive.py +131 -0
- fla/ops/nsa/__init__.py +9 -0
- fla/ops/nsa/compression.py +536 -0
- fla/ops/nsa/naive.py +103 -0
- fla/ops/nsa/parallel.py +879 -0
- fla/ops/nsa/utils.py +92 -0
- fla/ops/path_attn/__init__.py +7 -0
- fla/ops/path_attn/cumprod_householder_bwd.py +140 -0
- fla/ops/path_attn/cumprod_householder_fwd.py +119 -0
- fla/ops/path_attn/intra_chunk_preprocess_bwd.py +140 -0
- fla/ops/path_attn/intra_chunk_preprocess_bwd_prepare.py +208 -0
- fla/ops/path_attn/intra_chunk_preprocess_fwd.py +168 -0
- fla/ops/path_attn/parallel.py +270 -0
- fla/ops/path_attn/parallel_path_bwd_inter_dkv.py +189 -0
- fla/ops/path_attn/parallel_path_bwd_inter_dqh.py +200 -0
- fla/ops/path_attn/parallel_path_bwd_intra.py +172 -0
- fla/ops/path_attn/parallel_path_fwd.py +195 -0
- fla/ops/path_attn/prepare_k_cache.py +74 -0
- fla/ops/path_attn/transform_q.py +108 -0
- fla/ops/rebased/__init__.py +7 -0
- fla/ops/rebased/naive.py +27 -0
- fla/ops/rebased/parallel.py +464 -0
- fla/ops/retention/__init__.py +13 -0
- fla/ops/retention/chunk.py +77 -0
- fla/ops/retention/fused_chunk.py +78 -0
- fla/ops/retention/fused_recurrent.py +33 -0
- fla/ops/retention/naive.py +15 -0
- fla/ops/retention/parallel.py +71 -0
- fla/ops/rwkv4/__init__.py +7 -0
- fla/ops/rwkv4/fused_recurrent.py +472 -0
- fla/ops/rwkv6/__init__.py +9 -0
- fla/ops/rwkv6/chunk.py +1300 -0
- fla/ops/rwkv6/chunk_naive.py +43 -0
- fla/ops/rwkv6/fused_recurrent.py +673 -0
- fla/ops/rwkv6/recurrent_naive.py +103 -0
- fla/ops/rwkv7/__init__.py +10 -0
- fla/ops/rwkv7/channel_mixing.py +323 -0
- fla/ops/rwkv7/chunk.py +78 -0
- fla/ops/rwkv7/fused_addcmul.py +293 -0
- fla/ops/rwkv7/fused_k_update.py +346 -0
- fla/ops/rwkv7/fused_recurrent.py +334 -0
- fla/ops/rwkv7/gate_output_correction.py +244 -0
- fla/ops/simple_gla/__init__.py +13 -0
- fla/ops/simple_gla/chunk.py +303 -0
- fla/ops/simple_gla/fused_chunk.py +108 -0
- fla/ops/simple_gla/fused_recurrent.py +112 -0
- fla/ops/simple_gla/naive.py +114 -0
- fla/ops/simple_gla/parallel.py +722 -0
- fla/ops/titans/__init__.py +7 -0
- fla/ops/titans/log_impl.py +153 -0
- fla/ops/titans/naive.py +375 -0
- fla/ops/ttt/__init__.py +9 -0
- fla/ops/ttt/chunk.py +1441 -0
- fla/ops/ttt/fused_chunk.py +831 -0
- fla/ops/ttt/naive.py +126 -0
- fla/ops/utils/__init__.py +58 -0
- fla/ops/utils/constant.py +6 -0
- fla/ops/utils/cumsum.py +465 -0
- fla/ops/utils/index.py +133 -0
- fla/ops/utils/logcumsumexp.py +52 -0
- fla/ops/utils/logsumexp.py +80 -0
- fla/ops/utils/matmul.py +245 -0
- fla/ops/utils/op.py +62 -0
- fla/ops/utils/pack.py +208 -0
- fla/ops/utils/pooling.py +207 -0
- fla/ops/utils/softmax.py +106 -0
- fla/ops/utils/solve_tril.py +381 -0
- fla/utils.py +462 -0
- fla_core-0.3.2.dist-info/METADATA +575 -0
- fla_core-0.3.2.dist-info/RECORD +188 -0
- fla_core-0.3.2.dist-info/WHEEL +5 -0
- fla_core-0.3.2.dist-info/licenses/LICENSE +21 -0
- fla_core-0.3.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
4
|
+
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
import triton
|
|
9
|
+
import triton.language as tl
|
|
10
|
+
|
|
11
|
+
from fla.ops.utils import prepare_chunk_indices
|
|
12
|
+
from fla.ops.utils.op import exp
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@triton.jit()
|
|
16
|
+
def chunk_update_once(
|
|
17
|
+
b_p,
|
|
18
|
+
b_k,
|
|
19
|
+
b_v,
|
|
20
|
+
b_m,
|
|
21
|
+
b_g_exp_q,
|
|
22
|
+
b_h,
|
|
23
|
+
b_lamb
|
|
24
|
+
):
|
|
25
|
+
b_o = tl.dot((tl.dot(b_p.to(b_k.dtype), tl.trans(b_k)) * b_m).to(b_v.dtype), b_v)
|
|
26
|
+
b_o += tl.dot((b_p * b_g_exp_q).to(b_h.dtype), b_h)
|
|
27
|
+
if b_lamb is not None:
|
|
28
|
+
b_o += b_lamb[None, :] * b_p
|
|
29
|
+
return b_o
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@triton.heuristics({
|
|
33
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
|
|
34
|
+
})
|
|
35
|
+
@triton.jit(do_not_specialize=['T'])
|
|
36
|
+
def chunk_fwd_mesa_cg_dim64_kernel(
|
|
37
|
+
q,
|
|
38
|
+
q_final,
|
|
39
|
+
k,
|
|
40
|
+
h,
|
|
41
|
+
o,
|
|
42
|
+
v,
|
|
43
|
+
h_kv,
|
|
44
|
+
g,
|
|
45
|
+
beta,
|
|
46
|
+
lamb,
|
|
47
|
+
cu_seqlens,
|
|
48
|
+
chunk_indices,
|
|
49
|
+
T,
|
|
50
|
+
max_CG_iteration: tl.constexpr,
|
|
51
|
+
H: tl.constexpr,
|
|
52
|
+
K: tl.constexpr,
|
|
53
|
+
BT: tl.constexpr,
|
|
54
|
+
BK: tl.constexpr,
|
|
55
|
+
IS_VARLEN: tl.constexpr,
|
|
56
|
+
):
|
|
57
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
58
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
59
|
+
|
|
60
|
+
if IS_VARLEN:
|
|
61
|
+
i_tg = i_t
|
|
62
|
+
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)
|
|
63
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
64
|
+
T = eos - bos
|
|
65
|
+
NT = tl.cdiv(T, BT)
|
|
66
|
+
else:
|
|
67
|
+
NT = tl.cdiv(T, BT)
|
|
68
|
+
i_tg = i_b * NT + i_t
|
|
69
|
+
bos, eos = i_b * T, i_b * T + T
|
|
70
|
+
|
|
71
|
+
o_t = i_t * BT + tl.arange(0, BT)
|
|
72
|
+
m_t = o_t < T
|
|
73
|
+
|
|
74
|
+
# offset calculation
|
|
75
|
+
q += (bos * H + i_h) * K
|
|
76
|
+
q_final += (bos * H + i_h) * K
|
|
77
|
+
k += (bos * H + i_h) * K
|
|
78
|
+
h += (i_tg * H + i_h).to(tl.int64) * K * K
|
|
79
|
+
g += bos * H + i_h
|
|
80
|
+
beta += bos * H + i_h
|
|
81
|
+
lamb += i_h * K
|
|
82
|
+
|
|
83
|
+
o += (bos * H + i_h) * K
|
|
84
|
+
v += (bos * H + i_h) * K
|
|
85
|
+
h_kv += (i_tg * H + i_h).to(tl.int64) * K * K
|
|
86
|
+
|
|
87
|
+
p_q = tl.make_block_ptr(q, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
88
|
+
p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
89
|
+
p_h = tl.make_block_ptr(h, (K, K), (K, 1), (0, 0), (BK, BK), (1, 0))
|
|
90
|
+
|
|
91
|
+
b_h = tl.load(p_h, boundary_check=(0, 1))
|
|
92
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
93
|
+
b_q = tl.load(p_q, boundary_check=(0, 1)).to(tl.float32)
|
|
94
|
+
|
|
95
|
+
p_g = tl.make_block_ptr(g, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
96
|
+
b_g = tl.load(p_g, boundary_check=(0,)).to(tl.float32)
|
|
97
|
+
p_beta = tl.make_block_ptr(beta, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
98
|
+
b_beta = tl.load(p_beta, boundary_check=(0,)).to(tl.float32)
|
|
99
|
+
p_lamb = tl.make_block_ptr(lamb, (K,), (1,), (0,), (BK,), (0,))
|
|
100
|
+
|
|
101
|
+
b_lamb = tl.load(p_lamb, boundary_check=(0,)).to(tl.float32)
|
|
102
|
+
|
|
103
|
+
b_m = exp(b_g[:, None] - b_g[None, :]) * b_beta[None, :]
|
|
104
|
+
b_m = tl.where((o_t[:, None] >= o_t[None, :]) & (m_t[:, None] & m_t[None, :]), b_m, 0)
|
|
105
|
+
b_g_exp_q = tl.exp(b_g)[:, None]
|
|
106
|
+
|
|
107
|
+
b_x = tl.zeros([BT, BK], dtype=tl.float32)
|
|
108
|
+
b_p = tl.zeros([BT, BK], dtype=tl.float32)
|
|
109
|
+
b_r = tl.zeros([BT, BK], dtype=tl.float32)
|
|
110
|
+
|
|
111
|
+
b_x += b_q * 0.
|
|
112
|
+
b_r += b_q
|
|
113
|
+
b_p += b_r
|
|
114
|
+
b_delta_old = tl.sum(b_r*b_r, axis=1)
|
|
115
|
+
for i in range(max_CG_iteration):
|
|
116
|
+
b_o = chunk_update_once(b_p, b_k, b_k, b_m, b_g_exp_q, b_h, b_lamb)
|
|
117
|
+
alpha = b_delta_old / (tl.sum(b_p*b_o, axis=1) + 1e-5)
|
|
118
|
+
b_x += alpha[:, None] * b_p
|
|
119
|
+
b_r = b_r - alpha[:, None] * b_o
|
|
120
|
+
b_delta_new = tl.sum(b_r*b_r, axis=1)
|
|
121
|
+
b_p = b_r + (b_delta_new / (b_delta_old + 1e-5))[:, None] * b_p
|
|
122
|
+
b_delta_old = b_delta_new
|
|
123
|
+
|
|
124
|
+
p_q_final = tl.make_block_ptr(q_final, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
125
|
+
tl.store(p_q_final, b_x.to(p_q_final.dtype.element_ty), boundary_check=(0, 1))
|
|
126
|
+
|
|
127
|
+
p_h_kv = tl.make_block_ptr(h_kv, (K, K), (K, 1), (0, 0), (BK, BK), (1, 0))
|
|
128
|
+
b_h_kv = tl.load(p_h_kv, boundary_check=(0, 1))
|
|
129
|
+
p_v = tl.make_block_ptr(v, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
130
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
131
|
+
b_o = chunk_update_once(b_x, b_k, b_v, b_m, b_g_exp_q, b_h_kv, None)
|
|
132
|
+
p_o = tl.make_block_ptr(o, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
133
|
+
tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1))
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def chunk_mesa_cg_fwd(
|
|
137
|
+
q: torch.Tensor,
|
|
138
|
+
k: torch.Tensor,
|
|
139
|
+
v: torch.Tensor,
|
|
140
|
+
h: torch.Tensor,
|
|
141
|
+
h_kv: torch.Tensor,
|
|
142
|
+
g_local_cumsum: torch.Tensor,
|
|
143
|
+
beta: torch.Tensor,
|
|
144
|
+
lamb: torch.Tensor, # lambda
|
|
145
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
146
|
+
chunk_size: int = 64,
|
|
147
|
+
max_CG_iteration: int = 30,
|
|
148
|
+
output_dtype: Optional[torch.dtype] = None
|
|
149
|
+
) -> torch.Tensor:
|
|
150
|
+
B, T, H, K = q.shape
|
|
151
|
+
assert K <= 128, "head dimension must be less than 128"
|
|
152
|
+
assert chunk_size <= 64 or K <= 64, "either chunk size or head dimension must be no greater than 64"
|
|
153
|
+
q_final = torch.empty_like(q, dtype=q.dtype if output_dtype is None else output_dtype)
|
|
154
|
+
|
|
155
|
+
assert v is not None, "v must be provided if calculate_output is True"
|
|
156
|
+
assert h_kv is not None, "h_kv must be provided if calculate_output is True"
|
|
157
|
+
o = torch.empty_like(v)
|
|
158
|
+
|
|
159
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, chunk_size) if cu_seqlens is not None else None
|
|
160
|
+
NT = triton.cdiv(T, chunk_size) if cu_seqlens is None else len(chunk_indices)
|
|
161
|
+
BK = max(triton.next_power_of_2(K), 16)
|
|
162
|
+
grid = (NT, H*B)
|
|
163
|
+
|
|
164
|
+
chunk_fwd_mesa_cg_dim64_kernel[grid](
|
|
165
|
+
q=q,
|
|
166
|
+
q_final=q_final,
|
|
167
|
+
o=o,
|
|
168
|
+
v=v,
|
|
169
|
+
h_kv=h_kv,
|
|
170
|
+
k=k,
|
|
171
|
+
h=h,
|
|
172
|
+
g=g_local_cumsum,
|
|
173
|
+
beta=beta,
|
|
174
|
+
lamb=lamb,
|
|
175
|
+
cu_seqlens=cu_seqlens,
|
|
176
|
+
chunk_indices=chunk_indices,
|
|
177
|
+
max_CG_iteration=max_CG_iteration,
|
|
178
|
+
T=T,
|
|
179
|
+
H=H,
|
|
180
|
+
K=K,
|
|
181
|
+
BT=chunk_size,
|
|
182
|
+
BK=BK,
|
|
183
|
+
num_warps=4,
|
|
184
|
+
num_stages=1
|
|
185
|
+
)
|
|
186
|
+
return q_final, o
|
|
@@ -0,0 +1,170 @@
|
|
|
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_offsets
|
|
11
|
+
from fla.ops.utils.op import exp
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@triton.heuristics({
|
|
15
|
+
'USE_INITIAL_STATE': lambda args: args['h_init'] is not None,
|
|
16
|
+
'STORE_FINAL_STATE': lambda args: args['h_final'] is not None,
|
|
17
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
18
|
+
})
|
|
19
|
+
@triton.autotune(
|
|
20
|
+
configs=[
|
|
21
|
+
triton.Config({}, num_warps=num_warps, num_stages=num_stages)
|
|
22
|
+
for num_warps in [1, 2, 4, 8]
|
|
23
|
+
for num_stages in [2, 3, 4]
|
|
24
|
+
],
|
|
25
|
+
key=['BT']
|
|
26
|
+
)
|
|
27
|
+
@triton.jit(do_not_specialize=['T'])
|
|
28
|
+
def chunk_mesa_net_fwd_kernel_h(
|
|
29
|
+
k,
|
|
30
|
+
v,
|
|
31
|
+
beta,
|
|
32
|
+
g,
|
|
33
|
+
h,
|
|
34
|
+
h_kv,
|
|
35
|
+
h_init,
|
|
36
|
+
h_kv_init,
|
|
37
|
+
h_final,
|
|
38
|
+
h_kv_final,
|
|
39
|
+
cu_seqlens,
|
|
40
|
+
split_offsets,
|
|
41
|
+
T,
|
|
42
|
+
H: tl.constexpr,
|
|
43
|
+
K: tl.constexpr,
|
|
44
|
+
V: tl.constexpr,
|
|
45
|
+
BT: tl.constexpr,
|
|
46
|
+
BS: tl.constexpr,
|
|
47
|
+
BK: tl.constexpr,
|
|
48
|
+
BV: tl.constexpr,
|
|
49
|
+
USE_INITIAL_STATE: tl.constexpr,
|
|
50
|
+
STORE_FINAL_STATE: tl.constexpr,
|
|
51
|
+
IS_VARLEN: tl.constexpr,
|
|
52
|
+
):
|
|
53
|
+
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
54
|
+
i_n, i_h = i_nh // H, i_nh % H
|
|
55
|
+
if IS_VARLEN:
|
|
56
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
57
|
+
T = eos - bos
|
|
58
|
+
NT = tl.cdiv(T, BT)
|
|
59
|
+
NS = tl.cdiv(T, BS)
|
|
60
|
+
boh = tl.load(split_offsets + i_n).to(tl.int32)
|
|
61
|
+
else:
|
|
62
|
+
bos, eos = i_n * T, i_n * T + T
|
|
63
|
+
NT = tl.cdiv(T, BT)
|
|
64
|
+
NS = tl.cdiv(T, BS)
|
|
65
|
+
boh = i_n * NS
|
|
66
|
+
|
|
67
|
+
# [BK, BV]
|
|
68
|
+
b_h = tl.zeros([BK, BV], dtype=tl.float32)
|
|
69
|
+
b_h_kv = tl.zeros([BK, BV], dtype=tl.float32)
|
|
70
|
+
if USE_INITIAL_STATE:
|
|
71
|
+
p_h0 = tl.make_block_ptr(h_init + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
|
72
|
+
b_h = tl.load(p_h0, boundary_check=(0, 1)).to(tl.float32)
|
|
73
|
+
p_h_kv0 = tl.make_block_ptr(h_kv_init + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
|
74
|
+
b_h_kv = tl.load(p_h_kv0, boundary_check=(0, 1)).to(tl.float32)
|
|
75
|
+
|
|
76
|
+
for i_t in range(NT):
|
|
77
|
+
i_s = i_t // (BS // BT)
|
|
78
|
+
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))
|
|
79
|
+
p_k2 = tl.make_block_ptr(k + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
|
80
|
+
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))
|
|
81
|
+
p_beta = tl.make_block_ptr(beta + (bos*H + i_h), (T,), (H,), (i_t * BT,), (BT, ), (0,))
|
|
82
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
83
|
+
|
|
84
|
+
o_h = ((boh + i_s) * H + i_h).to(tl.int64) * K*V
|
|
85
|
+
p_h = tl.make_block_ptr(h + o_h, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
|
86
|
+
p_h_kv = tl.make_block_ptr(h_kv + o_h, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
|
87
|
+
|
|
88
|
+
if i_t % (BS // BT) == 0:
|
|
89
|
+
tl.store(p_h, b_h.to(p_h.dtype.element_ty), boundary_check=(0, 1))
|
|
90
|
+
tl.store(p_h_kv, b_h_kv.to(p_h_kv.dtype.element_ty), boundary_check=(0, 1))
|
|
91
|
+
|
|
92
|
+
# [BK, BT]
|
|
93
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
94
|
+
b_k2 = tl.load(p_k2, boundary_check=(0, 1))
|
|
95
|
+
# [BT, BV]
|
|
96
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
97
|
+
last_idx = min((i_t + 1) * BT, T) - 1
|
|
98
|
+
|
|
99
|
+
# scalar decay
|
|
100
|
+
b_g_last = tl.load(g + bos * H + last_idx * H + i_h)
|
|
101
|
+
p_g = g + bos*H + (i_t * BT + tl.arange(0, BT)) * H + i_h
|
|
102
|
+
b_h *= exp(b_g_last)
|
|
103
|
+
b_h_kv *= exp(b_g_last)
|
|
104
|
+
b_g = tl.load(p_g, mask=(i_t * BT + tl.arange(0, BT) < T), other=0.)
|
|
105
|
+
b_k_decay = ((b_k * exp(b_g_last - b_g)[:, None]) * b_beta[:, None]).to(b_k2.dtype)
|
|
106
|
+
b_h += tl.dot(tl.trans(b_k_decay), b_k2)
|
|
107
|
+
b_h_kv += tl.dot(tl.trans(b_k_decay), b_v.to(b_k2.dtype))
|
|
108
|
+
|
|
109
|
+
if STORE_FINAL_STATE:
|
|
110
|
+
p_ht = tl.make_block_ptr(h_final + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
|
111
|
+
tl.store(p_ht, b_h.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
|
|
112
|
+
p_h_kv_final = tl.make_block_ptr(h_kv_final + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
|
113
|
+
tl.store(p_h_kv_final, b_h_kv.to(p_h_kv_final.dtype.element_ty), boundary_check=(0, 1))
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def chunk_mesa_fwd_h(
|
|
117
|
+
k: torch.Tensor,
|
|
118
|
+
v: torch.Tensor,
|
|
119
|
+
g: torch.Tensor,
|
|
120
|
+
beta: torch.Tensor,
|
|
121
|
+
h_init: torch.Tensor,
|
|
122
|
+
h_kv_init: torch.Tensor,
|
|
123
|
+
output_final_state: bool,
|
|
124
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
125
|
+
chunk_size: int = 64,
|
|
126
|
+
split_size: Optional[int] = None,
|
|
127
|
+
states_in_fp32: bool = False
|
|
128
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
129
|
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
|
130
|
+
assert K == V, "K must be equal to V for now"
|
|
131
|
+
BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
|
|
132
|
+
BS = BT if split_size is None else min(split_size, max(16, triton.next_power_of_2(T)))
|
|
133
|
+
assert BS % BT == 0, f"The `split_size` (got {BS}) must be a multiple of `chunk_size` {BT}"
|
|
134
|
+
# N: the actual number of sequences in the batch with either equal or variable lengths
|
|
135
|
+
if cu_seqlens is None:
|
|
136
|
+
N, NS, split_offsets = B, triton.cdiv(T, BS), None
|
|
137
|
+
else:
|
|
138
|
+
split_offsets = prepare_chunk_offsets(cu_seqlens, BS)
|
|
139
|
+
N, NS = len(cu_seqlens) - 1, split_offsets[-1].item()
|
|
140
|
+
|
|
141
|
+
h = k.new_empty(B, NS, H, K, V, dtype=k.dtype if not states_in_fp32 else torch.float)
|
|
142
|
+
h_kv = k.new_empty(B, NS, H, K, V, dtype=k.dtype if not states_in_fp32 else torch.float)
|
|
143
|
+
h_final = k.new_empty(N, H, K, V, dtype=torch.float) if output_final_state else None
|
|
144
|
+
h_kv_final = k.new_empty(N, H, K, V, dtype=torch.float)
|
|
145
|
+
|
|
146
|
+
def grid(meta): return (triton.cdiv(K, 64), triton.cdiv(V, 64), N * H)
|
|
147
|
+
|
|
148
|
+
chunk_mesa_net_fwd_kernel_h[grid](
|
|
149
|
+
k=k,
|
|
150
|
+
v=v,
|
|
151
|
+
beta=beta,
|
|
152
|
+
g=g,
|
|
153
|
+
h=h,
|
|
154
|
+
h_kv=h_kv,
|
|
155
|
+
h_init=h_init,
|
|
156
|
+
h_kv_init=h_kv_init,
|
|
157
|
+
h_final=h_final,
|
|
158
|
+
h_kv_final=h_kv_final,
|
|
159
|
+
cu_seqlens=cu_seqlens,
|
|
160
|
+
split_offsets=split_offsets,
|
|
161
|
+
T=T,
|
|
162
|
+
H=H,
|
|
163
|
+
K=K,
|
|
164
|
+
V=V,
|
|
165
|
+
BT=BT,
|
|
166
|
+
BS=BS,
|
|
167
|
+
BK=64,
|
|
168
|
+
BV=64,
|
|
169
|
+
)
|
|
170
|
+
return h, h_kv, h_final, h_kv_final
|
|
@@ -0,0 +1,191 @@
|
|
|
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
|
+
|
|
13
|
+
|
|
14
|
+
@triton.heuristics({
|
|
15
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
|
|
16
|
+
})
|
|
17
|
+
@triton.jit(do_not_specialize=['T'])
|
|
18
|
+
def chunk_mesa_net_h_kk_bwd_intra_kernel(
|
|
19
|
+
k,
|
|
20
|
+
beta,
|
|
21
|
+
h,
|
|
22
|
+
dh,
|
|
23
|
+
g,
|
|
24
|
+
q_star,
|
|
25
|
+
dq,
|
|
26
|
+
dk,
|
|
27
|
+
dg,
|
|
28
|
+
dbeta,
|
|
29
|
+
dk_beta,
|
|
30
|
+
dlamb,
|
|
31
|
+
cu_seqlens,
|
|
32
|
+
chunk_indices,
|
|
33
|
+
B: tl.constexpr,
|
|
34
|
+
T,
|
|
35
|
+
H: tl.constexpr,
|
|
36
|
+
K: tl.constexpr,
|
|
37
|
+
V: tl.constexpr,
|
|
38
|
+
BT: tl.constexpr,
|
|
39
|
+
BK: tl.constexpr,
|
|
40
|
+
BV: tl.constexpr,
|
|
41
|
+
IS_VARLEN: 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
|
+
|
|
46
|
+
if IS_VARLEN:
|
|
47
|
+
i_tg = i_t
|
|
48
|
+
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)
|
|
49
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
50
|
+
T = eos - bos
|
|
51
|
+
NT = tl.cdiv(T, BT)
|
|
52
|
+
else:
|
|
53
|
+
NT = tl.cdiv(T, BT)
|
|
54
|
+
i_tg = i_b * NT + i_t
|
|
55
|
+
bos, eos = i_b * T, i_b * T + T
|
|
56
|
+
|
|
57
|
+
o_t = i_t * BT + tl.arange(0, BT)
|
|
58
|
+
m_t = o_t < T
|
|
59
|
+
|
|
60
|
+
# offset calculation
|
|
61
|
+
q_star += (bos * H + i_h) * V
|
|
62
|
+
dq += (bos * H + i_h) * V
|
|
63
|
+
h += (i_tg * H + i_h).to(tl.int64) * K*V
|
|
64
|
+
dh += (i_tg * H + i_h).to(tl.int64) * K*V
|
|
65
|
+
k += (bos * H + i_h) * K
|
|
66
|
+
dk += (bos * H + i_h) * K
|
|
67
|
+
dk_beta += (bos * H + i_h) * K
|
|
68
|
+
dlamb += (i_tg * H + i_h).to(tl.int64) * K
|
|
69
|
+
beta += (bos * H + i_h)
|
|
70
|
+
dbeta += (bos * H + i_h)
|
|
71
|
+
g += bos * H + i_h
|
|
72
|
+
dg += bos * H + i_h
|
|
73
|
+
|
|
74
|
+
b_dk = tl.zeros([BT, BK], dtype=tl.float32)
|
|
75
|
+
b_dv = tl.zeros([BT, BK], dtype=tl.float32)
|
|
76
|
+
b_dbeta = tl.zeros([BT, ], dtype=tl.float32)
|
|
77
|
+
b_dg_last = tl.zeros([1,], dtype=tl.float32)
|
|
78
|
+
b_dg = tl.zeros([BT,], dtype=tl.float32)
|
|
79
|
+
|
|
80
|
+
p_g = tl.make_block_ptr(g, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
81
|
+
b_g = tl.load(p_g, boundary_check=(0,))
|
|
82
|
+
b_g_last = tl.load(g + (min(i_t * BT + BT, T) - 1) * H)
|
|
83
|
+
b_gk = tl.where(m_t, exp(b_g_last - b_g), 0)
|
|
84
|
+
|
|
85
|
+
p_q_star = tl.make_block_ptr(q_star, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
|
|
86
|
+
b_q_star = tl.load(p_q_star, boundary_check=(0, 1))
|
|
87
|
+
p_dq = tl.make_block_ptr(dq, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
|
|
88
|
+
b_dq = tl.load(p_dq, boundary_check=(0, 1))
|
|
89
|
+
b_dlamb = -tl.sum(b_q_star * b_dq, axis=0)
|
|
90
|
+
p_dlamb = tl.make_block_ptr(dlamb, (K,), (1,), (0,), (BK,), (0,))
|
|
91
|
+
tl.store(p_dlamb, b_dlamb.to(p_dlamb.dtype.element_ty), boundary_check=(0,))
|
|
92
|
+
|
|
93
|
+
p_h = tl.make_block_ptr(h, (V, K), (1, V), (0, 0), (BV, BK), (0, 1))
|
|
94
|
+
p_dh = tl.make_block_ptr(dh, (V, K), (1, V), (0, 0), (BV, BK), (0, 1))
|
|
95
|
+
p_beta = tl.make_block_ptr(beta, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
96
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
97
|
+
p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
98
|
+
b_v = tl.load(p_k, boundary_check=(0, 1))
|
|
99
|
+
b_k = (b_v * b_beta[:, None]).to(b_v.dtype)
|
|
100
|
+
|
|
101
|
+
b_m = tl.where((o_t[:, None] >= o_t[None, :]) & (m_t[:, None] & m_t[None, :]), exp(b_g[:, None] - b_g[None, :]), 0)
|
|
102
|
+
b_s = tl.dot(b_q_star, tl.trans(b_k)) * b_m
|
|
103
|
+
b_ds = tl.dot(b_dq, tl.trans(b_v))
|
|
104
|
+
b_dv += tl.dot(tl.trans(b_s.to(b_dq.dtype)), b_dq)
|
|
105
|
+
b_dm = b_s * b_ds
|
|
106
|
+
b_dm = tl.where(tl.arange(0, BT)[:, None] >= tl.arange(0, BT)[None, :], b_dm, 0)
|
|
107
|
+
b_dg += tl.sum(b_dm, axis=1)
|
|
108
|
+
b_dg -= tl.sum(b_dm, axis=0)
|
|
109
|
+
b_ds = b_ds * b_m
|
|
110
|
+
b_dk += tl.dot(tl.trans(b_ds.to(b_q_star.dtype)), b_q_star)
|
|
111
|
+
|
|
112
|
+
b_h = tl.load(p_h, boundary_check=(0, 1))
|
|
113
|
+
b_dg += tl.sum(tl.dot(b_dq, tl.trans(b_h)) * tl.exp(b_g)[:, None] * b_q_star, axis=1)
|
|
114
|
+
b_dh = tl.load(p_dh, boundary_check=(0, 1))
|
|
115
|
+
b_dk2 = tl.dot(b_v, b_dh.to(b_v.dtype)) * b_gk[:, None]
|
|
116
|
+
b_dg -= tl.sum(b_dk2 * b_k, axis=1)
|
|
117
|
+
b_dg_last += tl.sum(b_dk2 * b_k)
|
|
118
|
+
b_dk += b_dk2
|
|
119
|
+
b_dv += tl.dot(b_k, tl.trans(b_dh).to(b_k.dtype)) * b_gk[:, None]
|
|
120
|
+
b_dh = b_dh * b_h
|
|
121
|
+
b_dg_last += tl.sum(b_dh) * exp(b_g_last)
|
|
122
|
+
|
|
123
|
+
p_dk_beta = tl.make_block_ptr(dk_beta, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
124
|
+
b_dk -= tl.load(p_dk_beta, boundary_check=(0, 1))
|
|
125
|
+
b_dbeta = tl.sum(b_dk * b_v, axis=1)
|
|
126
|
+
b_dk = b_dk * b_beta[:, None] + b_dv
|
|
127
|
+
b_dk = -b_dk
|
|
128
|
+
|
|
129
|
+
b_dg = tl.where(o_t < min(i_t * BT + BT, T) - 1, b_dg, b_dg + b_dg_last)
|
|
130
|
+
p_dk = tl.make_block_ptr(dk, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
131
|
+
tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
|
|
132
|
+
p_dg = tl.make_block_ptr(dg, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
133
|
+
tl.store(p_dg, -b_dg.to(p_dg.dtype.element_ty), boundary_check=(0,))
|
|
134
|
+
p_dbeta = tl.make_block_ptr(dbeta, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
135
|
+
tl.store(p_dbeta, -b_dbeta.to(p_dbeta.dtype.element_ty), boundary_check=(0,))
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def chunk_mesa_net_h_kk_bwd_intra_fn(
|
|
139
|
+
k: torch.Tensor,
|
|
140
|
+
beta: torch.Tensor,
|
|
141
|
+
g: torch.Tensor,
|
|
142
|
+
h: torch.Tensor,
|
|
143
|
+
dh: torch.Tensor,
|
|
144
|
+
q_star: torch.Tensor,
|
|
145
|
+
dq: torch.Tensor,
|
|
146
|
+
dk_beta: torch.Tensor,
|
|
147
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
148
|
+
chunk_size: int = 64,
|
|
149
|
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
150
|
+
|
|
151
|
+
B, T, H, K = k.shape
|
|
152
|
+
V = K
|
|
153
|
+
BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
|
|
154
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
155
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
156
|
+
|
|
157
|
+
# CONST_TILING = 64
|
|
158
|
+
BK = max(triton.next_power_of_2(K), 16)
|
|
159
|
+
BV = max(triton.next_power_of_2(V), 16)
|
|
160
|
+
dk = torch.empty_like(k)
|
|
161
|
+
dg = torch.empty_like(g)
|
|
162
|
+
dbeta = torch.empty_like(beta)
|
|
163
|
+
dlamb = torch.empty(B, NT, H, K, dtype=torch.float32, device=k.device)
|
|
164
|
+
grid = (NT, B * H)
|
|
165
|
+
|
|
166
|
+
chunk_mesa_net_h_kk_bwd_intra_kernel[grid](
|
|
167
|
+
k=k,
|
|
168
|
+
h=h,
|
|
169
|
+
dh=dh,
|
|
170
|
+
g=g,
|
|
171
|
+
q_star=q_star,
|
|
172
|
+
beta=beta,
|
|
173
|
+
dbeta=dbeta,
|
|
174
|
+
dq=dq,
|
|
175
|
+
dk=dk,
|
|
176
|
+
dk_beta=dk_beta,
|
|
177
|
+
dg=dg,
|
|
178
|
+
dlamb=dlamb,
|
|
179
|
+
cu_seqlens=cu_seqlens,
|
|
180
|
+
chunk_indices=chunk_indices,
|
|
181
|
+
B=B,
|
|
182
|
+
T=T,
|
|
183
|
+
H=H,
|
|
184
|
+
K=K,
|
|
185
|
+
V=V,
|
|
186
|
+
BT=BT,
|
|
187
|
+
BK=BK,
|
|
188
|
+
BV=BV
|
|
189
|
+
)
|
|
190
|
+
dlamb = dlamb.sum([0, 1])
|
|
191
|
+
return dk, dg, dlamb, dbeta
|