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,464 @@
|
|
|
1
|
+
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
import triton
|
|
7
|
+
import triton.language as tl
|
|
8
|
+
|
|
9
|
+
from fla.utils import autocast_custom_bwd, autocast_custom_fwd, input_guard
|
|
10
|
+
|
|
11
|
+
# Rebased: Linear Transformers with Learnable Kernel Functions are Better In-Context Models
|
|
12
|
+
# https://github.com/corl-team/rebased/blob/main/flash_linear_attention/fla/ops/triton/rebased_fast/parallel.py
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@triton.jit(do_not_specialize=['T'])
|
|
16
|
+
def parallel_rebased_fwd_kernel(
|
|
17
|
+
q,
|
|
18
|
+
k,
|
|
19
|
+
v,
|
|
20
|
+
o,
|
|
21
|
+
z,
|
|
22
|
+
scale,
|
|
23
|
+
T,
|
|
24
|
+
B: tl.constexpr,
|
|
25
|
+
H: tl.constexpr,
|
|
26
|
+
K: tl.constexpr,
|
|
27
|
+
V: tl.constexpr,
|
|
28
|
+
BTL: tl.constexpr,
|
|
29
|
+
BTS: tl.constexpr,
|
|
30
|
+
BK: tl.constexpr,
|
|
31
|
+
BV: tl.constexpr,
|
|
32
|
+
):
|
|
33
|
+
# i_c: chunk index. used for sequence parallelism
|
|
34
|
+
i_kv, i_c, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
35
|
+
NV = tl.cdiv(V, BV)
|
|
36
|
+
i_k = i_kv // (NV)
|
|
37
|
+
i_v = i_kv % (NV)
|
|
38
|
+
|
|
39
|
+
p_q = tl.make_block_ptr(q + i_bh * T*K, (T, K), (K, 1), (i_c*BTL, i_k*BK), (BTL, BK), (1, 0))
|
|
40
|
+
p_k = tl.make_block_ptr(k + i_bh * T*K, (K, T), (1, K), (i_k*BK, 0), (BK, BTS), (0, 1))
|
|
41
|
+
p_v = tl.make_block_ptr(v + i_bh * T*V, (T, V), (V, 1), (0, i_v*BV), (BTS, BV), (1, 0))
|
|
42
|
+
|
|
43
|
+
# [BQ, BD] block Q, in the shared memory throughout the whole kernel
|
|
44
|
+
b_q = tl.load(p_q, boundary_check=(0, 1))
|
|
45
|
+
b_q = (b_q * scale).to(b_q.dtype)
|
|
46
|
+
b_o = tl.zeros([BTL, BV], dtype=tl.float32)
|
|
47
|
+
b_z = tl.zeros([BTL], dtype=tl.float32)
|
|
48
|
+
|
|
49
|
+
# Q block and K block have no overlap
|
|
50
|
+
# no need for mask, thereby saving flops
|
|
51
|
+
for _ in range(0, i_c*BTL, BTS):
|
|
52
|
+
# [BK, BTS]
|
|
53
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
54
|
+
|
|
55
|
+
# [BTS, BV]
|
|
56
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
57
|
+
# [BTL, BTS]
|
|
58
|
+
b_s = tl.dot(b_q, (b_k), allow_tf32=False)
|
|
59
|
+
b_s = b_s * b_s
|
|
60
|
+
b_z += tl.sum(b_s, axis=1)
|
|
61
|
+
|
|
62
|
+
# [BQ, BD]
|
|
63
|
+
b_o = b_o + tl.dot(b_s.to(b_v.dtype), b_v, allow_tf32=False)
|
|
64
|
+
p_k = tl.advance(p_k, (0, BTS))
|
|
65
|
+
p_v = tl.advance(p_v, (BTS, 0))
|
|
66
|
+
|
|
67
|
+
# # rescale interchunk output
|
|
68
|
+
tl.debug_barrier()
|
|
69
|
+
o_q = tl.arange(0, BTL)
|
|
70
|
+
# # sync threads, easy for compiler to optimize
|
|
71
|
+
# tl.debug_barrier()
|
|
72
|
+
|
|
73
|
+
o_k = tl.arange(0, BTS)
|
|
74
|
+
p_k = tl.make_block_ptr(k + i_bh * T*K, (K, T), (1, K), (i_k*BK, i_c*BTL), (BK, BTS), (0, 1))
|
|
75
|
+
p_v = tl.make_block_ptr(v + i_bh * T*V, (T, V), (V, 1), (i_c*BTL, i_v*BV), (BTS, BV), (1, 0))
|
|
76
|
+
# Q block and K block have overlap. masks required
|
|
77
|
+
for _ in range(i_c*BTL, (i_c + 1) * BTL, BTS):
|
|
78
|
+
# [BK, BTS]
|
|
79
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
80
|
+
# [BTS, BV]
|
|
81
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
82
|
+
# [BTL, BTS]
|
|
83
|
+
m_s = o_q[:, None] >= o_k[None, :]
|
|
84
|
+
b_s = tl.dot(b_q, b_k, allow_tf32=False)
|
|
85
|
+
b_s = b_s * b_s
|
|
86
|
+
b_s = tl.where(m_s, b_s, 0)
|
|
87
|
+
b_z += tl.sum(b_s, axis=1)
|
|
88
|
+
# [BTL, BV]
|
|
89
|
+
b_o += tl.dot(b_s.to(b_q.dtype), b_v, allow_tf32=False)
|
|
90
|
+
p_k = tl.advance(p_k, (0, BTS))
|
|
91
|
+
p_v = tl.advance(p_v, (BTS, 0))
|
|
92
|
+
o_k += BTS
|
|
93
|
+
|
|
94
|
+
p_o = tl.make_block_ptr(o + (i_bh + B * H * i_k) * T*V, (T, V), (V, 1), (i_c*BTL, i_v*BV), (BTL, BV), (1, 0))
|
|
95
|
+
p_z = z + (i_bh + B * H * i_k) * T + i_c*BTL + tl.arange(0, BTL)
|
|
96
|
+
tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1))
|
|
97
|
+
tl.store(p_z, b_z.to(p_z.dtype.element_ty), mask=((i_c*BTL + tl.arange(0, BTL)) < T))
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@triton.jit(do_not_specialize=['T'])
|
|
101
|
+
def _parallel_rebased_bwd_dq(
|
|
102
|
+
i_bh,
|
|
103
|
+
i_c,
|
|
104
|
+
i_k,
|
|
105
|
+
i_v,
|
|
106
|
+
i_h,
|
|
107
|
+
q,
|
|
108
|
+
k,
|
|
109
|
+
v,
|
|
110
|
+
do,
|
|
111
|
+
dz,
|
|
112
|
+
dq,
|
|
113
|
+
scale,
|
|
114
|
+
T,
|
|
115
|
+
B: tl.constexpr,
|
|
116
|
+
H: tl.constexpr,
|
|
117
|
+
K: tl.constexpr,
|
|
118
|
+
V: tl.constexpr,
|
|
119
|
+
BTL: tl.constexpr,
|
|
120
|
+
BTS: tl.constexpr,
|
|
121
|
+
BK: tl.constexpr,
|
|
122
|
+
BV: tl.constexpr
|
|
123
|
+
):
|
|
124
|
+
p_do = tl.make_block_ptr(do + i_bh * T*V, (T, V), (V, 1), (i_c*BTL, i_v*BV), (BTL, BV), (1, 0))
|
|
125
|
+
p_q = tl.make_block_ptr(q + (i_bh) * T*K, (T, K), (K, 1), (i_c*BTL, i_k*BK), (BTL, BK), (1, 0))
|
|
126
|
+
b_q = tl.load(p_q, boundary_check=(0, 1))
|
|
127
|
+
b_do = tl.load(p_do, boundary_check=(0, 1)).to(b_q.dtype)
|
|
128
|
+
b_q = (b_q * scale).to(b_q.dtype)
|
|
129
|
+
b_dq = tl.zeros([BTL, BK], dtype=tl.float32)
|
|
130
|
+
p_k = tl.make_block_ptr(k + i_bh * T*K, (T, K), (K, 1), (0, i_k*BK), (BTS, BK), (1, 0))
|
|
131
|
+
p_v = tl.make_block_ptr(v + i_bh * T*V, (V, T), (1, V), (i_v*BV, 0), (BV, BTS), (0, 1))
|
|
132
|
+
p_dz = dz + i_bh * T + i_c*BTL + tl.arange(0, BTL)
|
|
133
|
+
b_dz = tl.load(p_dz, mask=(i_c*BTL + tl.arange(0, BTL)) < T)
|
|
134
|
+
|
|
135
|
+
for _ in range(0, i_c*BTL, BTS):
|
|
136
|
+
# [BTS, BK]
|
|
137
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
138
|
+
# [BV, BTS]
|
|
139
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
140
|
+
# [BTL, BTS]
|
|
141
|
+
b_ds = tl.dot(b_do, b_v, allow_tf32=False)
|
|
142
|
+
if i_v == 0:
|
|
143
|
+
b_ds += b_dz[:, None]
|
|
144
|
+
else:
|
|
145
|
+
b_ds = b_ds
|
|
146
|
+
b_s = tl.dot(b_q, tl.trans(b_k), allow_tf32=False)
|
|
147
|
+
# [BQ, BD]
|
|
148
|
+
b_dq += tl.dot((2 * b_ds * b_s).to(b_v.dtype), b_k, allow_tf32=False)
|
|
149
|
+
p_k = tl.advance(p_k, (BTS, 0))
|
|
150
|
+
p_v = tl.advance(p_v, (0, BTS))
|
|
151
|
+
|
|
152
|
+
b_dq *= scale
|
|
153
|
+
o_q = tl.arange(0, BTL)
|
|
154
|
+
o_k = tl.arange(0, BTS)
|
|
155
|
+
p_k = tl.make_block_ptr(k + i_bh * T*K, (T, K), (K, 1), (i_c*BTL, i_k*BK), (BTS, BK), (1, 0))
|
|
156
|
+
p_v = tl.make_block_ptr(v + i_bh * T*V, (V, T), (1, V), (i_v*BV, i_c*BTL), (BV, BTS), (0, 1))
|
|
157
|
+
# Q block and K block have overlap. masks required
|
|
158
|
+
for _ in range(i_c*BTL, (i_c + 1) * BTL, BTS):
|
|
159
|
+
# [BTS, BK]
|
|
160
|
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
161
|
+
# [BV, BTS]
|
|
162
|
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
163
|
+
# [BTL, BTS]
|
|
164
|
+
m_s = o_q[:, None] >= o_k[None, :]
|
|
165
|
+
b_ds = tl.dot(b_do, b_v, allow_tf32=False)
|
|
166
|
+
if i_v == 0:
|
|
167
|
+
b_ds += b_dz[:, None]
|
|
168
|
+
else:
|
|
169
|
+
b_ds = b_ds
|
|
170
|
+
b_ds = tl.where(m_s, b_ds, 0) * scale
|
|
171
|
+
b_s = tl.dot(b_q, tl.trans(b_k), allow_tf32=False)
|
|
172
|
+
b_s = tl.where(m_s, b_s, 0)
|
|
173
|
+
# [BTL, BK]
|
|
174
|
+
b_dq += tl.dot((2 * b_ds * b_s).to(b_k.dtype),
|
|
175
|
+
b_k, allow_tf32=False)
|
|
176
|
+
p_k = tl.advance(p_k, (BTS, 0))
|
|
177
|
+
p_v = tl.advance(p_v, (0, BTS))
|
|
178
|
+
o_k += BTS
|
|
179
|
+
p_dq = tl.make_block_ptr(dq + (i_bh + B * H * i_v) * T*K, (T, K), (K, 1), (i_c*BTL, i_k*BK), (BTL, BK), (1, 0))
|
|
180
|
+
tl.store(p_dq, b_dq.to(p_dq.dtype.element_ty), boundary_check=(0, 1))
|
|
181
|
+
return
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
@triton.jit(do_not_specialize=['T'])
|
|
185
|
+
def _parallel_rebased_bwd_dkv(
|
|
186
|
+
i_bh,
|
|
187
|
+
i_c,
|
|
188
|
+
i_k,
|
|
189
|
+
i_v,
|
|
190
|
+
i_h,
|
|
191
|
+
q,
|
|
192
|
+
k,
|
|
193
|
+
v,
|
|
194
|
+
do,
|
|
195
|
+
dz,
|
|
196
|
+
dk,
|
|
197
|
+
dv,
|
|
198
|
+
scale,
|
|
199
|
+
T,
|
|
200
|
+
B: tl.constexpr,
|
|
201
|
+
H: tl.constexpr,
|
|
202
|
+
K: tl.constexpr,
|
|
203
|
+
V: tl.constexpr,
|
|
204
|
+
BTL: tl.constexpr,
|
|
205
|
+
BTS: tl.constexpr,
|
|
206
|
+
BK: tl.constexpr,
|
|
207
|
+
BV: tl.constexpr,
|
|
208
|
+
):
|
|
209
|
+
# compute dk dv
|
|
210
|
+
p_k = tl.make_block_ptr(k + i_bh * T*K, (T, K), (K, 1), (i_c*BTL, i_k*BK), (BTL, BK), (1, 0))
|
|
211
|
+
p_v = tl.make_block_ptr(v + i_bh * T*V, (T, V), (V, 1), (i_c*BTL, i_v*BV), (BTL, BV), (1, 0))
|
|
212
|
+
b_k, b_v = tl.load(p_k, boundary_check=(0, 1)), tl.load(p_v, boundary_check=(0, 1))
|
|
213
|
+
b_dk, b_dv = tl.zeros([BTL, BK], dtype=tl.float32), tl.zeros(
|
|
214
|
+
[BTL, BV], dtype=tl.float32)
|
|
215
|
+
|
|
216
|
+
for i in range((tl.cdiv(T, BTS) * BTS)-BTS, (i_c + 1) * BTL - BTS, -BTS):
|
|
217
|
+
p_q = tl.make_block_ptr(q + i_bh * T*K, (K, T), (1, K), (i_k*BK, i), (BK, BTS), (0, 1))
|
|
218
|
+
p_do = tl.make_block_ptr(do + i_bh * T*V, (V, T), (1, V), (i_v*BV, i), (BV, BTS), (0, 1))
|
|
219
|
+
p_dz = dz + i_bh * T + i + tl.arange(0, BTS)
|
|
220
|
+
# [BK, BTS]
|
|
221
|
+
b_q = tl.load(p_q, boundary_check=(0, 1))
|
|
222
|
+
# [BV, BTS]
|
|
223
|
+
b_do = tl.load(p_do, boundary_check=(0, 1)).to(b_q.dtype)
|
|
224
|
+
b_dz = tl.load(p_dz, mask=(i + tl.arange(0, BTS)) < T)
|
|
225
|
+
# [BTL, BTS]
|
|
226
|
+
b_s = tl.dot(b_k.to(b_q.dtype), b_q, allow_tf32=False) * scale
|
|
227
|
+
b_s2 = b_s * b_s
|
|
228
|
+
b_dv += tl.dot(b_s2.to(b_q.dtype), tl.trans(b_do), allow_tf32=False)
|
|
229
|
+
b_ds = tl.dot(b_v, b_do, allow_tf32=False) * scale
|
|
230
|
+
if i_v == 0:
|
|
231
|
+
b_ds += b_dz[None, :] * scale
|
|
232
|
+
else:
|
|
233
|
+
b_ds = b_ds
|
|
234
|
+
b_dk += tl.dot((2 * b_ds * b_s).to(b_q.dtype), tl.trans(b_q), allow_tf32=False)
|
|
235
|
+
|
|
236
|
+
tl.debug_barrier()
|
|
237
|
+
o_q, o_k = tl.arange(0, BTS), tl.arange(0, BTL)
|
|
238
|
+
for i in range(i_c*BTL, (i_c+1)*BTL, BTS):
|
|
239
|
+
p_q = tl.make_block_ptr(q + i_bh * T*K, (K, T), (1, K), (i_k*BK, i), (BK, BTS), (0, 1))
|
|
240
|
+
p_do = tl.make_block_ptr(do + i_bh * T*V, (V, T), (1, V), (i_v*BV, i), (BV, BTS), (0, 1))
|
|
241
|
+
p_dz = dz + i_bh * T + i + tl.arange(0, BTS)
|
|
242
|
+
b_q = tl.load(p_q, boundary_check=(0, 1)) # [BD, BQ]
|
|
243
|
+
b_do = tl.load(p_do, boundary_check=(0, 1)).to(b_q.dtype)
|
|
244
|
+
b_dz = tl.load(p_dz, mask=(i + tl.arange(0, BTS)) < T)
|
|
245
|
+
# [BK, BQ]
|
|
246
|
+
m_s = o_k[:, None] <= o_q[None, :]
|
|
247
|
+
b_s = tl.dot(b_k, b_q, allow_tf32=False) * scale
|
|
248
|
+
b_s2 = b_s * b_s
|
|
249
|
+
b_s = tl.where(m_s, b_s, 0)
|
|
250
|
+
b_s2 = tl.where(m_s, b_s2, 0)
|
|
251
|
+
|
|
252
|
+
b_ds = tl.dot(b_v, b_do, allow_tf32=False)
|
|
253
|
+
if i_v == 0:
|
|
254
|
+
b_ds += b_dz[None, :]
|
|
255
|
+
else:
|
|
256
|
+
b_ds = b_ds
|
|
257
|
+
b_ds = tl.where(m_s, b_ds, 0) * scale
|
|
258
|
+
# [BK, BD]
|
|
259
|
+
b_dv += tl.dot(b_s2.to(b_q.dtype), tl.trans(b_do), allow_tf32=False)
|
|
260
|
+
b_dk += tl.dot((2 * b_ds * b_s).to(b_q.dtype), tl.trans(b_q), allow_tf32=False)
|
|
261
|
+
o_q += BTS
|
|
262
|
+
|
|
263
|
+
p_dk = tl.make_block_ptr(dk + (i_bh + B * H * i_v) * T*K, (T, K), (K, 1), (i_c*BTL, i_k*BK), (BTL, BK), (1, 0))
|
|
264
|
+
p_dv = tl.make_block_ptr(dv + (i_bh + B * H * i_k) * T*V, (T, V), (V, 1), (i_c*BTL, i_v*BV), (BTL, BV), (1, 0))
|
|
265
|
+
tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
|
|
266
|
+
tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
|
|
267
|
+
return
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
@triton.jit(do_not_specialize=['T'])
|
|
271
|
+
def parallel_rebased_bwd_kernel(
|
|
272
|
+
q,
|
|
273
|
+
k,
|
|
274
|
+
v,
|
|
275
|
+
do,
|
|
276
|
+
dz,
|
|
277
|
+
dq,
|
|
278
|
+
dk,
|
|
279
|
+
dv,
|
|
280
|
+
scale,
|
|
281
|
+
T,
|
|
282
|
+
B: tl.constexpr,
|
|
283
|
+
H: tl.constexpr,
|
|
284
|
+
K: tl.constexpr,
|
|
285
|
+
V: tl.constexpr,
|
|
286
|
+
BTL: tl.constexpr,
|
|
287
|
+
BTS: tl.constexpr,
|
|
288
|
+
BK: tl.constexpr,
|
|
289
|
+
BV: tl.constexpr
|
|
290
|
+
):
|
|
291
|
+
i_kv, i_c, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
292
|
+
NV = tl.cdiv(V, BV)
|
|
293
|
+
i_k = i_kv // (NV)
|
|
294
|
+
i_v = i_kv % (NV)
|
|
295
|
+
i_h = i_bh % H
|
|
296
|
+
_parallel_rebased_bwd_dq(
|
|
297
|
+
i_bh,
|
|
298
|
+
i_c,
|
|
299
|
+
i_k,
|
|
300
|
+
i_v,
|
|
301
|
+
i_h,
|
|
302
|
+
q,
|
|
303
|
+
k,
|
|
304
|
+
v,
|
|
305
|
+
do,
|
|
306
|
+
dz,
|
|
307
|
+
dq,
|
|
308
|
+
scale,
|
|
309
|
+
B=B,
|
|
310
|
+
H=H,
|
|
311
|
+
T=T,
|
|
312
|
+
K=K,
|
|
313
|
+
V=V,
|
|
314
|
+
BTL=BTL,
|
|
315
|
+
BTS=BTS,
|
|
316
|
+
BK=BK,
|
|
317
|
+
BV=BV
|
|
318
|
+
)
|
|
319
|
+
tl.debug_barrier()
|
|
320
|
+
_parallel_rebased_bwd_dkv(
|
|
321
|
+
i_bh,
|
|
322
|
+
i_c,
|
|
323
|
+
i_k,
|
|
324
|
+
i_v,
|
|
325
|
+
i_h,
|
|
326
|
+
q,
|
|
327
|
+
k,
|
|
328
|
+
v,
|
|
329
|
+
do,
|
|
330
|
+
dz,
|
|
331
|
+
dk,
|
|
332
|
+
dv,
|
|
333
|
+
scale,
|
|
334
|
+
B=B,
|
|
335
|
+
H=H,
|
|
336
|
+
T=T,
|
|
337
|
+
K=K,
|
|
338
|
+
V=V,
|
|
339
|
+
BTL=BTL,
|
|
340
|
+
BTS=BTS,
|
|
341
|
+
BK=BK,
|
|
342
|
+
BV=BV
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
class ParallelBasedFunction(torch.autograd.Function):
|
|
347
|
+
|
|
348
|
+
@staticmethod
|
|
349
|
+
@input_guard
|
|
350
|
+
@autocast_custom_fwd
|
|
351
|
+
def forward(ctx, q, k, v, scale):
|
|
352
|
+
BTL, BTS = 128, 32
|
|
353
|
+
assert BTL % BTS == 0
|
|
354
|
+
# assert q.shape[-1] % 16 == 0
|
|
355
|
+
BK = min(128, max(triton.next_power_of_2(k.shape[-1]), 16))
|
|
356
|
+
BV = min(128, max(triton.next_power_of_2(v.shape[-1]), 16))
|
|
357
|
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
|
358
|
+
num_stages = 2
|
|
359
|
+
num_warps = 4
|
|
360
|
+
NK = triton.cdiv(K, BK)
|
|
361
|
+
NV = triton.cdiv(V, BV)
|
|
362
|
+
grid = (NK * NV, triton.cdiv(T, BTL), B * H)
|
|
363
|
+
|
|
364
|
+
assert NK == 1, "will encounter some synchronization issue if not."
|
|
365
|
+
|
|
366
|
+
o = torch.empty(NK, B, H, T, V, device=q.device)
|
|
367
|
+
z = torch.empty(NK, B, H, T, device=q.device)
|
|
368
|
+
parallel_rebased_fwd_kernel[grid](
|
|
369
|
+
q,
|
|
370
|
+
k,
|
|
371
|
+
v,
|
|
372
|
+
o,
|
|
373
|
+
z,
|
|
374
|
+
scale,
|
|
375
|
+
T=T,
|
|
376
|
+
B=B,
|
|
377
|
+
H=H,
|
|
378
|
+
K=K,
|
|
379
|
+
V=V,
|
|
380
|
+
BTL=BTL,
|
|
381
|
+
BTS=BTS,
|
|
382
|
+
BK=BK,
|
|
383
|
+
BV=BV,
|
|
384
|
+
num_warps=num_warps,
|
|
385
|
+
num_stages=num_stages
|
|
386
|
+
)
|
|
387
|
+
ctx.save_for_backward(q, k, v)
|
|
388
|
+
ctx.scale = scale
|
|
389
|
+
return o.sum(0).to(q.dtype), z.sum(0).to(q.dtype)
|
|
390
|
+
|
|
391
|
+
@staticmethod
|
|
392
|
+
@input_guard
|
|
393
|
+
@autocast_custom_bwd
|
|
394
|
+
def backward(ctx, do, dz):
|
|
395
|
+
q, k, v = ctx.saved_tensors
|
|
396
|
+
scale = ctx.scale
|
|
397
|
+
BTL, BTS = 64, 32
|
|
398
|
+
assert BTL % BTS == 0
|
|
399
|
+
BK = min(128, max(triton.next_power_of_2(k.shape[-1]), 16))
|
|
400
|
+
BV = min(128, max(triton.next_power_of_2(v.shape[-1]), 16))
|
|
401
|
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
|
402
|
+
num_stages = 2
|
|
403
|
+
num_warps = 4
|
|
404
|
+
NK = triton.cdiv(K, BK)
|
|
405
|
+
NV = triton.cdiv(V, BV)
|
|
406
|
+
grid = (NK * NV, triton.cdiv(T, BTL), B * H)
|
|
407
|
+
|
|
408
|
+
assert NK == 1, "will encounter some synchronization issue if not"
|
|
409
|
+
|
|
410
|
+
dq = torch.empty(NV, B, H, T, K, dtype=q.dtype, device=q.device)
|
|
411
|
+
dk = torch.empty(NV, B, H, T, K, dtype=q.dtype, device=q.device)
|
|
412
|
+
dv = torch.empty(NK, B, H, T, V, dtype=q.dtype, device=q.device)
|
|
413
|
+
|
|
414
|
+
parallel_rebased_bwd_kernel[grid](
|
|
415
|
+
q,
|
|
416
|
+
k,
|
|
417
|
+
v,
|
|
418
|
+
do,
|
|
419
|
+
dz,
|
|
420
|
+
dq,
|
|
421
|
+
dk,
|
|
422
|
+
dv,
|
|
423
|
+
scale,
|
|
424
|
+
T=T,
|
|
425
|
+
B=B,
|
|
426
|
+
H=H,
|
|
427
|
+
K=K,
|
|
428
|
+
V=V,
|
|
429
|
+
BTL=BTL,
|
|
430
|
+
BTS=BTS,
|
|
431
|
+
BK=BK,
|
|
432
|
+
BV=BV,
|
|
433
|
+
num_warps=num_warps,
|
|
434
|
+
num_stages=num_stages
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
return dq.sum(0).to(q.dtype), dk.sum(0).to(k.dtype), dv.sum(0).to(v.dtype), None
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def parallel_rebased(
|
|
441
|
+
q: torch.Tensor,
|
|
442
|
+
k: torch.Tensor,
|
|
443
|
+
v: torch.Tensor,
|
|
444
|
+
eps: float = 1e-5,
|
|
445
|
+
use_scale: bool = True,
|
|
446
|
+
use_normalize: bool = True,
|
|
447
|
+
return_both: bool = False,
|
|
448
|
+
head_first: bool = False
|
|
449
|
+
):
|
|
450
|
+
assert q.shape[-1] <= 128, "only support feature dim up to 128"
|
|
451
|
+
if use_scale:
|
|
452
|
+
scale = q.shape[-1] ** -0.5
|
|
453
|
+
else:
|
|
454
|
+
scale = 1
|
|
455
|
+
if not head_first:
|
|
456
|
+
q, k, v = map(lambda x: x.transpose(1, 2), (q, k, v))
|
|
457
|
+
o, z = ParallelBasedFunction.apply(q, k, v, scale)
|
|
458
|
+
if return_both:
|
|
459
|
+
return o, z
|
|
460
|
+
if use_normalize:
|
|
461
|
+
o = o / (z[..., None] + eps)
|
|
462
|
+
if not head_first:
|
|
463
|
+
o = o.transpose(1, 2)
|
|
464
|
+
return o.to(q.dtype)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .chunk import chunk_retention
|
|
4
|
+
from .fused_chunk import fused_chunk_retention
|
|
5
|
+
from .fused_recurrent import fused_recurrent_retention
|
|
6
|
+
from .parallel import parallel_retention
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
'chunk_retention',
|
|
10
|
+
'fused_chunk_retention',
|
|
11
|
+
'parallel_retention',
|
|
12
|
+
'fused_recurrent_retention'
|
|
13
|
+
]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
import warnings
|
|
5
|
+
from typing import Optional, Tuple
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
from fla.ops.simple_gla.chunk import chunk_simple_gla
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@torch.compiler.disable
|
|
13
|
+
def chunk_retention(
|
|
14
|
+
q: torch.Tensor,
|
|
15
|
+
k: torch.Tensor,
|
|
16
|
+
v: torch.Tensor,
|
|
17
|
+
scale: Optional[float] = None,
|
|
18
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
19
|
+
output_final_state: bool = False,
|
|
20
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
21
|
+
head_first: bool = False
|
|
22
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
23
|
+
r"""
|
|
24
|
+
Args:
|
|
25
|
+
q (torch.Tensor):
|
|
26
|
+
queries of shape `[B, T, H, K]`.
|
|
27
|
+
k (torch.Tensor):
|
|
28
|
+
keys of shape `[B, T, H, K]`.
|
|
29
|
+
v (torch.Tensor):
|
|
30
|
+
values of shape `[B, T, H, V]`.
|
|
31
|
+
scale (Optional[float]):
|
|
32
|
+
Scale factor for the attention scores.
|
|
33
|
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
|
34
|
+
initial_state (Optional[torch.Tensor]):
|
|
35
|
+
Initial state of shape `[N, H, K, V]` for `N` input sequences.
|
|
36
|
+
For equal-length input sequences, `N` equals the batch size `B`.
|
|
37
|
+
Default: `None`.
|
|
38
|
+
output_final_state (Optional[bool]):
|
|
39
|
+
Whether to output the final state of shape `[N, H, K, V]`. Default: `False`.
|
|
40
|
+
cu_seqlens (torch.LongTensor):
|
|
41
|
+
Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
|
|
42
|
+
consistent with the FlashAttention API.
|
|
43
|
+
head_first (Optional[bool]):
|
|
44
|
+
Whether the inputs are in the head-first format. Default: `False`.
|
|
45
|
+
This argument has been deprecated.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
o (torch.Tensor):
|
|
49
|
+
Outputs of shape `[B, T, H, V]`.
|
|
50
|
+
final_state (torch.Tensor):
|
|
51
|
+
Final state of shape `[N, H, K, V]` if `output_final_state=True` else `None`.
|
|
52
|
+
|
|
53
|
+
"""
|
|
54
|
+
if head_first:
|
|
55
|
+
raise DeprecationWarning(
|
|
56
|
+
"head_first is deprecated and will be removed in a future version. "
|
|
57
|
+
"Please use head_first=False for now instead."
|
|
58
|
+
)
|
|
59
|
+
if not head_first and q.shape[1] < q.shape[2]:
|
|
60
|
+
warnings.warn(
|
|
61
|
+
f"Input tensor shape suggests potential format mismatch: seq_len ({q.shape[1]}) < num_heads ({q.shape[2]}). "
|
|
62
|
+
"This may indicate the inputs were passed in head-first format [B, H, T, ...] "
|
|
63
|
+
"when head_first=False was specified. "
|
|
64
|
+
"Please verify your input tensor format matches the expected shape [B, T, H, ...]."
|
|
65
|
+
)
|
|
66
|
+
g_gamma = (1 - q.new_tensor(2., dtype=torch.float).pow(-5. - q.new_tensor(range(q.shape[2]), dtype=torch.float))).log()
|
|
67
|
+
o, final_state = chunk_simple_gla(
|
|
68
|
+
q=q,
|
|
69
|
+
k=k,
|
|
70
|
+
v=v,
|
|
71
|
+
scale=scale,
|
|
72
|
+
g_gamma=g_gamma,
|
|
73
|
+
initial_state=initial_state,
|
|
74
|
+
output_final_state=output_final_state,
|
|
75
|
+
cu_seqlens=cu_seqlens
|
|
76
|
+
)
|
|
77
|
+
return o, final_state
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
3
|
+
|
|
4
|
+
import warnings
|
|
5
|
+
from typing import Optional, Tuple
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
from fla.ops.simple_gla import fused_chunk_simple_gla
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@torch.compiler.disable
|
|
13
|
+
def fused_chunk_retention(
|
|
14
|
+
q: torch.Tensor,
|
|
15
|
+
k: torch.Tensor,
|
|
16
|
+
v: torch.Tensor,
|
|
17
|
+
scale: Optional[float] = None,
|
|
18
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
19
|
+
output_final_state: bool = False,
|
|
20
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
21
|
+
head_first: bool = False,
|
|
22
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
23
|
+
r"""
|
|
24
|
+
Args:
|
|
25
|
+
q (torch.Tensor):
|
|
26
|
+
queries of shape `[B, T, H, K]`.
|
|
27
|
+
k (torch.Tensor):
|
|
28
|
+
keys of shape `[B, T, H, K]`.
|
|
29
|
+
v (torch.Tensor):
|
|
30
|
+
values of shape `[B, T, H, V]`.
|
|
31
|
+
scale (Optional[float]):
|
|
32
|
+
Scale factor for the attention scores.
|
|
33
|
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
|
34
|
+
initial_state (Optional[torch.Tensor]):
|
|
35
|
+
Initial state of shape `[N, H, K, V]` for `N` input sequences.
|
|
36
|
+
For equal-length input sequences, `N` equals the batch size `B`.
|
|
37
|
+
Default: `None`.
|
|
38
|
+
output_final_state (Optional[bool]):
|
|
39
|
+
Whether to output the final state of shape `[N, H, K, V]`. Default: `False`.
|
|
40
|
+
cu_seqlens (torch.LongTensor):
|
|
41
|
+
Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
|
|
42
|
+
consistent with the FlashAttention API.
|
|
43
|
+
head_first (Optional[bool]):
|
|
44
|
+
Whether the inputs are in the head-first format.
|
|
45
|
+
Default: `False`.
|
|
46
|
+
This argument has been deprecated.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
o (torch.Tensor):
|
|
50
|
+
Outputs of shape `[B, T, H, V]`.
|
|
51
|
+
final_state (torch.Tensor):
|
|
52
|
+
Final state of shape `[N, H, K, V]` if `output_final_state=True` else `None`.
|
|
53
|
+
|
|
54
|
+
"""
|
|
55
|
+
if head_first:
|
|
56
|
+
raise DeprecationWarning(
|
|
57
|
+
"head_first is deprecated and will be removed in a future version. "
|
|
58
|
+
"Please use head_first=False for now instead."
|
|
59
|
+
)
|
|
60
|
+
if not head_first and q.shape[1] < q.shape[2]:
|
|
61
|
+
warnings.warn(
|
|
62
|
+
f"Input tensor shape suggests potential format mismatch: seq_len ({q.shape[1]}) < num_heads ({q.shape[2]}). "
|
|
63
|
+
"This may indicate the inputs were passed in head-first format [B, H, T, ...] "
|
|
64
|
+
"when head_first=False was specified. "
|
|
65
|
+
"Please verify your input tensor format matches the expected shape [B, T, H, ...]."
|
|
66
|
+
)
|
|
67
|
+
g_gamma = (1 - q.new_tensor(2., dtype=torch.float).pow(-5. - q.new_tensor(range(q.shape[2]), dtype=torch.float))).log()
|
|
68
|
+
o, final_state = fused_chunk_simple_gla(
|
|
69
|
+
q=q,
|
|
70
|
+
k=k,
|
|
71
|
+
v=v,
|
|
72
|
+
g_gamma=g_gamma,
|
|
73
|
+
scale=scale,
|
|
74
|
+
initial_state=initial_state,
|
|
75
|
+
output_final_state=output_final_state,
|
|
76
|
+
cu_seqlens=cu_seqlens
|
|
77
|
+
)
|
|
78
|
+
return o, final_state
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
|
|
8
|
+
from fla.ops.simple_gla.fused_recurrent import fused_recurrent_simple_gla
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def fused_recurrent_retention(
|
|
12
|
+
q: torch.Tensor,
|
|
13
|
+
k: torch.Tensor,
|
|
14
|
+
v: torch.Tensor,
|
|
15
|
+
scale: Optional[float] = None,
|
|
16
|
+
initial_state: Optional[torch.Tensor] = None,
|
|
17
|
+
output_final_state: bool = False,
|
|
18
|
+
reverse: bool = False,
|
|
19
|
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
|
20
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
21
|
+
g_gamma = (1 - q.new_tensor(2., dtype=torch.float).pow(-5. - q.new_tensor(range(q.shape[2]), dtype=torch.float))).log()
|
|
22
|
+
o, final_state = fused_recurrent_simple_gla(
|
|
23
|
+
q=q,
|
|
24
|
+
k=k,
|
|
25
|
+
v=v,
|
|
26
|
+
g_gamma=g_gamma,
|
|
27
|
+
scale=scale,
|
|
28
|
+
initial_state=initial_state,
|
|
29
|
+
output_final_state=output_final_state,
|
|
30
|
+
reverse=reverse,
|
|
31
|
+
cu_seqlens=cu_seqlens,
|
|
32
|
+
)
|
|
33
|
+
return o, final_state
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def naive_retention(q, k, v):
|
|
7
|
+
orig_type = q.dtype
|
|
8
|
+
q, k, v = q.float(), k.float(), v.float()
|
|
9
|
+
_, n_heads, seq_len, d_head = q.shape
|
|
10
|
+
s = (1 - q.new_tensor(2., dtype=torch.float).pow(-5. - q.new_tensor(range(n_heads), dtype=torch.float))).log2()
|
|
11
|
+
n = q.new_tensor(range(seq_len), dtype=torch.float)
|
|
12
|
+
n = torch.exp2((n.unsqueeze(-1) - n) * s.view(-1, 1, 1)) * n.unsqueeze(-1).ge(n)
|
|
13
|
+
s = torch.einsum('bhqd,bhkd,hqk->bhqk', q * d_head ** -0.5, k, n.to(q.dtype))
|
|
14
|
+
o = torch.einsum('bhqk,bhkd->bhqd', s, v)
|
|
15
|
+
return o.to(orig_type)
|