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
fla/ops/nsa/utils.py ADDED
@@ -0,0 +1,92 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ # Implements argsort based on bitonic sort.
5
+ # [What is bitonic sort?](https://en.wikipedia.org/wiki/Bitonic_sorter)
6
+
7
+ # Code adapted from https://github.com/triton-lang/triton/issues/3698#issuecomment-2067681396
8
+
9
+
10
+ import triton
11
+ import triton.language as tl
12
+
13
+ from fla.ops.utils.op import log2
14
+
15
+
16
+ @triton.jit
17
+ def _compare_and_swap(
18
+ x,
19
+ ids,
20
+ flip,
21
+ i: tl.constexpr,
22
+ n_dims: tl.constexpr,
23
+ ):
24
+ n_outer: tl.constexpr = x.numel >> n_dims
25
+ shape: tl.constexpr = [n_outer * 2**i, 2, 2**(n_dims - i - 1)]
26
+ y = tl.reshape(x, shape)
27
+ # slice left/right with 'stride' 2**(n_dims - i - 1)
28
+ mask = tl.arange(0, 2)[None, :, None]
29
+ left = tl.broadcast_to(tl.sum(y * (1 - mask), 1)[:, None, :], shape).to(y.dtype)
30
+ right = tl.broadcast_to(tl.sum(y * mask, 1)[:, None, :], shape).to(y.dtype)
31
+ left = tl.reshape(left, x.shape)
32
+ right = tl.reshape(right, x.shape)
33
+ # idx
34
+ y_idx = tl.reshape(ids, shape)
35
+ left_idx = tl.broadcast_to(tl.sum(y_idx * (1 - mask), 1)[:, None, :], shape)
36
+ right_idx = tl.broadcast_to(tl.sum(y_idx * mask, 1)[:, None, :], shape)
37
+ left_idx = tl.reshape(left_idx, x.shape).to(y_idx.dtype)
38
+ right_idx = tl.reshape(right_idx, x.shape).to(y_idx.dtype)
39
+ # actual compare-and-swap
40
+ idtype = tl.core.get_int_dtype(bitwidth=x.dtype.primitive_bitwidth, signed=True)
41
+ ileft = left.to(idtype, bitcast=True)
42
+ iright = right.to(idtype, bitcast=True)
43
+ ix = x.to(idtype, bitcast=True)
44
+
45
+ cond = (left > right) != flip
46
+ ret = ix ^ tl.where(cond, ileft ^ iright, tl.zeros_like(ix))
47
+ new_ids = ids ^ tl.where(cond, left_idx ^ right_idx, tl.zeros_like(ids))
48
+ return ret.to(x.dtype, bitcast=True), new_ids
49
+
50
+
51
+ @triton.jit
52
+ def _bitonic_merge(
53
+ x,
54
+ ids,
55
+ stage: tl.constexpr,
56
+ order: tl.constexpr,
57
+ n_dims: tl.constexpr,
58
+ ):
59
+ n_outer: tl.constexpr = x.numel >> n_dims
60
+ tl.static_assert(stage <= n_dims)
61
+ # flip denotes whether to re-arrange sub-sequences of elements in ascending or
62
+ # descending order.
63
+ # if flip = 00000000... then all elements will be re-arranged ascendingly at this stage
64
+ # if flip = 00110011... then all the elements will be re-arranged alternatingly (with
65
+ # a stride of 2) at this stage
66
+ if order == 2:
67
+ shape: tl.constexpr = [n_outer * 2**(n_dims - 1 - stage), 2, 2**stage]
68
+ flip = tl.reshape(tl.broadcast_to(tl.arange(0, 2)[None, :, None], shape), x.shape)
69
+ else:
70
+ flip = order
71
+ # perform `stage` rounds of `compare-and-swap`
72
+ for i in tl.static_range(stage):
73
+ x, ids = _compare_and_swap(x, ids, flip, i + (n_dims - stage), n_dims)
74
+ return x, ids
75
+
76
+
77
+ @triton.jit
78
+ def argsort(
79
+ x,
80
+ ids,
81
+ dim: tl.constexpr = None,
82
+ descending: tl.constexpr = tl.core.CONSTEXPR_0,
83
+ ):
84
+ # handle default dimension or check that it is the most minor dim
85
+ _dim: tl.constexpr = len(x.shape) - 1 if dim is None else dim
86
+ tl.static_assert(_dim == len(x.shape) - 1, "only minor dimension is currently supported")
87
+ # iteratively run bitonic merge-sort steps
88
+ n_dims: tl.constexpr = log2(x.shape[_dim])
89
+
90
+ for i in tl.static_range(1, n_dims + 1):
91
+ x, ids = _bitonic_merge(x, ids, i, 2 if i < n_dims else descending, n_dims)
92
+ return x, ids
@@ -0,0 +1,7 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from .parallel import parallel_path_attn
4
+
5
+ __all__ = [
6
+ 'parallel_path_attn'
7
+ ]
@@ -0,0 +1,140 @@
1
+ import torch
2
+ import triton
3
+ import triton.language as tl
4
+
5
+ from fla.ops.utils import prepare_chunk_indices, prepare_chunk_offsets
6
+ from fla.utils import check_shared_mem
7
+
8
+
9
+ @triton.heuristics({
10
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
11
+ })
12
+ @triton.jit(do_not_specialize=['T'])
13
+ def chunk_cumprod_householder_bwd_kernel(
14
+ hc_suffix, dhc_whole,
15
+ k, dk, w1, w2, dw1, dw2, dk_new,
16
+ cu_seqlens, split_indices, chunk_offsets, split_offsets,
17
+ BT: tl.constexpr, # previous small chunk size
18
+ K: tl.constexpr,
19
+ BK: tl.constexpr,
20
+ T: tl.constexpr,
21
+ S: tl.constexpr,
22
+ G: tl.constexpr,
23
+ H: tl.constexpr,
24
+ HQ: tl.constexpr,
25
+ IS_VARLEN: tl.constexpr,
26
+ ):
27
+ i_ss, i_hq = tl.program_id(0), tl.program_id(1)
28
+ i_h = i_hq // G
29
+
30
+ if IS_VARLEN:
31
+ i_n, i_s = tl.load(split_indices + i_ss * 2).to(tl.int32), tl.load(split_indices + i_ss * 2 + 1).to(tl.int32)
32
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
33
+ T = eos - bos
34
+ NS = tl.cdiv(T, S)
35
+ boh = tl.load(chunk_offsets + i_n).to(tl.int32)
36
+ boh_large = tl.load(split_offsets + i_n).to(tl.int32)
37
+ else:
38
+ NS = tl.cdiv(T, S)
39
+ i_n, i_s = i_ss // NS, i_ss % NS
40
+ bos, eos = i_n * T, i_n * T + T
41
+ boh = i_n * tl.cdiv(T, BT)
42
+ boh_large = i_n * tl.cdiv(T, S)
43
+
44
+ # offset calculations
45
+ dhc_whole += ((boh_large + i_s) * HQ + i_hq) * K * K
46
+ hc_suffix += ((boh + tl.cdiv(i_s * S, BT)) * H + i_h) * K * K
47
+ k += (bos * H + i_h) * K
48
+ w1 += (bos * H + i_h) * K
49
+ w2 += (bos * H + i_h) * K
50
+ dw1 += (bos * HQ + i_hq) * K
51
+ dw2 += (bos * HQ + i_hq) * K
52
+
53
+ # dh += ((boh + tl.cdiv(i_s * S, BT)) * HQ + i_hq) * K * K
54
+ dk += (bos * HQ + i_hq) * K
55
+ dk_new += (bos * HQ + i_hq) * K
56
+
57
+ stride_h = H * K * K
58
+ NT_small = tl.cdiv(min(S, T-i_s*S), BT)
59
+ p_dhc_whole = tl.make_block_ptr(dhc_whole, (K, K), (K, 1), (0, 0), (BK, BK), (1, 0))
60
+ b_dhc = tl.zeros([BK, BK], dtype=tl.float32)
61
+ b_dhc += tl.load(p_dhc_whole, boundary_check=(0, 1))
62
+
63
+ # calculate dh
64
+ for i_t_small in range(0, NT_small):
65
+ p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_s*S + i_t_small*BT, 0), (BT, BK), (1, 0))
66
+ p_dk = tl.make_block_ptr(dk, (T, K), (HQ*K, 1), (i_s*S + i_t_small*BT, 0), (BT, BK), (1, 0))
67
+ p_dk_new = tl.make_block_ptr(dk_new, (T, K), (HQ*K, 1), (i_s*S + i_t_small*BT, 0), (BT, BK), (1, 0))
68
+ p_hc = tl.make_block_ptr(hc_suffix + i_t_small * stride_h, (K, K), (K, 1), (0, 0), (BK, BK), (1, 0))
69
+
70
+ b_k = tl.load(p_k, boundary_check=(0, 1))
71
+ b_dk = tl.load(p_dk, boundary_check=(0, 1))
72
+
73
+ p_w1 = tl.make_block_ptr(w1, (T, K), (H*K, 1), (i_s*S + i_t_small*BT, 0), (BT, BK), (1, 0))
74
+ p_w2 = tl.make_block_ptr(w2, (T, K), (H*K, 1), (i_s*S + i_t_small*BT, 0), (BT, BK), (1, 0))
75
+
76
+ b_w1 = tl.load(p_w1, boundary_check=(0, 1))
77
+ b_w2 = tl.load(p_w2, boundary_check=(0, 1))
78
+ b_hc = tl.load(p_hc, boundary_check=(0, 1))
79
+
80
+ b_dk_new = b_dk - tl.dot(b_dk.to(b_hc.dtype), b_hc)
81
+ p_dk_new = tl.make_block_ptr(dk_new, (T, K), (HQ*K, 1), (i_s*S + i_t_small*BT, 0), (BT, BK), (1, 0))
82
+ tl.store(p_dk_new, b_dk_new.to(dk_new.dtype.element_ty), boundary_check=(0, 1))
83
+
84
+ b_dh = b_dhc - tl.dot(tl.trans(b_hc), b_dhc.to(b_hc.dtype))
85
+ b_dw2 = tl.dot(b_w1, b_dh.to(b_w1.dtype))
86
+ b_dw1 = tl.dot(b_w2, tl.trans(b_dh.to(b_w2.dtype)))
87
+
88
+ p_dw1 = tl.make_block_ptr(dw1, (T, K), (HQ*K, 1), (i_s*S + i_t_small*BT, 0), (BT, BK), (1, 0))
89
+ p_dw2 = tl.make_block_ptr(dw2, (T, K), (HQ*K, 1), (i_s*S + i_t_small*BT, 0), (BT, BK), (1, 0))
90
+
91
+ tl.store(p_dw1, b_dw1.to(dw1.dtype.element_ty), boundary_check=(0, 1))
92
+ tl.store(p_dw2, b_dw2.to(dw2.dtype.element_ty), boundary_check=(0, 1))
93
+
94
+ b_dhc = b_dhc - tl.dot(tl.dot(b_dhc.to(b_w2.dtype), tl.trans(b_w2)).to(b_w1.dtype), b_w1)
95
+ b_dhc -= tl.dot(tl.trans(b_dk).to(b_k.dtype), b_k)
96
+
97
+
98
+ def chunk_cumprod_householder_bwd_fn(
99
+ w1: torch.Tensor,
100
+ w2: torch.Tensor,
101
+ hc_suffix: torch.Tensor,
102
+ dhc_whole: torch.Tensor,
103
+ k: torch.Tensor,
104
+ dk: torch.Tensor,
105
+ S: int, # split size, aka large chunk size
106
+ BT: int, # small chunk size
107
+ cu_seqlens: torch.Tensor = None,
108
+ ):
109
+ B, T, HQ, K = dk.shape
110
+ H = k.shape[2]
111
+ G = HQ // H
112
+
113
+ split_indices = prepare_chunk_indices(cu_seqlens, S) if cu_seqlens is not None else None
114
+ chunk_offsets = prepare_chunk_offsets(cu_seqlens, BT) if cu_seqlens is not None else None
115
+ split_offsets = prepare_chunk_offsets(cu_seqlens, S) if cu_seqlens is not None else None
116
+
117
+ if cu_seqlens is None:
118
+ N = B
119
+ NS = N * triton.cdiv(T, S)
120
+ else:
121
+ N = len(cu_seqlens) - 1
122
+ NS = split_offsets[-1].item()
123
+
124
+ grid = (NS, HQ)
125
+ dw1 = torch.empty_like(dk, dtype=torch.float32)
126
+ dw2 = torch.empty_like(dk, dtype=torch.float32)
127
+ dk_new = torch.empty_like(dk, dtype=torch.float32)
128
+
129
+ chunk_cumprod_householder_bwd_kernel[grid](
130
+ hc_suffix=hc_suffix, dhc_whole=dhc_whole,
131
+ k=k, dk=dk, w1=w1, w2=w2, dw1=dw1, dw2=dw2, dk_new=dk_new,
132
+ cu_seqlens=cu_seqlens,
133
+ split_indices=split_indices, chunk_offsets=chunk_offsets, split_offsets=split_offsets,
134
+ BT=BT, K=K, G=G, H=H, HQ=HQ, BK=K,
135
+ T=T, S=S,
136
+ # SY (2025/07/08): I don't know why when K == 128 if I set num_warps=4 the result would be completely wrong
137
+ num_warps=8 if K == 128 else 4,
138
+ num_stages=2 if check_shared_mem('ampere') else 1
139
+ )
140
+ return dw1, dw2, dk_new
@@ -0,0 +1,119 @@
1
+ import torch
2
+ import triton
3
+ import triton.language as tl
4
+
5
+ from fla.ops.utils import prepare_chunk_indices, prepare_chunk_offsets
6
+ from fla.utils import check_shared_mem
7
+
8
+
9
+ @triton.heuristics({
10
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
11
+ })
12
+ @triton.jit
13
+ def chunk_cumprod_householder_fwd_kernel(
14
+ k,
15
+ k_new,
16
+ w1,
17
+ w2,
18
+ hc_suffix,
19
+ hc_whole,
20
+ cu_seqlens,
21
+ split_indices,
22
+ chunk_offsets,
23
+ split_offsets,
24
+ BT: tl.constexpr, # small chunk size
25
+ K: tl.constexpr,
26
+ H: tl.constexpr,
27
+ BK: tl.constexpr,
28
+ T: tl.constexpr,
29
+ S: tl.constexpr, # split size, aka large chunk size
30
+ IS_VARLEN: tl.constexpr,
31
+ ):
32
+ i_ss, i_h = tl.program_id(0), tl.program_id(1)
33
+
34
+ if IS_VARLEN:
35
+ i_n, i_s = tl.load(split_indices + i_ss * 2).to(tl.int32), tl.load(split_indices + i_ss * 2 + 1).to(tl.int32)
36
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
37
+ T = eos - bos
38
+ NS = tl.cdiv(T, S)
39
+
40
+ boh = tl.load(chunk_offsets + i_n).to(tl.int32)
41
+ boh_large = tl.load(split_offsets + i_n).to(tl.int32)
42
+ else:
43
+ NS = tl.cdiv(T, S)
44
+ i_n, i_s = i_ss // NS, i_ss % NS
45
+ bos, eos = i_n * T, i_n * T + T
46
+
47
+ boh = i_n * tl.cdiv(T, BT)
48
+ boh_large = i_n * tl.cdiv(T, S)
49
+
50
+ NT_small = tl.cdiv(min(S, T-i_s*S), BT)
51
+ stride_h = H*K*K
52
+
53
+ # offset calculations
54
+ hc_whole += ((boh_large + i_s) * H + i_h) * K * K
55
+ hc_suffix += ((boh + tl.cdiv(i_s * S, BT)) * H + i_h) * K * K
56
+
57
+ k += (bos * H + i_h) * K
58
+ k_new += (bos * H + i_h) * K
59
+ w1 += (bos * H + i_h) * K
60
+ w2 += (bos * H + i_h) * K
61
+
62
+ b_h = tl.zeros([BK, BK], dtype=tl.float32)
63
+ for i_t_small in range(NT_small-1, -1, -1):
64
+ p_hc_suffix = tl.make_block_ptr(hc_suffix + i_t_small * stride_h, (K, K), (K, 1), (0, 0), (BK, BK), (1, 0))
65
+ tl.store(p_hc_suffix, b_h.to(hc_suffix.dtype.element_ty), boundary_check=(0, 1))
66
+ p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_s * S + i_t_small * BT, 0), (BT, BK), (1, 0))
67
+ b_k = tl.load(p_k, boundary_check=(0, 1))
68
+ b_k = (b_k - tl.dot(b_k, tl.trans(b_h.to(b_k.dtype))))
69
+ p_w1 = tl.make_block_ptr(w1, (K, T), (1, H*K), (0, i_s * S + i_t_small * BT), (BK, BT), (0, 1))
70
+ p_w2 = tl.make_block_ptr(w2, (T, K), (H*K, 1), (i_s * S + i_t_small * BT, 0), (BT, BK), (1, 0))
71
+ b_w1 = tl.load(p_w1, boundary_check=(0, 1))
72
+ b_w2 = tl.load(p_w2, boundary_check=(0, 1))
73
+ b_v_new = (b_w1 - tl.dot(b_h.to(b_w1.dtype), b_w1)).to(b_w2.dtype)
74
+ b_h += tl.dot(b_v_new, b_w2)
75
+ p_k_new = tl.make_block_ptr(k_new, (T, K), (H*K, 1), (i_s * S + i_t_small * BT, 0), (BT, BK), (1, 0))
76
+ tl.store(p_k_new, b_k.to(k_new.dtype.element_ty), boundary_check=(0, 1))
77
+
78
+ p_hc_whole = tl.make_block_ptr(hc_whole, (K, K), (K, 1), (0, 0), (BK, BK), (1, 0))
79
+ tl.store(p_hc_whole, b_h.to(hc_whole.dtype.element_ty), boundary_check=(0, 1))
80
+
81
+
82
+ def chunk_cumprod_householder_fwd_fn(
83
+ k: torch.Tensor,
84
+ w1: torch.Tensor,
85
+ w2: torch.Tensor,
86
+ S: int, # split size, aka large chunk size
87
+ BT: int, # small chunk size
88
+ cu_seqlens: torch.Tensor = None
89
+ ):
90
+ B, T, H, K = k.shape
91
+
92
+ split_indices = prepare_chunk_indices(cu_seqlens, S) if cu_seqlens is not None else None
93
+ chunk_offsets = prepare_chunk_offsets(cu_seqlens, BT) if cu_seqlens is not None else None
94
+ split_offsets = prepare_chunk_offsets(cu_seqlens, S) if cu_seqlens is not None else None
95
+
96
+ if cu_seqlens is None:
97
+ N = B
98
+ NS = N * triton.cdiv(T, S)
99
+ NT = N * triton.cdiv(T, BT)
100
+ else:
101
+ N = len(cu_seqlens) - 1
102
+ NS = split_offsets[-1]
103
+ NT = chunk_offsets[-1]
104
+
105
+ grid = (NS, H)
106
+ hc_whole = torch.empty((NS, H, K, K), device=k.device, dtype=w1.dtype)
107
+ k_new = torch.empty_like(k, dtype=k.dtype)
108
+ hc_suffix = torch.empty((NT, H, K, K), device=k.device, dtype=w2.dtype)
109
+ chunk_cumprod_householder_fwd_kernel[grid](
110
+ k=k, k_new=k_new, w1=w1, w2=w2, hc_whole=hc_whole, hc_suffix=hc_suffix,
111
+ cu_seqlens=cu_seqlens,
112
+ split_indices=split_indices, chunk_offsets=chunk_offsets, split_offsets=split_offsets,
113
+ BT=BT, K=K, H=H, BK=K,
114
+ T=T, S=S,
115
+ # SY (2025/07/08): I don't know why when K == 128 if I set num_warps=4 the result would be completely wrong
116
+ num_warps=8 if K == 128 else 4,
117
+ num_stages=3 if check_shared_mem('ampere') else 1
118
+ )
119
+ return k_new, hc_suffix, hc_whole
@@ -0,0 +1,140 @@
1
+ import torch
2
+ import triton
3
+ import triton.language as tl
4
+
5
+ from fla.ops.utils import prepare_chunk_indices
6
+ from fla.utils import check_shared_mem
7
+
8
+
9
+ # episold
10
+ @triton.heuristics({
11
+ 'IS_VARLEN': lambda args: args['offsets'] is not None,
12
+ })
13
+ @triton.jit(do_not_specialize=['T'])
14
+ def intra_chunk_preprocess_bwd_kernel(
15
+ q, k, w, beta,
16
+ AT,
17
+ dA_local, dq, dq_new, dk, dk_new, dw, dbeta, dw1, dw2, T,
18
+ offsets, indices,
19
+ HQ: tl.constexpr, G: tl.constexpr, H: tl.constexpr,
20
+ K: tl.constexpr, BT: tl.constexpr, BK: tl.constexpr,
21
+ IS_VARLEN: tl.constexpr
22
+ ):
23
+ i_t, i_nh = tl.program_id(0), tl.program_id(1)
24
+ i_n, i_hq = i_nh // HQ, i_nh % HQ
25
+ i_h = i_hq // G
26
+
27
+ if IS_VARLEN:
28
+ i_n, i_t = tl.load(indices + i_t * 2).to(tl.int32), tl.load(indices + i_t * 2 + 1).to(tl.int32)
29
+ bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
30
+ T = eos - bos
31
+ else:
32
+ bos, eos = i_n * T, i_n * T + T
33
+
34
+ b_dk = tl.zeros([BT, BK], dtype=tl.float32)
35
+ b_dw_beta = tl.zeros([BT, BK], dtype=tl.float32)
36
+ b_dw = tl.zeros([BT, BK], dtype=tl.float32)
37
+ b_dT = tl.zeros([BT, BT], dtype=tl.float32)
38
+
39
+ p_q = tl.make_block_ptr(q + (bos * HQ + i_hq) * K, (T, K), (K*HQ, 1), (i_t * BT, 0), (BT, BK), (1, 0))
40
+ p_k = tl.make_block_ptr(k + (bos * H + i_h) * K, (T, K), (K*H, 1), (i_t * BT, 0), (BT, BK), (1, 0))
41
+ p_w = tl.make_block_ptr(w + (bos * H + i_h) * K, (T, K), (K*H, 1), (i_t * BT, 0), (BT, BK), (1, 0))
42
+ p_beta = tl.make_block_ptr(beta + (bos * H + i_h), (T, ), (H, ), (i_t * BT, ), (BT, ), (0, ))
43
+ p_T = tl.make_block_ptr(AT + (bos * H + i_h) * BT, (T, BT), (BT*H, 1), (i_t * BT, 0), (BT, BT), (1, 0))
44
+ b_w = tl.load(p_w, boundary_check=(0, 1))
45
+ b_beta = tl.load(p_beta, boundary_check=(0, ))
46
+ b_q = tl.load(p_q, boundary_check=(0, 1))
47
+ b_k = tl.load(p_k, boundary_check=(0, 1))
48
+ b_T = tl.load(p_T, boundary_check=(0, 1))
49
+ b_w_beta = (b_w * b_beta[:, None]).to(b_w.dtype)
50
+
51
+ o_i = tl.arange(0, BT)
52
+ b_qw = tl.where(o_i[:, None] >= o_i[None, :], tl.dot(b_q, tl.trans(b_w)), 0).to(b_q.dtype)
53
+ b_wbk = tl.where(o_i[:, None] > o_i[None, :], tl.dot(b_w_beta, tl.trans(b_k)), 0).to(b_k.dtype)
54
+ b_Twb = tl.dot(b_T, b_w_beta).to(b_w.dtype)
55
+ b_Twbk = tl.dot(b_T, b_wbk).to(b_w.dtype)
56
+
57
+ p_dA_local = tl.make_block_ptr(dA_local + (bos * HQ + i_hq) * BT, (T, BT), (BT*HQ, 1), (i_t * BT, 0), (BT, BT), (1, 0))
58
+ b_dA_local = tl.load(p_dA_local, boundary_check=(0, 1))
59
+
60
+ # # Twb part qw part.
61
+ p_dq = tl.make_block_ptr(dq + (bos * HQ + i_hq) * K, (T, K), (K*HQ, 1), (i_t * BT, 0), (BT, BK), (1, 0))
62
+ b_dq = tl.load(p_dq, boundary_check=(0, 1))
63
+
64
+ p_dw1 = tl.make_block_ptr(dw1 + (bos * HQ + i_hq) * K, (T, K), (K*HQ, 1), (i_t * BT, 0), (BT, BK), (1, 0))
65
+ b_dw += tl.load(p_dw1, boundary_check=(0, 1))
66
+
67
+ b_dqw = -tl.dot(b_dA_local, tl.trans(b_Twbk)) - tl.dot(b_dq.to(b_Twb.dtype), tl.trans(b_Twb))
68
+ p_dw2 = tl.make_block_ptr(dw2 + (bos * HQ + i_hq) * K, (T, K), (K*HQ, 1), (i_t * BT, 0), (BT, BK), (1, 0))
69
+ b_dTwb = -tl.dot(tl.trans(b_qw), b_dq) + tl.load(p_dw2, boundary_check=(0, 1))
70
+ b_dT += tl.dot(b_dTwb.to(b_w_beta.dtype), tl.trans(b_w_beta))
71
+ b_dw_beta += tl.dot(tl.trans(b_T), b_dTwb.to(b_T.dtype))
72
+
73
+ b_dqw = tl.where(tl.arange(0, BT)[:, None] >= tl.arange(0, BT)[None, :], b_dqw, 0)
74
+ b_dq += tl.dot(b_dA_local.to(b_k.dtype), b_k)
75
+ b_dq += tl.dot(b_dqw.to(b_w.dtype), b_w)
76
+ b_dw += tl.dot(tl.trans(b_dqw.to(b_q.dtype)), b_q)
77
+ p_q_new = tl.make_block_ptr(dq_new + (bos * HQ + i_hq) * K, (T, K), (K*HQ, 1), (i_t * BT, 0), (BT, BK), (1, 0))
78
+ tl.store(p_q_new, b_dq.to(dq_new.dtype.element_ty), boundary_check=(0, 1))
79
+
80
+ # Twbk part
81
+ p_dk = tl.make_block_ptr(dk + (bos * HQ + i_hq) * K, (T, K), (K*HQ, 1), (i_t * BT, 0), (BT, BK), (1, 0))
82
+ b_dk = tl.load(p_dk, boundary_check=(0, 1))
83
+ b_dTwbk = -tl.dot(tl.trans(b_qw), b_dA_local.to(b_qw.dtype)) - tl.dot(b_w, tl.trans(b_dk.to(b_w.dtype)))
84
+ b_dw -= tl.dot(b_Twbk, b_dk.to(b_w.dtype))
85
+ b_dT += tl.dot(b_dTwbk.to(b_wbk.dtype), tl.trans(b_wbk))
86
+ b_dwbk = tl.where(o_i[:, None] > o_i[None, :], tl.dot(tl.trans(b_T), b_dTwbk.to(b_T.dtype)), 0).to(b_w.dtype)
87
+ b_dw_beta += tl.dot(b_dwbk, b_k)
88
+
89
+ b_dk += tl.dot(tl.trans(b_dwbk), b_w_beta)
90
+ b_dk += tl.dot(tl.trans(b_dA_local), b_q)
91
+ p_dk_new = tl.make_block_ptr(dk_new + (bos * HQ + i_hq) * K, (T, K), (K*HQ, 1), (i_t * BT, 0), (BT, BK), (1, 0))
92
+ tl.store(p_dk_new, b_dk.to(dk_new.dtype.element_ty), boundary_check=(0, 1))
93
+
94
+ # matrix inverse's gradient
95
+ p_T = tl.make_block_ptr(AT + (bos * H + i_h) * BT, (BT, T), (1, BT*H), (0, i_t * BT), (BT, BT), (0, 1))
96
+ b_Tt = tl.load(p_T, boundary_check=(0, 1))
97
+ b_dT = tl.where(tl.arange(0, BT)[:, None] > tl.arange(0, BT)[None, :], b_dT, 0).to(b_w.dtype)
98
+ b_dT = tl.dot(b_Tt, b_dT).to(b_w.dtype)
99
+ b_dT = tl.dot(b_dT, b_Tt)
100
+ b_dT = tl.where(tl.arange(0, BT)[:, None] > tl.arange(0, BT)[None, :], -b_dT, 0).to(b_k.dtype)
101
+
102
+ b_dw_beta += tl.dot(b_dT, b_w)
103
+ b_dw += tl.dot(tl.trans(b_dT), b_w_beta)
104
+ b_dw += b_dw_beta * b_beta[:, None]
105
+ b_dbeta = tl.sum(b_dw_beta * b_w, axis=1)
106
+
107
+ p_dw = tl.make_block_ptr(dw + (bos * HQ + i_hq) * K, (T, K), (K*HQ, 1), (i_t * BT, 0), (BT, BK), (1, 0))
108
+ tl.store(p_dw, b_dw.to(dw.dtype.element_ty), boundary_check=(0, 1))
109
+ p_dbeta = tl.make_block_ptr(dbeta + (bos * HQ + i_hq), (T, ), (HQ, ), (i_t * BT, ), (BT, ), (0, ))
110
+ tl.store(p_dbeta, b_dbeta.to(dbeta.dtype.element_ty), boundary_check=(0, ))
111
+
112
+
113
+ def intra_chunk_preprocess_bwd_fn(q, k, w, beta,
114
+ dq, dk, dA_local,
115
+ dw1, dw2,
116
+ A, L, D, do, scale, cu_seqlens=None):
117
+ BT = A.shape[-1]
118
+ HQ = q.shape[-2]
119
+ B, T, H, K = k.shape
120
+ G = HQ//H
121
+ indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
122
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(indices)
123
+ grid = (NT, B*HQ)
124
+ # better precision because h would be of norm smaller than 1 anyways
125
+
126
+ dbeta = torch.empty(B, T, HQ, device=q.device, dtype=k.dtype if G == 1 else torch.float32)
127
+ dw = torch.empty(B, T, HQ, K, device=q.device, dtype=k.dtype if G == 1 else torch.float32)
128
+ dk_new = torch.empty_like(dk, dtype=k.dtype if G == 1 else torch.float32) # float32 reduction
129
+ dq_new = torch.empty_like(dq, dtype=q.dtype)
130
+
131
+ intra_chunk_preprocess_bwd_kernel[grid](
132
+ q=q, k=k, w=w, beta=beta,
133
+ AT=A,
134
+ dA_local=dA_local, dq=dq, dq_new=dq_new, dk=dk, dk_new=dk_new, dw=dw, dbeta=dbeta, dw1=dw1, dw2=dw2, T=T,
135
+ offsets=cu_seqlens, indices=indices,
136
+ HQ=HQ, G=G, H=H,
137
+ K=K, BT=BT, BK=triton.next_power_of_2(K),
138
+ num_stages=3 if check_shared_mem('hopper') else 1
139
+ )
140
+ return dq_new, dk_new, dbeta, dw