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,502 @@
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, prepare_chunk_offsets
11
+ from fla.ops.utils.op import exp
12
+ from fla.utils import is_nvidia_hopper, use_cuda_graph
13
+
14
+ NUM_WARPS = [2, 4] if is_nvidia_hopper else [2, 4, 8, 16]
15
+
16
+
17
+ @triton.heuristics({
18
+ 'USE_G': lambda args: args['g'] is not None,
19
+ 'USE_INITIAL_STATE': lambda args: args['h0'] is not None,
20
+ 'STORE_FINAL_STATE': lambda args: args['ht'] is not None,
21
+ 'SAVE_NEW_VALUE': lambda args: args['v_new'] is not None,
22
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
23
+ })
24
+ @triton.autotune(
25
+ configs=[
26
+ triton.Config({'BV': BV}, num_warps=num_warps, num_stages=num_stages)
27
+ for num_warps in [2, 4]
28
+ for num_stages in [2, 3, 4]
29
+ for BV in [32, 64]
30
+ ],
31
+ key=['H', 'K', 'V', 'BT', 'USE_G'],
32
+ use_cuda_graph=use_cuda_graph,
33
+ )
34
+ @triton.jit(do_not_specialize=['T'])
35
+ def chunk_gated_delta_product_fwd_kernel_h_blockdim64(
36
+ k,
37
+ v,
38
+ w,
39
+ v_new,
40
+ g,
41
+ h,
42
+ h0,
43
+ ht,
44
+ cu_seqlens,
45
+ chunk_offsets,
46
+ T,
47
+ num_householder: tl.constexpr, # number of delta products
48
+ H: tl.constexpr,
49
+ K: tl.constexpr,
50
+ V: tl.constexpr,
51
+ BT: tl.constexpr,
52
+ BV: tl.constexpr,
53
+ USE_G: tl.constexpr,
54
+ USE_INITIAL_STATE: tl.constexpr,
55
+ STORE_FINAL_STATE: tl.constexpr,
56
+ SAVE_NEW_VALUE: tl.constexpr,
57
+ IS_VARLEN: tl.constexpr,
58
+ ):
59
+ i_v, i_nh = tl.program_id(0), tl.program_id(1)
60
+ i_n, i_h = i_nh // H, i_nh % H
61
+ if IS_VARLEN:
62
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
63
+ T = eos - bos
64
+ NT = tl.cdiv(T, BT)
65
+ boh = tl.load(chunk_offsets + i_n).to(tl.int32)
66
+ else:
67
+ bos, eos = i_n * T, i_n * T + T
68
+ NT = tl.cdiv(T, BT)
69
+ boh = i_n * tl.cdiv(T // num_householder, BT)
70
+
71
+ # [BK, BV]
72
+ b_h1 = tl.zeros([64, BV], dtype=tl.float32)
73
+ if K > 64:
74
+ b_h2 = tl.zeros([64, BV], dtype=tl.float32)
75
+ if K > 128:
76
+ b_h3 = tl.zeros([64, BV], dtype=tl.float32)
77
+ if K > 192:
78
+ b_h4 = tl.zeros([64, BV], dtype=tl.float32)
79
+
80
+ # calculate offset
81
+ h += (boh * H + i_h) * K*V
82
+ v += (bos * H + i_h) * V
83
+ k += (bos * H + i_h) * K
84
+ w += (bos * H + i_h) * K
85
+ if SAVE_NEW_VALUE:
86
+ v_new += (bos * H + i_h) * V
87
+ stride_v = H*V
88
+ stride_h = H*K*V
89
+ stride_k = H*K
90
+ if USE_INITIAL_STATE:
91
+ h0 = h0 + i_nh * K*V
92
+ if STORE_FINAL_STATE:
93
+ ht = ht + i_nh * K*V
94
+
95
+ # load initial state
96
+ if USE_INITIAL_STATE:
97
+ p_h0_1 = tl.make_block_ptr(h0, (K, V), (V, 1), (0, i_v * BV), (64, BV), (1, 0))
98
+ b_h1 += tl.load(p_h0_1, boundary_check=(0, 1)).to(tl.float32)
99
+ if K > 64:
100
+ p_h0_2 = tl.make_block_ptr(h0, (K, V), (V, 1), (64, i_v * BV), (64, BV), (1, 0))
101
+ b_h2 += tl.load(p_h0_2, boundary_check=(0, 1)).to(tl.float32)
102
+ if K > 128:
103
+ p_h0_3 = tl.make_block_ptr(h0, (K, V), (V, 1), (128, i_v * BV), (64, BV), (1, 0))
104
+ b_h3 += tl.load(p_h0_3, boundary_check=(0, 1)).to(tl.float32)
105
+ if K > 192:
106
+ p_h0_4 = tl.make_block_ptr(h0, (K, V), (V, 1), (192, i_v * BV), (64, BV), (1, 0))
107
+ b_h4 += tl.load(p_h0_4, boundary_check=(0, 1)).to(tl.float32)
108
+
109
+ # main recurrence
110
+ for i_t in range(NT):
111
+ if i_t % num_householder == 0:
112
+ i_t_true = i_t // num_householder
113
+ p_h1 = tl.make_block_ptr(h + i_t_true * stride_h, (K, V), (V, 1), (0, i_v * BV), (64, BV), (1, 0))
114
+ tl.store(p_h1, b_h1.to(p_h1.dtype.element_ty), boundary_check=(0, 1))
115
+ if K > 64:
116
+ p_h2 = tl.make_block_ptr(h + i_t_true * stride_h, (K, V), (V, 1), (64, i_v * BV), (64, BV), (1, 0))
117
+ tl.store(p_h2, b_h2.to(p_h2.dtype.element_ty), boundary_check=(0, 1))
118
+ if K > 128:
119
+ p_h3 = tl.make_block_ptr(h + i_t_true * stride_h, (K, V), (V, 1), (128, i_v * BV), (64, BV), (1, 0))
120
+ tl.store(p_h3, b_h3.to(p_h3.dtype.element_ty), boundary_check=(0, 1))
121
+ if K > 192:
122
+ p_h4 = tl.make_block_ptr(h + i_t_true * stride_h, (K, V), (V, 1), (192, i_v * BV), (64, BV), (1, 0))
123
+ tl.store(p_h4, b_h4.to(p_h4.dtype.element_ty), boundary_check=(0, 1))
124
+
125
+ p_v = tl.make_block_ptr(v, (T, V), (stride_v, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
126
+ p_v_new = tl.make_block_ptr(v_new, (T, V), (stride_v, 1), (i_t * BT, i_v * BV),
127
+ (BT, BV), (1, 0)) if SAVE_NEW_VALUE else None
128
+ b_v_new = tl.zeros([BT, BV], dtype=tl.float32)
129
+ p_w = tl.make_block_ptr(w, (T, K), (stride_k, 1), (i_t * BT, 0), (BT, 64), (1, 0))
130
+ b_w = tl.load(p_w, boundary_check=(0, 1))
131
+ b_v_new += tl.dot(b_w, b_h1.to(b_w.dtype))
132
+ if K > 64:
133
+ p_w = tl.make_block_ptr(w, (T, K), (stride_k, 1), (i_t * BT, 64), (BT, 64), (1, 0))
134
+ b_w = tl.load(p_w, boundary_check=(0, 1))
135
+ b_v_new += tl.dot(b_w, b_h2.to(b_w.dtype))
136
+ if K > 128:
137
+ p_w = tl.make_block_ptr(w, (T, K), (stride_k, 1), (i_t * BT, 128), (BT, 64), (1, 0))
138
+ b_w = tl.load(p_w, boundary_check=(0, 1))
139
+ b_v_new += tl.dot(b_w, b_h3.to(b_w.dtype))
140
+ if K > 192:
141
+ p_w = tl.make_block_ptr(w, (T, K), (stride_k, 1), (i_t * BT, 192), (BT, 64), (1, 0))
142
+ b_w = tl.load(p_w, boundary_check=(0, 1))
143
+ b_v_new += tl.dot(b_w, b_h4.to(b_w.dtype))
144
+ b_v_new = -b_v_new + tl.load(p_v, boundary_check=(0, 1))
145
+
146
+ if SAVE_NEW_VALUE:
147
+ p_v_new = tl.make_block_ptr(v_new, (T, V), (stride_v, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
148
+ tl.store(p_v_new, b_v_new.to(p_v_new.dtype.element_ty), boundary_check=(0, 1))
149
+
150
+ if USE_G:
151
+ m_t = (i_t * BT + tl.arange(0, BT)) < T
152
+ last_idx = min((i_t + 1) * BT, T) - 1
153
+ b_g_last = tl.load(g + bos * H + last_idx * H + i_h)
154
+ p_g = tl.make_block_ptr(g + bos * H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
155
+ b_g = tl.load(p_g, boundary_check=(0,))
156
+ b_v_new = b_v_new * tl.where(m_t, exp(b_g_last - b_g), 0)[:, None]
157
+ b_g_last = exp(b_g_last)
158
+ b_h1 = b_h1 * b_g_last
159
+ if K > 64:
160
+ b_h2 = b_h2 * b_g_last
161
+ if K > 128:
162
+ b_h3 = b_h3 * b_g_last
163
+ if K > 192:
164
+ b_h4 = b_h4 * b_g_last
165
+ b_v_new = b_v_new.to(k.dtype.element_ty)
166
+ p_k = tl.make_block_ptr(k, (K, T), (1, stride_k), (0, i_t * BT), (64, BT), (0, 1))
167
+ b_k = tl.load(p_k, boundary_check=(0, 1))
168
+ b_h1 += tl.dot(b_k, b_v_new)
169
+ if K > 64:
170
+ p_k = tl.make_block_ptr(k, (K, T), (1, stride_k), (64, i_t * BT), (64, BT), (0, 1))
171
+ b_k = tl.load(p_k, boundary_check=(0, 1))
172
+ b_h2 += tl.dot(b_k, b_v_new)
173
+ if K > 128:
174
+ p_k = tl.make_block_ptr(k, (K, T), (1, stride_k), (128, i_t * BT), (64, BT), (0, 1))
175
+ b_k = tl.load(p_k, boundary_check=(0, 1))
176
+ b_h3 += tl.dot(b_k, b_v_new)
177
+ if K > 192:
178
+ p_k = tl.make_block_ptr(k, (K, T), (1, stride_k), (192, i_t * BT), (64, BT), (0, 1))
179
+ b_k = tl.load(p_k, boundary_check=(0, 1))
180
+ b_h4 += tl.dot(b_k, b_v_new)
181
+ # epilogue
182
+ if STORE_FINAL_STATE:
183
+ p_ht = tl.make_block_ptr(ht, (K, V), (V, 1), (0, i_v * BV), (64, BV), (1, 0))
184
+ tl.store(p_ht, b_h1.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
185
+ if K > 64:
186
+ p_ht = tl.make_block_ptr(ht, (K, V), (V, 1), (64, i_v * BV), (64, BV), (1, 0))
187
+ tl.store(p_ht, b_h2.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
188
+ if K > 128:
189
+ p_ht = tl.make_block_ptr(ht, (K, V), (V, 1), (128, i_v * BV), (64, BV), (1, 0))
190
+ tl.store(p_ht, b_h3.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
191
+ if K > 192:
192
+ p_ht = tl.make_block_ptr(ht, (K, V), (V, 1), (192, i_v * BV), (64, BV), (1, 0))
193
+ tl.store(p_ht, b_h4.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
194
+
195
+
196
+ @triton.heuristics({
197
+ 'USE_G': lambda args: args['g'] is not None,
198
+ 'USE_INITIAL_STATE': lambda args: args['dh0'] is not None,
199
+ 'USE_FINAL_STATE_GRADIENT': lambda args: args['dht'] is not None,
200
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None,
201
+ })
202
+ @triton.autotune(
203
+ configs=[
204
+ triton.Config({'BV': BV}, num_warps=num_warps, num_stages=num_stages)
205
+ for num_warps in [2, 4]
206
+ for num_stages in [4, 3, 2]
207
+ for BV in [64, 32]
208
+ ],
209
+ key=['H', 'K', 'V', 'BT', 'BV', 'USE_G'],
210
+ use_cuda_graph=use_cuda_graph,
211
+ )
212
+ @triton.jit(do_not_specialize=['T'])
213
+ def chunk_gated_delta_product_bwd_kernel_dhu_blockdim64(
214
+ q,
215
+ k,
216
+ w,
217
+ g,
218
+ dht,
219
+ dh0,
220
+ do,
221
+ dh,
222
+ dv,
223
+ dv2,
224
+ cu_seqlens,
225
+ chunk_offsets,
226
+ scale,
227
+ T,
228
+ H: tl.constexpr,
229
+ K: tl.constexpr,
230
+ V: tl.constexpr,
231
+ BT: tl.constexpr,
232
+ BV: tl.constexpr,
233
+ USE_G: tl.constexpr,
234
+ USE_INITIAL_STATE: tl.constexpr,
235
+ USE_FINAL_STATE_GRADIENT: tl.constexpr,
236
+ IS_VARLEN: tl.constexpr
237
+ ):
238
+ i_v, i_nh = tl.program_id(0), tl.program_id(1)
239
+ i_n, i_h = i_nh // H, i_nh % H
240
+ if IS_VARLEN:
241
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
242
+ T = eos - bos
243
+ NT = tl.cdiv(T, BT)
244
+ boh = tl.load(chunk_offsets + i_n).to(tl.int32)
245
+ else:
246
+ bos, eos = i_n * T, i_n * T + T
247
+ NT = tl.cdiv(T, BT)
248
+ boh = i_n * NT
249
+
250
+ # [BK, BV]
251
+ b_dh1 = tl.zeros([64, BV], dtype=tl.float32)
252
+ if K > 64:
253
+ b_dh2 = tl.zeros([64, BV], dtype=tl.float32)
254
+ if K > 128:
255
+ b_dh3 = tl.zeros([64, BV], dtype=tl.float32)
256
+ if K > 192:
257
+ b_dh4 = tl.zeros([64, BV], dtype=tl.float32)
258
+
259
+ # calculate offset
260
+ dh += (boh * H + i_h) * K*V
261
+ dv += (bos * H + i_h) * V
262
+ dv2 += (bos * H + i_h) * V
263
+ q += (bos * H + i_h) * K
264
+ k += (bos * H + i_h) * K
265
+ w += (bos * H + i_h) * K
266
+ do += (bos * H + i_h) * V
267
+ stride_v = H*V
268
+ stride_h = H*K*V
269
+ stride_k = H*K
270
+ if USE_INITIAL_STATE:
271
+ dh0 += i_nh * K*V
272
+ if USE_FINAL_STATE_GRADIENT:
273
+ dht += i_nh * K*V
274
+
275
+ if USE_FINAL_STATE_GRADIENT:
276
+ p_dht1 = tl.make_block_ptr(dht, (K, V), (V, 1), (0, i_v * BV), (64, BV), (1, 0))
277
+ b_dh1 += tl.load(p_dht1, boundary_check=(0, 1))
278
+ if K > 64:
279
+ p_dht2 = tl.make_block_ptr(dht, (K, V), (V, 1), (64, i_v * BV), (64, BV), (1, 0))
280
+ b_dh2 += tl.load(p_dht2, boundary_check=(0, 1))
281
+ if K > 128:
282
+ p_dht3 = tl.make_block_ptr(dht, (K, V), (V, 1), (128, i_v * BV), (64, BV), (1, 0))
283
+ b_dh3 += tl.load(p_dht3, boundary_check=(0, 1))
284
+ if K > 192:
285
+ p_dht4 = tl.make_block_ptr(dht, (K, V), (V, 1), (192, i_v * BV), (64, BV), (1, 0))
286
+ b_dh4 += tl.load(p_dht4, boundary_check=(0, 1))
287
+
288
+ for i_t in range(NT - 1, -1, -1):
289
+ p_dh1 = tl.make_block_ptr(dh + i_t*stride_h, (K, V), (V, 1), (0, i_v * BV), (64, BV), (1, 0))
290
+ tl.store(p_dh1, b_dh1.to(p_dh1.dtype.element_ty), boundary_check=(0, 1))
291
+ if K > 64:
292
+ p_dh2 = tl.make_block_ptr(dh + i_t*stride_h, (K, V), (V, 1), (64, i_v * BV), (64, BV), (1, 0))
293
+ tl.store(p_dh2, b_dh2.to(p_dh2.dtype.element_ty), boundary_check=(0, 1))
294
+ if K > 128:
295
+ p_dh3 = tl.make_block_ptr(dh + i_t*stride_h, (K, V), (V, 1), (128, i_v * BV), (64, BV), (1, 0))
296
+ tl.store(p_dh3, b_dh3.to(p_dh3.dtype.element_ty), boundary_check=(0, 1))
297
+ if K > 192:
298
+ p_dh4 = tl.make_block_ptr(dh + i_t*stride_h, (K, V), (V, 1), (192, i_v * BV), (64, BV), (1, 0))
299
+ tl.store(p_dh4, b_dh4.to(p_dh4.dtype.element_ty), boundary_check=(0, 1))
300
+
301
+ if USE_G:
302
+ last_idx = min((i_t + 1) * BT, T) - 1
303
+ bg_last = tl.load(g + (bos + last_idx) * H + i_h)
304
+ bg_last_exp = exp(bg_last)
305
+ p_g = tl.make_block_ptr(g + bos * H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
306
+ b_g = tl.load(p_g, boundary_check=(0,))
307
+ b_g_exp = exp(b_g)
308
+ else:
309
+ bg_last = None
310
+ last_idx = None
311
+ b_g = None
312
+ b_g_exp = None
313
+
314
+ p_dv = tl.make_block_ptr(dv, (T, V), (stride_v, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
315
+ p_wo = tl.make_block_ptr(do, (T, V), (stride_v, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
316
+ p_dv2 = tl.make_block_ptr(dv2, (T, V), (stride_v, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
317
+
318
+ b_wo = tl.load(p_wo, boundary_check=(0, 1))
319
+ b_dv = tl.zeros([BT, BV], dtype=tl.float32)
320
+
321
+ # Update dv
322
+ p_k = tl.make_block_ptr(k, (T, K), (stride_k, 1), (i_t * BT, 0), (BT, 64), (1, 0))
323
+ b_k = tl.load(p_k, boundary_check=(0, 1))
324
+ b_dv += tl.dot(b_k, b_dh1.to(b_k.dtype))
325
+
326
+ if K > 64:
327
+ p_k = tl.make_block_ptr(k, (T, K), (stride_k, 1), (i_t * BT, 64), (BT, 64), (1, 0))
328
+ b_k = tl.load(p_k, boundary_check=(0, 1))
329
+ b_dv += tl.dot(b_k, b_dh2.to(b_k.dtype))
330
+
331
+ if K > 128:
332
+ p_k = tl.make_block_ptr(k, (T, K), (stride_k, 1), (i_t * BT, 128), (BT, 64), (1, 0))
333
+ b_k = tl.load(p_k, boundary_check=(0, 1))
334
+ b_dv += tl.dot(b_k, b_dh3.to(b_k.dtype))
335
+
336
+ if K > 192:
337
+ p_k = tl.make_block_ptr(k, (T, K), (stride_k, 1), (i_t * BT, 192), (BT, 64), (1, 0))
338
+ b_k = tl.load(p_k, boundary_check=(0, 1))
339
+ b_dv += tl.dot(b_k, b_dh4.to(b_k.dtype))
340
+
341
+ if USE_G:
342
+ m_t = (i_t * BT + tl.arange(0, BT)) < T
343
+ b_dv *= tl.where(m_t, exp(bg_last - b_g), 0)[:, None]
344
+ b_dv += tl.load(p_dv, boundary_check=(0, 1))
345
+
346
+ tl.store(p_dv2, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
347
+ # Update dh
348
+ p_w = tl.make_block_ptr(w, (K, T), (1, stride_k), (0, i_t * BT), (64, BT), (0, 1))
349
+ p_q = tl.make_block_ptr(q, (K, T), (1, stride_k), (0, i_t * BT), (64, BT), (0, 1))
350
+ b_w = tl.load(p_w, boundary_check=(0, 1))
351
+ b_q = tl.load(p_q, boundary_check=(0, 1))
352
+ if USE_G:
353
+ b_dh1 *= bg_last_exp
354
+ b_q = b_q * b_g_exp[None, :]
355
+ b_q = (b_q * scale).to(b_q.dtype)
356
+ b_dh1 += tl.dot(b_q, b_wo.to(b_q.dtype))-tl.dot(b_w, b_dv.to(b_w.dtype))
357
+ if K > 64:
358
+ p_q = tl.make_block_ptr(q, (K, T), (1, stride_k), (64, i_t * BT), (64, BT), (0, 1))
359
+ p_w = tl.make_block_ptr(w, (K, T), (1, stride_k), (64, i_t * BT), (64, BT), (0, 1))
360
+ b_q = tl.load(p_q, boundary_check=(0, 1))
361
+ b_w = tl.load(p_w, boundary_check=(0, 1))
362
+ if USE_G:
363
+ b_dh2 *= bg_last_exp
364
+ b_q = b_q * b_g_exp[None, :]
365
+ b_q = (b_q * scale).to(b_q.dtype)
366
+ b_dh2 += tl.dot(b_q, b_wo.to(b_q.dtype))-tl.dot(b_w, b_dv.to(b_w.dtype))
367
+ if K > 128:
368
+ p_q = tl.make_block_ptr(q, (K, T), (1, stride_k), (128, i_t * BT), (64, BT), (0, 1))
369
+ p_w = tl.make_block_ptr(w, (K, T), (1, stride_k), (128, i_t * BT), (64, BT), (0, 1))
370
+ b_q = tl.load(p_q, boundary_check=(0, 1))
371
+ b_w = tl.load(p_w, boundary_check=(0, 1))
372
+ if USE_G:
373
+ b_dh3 *= bg_last_exp
374
+ b_q = b_q * b_g_exp[None, :]
375
+ b_q = (b_q * scale).to(b_q.dtype)
376
+ b_dh3 += tl.dot(b_q, b_wo.to(b_q.dtype))-tl.dot(b_w, b_dv.to(b_w.dtype))
377
+ if K > 192:
378
+ p_q = tl.make_block_ptr(q, (K, T), (1, stride_k), (192, i_t * BT), (64, BT), (0, 1))
379
+ p_w = tl.make_block_ptr(w, (K, T), (1, stride_k), (192, i_t * BT), (64, BT), (0, 1))
380
+ b_q = tl.load(p_q, boundary_check=(0, 1))
381
+ b_w = tl.load(p_w, boundary_check=(0, 1))
382
+ if USE_G:
383
+ b_dh4 *= bg_last_exp
384
+ b_q = b_q * b_g_exp[None, :]
385
+ b_q = (b_q * scale).to(b_q.dtype)
386
+ b_dh4 += tl.dot(b_q, b_wo.to(b_q.dtype))-tl.dot(b_w, b_dv.to(b_w.dtype))
387
+
388
+ if USE_INITIAL_STATE:
389
+ p_dh0 = tl.make_block_ptr(dh0, (K, V), (V, 1), (0, i_v * BV), (64, BV), (1, 0))
390
+ tl.store(p_dh0, b_dh1.to(p_dh0.dtype.element_ty), boundary_check=(0, 1))
391
+ if K > 64:
392
+ p_dh1 = tl.make_block_ptr(dh0, (K, V), (V, 1), (64, i_v * BV), (64, BV), (1, 0))
393
+ tl.store(p_dh1, b_dh2.to(p_dh1.dtype.element_ty), boundary_check=(0, 1))
394
+ if K > 128:
395
+ p_dh2 = tl.make_block_ptr(dh0, (K, V), (V, 1), (128, i_v * BV), (64, BV), (1, 0))
396
+ tl.store(p_dh2, b_dh3.to(p_dh2.dtype.element_ty), boundary_check=(0, 1))
397
+ if K > 192:
398
+ p_dh3 = tl.make_block_ptr(dh0, (K, V), (V, 1), (192, i_v * BV), (64, BV), (1, 0))
399
+ tl.store(p_dh3, b_dh4.to(p_dh3.dtype.element_ty), boundary_check=(0, 1))
400
+
401
+
402
+ def chunk_gated_delta_product_fwd_h(
403
+ k: torch.Tensor,
404
+ w: torch.Tensor,
405
+ u: torch.Tensor,
406
+ g: Optional[torch.Tensor] = None,
407
+ initial_state: Optional[torch.Tensor] = None,
408
+ output_final_state: bool = False,
409
+ chunk_size: int = 64, # SY: remove this argument and force chunk size 64?
410
+ save_new_value: bool = True,
411
+ cu_seqlens: Optional[torch.LongTensor] = None,
412
+ num_householder: int = 1,
413
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
414
+ B, T, H, K, V = *k.shape, u.shape[-1]
415
+ assert T % num_householder == 0, "T must be divisible by num_householder"
416
+ T_true = T // num_householder
417
+ BT = chunk_size
418
+ chunk_indices = prepare_chunk_indices(cu_seqlens // num_householder, chunk_size) if cu_seqlens is not None else None
419
+ # N: the actual number of sequences in the batch with either equal or variable lengths
420
+ if cu_seqlens is None:
421
+ N, NT, chunk_offsets = B, triton.cdiv(T_true, BT), None
422
+ else:
423
+ N, NT, chunk_offsets = len(cu_seqlens) - \
424
+ 1, len(chunk_indices), prepare_chunk_offsets(cu_seqlens // num_householder, BT)
425
+ assert K <= 256, "current kernel does not support head dimension larger than 256."
426
+ h = k.new_empty(B, NT, H, K, V)
427
+ final_state = k.new_empty(N, H, K, V, dtype=torch.float32) if output_final_state else None
428
+ v_new = torch.empty_like(u) if save_new_value else None
429
+
430
+ def grid(meta): return (triton.cdiv(V, meta['BV']), N*H)
431
+ chunk_gated_delta_product_fwd_kernel_h_blockdim64[grid](
432
+ k=k,
433
+ v=u,
434
+ w=w,
435
+ v_new=v_new,
436
+ g=g,
437
+ h=h,
438
+ h0=initial_state,
439
+ ht=final_state,
440
+ cu_seqlens=cu_seqlens,
441
+ chunk_offsets=chunk_offsets,
442
+ num_householder=num_householder,
443
+ T=T,
444
+ H=H,
445
+ K=K,
446
+ V=V,
447
+ BT=BT
448
+ )
449
+ return h, v_new, final_state
450
+
451
+
452
+ def chunk_gated_delta_product_bwd_dhu(
453
+ q: torch.Tensor,
454
+ k: torch.Tensor,
455
+ w: torch.Tensor,
456
+ g: torch.Tensor,
457
+ h0: torch.Tensor,
458
+ dht: Optional[torch.Tensor],
459
+ do: torch.Tensor,
460
+ dv: torch.Tensor,
461
+ scale: float,
462
+ cu_seqlens: Optional[torch.LongTensor] = None,
463
+ chunk_size: int = 64, # SY: remove this argument and force chunk size 64?
464
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
465
+ B, T, H, K, V = *q.shape, do.shape[-1]
466
+
467
+ # N: the actual number of sequences in the batch with either equal or variable lengths
468
+ BT = 64
469
+ assert K <= 256, "current kernel does not support head dimension being larger than 256."
470
+
471
+ chunk_indices = prepare_chunk_indices(cu_seqlens, chunk_size) if cu_seqlens is not None else None
472
+ if cu_seqlens is None:
473
+ N, NT, chunk_offsets = B, triton.cdiv(T, BT), None
474
+ else:
475
+ N, NT, chunk_offsets = len(cu_seqlens) - 1, len(chunk_indices), prepare_chunk_offsets(cu_seqlens, BT)
476
+
477
+ dh = q.new_empty(B, NT, H, K, V)
478
+ dh0 = torch.empty_like(h0, dtype=torch.float32) if h0 is not None else None
479
+ dv2 = torch.empty_like(dv)
480
+
481
+ def grid(meta): return (triton.cdiv(V, meta['BV']), N*H)
482
+ chunk_gated_delta_product_bwd_kernel_dhu_blockdim64[grid](
483
+ q=q,
484
+ k=k,
485
+ w=w,
486
+ g=g,
487
+ dht=dht,
488
+ dh0=dh0,
489
+ do=do,
490
+ dh=dh,
491
+ dv=dv,
492
+ dv2=dv2,
493
+ cu_seqlens=cu_seqlens,
494
+ chunk_offsets=chunk_offsets,
495
+ scale=scale,
496
+ T=T,
497
+ H=H,
498
+ K=K,
499
+ V=V,
500
+ BT=BT,
501
+ )
502
+ return dh, dh0, dv2
@@ -0,0 +1,154 @@
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
12
+ from fla.utils import check_shared_mem, is_nvidia_hopper
13
+
14
+ BKV_LIST = [64, 128] if check_shared_mem() else [32, 64]
15
+ NUM_WARPS = [2, 4] if is_nvidia_hopper else [2, 4, 8]
16
+
17
+
18
+ @triton.heuristics({
19
+ 'USE_G': lambda args: args['g'] is not None,
20
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
21
+ })
22
+ @triton.autotune(
23
+ configs=[
24
+ triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
25
+ for BK in BKV_LIST
26
+ for BV in BKV_LIST
27
+ for num_warps in NUM_WARPS
28
+ for num_stages in [2, 3, 4]
29
+ ],
30
+ key=['H', 'K', 'V', 'BT'],
31
+ )
32
+ @triton.jit(do_not_specialize=['T'])
33
+ def chunk_fwd_kernel_o(
34
+ q,
35
+ k,
36
+ v,
37
+ h,
38
+ g,
39
+ o,
40
+ cu_seqlens,
41
+ chunk_indices,
42
+ scale,
43
+ T,
44
+ num_householder: tl.constexpr,
45
+ H: tl.constexpr,
46
+ K: tl.constexpr,
47
+ V: tl.constexpr,
48
+ BT: tl.constexpr,
49
+ BK: tl.constexpr,
50
+ BV: tl.constexpr,
51
+ USE_G: tl.constexpr,
52
+ IS_VARLEN: tl.constexpr,
53
+ ):
54
+ i_v, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
55
+ i_b, i_h = i_bh // H, i_bh % H
56
+
57
+ if IS_VARLEN:
58
+ i_tg = i_t
59
+ 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)
60
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
61
+ T = eos - bos
62
+ NT = tl.cdiv(T, BT)
63
+ else:
64
+ NT = tl.cdiv(T, BT)
65
+ i_tg = i_b * NT + i_t
66
+ bos, eos = i_b * T, i_b * T + T
67
+
68
+ # offset calculation
69
+ q += (bos * H + i_h) * K
70
+ k += (bos * num_householder * H + i_h) * K
71
+ v += (bos * num_householder * H + i_h) * V
72
+ o += (bos * H + i_h) * V
73
+ h += (i_tg * H + i_h).to(tl.int64) * K*V
74
+
75
+ b_o = tl.zeros([BT, BV], dtype=tl.float32)
76
+
77
+ for i_k in range(tl.cdiv(K, BK)):
78
+ p_q = tl.make_block_ptr(q, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
79
+ p_h = tl.make_block_ptr(h, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
80
+ # [BT, BK]
81
+ b_q = tl.load(p_q, boundary_check=(0, 1))
82
+ # [BK, BV]
83
+ b_h = tl.load(p_h, boundary_check=(0, 1))
84
+ # [BT, BK] @ [BK, BV] -> [BT, BV]
85
+ b_o += tl.dot(b_q, b_h)
86
+
87
+ o_t = i_t * BT + tl.arange(0, BT)
88
+ m_t = o_t < T
89
+ if USE_G:
90
+ g += bos * H + i_h
91
+ p_g = tl.make_block_ptr(g, (T,), (H,), (i_t * BT,), (BT,), (0,))
92
+ b_g = tl.load(p_g, boundary_check=(0,))
93
+ m_A = (o_t[:, None] >= o_t[None, :]) & (m_t[:, None] & m_t)
94
+ b_m = tl.where(m_A, exp(b_g[:, None] - b_g[None, :]), 0)
95
+ b_o = b_o * exp(b_g)[:, None]
96
+ else:
97
+ b_m = ((o_t[:, None] >= o_t[None, :]) & (m_t[:, None] & m_t)).to(tl.float32)
98
+
99
+ for i_dp in range(num_householder):
100
+ b_A = tl.zeros([BT, BT], dtype=tl.float32)
101
+ for i_k in range(tl.cdiv(K, BK)):
102
+ p_q = tl.make_block_ptr(q, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
103
+ p_k = tl.make_block_ptr(k+i_dp*H*K, (K, T), (1, num_householder*H*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
104
+ # [BT, BK]
105
+ b_q = tl.load(p_q, boundary_check=(0, 1))
106
+ # [BK, BT]
107
+ b_k = tl.load(p_k, boundary_check=(0, 1))
108
+ # [BT, BK] @ [BK, BT] -> [BT, BT]
109
+ b_A += tl.dot(b_q, b_k)
110
+ b_A = b_A * b_m
111
+ p_v = tl.make_block_ptr(v+i_dp*H*V, (T, V), (H*V*num_householder, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
112
+ b_v = tl.load(p_v, boundary_check=(0, 1))
113
+ b_o += tl.dot(b_A.to(b_v.dtype), b_v)
114
+ b_o = b_o * scale
115
+ p_o = tl.make_block_ptr(o, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
116
+ tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1))
117
+
118
+
119
+ def chunk_gated_delta_product_fwd_o(
120
+ q: torch.Tensor,
121
+ k: torch.Tensor,
122
+ v: torch.Tensor,
123
+ h: torch.Tensor,
124
+ g: Optional[torch.Tensor] = None, # cumsum of log decay
125
+ scale: Optional[float] = None,
126
+ cu_seqlens: Optional[torch.LongTensor] = None,
127
+ chunk_size: int = 64,
128
+ num_householder: int = 1,
129
+ ) -> torch.Tensor:
130
+ assert q.shape[1] * num_householder == k.shape[1], "q.shape[1] * num_householder must be equal to k.shape[1]"
131
+ B, T, H, K, V = *q.shape, v.shape[-1]
132
+ BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
133
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
134
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
135
+ o = v.new_empty(B, T, H, V).fill_(-float('inf'))
136
+ def grid(meta): return (triton.cdiv(V, meta['BV']), NT, B * H)
137
+ chunk_fwd_kernel_o[grid](
138
+ q,
139
+ k,
140
+ v,
141
+ h,
142
+ g,
143
+ o,
144
+ cu_seqlens,
145
+ chunk_indices,
146
+ scale,
147
+ T=T,
148
+ num_householder=num_householder,
149
+ H=H,
150
+ K=K,
151
+ V=V,
152
+ BT=BT,
153
+ )
154
+ return o