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,381 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
3
+
4
+ import os
5
+ from typing import Optional
6
+
7
+ import torch
8
+ import triton
9
+ import triton.language as tl
10
+
11
+ from fla.ops.utils.index import prepare_chunk_indices
12
+ from fla.ops.utils.op import make_tensor_descriptor
13
+ from fla.utils import input_guard, is_amd, is_tma_supported
14
+
15
+ FLA_TRIL_PRECISION = os.environ.get('FLA_TRIL_PRECISION', 'ieee')
16
+ ALLOWED_TRIL_PRECISIONS = ['ieee', 'tf32'] if is_amd else ['ieee', 'tf32', 'tf32x3']
17
+ assert FLA_TRIL_PRECISION in ALLOWED_TRIL_PRECISIONS, \
18
+ f'FLA_TRIL_PRECISION must be one of {ALLOWED_TRIL_PRECISIONS}, but got {FLA_TRIL_PRECISION}'
19
+
20
+
21
+ @triton.heuristics({
22
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
23
+ })
24
+ @triton.autotune(
25
+ configs=[
26
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
27
+ for num_warps in [1, 2, 4, 8]
28
+ for num_stages in [2, 3, 4, 5]
29
+ ],
30
+ key=['BT'],
31
+ )
32
+ @triton.jit(do_not_specialize=['T'])
33
+ def solve_tril_16x16_kernel(
34
+ A,
35
+ Ai,
36
+ cu_seqlens,
37
+ chunk_indices,
38
+ T,
39
+ H: tl.constexpr,
40
+ BT: tl.constexpr,
41
+ USE_TMA: tl.constexpr,
42
+ IS_VARLEN: tl.constexpr,
43
+ DOT_PRECISION: tl.constexpr
44
+ ):
45
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
46
+ i_b, i_h = i_bh // H, i_bh % H
47
+ if IS_VARLEN:
48
+ 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)
49
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
50
+ T = eos - bos
51
+ else:
52
+ bos, eos = i_b * T, i_b * T + T
53
+ o_i = tl.arange(0, 16)
54
+ m_A = o_i[:, None] > o_i[None, :]
55
+ m_I = o_i[:, None] == o_i[None, :]
56
+
57
+ A = A + (bos*H + i_h) * BT
58
+ Ai = Ai + (bos*H + i_h) * 16
59
+
60
+ offset = (i_t * 16) % BT
61
+ if not USE_TMA:
62
+ p_A = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * 16, offset), (16, 16), (1, 0))
63
+ # [16, 16]
64
+ b_A = tl.load(p_A, boundary_check=(0, 1)).to(tl.float32)
65
+ else:
66
+ desc = make_tensor_descriptor(A, [T, BT], [H*BT, 1], [16, 16])
67
+ desc_o = make_tensor_descriptor(Ai, [T, 16], [H*16, 1], [16, 16])
68
+ b_A = desc.load([i_t * 16, offset]).to(tl.float32)
69
+ b_A = -tl.where(m_A, b_A, 0)
70
+
71
+ for i in range(2, min(16, T - i_t * 16)):
72
+ # [16]
73
+ b_a = -tl.load(A + (i_t * 16 + i) * H*BT + o_i + offset)
74
+ b_a = b_a + tl.sum(b_a[:, None] * b_A, 0)
75
+ b_A = tl.where((o_i == i)[:, None], b_a, b_A)
76
+ b_A += m_I
77
+ if not USE_TMA:
78
+ p_Ai = tl.make_block_ptr(Ai, (T, 16), (H*16, 1), (i_t * 16, 0), (16, 16), (1, 0))
79
+ tl.store(p_Ai, b_A.to(p_Ai.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
80
+ else:
81
+ desc_o.store([i_t * 16, 0], b_A.to(desc_o.dtype, fp_downcast_rounding="rtne"))
82
+
83
+
84
+ @triton.heuristics({
85
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
86
+ })
87
+ @triton.autotune(
88
+ configs=[
89
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
90
+ for num_warps in [1, 2, 4, 8]
91
+ for num_stages in [2, 3, 4, 5]
92
+ ],
93
+ key=['H', 'BT', 'IS_VARLEN'],
94
+ )
95
+ @triton.jit(do_not_specialize=['T'])
96
+ def merge_16x16_to_32x32_inverse_kernel(
97
+ A,
98
+ Ai,
99
+ cu_seqlens,
100
+ chunk_indices,
101
+ T,
102
+ H: tl.constexpr,
103
+ BT: tl.constexpr,
104
+ USE_TMA: tl.constexpr,
105
+ IS_VARLEN: tl.constexpr,
106
+ DOT_PRECISION: tl.constexpr
107
+ ):
108
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
109
+ i_b, i_h = i_bh // H, i_bh % H
110
+ if IS_VARLEN:
111
+ 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)
112
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
113
+ T = eos - bos
114
+ else:
115
+ bos, eos = i_b * T, i_b * T + T
116
+
117
+ o_i = tl.arange(0, 16)
118
+ m_A = o_i[:, None] > o_i[None, :]
119
+ m_I = o_i[:, None] == o_i[None, :]
120
+ A += (bos * H + i_h) * BT
121
+ Ai += (bos * H + i_h) * BT
122
+
123
+ if not USE_TMA:
124
+ p_A_11 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT, 0), (16, 16), (1, 0))
125
+ p_A_22 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 16, 16), (16, 16), (1, 0))
126
+ b_Ai_11 = tl.load(p_A_11, boundary_check=(0, 1)).to(tl.float32)
127
+ b_Ai_22 = tl.load(p_A_22, boundary_check=(0, 1)).to(tl.float32)
128
+ else:
129
+ desc = make_tensor_descriptor(A, [T, BT], [H*BT, 1], [16, 16])
130
+ desc_o = make_tensor_descriptor(Ai, [T, BT], [H*BT, 1], [16, 16])
131
+ b_Ai_11 = desc.load([i_t * BT + 0, 0]).to(tl.float32)
132
+ b_Ai_22 = desc.load([i_t * BT + 16, 16]).to(tl.float32)
133
+
134
+ # [16, 16]
135
+ b_Ai_11 = -tl.where(m_A, b_Ai_11, 0)
136
+ b_Ai_22 = -tl.where(m_A, b_Ai_22, 0)
137
+
138
+ for i in range(2, min(16, T - i_t * BT)):
139
+ b_a_11 = -tl.load(A + (i_t * BT + i) * H*BT + o_i)
140
+ b_a_11 += tl.sum(b_a_11[:, None] * b_Ai_11, 0)
141
+ b_Ai_11 = tl.where((o_i == i)[:, None], b_a_11, b_Ai_11)
142
+ for i in range(16 + 2, min(32, T - i_t * BT)):
143
+ b_a_22 = -tl.load(A + (i_t * BT + i) * H*BT + o_i + 16)
144
+ b_a_22 += tl.sum(b_a_22[:, None] * b_Ai_22, 0)
145
+ b_Ai_22 = tl.where((o_i == i - 16)[:, None], b_a_22, b_Ai_22)
146
+
147
+ b_Ai_11 += m_I
148
+ b_Ai_22 += m_I
149
+
150
+ if not USE_TMA:
151
+ p_A_21 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 16, 0), (16, 16), (1, 0))
152
+ b_A_21 = tl.load(p_A_21, boundary_check=(0, 1)).to(tl.float32)
153
+ else:
154
+ b_A_21 = desc.load([i_t * BT + 16, 0]).to(tl.float32)
155
+
156
+ b_Ai_21 = -tl.dot(tl.dot(b_Ai_22, b_A_21, input_precision=DOT_PRECISION), b_Ai_11, input_precision=DOT_PRECISION)
157
+
158
+ if not USE_TMA:
159
+ p_Ai_11 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT, 0), (16, 16), (1, 0))
160
+ p_Ai_21 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 16, 0), (16, 16), (1, 0))
161
+ p_Ai_22 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 16, 16), (16, 16), (1, 0))
162
+ tl.store(p_Ai_11, b_Ai_11.to(p_Ai_11.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
163
+ tl.store(p_Ai_22, b_Ai_22.to(p_Ai_22.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
164
+ tl.store(p_Ai_21, b_Ai_21.to(p_Ai_21.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
165
+ else:
166
+ desc_o.store([i_t * BT + 0, 0], b_Ai_11.to(desc_o.dtype, fp_downcast_rounding="rtne"))
167
+ desc_o.store([i_t * BT + 16, 0], b_Ai_21.to(desc_o.dtype, fp_downcast_rounding="rtne"))
168
+ desc_o.store([i_t * BT + 16, 16], b_Ai_22.to(desc_o.dtype, fp_downcast_rounding="rtne"))
169
+
170
+
171
+ @triton.heuristics({
172
+ 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
173
+ })
174
+ @triton.autotune(
175
+ configs=[
176
+ triton.Config({}, num_warps=num_warps, num_stages=num_stages)
177
+ for num_warps in [2, 4, 8]
178
+ for num_stages in [2, 3, 4, 5]
179
+ ],
180
+ key=['H', 'BT', 'IS_VARLEN'],
181
+ )
182
+ @triton.jit(do_not_specialize=['T'])
183
+ def merge_16x16_to_64x64_inverse_kernel(
184
+ A,
185
+ Ai,
186
+ cu_seqlens,
187
+ chunk_indices,
188
+ T,
189
+ H: tl.constexpr,
190
+ BT: tl.constexpr,
191
+ USE_TMA: tl.constexpr,
192
+ IS_VARLEN: tl.constexpr,
193
+ DOT_PRECISION: tl.constexpr
194
+ ):
195
+ i_t, i_bh = tl.program_id(0), tl.program_id(1)
196
+ i_b, i_h = i_bh // H, i_bh % H
197
+ if IS_VARLEN:
198
+ 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)
199
+ bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
200
+ T = eos - bos
201
+ else:
202
+ bos, eos = i_b * T, i_b * T + T
203
+
204
+ o_i = tl.arange(0, 16)
205
+ m_A = o_i[:, None] > o_i[None, :]
206
+ m_I = o_i[:, None] == o_i[None, :]
207
+ A += (bos * H + i_h) * BT
208
+ Ai += (bos * H + i_h) * BT
209
+
210
+ if not USE_TMA:
211
+ p_A_11 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT, 0), (16, 16), (1, 0))
212
+ p_A_22 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 16, 16), (16, 16), (1, 0))
213
+ p_A_33 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 32, 32), (16, 16), (1, 0))
214
+ p_A_44 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 48, 48), (16, 16), (1, 0))
215
+ b_Ai_11 = tl.load(p_A_11, boundary_check=(0, 1)).to(tl.float32)
216
+ b_Ai_22 = tl.load(p_A_22, boundary_check=(0, 1)).to(tl.float32)
217
+ b_Ai_33 = tl.load(p_A_33, boundary_check=(0, 1)).to(tl.float32)
218
+ b_Ai_44 = tl.load(p_A_44, boundary_check=(0, 1)).to(tl.float32)
219
+ else:
220
+ desc = make_tensor_descriptor(A, [T, BT], [H*BT, 1], [16, 16])
221
+ desc_o = make_tensor_descriptor(Ai, [T, BT], [H*BT, 1], [16, 16])
222
+ b_Ai_11 = desc.load([i_t * BT + 0, 0]).to(tl.float32)
223
+ b_Ai_22 = desc.load([i_t * BT + 16, 16]).to(tl.float32)
224
+ b_Ai_33 = desc.load([i_t * BT + 32, 32]).to(tl.float32)
225
+ b_Ai_44 = desc.load([i_t * BT + 48, 48]).to(tl.float32)
226
+
227
+ # [16, 16]
228
+ b_Ai_11 = -tl.where(m_A, b_Ai_11, 0)
229
+ b_Ai_22 = -tl.where(m_A, b_Ai_22, 0)
230
+ b_Ai_33 = -tl.where(m_A, b_Ai_33, 0)
231
+ b_Ai_44 = -tl.where(m_A, b_Ai_44, 0)
232
+
233
+ for i in range(2, min(16, T - i_t * BT)):
234
+ b_a_11 = -tl.load(A + (i_t * BT + i) * H*BT + o_i)
235
+ b_a_11 += tl.sum(b_a_11[:, None] * b_Ai_11, 0)
236
+ b_Ai_11 = tl.where((o_i == i)[:, None], b_a_11, b_Ai_11)
237
+ for i in range(16 + 2, min(32, T - i_t * BT)):
238
+ b_a_22 = -tl.load(A + (i_t * BT + i) * H*BT + o_i + 16)
239
+ b_a_22 += tl.sum(b_a_22[:, None] * b_Ai_22, 0)
240
+ b_Ai_22 = tl.where((o_i == i - 16)[:, None], b_a_22, b_Ai_22)
241
+ for i in range(32 + 2, min(48, T - i_t * BT)):
242
+ b_a_33 = -tl.load(A + (i_t * BT + i) * H*BT + o_i + 32)
243
+ b_a_33 += tl.sum(b_a_33[:, None] * b_Ai_33, 0)
244
+ b_Ai_33 = tl.where((o_i == i - 32)[:, None], b_a_33, b_Ai_33)
245
+ for i in range(48 + 2, min(64, T - i_t * BT)):
246
+ b_a_44 = -tl.load(A + (i_t * BT + i) * H*BT + o_i + 48)
247
+ b_a_44 += tl.sum(b_a_44[:, None] * b_Ai_44, 0)
248
+ b_Ai_44 = tl.where((o_i == i - 48)[:, None], b_a_44, b_Ai_44)
249
+ b_Ai_11 += m_I
250
+ b_Ai_22 += m_I
251
+ b_Ai_33 += m_I
252
+ b_Ai_44 += m_I
253
+
254
+ if not USE_TMA:
255
+ p_A_21 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 16, 0), (16, 16), (1, 0))
256
+ p_A_31 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 32, 0), (16, 16), (1, 0))
257
+ p_A_32 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 32, 16), (16, 16), (1, 0))
258
+ p_A_41 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 48, 0), (16, 16), (1, 0))
259
+ p_A_42 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 48, 16), (16, 16), (1, 0))
260
+ p_A_43 = tl.make_block_ptr(A, (T, BT), (H*BT, 1), (i_t * BT + 48, 32), (16, 16), (1, 0))
261
+ b_A_21 = tl.load(p_A_21, boundary_check=(0, 1)).to(tl.float32)
262
+ b_A_31 = tl.load(p_A_31, boundary_check=(0, 1)).to(tl.float32)
263
+ b_A_32 = tl.load(p_A_32, boundary_check=(0, 1)).to(tl.float32)
264
+ b_A_41 = tl.load(p_A_41, boundary_check=(0, 1)).to(tl.float32)
265
+ b_A_42 = tl.load(p_A_42, boundary_check=(0, 1)).to(tl.float32)
266
+ b_A_43 = tl.load(p_A_43, boundary_check=(0, 1)).to(tl.float32)
267
+ else:
268
+ b_A_21 = desc.load([i_t * BT + 16, 0]).to(tl.float32)
269
+ b_A_31 = desc.load([i_t * BT + 32, 0]).to(tl.float32)
270
+ b_A_32 = desc.load([i_t * BT + 32, 16]).to(tl.float32)
271
+ b_A_41 = desc.load([i_t * BT + 48, 0]).to(tl.float32)
272
+ b_A_42 = desc.load([i_t * BT + 48, 16]).to(tl.float32)
273
+ b_A_43 = desc.load([i_t * BT + 48, 32]).to(tl.float32)
274
+
275
+ b_Ai_21 = -tl.dot(tl.dot(b_Ai_22, b_A_21, input_precision=DOT_PRECISION), b_Ai_11, input_precision=DOT_PRECISION)
276
+ b_Ai_32 = -tl.dot(tl.dot(b_Ai_33, b_A_32, input_precision=DOT_PRECISION), b_Ai_22, input_precision=DOT_PRECISION)
277
+ b_Ai_43 = -tl.dot(tl.dot(b_Ai_44, b_A_43, input_precision=DOT_PRECISION), b_Ai_33, input_precision=DOT_PRECISION)
278
+
279
+ b_Ai_31 = -tl.dot(
280
+ b_Ai_33,
281
+ tl.dot(b_A_31, b_Ai_11, input_precision=DOT_PRECISION) +
282
+ tl.dot(b_A_32, b_Ai_21, input_precision=DOT_PRECISION),
283
+ input_precision=DOT_PRECISION
284
+ )
285
+ b_Ai_42 = -tl.dot(
286
+ b_Ai_44,
287
+ tl.dot(b_A_42, b_Ai_22, input_precision=DOT_PRECISION) +
288
+ tl.dot(b_A_43, b_Ai_32, input_precision=DOT_PRECISION),
289
+ input_precision=DOT_PRECISION
290
+ )
291
+ b_Ai_41 = -tl.dot(
292
+ b_Ai_44,
293
+ tl.dot(b_A_41, b_Ai_11, input_precision=DOT_PRECISION) +
294
+ tl.dot(b_A_42, b_Ai_21, input_precision=DOT_PRECISION) +
295
+ tl.dot(b_A_43, b_Ai_31, input_precision=DOT_PRECISION),
296
+ input_precision=DOT_PRECISION
297
+ )
298
+
299
+ if not USE_TMA:
300
+ p_Ai_11 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT, 0), (16, 16), (1, 0))
301
+ p_Ai_22 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 16, 16), (16, 16), (1, 0))
302
+ p_Ai_33 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 32, 32), (16, 16), (1, 0))
303
+ p_Ai_44 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 48, 48), (16, 16), (1, 0))
304
+ p_Ai_21 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 16, 0), (16, 16), (1, 0))
305
+ p_Ai_31 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 32, 0), (16, 16), (1, 0))
306
+ p_Ai_32 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 32, 16), (16, 16), (1, 0))
307
+ p_Ai_41 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 48, 0), (16, 16), (1, 0))
308
+ p_Ai_42 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 48, 16), (16, 16), (1, 0))
309
+ p_Ai_43 = tl.make_block_ptr(Ai, (T, BT), (H*BT, 1), (i_t * BT + 48, 32), (16, 16), (1, 0))
310
+ tl.store(p_Ai_11, b_Ai_11.to(p_Ai_11.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
311
+ tl.store(p_Ai_22, b_Ai_22.to(p_Ai_22.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
312
+ tl.store(p_Ai_33, b_Ai_33.to(p_Ai_33.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
313
+ tl.store(p_Ai_44, b_Ai_44.to(p_Ai_44.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
314
+ tl.store(p_Ai_21, b_Ai_21.to(p_Ai_21.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
315
+ tl.store(p_Ai_31, b_Ai_31.to(p_Ai_31.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
316
+ tl.store(p_Ai_32, b_Ai_32.to(p_Ai_32.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
317
+ tl.store(p_Ai_41, b_Ai_41.to(p_Ai_41.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
318
+ tl.store(p_Ai_42, b_Ai_42.to(p_Ai_42.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
319
+ tl.store(p_Ai_43, b_Ai_43.to(p_Ai_43.dtype.element_ty, fp_downcast_rounding="rtne"), boundary_check=(0, 1))
320
+ else:
321
+ desc_o.store([i_t * BT + 0, 0], b_Ai_11.to(desc_o.dtype, fp_downcast_rounding="rtne"))
322
+ desc_o.store([i_t * BT + 16, 16], b_Ai_22.to(desc_o.dtype, fp_downcast_rounding="rtne"))
323
+ desc_o.store([i_t * BT + 32, 32], b_Ai_33.to(desc_o.dtype, fp_downcast_rounding="rtne"))
324
+ desc_o.store([i_t * BT + 48, 48], b_Ai_44.to(desc_o.dtype, fp_downcast_rounding="rtne"))
325
+ desc_o.store([i_t * BT + 16, 0], b_Ai_21.to(desc_o.dtype, fp_downcast_rounding="rtne"))
326
+ desc_o.store([i_t * BT + 32, 0], b_Ai_31.to(desc_o.dtype, fp_downcast_rounding="rtne"))
327
+ desc_o.store([i_t * BT + 32, 16], b_Ai_32.to(desc_o.dtype, fp_downcast_rounding="rtne"))
328
+ desc_o.store([i_t * BT + 48, 0], b_Ai_41.to(desc_o.dtype, fp_downcast_rounding="rtne"))
329
+ desc_o.store([i_t * BT + 48, 16], b_Ai_42.to(desc_o.dtype, fp_downcast_rounding="rtne"))
330
+ desc_o.store([i_t * BT + 48, 32], b_Ai_43.to(desc_o.dtype, fp_downcast_rounding="rtne"))
331
+
332
+
333
+ @input_guard
334
+ def solve_tril(
335
+ A: torch.Tensor,
336
+ cu_seqlens: Optional[torch.Tensor] = None,
337
+ output_dtype: torch.dtype = torch.float
338
+ ) -> torch.Tensor:
339
+ """
340
+ Compute the inverse of the matrix I + A
341
+ A should be strictly lower triangular, i.e., A.triu() == 0.
342
+
343
+ Args:
344
+ A (torch.Tensor):
345
+ [B, T, H, BT], where BT should only be 16, 32, or 64.
346
+ cu_seqlens (torch.Tensor):
347
+ The cumulative sequence lengths of the input tensor. Default: `None`.
348
+ output_dtype (torch.dtype):
349
+ The dtype of the output tensor. Default: `torch.float`.
350
+ If `None`, the output dtype will be the same as the input dtype.
351
+
352
+ Returns:
353
+ (I + A)^-1 with the same shape as A
354
+ """
355
+ assert A.shape[-1] in [16, 32, 64]
356
+ output_dtype = A.dtype if output_dtype is None else output_dtype
357
+
358
+ B, T, H, BT = A.shape
359
+ chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
360
+ NT = len(chunk_indices) if cu_seqlens is not None else triton.cdiv(T, BT)
361
+
362
+ Ai = torch.zeros_like(A, dtype=output_dtype)
363
+ if BT == 16:
364
+ merge_fn = solve_tril_16x16_kernel
365
+ elif BT == 32:
366
+ merge_fn = merge_16x16_to_32x32_inverse_kernel
367
+ elif BT == 64:
368
+ merge_fn = merge_16x16_to_64x64_inverse_kernel
369
+
370
+ merge_fn[NT, B * H](
371
+ A=A,
372
+ Ai=Ai,
373
+ cu_seqlens=cu_seqlens,
374
+ chunk_indices=chunk_indices,
375
+ T=T,
376
+ H=H,
377
+ BT=BT,
378
+ USE_TMA=is_tma_supported,
379
+ DOT_PRECISION=FLA_TRIL_PRECISION,
380
+ )
381
+ return Ai