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.
- fla/__init__.py +4 -0
- fla/modules/__init__.py +33 -0
- fla/modules/activations.py +548 -0
- fla/modules/convolution.py +1143 -0
- fla/modules/feature_map.py +300 -0
- fla/modules/fused_bitlinear.py +632 -0
- fla/modules/fused_cross_entropy.py +419 -0
- fla/modules/fused_kl_div.py +324 -0
- fla/modules/fused_linear_cross_entropy.py +620 -0
- fla/modules/fused_norm_gate.py +1253 -0
- fla/modules/grpo.py +411 -0
- fla/modules/l2norm.py +285 -0
- fla/modules/l2warp.py +38 -0
- fla/modules/layernorm.py +1433 -0
- fla/modules/layernorm_gated.py +528 -0
- fla/modules/mlp.py +132 -0
- fla/modules/parallel.py +42 -0
- fla/modules/rotary.py +500 -0
- fla/modules/token_shift.py +543 -0
- fla/ops/__init__.py +53 -0
- fla/ops/abc/__init__.py +7 -0
- fla/ops/abc/chunk.py +1117 -0
- fla/ops/abc/naive.py +96 -0
- fla/ops/attn/__init__.py +7 -0
- fla/ops/attn/decoding.py +182 -0
- fla/ops/attn/parallel.py +730 -0
- fla/ops/based/__init__.py +9 -0
- fla/ops/based/fused_chunk.py +373 -0
- fla/ops/based/naive.py +72 -0
- fla/ops/based/parallel.py +408 -0
- fla/ops/comba/__init__.py +7 -0
- fla/ops/comba/chunk.py +342 -0
- fla/ops/comba/fused_recurrent.py +332 -0
- fla/ops/comba/utils.py +172 -0
- fla/ops/comba/wy_fast.py +423 -0
- fla/ops/common/__init__.py +1 -0
- fla/ops/common/chunk_delta_h.py +527 -0
- fla/ops/common/chunk_h.py +384 -0
- fla/ops/common/chunk_h_parallel.py +551 -0
- fla/ops/common/chunk_h_split.py +596 -0
- fla/ops/common/chunk_o.py +681 -0
- fla/ops/common/chunk_scaled_dot_kkt.py +305 -0
- fla/ops/common/fused_chunk.py +629 -0
- fla/ops/common/fused_recurrent.py +567 -0
- fla/ops/delta_rule/__init__.py +11 -0
- fla/ops/delta_rule/chunk.py +311 -0
- fla/ops/delta_rule/fused_chunk.py +6 -0
- fla/ops/delta_rule/fused_recurrent.py +535 -0
- fla/ops/delta_rule/naive.py +120 -0
- fla/ops/delta_rule/parallel.py +403 -0
- fla/ops/delta_rule/wy_fast.py +294 -0
- fla/ops/forgetting_attn/__init__.py +7 -0
- fla/ops/forgetting_attn/parallel.py +64 -0
- fla/ops/gated_delta_product/__init__.py +5 -0
- fla/ops/gated_delta_product/chunk.py +305 -0
- fla/ops/gated_delta_product/chunk_deltaproduct_h.py +502 -0
- fla/ops/gated_delta_product/chunk_deltaproduct_o.py +154 -0
- fla/ops/gated_delta_product/chunk_ref.py +68 -0
- fla/ops/gated_delta_product/naive.py +36 -0
- fla/ops/gated_delta_rule/__init__.py +7 -0
- fla/ops/gated_delta_rule/chunk.py +340 -0
- fla/ops/gated_delta_rule/fused_recurrent.py +351 -0
- fla/ops/gated_delta_rule/wy_fast.py +296 -0
- fla/ops/generalized_delta_rule/__init__.py +9 -0
- fla/ops/generalized_delta_rule/dplr/__init__.py +7 -0
- fla/ops/generalized_delta_rule/dplr/chunk.py +362 -0
- fla/ops/generalized_delta_rule/dplr/chunk_A_bwd.py +367 -0
- fla/ops/generalized_delta_rule/dplr/chunk_A_fwd.py +198 -0
- fla/ops/generalized_delta_rule/dplr/chunk_h_bwd.py +175 -0
- fla/ops/generalized_delta_rule/dplr/chunk_h_fwd.py +175 -0
- fla/ops/generalized_delta_rule/dplr/chunk_o_bwd.py +430 -0
- fla/ops/generalized_delta_rule/dplr/chunk_o_fwd.py +125 -0
- fla/ops/generalized_delta_rule/dplr/fused_recurrent.py +268 -0
- fla/ops/generalized_delta_rule/dplr/naive.py +96 -0
- fla/ops/generalized_delta_rule/dplr/wy_fast_bwd.py +164 -0
- fla/ops/generalized_delta_rule/dplr/wy_fast_fwd.py +284 -0
- fla/ops/generalized_delta_rule/iplr/__init__.py +7 -0
- fla/ops/generalized_delta_rule/iplr/chunk.py +492 -0
- fla/ops/generalized_delta_rule/iplr/fused_recurrent.py +452 -0
- fla/ops/generalized_delta_rule/iplr/naive.py +69 -0
- fla/ops/generalized_delta_rule/iplr/wy_fast.py +300 -0
- fla/ops/gla/__init__.py +11 -0
- fla/ops/gla/chunk.py +1298 -0
- fla/ops/gla/fused_chunk.py +642 -0
- fla/ops/gla/fused_recurrent.py +111 -0
- fla/ops/gla/naive.py +41 -0
- fla/ops/gsa/__init__.py +9 -0
- fla/ops/gsa/chunk.py +1133 -0
- fla/ops/gsa/fused_recurrent.py +536 -0
- fla/ops/gsa/naive.py +69 -0
- fla/ops/hgrn/__init__.py +9 -0
- fla/ops/hgrn/chunk.py +282 -0
- fla/ops/hgrn/fused_recurrent.py +308 -0
- fla/ops/hgrn/naive.py +63 -0
- fla/ops/lightning_attn/__init__.py +9 -0
- fla/ops/lightning_attn/chunk.py +84 -0
- fla/ops/lightning_attn/fused_recurrent.py +84 -0
- fla/ops/linear_attn/__init__.py +11 -0
- fla/ops/linear_attn/chunk.py +76 -0
- fla/ops/linear_attn/fused_chunk.py +61 -0
- fla/ops/linear_attn/fused_recurrent.py +35 -0
- fla/ops/linear_attn/naive.py +64 -0
- fla/ops/linear_attn/utils.py +10 -0
- fla/ops/log_linear_attn/__init__.py +7 -0
- fla/ops/log_linear_attn/chunk.py +1904 -0
- fla/ops/log_linear_attn/naive.py +50 -0
- fla/ops/mesa_net/__init__.py +5 -0
- fla/ops/mesa_net/chunk.py +371 -0
- fla/ops/mesa_net/chunk_cg_solver_bwd.py +162 -0
- fla/ops/mesa_net/chunk_cg_solver_fwd.py +186 -0
- fla/ops/mesa_net/chunk_h_fwd.py +170 -0
- fla/ops/mesa_net/chunk_h_kk_intra_bwd.py +191 -0
- fla/ops/mesa_net/chunk_h_kv_intra_bwd.py +209 -0
- fla/ops/mesa_net/chunk_h_kv_intra_bwd_separate.py +300 -0
- fla/ops/mesa_net/decoding_one_step.py +175 -0
- fla/ops/mesa_net/naive.py +131 -0
- fla/ops/nsa/__init__.py +9 -0
- fla/ops/nsa/compression.py +536 -0
- fla/ops/nsa/naive.py +103 -0
- fla/ops/nsa/parallel.py +879 -0
- fla/ops/nsa/utils.py +92 -0
- fla/ops/path_attn/__init__.py +7 -0
- fla/ops/path_attn/cumprod_householder_bwd.py +140 -0
- fla/ops/path_attn/cumprod_householder_fwd.py +119 -0
- fla/ops/path_attn/intra_chunk_preprocess_bwd.py +140 -0
- fla/ops/path_attn/intra_chunk_preprocess_bwd_prepare.py +208 -0
- fla/ops/path_attn/intra_chunk_preprocess_fwd.py +168 -0
- fla/ops/path_attn/parallel.py +270 -0
- fla/ops/path_attn/parallel_path_bwd_inter_dkv.py +189 -0
- fla/ops/path_attn/parallel_path_bwd_inter_dqh.py +200 -0
- fla/ops/path_attn/parallel_path_bwd_intra.py +172 -0
- fla/ops/path_attn/parallel_path_fwd.py +195 -0
- fla/ops/path_attn/prepare_k_cache.py +74 -0
- fla/ops/path_attn/transform_q.py +108 -0
- fla/ops/rebased/__init__.py +7 -0
- fla/ops/rebased/naive.py +27 -0
- fla/ops/rebased/parallel.py +464 -0
- fla/ops/retention/__init__.py +13 -0
- fla/ops/retention/chunk.py +77 -0
- fla/ops/retention/fused_chunk.py +78 -0
- fla/ops/retention/fused_recurrent.py +33 -0
- fla/ops/retention/naive.py +15 -0
- fla/ops/retention/parallel.py +71 -0
- fla/ops/rwkv4/__init__.py +7 -0
- fla/ops/rwkv4/fused_recurrent.py +472 -0
- fla/ops/rwkv6/__init__.py +9 -0
- fla/ops/rwkv6/chunk.py +1300 -0
- fla/ops/rwkv6/chunk_naive.py +43 -0
- fla/ops/rwkv6/fused_recurrent.py +673 -0
- fla/ops/rwkv6/recurrent_naive.py +103 -0
- fla/ops/rwkv7/__init__.py +10 -0
- fla/ops/rwkv7/channel_mixing.py +323 -0
- fla/ops/rwkv7/chunk.py +78 -0
- fla/ops/rwkv7/fused_addcmul.py +293 -0
- fla/ops/rwkv7/fused_k_update.py +346 -0
- fla/ops/rwkv7/fused_recurrent.py +334 -0
- fla/ops/rwkv7/gate_output_correction.py +244 -0
- fla/ops/simple_gla/__init__.py +13 -0
- fla/ops/simple_gla/chunk.py +303 -0
- fla/ops/simple_gla/fused_chunk.py +108 -0
- fla/ops/simple_gla/fused_recurrent.py +112 -0
- fla/ops/simple_gla/naive.py +114 -0
- fla/ops/simple_gla/parallel.py +722 -0
- fla/ops/titans/__init__.py +7 -0
- fla/ops/titans/log_impl.py +153 -0
- fla/ops/titans/naive.py +375 -0
- fla/ops/ttt/__init__.py +9 -0
- fla/ops/ttt/chunk.py +1441 -0
- fla/ops/ttt/fused_chunk.py +831 -0
- fla/ops/ttt/naive.py +126 -0
- fla/ops/utils/__init__.py +58 -0
- fla/ops/utils/constant.py +6 -0
- fla/ops/utils/cumsum.py +465 -0
- fla/ops/utils/index.py +133 -0
- fla/ops/utils/logcumsumexp.py +52 -0
- fla/ops/utils/logsumexp.py +80 -0
- fla/ops/utils/matmul.py +245 -0
- fla/ops/utils/op.py +62 -0
- fla/ops/utils/pack.py +208 -0
- fla/ops/utils/pooling.py +207 -0
- fla/ops/utils/softmax.py +106 -0
- fla/ops/utils/solve_tril.py +381 -0
- fla/utils.py +462 -0
- fla_core-0.3.2.dist-info/METADATA +575 -0
- fla_core-0.3.2.dist-info/RECORD +188 -0
- fla_core-0.3.2.dist-info/WHEEL +5 -0
- fla_core-0.3.2.dist-info/licenses/LICENSE +21 -0
- fla_core-0.3.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
import warnings
|
|
5
|
+
from typing import Tuple
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
import triton
|
|
9
|
+
import triton.language as tl
|
|
10
|
+
from einops import rearrange
|
|
11
|
+
|
|
12
|
+
from fla.ops.delta_rule.wy_fast import fwd_prepare_T
|
|
13
|
+
from fla.utils import autocast_custom_bwd, autocast_custom_fwd, input_guard
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@triton.autotune(
|
|
17
|
+
configs=[
|
|
18
|
+
triton.Config({}, num_warps=num_warps)
|
|
19
|
+
for num_warps in [1, 2, 4]
|
|
20
|
+
],
|
|
21
|
+
key=['BT', 'K', 'V'],
|
|
22
|
+
)
|
|
23
|
+
@triton.jit(do_not_specialize=['T'])
|
|
24
|
+
def chunk_transform_qk_fwd_kernel(
|
|
25
|
+
q,
|
|
26
|
+
k,
|
|
27
|
+
v,
|
|
28
|
+
beta,
|
|
29
|
+
o,
|
|
30
|
+
A,
|
|
31
|
+
q_new,
|
|
32
|
+
k_new,
|
|
33
|
+
A_local,
|
|
34
|
+
scale,
|
|
35
|
+
T,
|
|
36
|
+
K: tl.constexpr,
|
|
37
|
+
V: tl.constexpr,
|
|
38
|
+
BK: tl.constexpr,
|
|
39
|
+
BV: tl.constexpr,
|
|
40
|
+
BT: tl.constexpr,
|
|
41
|
+
OUTPUT_ATTENTIONS: tl.constexpr
|
|
42
|
+
):
|
|
43
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
44
|
+
|
|
45
|
+
p_q = tl.make_block_ptr(q + i_bh * T*K, (T, K), (K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
46
|
+
p_k = tl.make_block_ptr(k + i_bh * T*K, (T, K), (K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
47
|
+
p_v = tl.make_block_ptr(v + i_bh * T*V, (T, V), (V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
|
|
48
|
+
b_q = (tl.load(p_q, boundary_check=(0, 1)) * scale).to(p_q.dtype.element_ty)
|
|
49
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
50
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
51
|
+
|
|
52
|
+
p_T = tl.make_block_ptr(A + i_bh * T * BT, (T, BT), (BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
|
|
53
|
+
b_T = tl.load(p_T, boundary_check=(0, 1))
|
|
54
|
+
|
|
55
|
+
o_i = tl.arange(0, BT)
|
|
56
|
+
m_t = o_i[:, None] >= o_i[None, :]
|
|
57
|
+
b_qk = tl.where(m_t, tl.dot(b_q, tl.trans(b_k), allow_tf32=False), 0).to(b_q.dtype)
|
|
58
|
+
m_t = o_i[:, None] > o_i[None, :]
|
|
59
|
+
b_kk = tl.where(m_t, tl.dot(b_k, tl.trans(b_k), allow_tf32=False), 0).to(b_k.dtype)
|
|
60
|
+
|
|
61
|
+
p_beta = tl.make_block_ptr(beta + i_bh * T, (T, ), (1, ), (i_t * BT, ), (BT, ), (0, ))
|
|
62
|
+
b_beta = tl.load(p_beta, boundary_check=(0, ))
|
|
63
|
+
b_k_beta = (b_k * b_beta[:, None]).to(b_k.dtype)
|
|
64
|
+
|
|
65
|
+
b_qkT = tl.dot(b_qk, b_T, allow_tf32=False).to(b_k.dtype)
|
|
66
|
+
|
|
67
|
+
if OUTPUT_ATTENTIONS:
|
|
68
|
+
p_a = tl.make_block_ptr(A_local + i_bh * T * BT, (T, BT), (BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
|
|
69
|
+
tl.store(p_a, b_qkT.to(p_a.dtype.element_ty), boundary_check=(0, 1))
|
|
70
|
+
|
|
71
|
+
b_kkT = tl.dot(b_kk, b_T, allow_tf32=False).to(b_k.dtype)
|
|
72
|
+
p_o = tl.make_block_ptr(o + i_bh * T*V, (T, V), (V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
|
|
73
|
+
tl.store(p_o, tl.dot(b_qkT, b_v).to(p_o.dtype.element_ty), boundary_check=(0, 1))
|
|
74
|
+
|
|
75
|
+
p_q_new = tl.make_block_ptr(q_new + i_bh * T*K, (T, K), (K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
76
|
+
tl.store(p_q_new, (b_q - tl.dot(b_qkT, b_k_beta, allow_tf32=False)).to(p_q_new.dtype.element_ty), boundary_check=(0, 1))
|
|
77
|
+
|
|
78
|
+
p_k_new = tl.make_block_ptr(k_new + i_bh * T*K, (T, K), (K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
79
|
+
b_k_new = b_k - tl.dot(tl.trans(b_kkT), b_k_beta, allow_tf32=False)
|
|
80
|
+
tl.store(p_k_new, b_k_new.to(p_k_new.dtype.element_ty), boundary_check=(0, 1))
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def chunk_transform_qk_fwd(
|
|
84
|
+
q: torch.Tensor,
|
|
85
|
+
k: torch.Tensor,
|
|
86
|
+
v: torch.Tensor,
|
|
87
|
+
beta: torch.Tensor,
|
|
88
|
+
A: torch.Tensor,
|
|
89
|
+
scale: float,
|
|
90
|
+
chunk_size: int,
|
|
91
|
+
output_attentions: bool
|
|
92
|
+
):
|
|
93
|
+
B, H, T, K = k.shape
|
|
94
|
+
BT = chunk_size
|
|
95
|
+
q_new = torch.empty_like(q)
|
|
96
|
+
k_new = torch.empty_like(k)
|
|
97
|
+
o = torch.empty_like(v)
|
|
98
|
+
grid = (triton.cdiv(T, BT), B*H)
|
|
99
|
+
V = v.shape[-1]
|
|
100
|
+
A_local = torch.empty_like(A) if output_attentions else None
|
|
101
|
+
chunk_transform_qk_fwd_kernel[grid](
|
|
102
|
+
q,
|
|
103
|
+
k,
|
|
104
|
+
v,
|
|
105
|
+
beta,
|
|
106
|
+
o,
|
|
107
|
+
A,
|
|
108
|
+
q_new,
|
|
109
|
+
k_new,
|
|
110
|
+
A_local,
|
|
111
|
+
scale=scale,
|
|
112
|
+
T=T,
|
|
113
|
+
K=K,
|
|
114
|
+
V=V,
|
|
115
|
+
BT=BT,
|
|
116
|
+
BK=triton.next_power_of_2(K),
|
|
117
|
+
BV=triton.next_power_of_2(V),
|
|
118
|
+
OUTPUT_ATTENTIONS=output_attentions
|
|
119
|
+
)
|
|
120
|
+
return q_new, k_new, o, A_local
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@triton.autotune(
|
|
124
|
+
configs=[
|
|
125
|
+
triton.Config({}, num_warps=1),
|
|
126
|
+
triton.Config({}, num_warps=2),
|
|
127
|
+
],
|
|
128
|
+
key=['BT'],
|
|
129
|
+
)
|
|
130
|
+
@triton.jit(do_not_specialize=['T'])
|
|
131
|
+
def save_intra_chunk_attn(
|
|
132
|
+
A,
|
|
133
|
+
A_local,
|
|
134
|
+
T,
|
|
135
|
+
BT: tl.constexpr,
|
|
136
|
+
):
|
|
137
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
138
|
+
p_A = tl.make_block_ptr(A + i_bh * T * T, (T, T), (T, 1), (i_t * BT, i_t * BT), (BT, BT), (1, 0))
|
|
139
|
+
p_A_local = tl.make_block_ptr(A_local + i_bh * T * BT, (T, BT), (BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
|
|
140
|
+
b_A_local = tl.load(p_A_local, boundary_check=(0, 1))
|
|
141
|
+
tl.store(p_A, b_A_local.to(p_A.dtype.element_ty), boundary_check=(0, 1))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@triton.heuristics({
|
|
145
|
+
'OUTPUT_ATTENTIONS': lambda args: args['attn'] is not None
|
|
146
|
+
})
|
|
147
|
+
@triton.jit(do_not_specialize=['T'])
|
|
148
|
+
def parallel_delta_rule_fwd_kernel(
|
|
149
|
+
q,
|
|
150
|
+
k,
|
|
151
|
+
k2, # original k
|
|
152
|
+
v,
|
|
153
|
+
beta,
|
|
154
|
+
o,
|
|
155
|
+
o_new,
|
|
156
|
+
attn,
|
|
157
|
+
T,
|
|
158
|
+
K: tl.constexpr,
|
|
159
|
+
V: tl.constexpr,
|
|
160
|
+
BT: tl.constexpr,
|
|
161
|
+
BS: tl.constexpr,
|
|
162
|
+
BK: tl.constexpr,
|
|
163
|
+
BV: tl.constexpr,
|
|
164
|
+
OUTPUT_ATTENTIONS: tl.constexpr
|
|
165
|
+
):
|
|
166
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
167
|
+
p_q = tl.make_block_ptr(q + i_bh * T*K, (T, K), (K, 1), (i_t * BT, 0), (BT, BK), (1, 0))
|
|
168
|
+
|
|
169
|
+
# the Q block is kept in the shared memory throughout the whole kernel
|
|
170
|
+
# [BT, BK]
|
|
171
|
+
b_q = tl.zeros([BT, BK], dtype=tl.float32)
|
|
172
|
+
b_q += tl.load(p_q, boundary_check=(0, 1))
|
|
173
|
+
|
|
174
|
+
b_o = tl.zeros([BT, BV], dtype=tl.float32)
|
|
175
|
+
p_o = tl.make_block_ptr(o + i_bh * T*V, (T, V), (V, 1), (i_t * BT, 0), (BT, BV), (1, 0))
|
|
176
|
+
b_o += tl.load(p_o, boundary_check=(0, 1))
|
|
177
|
+
|
|
178
|
+
# As opposed to Flashattention, this kernel requires scanning the KV blocks from right to left
|
|
179
|
+
# Q block and K block have overlap.
|
|
180
|
+
# masks required
|
|
181
|
+
for offset in range((i_t + 1) * BT - 2 * BS, i_t * BT - BS, -BS):
|
|
182
|
+
p_k = tl.make_block_ptr(k + i_bh * T*K, (K, T), (1, K), (0, offset), (BK, BS), (0, 1))
|
|
183
|
+
p_k2 = tl.make_block_ptr(k2 + i_bh * T*K, (T, K), (K, 1), (offset, 0), (BS, BK), (1, 0))
|
|
184
|
+
p_v = tl.make_block_ptr(v + i_bh * T*V, (T, V), (V, 1), (offset, 0), (BS, BV), (1, 0))
|
|
185
|
+
p_beta = tl.make_block_ptr(beta + i_bh * T, (T, ), (1, ), (offset, ), (BS, ), (0,))
|
|
186
|
+
# [BK, BS]
|
|
187
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
188
|
+
# [BS, BV]
|
|
189
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
190
|
+
# [BS]
|
|
191
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
192
|
+
# [BT, BS]
|
|
193
|
+
m_s = tl.arange(0, BT) >= (offset - i_t*BT + BS)
|
|
194
|
+
b_s = tl.dot(b_q.to(b_k.dtype), b_k, allow_tf32=False)
|
|
195
|
+
b_s = tl.where(m_s[:, None], b_s, 0)
|
|
196
|
+
|
|
197
|
+
b_o += tl.dot(b_s.to(b_v.dtype), b_v, allow_tf32=False)
|
|
198
|
+
b_k2 = (tl.load(p_k2, boundary_check=(0, 1)) * b_beta[:, None]).to(b_v.dtype)
|
|
199
|
+
b_q -= tl.dot(b_s.to(b_v.dtype), b_k2, allow_tf32=False)
|
|
200
|
+
|
|
201
|
+
if OUTPUT_ATTENTIONS:
|
|
202
|
+
p_a = tl.make_block_ptr(attn + i_bh * T * T, (T, T), (T, 1), (i_t * BT, offset), (BT, BS), (1, 0))
|
|
203
|
+
tl.store(p_a, b_s.to(p_a.dtype.element_ty), boundary_check=(0, 1))
|
|
204
|
+
|
|
205
|
+
# Q block and K block have no overlap
|
|
206
|
+
# no need for mask, thereby saving flops
|
|
207
|
+
for offset in range(i_t * BT - BS, -BS, -BS):
|
|
208
|
+
p_k = tl.make_block_ptr(k + i_bh * T*K, (K, T), (1, K), (0, offset), (BK, BS), (0, 1))
|
|
209
|
+
p_v = tl.make_block_ptr(v + i_bh * T*V, (T, V), (V, 1), (offset, 0), (BS, BV), (1, 0))
|
|
210
|
+
p_beta = tl.make_block_ptr(beta + i_bh * T, (T, ), (1, ), (offset, ), (BS, ), (0,))
|
|
211
|
+
p_k2 = tl.make_block_ptr(k2 + i_bh * T*K, (T, K), (K, 1), (offset, 0), (BS, BK), (1, 0))
|
|
212
|
+
|
|
213
|
+
# [BK, BS]
|
|
214
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
215
|
+
# [BS, BV]
|
|
216
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
217
|
+
# [BS]
|
|
218
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
219
|
+
# [BT, BS]
|
|
220
|
+
b_s = (tl.dot(b_q.to(b_k.dtype), b_k, allow_tf32=False))
|
|
221
|
+
# [BT, BV]
|
|
222
|
+
b_o += tl.dot(b_s.to(b_v.dtype), b_v, allow_tf32=False)
|
|
223
|
+
b_k2 = (tl.load(p_k2, boundary_check=(0, 1)) * b_beta[:, None]).to(b_v.dtype)
|
|
224
|
+
b_q -= tl.dot(b_s.to(b_v.dtype), b_k2, allow_tf32=False).to(b_q.dtype)
|
|
225
|
+
|
|
226
|
+
if OUTPUT_ATTENTIONS:
|
|
227
|
+
p_a = tl.make_block_ptr(attn + i_bh * T * T, (T, T), (T, 1), (i_t * BT, offset), (BT, BS), (1, 0))
|
|
228
|
+
tl.store(p_a, b_s.to(p_a.dtype.element_ty), boundary_check=(0, 1))
|
|
229
|
+
|
|
230
|
+
p_o_new = tl.make_block_ptr(o_new + i_bh * T*V, (T, V), (V, 1), (i_t*BT, 0), (BT, BV), (1, 0))
|
|
231
|
+
tl.store(p_o_new, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1))
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
class ParallelDeltaRuleFunction(torch.autograd.Function):
|
|
235
|
+
|
|
236
|
+
@staticmethod
|
|
237
|
+
@input_guard
|
|
238
|
+
@autocast_custom_fwd
|
|
239
|
+
def forward(ctx, q, k, v, beta, scale, output_attentions):
|
|
240
|
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
|
241
|
+
assert q.shape[-1] <= 128, 'The maximum supported sequence length is 128.'
|
|
242
|
+
BT, BS = 128, 32
|
|
243
|
+
BK = triton.next_power_of_2(k.shape[-1])
|
|
244
|
+
BV = triton.next_power_of_2(v.shape[-1])
|
|
245
|
+
assert BT % BS == 0
|
|
246
|
+
|
|
247
|
+
A = fwd_prepare_T(k, beta, BS)
|
|
248
|
+
attn = q.new_zeros(B, H, T, T) if output_attentions else None
|
|
249
|
+
q_new, k_new, o, A_local = chunk_transform_qk_fwd(
|
|
250
|
+
q,
|
|
251
|
+
k,
|
|
252
|
+
v,
|
|
253
|
+
beta,
|
|
254
|
+
A,
|
|
255
|
+
scale,
|
|
256
|
+
BS,
|
|
257
|
+
output_attentions
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
num_stages = 3 if K <= 64 else 2
|
|
261
|
+
num_warps = 4
|
|
262
|
+
grid = (triton.cdiv(T, BT), B * H)
|
|
263
|
+
o_new = torch.empty_like(o)
|
|
264
|
+
|
|
265
|
+
parallel_delta_rule_fwd_kernel[grid](
|
|
266
|
+
q=q_new,
|
|
267
|
+
k=k_new,
|
|
268
|
+
k2=k,
|
|
269
|
+
v=v,
|
|
270
|
+
beta=beta,
|
|
271
|
+
o=o,
|
|
272
|
+
o_new=o_new,
|
|
273
|
+
attn=attn,
|
|
274
|
+
T=T,
|
|
275
|
+
K=K,
|
|
276
|
+
V=V,
|
|
277
|
+
BT=BT,
|
|
278
|
+
BS=BS,
|
|
279
|
+
BK=BK,
|
|
280
|
+
BV=BV,
|
|
281
|
+
num_stages=num_stages,
|
|
282
|
+
num_warps=num_warps
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
if output_attentions:
|
|
286
|
+
grid = (triton.cdiv(T, BS), B * H)
|
|
287
|
+
save_intra_chunk_attn[grid](
|
|
288
|
+
A=attn,
|
|
289
|
+
A_local=A_local,
|
|
290
|
+
T=T,
|
|
291
|
+
BT=BS
|
|
292
|
+
)
|
|
293
|
+
return o_new.to(q.dtype), attn
|
|
294
|
+
|
|
295
|
+
@staticmethod
|
|
296
|
+
@input_guard
|
|
297
|
+
@autocast_custom_bwd
|
|
298
|
+
def backward(ctx, do, d_attn=None):
|
|
299
|
+
raise NotImplementedError('Backward pass is not implemented. Stay tuned!')
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def parallel_delta_rule(
|
|
303
|
+
q: torch.Tensor,
|
|
304
|
+
k: torch.Tensor,
|
|
305
|
+
v: torch.Tensor,
|
|
306
|
+
beta: torch.Tensor,
|
|
307
|
+
scale: float = None,
|
|
308
|
+
output_attentions: bool = False,
|
|
309
|
+
head_first: bool = False
|
|
310
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
311
|
+
r"""
|
|
312
|
+
Args:
|
|
313
|
+
q (torch.Tensor):
|
|
314
|
+
queries of shape `[B, T, H, K]`.
|
|
315
|
+
k (torch.Tensor):
|
|
316
|
+
keys of shape `[B, T, H, K]`.
|
|
317
|
+
v (torch.Tensor):
|
|
318
|
+
values of shape `[B, T, H, V]`.
|
|
319
|
+
beta (torch.Tensor):
|
|
320
|
+
betas of shape `[B, T, H]`.
|
|
321
|
+
scale (Optional[float]):
|
|
322
|
+
Scale factor for attention scores.
|
|
323
|
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
|
324
|
+
output_attentions (bool):
|
|
325
|
+
Whether to output the materialized attention scores of shape [B, H, T, T]. Default: `False`.
|
|
326
|
+
head_first (Optional[bool]):
|
|
327
|
+
Whether the inputs are in the head-first format. Default: `False`.
|
|
328
|
+
This argument has been deprecated.
|
|
329
|
+
|
|
330
|
+
Returns:
|
|
331
|
+
o (torch.Tensor):
|
|
332
|
+
Outputs of shape `[B, T, H, V]`.
|
|
333
|
+
attn (torch.Tensor):
|
|
334
|
+
Attention scores of shape `[B, H, T, T]` if `output_attentions=True` else `None`.
|
|
335
|
+
"""
|
|
336
|
+
if head_first:
|
|
337
|
+
raise DeprecationWarning(
|
|
338
|
+
"head_first is deprecated and will be removed in a future version. "
|
|
339
|
+
"Please use head_first=False for now instead."
|
|
340
|
+
)
|
|
341
|
+
if not head_first and q.shape[1] < q.shape[2]:
|
|
342
|
+
warnings.warn(
|
|
343
|
+
f"Input tensor shape suggests potential format mismatch: seq_len ({q.shape[1]}) < num_heads ({q.shape[2]}). "
|
|
344
|
+
"This may indicate the inputs were passed in head-first format [B, H, T, ...] "
|
|
345
|
+
"when head_first=False was specified. "
|
|
346
|
+
"Please verify your input tensor format matches the expected shape [B, T, H, ...]."
|
|
347
|
+
)
|
|
348
|
+
o, attn = ParallelDeltaRuleFunction.apply(q, k, v, beta, scale, output_attentions)
|
|
349
|
+
return o, attn
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def naive_delta_rule_parallel(q, k, v, beta, BM=128, BN=32):
|
|
353
|
+
b, h, l, d_k = q.shape
|
|
354
|
+
q = q * (d_k ** -0.5)
|
|
355
|
+
v = v * beta[..., None]
|
|
356
|
+
k_beta = k * beta[..., None]
|
|
357
|
+
# compute (I - tri(diag(beta) KK^T))^{-1}
|
|
358
|
+
q, k, v, k_beta = map(lambda x: rearrange(x, 'b h (n c) d -> b h n c d', c=BN), [q, k, v, k_beta])
|
|
359
|
+
mask = torch.triu(torch.ones(BN, BN, dtype=torch.bool, device=q.device), diagonal=0)
|
|
360
|
+
T = -(k_beta @ k.transpose(-1, -2)).masked_fill(mask, 0)
|
|
361
|
+
for i in range(1, BN):
|
|
362
|
+
T[..., i, :i] = T[..., i, :i].clone() + (T[..., i, :, None].clone() * T[..., :, :i].clone()).sum(-2)
|
|
363
|
+
T = T + torch.eye(BN, dtype=q.dtype, device=q.device)
|
|
364
|
+
|
|
365
|
+
mask2 = torch.triu(torch.ones(BN, BN, dtype=torch.bool, device=q.device), diagonal=1)
|
|
366
|
+
A_local = (q @ k.transpose(-1, -2)).masked_fill(mask2, 0) @ T
|
|
367
|
+
o_intra = A_local @ v
|
|
368
|
+
|
|
369
|
+
# apply cumprod transition matrices on k to the last position within the chunk
|
|
370
|
+
k = k - ((k @ k.transpose(-1, -2)).masked_fill(mask, 0) @ T).transpose(-1, -2) @ k_beta
|
|
371
|
+
# apply cumprod transition matrices on q to the first position within the chunk
|
|
372
|
+
q = q - A_local @ k_beta
|
|
373
|
+
o_intra = A_local @ v
|
|
374
|
+
|
|
375
|
+
A = torch.zeros(b, h, l, l, device=q.device)
|
|
376
|
+
|
|
377
|
+
q, k, v, k_beta, o_intra = map(lambda x: rearrange(x, 'b h n c d -> b h (n c) d'), [q, k, v, k_beta, o_intra])
|
|
378
|
+
o = torch.empty_like(v)
|
|
379
|
+
for i in range(0, l, BM):
|
|
380
|
+
q_i = q[:, :, i:i+BM]
|
|
381
|
+
o_i = o_intra[:, :, i:i+BM]
|
|
382
|
+
# intra block
|
|
383
|
+
for j in range(i + BM - 2 * BN, i-BN, -BN):
|
|
384
|
+
k_j = k[:, :, j:j+BN]
|
|
385
|
+
A_ij = q_i @ k_j.transpose(-1, -2)
|
|
386
|
+
mask = torch.arange(i, i+BM) >= (j + BN)
|
|
387
|
+
A_ij = A_ij.masked_fill_(~mask[:, None].to(A_ij.device), 0)
|
|
388
|
+
A[:, :, i:i+BM, j:j+BN] = A_ij
|
|
389
|
+
q_i = q_i - A_ij @ k_beta[:, :, j:j+BN]
|
|
390
|
+
o_i += A_ij @ v[:, :, j:j+BN]
|
|
391
|
+
# inter block
|
|
392
|
+
for j in range(i - BN, -BN, -BN):
|
|
393
|
+
k_j = k[:, :, j:j+BN]
|
|
394
|
+
A_ij = q_i @ k_j.transpose(-1, -2)
|
|
395
|
+
A[:, :, i:i+BM, j:j+BN] = A_ij
|
|
396
|
+
q_i = q_i - A_ij @ k_beta[:, :, j:j+BN]
|
|
397
|
+
o_i += A_ij @ v[:, :, j:j+BN]
|
|
398
|
+
o[:, :, i:i+BM] = o_i
|
|
399
|
+
|
|
400
|
+
for i in range(0, l//BN):
|
|
401
|
+
A[:, :, i*BN:i*BN+BN, i*BN:i*BN+BN] = A_local[:, :, i]
|
|
402
|
+
|
|
403
|
+
return o, A
|
|
@@ -0,0 +1,294 @@
|
|
|
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.common.chunk_scaled_dot_kkt import chunk_scaled_dot_kkt_fwd
|
|
11
|
+
from fla.ops.utils import prepare_chunk_indices
|
|
12
|
+
from fla.ops.utils.solve_tril import solve_tril
|
|
13
|
+
from fla.utils import check_shared_mem, is_nvidia_hopper
|
|
14
|
+
|
|
15
|
+
NUM_WARPS = [2, 4] if is_nvidia_hopper else [2, 4, 8]
|
|
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({}, num_warps=num_warps, num_stages=num_stages)
|
|
24
|
+
for num_warps in [2, 4, 8]
|
|
25
|
+
for num_stages in [2, 3, 4]
|
|
26
|
+
],
|
|
27
|
+
key=['H', 'K', 'V', 'BT', 'BK', 'BV', 'IS_VARLEN'],
|
|
28
|
+
)
|
|
29
|
+
@triton.jit(do_not_specialize=['T'])
|
|
30
|
+
def recompute_w_u_fwd_kernel(
|
|
31
|
+
k,
|
|
32
|
+
v,
|
|
33
|
+
beta,
|
|
34
|
+
w,
|
|
35
|
+
u,
|
|
36
|
+
A,
|
|
37
|
+
cu_seqlens,
|
|
38
|
+
chunk_indices,
|
|
39
|
+
T,
|
|
40
|
+
H: tl.constexpr,
|
|
41
|
+
K: tl.constexpr,
|
|
42
|
+
V: tl.constexpr,
|
|
43
|
+
BT: tl.constexpr,
|
|
44
|
+
BK: tl.constexpr,
|
|
45
|
+
BV: tl.constexpr,
|
|
46
|
+
IS_VARLEN: tl.constexpr
|
|
47
|
+
):
|
|
48
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
49
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
50
|
+
if IS_VARLEN:
|
|
51
|
+
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)
|
|
52
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
53
|
+
T = eos - bos
|
|
54
|
+
else:
|
|
55
|
+
bos, eos = i_b * T, i_b * T + T
|
|
56
|
+
|
|
57
|
+
p_beta = tl.make_block_ptr(beta + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
58
|
+
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))
|
|
59
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
60
|
+
b_A = tl.load(p_A, boundary_check=(0, 1))
|
|
61
|
+
|
|
62
|
+
for i_v in range(tl.cdiv(V, BV)):
|
|
63
|
+
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))
|
|
64
|
+
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))
|
|
65
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
66
|
+
b_vb = (b_v * b_beta[:, None]).to(b_v.dtype)
|
|
67
|
+
b_u = tl.dot(b_A.to(b_vb.dtype), b_vb, allow_tf32=False)
|
|
68
|
+
tl.store(p_u, (b_u).to(p_u.dtype.element_ty), boundary_check=(0, 1))
|
|
69
|
+
|
|
70
|
+
for i_k in range(tl.cdiv(K, BK)):
|
|
71
|
+
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))
|
|
72
|
+
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))
|
|
73
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
74
|
+
b_kb = (b_k * b_beta[:, None]).to(b_k.dtype)
|
|
75
|
+
b_w = tl.dot(b_A.to(b_kb.dtype), b_kb, allow_tf32=False)
|
|
76
|
+
tl.store(p_w, b_w.to(p_w.dtype.element_ty), boundary_check=(0, 1))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@triton.heuristics({
|
|
80
|
+
'IS_VARLEN': lambda args: args['cu_seqlens'] is not None
|
|
81
|
+
})
|
|
82
|
+
@triton.autotune(
|
|
83
|
+
configs=[
|
|
84
|
+
triton.Config({}, num_warps=num_warps, num_stages=num_stages)
|
|
85
|
+
for num_warps in NUM_WARPS
|
|
86
|
+
for num_stages in [2, 3, 4]
|
|
87
|
+
],
|
|
88
|
+
key=['H', 'K', 'V', 'BT', 'BK', 'BV', 'IS_VARLEN'],
|
|
89
|
+
)
|
|
90
|
+
@triton.jit(do_not_specialize=['T'])
|
|
91
|
+
def prepare_wy_repr_bwd_kernel(
|
|
92
|
+
k,
|
|
93
|
+
v,
|
|
94
|
+
beta,
|
|
95
|
+
A,
|
|
96
|
+
dw,
|
|
97
|
+
du,
|
|
98
|
+
dk,
|
|
99
|
+
dv,
|
|
100
|
+
dbeta,
|
|
101
|
+
cu_seqlens,
|
|
102
|
+
chunk_indices,
|
|
103
|
+
T,
|
|
104
|
+
H: tl.constexpr,
|
|
105
|
+
K: tl.constexpr,
|
|
106
|
+
V: tl.constexpr,
|
|
107
|
+
BT: tl.constexpr,
|
|
108
|
+
BK: tl.constexpr,
|
|
109
|
+
BV: tl.constexpr,
|
|
110
|
+
IS_VARLEN: tl.constexpr
|
|
111
|
+
):
|
|
112
|
+
i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
113
|
+
i_b, i_h = i_bh // H, i_bh % H
|
|
114
|
+
if IS_VARLEN:
|
|
115
|
+
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)
|
|
116
|
+
bos, eos = tl.load(cu_seqlens + i_n).to(tl.int32), tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
117
|
+
T = eos - bos
|
|
118
|
+
else:
|
|
119
|
+
bos, eos = i_b * T, i_b * T + T
|
|
120
|
+
|
|
121
|
+
p_beta = tl.make_block_ptr(beta + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
122
|
+
p_A = tl.make_block_ptr(A + (bos*H + i_h) * BT, (BT, T), (1, H*BT), (0, i_t * BT), (BT, BT), (0, 1))
|
|
123
|
+
|
|
124
|
+
b_beta = tl.load(p_beta, boundary_check=(0,))
|
|
125
|
+
b_A = tl.load(p_A, boundary_check=(0, 1))
|
|
126
|
+
|
|
127
|
+
b_dbeta = tl.zeros([BT], dtype=tl.float32)
|
|
128
|
+
b_dA = tl.zeros([BT, BT], dtype=tl.float32)
|
|
129
|
+
for i_v in range(tl.cdiv(V, BV)):
|
|
130
|
+
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))
|
|
131
|
+
p_dv = tl.make_block_ptr(dv + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
|
132
|
+
p_du = tl.make_block_ptr(du + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
|
133
|
+
|
|
134
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
135
|
+
b_v_beta = (b_v * b_beta[:, None]).to(b_v.dtype)
|
|
136
|
+
b_du = tl.load(p_du, boundary_check=(0, 1))
|
|
137
|
+
b_dA += tl.dot(b_du, tl.trans(b_v_beta), allow_tf32=False)
|
|
138
|
+
b_dv_beta = tl.dot(b_A, b_du, allow_tf32=False)
|
|
139
|
+
b_dv = b_dv_beta * b_beta[:, None]
|
|
140
|
+
b_dbeta += tl.sum(b_dv_beta * b_v, 1)
|
|
141
|
+
|
|
142
|
+
tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
|
|
143
|
+
|
|
144
|
+
for i_k in range(tl.cdiv(K, BK)):
|
|
145
|
+
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))
|
|
146
|
+
p_dk = tl.make_block_ptr(dk + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
147
|
+
p_dw = tl.make_block_ptr(dw + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
148
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
149
|
+
b_k_beta = (b_k * b_beta[:, None]).to(b_k.dtype)
|
|
150
|
+
b_dw = tl.load(p_dw, boundary_check=(0, 1))
|
|
151
|
+
b_dA += tl.dot(b_dw, tl.trans(b_k_beta), allow_tf32=False)
|
|
152
|
+
b_dk_beta = tl.dot(b_A, b_dw, allow_tf32=False)
|
|
153
|
+
b_dk = b_dk_beta * b_beta[:, None]
|
|
154
|
+
b_dbeta += tl.sum(b_dk_beta * b_k, 1)
|
|
155
|
+
|
|
156
|
+
tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
|
|
157
|
+
|
|
158
|
+
b_dA = tl.where(tl.arange(0, BT)[:, None] > tl.arange(0, BT)[None, :], b_dA, 0)
|
|
159
|
+
b_dA = tl.dot(b_dA.to(b_A.dtype), b_A)
|
|
160
|
+
b_dA = tl.dot(b_A, b_dA.to(b_A.dtype))
|
|
161
|
+
b_dA = tl.where(tl.arange(0, BT)[:, None] > tl.arange(0, BT)[None, :], -b_dA, 0).to(k.dtype.element_ty)
|
|
162
|
+
|
|
163
|
+
for i_k in range(tl.cdiv(K, BK)):
|
|
164
|
+
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))
|
|
165
|
+
p_dk = tl.make_block_ptr(dk + (bos*H + i_h) * K, (T, K), (H*K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
|
|
166
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
167
|
+
b_dk = tl.load(p_dk, boundary_check=(0, 1))
|
|
168
|
+
b_k_beta = (b_k * b_beta[:, None]).to(b_k.dtype)
|
|
169
|
+
|
|
170
|
+
b_dk_beta = tl.dot(b_dA, b_k, allow_tf32=False)
|
|
171
|
+
b_dbeta += tl.sum(b_dk_beta * b_k, 1)
|
|
172
|
+
b_dk += tl.dot(tl.trans(b_dA), b_k_beta, allow_tf32=False)
|
|
173
|
+
b_dk += b_dk_beta * b_beta[:, None]
|
|
174
|
+
tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
|
|
175
|
+
|
|
176
|
+
p_dbeta = tl.make_block_ptr(dbeta + bos*H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
177
|
+
tl.store(p_dbeta, b_dbeta.to(p_dbeta.dtype.element_ty), boundary_check=(0,))
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def prepare_wy_repr_fwd(
|
|
181
|
+
k: torch.Tensor,
|
|
182
|
+
v: torch.Tensor,
|
|
183
|
+
beta: torch.Tensor,
|
|
184
|
+
cu_seqlens: Optional[torch.LongTensor],
|
|
185
|
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
186
|
+
A = chunk_scaled_dot_kkt_fwd(
|
|
187
|
+
k=k,
|
|
188
|
+
beta=beta,
|
|
189
|
+
cu_seqlens=cu_seqlens,
|
|
190
|
+
chunk_size=64,
|
|
191
|
+
output_dtype=torch.float32,
|
|
192
|
+
)
|
|
193
|
+
A = solve_tril(
|
|
194
|
+
A=A,
|
|
195
|
+
cu_seqlens=cu_seqlens,
|
|
196
|
+
output_dtype=k.dtype
|
|
197
|
+
)
|
|
198
|
+
w, u = recompute_w_u_fwd(
|
|
199
|
+
k=k,
|
|
200
|
+
v=v,
|
|
201
|
+
beta=beta,
|
|
202
|
+
A=A,
|
|
203
|
+
cu_seqlens=cu_seqlens,
|
|
204
|
+
)
|
|
205
|
+
return w, u, A
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def recompute_w_u_fwd(
|
|
209
|
+
k: torch.Tensor,
|
|
210
|
+
v: torch.Tensor,
|
|
211
|
+
beta: torch.Tensor,
|
|
212
|
+
A: torch.Tensor,
|
|
213
|
+
cu_seqlens: Optional[torch.LongTensor],
|
|
214
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
215
|
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
|
216
|
+
BT = 64
|
|
217
|
+
CONST_TILING = 64 if check_shared_mem() else 32
|
|
218
|
+
BK = min(max(triton.next_power_of_2(K), 16), CONST_TILING)
|
|
219
|
+
BV = min(max(triton.next_power_of_2(V), 16), CONST_TILING)
|
|
220
|
+
|
|
221
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
222
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
223
|
+
|
|
224
|
+
u = torch.empty_like(v)
|
|
225
|
+
w = torch.empty_like(k)
|
|
226
|
+
recompute_w_u_fwd_kernel[(NT, B*H)](
|
|
227
|
+
k,
|
|
228
|
+
v,
|
|
229
|
+
beta,
|
|
230
|
+
w,
|
|
231
|
+
u,
|
|
232
|
+
A,
|
|
233
|
+
cu_seqlens=cu_seqlens,
|
|
234
|
+
chunk_indices=chunk_indices,
|
|
235
|
+
T=T,
|
|
236
|
+
H=H,
|
|
237
|
+
K=K,
|
|
238
|
+
V=V,
|
|
239
|
+
BT=BT,
|
|
240
|
+
BK=BK,
|
|
241
|
+
BV=BV,
|
|
242
|
+
)
|
|
243
|
+
return w, u
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def prepare_wy_repr_bwd(
|
|
247
|
+
k: torch.Tensor,
|
|
248
|
+
v: torch.Tensor,
|
|
249
|
+
beta: torch.Tensor,
|
|
250
|
+
A: torch.Tensor,
|
|
251
|
+
dw: torch.Tensor,
|
|
252
|
+
du: torch.Tensor,
|
|
253
|
+
cu_seqlens: Optional[torch.LongTensor]
|
|
254
|
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
255
|
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
|
256
|
+
BT = A.shape[-1]
|
|
257
|
+
CONST_TILING = 64 if check_shared_mem() else 32
|
|
258
|
+
BK = min(max(triton.next_power_of_2(K), 16), CONST_TILING)
|
|
259
|
+
BV = min(max(triton.next_power_of_2(V), 16), CONST_TILING)
|
|
260
|
+
|
|
261
|
+
chunk_indices = prepare_chunk_indices(cu_seqlens, BT) if cu_seqlens is not None else None
|
|
262
|
+
NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
263
|
+
|
|
264
|
+
dk = torch.empty_like(k)
|
|
265
|
+
dv = torch.empty_like(v)
|
|
266
|
+
dbeta = torch.empty_like(beta)
|
|
267
|
+
prepare_wy_repr_bwd_kernel[(NT, B * H)](
|
|
268
|
+
k,
|
|
269
|
+
v,
|
|
270
|
+
beta,
|
|
271
|
+
A,
|
|
272
|
+
dw,
|
|
273
|
+
du,
|
|
274
|
+
dk,
|
|
275
|
+
dv,
|
|
276
|
+
dbeta,
|
|
277
|
+
cu_seqlens=cu_seqlens,
|
|
278
|
+
chunk_indices=chunk_indices,
|
|
279
|
+
T=T,
|
|
280
|
+
H=H,
|
|
281
|
+
K=K,
|
|
282
|
+
V=V,
|
|
283
|
+
BT=BT,
|
|
284
|
+
BK=BK,
|
|
285
|
+
BV=BV,
|
|
286
|
+
)
|
|
287
|
+
return dk, dv, dbeta
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
fwd_prepare_wy_repr = prepare_wy_repr_fwd
|
|
291
|
+
|
|
292
|
+
bwd_prepare_wy_repr = prepare_wy_repr_bwd
|
|
293
|
+
|
|
294
|
+
fwd_recompute_w_u = recompute_w_u_fwd
|