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,293 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
from typing import Optional
|
|
7
|
+
|
|
8
|
+
import torch
|
|
9
|
+
import triton
|
|
10
|
+
import triton.language as tl
|
|
11
|
+
from packaging.version import Version
|
|
12
|
+
|
|
13
|
+
from fla.utils import check_pytorch_version, input_guard, is_amd, use_cuda_graph
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
if not check_pytorch_version('2.4'):
|
|
18
|
+
logger.warning('PyTorch < 2.4 detected - computations may be slower due to lack of optimizations')
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def identity_decorator(fn):
|
|
22
|
+
return fn
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
current_python_version = Version(f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")
|
|
26
|
+
min_torch_compile_version = Version("3.11")
|
|
27
|
+
fla_use_compile = os.getenv('FLA_USE_COMPILE', '1').lower() in ('1', 'true', 'yes')
|
|
28
|
+
|
|
29
|
+
if current_python_version >= min_torch_compile_version and fla_use_compile:
|
|
30
|
+
torch_compile = torch.compile(fullgraph=True)
|
|
31
|
+
else:
|
|
32
|
+
logger.warning('torch.compile is not available in Python 3.10, using identity decorator instead')
|
|
33
|
+
torch_compile = identity_decorator
|
|
34
|
+
|
|
35
|
+
NUM_WARPS_AUTOTUNE = [2, 4, 8, 16] if is_amd else [2, 4, 8, 16, 32]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@triton.autotune(
|
|
39
|
+
configs=[
|
|
40
|
+
triton.Config({'BT': BT}, num_warps=num_warps, num_stages=num_stages)
|
|
41
|
+
for num_warps in NUM_WARPS_AUTOTUNE
|
|
42
|
+
for num_stages in [1, 2, 3]
|
|
43
|
+
for BT in [2, 4, 8]
|
|
44
|
+
],
|
|
45
|
+
key=['BD'],
|
|
46
|
+
use_cuda_graph=use_cuda_graph,
|
|
47
|
+
)
|
|
48
|
+
@triton.jit
|
|
49
|
+
def fused_addcmul_fwd_kernel(
|
|
50
|
+
hidden,
|
|
51
|
+
delta,
|
|
52
|
+
ixr, ixw, ixk, ixv, ixa, ixg,
|
|
53
|
+
oxr, oxw, oxk, oxv, oxa, oxg,
|
|
54
|
+
use_xg: tl.constexpr,
|
|
55
|
+
T,
|
|
56
|
+
T_OFFSET,
|
|
57
|
+
BT: tl.constexpr,
|
|
58
|
+
D: tl.constexpr,
|
|
59
|
+
BD: tl.constexpr,
|
|
60
|
+
):
|
|
61
|
+
i_b, i_t = tl.program_id(0), tl.program_id(1) * BT
|
|
62
|
+
|
|
63
|
+
bos = i_b * (T + T_OFFSET)
|
|
64
|
+
t_vec = i_t + T_OFFSET + tl.arange(0, BT)
|
|
65
|
+
mask_t = t_vec < (T + T_OFFSET)
|
|
66
|
+
o_d = tl.arange(0, BD)[None, :]
|
|
67
|
+
off_vec = (bos + t_vec)[:, None] * D + o_d
|
|
68
|
+
m_d = o_d < D
|
|
69
|
+
mask = mask_t[:, None] & m_d
|
|
70
|
+
|
|
71
|
+
b_h = tl.load(hidden + off_vec, mask=mask, other=0.)
|
|
72
|
+
b_x = tl.load(delta + off_vec, mask=mask, other=0.)
|
|
73
|
+
b_r = tl.load(ixr + o_d, mask=m_d)
|
|
74
|
+
b_w = tl.load(ixw + o_d, mask=m_d)
|
|
75
|
+
b_k = tl.load(ixk + o_d, mask=m_d)
|
|
76
|
+
b_v = tl.load(ixv + o_d, mask=m_d)
|
|
77
|
+
b_a = tl.load(ixa + o_d, mask=m_d)
|
|
78
|
+
|
|
79
|
+
o_r = tl.fma(b_x, b_r, b_h)
|
|
80
|
+
o_w = tl.fma(b_x, b_w, b_h)
|
|
81
|
+
o_k = tl.fma(b_x, b_k, b_h)
|
|
82
|
+
o_v = tl.fma(b_x, b_v, b_h)
|
|
83
|
+
o_a = tl.fma(b_x, b_a, b_h)
|
|
84
|
+
|
|
85
|
+
tl.store(oxr + off_vec, o_r.to(oxr.dtype.element_ty), mask=mask)
|
|
86
|
+
tl.store(oxw + off_vec, o_w.to(oxw.dtype.element_ty), mask=mask)
|
|
87
|
+
tl.store(oxk + off_vec, o_k.to(oxk.dtype.element_ty), mask=mask)
|
|
88
|
+
tl.store(oxv + off_vec, o_v.to(oxv.dtype.element_ty), mask=mask)
|
|
89
|
+
tl.store(oxa + off_vec, o_a.to(oxa.dtype.element_ty), mask=mask)
|
|
90
|
+
|
|
91
|
+
if use_xg:
|
|
92
|
+
b_g = tl.load(ixg + o_d, mask=m_d)
|
|
93
|
+
o_g = tl.fma(b_x, b_g, b_h)
|
|
94
|
+
tl.store(oxg + off_vec, o_g.to(oxg.dtype.element_ty), mask=mask)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@triton.autotune(
|
|
98
|
+
configs=[
|
|
99
|
+
triton.Config({'BT': BT}, num_warps=num_warps, num_stages=num_stages)
|
|
100
|
+
for num_warps in NUM_WARPS_AUTOTUNE
|
|
101
|
+
for num_stages in [1, 2, 3]
|
|
102
|
+
for BT in [2, 4, 8]
|
|
103
|
+
],
|
|
104
|
+
key=['BD'],
|
|
105
|
+
use_cuda_graph=use_cuda_graph,
|
|
106
|
+
)
|
|
107
|
+
@triton.jit
|
|
108
|
+
def addcmul_bwd_kernel1(
|
|
109
|
+
ixr,
|
|
110
|
+
ixw,
|
|
111
|
+
ixk,
|
|
112
|
+
ixv,
|
|
113
|
+
ixa,
|
|
114
|
+
ixg,
|
|
115
|
+
dxr,
|
|
116
|
+
dxw,
|
|
117
|
+
dxk,
|
|
118
|
+
dxv,
|
|
119
|
+
dxa,
|
|
120
|
+
dxg,
|
|
121
|
+
ghidden,
|
|
122
|
+
gx,
|
|
123
|
+
use_xg: tl.constexpr,
|
|
124
|
+
T,
|
|
125
|
+
T_OFFSET,
|
|
126
|
+
BT: tl.constexpr,
|
|
127
|
+
D: tl.constexpr,
|
|
128
|
+
BD: tl.constexpr,
|
|
129
|
+
DTYPE: tl.constexpr,
|
|
130
|
+
):
|
|
131
|
+
i_b, i_t = tl.program_id(0), tl.program_id(1)
|
|
132
|
+
|
|
133
|
+
t_idx = T_OFFSET + i_t * BT + tl.arange(0, BT)[:, None]
|
|
134
|
+
mask_t = t_idx < (T + T_OFFSET)
|
|
135
|
+
|
|
136
|
+
d_idx = tl.arange(0, BD)[None, :]
|
|
137
|
+
mask_d = d_idx < D
|
|
138
|
+
mask = mask_t & mask_d
|
|
139
|
+
|
|
140
|
+
offset_base = i_b * (T + T_OFFSET) * D
|
|
141
|
+
x_idx = (offset_base + t_idx * D + d_idx).to(tl.uint32)
|
|
142
|
+
|
|
143
|
+
b_dxr = tl.load(dxr + x_idx, mask=mask).to(DTYPE)
|
|
144
|
+
b_dxw = tl.load(dxw + x_idx, mask=mask).to(DTYPE)
|
|
145
|
+
b_dxk = tl.load(dxk + x_idx, mask=mask).to(DTYPE)
|
|
146
|
+
b_dxv = tl.load(dxv + x_idx, mask=mask).to(DTYPE)
|
|
147
|
+
b_dxa = tl.load(dxa + x_idx, mask=mask).to(DTYPE)
|
|
148
|
+
|
|
149
|
+
b_ixr = tl.load(ixr + d_idx, mask=mask_d).to(DTYPE)
|
|
150
|
+
b_ixw = tl.load(ixw + d_idx, mask=mask_d).to(DTYPE)
|
|
151
|
+
b_ixk = tl.load(ixk + d_idx, mask=mask_d).to(DTYPE)
|
|
152
|
+
b_ixv = tl.load(ixv + d_idx, mask=mask_d).to(DTYPE)
|
|
153
|
+
b_ixa = tl.load(ixa + d_idx, mask=mask_d).to(DTYPE)
|
|
154
|
+
|
|
155
|
+
g_hidden = b_dxr + b_dxw + b_dxk + b_dxv + b_dxa
|
|
156
|
+
g_x = b_dxr * b_ixr + b_dxw * b_ixw + b_dxk * b_ixk + b_dxv * b_ixv + b_dxa * b_ixa
|
|
157
|
+
|
|
158
|
+
if use_xg:
|
|
159
|
+
b_dxg = tl.load(dxg + x_idx, mask=mask).to(DTYPE)
|
|
160
|
+
b_ixg = tl.load(ixg + d_idx, mask=mask_d).to(DTYPE)
|
|
161
|
+
g_hidden += b_dxg
|
|
162
|
+
g_x += b_dxg * b_ixg
|
|
163
|
+
|
|
164
|
+
tl.store(ghidden + x_idx, g_hidden.to(ghidden.dtype.element_ty), mask=mask)
|
|
165
|
+
tl.store(gx + x_idx, g_x.to(gx.dtype.element_ty), mask=mask)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def addcmul_bwd1(d_xr, d_xw, d_xk, d_xv, d_xa, d_xg,
|
|
169
|
+
x_r, x_w, x_k, x_v, x_a, x_g, hidden_states, delta, use_xg, inplace=True):
|
|
170
|
+
B, T, D = hidden_states.size()
|
|
171
|
+
g_hiddn = hidden_states if inplace else torch.empty_like(hidden_states)
|
|
172
|
+
g_delta = torch.empty_like(delta)
|
|
173
|
+
for t in range(0, T, 65536):
|
|
174
|
+
T_OFFSET = t
|
|
175
|
+
T_SIZE = min(65536, T - t)
|
|
176
|
+
def grid(meta): return (B, triton.cdiv(T_SIZE, meta['BT']))
|
|
177
|
+
addcmul_bwd_kernel1[grid](
|
|
178
|
+
ixr=x_r,
|
|
179
|
+
ixw=x_w,
|
|
180
|
+
ixk=x_k,
|
|
181
|
+
ixv=x_v,
|
|
182
|
+
ixa=x_a,
|
|
183
|
+
ixg=x_g,
|
|
184
|
+
dxr=d_xr,
|
|
185
|
+
dxw=d_xw,
|
|
186
|
+
dxk=d_xk,
|
|
187
|
+
dxv=d_xv,
|
|
188
|
+
dxa=d_xa,
|
|
189
|
+
dxg=d_xg,
|
|
190
|
+
ghidden=g_hiddn,
|
|
191
|
+
gx=g_delta,
|
|
192
|
+
use_xg=use_xg,
|
|
193
|
+
T=T_SIZE,
|
|
194
|
+
T_OFFSET=T_OFFSET,
|
|
195
|
+
D=D,
|
|
196
|
+
BD=triton.next_power_of_2(D),
|
|
197
|
+
DTYPE=tl.float16 if hidden_states.dtype == torch.float16 else tl.float32,
|
|
198
|
+
)
|
|
199
|
+
return g_hiddn, g_delta
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
@torch_compile
|
|
203
|
+
def addcmul_bwd2(d_oxr, d_xw, d_xk, d_xv, d_xa, d_xg, delta, use_xg: bool):
|
|
204
|
+
g_xr = (d_oxr * delta).sum(dim=(0, 1), keepdim=True, dtype=torch.float32)
|
|
205
|
+
g_xw = (d_xw * delta).sum(dim=(0, 1), keepdim=True, dtype=torch.float32)
|
|
206
|
+
g_xk = (d_xk * delta).sum(dim=(0, 1), keepdim=True, dtype=torch.float32)
|
|
207
|
+
g_xv = (d_xv * delta).sum(dim=(0, 1), keepdim=True, dtype=torch.float32)
|
|
208
|
+
g_xa = (d_xa * delta).sum(dim=(0, 1), keepdim=True, dtype=torch.float32)
|
|
209
|
+
g_xg = (d_xg * delta).sum(dim=(0, 1), keepdim=True, dtype=torch.float32) if use_xg else None
|
|
210
|
+
return g_xr, g_xw, g_xk, g_xv, g_xa, g_xg
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class Rwkv7FusedAddcmul(torch.autograd.Function):
|
|
214
|
+
@staticmethod
|
|
215
|
+
@input_guard
|
|
216
|
+
def forward(
|
|
217
|
+
ctx, hidden_states, delta,
|
|
218
|
+
x_r, x_w, x_k, x_v, x_a, x_g,
|
|
219
|
+
):
|
|
220
|
+
B, T, D = hidden_states.size()
|
|
221
|
+
oxr = torch.empty_like(hidden_states)
|
|
222
|
+
oxw = torch.empty_like(hidden_states)
|
|
223
|
+
oxk = torch.empty_like(hidden_states)
|
|
224
|
+
oxv = torch.empty_like(hidden_states)
|
|
225
|
+
oxa = torch.empty_like(hidden_states)
|
|
226
|
+
if x_g is not None:
|
|
227
|
+
use_xg = True
|
|
228
|
+
oxg = torch.empty_like(hidden_states)
|
|
229
|
+
else:
|
|
230
|
+
use_xg = False
|
|
231
|
+
oxg = None
|
|
232
|
+
|
|
233
|
+
for t in range(0, T, 65536):
|
|
234
|
+
T_OFFSET = t
|
|
235
|
+
T_SIZE = min(65536, T - t)
|
|
236
|
+
def grid(meta): return (B, triton.cdiv(T_SIZE, meta['BT']))
|
|
237
|
+
fused_addcmul_fwd_kernel[grid](
|
|
238
|
+
hidden_states, delta,
|
|
239
|
+
x_r, x_w, x_k, x_v, x_a, x_g,
|
|
240
|
+
oxr, oxw, oxk, oxv, oxa, oxg,
|
|
241
|
+
use_xg,
|
|
242
|
+
T=T_SIZE,
|
|
243
|
+
T_OFFSET=T_OFFSET,
|
|
244
|
+
D=D,
|
|
245
|
+
BD=triton.next_power_of_2(D),
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
ctx.save_for_backward(hidden_states, delta,
|
|
249
|
+
x_r, x_w, x_k, x_v, x_a, x_g)
|
|
250
|
+
ctx.use_xg = use_xg
|
|
251
|
+
return oxr, oxw, oxk, oxv, oxa, oxg
|
|
252
|
+
|
|
253
|
+
@staticmethod
|
|
254
|
+
@input_guard
|
|
255
|
+
def backward(ctx, dxr,
|
|
256
|
+
dxw, dxk, dxv, dxa, dxg):
|
|
257
|
+
hidden_states, delta, x_r, x_w, x_k, x_v, x_a, x_g = ctx.saved_tensors
|
|
258
|
+
|
|
259
|
+
d_hiddn, d_xx = addcmul_bwd1(dxr, dxw, dxk, dxv, dxa, dxg, x_r, x_w, x_k, x_v, x_a, x_g,
|
|
260
|
+
hidden_states, delta, ctx.use_xg)
|
|
261
|
+
|
|
262
|
+
d_ixr, d_ixw, d_ixk, d_ixv, d_ixa, d_ixg = addcmul_bwd2(dxr, dxw, dxk, dxv, dxa, dxg, delta, ctx.use_xg)
|
|
263
|
+
|
|
264
|
+
return d_hiddn, d_xx, d_ixr, d_ixw, d_ixk, d_ixv, d_ixa, d_ixg
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def fused_addcmul_rwkv7(
|
|
268
|
+
hidden_states: torch.Tensor,
|
|
269
|
+
delta: torch.Tensor,
|
|
270
|
+
xr: torch.Tensor,
|
|
271
|
+
xw: torch.Tensor,
|
|
272
|
+
xk: torch.Tensor,
|
|
273
|
+
xv: torch.Tensor,
|
|
274
|
+
xa: torch.Tensor,
|
|
275
|
+
xg: Optional[torch.Tensor] = None
|
|
276
|
+
):
|
|
277
|
+
if hidden_states.shape[1] == 1:
|
|
278
|
+
# Special case for decode
|
|
279
|
+
return torch_addcmul_rwkv7(hidden_states, delta, xr, xw, xk, xv, xa, xg)
|
|
280
|
+
return Rwkv7FusedAddcmul.apply(hidden_states, delta, xr, xw, xk, xv, xa, xg)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def torch_addcmul_rwkv7(hidden_states, delta, xr, xw, xk, xv, xa, xg=None):
|
|
284
|
+
oxr = torch.addcmul(hidden_states, delta, xr)
|
|
285
|
+
oxw = torch.addcmul(hidden_states, delta, xw)
|
|
286
|
+
oxk = torch.addcmul(hidden_states, delta, xk)
|
|
287
|
+
oxv = torch.addcmul(hidden_states, delta, xv)
|
|
288
|
+
oxa = torch.addcmul(hidden_states, delta, xa)
|
|
289
|
+
if xg is not None:
|
|
290
|
+
oxg = torch.addcmul(hidden_states, delta, xg)
|
|
291
|
+
return oxr, oxw, oxk, oxv, oxa, oxg
|
|
292
|
+
else:
|
|
293
|
+
return oxr, oxw, oxk, oxv, oxa, None
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
import triton
|
|
7
|
+
import triton.language as tl
|
|
8
|
+
|
|
9
|
+
from fla.ops.utils import prepare_chunk_indices
|
|
10
|
+
from fla.utils import get_multiprocessor_count, input_guard, is_amd
|
|
11
|
+
|
|
12
|
+
NUM_WARPS_AUTOTUNE = [2, 4, 8, 16] if is_amd else [2, 4, 8, 16, 32]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@torch.jit.script
|
|
16
|
+
def k_update_ref(k: torch.Tensor, a: torch.Tensor, ka: torch.Tensor) -> torch.Tensor:
|
|
17
|
+
return k.addcmul(k * (a - 1), ka)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@triton.heuristics({'IS_VARLEN': lambda args: args['cu_seqlens'] is not None})
|
|
21
|
+
@triton.autotune(
|
|
22
|
+
configs=[
|
|
23
|
+
triton.Config({}, num_warps=w, num_stages=s)
|
|
24
|
+
for w in NUM_WARPS_AUTOTUNE
|
|
25
|
+
for s in [1, 2, 3]
|
|
26
|
+
],
|
|
27
|
+
key=['BD']
|
|
28
|
+
)
|
|
29
|
+
@triton.jit
|
|
30
|
+
def k_update_fwd_kernel_short(
|
|
31
|
+
k, a, ka, out,
|
|
32
|
+
cu_seqlens,
|
|
33
|
+
T, D,
|
|
34
|
+
BD: tl.constexpr,
|
|
35
|
+
IS_VARLEN: tl.constexpr,
|
|
36
|
+
):
|
|
37
|
+
i_b, i_t = tl.program_id(0), tl.program_id(1)
|
|
38
|
+
|
|
39
|
+
if IS_VARLEN:
|
|
40
|
+
bos = tl.load(cu_seqlens + i_b).to(tl.int32)
|
|
41
|
+
eos = tl.load(cu_seqlens + i_b + 1).to(tl.int32)
|
|
42
|
+
g_t = bos + i_t
|
|
43
|
+
if g_t >= eos:
|
|
44
|
+
return
|
|
45
|
+
offset = g_t * D
|
|
46
|
+
else:
|
|
47
|
+
g_t = i_t
|
|
48
|
+
offset = i_b * T * D + g_t * D
|
|
49
|
+
|
|
50
|
+
o_d = tl.arange(0, BD)
|
|
51
|
+
m_d = o_d < D
|
|
52
|
+
off = offset + o_d
|
|
53
|
+
|
|
54
|
+
b_k = tl.load(k + off, mask=m_d, other=0.).to(tl.float32)
|
|
55
|
+
b_a = tl.load(a + off, mask=m_d, other=0.).to(tl.float32)
|
|
56
|
+
b_ka = tl.load(ka + o_d, mask=m_d, eviction_policy='evict_last').to(tl.float32)
|
|
57
|
+
|
|
58
|
+
out_val = b_k * (1 + (b_a - 1) * b_ka)
|
|
59
|
+
tl.store(out + off, out_val.to(out.dtype.element_ty), mask=m_d)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@triton.heuristics({'IS_VARLEN': lambda args: args['cu_seqlens'] is not None})
|
|
63
|
+
@triton.autotune(
|
|
64
|
+
configs=[
|
|
65
|
+
triton.Config({}, num_warps=w, num_stages=s)
|
|
66
|
+
for w in NUM_WARPS_AUTOTUNE
|
|
67
|
+
for s in [1, 2, 3]
|
|
68
|
+
],
|
|
69
|
+
key=['BD', 'BT']
|
|
70
|
+
)
|
|
71
|
+
@triton.jit
|
|
72
|
+
def k_update_fwd_kernel_long(
|
|
73
|
+
k, a, ka, out,
|
|
74
|
+
cu_seqlens, chunk_indices,
|
|
75
|
+
T, D,
|
|
76
|
+
BD: tl.constexpr, BT: tl.constexpr,
|
|
77
|
+
IS_VARLEN: tl.constexpr,
|
|
78
|
+
):
|
|
79
|
+
i_d, i_t_blk, i_b = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
80
|
+
|
|
81
|
+
if IS_VARLEN:
|
|
82
|
+
i_n, i_t_blk = tl.load(chunk_indices + i_t_blk * 2).to(tl.int32), \
|
|
83
|
+
tl.load(chunk_indices + i_t_blk * 2 + 1).to(tl.int32)
|
|
84
|
+
bos = tl.load(cu_seqlens + i_n).to(tl.int32)
|
|
85
|
+
eos = tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
86
|
+
t_start = i_t_blk * BT
|
|
87
|
+
t_end = tl.minimum(t_start + BT, eos - bos)
|
|
88
|
+
else:
|
|
89
|
+
bos = i_b * T
|
|
90
|
+
eos = (i_b + 1) * T
|
|
91
|
+
t_start = i_t_blk * BT
|
|
92
|
+
t_end = tl.minimum(t_start + BT, T)
|
|
93
|
+
|
|
94
|
+
o_d = i_d * BD + tl.arange(0, BD)
|
|
95
|
+
m_d = o_d < D
|
|
96
|
+
|
|
97
|
+
for t in range(t_start, t_end):
|
|
98
|
+
global_t = bos + t
|
|
99
|
+
off = global_t * D + o_d
|
|
100
|
+
b_k = tl.load(k + off, mask=m_d, other=0.).to(tl.float32)
|
|
101
|
+
b_a = tl.load(a + off, mask=m_d, other=0.).to(tl.float32)
|
|
102
|
+
b_ka = tl.load(ka + o_d, mask=m_d, eviction_policy='evict_last').to(tl.float32)
|
|
103
|
+
out_val = b_k * (1 + (b_a - 1) * b_ka)
|
|
104
|
+
tl.store(out + off, out_val.to(out.dtype.element_ty), mask=m_d)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@triton.heuristics({'IS_VARLEN': lambda args: args['cu_seqlens'] is not None})
|
|
108
|
+
@triton.autotune(
|
|
109
|
+
configs=[
|
|
110
|
+
triton.Config({'BT': BT}, num_warps=w, num_stages=s)
|
|
111
|
+
for w in NUM_WARPS_AUTOTUNE
|
|
112
|
+
for s in [1, 2, 3]
|
|
113
|
+
for BT in [2, 4, 8]
|
|
114
|
+
],
|
|
115
|
+
key=['BD']
|
|
116
|
+
)
|
|
117
|
+
@triton.jit
|
|
118
|
+
def k_update_bwd_kernel_short(
|
|
119
|
+
grad_out, k, a, ka,
|
|
120
|
+
dk, da, dka,
|
|
121
|
+
cu_seqlens,
|
|
122
|
+
T, D,
|
|
123
|
+
BT: tl.constexpr,
|
|
124
|
+
BD: tl.constexpr,
|
|
125
|
+
IS_VARLEN: tl.constexpr,
|
|
126
|
+
):
|
|
127
|
+
i_b, i_t_base = tl.program_id(0), tl.program_id(1) * BT
|
|
128
|
+
|
|
129
|
+
if IS_VARLEN:
|
|
130
|
+
bos = tl.load(cu_seqlens + i_b).to(tl.int32)
|
|
131
|
+
eos = tl.load(cu_seqlens + i_b + 1).to(tl.int32)
|
|
132
|
+
seq_len = eos - bos
|
|
133
|
+
else:
|
|
134
|
+
bos = i_b * T
|
|
135
|
+
eos = (i_b + 1) * T
|
|
136
|
+
seq_len = T
|
|
137
|
+
|
|
138
|
+
t_vec = i_t_base + tl.arange(0, BT)
|
|
139
|
+
mask_t = t_vec < seq_len
|
|
140
|
+
global_t_vec = bos + t_vec
|
|
141
|
+
|
|
142
|
+
o_d = tl.arange(0, BD)[None, :]
|
|
143
|
+
m_d = o_d < D
|
|
144
|
+
off = global_t_vec[:, None] * D + o_d
|
|
145
|
+
|
|
146
|
+
b_go = tl.load(grad_out + off, mask=mask_t[:, None] & m_d, other=0.).to(tl.float32)
|
|
147
|
+
b_k = tl.load(k + off, mask=mask_t[:, None] & m_d, other=0.).to(tl.float32)
|
|
148
|
+
b_a = tl.load(a + off, mask=mask_t[:, None] & m_d, other=0.).to(tl.float32)
|
|
149
|
+
b_ka = tl.load(ka + o_d, mask=m_d, eviction_policy='evict_last').to(tl.float32) # [1, BD]
|
|
150
|
+
|
|
151
|
+
dk_vec = b_go * (1 + (b_a - 1) * b_ka)
|
|
152
|
+
da_vec = b_go * b_k * b_ka
|
|
153
|
+
dka_vec = b_go * b_k * (b_a - 1)
|
|
154
|
+
tl.store(dk + off, dk_vec.to(dk.dtype.element_ty), mask=mask_t[:, None] & m_d)
|
|
155
|
+
tl.store(da + off, da_vec.to(da.dtype.element_ty), mask=mask_t[:, None] & m_d)
|
|
156
|
+
tl.store(dka + off, dka_vec.to(dka.dtype.element_ty), mask=mask_t[:, None] & m_d)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@triton.heuristics({'IS_VARLEN': lambda args: args['cu_seqlens'] is not None})
|
|
160
|
+
@triton.autotune(
|
|
161
|
+
configs=[
|
|
162
|
+
triton.Config({}, num_warps=w, num_stages=s)
|
|
163
|
+
for w in NUM_WARPS_AUTOTUNE
|
|
164
|
+
for s in [1, 2, 3]
|
|
165
|
+
],
|
|
166
|
+
key=['BD', 'BT']
|
|
167
|
+
)
|
|
168
|
+
@triton.jit
|
|
169
|
+
def k_update_bwd_kernel_long(
|
|
170
|
+
grad_out, k, a, ka,
|
|
171
|
+
dk, da, dka,
|
|
172
|
+
cu_seqlens, chunk_indices,
|
|
173
|
+
T, D,
|
|
174
|
+
BD: tl.constexpr, BT: tl.constexpr,
|
|
175
|
+
IS_VARLEN: tl.constexpr,
|
|
176
|
+
):
|
|
177
|
+
i_d, i_t_blk, i_b = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
178
|
+
|
|
179
|
+
if IS_VARLEN:
|
|
180
|
+
i_n, i_t_blk = tl.load(chunk_indices + i_t_blk * 2).to(tl.int32), \
|
|
181
|
+
tl.load(chunk_indices + i_t_blk * 2 + 1).to(tl.int32)
|
|
182
|
+
bos = tl.load(cu_seqlens + i_n).to(tl.int32)
|
|
183
|
+
eos = tl.load(cu_seqlens + i_n + 1).to(tl.int32)
|
|
184
|
+
t_start = i_t_blk * BT
|
|
185
|
+
t_end = tl.minimum(t_start + BT, eos - bos)
|
|
186
|
+
else:
|
|
187
|
+
bos = i_b * T
|
|
188
|
+
eos = (i_b + 1) * T
|
|
189
|
+
t_start = i_t_blk * BT
|
|
190
|
+
t_end = tl.minimum(t_start + BT, T)
|
|
191
|
+
|
|
192
|
+
o_d = i_d * BD + tl.arange(0, BD)
|
|
193
|
+
m_d = o_d < D
|
|
194
|
+
|
|
195
|
+
for t in range(t_start, t_end):
|
|
196
|
+
global_t = bos + t
|
|
197
|
+
off = global_t * D + o_d
|
|
198
|
+
|
|
199
|
+
b_go = tl.load(grad_out + off, mask=m_d, other=0.).to(tl.float32)
|
|
200
|
+
b_k = tl.load(k + off, mask=m_d, other=0.).to(tl.float32)
|
|
201
|
+
b_a = tl.load(a + off, mask=m_d, other=0.).to(tl.float32)
|
|
202
|
+
b_ka = tl.load(ka + o_d, mask=m_d, eviction_policy='evict_last').to(tl.float32)
|
|
203
|
+
|
|
204
|
+
tl.store(dk + off, (b_go * (1 + (b_a - 1) * b_ka)).to(dk.dtype.element_ty), mask=m_d)
|
|
205
|
+
tl.store(da + off, (b_go * b_k * b_ka).to(da.dtype.element_ty), mask=m_d)
|
|
206
|
+
tl.store(dka + off, (b_go * b_k * (b_a - 1)).to(dka.dtype.element_ty), mask=m_d)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def k_update_fwd(
|
|
210
|
+
k: torch.Tensor,
|
|
211
|
+
a: torch.Tensor,
|
|
212
|
+
ka: torch.Tensor,
|
|
213
|
+
cu_seqlens: Optional[torch.Tensor] = None,
|
|
214
|
+
) -> torch.Tensor:
|
|
215
|
+
B, T, D = k.shape
|
|
216
|
+
out = torch.empty_like(k)
|
|
217
|
+
use_short = T <= 512
|
|
218
|
+
|
|
219
|
+
if use_short:
|
|
220
|
+
if cu_seqlens is not None:
|
|
221
|
+
N = len(cu_seqlens) - 1
|
|
222
|
+
else:
|
|
223
|
+
N = B
|
|
224
|
+
BD = triton.next_power_of_2(D)
|
|
225
|
+
grid = (N, T)
|
|
226
|
+
k_update_fwd_kernel_short[grid](
|
|
227
|
+
k, a, ka, out,
|
|
228
|
+
cu_seqlens,
|
|
229
|
+
T, D,
|
|
230
|
+
BD=BD,
|
|
231
|
+
)
|
|
232
|
+
else:
|
|
233
|
+
BT = min(64, triton.next_power_of_2(
|
|
234
|
+
triton.cdiv(max(16, B * T), get_multiprocessor_count(k.device.index))
|
|
235
|
+
))
|
|
236
|
+
if cu_seqlens is not None:
|
|
237
|
+
chunk_idx = prepare_chunk_indices(cu_seqlens, BT)
|
|
238
|
+
NT = len(chunk_idx)
|
|
239
|
+
N = len(cu_seqlens) - 1
|
|
240
|
+
else:
|
|
241
|
+
chunk_idx = None
|
|
242
|
+
NT = triton.cdiv(T, BT)
|
|
243
|
+
N = B
|
|
244
|
+
|
|
245
|
+
BD = triton.next_power_of_2(D)
|
|
246
|
+
|
|
247
|
+
def grid(meta):
|
|
248
|
+
return (triton.cdiv(D, meta['BD']), NT, N)
|
|
249
|
+
|
|
250
|
+
k_update_fwd_kernel_long[grid](
|
|
251
|
+
k, a, ka, out,
|
|
252
|
+
cu_seqlens, chunk_idx,
|
|
253
|
+
T, D,
|
|
254
|
+
BD=BD, BT=BT,
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
return out, use_short, N, T
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def k_update_bwd(
|
|
261
|
+
grad_out: torch.Tensor,
|
|
262
|
+
k: torch.Tensor,
|
|
263
|
+
a: torch.Tensor,
|
|
264
|
+
ka: torch.Tensor,
|
|
265
|
+
cu_seqlens: Optional[torch.Tensor],
|
|
266
|
+
use_short: bool,
|
|
267
|
+
N: int,
|
|
268
|
+
T: int,
|
|
269
|
+
):
|
|
270
|
+
B, _, D = grad_out.shape
|
|
271
|
+
dk = torch.empty_like(k)
|
|
272
|
+
da = torch.empty_like(a)
|
|
273
|
+
dka_tmp = torch.empty_like(k, dtype=torch.float32)
|
|
274
|
+
|
|
275
|
+
if use_short:
|
|
276
|
+
BD = triton.next_power_of_2(D)
|
|
277
|
+
def grid(meta): return (N, triton.cdiv(T, meta['BT']))
|
|
278
|
+
k_update_bwd_kernel_short[grid](
|
|
279
|
+
grad_out, k, a, ka,
|
|
280
|
+
dk, da, dka_tmp,
|
|
281
|
+
cu_seqlens,
|
|
282
|
+
T, D,
|
|
283
|
+
BD=BD,
|
|
284
|
+
)
|
|
285
|
+
else:
|
|
286
|
+
BT = min(64, triton.next_power_of_2(
|
|
287
|
+
triton.cdiv(max(16, B * T), get_multiprocessor_count(grad_out.device.index))
|
|
288
|
+
))
|
|
289
|
+
if cu_seqlens is not None:
|
|
290
|
+
chunk_idx = prepare_chunk_indices(cu_seqlens, BT)
|
|
291
|
+
NT = len(chunk_idx)
|
|
292
|
+
else:
|
|
293
|
+
chunk_idx = None
|
|
294
|
+
NT = triton.cdiv(T, BT)
|
|
295
|
+
|
|
296
|
+
BD = triton.next_power_of_2(D)
|
|
297
|
+
|
|
298
|
+
def grid(meta):
|
|
299
|
+
return (triton.cdiv(D, meta['BD']), NT, N)
|
|
300
|
+
|
|
301
|
+
k_update_bwd_kernel_long[grid](
|
|
302
|
+
grad_out, k, a, ka,
|
|
303
|
+
dk, da, dka_tmp,
|
|
304
|
+
cu_seqlens, chunk_idx,
|
|
305
|
+
T, D,
|
|
306
|
+
BD=BD, BT=BT,
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
if dka_tmp.dim() == 3:
|
|
310
|
+
dka = dka_tmp.sum(dim=(0, 1), keepdim=True).type_as(ka)
|
|
311
|
+
else:
|
|
312
|
+
dka = dka_tmp.sum(dim=(0, 1)).type_as(ka)
|
|
313
|
+
|
|
314
|
+
return dk, da, dka
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
class KUpdateFunction(torch.autograd.Function):
|
|
318
|
+
@staticmethod
|
|
319
|
+
@input_guard
|
|
320
|
+
def forward(ctx, k, a, ka, cu_seqlens=None):
|
|
321
|
+
out, use_short, N, T = k_update_fwd(k, a, ka, cu_seqlens)
|
|
322
|
+
ctx.save_for_backward(k, a, ka)
|
|
323
|
+
ctx.use_short = use_short
|
|
324
|
+
ctx.N = N
|
|
325
|
+
ctx.T = T
|
|
326
|
+
ctx.cu_seqlens = cu_seqlens
|
|
327
|
+
return out
|
|
328
|
+
|
|
329
|
+
@staticmethod
|
|
330
|
+
@input_guard
|
|
331
|
+
def backward(ctx, grad_output):
|
|
332
|
+
k, a, ka = ctx.saved_tensors
|
|
333
|
+
dk, da, dka = k_update_bwd(
|
|
334
|
+
grad_output, k, a, ka,
|
|
335
|
+
ctx.cu_seqlens,
|
|
336
|
+
ctx.use_short,
|
|
337
|
+
ctx.N,
|
|
338
|
+
ctx.T,
|
|
339
|
+
)
|
|
340
|
+
return dk, da, dka, None
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def fused_k_rwkv7(k, a, ka, cu_seqlens=None):
|
|
344
|
+
if k.shape[1] == 1:
|
|
345
|
+
return k_update_ref(k, a, ka)
|
|
346
|
+
return KUpdateFunction.apply(k, a, ka, cu_seqlens)
|