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,300 @@
1
+
2
+ # -*- coding: utf-8 -*-
3
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
4
+
5
+ from typing import Optional, Tuple
6
+
7
+ import torch
8
+ import triton
9
+ import triton.language as tl
10
+
11
+ from fla.ops.utils import prepare_chunk_indices
12
+ from fla.utils import check_shared_mem, is_nvidia_hopper
13
+
14
+ NUM_WARPS = [2, 4] if is_nvidia_hopper else [2, 4, 8]
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)
23
+ for num_warps in [1, 2, 4, 8, 16]
24
+ ],
25
+ key=['BK']
26
+ )
27
+ @triton.jit(do_not_specialize=['T'])
28
+ def prepare_wy_repr_fwd_kernel_chunk32(
29
+ a,
30
+ b,
31
+ A,
32
+ cu_seqlens,
33
+ chunk_indices,
34
+ T,
35
+ H: tl.constexpr,
36
+ K: tl.constexpr,
37
+ BT: tl.constexpr,
38
+ BK: tl.constexpr,
39
+ BC: tl.constexpr, # dummy placeholder
40
+ IS_VARLEN: tl.constexpr,
41
+ ):
42
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
43
+ i_b, i_h = i_bh // H, i_bh % H
44
+ if IS_VARLEN:
45
+ 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)
46
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
47
+ T = eos - bos
48
+ else:
49
+ bos, eos = i_b * T, i_b * T + T
50
+
51
+ b_A = tl.zeros([BT, BT], dtype=tl.float32)
52
+ for i_k in range(tl.cdiv(K, BK)):
53
+ p_a = tl.make_block_ptr(a + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
54
+ p_b = tl.make_block_ptr(b + (bos * H + i_h) * K, (K, T), (1, K*H), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
55
+ b_a = tl.load(p_a, boundary_check=(0, 1))
56
+ b_b = tl.load(p_b, boundary_check=(0, 1))
57
+ b_A += tl.dot(b_a, b_b)
58
+
59
+ b_A = tl.where(tl.arange(0, BT)[:, None] > tl.arange(0, BT)[None, :], b_A, 0)
60
+ for i in range(1, BT):
61
+ mask = tl.arange(0, BT) == i
62
+ b_a = tl.sum(tl.where(mask[:, None], b_A, 0), 0)
63
+ b_a = b_a + tl.sum(b_a[:, None] * b_A, 0) * (tl.arange(0, BT) < i)
64
+ b_A = tl.where(mask[:, None], b_a, b_A)
65
+ b_A += tl.arange(0, BT)[:, None] == tl.arange(0, BT)[None, :]
66
+
67
+ p_A = tl.make_block_ptr(A + (bos*H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
68
+ tl.store(p_A, b_A.to(p_A.dtype.element_ty), boundary_check=(0, 1))
69
+
70
+
71
+ @triton.heuristics({
72
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
73
+ })
74
+ @triton.autotune(
75
+ configs=[
76
+ triton.Config({}, num_warps=num_warps)
77
+ for num_warps in [1, 2, 4, 8, 16]
78
+ ],
79
+ key=['BK']
80
+ )
81
+ @triton.jit(do_not_specialize=['T'])
82
+ def prepare_wy_repr_fwd_kernel_chunk64(
83
+ a,
84
+ b,
85
+ A,
86
+ cu_seqlens,
87
+ chunk_indices,
88
+ T,
89
+ H: tl.constexpr,
90
+ K: tl.constexpr,
91
+ BT: tl.constexpr,
92
+ BK: tl.constexpr,
93
+ BC: tl.constexpr,
94
+ IS_VARLEN: tl.constexpr,
95
+ ):
96
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
97
+ i_b, i_h = i_bh // H, i_bh % H
98
+ if IS_VARLEN:
99
+ 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)
100
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
101
+ T = eos - bos
102
+ else:
103
+ bos, eos = i_b * T, i_b * T + T
104
+
105
+ b_A = tl.zeros([BC, BC], dtype=tl.float32)
106
+ b_A2 = tl.zeros([BC, BC], dtype=tl.float32)
107
+ b_A3 = tl.zeros([BC, BC], dtype=tl.float32)
108
+
109
+ for i_k in range(tl.cdiv(K, BK)):
110
+ p_a1 = tl.make_block_ptr(a + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BC, BK), (1, 0))
111
+ p_a2 = tl.make_block_ptr(a + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT + BC, i_k * BK), (BC, BK), (1, 0))
112
+ p_b1 = tl.make_block_ptr(b + (bos * H + i_h) * K, (K, T), (1, K*H), (i_k * BK, i_t * BT), (BK, BC), (0, 1))
113
+ p_b2 = tl.make_block_ptr(b + (bos * H + i_h) * K, (K, T), (1, K*H), (i_k * BK, i_t * BT + BC), (BK, BC), (0, 1))
114
+ b_a1 = tl.load(p_a1, boundary_check=(0, 1))
115
+ b_a2 = tl.load(p_a2, boundary_check=(0, 1))
116
+ b_b1 = tl.load(p_b1, boundary_check=(0, 1))
117
+ b_b2 = tl.load(p_b2, boundary_check=(0, 1))
118
+ b_A += tl.dot(b_a1, b_b1, allow_tf32=False)
119
+ b_A2 += tl.dot(b_a2, b_b2, allow_tf32=False)
120
+ b_A3 += tl.dot(b_a2, b_b1, allow_tf32=False)
121
+
122
+ b_A = tl.where(tl.arange(0, BC)[:, None] > tl.arange(0, BC)[None, :], b_A, 0)
123
+ b_A2 = tl.where(tl.arange(0, BC)[:, None] > tl.arange(0, BC)[None, :], b_A2, 0)
124
+
125
+ for i in range(1, BC):
126
+ mask = tl.arange(0, BC) == i
127
+ b_a = tl.sum(tl.where(mask[:, None], b_A, 0), 0)
128
+ b_a2 = tl.sum(tl.where(mask[:, None], b_A2, 0), 0)
129
+ b_a = b_a + tl.sum(b_a[:, None] * b_A, 0) * (tl.arange(0, BC) < i)
130
+ b_a2 = b_a2 + tl.sum(b_a2[:, None] * b_A2, 0) * (tl.arange(0, BC) < i)
131
+ b_A = tl.where(mask[:, None], b_a, b_A)
132
+ b_A2 = tl.where(mask[:, None], b_a2, b_A2)
133
+
134
+ # blockwise computation of lower triangular matrix's inverse
135
+ # i.e., [A11, 0; A21, A22]^-1 = [A11^-1, 0; -A22^-1 A21 A11^-1, A22^-1]
136
+ b_A += tl.arange(0, BC)[:, None] == tl.arange(0, BC)[None, :]
137
+ b_A2 += tl.arange(0, BC)[:, None] == tl.arange(0, BC)[None, :]
138
+ b_A3 = tl.dot(tl.dot(b_A2, b_A3, allow_tf32=False), b_A, allow_tf32=False)
139
+
140
+ p_A1 = tl.make_block_ptr(A + (bos*H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BC, BC), (1, 0))
141
+ p_A2 = tl.make_block_ptr(A + (bos*H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT + BC, BC), (BC, BC), (1, 0))
142
+ p_A3 = tl.make_block_ptr(A + (bos*H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT + BC, 0), (BC, BC), (1, 0))
143
+ p_A4 = tl.make_block_ptr(A + (bos*H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, BC), (BC, BC), (1, 0))
144
+ tl.store(p_A1, b_A.to(p_A1.dtype.element_ty), boundary_check=(0, 1))
145
+ tl.store(p_A2, b_A2.to(p_A2.dtype.element_ty), boundary_check=(0, 1))
146
+ tl.store(p_A3, b_A3.to(p_A3.dtype.element_ty), boundary_check=(0, 1))
147
+ # causal mask
148
+ tl.store(p_A4, tl.zeros([BC, BC], dtype=tl.float32).to(p_A4.dtype.element_ty), boundary_check=(0, 1))
149
+
150
+
151
+ @triton.heuristics({
152
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
153
+ })
154
+ @triton.autotune(
155
+ configs=[
156
+ triton.Config({}, num_warps=num_warps)
157
+ for num_warps in NUM_WARPS
158
+ ],
159
+ key=['BT', 'BK', 'BV']
160
+ )
161
+ @triton.jit(do_not_specialize=['T'])
162
+ def wu_fwd_kernel(
163
+ w,
164
+ u,
165
+ a,
166
+ k,
167
+ v,
168
+ A,
169
+ cu_seqlens,
170
+ chunk_indices,
171
+ T,
172
+ H: tl.constexpr,
173
+ K: tl.constexpr,
174
+ V: tl.constexpr,
175
+ BT: tl.constexpr,
176
+ BK: tl.constexpr,
177
+ BV: tl.constexpr,
178
+ IS_VARLEN: tl.constexpr,
179
+ ):
180
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
181
+ i_b, i_h = i_bh // H, i_bh % H
182
+ if IS_VARLEN:
183
+ 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)
184
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
185
+ T = eos - bos
186
+ else:
187
+ bos, eos = i_b * T, i_b * T + T
188
+
189
+ p_A = tl.make_block_ptr(A + (bos*H + i_h) * BT, (T, BT), (H*BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
190
+
191
+ b_A = tl.load(p_A, boundary_check=(0, 1))
192
+ b_Aak = tl.zeros([BT, BT], dtype=tl.float32)
193
+
194
+ for i_k in range(tl.cdiv(K, BK)):
195
+ p_k = tl.make_block_ptr(k + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
196
+ p_a = tl.make_block_ptr(a + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
197
+ p_w = tl.make_block_ptr(w + (bos * H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
198
+ b_k = tl.load(p_k, boundary_check=(0, 1))
199
+ b_a = tl.load(p_a, boundary_check=(0, 1))
200
+ b_w = tl.dot(b_A, b_a)
201
+ b_Aak += tl.dot(b_a, tl.trans(b_k))
202
+ tl.store(p_w, b_w.to(p_w.dtype.element_ty), boundary_check=(0, 1))
203
+
204
+ b_Aak = tl.where(tl.arange(0, BT)[:, None] > tl.arange(0, BT)[None, :], b_Aak, 0)
205
+ b_Aak = b_Aak.to(k.dtype.element_ty)
206
+
207
+ for i_v in range(tl.cdiv(V, BV)):
208
+ 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))
209
+ p_u = tl.make_block_ptr(u + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
210
+ b_v = tl.load(p_v, boundary_check=(0, 1))
211
+ b_v = tl.dot(b_Aak, b_v).to(v.dtype.element_ty)
212
+ b_u = tl.dot(b_A, b_v)
213
+ tl.store(p_u, b_u.to(p_u.dtype.element_ty), boundary_check=(0, 1))
214
+
215
+
216
+ def prepare_wy_repr_fwd(
217
+ a: torch.Tensor,
218
+ b: torch.Tensor,
219
+ v: torch.Tensor,
220
+ k: torch.Tensor,
221
+ cu_seqlens: Optional[torch.LongTensor],
222
+ chunk_size: int = 64
223
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
224
+ B, T, H, K = a.shape
225
+ BT = min(chunk_size, max(triton.next_power_of_2(T), 16))
226
+
227
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
228
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
229
+ BC = min(BT, 32)
230
+ BK = min(max(triton.next_power_of_2(K), 16), 64)
231
+
232
+ A = torch.empty(B, T, H, BT, device=a.device, dtype=a.dtype)
233
+ fwd_fn = prepare_wy_repr_fwd_kernel_chunk64 if BT == 64 else prepare_wy_repr_fwd_kernel_chunk32
234
+
235
+ fwd_fn[(NT, B * H)](
236
+ a=a,
237
+ b=b,
238
+ A=A,
239
+ cu_seqlens=cu_seqlens,
240
+ chunk_indices=chunk_indices,
241
+ T=T,
242
+ H=H,
243
+ K=K,
244
+ BT=BT,
245
+ BK=BK,
246
+ BC=BC,
247
+ )
248
+ w, u = wu_fwd(
249
+ a=a,
250
+ v=v,
251
+ k=k,
252
+ A=A,
253
+ cu_seqlens=cu_seqlens,
254
+ chunk_size=chunk_size
255
+ )
256
+ return w, u, A
257
+
258
+
259
+ def wu_fwd(
260
+ a: torch.Tensor,
261
+ v: torch.Tensor,
262
+ k: torch.Tensor,
263
+ A: torch.Tensor,
264
+ cu_seqlens: Optional[torch.LongTensor],
265
+ chunk_size: int
266
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
267
+ B, T, H, K, V = *a.shape, v.shape[-1]
268
+ BT = min(chunk_size, max(triton.next_power_of_2(T), 16))
269
+
270
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
271
+ NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
272
+ CONST_TILING = 64 if check_shared_mem() else 32
273
+ BK = min(max(triton.next_power_of_2(K), 16), CONST_TILING)
274
+ BV = min(max(triton.next_power_of_2(V), 16), CONST_TILING)
275
+
276
+ u = torch.empty_like(v)
277
+ w = torch.empty_like(a)
278
+ wu_fwd_kernel[(NT, B*H)](
279
+ a=a,
280
+ v=v,
281
+ w=w,
282
+ u=u,
283
+ A=A,
284
+ k=k,
285
+ cu_seqlens=cu_seqlens,
286
+ chunk_indices=chunk_indices,
287
+ T=T,
288
+ H=H,
289
+ K=K,
290
+ V=V,
291
+ BT=BT,
292
+ BK=BK,
293
+ BV=BV,
294
+ )
295
+ return w, u
296
+
297
+
298
+ fwd_prepare_wy_repr = prepare_wy_repr_fwd
299
+
300
+ fwd_wu = wu_fwd
@@ -0,0 +1,11 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from .chunk import chunk_gla
4
+ from .fused_chunk import fused_chunk_gla
5
+ from .fused_recurrent import fused_recurrent_gla
6
+
7
+ __all__ = [
8
+ 'chunk_gla',
9
+ 'fused_chunk_gla',
10
+ 'fused_recurrent_gla'
11
+ ]