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,430 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ from typing import Optional, Tuple
5
+
6
+ import torch
7
+ import triton
8
+ import triton.language as tl
9
+
10
+ from fla.ops.utils import prepare_chunk_indices
11
+ from fla.ops.utils.op import exp
12
+ from fla.utils import check_shared_mem, is_amd, use_cuda_graph
13
+
14
+ NUM_WARPS_AUTOTUNE = [2, 4, 8, 16] if is_amd else [2, 4, 8, 16, 32]
15
+
16
+ BK_LIST = [32, 64, 128] if check_shared_mem() else [16, 32]
17
+
18
+
19
+ @triton.heuristics({
20
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
21
+ })
22
+ @triton.autotune(
23
+ configs=[
24
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
25
+ for num_warps in NUM_WARPS_AUTOTUNE
26
+ for num_stages in [2, 3, 4]
27
+ ],
28
+ key=['BV', 'BT'],
29
+ use_cuda_graph=use_cuda_graph,
30
+ )
31
+ @triton.jit(do_not_specialize=['T'])
32
+ def chunk_dplr_bwd_kernel_dAu(
33
+ v,
34
+ do,
35
+ v_new,
36
+ A_qb,
37
+ dA_qk,
38
+ dA_qb,
39
+ dv_new,
40
+ cu_seqlens,
41
+ chunk_indices,
42
+ scale: tl.constexpr,
43
+ T,
44
+ H: tl.constexpr,
45
+ V: tl.constexpr,
46
+ BT: tl.constexpr,
47
+ BV: tl.constexpr,
48
+ IS_VARLEN: tl.constexpr,
49
+ ):
50
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
51
+ i_b, i_h = i_bh // H, i_bh % H
52
+ if IS_VARLEN:
53
+ 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)
54
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
55
+ else:
56
+ bos, eos = i_b * T, i_b * T + T
57
+ T = eos - bos
58
+
59
+ b_dA_qk = tl.zeros([BT, BT], dtype=tl.float32)
60
+ b_dA_qb = tl.zeros([BT, BT], dtype=tl.float32)
61
+
62
+ p_A_qb = tl.make_block_ptr(A_qb + (bos * H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
63
+
64
+ b_A_qb = tl.load(p_A_qb, boundary_check=(0, 1))
65
+ # causal mask
66
+ b_A_qb = tl.where(tl.arange(0, BT)[:, None] >= tl.arange(0, BT)[None, :], b_A_qb, 0.).to(b_A_qb.dtype)
67
+
68
+ for i_v in range(tl.cdiv(V, BV)):
69
+ p_do = tl.make_block_ptr(do + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
70
+ p_v = tl.make_block_ptr(v + (bos*H + i_h) * V, (V, T), (1, H*V), (i_v * BV, i_t * BT), (BV, BT), (0, 1))
71
+ p_v_new = tl.make_block_ptr(v_new + (bos*H + i_h) * V, (V, T), (1, H*V), (i_v * BV, i_t * BT), (BV, BT), (0, 1))
72
+ p_dv_new = tl.make_block_ptr(dv_new + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
73
+ b_v = tl.load(p_v, boundary_check=(0, 1))
74
+ b_do = tl.load(p_do, boundary_check=(0, 1))
75
+ b_v_new = tl.load(p_v_new, boundary_check=(0, 1))
76
+ b_dA_qk += tl.dot(b_do, b_v)
77
+ b_dA_qb += tl.dot(b_do, b_v_new)
78
+ b_dv_new = tl.dot(tl.trans(b_A_qb), b_do)
79
+ # for recurrent
80
+ tl.store(p_dv_new, b_dv_new.to(p_dv_new.dtype.element_ty), boundary_check=(0, 1))
81
+
82
+ p_dA_qk = tl.make_block_ptr(dA_qk + (bos * H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
83
+ p_dA_qb = tl.make_block_ptr(dA_qb + (bos * H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
84
+ m_s = tl.arange(0, BT)[:, None] >= tl.arange(0, BT)[None, :]
85
+ b_dA_qk = tl.where(m_s, b_dA_qk * scale, 0.)
86
+ tl.store(p_dA_qk, b_dA_qk.to(p_dA_qk.dtype.element_ty), boundary_check=(0, 1))
87
+ b_dA_qb = tl.where(m_s, b_dA_qb * scale, 0.)
88
+ tl.store(p_dA_qb, b_dA_qb.to(p_dA_qb.dtype.element_ty), boundary_check=(0, 1))
89
+
90
+
91
+ @triton.heuristics({
92
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
93
+ })
94
+ @triton.autotune(
95
+ configs=[
96
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
97
+ for num_warps in NUM_WARPS_AUTOTUNE
98
+ for num_stages in [2, 3, 4]
99
+ ],
100
+ key=['BT', 'BK', 'BV'],
101
+ use_cuda_graph=use_cuda_graph,
102
+ )
103
+ @triton.jit
104
+ def chunk_dplr_bwd_o_kernel(
105
+ v,
106
+ v_new,
107
+ h,
108
+ do,
109
+ dh,
110
+ dk,
111
+ db,
112
+ w,
113
+ dq,
114
+ dv,
115
+ dw,
116
+ gk,
117
+ dgk_last,
118
+ k,
119
+ b,
120
+ cu_seqlens,
121
+ chunk_indices,
122
+ T,
123
+ H: tl.constexpr,
124
+ K: tl.constexpr,
125
+ V: tl.constexpr,
126
+ BT: tl.constexpr,
127
+ BK: tl.constexpr,
128
+ BV: tl.constexpr,
129
+ IS_VARLEN: tl.constexpr,
130
+ ):
131
+ i_k, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
132
+ i_b, i_h = i_bh // H, i_bh % H
133
+
134
+ if IS_VARLEN:
135
+ i_tg = i_t
136
+ 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)
137
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
138
+ T = eos - bos
139
+ NT = tl.cdiv(T, BT)
140
+ else:
141
+ NT = tl.cdiv(T, BT)
142
+ i_tg = i_b * NT + i_t
143
+ bos, eos = i_b * T, i_b * T + T
144
+
145
+ # offset calculation
146
+ v += (bos * H + i_h) * V
147
+ v_new += (bos * H + i_h) * V
148
+ do += (bos * H + i_h) * V
149
+ h += (i_tg * H + i_h) * K * V
150
+ dh += (i_tg * H + i_h) * K * V
151
+ dk += (bos * H + i_h) * K
152
+ k += (bos * H + i_h) * K
153
+ db += (bos * H + i_h) * K
154
+ b += (bos * H + i_h) * K
155
+ dw += (bos * H + i_h) * K
156
+ dv += (bos * H + i_h) * V
157
+ dq += (bos * H + i_h) * K
158
+ w += (bos * H + i_h) * K
159
+
160
+ dgk_last += (i_tg * H + i_h) * K
161
+ gk += (bos * H + i_h) * K
162
+
163
+ stride_qk = H*K
164
+ stride_vo = H*V
165
+
166
+ b_dq = tl.zeros([BT, BK], dtype=tl.float32)
167
+ b_dk = tl.zeros([BT, BK], dtype=tl.float32)
168
+ b_dw = tl.zeros([BT, BK], dtype=tl.float32)
169
+ b_db = tl.zeros([BT, BK], dtype=tl.float32)
170
+ b_dgk_last = tl.zeros([BK], dtype=tl.float32)
171
+
172
+ for i_v in range(tl.cdiv(V, BV)):
173
+ p_v = tl.make_block_ptr(v, (T, V), (stride_vo, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
174
+ p_v_new = tl.make_block_ptr(v_new, (T, V), (stride_vo, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
175
+ p_do = tl.make_block_ptr(do, (T, V), (stride_vo, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
176
+ p_h = tl.make_block_ptr(h, (V, K), (1, V), (i_v * BV, i_k * BK), (BV, BK), (0, 1))
177
+ p_dh = tl.make_block_ptr(dh, (V, K), (1, V), (i_v * BV, i_k * BK), (BV, BK), (0, 1))
178
+ # [BT, BV]
179
+ b_v = tl.load(p_v, boundary_check=(0, 1))
180
+ b_v_new = tl.load(p_v_new, boundary_check=(0, 1))
181
+ b_do = tl.load(p_do, boundary_check=(0, 1))
182
+ # [BV, BK]
183
+ b_h = tl.load(p_h, boundary_check=(0, 1))
184
+ b_dh = tl.load(p_dh, boundary_check=(0, 1))
185
+ b_dgk_last += tl.sum((b_h * b_dh).to(tl.float32), axis=0)
186
+
187
+ # [BT, BV] @ [BV, BK] -> [BT, BK]
188
+ b_dq += tl.dot(b_do, b_h.to(b_do.dtype))
189
+ # [BT, BV] @ [BV, BK] -> [BT, BK]
190
+ b_dk += tl.dot(b_v, b_dh.to(b_v.dtype))
191
+ b_db += tl.dot(b_v_new, b_dh.to(b_v_new.dtype))
192
+ p_dv = tl.make_block_ptr(dv, (T, V), (stride_vo, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
193
+ b_dv = tl.load(p_dv, boundary_check=(0, 1))
194
+ b_dw += tl.dot(b_dv.to(b_v.dtype), b_h.to(b_v.dtype))
195
+
196
+ m_k = (i_k*BK+tl.arange(0, BK)) < K
197
+ last_idx = min(i_t * BT + BT, T) - 1
198
+ b_gk_last = tl.load(gk + last_idx * stride_qk + i_k*BK + tl.arange(0, BK), mask=m_k, other=float('-inf'))
199
+ b_dgk_last *= exp(b_gk_last)
200
+ p_k = tl.make_block_ptr(k, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
201
+ p_b = tl.make_block_ptr(b, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
202
+ b_k = tl.load(p_k, boundary_check=(0, 1))
203
+ b_b = tl.load(p_b, boundary_check=(0, 1))
204
+ b_dgk_last += tl.sum(b_k * b_dk, axis=0)
205
+ b_dgk_last += tl.sum(b_b * b_db, axis=0)
206
+ tl.store(dgk_last + tl.arange(0, BK) + i_k * BK, b_dgk_last, mask=m_k)
207
+
208
+ p_dw = tl.make_block_ptr(dw, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
209
+ p_dk = tl.make_block_ptr(dk, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
210
+ p_db = tl.make_block_ptr(db, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
211
+ p_dq = tl.make_block_ptr(dq, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
212
+ tl.store(p_dw, b_dw.to(p_dw.dtype.element_ty), boundary_check=(0, 1))
213
+ tl.store(p_dk, b_dk.to(p_dk.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
+ tl.store(p_dq, b_dq.to(p_dq.dtype.element_ty), boundary_check=(0, 1))
216
+
217
+
218
+ @triton.heuristics({
219
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
220
+ })
221
+ @triton.autotune(
222
+ configs=[
223
+ triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
224
+ for num_warps in NUM_WARPS_AUTOTUNE
225
+ for num_stages in [2, 3, 4]
226
+ for BK in BK_LIST
227
+ for BV in BK_LIST
228
+ ],
229
+ key=['BT'],
230
+ use_cuda_graph=use_cuda_graph,
231
+ )
232
+ @triton.jit
233
+ def chunk_dplr_bwd_kernel_dv(
234
+ A_qk,
235
+ kg,
236
+ do,
237
+ dv,
238
+ dh,
239
+ cu_seqlens,
240
+ chunk_indices,
241
+ T,
242
+ H: tl.constexpr,
243
+ K: tl.constexpr,
244
+ V: tl.constexpr,
245
+ BT: tl.constexpr,
246
+ BK: tl.constexpr,
247
+ BV: tl.constexpr,
248
+ IS_VARLEN: tl.constexpr,
249
+ ):
250
+ i_v, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
251
+ i_b, i_h = i_bh // H, i_bh % H
252
+ if IS_VARLEN:
253
+ i_tg = i_t
254
+ 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)
255
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
256
+ T = eos - bos
257
+ NT = tl.cdiv(T, BT)
258
+ else:
259
+ NT = tl.cdiv(T, BT)
260
+ i_tg = i_b * NT + i_t
261
+ bos, eos = i_b * T, i_b * T + T
262
+
263
+ b_dv = tl.zeros([BT, BV], dtype=tl.float32)
264
+
265
+ # offset calculation
266
+ A_qk += (bos * H + i_h) * BT
267
+ do += (bos * H + i_h) * V
268
+ dv += (bos * H + i_h) * V
269
+ kg += (bos * H + i_h) * K
270
+ dh += (i_tg * H + i_h) * K*V
271
+
272
+ stride_qk = H*K
273
+ stride_vo = H*V
274
+ stride_A = H*BT
275
+
276
+ for i_k in range(tl.cdiv(K, BK)):
277
+ p_dh = tl.make_block_ptr(dh, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
278
+ p_kg = tl.make_block_ptr(kg, (T, K), (stride_qk, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
279
+ b_dh = tl.load(p_dh, boundary_check=(0, 1))
280
+ b_kg = tl.load(p_kg, boundary_check=(0, 1))
281
+ b_dv += tl.dot(b_kg, b_dh.to(b_kg.dtype))
282
+
283
+ p_Aqk = tl.make_block_ptr(A_qk, (BT, T), (1, stride_A), (0, i_t * BT), (BT, BT), (0, 1))
284
+ b_A = tl.where(tl.arange(0, BT)[:, None] <= tl.arange(0, BT)[None, :], tl.load(p_Aqk, boundary_check=(0, 1)), 0)
285
+ p_do = tl.make_block_ptr(do, (T, V), (stride_vo, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
286
+ p_dv = tl.make_block_ptr(dv, (T, V), (stride_vo, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
287
+ b_do = tl.load(p_do, boundary_check=(0, 1))
288
+ b_dv += tl.dot(b_A.to(b_do.dtype), b_do)
289
+ tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
290
+
291
+
292
+ def chunk_dplr_bwd_dv(
293
+ A_qk: torch.Tensor,
294
+ kg: torch.Tensor,
295
+ do: torch.Tensor,
296
+ dh: torch.Tensor,
297
+ cu_seqlens: Optional[torch.LongTensor] = None,
298
+ chunk_size: int = 64
299
+ ) -> torch.Tensor:
300
+ B, T, H, K, V = *kg.shape, do.shape[-1]
301
+ BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
302
+
303
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
304
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
305
+
306
+ dv = torch.empty_like(do)
307
+
308
+ def grid(meta): return (triton.cdiv(V, meta['BV']), NT, B * H)
309
+ chunk_dplr_bwd_kernel_dv[grid](
310
+ A_qk=A_qk,
311
+ kg=kg,
312
+ do=do,
313
+ dv=dv,
314
+ dh=dh,
315
+ cu_seqlens=cu_seqlens,
316
+ chunk_indices=chunk_indices,
317
+ T=T,
318
+ H=H,
319
+ K=K,
320
+ V=V,
321
+ BT=BT,
322
+ )
323
+ return dv
324
+
325
+
326
+ def chunk_dplr_bwd_o(
327
+ k: torch.Tensor,
328
+ b: torch.Tensor,
329
+ v: torch.Tensor,
330
+ v_new: torch.Tensor,
331
+ gk: torch.Tensor,
332
+ do: torch.Tensor,
333
+ h: torch.Tensor,
334
+ dh: torch.Tensor,
335
+ dv: torch.Tensor,
336
+ w: torch.Tensor,
337
+ cu_seqlens: Optional[torch.LongTensor] = None,
338
+ chunk_size: int = 64,
339
+ scale: float = 1.0,
340
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
341
+
342
+ B, T, H, K, V = *w.shape, v.shape[-1]
343
+
344
+ BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
345
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
346
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
347
+
348
+ BK = min(max(triton.next_power_of_2(K), 16), 64) if check_shared_mem() else min(triton.next_power_of_2(K), 32)
349
+ BV = min(max(triton.next_power_of_2(V), 16), 64) if check_shared_mem() else min(triton.next_power_of_2(K), 32)
350
+ NK = triton.cdiv(K, BK)
351
+ dq = torch.empty_like(k)
352
+ dk = torch.empty_like(k)
353
+ dw = torch.empty_like(w)
354
+ db = torch.empty_like(b)
355
+ grid = (NK, NT, B * H)
356
+
357
+ dgk_last = torch.empty(B, NT, H, K, dtype=torch.float, device=w.device)
358
+
359
+ chunk_dplr_bwd_o_kernel[grid](
360
+ k=k,
361
+ b=b,
362
+ v=v,
363
+ v_new=v_new,
364
+ h=h,
365
+ do=do,
366
+ dh=dh,
367
+ dq=dq,
368
+ dk=dk,
369
+ db=db,
370
+ dgk_last=dgk_last,
371
+ w=w,
372
+ dv=dv,
373
+ dw=dw,
374
+ gk=gk,
375
+ cu_seqlens=cu_seqlens,
376
+ chunk_indices=chunk_indices,
377
+ T=T,
378
+ H=H,
379
+ K=K,
380
+ V=V,
381
+ BT=BT,
382
+ BK=BK,
383
+ BV=BV,
384
+ )
385
+ return dq, dk, dw, db, dgk_last
386
+
387
+
388
+ def chunk_dplr_bwd_dAu(
389
+ v: torch.Tensor,
390
+ v_new: torch.Tensor,
391
+ do: torch.Tensor,
392
+ A_qb: torch.Tensor,
393
+ scale: float,
394
+ cu_seqlens: Optional[torch.LongTensor] = None,
395
+ chunk_size: int = 64
396
+ ) -> torch.Tensor:
397
+ B, T, H, V = v.shape
398
+ BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
399
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
400
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
401
+
402
+ if check_shared_mem('ampere'): # A100
403
+ BV = min(triton.next_power_of_2(V), 128)
404
+ elif check_shared_mem('ada'): # 4090
405
+ BV = min(max(triton.next_power_of_2(V), 16), 64)
406
+ else:
407
+ BV = min(triton.next_power_of_2(V), 32)
408
+
409
+ grid = (NT, B * H)
410
+ dA_qk = torch.empty(B, T, H, BT, dtype=torch.float, device=v.device)
411
+ dA_qb = torch.empty(B, T, H, BT, dtype=torch.float, device=v.device)
412
+ dv_new = torch.empty_like(v_new)
413
+ chunk_dplr_bwd_kernel_dAu[grid](
414
+ v=v,
415
+ do=do,
416
+ v_new=v_new,
417
+ A_qb=A_qb,
418
+ dA_qk=dA_qk,
419
+ dA_qb=dA_qb,
420
+ dv_new=dv_new,
421
+ cu_seqlens=cu_seqlens,
422
+ chunk_indices=chunk_indices,
423
+ scale=scale,
424
+ T=T,
425
+ H=H,
426
+ V=V,
427
+ BT=BT,
428
+ BV=BV,
429
+ )
430
+ return dv_new, dA_qk, dA_qb
@@ -0,0 +1,125 @@
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.utils import check_shared_mem, is_amd, use_cuda_graph
12
+
13
+ NUM_WARPS_AUTOTUNE = [2, 4, 8, 16] if is_amd else [2, 4, 8, 16, 32]
14
+
15
+ BK_LIST = [32, 64, 128] if check_shared_mem() else [16, 32]
16
+
17
+
18
+ @triton.heuristics({
19
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
20
+ })
21
+ @triton.autotune(
22
+ configs=[
23
+ triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
24
+ for BK in BK_LIST
25
+ for BV in BK_LIST
26
+ for num_warps in NUM_WARPS_AUTOTUNE
27
+ for num_stages in [2, 3, 4]
28
+ ],
29
+ key=['BT'],
30
+ use_cuda_graph=use_cuda_graph,
31
+ )
32
+ @triton.jit(do_not_specialize=['T'])
33
+ def chunk_dplr_fwd_kernel_o(
34
+ qg,
35
+ v,
36
+ v_new,
37
+ A_qk,
38
+ A_qb,
39
+ h,
40
+ o,
41
+ cu_seqlens,
42
+ chunk_indices,
43
+ T,
44
+ H: tl.constexpr,
45
+ K: tl.constexpr,
46
+ V: tl.constexpr,
47
+ BT: tl.constexpr,
48
+ BK: tl.constexpr,
49
+ BV: tl.constexpr,
50
+ IS_VARLEN: tl.constexpr,
51
+ ):
52
+ i_v, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
53
+ i_b, i_h = i_bh // H, i_bh % H
54
+
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
+ b_o = tl.zeros([BT, BV], dtype=tl.float32)
67
+ for i_k in range(tl.cdiv(K, BK)):
68
+ p_qg = tl.make_block_ptr(qg + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
69
+ p_h = tl.make_block_ptr(h + (i_tg * H + i_h) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
70
+ b_qg = tl.load(p_qg, boundary_check=(0, 1))
71
+ b_h = tl.load(p_h, boundary_check=(0, 1))
72
+ b_o += tl.dot(b_qg, b_h)
73
+
74
+ p_Aqk = tl.make_block_ptr(A_qk + (bos * H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
75
+ p_Aqb = tl.make_block_ptr(A_qb + (bos * H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
76
+ p_v = tl.make_block_ptr(v + (bos * H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
77
+ p_v_new = tl.make_block_ptr(v_new + (bos * H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
78
+ p_o = tl.make_block_ptr(o + (bos * H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
79
+
80
+ m_s = tl.arange(0, BT)[:, None] >= tl.arange(0, BT)[None, :]
81
+ b_Aqk = tl.load(p_Aqk, boundary_check=(0, 1))
82
+ b_Aqb = tl.load(p_Aqb, boundary_check=(0, 1))
83
+ b_Aqk = tl.where(m_s, b_Aqk, 0)
84
+ b_Aqb = tl.where(m_s, b_Aqb, 0)
85
+ b_v = tl.load(p_v, boundary_check=(0, 1))
86
+ b_v_new = tl.load(p_v_new, boundary_check=(0, 1))
87
+ b_o = b_o + tl.dot(b_Aqk.to(b_v.dtype), b_v) + tl.dot(b_Aqb.to(b_v_new.dtype), b_v_new)
88
+ tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1))
89
+
90
+
91
+ def chunk_dplr_fwd_o(
92
+ qg: torch.Tensor,
93
+ v: torch.Tensor,
94
+ v_new: torch.Tensor,
95
+ A_qk: torch.Tensor,
96
+ A_qb: torch.Tensor,
97
+ h: torch.Tensor,
98
+ cu_seqlens: Optional[torch.LongTensor] = None,
99
+ chunk_size: int = 64
100
+ ) -> torch.Tensor:
101
+ B, T, H, K, V = *qg.shape, v.shape[-1]
102
+ BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
103
+
104
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
105
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
106
+
107
+ o = torch.empty_like(v)
108
+ def grid(meta): return (triton.cdiv(V, meta['BV']), NT, B * H)
109
+ chunk_dplr_fwd_kernel_o[grid](
110
+ qg=qg,
111
+ v=v,
112
+ v_new=v_new,
113
+ A_qk=A_qk,
114
+ A_qb=A_qb,
115
+ h=h,
116
+ o=o,
117
+ cu_seqlens=cu_seqlens,
118
+ chunk_indices=chunk_indices,
119
+ T=T,
120
+ H=H,
121
+ K=K,
122
+ V=V,
123
+ BT=BT,
124
+ )
125
+ return o