fla-core 0.3.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. fla/__init__.py +4 -0
  2. fla/modules/__init__.py +33 -0
  3. fla/modules/activations.py +548 -0
  4. fla/modules/convolution.py +1143 -0
  5. fla/modules/feature_map.py +300 -0
  6. fla/modules/fused_bitlinear.py +632 -0
  7. fla/modules/fused_cross_entropy.py +419 -0
  8. fla/modules/fused_kl_div.py +324 -0
  9. fla/modules/fused_linear_cross_entropy.py +620 -0
  10. fla/modules/fused_norm_gate.py +1253 -0
  11. fla/modules/grpo.py +411 -0
  12. fla/modules/l2norm.py +285 -0
  13. fla/modules/l2warp.py +38 -0
  14. fla/modules/layernorm.py +1433 -0
  15. fla/modules/layernorm_gated.py +528 -0
  16. fla/modules/mlp.py +132 -0
  17. fla/modules/parallel.py +42 -0
  18. fla/modules/rotary.py +500 -0
  19. fla/modules/token_shift.py +543 -0
  20. fla/ops/__init__.py +53 -0
  21. fla/ops/abc/__init__.py +7 -0
  22. fla/ops/abc/chunk.py +1117 -0
  23. fla/ops/abc/naive.py +96 -0
  24. fla/ops/attn/__init__.py +7 -0
  25. fla/ops/attn/decoding.py +182 -0
  26. fla/ops/attn/parallel.py +730 -0
  27. fla/ops/based/__init__.py +9 -0
  28. fla/ops/based/fused_chunk.py +373 -0
  29. fla/ops/based/naive.py +72 -0
  30. fla/ops/based/parallel.py +408 -0
  31. fla/ops/comba/__init__.py +7 -0
  32. fla/ops/comba/chunk.py +342 -0
  33. fla/ops/comba/fused_recurrent.py +332 -0
  34. fla/ops/comba/utils.py +172 -0
  35. fla/ops/comba/wy_fast.py +423 -0
  36. fla/ops/common/__init__.py +1 -0
  37. fla/ops/common/chunk_delta_h.py +527 -0
  38. fla/ops/common/chunk_h.py +384 -0
  39. fla/ops/common/chunk_h_parallel.py +551 -0
  40. fla/ops/common/chunk_h_split.py +596 -0
  41. fla/ops/common/chunk_o.py +681 -0
  42. fla/ops/common/chunk_scaled_dot_kkt.py +305 -0
  43. fla/ops/common/fused_chunk.py +629 -0
  44. fla/ops/common/fused_recurrent.py +567 -0
  45. fla/ops/delta_rule/__init__.py +11 -0
  46. fla/ops/delta_rule/chunk.py +311 -0
  47. fla/ops/delta_rule/fused_chunk.py +6 -0
  48. fla/ops/delta_rule/fused_recurrent.py +535 -0
  49. fla/ops/delta_rule/naive.py +120 -0
  50. fla/ops/delta_rule/parallel.py +403 -0
  51. fla/ops/delta_rule/wy_fast.py +294 -0
  52. fla/ops/forgetting_attn/__init__.py +7 -0
  53. fla/ops/forgetting_attn/parallel.py +64 -0
  54. fla/ops/gated_delta_product/__init__.py +5 -0
  55. fla/ops/gated_delta_product/chunk.py +305 -0
  56. fla/ops/gated_delta_product/chunk_deltaproduct_h.py +502 -0
  57. fla/ops/gated_delta_product/chunk_deltaproduct_o.py +154 -0
  58. fla/ops/gated_delta_product/chunk_ref.py +68 -0
  59. fla/ops/gated_delta_product/naive.py +36 -0
  60. fla/ops/gated_delta_rule/__init__.py +7 -0
  61. fla/ops/gated_delta_rule/chunk.py +340 -0
  62. fla/ops/gated_delta_rule/fused_recurrent.py +351 -0
  63. fla/ops/gated_delta_rule/wy_fast.py +296 -0
  64. fla/ops/generalized_delta_rule/__init__.py +9 -0
  65. fla/ops/generalized_delta_rule/dplr/__init__.py +7 -0
  66. fla/ops/generalized_delta_rule/dplr/chunk.py +362 -0
  67. fla/ops/generalized_delta_rule/dplr/chunk_A_bwd.py +367 -0
  68. fla/ops/generalized_delta_rule/dplr/chunk_A_fwd.py +198 -0
  69. fla/ops/generalized_delta_rule/dplr/chunk_h_bwd.py +175 -0
  70. fla/ops/generalized_delta_rule/dplr/chunk_h_fwd.py +175 -0
  71. fla/ops/generalized_delta_rule/dplr/chunk_o_bwd.py +430 -0
  72. fla/ops/generalized_delta_rule/dplr/chunk_o_fwd.py +125 -0
  73. fla/ops/generalized_delta_rule/dplr/fused_recurrent.py +268 -0
  74. fla/ops/generalized_delta_rule/dplr/naive.py +96 -0
  75. fla/ops/generalized_delta_rule/dplr/wy_fast_bwd.py +164 -0
  76. fla/ops/generalized_delta_rule/dplr/wy_fast_fwd.py +284 -0
  77. fla/ops/generalized_delta_rule/iplr/__init__.py +7 -0
  78. fla/ops/generalized_delta_rule/iplr/chunk.py +492 -0
  79. fla/ops/generalized_delta_rule/iplr/fused_recurrent.py +452 -0
  80. fla/ops/generalized_delta_rule/iplr/naive.py +69 -0
  81. fla/ops/generalized_delta_rule/iplr/wy_fast.py +300 -0
  82. fla/ops/gla/__init__.py +11 -0
  83. fla/ops/gla/chunk.py +1298 -0
  84. fla/ops/gla/fused_chunk.py +642 -0
  85. fla/ops/gla/fused_recurrent.py +111 -0
  86. fla/ops/gla/naive.py +41 -0
  87. fla/ops/gsa/__init__.py +9 -0
  88. fla/ops/gsa/chunk.py +1133 -0
  89. fla/ops/gsa/fused_recurrent.py +536 -0
  90. fla/ops/gsa/naive.py +69 -0
  91. fla/ops/hgrn/__init__.py +9 -0
  92. fla/ops/hgrn/chunk.py +282 -0
  93. fla/ops/hgrn/fused_recurrent.py +308 -0
  94. fla/ops/hgrn/naive.py +63 -0
  95. fla/ops/lightning_attn/__init__.py +9 -0
  96. fla/ops/lightning_attn/chunk.py +84 -0
  97. fla/ops/lightning_attn/fused_recurrent.py +84 -0
  98. fla/ops/linear_attn/__init__.py +11 -0
  99. fla/ops/linear_attn/chunk.py +76 -0
  100. fla/ops/linear_attn/fused_chunk.py +61 -0
  101. fla/ops/linear_attn/fused_recurrent.py +35 -0
  102. fla/ops/linear_attn/naive.py +64 -0
  103. fla/ops/linear_attn/utils.py +10 -0
  104. fla/ops/log_linear_attn/__init__.py +7 -0
  105. fla/ops/log_linear_attn/chunk.py +1904 -0
  106. fla/ops/log_linear_attn/naive.py +50 -0
  107. fla/ops/mesa_net/__init__.py +5 -0
  108. fla/ops/mesa_net/chunk.py +371 -0
  109. fla/ops/mesa_net/chunk_cg_solver_bwd.py +162 -0
  110. fla/ops/mesa_net/chunk_cg_solver_fwd.py +186 -0
  111. fla/ops/mesa_net/chunk_h_fwd.py +170 -0
  112. fla/ops/mesa_net/chunk_h_kk_intra_bwd.py +191 -0
  113. fla/ops/mesa_net/chunk_h_kv_intra_bwd.py +209 -0
  114. fla/ops/mesa_net/chunk_h_kv_intra_bwd_separate.py +300 -0
  115. fla/ops/mesa_net/decoding_one_step.py +175 -0
  116. fla/ops/mesa_net/naive.py +131 -0
  117. fla/ops/nsa/__init__.py +9 -0
  118. fla/ops/nsa/compression.py +536 -0
  119. fla/ops/nsa/naive.py +103 -0
  120. fla/ops/nsa/parallel.py +879 -0
  121. fla/ops/nsa/utils.py +92 -0
  122. fla/ops/path_attn/__init__.py +7 -0
  123. fla/ops/path_attn/cumprod_householder_bwd.py +140 -0
  124. fla/ops/path_attn/cumprod_householder_fwd.py +119 -0
  125. fla/ops/path_attn/intra_chunk_preprocess_bwd.py +140 -0
  126. fla/ops/path_attn/intra_chunk_preprocess_bwd_prepare.py +208 -0
  127. fla/ops/path_attn/intra_chunk_preprocess_fwd.py +168 -0
  128. fla/ops/path_attn/parallel.py +270 -0
  129. fla/ops/path_attn/parallel_path_bwd_inter_dkv.py +189 -0
  130. fla/ops/path_attn/parallel_path_bwd_inter_dqh.py +200 -0
  131. fla/ops/path_attn/parallel_path_bwd_intra.py +172 -0
  132. fla/ops/path_attn/parallel_path_fwd.py +195 -0
  133. fla/ops/path_attn/prepare_k_cache.py +74 -0
  134. fla/ops/path_attn/transform_q.py +108 -0
  135. fla/ops/rebased/__init__.py +7 -0
  136. fla/ops/rebased/naive.py +27 -0
  137. fla/ops/rebased/parallel.py +464 -0
  138. fla/ops/retention/__init__.py +13 -0
  139. fla/ops/retention/chunk.py +77 -0
  140. fla/ops/retention/fused_chunk.py +78 -0
  141. fla/ops/retention/fused_recurrent.py +33 -0
  142. fla/ops/retention/naive.py +15 -0
  143. fla/ops/retention/parallel.py +71 -0
  144. fla/ops/rwkv4/__init__.py +7 -0
  145. fla/ops/rwkv4/fused_recurrent.py +472 -0
  146. fla/ops/rwkv6/__init__.py +9 -0
  147. fla/ops/rwkv6/chunk.py +1300 -0
  148. fla/ops/rwkv6/chunk_naive.py +43 -0
  149. fla/ops/rwkv6/fused_recurrent.py +673 -0
  150. fla/ops/rwkv6/recurrent_naive.py +103 -0
  151. fla/ops/rwkv7/__init__.py +10 -0
  152. fla/ops/rwkv7/channel_mixing.py +323 -0
  153. fla/ops/rwkv7/chunk.py +78 -0
  154. fla/ops/rwkv7/fused_addcmul.py +293 -0
  155. fla/ops/rwkv7/fused_k_update.py +346 -0
  156. fla/ops/rwkv7/fused_recurrent.py +334 -0
  157. fla/ops/rwkv7/gate_output_correction.py +244 -0
  158. fla/ops/simple_gla/__init__.py +13 -0
  159. fla/ops/simple_gla/chunk.py +303 -0
  160. fla/ops/simple_gla/fused_chunk.py +108 -0
  161. fla/ops/simple_gla/fused_recurrent.py +112 -0
  162. fla/ops/simple_gla/naive.py +114 -0
  163. fla/ops/simple_gla/parallel.py +722 -0
  164. fla/ops/titans/__init__.py +7 -0
  165. fla/ops/titans/log_impl.py +153 -0
  166. fla/ops/titans/naive.py +375 -0
  167. fla/ops/ttt/__init__.py +9 -0
  168. fla/ops/ttt/chunk.py +1441 -0
  169. fla/ops/ttt/fused_chunk.py +831 -0
  170. fla/ops/ttt/naive.py +126 -0
  171. fla/ops/utils/__init__.py +58 -0
  172. fla/ops/utils/constant.py +6 -0
  173. fla/ops/utils/cumsum.py +465 -0
  174. fla/ops/utils/index.py +133 -0
  175. fla/ops/utils/logcumsumexp.py +52 -0
  176. fla/ops/utils/logsumexp.py +80 -0
  177. fla/ops/utils/matmul.py +245 -0
  178. fla/ops/utils/op.py +62 -0
  179. fla/ops/utils/pack.py +208 -0
  180. fla/ops/utils/pooling.py +207 -0
  181. fla/ops/utils/softmax.py +106 -0
  182. fla/ops/utils/solve_tril.py +381 -0
  183. fla/utils.py +462 -0
  184. fla_core-0.3.2.dist-info/METADATA +575 -0
  185. fla_core-0.3.2.dist-info/RECORD +188 -0
  186. fla_core-0.3.2.dist-info/WHEEL +5 -0
  187. fla_core-0.3.2.dist-info/licenses/LICENSE +21 -0
  188. fla_core-0.3.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,209 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ import torch
5
+ import triton
6
+ import triton.language as tl
7
+
8
+ from fla.ops.mesa_net.chunk_h_kv_intra_bwd_separate import chunk_mesa_net_h_kv_bwd_intra_separate_fn
9
+ from fla.ops.utils import prepare_chunk_indices
10
+ from fla.ops.utils.op import exp
11
+ from fla.utils import check_shared_mem, is_nvidia_hopper
12
+
13
+ NUM_WARPS = [2, 4] if is_nvidia_hopper else [2, 4, 8]
14
+
15
+
16
+ @triton.heuristics({
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 NUM_WARPS
23
+ for num_stages in [2, 3, 4]
24
+ ],
25
+ key=['H', 'K', 'V', 'BT', 'BK', 'BV'],
26
+ )
27
+ @triton.jit(do_not_specialize=['T'])
28
+ def chunk_mesa_net_h_kv_bwd_intra_kernel(
29
+ q_star,
30
+ k,
31
+ v,
32
+ beta,
33
+ h_kv,
34
+ g,
35
+ do,
36
+ dh_kv,
37
+ dq,
38
+ dk_beta,
39
+ dg,
40
+ dv,
41
+ cu_seqlens,
42
+ chunk_indices,
43
+ B: tl.constexpr,
44
+ T,
45
+ H: tl.constexpr,
46
+ K: tl.constexpr,
47
+ V: tl.constexpr,
48
+ BT: tl.constexpr,
49
+ BK: tl.constexpr,
50
+ BV: tl.constexpr,
51
+ IS_VARLEN: tl.constexpr,
52
+ ):
53
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
54
+ i_b, i_h = i_bh // H, i_bh % H
55
+ if IS_VARLEN:
56
+ i_tg = i_t
57
+ 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)
58
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
59
+ T = eos - bos
60
+ NT = tl.cdiv(T, BT)
61
+ else:
62
+ NT = tl.cdiv(T, BT)
63
+ i_tg = i_b * NT + i_t
64
+ bos, eos = i_b * T, i_b * T + T
65
+
66
+ o_t = i_t * BT + tl.arange(0, BT)
67
+ m_t = o_t < T
68
+
69
+ # offset calculation
70
+ v += (bos * H + i_h) * V
71
+ do += (bos * H + i_h) * V
72
+ h_kv += (i_tg * H + i_h).to(tl.int64) * K*V
73
+ dh_kv += (i_tg * H + i_h).to(tl.int64) * K*V
74
+ q_star += (bos * H + i_h) * K
75
+ k += (bos * H + i_h) * K
76
+ beta += (bos * H + i_h)
77
+ g += bos * H + i_h
78
+ dg += bos * H + i_h
79
+ dq += (bos * H + i_h) * K
80
+ dk_beta += (bos * H + i_h) * K
81
+ dv += (bos * H + i_h) * V
82
+
83
+ b_dq = tl.zeros([BT, BK], dtype=tl.float32)
84
+ b_dk = tl.zeros([BT, BK], dtype=tl.float32)
85
+ b_ds = tl.zeros([BT, BT], dtype=tl.float32)
86
+ b_dv = tl.zeros([BT, BK], dtype=tl.float32)
87
+ b_dg_last = tl.zeros([1,], dtype=tl.float32)
88
+ b_dg = tl.zeros([BT,], dtype=tl.float32)
89
+
90
+ p_q = tl.make_block_ptr(q_star, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
91
+ p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
92
+ p_v = tl.make_block_ptr(v, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
93
+ p_g = tl.make_block_ptr(g, (T,), (H,), (i_t * BT,), (BT,), (0,))
94
+ p_beta = tl.make_block_ptr(beta, (T, ), (H, ), (i_t * BT,), (BT,), (0,))
95
+ p_do = tl.make_block_ptr(do, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
96
+ p_h = tl.make_block_ptr(h_kv, (V, K), (1, V), (0, 0), (BV, BK), (0, 1))
97
+ p_dh = tl.make_block_ptr(dh_kv, (V, K), (1, V), (0, 0), (BV, BK), (0, 1))
98
+
99
+ b_q = tl.load(p_q, boundary_check=(0, 1))
100
+ b_k = tl.load(p_k, boundary_check=(0, 1))
101
+ b_v = tl.load(p_v, boundary_check=(0, 1))
102
+ b_g = tl.load(p_g, boundary_check=(0,))
103
+ b_beta = tl.load(p_beta, boundary_check=(0, ))
104
+ b_do = tl.load(p_do, boundary_check=(0, 1))
105
+ b_h = tl.load(p_h, boundary_check=(0, 1))
106
+ b_dh = tl.load(p_dh, boundary_check=(0, 1))
107
+ b_g_last = tl.load(g + (min(i_t * BT + BT, T) - 1) * H)
108
+
109
+ # calculation
110
+ b_dg_last += tl.sum(b_h * b_dh)
111
+ b_dg_last *= exp(b_g_last)
112
+
113
+ b_m = tl.where((o_t[:, None] >= o_t[None, :]) & (m_t[:, None] & m_t[None, :]), exp(b_g[:, None] - b_g[None, :]), 0)
114
+ b_k = (b_k * b_beta[:, None]).to(b_k.dtype)
115
+ b_s = tl.dot(b_q, tl.trans(b_k)) * b_m
116
+
117
+ b_ds = tl.dot(b_do, tl.trans(b_v))
118
+ b_dm = b_s * b_ds
119
+ b_dm = tl.where(tl.arange(0, BT)[:, None] >= tl.arange(0, BT)[None, :], b_dm, 0)
120
+
121
+ b_dg += tl.sum(b_dm, axis=1)
122
+ b_dg -= tl.sum(b_dm, axis=0)
123
+
124
+ b_g_exp_q = exp(b_g)
125
+ b_g_exp_k = tl.where(m_t, exp(-b_g + b_g_last), 0)
126
+ b_ds = b_ds * b_m
127
+ b_dq += tl.dot(b_do, b_h.to(b_do.dtype)) * b_g_exp_q[:, None]
128
+ b_dk += tl.dot(b_v, b_dh.to(b_v.dtype)) * b_g_exp_k[:, None]
129
+ b_dg_last += tl.sum(b_dk * b_k)
130
+ b_dg -= tl.sum(b_dk * b_k, axis=1)
131
+ b_dg += tl.sum(b_dq * b_q, axis=1)
132
+ b_dq += tl.dot(b_ds.to(b_k.dtype), b_k)
133
+ b_dv += tl.dot(b_k, tl.trans(b_dh).to(b_k.dtype)) * b_g_exp_k[:, None] + tl.dot(tl.trans(b_s.to(b_do.dtype)), b_do)
134
+ b_dk += tl.dot(tl.trans(b_ds.to(b_q.dtype)), b_q)
135
+
136
+ b_dg = tl.where(o_t < min(i_t * BT + BT, T) - 1, b_dg, b_dg + b_dg_last)
137
+ p_dq = tl.make_block_ptr(dq, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
138
+ p_dk = tl.make_block_ptr(dk_beta, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
139
+ p_dv = tl.make_block_ptr(dv, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
140
+ p_dg = tl.make_block_ptr(dg, (T,), (H,), (i_t * BT,), (BT,), (0,))
141
+ tl.store(p_dq, b_dq.to(p_dq.dtype.element_ty), boundary_check=(0, 1))
142
+ tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
143
+ tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
144
+ tl.store(p_dg, b_dg.to(p_dg.dtype.element_ty), boundary_check=(0,))
145
+
146
+
147
+ def chunk_mesa_net_h_kv_bwd_intra_fn(
148
+ q_star,
149
+ k,
150
+ v,
151
+ beta,
152
+ h_kv,
153
+ dh_kv,
154
+ g,
155
+ do,
156
+ cu_seqlens,
157
+ chunk_size=64
158
+ ):
159
+ # share memory is not large enough for a single fused kernel
160
+ if not check_shared_mem('ampere'):
161
+ return chunk_mesa_net_h_kv_bwd_intra_separate_fn(
162
+ q_star=q_star,
163
+ k=k,
164
+ v=v,
165
+ beta=beta,
166
+ h_kv=h_kv,
167
+ dh_kv=dh_kv,
168
+ g=g,
169
+ do=do,
170
+ cu_seqlens=cu_seqlens,
171
+ chunk_size=chunk_size
172
+ )
173
+ B, T, H, K, V = *k.shape, v.shape[-1]
174
+ BT = chunk_size
175
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
176
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
177
+
178
+ BK = max(triton.next_power_of_2(K), 16)
179
+ BV = max(triton.next_power_of_2(V), 16)
180
+ dq = torch.empty_like(q_star, dtype=torch.float32)
181
+ dk = torch.empty_like(k)
182
+ dv = torch.empty_like(v)
183
+ dg = torch.empty_like(g)
184
+ grid = (NT, B * H)
185
+ chunk_mesa_net_h_kv_bwd_intra_kernel[grid](
186
+ q_star=q_star,
187
+ k=k,
188
+ v=v,
189
+ beta=beta,
190
+ h_kv=h_kv,
191
+ g=g,
192
+ do=do,
193
+ dh_kv=dh_kv,
194
+ dq=dq,
195
+ dk_beta=dk,
196
+ dg=dg,
197
+ dv=dv,
198
+ cu_seqlens=cu_seqlens,
199
+ chunk_indices=chunk_indices,
200
+ B=B,
201
+ T=T,
202
+ H=H,
203
+ K=K,
204
+ V=V,
205
+ BT=BT,
206
+ BK=BK,
207
+ BV=BV
208
+ )
209
+ return dq, dk, dv, dg
@@ -0,0 +1,300 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ import torch
5
+ import triton
6
+ import triton.language as tl
7
+
8
+ from fla.ops.utils import prepare_chunk_indices
9
+ from fla.ops.utils.op import exp
10
+ from fla.utils import is_nvidia_hopper
11
+
12
+ NUM_WARPS = [2, 4] if is_nvidia_hopper else [2, 4, 8]
13
+
14
+
15
+ @triton.heuristics({
16
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
17
+ })
18
+ @triton.autotune(
19
+ configs=[
20
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
21
+ for num_warps in NUM_WARPS
22
+ for num_stages in [2, 3, 4]
23
+ ],
24
+ key=['H', 'K', 'V', 'BT', 'BK', 'BV'],
25
+ )
26
+ @triton.jit(do_not_specialize=['T'])
27
+ def chunk_mesa_net_h_kv_bwd_intra_kernel_dkv(
28
+ q_star,
29
+ k,
30
+ v,
31
+ beta,
32
+ h_kv,
33
+ g,
34
+ do,
35
+ dh_kv,
36
+ dk_beta,
37
+ dg,
38
+ dv,
39
+ cu_seqlens,
40
+ chunk_indices,
41
+ B: tl.constexpr,
42
+ T,
43
+ H: tl.constexpr,
44
+ K: tl.constexpr,
45
+ V: tl.constexpr,
46
+ BT: tl.constexpr,
47
+ BK: tl.constexpr,
48
+ BV: tl.constexpr,
49
+ IS_VARLEN: tl.constexpr,
50
+ ):
51
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
52
+ i_b, i_h = i_bh // H, i_bh % H
53
+ if IS_VARLEN:
54
+ i_tg = i_t
55
+ 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)
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
+ else:
60
+ NT = tl.cdiv(T, BT)
61
+ i_tg = i_b * NT + i_t
62
+ bos, eos = i_b * T, i_b * T + T
63
+
64
+ o_t = i_t * BT + tl.arange(0, BT)
65
+ m_t = o_t < T
66
+
67
+ # offset calculation
68
+ v += (bos * H + i_h) * V
69
+ do += (bos * H + i_h) * V
70
+ h_kv += (i_tg * H + i_h).to(tl.int64) * K*V
71
+ dh_kv += (i_tg * H + i_h).to(tl.int64) * K*V
72
+ q_star += (bos * H + i_h) * K
73
+ k += (bos * H + i_h) * K
74
+ beta += (bos * H + i_h)
75
+ g += bos * H + i_h
76
+ dg += bos * H + i_h
77
+ dk_beta += (bos * H + i_h) * K
78
+ dv += (bos * H + i_h) * V
79
+
80
+ b_dk = tl.zeros([BT, BK], dtype=tl.float32)
81
+ b_ds = tl.zeros([BT, BT], dtype=tl.float32)
82
+ b_dv = tl.zeros([BT, BK], dtype=tl.float32)
83
+ b_dg_last = tl.zeros([1,], dtype=tl.float32)
84
+ b_dg = tl.zeros([BT,], dtype=tl.float32)
85
+
86
+ p_v = tl.make_block_ptr(v, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
87
+ p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
88
+ p_beta = tl.make_block_ptr(beta, (T, ), (H, ), (i_t * BT,), (BT,), (0,))
89
+ p_do = tl.make_block_ptr(do, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
90
+ p_h = tl.make_block_ptr(h_kv, (V, K), (1, V), (0, 0), (BV, BK), (0, 1))
91
+ p_dh = tl.make_block_ptr(dh_kv, (V, K), (1, V), (0, 0), (BV, BK), (0, 1))
92
+ p_q = tl.make_block_ptr(q_star, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
93
+ p_g = tl.make_block_ptr(g, (T,), (H,), (i_t * BT,), (BT,), (0,))
94
+
95
+ b_q = tl.load(p_q, boundary_check=(0, 1))
96
+ b_v = tl.load(p_v, boundary_check=(0, 1))
97
+ b_k = tl.load(p_k, boundary_check=(0, 1))
98
+ b_beta = tl.load(p_beta, boundary_check=(0, ))
99
+ b_g = tl.load(p_g, boundary_check=(0,))
100
+ b_do = tl.load(p_do, boundary_check=(0, 1))
101
+ b_h = tl.load(p_h, boundary_check=(0, 1))
102
+ b_dh = tl.load(p_dh, boundary_check=(0, 1))
103
+ b_g_last = tl.load(g + (min(i_t * BT + BT, T) - 1) * H)
104
+
105
+ # calculation
106
+ b_dg_last += tl.sum(b_h * b_dh)
107
+ b_dg_last *= exp(b_g_last)
108
+
109
+ b_m = tl.where((o_t[:, None] >= o_t[None, :]) & (m_t[:, None] & m_t[None, :]), exp(b_g[:, None] - b_g[None, :]), 0)
110
+ b_k = (b_k * b_beta[:, None]).to(b_k.dtype)
111
+ b_s = tl.dot(b_q, tl.trans(b_k)) * b_m
112
+ b_ds = tl.dot(b_do, tl.trans(b_v))
113
+ b_dm = b_s * b_ds
114
+ b_dm = tl.where(tl.arange(0, BT)[:, None] >= tl.arange(0, BT)[None, :], b_dm, 0)
115
+ b_dg += tl.sum(b_dm, axis=1)
116
+ b_dg -= tl.sum(b_dm, axis=0)
117
+ b_g_exp_k = tl.where(m_t, exp(-b_g + b_g_last), 0)
118
+ b_ds = b_ds * b_m
119
+ b_dk += tl.dot(b_v, b_dh.to(b_v.dtype)) * b_g_exp_k[:, None]
120
+ b_dg_last += tl.sum(b_dk * b_k)
121
+ b_dg -= tl.sum(b_dk * b_k, axis=1)
122
+ b_dv += tl.dot(b_k, tl.trans(b_dh).to(b_k.dtype)) * b_g_exp_k[:, None] + tl.dot(tl.trans(b_s.to(b_do.dtype)), b_do)
123
+ b_dk += tl.dot(tl.trans(b_ds.to(b_q.dtype)), b_q)
124
+ b_dg = tl.where(o_t < min(i_t * BT + BT, T) - 1, b_dg, b_dg + b_dg_last)
125
+ p_dk = tl.make_block_ptr(dk_beta, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
126
+ p_dv = tl.make_block_ptr(dv, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
127
+ p_dg = tl.make_block_ptr(dg, (T,), (H,), (i_t * BT,), (BT,), (0,))
128
+ tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
129
+ tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
130
+ tl.store(p_dg, b_dg.to(p_dg.dtype.element_ty), boundary_check=(0,))
131
+
132
+
133
+ @triton.heuristics({
134
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
135
+ })
136
+ @triton.autotune(
137
+ configs=[
138
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
139
+ for num_warps in NUM_WARPS
140
+ for num_stages in [2, 3, 4]
141
+ ],
142
+ key=['H', 'K', 'V', 'BT', 'BK', 'BV'],
143
+ )
144
+ @triton.jit(do_not_specialize=['T'])
145
+ def chunk_mesa_net_h_kv_bwd_intra_kernel_dq(
146
+ q_star,
147
+ k,
148
+ v,
149
+ beta,
150
+ h_kv,
151
+ g,
152
+ do,
153
+ dq,
154
+ dg_prev,
155
+ dg,
156
+ cu_seqlens,
157
+ chunk_indices,
158
+ B: tl.constexpr,
159
+ T,
160
+ H: tl.constexpr,
161
+ K: tl.constexpr,
162
+ V: tl.constexpr,
163
+ BT: tl.constexpr,
164
+ BK: tl.constexpr,
165
+ BV: tl.constexpr,
166
+ IS_VARLEN: tl.constexpr,
167
+ ):
168
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
169
+ i_b, i_h = i_bh // H, i_bh % H
170
+ if IS_VARLEN:
171
+ i_tg = i_t
172
+ 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)
173
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
174
+ T = eos - bos
175
+ NT = tl.cdiv(T, BT)
176
+ else:
177
+ NT = tl.cdiv(T, BT)
178
+ i_tg = i_b * NT + i_t
179
+ bos, eos = i_b * T, i_b * T + T
180
+
181
+ o_t = i_t * BT + tl.arange(0, BT)
182
+ m_t = o_t < T
183
+
184
+ # offset calculation
185
+ v += (bos * H + i_h) * V
186
+ do += (bos * H + i_h) * V
187
+ h_kv += (i_tg * H + i_h).to(tl.int64) * K*V
188
+ q_star += (bos * H + i_h) * K
189
+ k += (bos * H + i_h) * K
190
+ beta += (bos * H + i_h)
191
+ g += bos * H + i_h
192
+ dg_prev += bos * H + i_h
193
+ dg += bos * H + i_h
194
+ dq += (bos * H + i_h) * K
195
+
196
+ b_dq = tl.zeros([BT, BK], dtype=tl.float32)
197
+
198
+ p_v = tl.make_block_ptr(v, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
199
+ p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
200
+ p_beta = tl.make_block_ptr(beta, (T, ), (H, ), (i_t * BT,), (BT,), (0,))
201
+ p_do = tl.make_block_ptr(do, (T, V), (H*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
202
+ p_h = tl.make_block_ptr(h_kv, (V, K), (1, V), (0, 0), (BV, BK), (0, 1))
203
+ p_q = tl.make_block_ptr(q_star, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
204
+ p_g = tl.make_block_ptr(g, (T,), (H,), (i_t * BT,), (BT,), (0,))
205
+ p_dg_prev = tl.make_block_ptr(dg_prev, (T,), (H,), (i_t * BT,), (BT,), (0,))
206
+ p_dg = tl.make_block_ptr(dg, (T,), (H,), (i_t * BT,), (BT,), (0,))
207
+ p_dq = tl.make_block_ptr(dq, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
208
+
209
+ b_q = tl.load(p_q, boundary_check=(0, 1))
210
+ b_v = tl.load(p_v, boundary_check=(0, 1))
211
+ b_k = tl.load(p_k, boundary_check=(0, 1))
212
+ b_beta = tl.load(p_beta, boundary_check=(0, ))
213
+ b_g = tl.load(p_g, boundary_check=(0,))
214
+ b_do = tl.load(p_do, boundary_check=(0, 1))
215
+ b_h = tl.load(p_h, boundary_check=(0, 1))
216
+
217
+ b_m = tl.where((o_t[:, None] >= o_t[None, :]) & (m_t[:, None] & m_t[None, :]), exp(b_g[:, None] - b_g[None, :]), 0)
218
+ b_k = (b_k * b_beta[:, None]).to(b_k.dtype)
219
+
220
+ b_ds = tl.dot(b_do, tl.trans(b_v)) * b_m
221
+ b_g_exp_q = exp(b_g)
222
+ b_dq = tl.dot(b_do, b_h.to(b_do.dtype)) * b_g_exp_q[:, None]
223
+ b_dg = tl.sum(b_dq * b_q, axis=1) + tl.load(p_dg_prev, boundary_check=(0,))
224
+ b_dq += tl.dot(b_ds.to(b_k.dtype), b_k)
225
+
226
+ tl.store(p_dq, b_dq.to(p_dq.dtype.element_ty), boundary_check=(0, 1))
227
+ tl.store(p_dg, b_dg.to(p_dg.dtype.element_ty), boundary_check=(0,))
228
+
229
+
230
+ def chunk_mesa_net_h_kv_bwd_intra_separate_fn(
231
+ q_star,
232
+ k,
233
+ v,
234
+ beta,
235
+ h_kv,
236
+ dh_kv,
237
+ g,
238
+ do,
239
+ cu_seqlens,
240
+ chunk_size=64
241
+ ):
242
+ B, T, H, K, V = *k.shape, v.shape[-1]
243
+ BT = chunk_size
244
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
245
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
246
+
247
+ BK = max(triton.next_power_of_2(K), 16)
248
+ BV = max(triton.next_power_of_2(V), 16)
249
+ dq = torch.empty_like(q_star, dtype=torch.float32)
250
+ dk = torch.empty_like(k)
251
+ dv = torch.empty_like(v)
252
+ dg = torch.empty_like(g)
253
+ grid = (NT, B * H)
254
+ chunk_mesa_net_h_kv_bwd_intra_kernel_dkv[grid](
255
+ q_star=q_star,
256
+ k=k,
257
+ v=v,
258
+ beta=beta,
259
+ h_kv=h_kv,
260
+ g=g,
261
+ do=do,
262
+ dh_kv=dh_kv,
263
+ dk_beta=dk,
264
+ dg=dg,
265
+ dv=dv,
266
+ cu_seqlens=cu_seqlens,
267
+ chunk_indices=chunk_indices,
268
+ B=B,
269
+ T=T,
270
+ H=H,
271
+ K=K,
272
+ V=V,
273
+ BT=BT,
274
+ BK=BK,
275
+ BV=BV
276
+ )
277
+ dg_final = torch.empty_like(dg)
278
+ chunk_mesa_net_h_kv_bwd_intra_kernel_dq[grid](
279
+ q_star=q_star,
280
+ k=k,
281
+ v=v,
282
+ beta=beta,
283
+ h_kv=h_kv,
284
+ g=g,
285
+ do=do,
286
+ dg=dg_final,
287
+ dg_prev=dg,
288
+ dq=dq,
289
+ cu_seqlens=cu_seqlens,
290
+ chunk_indices=chunk_indices,
291
+ B=B,
292
+ T=T,
293
+ H=H,
294
+ K=K,
295
+ V=V,
296
+ BT=BT,
297
+ BK=BK,
298
+ BV=BV
299
+ )
300
+ return dq, dk, dv, dg_final
@@ -0,0 +1,175 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ import torch
5
+ import triton
6
+ import triton.language as tl
7
+
8
+ from fla.ops.utils.op import exp
9
+ from fla.utils import input_guard
10
+
11
+
12
+ @triton.jit
13
+ def mesa_net_decoding_one_step_kernel(
14
+ q,
15
+ k,
16
+ v,
17
+ g,
18
+ o,
19
+ lamb,
20
+ beta,
21
+ prev_h_kk,
22
+ prev_h_kv,
23
+ curr_h_kk,
24
+ curr_h_kv,
25
+ B: tl.constexpr,
26
+ H: tl.constexpr,
27
+ K: tl.constexpr,
28
+ V: tl.constexpr,
29
+ BK: tl.constexpr,
30
+ BV: tl.constexpr,
31
+ MAX_CG_STEP: tl.constexpr,
32
+ ):
33
+ i_nh = tl.program_id(0)
34
+ i_h = i_nh % H
35
+
36
+ o_k = tl.arange(0, BK)
37
+ o_v = tl.arange(0, BV)
38
+
39
+ p_q = q + i_nh * K + o_k
40
+ p_k = k + i_nh * K + o_k
41
+ p_v = v + i_nh * V + o_v
42
+ p_beta = beta + i_nh
43
+ p_g = g + i_nh
44
+ p_lamb = lamb + i_h * K + o_k
45
+
46
+ b_g = exp(tl.load(p_g).to(tl.float32))
47
+ b_beta = tl.load(p_beta).to(tl.float32)
48
+
49
+ mask_k = o_k < K
50
+ mask_v = o_v < V
51
+ mask_kk = mask_k[:, None] & mask_k[None, :]
52
+ mask_kv = mask_k[:, None] & mask_v[None, :]
53
+
54
+ b_k = tl.load(p_k, mask=mask_k, other=0).to(tl.float32)
55
+ b_v = tl.load(p_v, mask=mask_v, other=0).to(tl.float32)
56
+ b_q = tl.load(p_q, mask=mask_k, other=0).to(tl.float32)
57
+ b_lamb = tl.load(p_lamb, mask=mask_k, other=0).to(tl.float32)
58
+
59
+ p_hkk_prev = prev_h_kk + i_nh * K * K + o_k[:, None] * K + o_k[None, :]
60
+ b_h_kk = tl.load(p_hkk_prev, mask=mask_kk, other=0).to(tl.float32)
61
+
62
+ b_h_kk = b_h_kk * b_g + (b_k * b_beta)[:, None] * b_k[None, :]
63
+
64
+ p_hkk_curr = curr_h_kk + i_nh * K * K + o_k[:, None] * K + o_k[None, :]
65
+ tl.store(p_hkk_curr, b_h_kk.to(p_hkk_curr.dtype.element_ty), mask=mask_kk)
66
+
67
+ p_hkv_prev = prev_h_kv + i_nh * K * V + o_k[:, None] * V + o_v[None, :]
68
+ b_h_kv = tl.load(p_hkv_prev, mask=mask_kv, other=0).to(tl.float32)
69
+ b_h_kv = b_h_kv * b_g + (b_k * b_beta)[:, None] * b_v[None, :]
70
+ p_hkv_curr = curr_h_kv + i_nh * K * V + o_k[:, None] * V + o_v[None, :]
71
+ tl.store(p_hkv_curr, b_h_kv.to(p_hkv_curr.dtype.element_ty), mask=mask_kv)
72
+
73
+ diag_mask = tl.arange(0, BK)[:, None] == tl.arange(0, BK)[None, :]
74
+ diag_mask = diag_mask & mask_kk
75
+ b_h_kk_diag = tl.sum(tl.where(diag_mask, b_h_kk, 0.0), axis=1)
76
+
77
+ b_x = b_q / (b_h_kk_diag + b_lamb + 1e-5)
78
+ b_Hx = tl.sum(b_h_kk * b_x[:, None], axis=0)
79
+ b_r = b_q - b_Hx - b_lamb * b_x
80
+ b_p = tl.zeros([BK,], dtype=tl.float32)
81
+ b_p += b_r
82
+ delta_old = tl.sum(b_r * b_r)
83
+
84
+ for i_iter in range(MAX_CG_STEP):
85
+ b_Ap = tl.sum(b_h_kk * b_p[:, None], axis=0) + b_lamb * b_p
86
+ pAp = tl.sum(b_p * b_Ap)
87
+ alpha = delta_old / (pAp + 1e-5)
88
+ b_x = b_x + alpha * b_p
89
+ b_r = b_r - alpha * b_Ap
90
+ delta_new = tl.sum(b_r * b_r)
91
+ beta_cg = delta_new / (delta_old + 1e-5)
92
+ b_p = b_r + beta_cg * b_p
93
+ delta_old = delta_new
94
+ b_o = tl.sum(b_h_kv * b_x[:, None], axis=0)
95
+ p_o = o + i_nh * V + o_v
96
+ tl.store(p_o, b_o.to(p_o.dtype.element_ty), mask=mask_v)
97
+
98
+
99
+ @input_guard
100
+ def mesa_net_decoding_one_step(
101
+ q: torch.Tensor,
102
+ k: torch.Tensor,
103
+ v: torch.Tensor,
104
+ g: torch.Tensor,
105
+ lamb: torch.Tensor,
106
+ beta: torch.Tensor,
107
+ prev_h_kk: torch.Tensor,
108
+ prev_h_kv: torch.Tensor,
109
+ max_CG_iteration: int = 30
110
+ ):
111
+ """
112
+ Triton implementation of Mesa Net CG one step
113
+
114
+ Args:
115
+ q (torch.Tensor):
116
+ query tensor [B, H, K]
117
+ k (torch.Tensor):
118
+ key tensor [B, H, K]
119
+ v (torch.Tensor):
120
+ value tensor [B, H, V]
121
+ g (torch.Tensor):
122
+ gate tensor [B, H]
123
+ lamb (torch.Tensor):
124
+ lambda tensor [H, K]
125
+ beta (torch.Tensor):
126
+ beta tensor [B, H]
127
+ prev_h_kk (torch.Tensor):
128
+ previous hidden state KK [B, H, K, K]
129
+ prev_h_kv (torch.Tensor):
130
+ previous hidden state KV [B, H, K, V]
131
+ max_CG_iteration (int):
132
+ maximum CG iterations
133
+
134
+ Returns:
135
+ o (torch.Tensor):
136
+ output tensor [B, H, V]
137
+ h_kk_new (torch.Tensor):
138
+ updated hidden state KK [B, H, K, K]
139
+ h_kv_new (torch.Tensor):
140
+ updated hidden state KV [B, H, K, V]
141
+ """
142
+ B, H, K, V = *q.shape, v.shape[-1]
143
+
144
+ o = torch.empty((B, H, V), dtype=q.dtype, device=q.device)
145
+ curr_h_kk = torch.empty_like(prev_h_kk)
146
+ curr_h_kv = torch.empty_like(prev_h_kv)
147
+
148
+ BK = max(triton.next_power_of_2(K), 16)
149
+ BV = max(triton.next_power_of_2(V), 16)
150
+
151
+ assert BK <= 128 and BV <= 128, "BK and BV must be less than or equal to 128"
152
+
153
+ grid = (B * H,)
154
+ mesa_net_decoding_one_step_kernel[grid](
155
+ q=q,
156
+ k=k,
157
+ v=v,
158
+ g=g,
159
+ o=o,
160
+ lamb=lamb,
161
+ beta=beta,
162
+ prev_h_kk=prev_h_kk,
163
+ prev_h_kv=prev_h_kv,
164
+ curr_h_kk=curr_h_kk,
165
+ curr_h_kv=curr_h_kv,
166
+ B=B,
167
+ H=H,
168
+ K=K,
169
+ V=V,
170
+ BK=BK,
171
+ BV=BV,
172
+ MAX_CG_STEP=max_CG_iteration,
173
+ num_warps=4 if BK <= 64 else 8
174
+ )
175
+ return o, curr_h_kk, curr_h_kv