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,195 @@
1
+ import torch
2
+ import triton
3
+ import triton.language as tl
4
+
5
+ from fla.ops.utils import prepare_chunk_indices
6
+
7
+
8
+ @triton.heuristics({
9
+ 'USE_GATE': lambda args: args['g_cumsum'] is not None,
10
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
11
+ })
12
+ @triton.jit(do_not_specialize=['T'])
13
+ def parallel_path_fwd_kernel(
14
+ q,
15
+ k,
16
+ v,
17
+ o,
18
+ o_new,
19
+ g_cumsum,
20
+ w1,
21
+ w2,
22
+ scale,
23
+ L,
24
+ L_new,
25
+ M,
26
+ cu_seqlens,
27
+ indices,
28
+ T,
29
+ G: tl.constexpr,
30
+ HQ: tl.constexpr,
31
+ H: tl.constexpr,
32
+ K: tl.constexpr,
33
+ V: tl.constexpr,
34
+ BT: tl.constexpr,
35
+ BS: tl.constexpr,
36
+ BK: tl.constexpr,
37
+ BV: tl.constexpr,
38
+ USE_GATE: tl.constexpr,
39
+ IS_VARLEN: tl.constexpr,
40
+ ):
41
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
42
+ i_b, i_hq = i_bh // HQ, i_bh % HQ
43
+ i_h = i_hq // G
44
+
45
+ if IS_VARLEN:
46
+ i_n, i_t = tl.load(indices + i_t * 2).to(tl.int32), tl.load(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
+ i_n = i_b
51
+ bos, eos = i_n * T, i_n * T + T
52
+
53
+ p_q = tl.make_block_ptr(q + (bos * HQ + i_hq) * K, (T, K), (HQ*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
54
+ b_q = tl.zeros([BT, BK], dtype=tl.float32)
55
+ b_q += tl.load(p_q, boundary_check=(0, 1))
56
+ sm_scale = scale * 1.44269504
57
+ b_o = tl.zeros([BT, BV], dtype=tl.float32)
58
+ p_o = tl.make_block_ptr(o + (bos * HQ + i_hq) * V, (T, V), (HQ*V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
59
+ b_o += tl.load(p_o, boundary_check=(0, 1))
60
+
61
+ p_L = tl.make_block_ptr(L + bos * HQ + i_hq, (T, ), (HQ, ), (i_t * BT, ), (BT, ), (0,))
62
+ p_M = tl.make_block_ptr(M + bos * HQ + i_hq, (T, ), (HQ, ), (i_t * BT, ), (BT, ), (0,))
63
+ b_l = tl.load(p_L, boundary_check=(0,))
64
+ b_m = tl.load(p_M, boundary_check=(0,))
65
+
66
+ if USE_GATE:
67
+ p_g_cumsum_q = tl.make_block_ptr(g_cumsum + bos * HQ + i_hq, (T, ), (HQ, ), (i_t * BT, ), (BT, ), (0,))
68
+ b_g_cumsum_q = tl.load(p_g_cumsum_q, boundary_check=(0,))
69
+ else:
70
+ b_g_cumsum_q = None
71
+
72
+ for offset in range((i_t + 1) * BT - 2 * BS, i_t*BT-BS, -BS):
73
+ p_k = tl.make_block_ptr(k + (bos * H + i_h) * K, (K, T), (1, K*H), (0, offset), (BK, BS), (0, 1)) # GQA when H!=HQ
74
+ p_v = tl.make_block_ptr(v + (bos * H + i_h) * V, (T, V), (V*H, 1), (offset, 0), (BS, BV), (1, 0)) # GQA when H!=HQ
75
+ p_w1 = tl.make_block_ptr(w1 + (bos * H + i_h) * K, (K, T), (1, K*H), (0, offset), (BK, BS), (0, 1))
76
+ p_w2 = tl.make_block_ptr(w2 + (bos * H + i_h) * K, (T, K), (K*H, 1), (offset, 0), (BS, BK), (1, 0))
77
+ # [BK, BS]
78
+
79
+ b_k = tl.load(p_k, boundary_check=(0, 1))
80
+ # [BS, BV]
81
+ b_v = tl.load(p_v, boundary_check=(0, 1))
82
+ # [BK, BK]
83
+ b_w1 = tl.load(p_w1, boundary_check=(0, 1))
84
+ b_w2 = tl.load(p_w2, boundary_check=(0, 1))
85
+ # [BT, BS]
86
+ m_s = i_t * BT + tl.arange(0, BT) >= (offset + BS)
87
+ b_s = tl.dot(b_q.to(b_k.dtype), b_k)
88
+
89
+ if USE_GATE:
90
+ p_g_cumsum_k = tl.make_block_ptr(g_cumsum + (bos * HQ + i_hq), (T, ), (HQ, ), (offset, ), (BS, ), (0,))
91
+ b_g_cumsum_k = tl.load(p_g_cumsum_k, boundary_check=(0,))
92
+ b_s = b_s + b_g_cumsum_q[:, None] - b_g_cumsum_k[None, :]
93
+ b_s = tl.where(m_s[:, None], b_s * sm_scale, float("-inf"))
94
+ b_m_new = tl.maximum(b_m, tl.max(b_s, 1))
95
+ alpha = tl.math.exp2((b_m - b_m_new))
96
+ b_s = tl.math.exp2(b_s - b_m_new[:, None])
97
+ b_o *= alpha[:, None]
98
+ b_l = b_l * alpha + tl.sum(b_s, 1)
99
+ b_m = b_m_new
100
+ b_o += tl.dot(b_s.to(b_v.dtype), b_v)
101
+ b_s2 = tl.dot(b_q.to(b_w1.dtype), b_w1)
102
+ b_s2 = tl.where(m_s[:, None], b_s2, 0)
103
+ b_q -= tl.dot(b_s2.to(b_w2.dtype), b_w2)
104
+
105
+ tl.debug_barrier()
106
+
107
+ for offset in range(i_t * BT - BS, -BS, -BS):
108
+ p_k = tl.make_block_ptr(k + (bos * H + i_h) * K, (K, T), (1, K*H), (0, offset), (BK, BS), (0, 1)) # GQA when H!=HQ
109
+ p_v = tl.make_block_ptr(v + (bos * H + i_h) * V, (T, V), (V*H, 1), (offset, 0), (BS, BV), (1, 0)) # GQA when H!=HQ
110
+ p_w1 = tl.make_block_ptr(w1 + (bos * H + i_h) * K, (K, T), (1, K*H), (0, offset), (BK, BS), (0, 1))
111
+ p_w2 = tl.make_block_ptr(w2 + (bos * H + i_h) * K, (T, K), (K*H, 1), (offset, 0), (BS, BK), (1, 0))
112
+ # [BK, BS]
113
+ b_k = tl.load(p_k, boundary_check=(0, 1))
114
+ # [BS, BV]
115
+ b_v = tl.load(p_v, boundary_check=(0, 1))
116
+ b_w1 = tl.load(p_w1, boundary_check=(0, 1))
117
+ b_w2 = tl.load(p_w2, boundary_check=(0, 1))
118
+ # [BT, BS]
119
+ b_s = tl.dot(b_q.to(b_k.dtype), b_k)
120
+ if USE_GATE:
121
+ p_g_cumsum_k = tl.make_block_ptr(g_cumsum + (bos * HQ + i_hq), (T, ), (HQ, ), (offset, ), (BS, ), (0,))
122
+ b_g_cumsum_k = tl.load(p_g_cumsum_k, boundary_check=(0,))
123
+ b_s = b_s + b_g_cumsum_q[:, None] - b_g_cumsum_k[None, :]
124
+ b_s = b_s * sm_scale
125
+ b_m_new = tl.maximum(b_m, tl.max(b_s, 1))
126
+ alpha = tl.math.exp2((b_m - b_m_new))
127
+ b_s = tl.math.exp2(b_s - b_m_new[:, None])
128
+ b_o *= alpha[:, None]
129
+ b_l = b_l * alpha + tl.sum(b_s, 1)
130
+ b_m = b_m_new
131
+ b_o += tl.dot(b_s.to(b_v.dtype), b_v)
132
+ b_s2 = tl.dot(b_q.to(b_w1.dtype), b_w1)
133
+ b_q -= tl.dot(b_s2.to(b_w2.dtype), b_w2)
134
+
135
+ b_o = b_o / b_l[:, None]
136
+ p_o_new = tl.make_block_ptr(o_new + (bos * HQ + i_hq) * V, (T, V), (HQ*V, 1), (i_t*BT, 0), (BT, BV), (1, 0))
137
+ tl.store(p_o_new, b_o.to(p_o_new.dtype.element_ty), boundary_check=(0, 1))
138
+ b_l = tl.math.log2(b_l) + b_m
139
+ p_L_new = tl.make_block_ptr(L_new + (bos * HQ + i_hq), (T, ), (HQ, ), (i_t * BT, ), (BT, ), (0,))
140
+ tl.store(p_L_new, b_l.to(p_L_new.dtype.element_ty), boundary_check=(0,))
141
+
142
+
143
+ def parallel_path_fwd_fn(
144
+ q,
145
+ k,
146
+ v,
147
+ o,
148
+ g_cumsum,
149
+ w1,
150
+ w2,
151
+ scale,
152
+ L,
153
+ M,
154
+ cu_seqlens,
155
+ BT,
156
+ BS,
157
+ ):
158
+ B, T, HQ, K = q.shape
159
+ V = v.shape[-1]
160
+ H = k.shape[-2]
161
+ G = HQ // H
162
+ indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
163
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(indices)
164
+ grid = (NT, B * HQ)
165
+ o_new = torch.empty_like(o, dtype=v.dtype)
166
+ L_new = torch.empty_like(L)
167
+
168
+ parallel_path_fwd_kernel[grid](
169
+ q=q,
170
+ k=k,
171
+ v=v,
172
+ o=o,
173
+ o_new=o_new,
174
+ w1=w1,
175
+ w2=w2,
176
+ g_cumsum=g_cumsum,
177
+ scale=scale,
178
+ cu_seqlens=cu_seqlens,
179
+ indices=indices,
180
+ L=L,
181
+ L_new=L_new,
182
+ M=M,
183
+ T=T,
184
+ K=K,
185
+ V=V,
186
+ BK=triton.next_power_of_2(K),
187
+ BV=triton.next_power_of_2(V),
188
+ G=G,
189
+ HQ=HQ,
190
+ H=H,
191
+ BS=BS,
192
+ BT=BT,
193
+ num_warps=8 if (BT == 128 and K == 128) else 4
194
+ )
195
+ return o_new, L_new
@@ -0,0 +1,74 @@
1
+ import torch
2
+ import triton
3
+ import triton.language as tl
4
+
5
+ from fla.ops.utils import prepare_chunk_indices
6
+
7
+
8
+ @triton.heuristics({
9
+ 'IS_VARLEN': lambda args: args['offsets'] is not None
10
+ })
11
+ @triton.jit(do_not_specialize=['T'])
12
+ def parallel_path_fwd_kernel_prepare_k_cache(
13
+ k, k_new, w1, w2,
14
+ offsets, indices,
15
+ T,
16
+ H: tl.constexpr,
17
+ K: tl.constexpr,
18
+ BT: tl.constexpr, BK: tl.constexpr,
19
+ IS_VARLEN: tl.constexpr
20
+ ):
21
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
22
+ i_b, i_h = i_bh // H, i_bh % H
23
+
24
+ if IS_VARLEN:
25
+ i_n, i_t = tl.load(indices + i_t * 2).to(tl.int32), tl.load(indices + i_t * 2 + 1).to(tl.int32)
26
+ bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
27
+ T = eos - bos
28
+ else:
29
+ i_n = i_b
30
+ bos, eos = i_n * T, i_n * T + T
31
+
32
+ k += (bos * H + i_h) * K
33
+ k_new += (bos * H + i_h) * K
34
+ w1 += (bos * H + i_h) * K
35
+ w2 += (bos * H + i_h) * K
36
+ # constants
37
+ p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
38
+ b_k = tl.zeros([BT, BK], dtype=tl.float32)
39
+ b_k += tl.load(p_k, boundary_check=(0, 1))
40
+ for k_block_idx in range(i_t + 1, tl.cdiv(T, BT)):
41
+ p_w1 = tl.make_block_ptr(w1, (T, K), (H*K, 1), (k_block_idx * BT, 0), (BT, BK), (1, 0))
42
+ p_w2 = tl.make_block_ptr(w2, (T, K), (H*K, 1), (k_block_idx * BT, 0), (BT, BK), (1, 0))
43
+ b_w1 = tl.load(p_w1, boundary_check=(0, 1))
44
+ b_w2 = tl.load(p_w2, boundary_check=(0, 1))
45
+ b_A = tl.dot(b_k.to(b_w2.dtype), tl.trans(b_w2))
46
+ b_k = b_k - tl.dot(b_A.to(b_w1.dtype), b_w1)
47
+
48
+ p_k_new = tl.make_block_ptr(k_new, (T, K), (H*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
49
+ tl.store(p_k_new, b_k.to(p_k_new.dtype.element_ty), boundary_check=(0, 1))
50
+
51
+
52
+ def prepare_k_cache_fn(k, w1, w2, cu_seqlens, BS, use_cache=False):
53
+ if not use_cache:
54
+ return None
55
+ else:
56
+ B, T, H, K = k.shape
57
+ k_new = torch.empty_like(k)
58
+ indices = prepare_chunk_indices(cu_seqlens, BS) if cu_seqlens is not None else None
59
+ NT = triton.cdiv(T, BS) if cu_seqlens is None else len(indices)
60
+ grid = (NT, B * H)
61
+ parallel_path_fwd_kernel_prepare_k_cache[grid](
62
+ k=k,
63
+ k_new=k_new,
64
+ w1=w1,
65
+ w2=w2,
66
+ offsets=cu_seqlens,
67
+ indices=indices,
68
+ H=H,
69
+ T=T,
70
+ K=K,
71
+ BT=BS,
72
+ BK=triton.next_power_of_2(K)
73
+ )
74
+ return k_new
@@ -0,0 +1,108 @@
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 get_max_num_splits, prepare_chunk_indices
9
+
10
+
11
+ @triton.heuristics({
12
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
13
+ })
14
+ @triton.jit(do_not_specialize=['T'])
15
+ def transform_q_fwd_kernel(
16
+ q,
17
+ q_new,
18
+ w1,
19
+ w2,
20
+ cu_seqlens,
21
+ indices,
22
+ T,
23
+ S: tl.constexpr,
24
+ G: tl.constexpr,
25
+ HQ: tl.constexpr,
26
+ H: tl.constexpr,
27
+ K: tl.constexpr,
28
+ BT: tl.constexpr,
29
+ BS: tl.constexpr,
30
+ BK: tl.constexpr,
31
+ NUM_BLOCKS: tl.constexpr,
32
+ IS_VARLEN: tl.constexpr,
33
+ ):
34
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
35
+ i_b, i_hq = i_bh // HQ, i_bh % HQ
36
+ i_h = i_hq // G
37
+
38
+ if IS_VARLEN:
39
+ i_n, i_t = tl.load(indices + i_t * 2).to(tl.int32), tl.load(indices + i_t * 2 + 1).to(tl.int32)
40
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
41
+ T = eos - bos
42
+ else:
43
+ i_n = i_b
44
+ bos, eos = i_n * T, i_n * T + T
45
+ # boh = i_n * tl.cdiv(T, BS)
46
+ p_q = tl.make_block_ptr(q + (bos * HQ + i_hq) * K, (T, K), (HQ*K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
47
+ b_q = tl.zeros([BT, BK], dtype=tl.float32)
48
+ b_q += tl.load(p_q, boundary_check=(0, 1))
49
+
50
+ if BS == BT:
51
+ if (i_t * BT) % S == 0:
52
+ p_q_new = tl.make_block_ptr(q_new + ((bos * NUM_BLOCKS + (i_t * BT // S)) * HQ + i_hq) * K,
53
+ (T, K), (HQ*K*NUM_BLOCKS, 1), (i_t * BT, 0), (BT, BK), (1, 0))
54
+ tl.store(p_q_new, b_q.to(q_new.dtype.element_ty), boundary_check=(0, 1))
55
+
56
+ for offset in range((i_t + 1) * BT - 2 * BS, S-BS, -BS):
57
+ p_w1 = tl.make_block_ptr(w1 + (bos * H + i_h) * K, (K, T), (1, K*H), (0, offset), (BK, BS), (0, 1))
58
+ p_w2 = tl.make_block_ptr(w2 + (bos * H + i_h) * K, (T, K), (K*H, 1), (offset, 0), (BS, BK), (1, 0))
59
+ b_w1 = tl.load(p_w1, boundary_check=(0, 1))
60
+ b_w2 = tl.load(p_w2, boundary_check=(0, 1))
61
+ m_s = i_t * BT + tl.arange(0, BT) >= (offset + BS)
62
+ b_s2 = tl.dot(b_q.to(b_w1.dtype), b_w1)
63
+ b_s2 = tl.where(m_s[:, None], b_s2, 0)
64
+ b_q -= tl.dot(b_s2.to(b_w2.dtype), b_w2)
65
+
66
+ if offset % S == 0:
67
+ p_q_new = tl.make_block_ptr(q_new + ((bos * NUM_BLOCKS + (offset // S)) * HQ + i_hq) * K,
68
+ (T, K), (HQ*K*NUM_BLOCKS, 1), (i_t * BT, 0), (BT, BK), (1, 0))
69
+ tl.store(p_q_new, b_q.to(q_new.dtype.element_ty), boundary_check=(0, 1))
70
+
71
+
72
+ def transform_q_fwd_fn(
73
+ q,
74
+ w1,
75
+ w2,
76
+ cu_seqlens,
77
+ BT,
78
+ BS,
79
+ S
80
+ ):
81
+ B, T, HQ, K = q.shape
82
+ H = w1.shape[-2]
83
+ G = HQ // H
84
+ indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
85
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(indices)
86
+
87
+ num_blocks = triton.cdiv(T, S) if cu_seqlens is None else get_max_num_splits(cu_seqlens, S)
88
+ q_new = torch.zeros(B, T, num_blocks, HQ, K, dtype=q.dtype, device=q.device)
89
+ transform_q_fwd_kernel[(NT, B * HQ)](
90
+ q=q,
91
+ q_new=q_new,
92
+ w1=w1,
93
+ w2=w2,
94
+ cu_seqlens=cu_seqlens,
95
+ indices=indices,
96
+ T=T,
97
+ K=K,
98
+ BK=triton.next_power_of_2(K),
99
+ G=G,
100
+ HQ=HQ,
101
+ H=H,
102
+ BS=BS,
103
+ BT=BT,
104
+ S=S,
105
+ NUM_BLOCKS=num_blocks,
106
+ num_warps=8 if (BT == 128 and K == 128) else 4
107
+ )
108
+ return q_new
@@ -0,0 +1,7 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from .parallel import parallel_rebased
4
+
5
+ __all__ = [
6
+ 'parallel_rebased'
7
+ ]
@@ -0,0 +1,27 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ from typing import Optional
5
+
6
+ import torch
7
+
8
+
9
+ def naive_parallel_rebased(
10
+ q: torch.Tensor,
11
+ k: torch.Tensor,
12
+ v: torch.Tensor,
13
+ scale: Optional[float] = None,
14
+ use_norm: bool = True,
15
+ ) -> torch.Tensor:
16
+ if scale is None:
17
+ scale = q.shape[-1] ** -0.5
18
+ q = q * scale
19
+ attn = q @ k.transpose(-2, -1)
20
+ attn = attn ** 2
21
+ attn.masked_fill_(~torch.tril(torch.ones(q.shape[-2], q.shape[-2], dtype=torch.bool, device=q.device)), 0)
22
+ o = attn @ v
23
+ if use_norm:
24
+ z = attn.sum(-1)
25
+ return o / (z[..., None] + 1e-6)
26
+ else:
27
+ return o