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,367 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ from typing import Optional
5
+
6
+ import torch
7
+ import triton
8
+ import triton.language as tl
9
+
10
+ from fla.ops.utils import prepare_chunk_indices
11
+ from fla.ops.utils.op import exp, gather
12
+ from fla.utils import check_shared_mem, is_amd, is_gather_supported, use_cuda_graph
13
+
14
+ NUM_WARPS_AUTOTUNE = [2, 4, 8, 16] if is_amd else [2, 4, 8, 16, 32]
15
+
16
+
17
+ @triton.heuristics({
18
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
19
+ })
20
+ @triton.autotune(
21
+ configs=[
22
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
23
+ for num_warps in NUM_WARPS_AUTOTUNE
24
+ for num_stages in [2, 3, 4]
25
+ ],
26
+ key=['BK', 'BT', 'K'],
27
+ use_cuda_graph=use_cuda_graph,
28
+ )
29
+ @triton.jit(do_not_specialize=['T'])
30
+ def chunk_dplr_bwd_kernel_intra(
31
+ q,
32
+ k,
33
+ a,
34
+ b,
35
+ gi,
36
+ ge,
37
+ dAqk,
38
+ dAqb,
39
+ dAak,
40
+ dAab,
41
+ dq,
42
+ dk,
43
+ da,
44
+ db,
45
+ dqg,
46
+ dkg,
47
+ dag,
48
+ dbg,
49
+ dgk,
50
+ dgk_offset,
51
+ cu_seqlens,
52
+ chunk_indices,
53
+ scale: tl.constexpr,
54
+ T,
55
+ H: tl.constexpr,
56
+ K: tl.constexpr,
57
+ BT: tl.constexpr,
58
+ BC: tl.constexpr,
59
+ BK: tl.constexpr,
60
+ IS_VARLEN: tl.constexpr,
61
+ GATHER_SUPPORTED: tl.constexpr
62
+ ):
63
+ i_k, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
64
+ i_b, i_h = i_bh // H, i_bh % H
65
+ if IS_VARLEN:
66
+ 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)
67
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
68
+ T = eos - bos
69
+ else:
70
+ bos, eos = (i_b * T).to(tl.int32), (i_b * T + T).to(tl.int32)
71
+
72
+ if i_t * BT >= T:
73
+ return
74
+
75
+ # offset calculation
76
+ ge += (bos*H + i_h) * K
77
+ gi += (bos*H + i_h) * K
78
+ q += (bos*H + i_h) * K
79
+ a += (bos*H + i_h) * K
80
+ b += (bos*H + i_h) * K
81
+ k += (bos*H + i_h) * K
82
+ dq += (bos*H + i_h) * K
83
+ dk += (bos*H + i_h) * K
84
+ da += (bos*H + i_h) * K
85
+ db += (bos*H + i_h) * K
86
+ dqg += (bos*H + i_h) * K
87
+ dag += (bos*H + i_h) * K
88
+ dkg += (bos*H + i_h) * K
89
+ dbg += (bos*H + i_h) * K
90
+ dgk += (bos*H + i_h) * K
91
+ dgk_offset += (bos*H + i_h) * K
92
+ dAqk += (bos*H + i_h) * BT
93
+ dAqb += (bos*H + i_h) * BT
94
+ dAak += (bos*H + i_h) * BT
95
+ dAab += (bos*H + i_h) * BT
96
+
97
+ stride_qk = H*K
98
+ stride_A = H*BT
99
+
100
+ p_ge = tl.make_block_ptr(ge, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
101
+ p_gi = tl.make_block_ptr(gi, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
102
+ # [BC, BK]
103
+ b_ge = tl.load(p_ge, boundary_check=(0, 1))
104
+ b_gi = tl.load(p_gi, boundary_check=(0, 1))
105
+ b_dq = tl.zeros([BC, BK], dtype=tl.float32)
106
+ b_da = tl.zeros([BC, BK], dtype=tl.float32)
107
+ b_dk = tl.zeros([BC, BK], dtype=tl.float32)
108
+ b_db = tl.zeros([BC, BK], dtype=tl.float32)
109
+ # intra chunk gradient calculation
110
+ p_dAqk = tl.make_block_ptr(dAqk, (T, BT), (stride_A, 1), (i_t*BT, 0), (BC, BC), (1, 0))
111
+ p_dAab = tl.make_block_ptr(dAab, (T, BT), (stride_A, 1), (i_t*BT, 0), (BC, BC), (1, 0))
112
+ p_dAqb = tl.make_block_ptr(dAqb, (T, BT), (stride_A, 1), (i_t*BT, 0), (BC, BC), (1, 0))
113
+ p_dAak = tl.make_block_ptr(dAak, (T, BT), (stride_A, 1), (i_t*BT, 0), (BC, BC), (1, 0))
114
+ o_i = tl.arange(0, BC)
115
+ p_k = tl.make_block_ptr(k, (T, K), (stride_qk, 1), (i_t*BT, i_k*BK), (BC, BK), (1, 0))
116
+ p_b = tl.make_block_ptr(b, (T, K), (stride_qk, 1), (i_t*BT, i_k*BK), (BC, BK), (1, 0))
117
+ p_a = tl.make_block_ptr(a, (T, K), (stride_qk, 1), (i_t*BT, i_k*BK), (BC, BK), (1, 0))
118
+ p_q = tl.make_block_ptr(q, (T, K), (stride_qk, 1), (i_t*BT, i_k*BK), (BC, BK), (1, 0))
119
+ b_k = tl.load(p_k, boundary_check=(0, 1))
120
+ b_b = tl.load(p_b, boundary_check=(0, 1))
121
+ b_q = tl.load(p_q, boundary_check=(0, 1))
122
+ b_a = tl.load(p_a, boundary_check=(0, 1))
123
+ b_dAqk = tl.load(p_dAqk, boundary_check=(0, 1))
124
+ b_dAab = tl.load(p_dAab, boundary_check=(0, 1))
125
+ b_dAqb = tl.load(p_dAqb, boundary_check=(0, 1))
126
+ b_dAak = tl.load(p_dAak, boundary_check=(0, 1))
127
+
128
+ # inter chunk gradient calculation
129
+ o_k = i_k * BK + tl.arange(0, BK)
130
+ m_k = o_k < K
131
+ # intra chunk gradient calculation
132
+ for j in range(0, min(BC, T - i_t * BT)):
133
+ # trick to index the block
134
+ if GATHER_SUPPORTED:
135
+ row_idx = tl.full([1, BK], j, dtype=tl.int16)
136
+ col_idx = tl.full([BC, 1], j, dtype=tl.int16)
137
+ row_idx_bc = tl.full([1, BC], j, dtype=tl.int16)
138
+ # [1, BK]
139
+ b_kj = gather(b_k, row_idx, axis=0)
140
+ b_bj = gather(b_b, row_idx, axis=0)
141
+ b_gij = gather(b_gi, row_idx, axis=0)
142
+ b_gej = gather(b_ge, row_idx, axis=0)
143
+ b_qj = gather(b_q, row_idx, axis=0)
144
+ b_aj = gather(b_a, row_idx, axis=0)
145
+ # [BC, 1]
146
+ b_dAqk_j = gather(b_dAqk, col_idx, axis=1)
147
+ b_dAab_j = gather(b_dAab, col_idx, axis=1)
148
+ b_dAqb_j = gather(b_dAqb, col_idx, axis=1)
149
+ b_dAak_j = gather(b_dAak, col_idx, axis=1)
150
+ # [1, BC] -> [BC, 1]
151
+ b_dA_qk_j = tl.sum(gather(b_dAqk, row_idx_bc, axis=0), 0)[:, None]
152
+ b_dA_qk_j = tl.sum(gather(b_dAqk, row_idx_bc, axis=0), 0)[:, None]
153
+ b_dA_ab_j = tl.sum(gather(b_dAab, row_idx_bc, axis=0), 0)[:, None]
154
+ b_dA_qb_j = tl.sum(gather(b_dAqb, row_idx_bc, axis=0), 0)[:, None]
155
+ b_dA_ak_j = tl.sum(gather(b_dAak, row_idx_bc, axis=0), 0)[:, None]
156
+ else:
157
+ mask_idx = tl.arange(0, BC) == j
158
+ b_kj = tl.sum(tl.where(mask_idx[:, None], b_k, 0), 0)[None, :]
159
+ b_bj = tl.sum(tl.where(mask_idx[:, None], b_b, 0), 0)[None, :]
160
+ b_gij = tl.sum(tl.where(mask_idx[:, None], b_gi, 0), 0)[None, :]
161
+ b_gej = tl.sum(tl.where(mask_idx[:, None], b_ge, 0), 0)[None, :]
162
+ b_dAqk_j = tl.sum(tl.where(mask_idx[None, :], b_dAqk, 0), 1)[:, None]
163
+ b_dAab_j = tl.sum(tl.where(mask_idx[None, :], b_dAab, 0), 1)[:, None]
164
+ b_dAqb_j = tl.sum(tl.where(mask_idx[None, :], b_dAqb, 0), 1)[:, None]
165
+ b_dAak_j = tl.sum(tl.where(mask_idx[None, :], b_dAak, 0), 1)[:, None]
166
+ b_dA_qk_j = tl.sum(tl.where(mask_idx[:, None], b_dAqk, 0), 0)[:, None]
167
+ b_dA_ab_j = tl.sum(tl.where(mask_idx[:, None], b_dAab, 0), 0)[:, None]
168
+ b_dA_qb_j = tl.sum(tl.where(mask_idx[:, None], b_dAqb, 0), 0)[:, None]
169
+ b_dA_ak_j = tl.sum(tl.where(mask_idx[:, None], b_dAak, 0), 0)[:, None]
170
+ # [1, BK] b_qj, b_aj
171
+ b_qj = tl.sum(tl.where(mask_idx[:, None], b_q, 0), 0)[None, :]
172
+ b_aj = tl.sum(tl.where(mask_idx[:, None], b_a, 0), 0)[None, :]
173
+
174
+ m_e = o_i[:, None] > j
175
+ m_i = o_i[:, None] >= j
176
+ tmp1 = exp(b_gi - b_gij)
177
+ tmp2 = exp(b_ge - b_gij)
178
+ b_dq += tl.where(m_i, b_dAqk_j * b_kj * tmp1, 0.)
179
+ b_dq += tl.where(m_i, b_dAqb_j * b_bj * tmp1, 0.)
180
+ b_da += tl.where(m_e, b_dAab_j * b_bj * tmp2, 0.)
181
+ b_da += tl.where(m_e, b_dAak_j * b_kj * tmp2, 0.)
182
+
183
+ m_i = o_i[:, None] <= j
184
+ m_e = o_i[:, None] < j
185
+ tmp1 = exp(b_gij - b_gi)
186
+ tmp2 = exp(b_gej - b_gi)
187
+ b_dk += tl.where(m_i, b_dA_qk_j * b_qj * tmp1, 0.)
188
+ b_dk += tl.where(m_e, b_dA_ak_j * b_aj * tmp2, 0.)
189
+ b_db += tl.where(m_i, b_dA_qb_j * b_qj * tmp1, 0.)
190
+ b_db += tl.where(m_e, b_dA_ab_j * b_aj * tmp2, 0.)
191
+
192
+ # post processing
193
+ p_dq = tl.make_block_ptr(dq, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
194
+ p_dk = tl.make_block_ptr(dk, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
195
+ p_da = tl.make_block_ptr(da, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
196
+ p_db = tl.make_block_ptr(db, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
197
+ p_dgk = tl.make_block_ptr(dgk, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
198
+ p_dgk_offset = tl.make_block_ptr(dgk_offset, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
199
+ p_dqg = tl.make_block_ptr(dqg, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
200
+ p_dkg = tl.make_block_ptr(dkg, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
201
+ p_dag = tl.make_block_ptr(dag, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
202
+ p_dbg = tl.make_block_ptr(dbg, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
203
+ p_gn = gi + (min(i_t * BT + BT, T) - 1)*stride_qk + o_k
204
+ p_gn = tl.max_contiguous(tl.multiple_of(p_gn, BK), BK)
205
+ b_gn = tl.load(p_gn, mask=m_k, other=0)
206
+ b_da += tl.load(p_dag, boundary_check=(0, 1)) * exp(b_ge)
207
+ b_dq += tl.load(p_dqg, boundary_check=(0, 1)) * exp(b_gi) * scale
208
+ tmp = exp(b_gn[None, :] - b_gi)
209
+ b_dk += tl.load(p_dkg, boundary_check=(0, 1)).to(tl.float32) * tmp
210
+ b_db += tl.load(p_dbg, boundary_check=(0, 1)).to(tl.float32) * tmp
211
+ tl.store(p_dq, (b_dq).to(p_dq.dtype.element_ty), boundary_check=(0, 1))
212
+ tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
213
+ tl.store(p_da, b_da.to(p_da.dtype.element_ty), boundary_check=(0, 1))
214
+ tl.store(p_db, b_db.to(p_db.dtype.element_ty), boundary_check=(0, 1))
215
+ b_dgk = (b_dq * b_q + b_da * b_a - b_dk * b_k - b_db * b_b).to(tl.float32)
216
+ b_dgk_offset = b_da * b_a
217
+ tl.store(p_dgk, b_dgk.to(p_dgk.dtype.element_ty), boundary_check=(0, 1))
218
+ tl.store(p_dgk_offset, b_dgk_offset.to(p_dgk_offset.dtype.element_ty), boundary_check=(0, 1))
219
+
220
+
221
+ @triton.heuristics({
222
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
223
+ })
224
+ @triton.autotune(
225
+ configs=[
226
+ triton.Config({'BK': BK}, num_warps=num_warps, num_stages=num_stages)
227
+ for num_warps in NUM_WARPS_AUTOTUNE
228
+ for num_stages in [2, 3, 4]
229
+ for BK in [32, 64]
230
+ ],
231
+ key=['BK', 'BT', 'K'],
232
+ use_cuda_graph=use_cuda_graph,
233
+ )
234
+ @triton.jit(do_not_specialize=['T'])
235
+ def chunk_dplr_bwd_dgk_kernel(
236
+ dgk,
237
+ dgk_offset,
238
+ dgk_last,
239
+ dgk_output,
240
+ cu_seqlens,
241
+ chunk_indices,
242
+ T,
243
+ H: tl.constexpr,
244
+ K: tl.constexpr,
245
+ BT: tl.constexpr,
246
+ BK: tl.constexpr,
247
+ IS_VARLEN: tl.constexpr,
248
+ ):
249
+ i_t, i_k, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
250
+ i_b, i_h = i_bh // H, i_bh % H
251
+ if IS_VARLEN:
252
+ i_tg = i_t
253
+ 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)
254
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
255
+ T = eos - bos
256
+ NT = tl.cdiv(T, BT)
257
+ else:
258
+ NT = tl.cdiv(T, BT)
259
+ i_tg = (i_b * NT + i_t).to(tl.int32)
260
+ bos, eos = (i_b * T).to(tl.int32), (i_b * T + T).to(tl.int32)
261
+
262
+ stride_qk = H * K
263
+ dgk += (bos * H + i_h) * K
264
+ dgk_offset += (bos * H + i_h) * K
265
+ dgk_last += (i_tg * H + i_h) * K
266
+ dgk_output += (bos * H + i_h) * K
267
+ p_dgk_last = dgk_last + tl.arange(0, BK) + i_k * BK
268
+ m_k = tl.arange(0, BK) + i_k * BK < K
269
+ b_dgk_last = tl.load(p_dgk_last, mask=m_k, other=0)
270
+ p_dgk_offset = tl.make_block_ptr(dgk_offset, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
271
+ p_dgk = tl.make_block_ptr(dgk, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
272
+ b_dgk = tl.load(p_dgk, boundary_check=(0, 1))
273
+ b_dgk_offset = tl.load(p_dgk_offset, boundary_check=(0, 1))
274
+ # m_inv_cumsum = (tl.arange(0, BT)[:, None] <= tl.arange(0, BT)[None, :]).to(tl.float32)
275
+ # b_dgk_cumsum = tl.dot(m_inv_cumsum, b_dgk, allow_tf32=False)
276
+ b_dgk_cumsum = tl.cumsum(b_dgk, 0, reverse=True)
277
+ b_dgk_cumsum += b_dgk_last[None, :]
278
+ b_dgk_cumsum -= b_dgk_offset
279
+ p_dgk_output = tl.make_block_ptr(dgk_output, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
280
+ tl.store(p_dgk_output, b_dgk_cumsum.to(p_dgk_output.dtype.element_ty), boundary_check=(0, 1))
281
+
282
+
283
+ def chunk_dplr_bwd_dqk_intra(
284
+ q: torch.Tensor,
285
+ k: torch.Tensor,
286
+ a: torch.Tensor,
287
+ b: torch.Tensor,
288
+ gi: torch.Tensor,
289
+ ge: torch.Tensor,
290
+ dAqk: torch.Tensor,
291
+ dAqb: torch.Tensor,
292
+ dAak: torch.Tensor,
293
+ dAab: torch.Tensor,
294
+ dqg: torch.Tensor,
295
+ dkg: torch.Tensor,
296
+ dag: torch.Tensor,
297
+ dbg: torch.Tensor,
298
+ dgk_last: torch.Tensor,
299
+ scale: float = 1.0,
300
+ cu_seqlens: Optional[torch.LongTensor] = None,
301
+ chunk_size: int = 64,
302
+ ):
303
+ B, T, H, K = q.shape
304
+ BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
305
+ BK = min(64, triton.next_power_of_2(K)) if check_shared_mem() else min(32, triton.next_power_of_2(K))
306
+
307
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
308
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
309
+ NK = triton.cdiv(K, BK)
310
+
311
+ dq = torch.empty_like(q)
312
+ dk = torch.empty_like(k)
313
+ da = torch.empty_like(a)
314
+ db = torch.empty_like(b)
315
+ dgk = torch.empty_like(gi, dtype=torch.float)
316
+ dgk_offset = torch.empty_like(gi, dtype=torch.float)
317
+
318
+ grid = (NK, NT, B * H)
319
+ chunk_dplr_bwd_kernel_intra[grid](
320
+ q=q,
321
+ k=k,
322
+ a=a,
323
+ b=b,
324
+ gi=gi,
325
+ ge=ge,
326
+ dAqk=dAqk,
327
+ dAqb=dAqb,
328
+ dAak=dAak,
329
+ dAab=dAab,
330
+ dq=dq,
331
+ dk=dk,
332
+ dgk=dgk,
333
+ dgk_offset=dgk_offset,
334
+ dqg=dqg,
335
+ dkg=dkg,
336
+ dag=dag,
337
+ dbg=dbg,
338
+ da=da,
339
+ db=db,
340
+ cu_seqlens=cu_seqlens,
341
+ chunk_indices=chunk_indices,
342
+ scale=scale,
343
+ T=T,
344
+ H=H,
345
+ K=K,
346
+ BT=BT,
347
+ BC=BT,
348
+ BK=BK,
349
+ GATHER_SUPPORTED=is_gather_supported
350
+ )
351
+
352
+ dgk_output = torch.empty_like(dgk)
353
+
354
+ def grid(meta): return (NT, triton.cdiv(K, meta['BK']), B * H)
355
+ chunk_dplr_bwd_dgk_kernel[grid](
356
+ dgk=dgk,
357
+ dgk_offset=dgk_offset,
358
+ dgk_last=dgk_last,
359
+ dgk_output=dgk_output,
360
+ cu_seqlens=cu_seqlens,
361
+ chunk_indices=chunk_indices,
362
+ T=T,
363
+ H=H,
364
+ K=K,
365
+ BT=BT,
366
+ )
367
+ return dq, dk, da, db, dgk_output
@@ -0,0 +1,198 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ from typing import Optional
5
+
6
+ import torch
7
+ import triton
8
+ import triton.language as tl
9
+
10
+ from fla.ops.utils import prepare_chunk_indices
11
+ from fla.ops.utils.op import exp, gather
12
+ from fla.utils import is_amd, is_gather_supported, use_cuda_graph
13
+
14
+ NUM_WARPS_AUTOTUNE = [2, 4, 8, 16] if is_amd else [2, 4, 8, 16, 32]
15
+
16
+
17
+ @triton.heuristics({
18
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
19
+ })
20
+ @triton.autotune(
21
+ configs=[
22
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
23
+ for num_warps in NUM_WARPS_AUTOTUNE
24
+ for num_stages in [2, 3, 4]
25
+ ],
26
+ key=['BK', 'BT'],
27
+ use_cuda_graph=use_cuda_graph,
28
+ )
29
+ @triton.jit(do_not_specialize=['T'])
30
+ def chunk_dplr_fwd_A_kernel_intra_sub_intra(
31
+ q,
32
+ k,
33
+ a,
34
+ b,
35
+ gi,
36
+ ge,
37
+ qg,
38
+ kg,
39
+ ag,
40
+ bg,
41
+ Aqk,
42
+ Aqb,
43
+ Aab,
44
+ Aak,
45
+ cu_seqlens,
46
+ chunk_indices,
47
+ scale: tl.constexpr,
48
+ T,
49
+ H: tl.constexpr,
50
+ K: tl.constexpr,
51
+ BT: tl.constexpr,
52
+ BC: tl.constexpr,
53
+ BK: tl.constexpr,
54
+ IS_VARLEN: tl.constexpr,
55
+ GATHER_SUPPORTED: tl.constexpr
56
+ ):
57
+ i_t, i_b, i_h = tl.program_id(0), tl.program_id(1), tl.program_id(2)
58
+
59
+ if IS_VARLEN:
60
+ 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)
61
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
62
+ T = eos - bos
63
+ else:
64
+ bos, eos = i_b * T, i_b * T + T
65
+
66
+ if i_t * BT >= T:
67
+ return
68
+
69
+ o_i = tl.arange(0, BC)
70
+ o_k = tl.arange(0, BK)
71
+ m_k = o_k < K
72
+ m_A = (i_t * BT + tl.arange(0, BC)) < T
73
+ last_idx = min((i_t+1) * BT, T) - 1
74
+ o_A = (bos + i_t * BT + tl.arange(0, BC)) * H*BT + i_h * BT
75
+ p_q = tl.make_block_ptr(q + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
76
+ p_k = tl.make_block_ptr(k + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
77
+ p_a = tl.make_block_ptr(a + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
78
+ p_b = tl.make_block_ptr(b + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
79
+ p_gi = tl.make_block_ptr(gi + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
80
+ p_ge = tl.make_block_ptr(ge + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
81
+ p_g_last = gi + (bos * H + i_h) * K + last_idx * H * K + tl.arange(0, BK)
82
+ b_g_last = tl.load(p_g_last, mask=m_k, other=0)
83
+ p_qg = tl.make_block_ptr(qg + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
84
+ p_kg = tl.make_block_ptr(kg + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
85
+ p_ag = tl.make_block_ptr(ag + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
86
+ p_bg = tl.make_block_ptr(bg + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, 0), (BC, BK), (1, 0))
87
+
88
+ b_q = tl.load(p_q, boundary_check=(0, 1))
89
+ b_q = b_q * scale
90
+ b_k = tl.load(p_k, boundary_check=(0, 1))
91
+ b_a = tl.load(p_a, boundary_check=(0, 1))
92
+ b_b = tl.load(p_b, boundary_check=(0, 1))
93
+ b_gi = tl.load(p_gi, boundary_check=(0, 1)).to(tl.float32)
94
+ b_ge = tl.load(p_ge, boundary_check=(0, 1)).to(tl.float32)
95
+
96
+ # deal with decay term.
97
+ g_exp = exp(b_gi)
98
+ g_exp_inv = exp(-b_gi + b_g_last[None, :])
99
+ b_qg = b_q * g_exp
100
+ b_kg = b_k * g_exp_inv
101
+ b_bg = b_b * g_exp_inv
102
+ b_ag = b_a * exp(b_ge)
103
+ tl.store(p_qg, b_qg.to(p_qg.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
104
+ tl.store(p_bg, b_bg.to(p_bg.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
105
+ tl.store(p_ag, b_ag.to(p_ag.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
106
+ tl.store(p_kg, b_kg.to(p_kg.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
107
+ # tl.debug_barrier()
108
+
109
+ b_q = b_q.to(b_k.dtype)
110
+ # inner attn
111
+ for j in range(0, min(BC, T - i_t * BT)):
112
+ # a trick to index the j-th row of b_k, b_g, b_b
113
+ if GATHER_SUPPORTED:
114
+ row_idx = tl.full([1, BK], j, dtype=tl.int16)
115
+ # [1, BK]
116
+ b_k_j = gather(b_k, row_idx, axis=0)
117
+ b_gk_j = gather(b_gi, row_idx, axis=0)
118
+ b_b_j = gather(b_b, row_idx, axis=0)
119
+ else:
120
+ mask = tl.arange(0, BC) == j
121
+ b_k_j = tl.sum(tl.where(mask[:, None], b_k, 0), 0)[None, :]
122
+ b_gk_j = tl.sum(tl.where(mask[:, None], b_gi, 0), 0)[None, :]
123
+ b_b_j = tl.sum(tl.where(mask[:, None], b_b, 0), 0)[None, :]
124
+ tmp = exp(b_gi - b_gk_j)
125
+ b_A_qk = tl.sum(b_q * b_k_j * tmp, 1)
126
+ m_i = (o_i >= j).to(tl.float32)
127
+ b_A_qk = b_A_qk * m_i
128
+ b_A_qb = tl.sum(b_q * b_b_j * tmp, 1)
129
+ b_A_qb = b_A_qb * m_i
130
+ tmp2 = exp(b_ge - b_gk_j)
131
+ b_A_ak = tl.sum(b_a * b_k_j * tmp2, 1)
132
+ m_i2 = (o_i > j).to(tl.float32)
133
+ b_A_ak = b_A_ak * m_i2
134
+ b_A_ab = tl.sum(b_a * b_b_j * tmp2, 1)
135
+ b_A_ab = b_A_ab * m_i2
136
+
137
+ tl.store(Aqk + o_A + j, b_A_qk.to(dtype=Aqk.dtype.element_ty, fp_downcast_rounding="rtne"), mask=m_A)
138
+ tl.store(Aqb + o_A + j, b_A_qb.to(dtype=Aqb.dtype.element_ty, fp_downcast_rounding="rtne"), mask=m_A)
139
+ tl.store(Aab + o_A + j, b_A_ab.to(dtype=Aqb.dtype.element_ty, fp_downcast_rounding="rtne"), mask=m_A)
140
+ tl.store(Aak + o_A + j, b_A_ak.to(dtype=Aqk.dtype.element_ty, fp_downcast_rounding="rtne"), mask=m_A)
141
+
142
+
143
+ def chunk_dplr_fwd_intra(
144
+ q: torch.Tensor,
145
+ k: torch.Tensor,
146
+ a: torch.Tensor,
147
+ b: torch.Tensor,
148
+ gi: torch.Tensor,
149
+ ge: torch.Tensor,
150
+ scale: float,
151
+ chunk_size: int,
152
+ cu_seqlens: Optional[torch.LongTensor] = None,
153
+ ):
154
+ B, T, H, K = k.shape
155
+ BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
156
+
157
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
158
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
159
+
160
+ Aqk = q.new_empty(B, T, H, BT, dtype=q.dtype)
161
+ Aqb = q.new_empty(B, T, H, BT, dtype=q.dtype)
162
+ # involving matrix inverse and it'd be better to use float here.
163
+ Aab = q.new_empty(B, T, H, BT, dtype=torch.float)
164
+ Aak = q.new_empty(B, T, H, BT, dtype=torch.float)
165
+
166
+ grid = (NT, B, H)
167
+ BK = max(triton.next_power_of_2(K), 16)
168
+ qg = torch.empty_like(q)
169
+ kg = torch.empty_like(k, dtype=q.dtype)
170
+ ag = torch.empty_like(a, dtype=q.dtype)
171
+ bg = torch.empty_like(b, dtype=q.dtype)
172
+ chunk_dplr_fwd_A_kernel_intra_sub_intra[grid](
173
+ q=q,
174
+ k=k,
175
+ a=a,
176
+ b=b,
177
+ gi=gi,
178
+ ge=ge,
179
+ Aqk=Aqk,
180
+ Aqb=Aqb,
181
+ Aab=Aab,
182
+ Aak=Aak,
183
+ qg=qg,
184
+ kg=kg,
185
+ ag=ag,
186
+ bg=bg,
187
+ cu_seqlens=cu_seqlens,
188
+ chunk_indices=chunk_indices,
189
+ scale=scale,
190
+ T=T,
191
+ H=H,
192
+ K=K,
193
+ BT=BT,
194
+ BC=BT,
195
+ BK=BK,
196
+ GATHER_SUPPORTED=is_gather_supported
197
+ )
198
+ return Aab, Aqk, Aak, Aqb, qg, kg, ag, bg