Safetensors
llama
custom_code
federico-alvetreti commited on
Commit
083317e
·
verified ·
1 Parent(s): ee66dbc

Upload 8B SFT 16k NSA checkpoint

Browse files
added_tokens.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "<|execute_end|>": 73444,
3
+ "<|execute_start|>": 73443,
4
+ "<|fim_middle|>": 73446,
5
+ "<|fim_prefix|>": 73445,
6
+ "<|fim_suffix|>": 73447,
7
+ "<|im_end|>": 73440,
8
+ "<|im_start|>": 73441,
9
+ "<|tool_call|>": 73442
10
+ }
compressed_attention.py ADDED
@@ -0,0 +1,1404 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2025 Xunhao Lai & Jianqiao Lu.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import math
15
+ from typing import Any, Tuple, Union
16
+ from collections import Counter
17
+ import torch
18
+ import triton
19
+ import triton.language as tl
20
+ import warnings
21
+
22
+ import torch
23
+ def is_hopper_gpu():
24
+ if torch.cuda.is_available():
25
+ device_capability = torch.cuda.get_device_capability()
26
+ major, minor = device_capability
27
+ return major == 9
28
+ return False
29
+ def get_compressed_seqlens(
30
+ cu_seqlens: torch.Tensor, kernel_size: int, kernel_stride: int
31
+ ):
32
+ # compute seqlens after compression
33
+ seqlens = cu_seqlens[1:] - cu_seqlens[:-1]
34
+ y_seqlens = torch.floor((seqlens - kernel_size) / kernel_stride).to(torch.int32) + 1
35
+ # corner case, if sequence_length < kernel_size, no compression for this sequence
36
+ y_seqlens[seqlens < kernel_size] = 0
37
+ y_cu_seqlens = torch.zeros(
38
+ y_seqlens.shape[0] + 1, dtype=torch.int32, device=cu_seqlens.device
39
+ )
40
+ y_cu_seqlens[1:] = torch.cumsum(y_seqlens, dim=0)
41
+ return y_seqlens, y_cu_seqlens
42
+
43
+
44
+ def get_num_warps_stages(head_dim, block_size, is_hopper_gpu):
45
+ """
46
+ Returns recommended num_warps and num_stages for a Sparse Attention kernel in Triton.
47
+
48
+ Args:
49
+ head_dim (int): Size of the head dimension.
50
+ block_size (int): Size of the block in the attention matrix.
51
+ is_hopper_gpu (bool): True if Hopper GPU, False if Ampere GPU.
52
+
53
+ Returns:
54
+ tuple: (num_warps, num_stages) recommended values.
55
+ """
56
+ # Determine if head_dim and block_size exceed 64
57
+ head_large = head_dim > 64
58
+ block_large = block_size > 64
59
+
60
+ if is_hopper_gpu:
61
+ # Hopper GPU recommendations
62
+ if head_large and block_large:
63
+ num_warps = 8
64
+ num_stages = 3
65
+ elif head_large or block_large:
66
+ num_warps = 4
67
+ num_stages = 3
68
+ else:
69
+ num_warps = 2
70
+ num_stages = 2
71
+ else:
72
+ # Ampere GPU recommendations
73
+ if head_large and block_large:
74
+ num_warps = 8
75
+ num_stages = 3
76
+ elif head_large or block_large:
77
+ num_warps = 8
78
+ num_stages = 3
79
+ else:
80
+ num_warps = 2
81
+ num_stages = 2
82
+ return num_warps, num_stages
83
+
84
+
85
+ IS_HOPPER_GPU = is_hopper_gpu()
86
+
87
+
88
+ @triton.jit
89
+ def forward_kernel(
90
+ q_ptr, # Q: n x h x d
91
+ k_ptr, # K: n x h x d
92
+ v_ptr, # V: n x h x d
93
+ o_ptr, # O: n x h x d
94
+ lse_ptr, # LSE: h x n
95
+ # size and stride at compresstion
96
+ kernel_size,
97
+ kernel_stride,
98
+ # seqlens
99
+ cu_seqlens_q,
100
+ cu_seqlens_k,
101
+ # shape
102
+ NUM_KV_HEADS,
103
+ NUM_SHARE_Q_HEADS,
104
+ HEAD_DIM,
105
+ # sm_scale
106
+ sm_scale,
107
+ # stride
108
+ stride_qn,
109
+ stride_qh,
110
+ stride_qd,
111
+ stride_kn,
112
+ stride_kh,
113
+ stride_kd,
114
+ stride_vn,
115
+ stride_vh,
116
+ stride_vd,
117
+ stride_on,
118
+ stride_oh,
119
+ stride_od,
120
+ stride_lh,
121
+ stride_ln,
122
+ # META parameters
123
+ BLOCK_SIZE_Q: tl.constexpr, # q block size
124
+ BLOCK_SIZE_K: tl.constexpr, # k block size
125
+ BLOCK_SIZE_D: tl.constexpr,
126
+ ):
127
+ qk_scale = sm_scale * 1.44269504
128
+ # get batch id and head id
129
+ pid_b = tl.program_id(0)
130
+ pid_h = tl.program_id(1)
131
+ pid_q = tl.program_id(2)
132
+ pid_kh = pid_h // NUM_SHARE_Q_HEADS
133
+ # get q k start and len after rmpad
134
+ q_start = tl.load(cu_seqlens_q + pid_b)
135
+ q_len = tl.load(cu_seqlens_q + pid_b + 1) - q_start
136
+ k_start = tl.load(cu_seqlens_k + pid_b)
137
+ k_len = tl.load(cu_seqlens_k + pid_b + 1) - k_start
138
+ # skip first kernel_size query block, because they do no attend to any keys
139
+ q_start_in_seq = pid_q * BLOCK_SIZE_Q + kernel_size - 1
140
+ if q_start_in_seq >= q_len:
141
+ return
142
+ # init qkv pointer
143
+ q_ptrs = tl.make_block_ptr(
144
+ base=q_ptr + q_start * stride_qn + pid_h * stride_qh,
145
+ shape=(q_len, HEAD_DIM),
146
+ strides=(stride_qn, stride_qd),
147
+ offsets=(q_start_in_seq, 0),
148
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_D),
149
+ order=(1, 0),
150
+ )
151
+ k_ptrs = tl.make_block_ptr(
152
+ base=k_ptr + k_start * stride_kn + pid_kh * stride_kh,
153
+ shape=(HEAD_DIM, k_len),
154
+ strides=(stride_kd, stride_kn),
155
+ offsets=(0, 0),
156
+ block_shape=(BLOCK_SIZE_D, BLOCK_SIZE_K),
157
+ order=(0, 1),
158
+ )
159
+ v_ptrs = tl.make_block_ptr(
160
+ base=v_ptr + k_start * stride_vn + pid_kh * stride_vh,
161
+ shape=(k_len, HEAD_DIM),
162
+ strides=(stride_vn, stride_vd),
163
+ offsets=(0, 0),
164
+ block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_D),
165
+ order=(1, 0),
166
+ )
167
+ # load q
168
+ q = tl.load(q_ptrs, boundary_check=(0, 1), padding_option="zero")
169
+ # init statistics
170
+ off_q = tl.arange(0, BLOCK_SIZE_Q) + q_start_in_seq
171
+ off_k = tl.arange(0, BLOCK_SIZE_K) * kernel_stride + kernel_size - 1
172
+ m_i = tl.full((BLOCK_SIZE_Q,), float("-inf"), dtype=tl.float32)
173
+ lse_i = tl.full((BLOCK_SIZE_Q,), float("-inf"), dtype=tl.float32)
174
+ acc_o = tl.full((BLOCK_SIZE_Q, BLOCK_SIZE_D), 0, dtype=tl.float32)
175
+ # attention
176
+ lo = 0
177
+ hi = min(k_len, (q_start_in_seq + BLOCK_SIZE_Q - kernel_size) // kernel_stride + 1)
178
+ for i in range(lo, hi, BLOCK_SIZE_K):
179
+ i = tl.multiple_of(i, BLOCK_SIZE_K)
180
+ # load k
181
+ k = tl.load(k_ptrs, boundary_check=(1, 0), padding_option="zero")
182
+ # compute qk
183
+ qk = tl.zeros((BLOCK_SIZE_Q, BLOCK_SIZE_K), dtype=tl.float32)
184
+ qk += tl.where(
185
+ off_q[:, None] >= (i * kernel_stride + off_k)[None, :], 0, float("-inf")
186
+ )
187
+ qk += tl.dot(q, k) * qk_scale
188
+ # compute m_ij and l_ij
189
+ m_ij = tl.maximum(m_i, tl.max(qk, axis=1))
190
+ p = tl.exp2(qk - m_ij[:, None])
191
+ l_ij = tl.sum(p, axis=1)
192
+ # scale acc_o
193
+ acc_o_scale = tl.exp2(m_i - m_ij)
194
+ acc_o = acc_o * acc_o_scale[:, None]
195
+ # load v and update acc_o
196
+ v = tl.load(v_ptrs, boundary_check=(0, 1), padding_option="zero")
197
+ p = p.to(v.dtype)
198
+ acc_o += tl.dot(p, v)
199
+ # update statistics
200
+ m_i = m_ij
201
+ lse_i = m_ij + tl.math.log2(tl.exp2(lse_i - m_ij) + l_ij)
202
+ # update ptrs
203
+ k_ptrs = tl.advance(k_ptrs, (0, BLOCK_SIZE_K))
204
+ v_ptrs = tl.advance(v_ptrs, (BLOCK_SIZE_K, 0))
205
+ # final scale
206
+ acc_o = acc_o * tl.exp2(m_i - lse_i)[:, None]
207
+ # save output
208
+ o_ptrs = tl.make_block_ptr(
209
+ base=o_ptr + q_start * stride_on + pid_h * stride_oh,
210
+ shape=(q_len, HEAD_DIM),
211
+ strides=(stride_on, stride_od),
212
+ offsets=(q_start_in_seq, 0),
213
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_D),
214
+ order=(1, 0),
215
+ )
216
+ tl.store(o_ptrs, acc_o.to(o_ptr.dtype.element_ty), boundary_check=(0, 1))
217
+ # save lse
218
+ l_ptrs = lse_ptr + q_start * stride_ln + pid_h * stride_lh + off_q * stride_ln
219
+ tl.store(l_ptrs, lse_i, mask=off_q < q_len)
220
+
221
+
222
+ @triton.jit
223
+ def backward_sum_o_do(
224
+ o_ptr, # O: n x h x d
225
+ do_ptr, # dO: n x h x d
226
+ delta_ptr, # D: h x n
227
+ o_len,
228
+ HEAD_DIM,
229
+ stride_on,
230
+ stride_oh,
231
+ stride_od,
232
+ stride_don,
233
+ stride_doh,
234
+ stride_dod,
235
+ stride_dh,
236
+ stride_dn,
237
+ BLOCK_SIZE_O: tl.constexpr,
238
+ BLOCK_SIZE_D: tl.constexpr,
239
+ ):
240
+ pid_n = tl.program_id(0)
241
+ pid_h = tl.program_id(1)
242
+ off_n = pid_n * BLOCK_SIZE_O + tl.arange(0, BLOCK_SIZE_O)
243
+ off_d = tl.arange(0, BLOCK_SIZE_D)
244
+ o = tl.load(
245
+ o_ptr
246
+ + off_n[:, None] * stride_on
247
+ + pid_h * stride_oh
248
+ + off_d[None, :] * stride_od,
249
+ mask=(off_n[:, None] < o_len) & (off_d[None, :] < HEAD_DIM),
250
+ other=0,
251
+ ).to(tl.float32)
252
+ do = tl.load(
253
+ do_ptr
254
+ + off_n[:, None] * stride_don
255
+ + pid_h * stride_doh
256
+ + off_d[None, :] * stride_dod,
257
+ mask=(off_n[:, None] < o_len) & (off_d[None, :] < HEAD_DIM),
258
+ other=0,
259
+ ).to(tl.float32)
260
+ delta = tl.sum(o * do, axis=1)
261
+ tl.store(
262
+ delta_ptr + pid_h * stride_dh + off_n * stride_dn, delta, mask=off_n < o_len
263
+ )
264
+
265
+
266
+ @triton.jit
267
+ def backward_dkdv(
268
+ q_ptr, # Q: n x qh x d
269
+ k_ptr, # K: n x kh x d
270
+ v_ptr, # V: n x kh x d
271
+ lse_ptr, # LSE: qh x n
272
+ d_ptr, # Delta: qh x n
273
+ do_ptr,
274
+ dk_ptr, # DK: sh x n x kh x d
275
+ dv_ptr, # DV: sh x n x kh x d
276
+ kernel_size,
277
+ kernel_stride,
278
+ # seqlens
279
+ cu_seqlens_q,
280
+ cu_seqlens_k,
281
+ # shape
282
+ NUM_KV_HEADS,
283
+ NUM_SHARE_Q_HEADS,
284
+ HEAD_DIM,
285
+ # sm_scale
286
+ sm_scale,
287
+ # stride
288
+ stride_qn,
289
+ stride_qh,
290
+ stride_qd,
291
+ stride_kn,
292
+ stride_kh,
293
+ stride_kd,
294
+ stride_vn,
295
+ stride_vh,
296
+ stride_vd,
297
+ stride_lh,
298
+ stride_ln,
299
+ stride_dh,
300
+ stride_dn,
301
+ stride_don,
302
+ stride_doh,
303
+ stride_dod,
304
+ stride_dks,
305
+ stride_dkn,
306
+ stride_dkh,
307
+ stride_dkd,
308
+ stride_dvs,
309
+ stride_dvn,
310
+ stride_dvh,
311
+ stride_dvd,
312
+ # META parameters
313
+ BLOCK_SIZE_Q: tl.constexpr, # q block size
314
+ BLOCK_SIZE_K: tl.constexpr, # k block size
315
+ BLOCK_SIZE_D: tl.constexpr,
316
+ ):
317
+ qk_scale = sm_scale * 1.44269504
318
+ # get batch id and head id
319
+ pid_b = tl.program_id(0)
320
+ pid_h = tl.program_id(1)
321
+ pid_kh = pid_h // NUM_SHARE_Q_HEADS
322
+ pid_sh = pid_h % NUM_SHARE_Q_HEADS
323
+ pid_k = tl.program_id(2)
324
+ # get q k start and len after rmpad
325
+ q_start = tl.load(cu_seqlens_q + pid_b)
326
+ q_len = tl.load(cu_seqlens_q + pid_b + 1) - q_start
327
+ k_start = tl.load(cu_seqlens_k + pid_b)
328
+ k_len = tl.load(cu_seqlens_k + pid_b + 1) - k_start
329
+ if BLOCK_SIZE_K * pid_k >= k_len:
330
+ return
331
+ # init pointers
332
+ k_ptrs = tl.make_block_ptr(
333
+ base=k_ptr + k_start * stride_kn + pid_kh * stride_kh,
334
+ shape=(k_len, HEAD_DIM),
335
+ strides=(stride_kn, stride_kd),
336
+ offsets=(pid_k * BLOCK_SIZE_K, 0),
337
+ block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_D),
338
+ order=(1, 0),
339
+ )
340
+ dk_ptrs = tl.make_block_ptr(
341
+ base=dk_ptr + k_start * stride_dkn + pid_kh * stride_dkh + pid_sh * stride_dks,
342
+ shape=(k_len, HEAD_DIM),
343
+ strides=(stride_dkn, stride_dkd),
344
+ offsets=(pid_k * BLOCK_SIZE_K, 0),
345
+ block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_D),
346
+ order=(1, 0),
347
+ )
348
+ v_ptrs = tl.make_block_ptr(
349
+ base=v_ptr + k_start * stride_vn + pid_kh * stride_vh,
350
+ shape=(k_len, HEAD_DIM),
351
+ strides=(stride_vn, stride_vd),
352
+ offsets=(pid_k * BLOCK_SIZE_K, 0),
353
+ block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_D),
354
+ order=(1, 0),
355
+ )
356
+ dv_ptrs = tl.make_block_ptr(
357
+ base=dv_ptr + k_start * stride_dvn + pid_kh * stride_dvh + pid_sh * stride_dvs,
358
+ shape=(k_len, HEAD_DIM),
359
+ strides=(stride_dvn, stride_dvd),
360
+ offsets=(pid_k * BLOCK_SIZE_K, 0),
361
+ block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_D),
362
+ order=(1, 0),
363
+ )
364
+ # offsets
365
+ off_q = tl.arange(0, BLOCK_SIZE_Q)
366
+ off_k = (
367
+ pid_k * BLOCK_SIZE_K * kernel_stride
368
+ + tl.arange(0, BLOCK_SIZE_K) * kernel_stride
369
+ + kernel_size
370
+ - 1
371
+ )
372
+ # load k v and keep in SRAM
373
+ k = tl.load(k_ptrs, boundary_check=(0, 1), padding_option="zero")
374
+ v = tl.load(v_ptrs, boundary_check=(0, 1), padding_option="zero")
375
+ # init dk dv
376
+ dk = tl.zeros((BLOCK_SIZE_K, BLOCK_SIZE_D), dtype=tl.float32)
377
+ dv = tl.zeros((BLOCK_SIZE_K, BLOCK_SIZE_D), dtype=tl.float32)
378
+ q_lo = pid_k * BLOCK_SIZE_K * kernel_stride + kernel_size - 1
379
+ q_ptrs = tl.make_block_ptr(
380
+ base=q_ptr + q_start * stride_qn + pid_h * stride_qh,
381
+ shape=(HEAD_DIM, q_len),
382
+ strides=(stride_qd, stride_qn),
383
+ offsets=(0, q_lo),
384
+ block_shape=(BLOCK_SIZE_D, BLOCK_SIZE_Q),
385
+ order=(0, 1),
386
+ )
387
+ do_ptrs = tl.make_block_ptr(
388
+ base=do_ptr + q_start * stride_don + pid_h * stride_doh,
389
+ shape=(HEAD_DIM, q_len),
390
+ strides=(stride_dod, stride_don),
391
+ offsets=(0, q_lo),
392
+ block_shape=(BLOCK_SIZE_D, BLOCK_SIZE_Q),
393
+ order=(0, 1),
394
+ )
395
+ d_ptrs = tl.make_block_ptr(
396
+ base=d_ptr + q_start * stride_dn + pid_h * stride_dh,
397
+ shape=(1, q_len),
398
+ strides=(0, stride_dn),
399
+ offsets=(0, q_lo),
400
+ block_shape=(1, BLOCK_SIZE_Q),
401
+ order=(1, 0),
402
+ )
403
+ lse_ptrs = tl.make_block_ptr(
404
+ base=lse_ptr + q_start * stride_ln + pid_h * stride_lh,
405
+ shape=(1, q_len),
406
+ strides=(0, stride_ln),
407
+ offsets=(0, q_lo),
408
+ block_shape=(1, BLOCK_SIZE_Q),
409
+ order=(0, 1),
410
+ )
411
+ # loop for q blocks
412
+ for i in range(q_lo, q_len, BLOCK_SIZE_Q):
413
+ # load
414
+ q = tl.load(q_ptrs, boundary_check=(0, 1), padding_option="zero")
415
+ do = tl.load(do_ptrs, boundary_check=(0, 1), padding_option="zero")
416
+ lse = tl.load(lse_ptrs, boundary_check=(0, 1), padding_option="zero")
417
+ d = tl.load(d_ptrs, boundary_check=(0, 1), padding_option="zero")
418
+ # compute qk
419
+ # [BLOCK_SIZE_K, HEAD_DIM] @ [HEAD_DIM, BLOCK_SIE_Q] -> [BLOCK_SIZE_K, BLOCK_SIE_Q]
420
+ qk = tl.where(off_k[:, None] <= (off_q + i)[None, :], float(0.0), float("-inf"))
421
+ qk += tl.dot(k, q) * qk_scale
422
+ # compute p, ds
423
+ # [BLOCK_SIZE_K, BLOCK_SIE_Q] - [1, BLOCK_SIZE_Q] -> [BLOCK_SIZE_K, BLOCK_SIE_Q]
424
+ p = tl.exp2(qk - lse)
425
+ # [BLOCK_SIZE_K, HEAD_DIM] @ [HEAD_DIM, BLOCK_SIE_Q] -> [BLOCK_SIZE_K, BLOCK_SIE_Q]
426
+ dp = tl.dot(v, do)
427
+ ds = sm_scale * p * (dp - d)
428
+ # cast dtype
429
+ p = p.to(do.dtype)
430
+ ds = ds.to(q.dtype)
431
+ # update dk and dv
432
+ # [BLOCK_SIZE_K, BLOCK_SIE_Q] @ [BLOCK_SIE_Q, HEAD_DIM] -> [BLOCK_SIZE_K, HEAD_DIM]
433
+ dk += tl.dot(ds, tl.trans(q))
434
+ dv += tl.dot(p, tl.trans(do))
435
+ # increment pointers
436
+ q_ptrs = tl.advance(q_ptrs, (0, BLOCK_SIZE_Q))
437
+ do_ptrs = tl.advance(do_ptrs, (0, BLOCK_SIZE_Q))
438
+ lse_ptrs = tl.advance(lse_ptrs, (0, BLOCK_SIZE_Q))
439
+ d_ptrs = tl.advance(d_ptrs, (0, BLOCK_SIZE_Q))
440
+ # save dk dv
441
+ tl.store(dk_ptrs, dk.to(dk_ptr.dtype.element_ty), boundary_check=(0, 1))
442
+ tl.store(dv_ptrs, dv.to(dv_ptr.dtype.element_ty), boundary_check=(0, 1))
443
+
444
+
445
+ @triton.jit
446
+ def backward_dq(
447
+ q_ptr, # Q: n x qh x d
448
+ k_ptr, # K: n x kh x d
449
+ v_ptr, # V: n x kh x d
450
+ lse_ptr, # LSE: qh x n
451
+ d_ptr, # Delta: qh x n
452
+ do_ptr,
453
+ dq_ptr,
454
+ kernel_size,
455
+ kernel_stride,
456
+ # seqlens
457
+ cu_seqlens_q,
458
+ cu_seqlens_k,
459
+ # shape
460
+ NUM_KV_HEADS,
461
+ NUM_SHARE_Q_HEADS,
462
+ HEAD_DIM,
463
+ # sm_scale
464
+ sm_scale,
465
+ # stride
466
+ stride_qn,
467
+ stride_qh,
468
+ stride_qd,
469
+ stride_kn,
470
+ stride_kh,
471
+ stride_kd,
472
+ stride_vn,
473
+ stride_vh,
474
+ stride_vd,
475
+ stride_lh,
476
+ stride_ln,
477
+ stride_dh,
478
+ stride_dn,
479
+ stride_don,
480
+ stride_doh,
481
+ stride_dod,
482
+ stride_dqn,
483
+ stride_dqh,
484
+ stride_dqd,
485
+ # META parameters
486
+ BLOCK_SIZE_Q: tl.constexpr, # q block size
487
+ BLOCK_SIZE_K: tl.constexpr, # k block size
488
+ BLOCK_SIZE_D: tl.constexpr,
489
+ ):
490
+ qk_scale = sm_scale * 1.44269504
491
+ # get batch id and head id
492
+ pid_b = tl.program_id(0)
493
+ pid_h = tl.program_id(1)
494
+ pid_q = tl.program_id(2)
495
+ pid_kh = pid_h // NUM_SHARE_Q_HEADS
496
+ # get q k start and len after rmpad
497
+ q_start = tl.load(cu_seqlens_q + pid_b)
498
+ q_len = tl.load(cu_seqlens_q + pid_b + 1) - q_start
499
+ k_start = tl.load(cu_seqlens_k + pid_b)
500
+ k_len = tl.load(cu_seqlens_k + pid_b + 1) - k_start
501
+ # skip first kernel_size query block, because they do no attend to any keys
502
+ q_start_in_seq = pid_q * BLOCK_SIZE_Q + kernel_size - 1
503
+ if q_start_in_seq >= q_len:
504
+ return
505
+ # init pointers
506
+ q_ptrs = tl.make_block_ptr(
507
+ base=q_ptr + q_start * stride_qn + pid_h * stride_qh,
508
+ shape=(q_len, HEAD_DIM),
509
+ strides=(stride_qn, stride_qd),
510
+ offsets=(q_start_in_seq, 0),
511
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_D),
512
+ order=(1, 0),
513
+ )
514
+ dq_ptrs = tl.make_block_ptr(
515
+ base=dq_ptr + q_start * stride_dqn + pid_h * stride_dqh,
516
+ shape=(q_len, HEAD_DIM),
517
+ strides=(stride_dqn, stride_dqd),
518
+ offsets=(q_start_in_seq, 0),
519
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_D),
520
+ order=(1, 0),
521
+ )
522
+ k_ptrs = tl.make_block_ptr(
523
+ base=k_ptr + k_start * stride_kn + pid_kh * stride_kh,
524
+ shape=(k_len, HEAD_DIM),
525
+ strides=(stride_kn, stride_kd),
526
+ offsets=(0, 0),
527
+ block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_D),
528
+ order=(1, 0),
529
+ )
530
+ v_ptrs = tl.make_block_ptr(
531
+ base=v_ptr + k_start * stride_vn + pid_kh * stride_vh,
532
+ shape=(HEAD_DIM, k_len),
533
+ strides=(stride_vd, stride_vn),
534
+ offsets=(0, 0),
535
+ block_shape=(BLOCK_SIZE_D, BLOCK_SIZE_K),
536
+ order=(0, 1),
537
+ )
538
+ do_ptrs = tl.make_block_ptr(
539
+ base=do_ptr + q_start * stride_don + pid_h * stride_doh,
540
+ shape=(q_len, HEAD_DIM),
541
+ strides=(stride_don, stride_dod),
542
+ offsets=(q_start_in_seq, 0),
543
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_D),
544
+ order=(1, 0),
545
+ )
546
+ d_ptrs = tl.make_block_ptr(
547
+ base=d_ptr + q_start * stride_dn + pid_h * stride_dh,
548
+ shape=(q_len, 1),
549
+ strides=(stride_dn, stride_dh),
550
+ offsets=(q_start_in_seq, 0),
551
+ block_shape=(BLOCK_SIZE_Q, 1),
552
+ order=(0, 1),
553
+ )
554
+ lse_ptrs = tl.make_block_ptr(
555
+ base=lse_ptr + q_start * stride_ln + pid_h * stride_lh,
556
+ shape=(q_len, 1),
557
+ strides=(stride_ln, stride_lh),
558
+ offsets=(q_start_in_seq, 0),
559
+ block_shape=(BLOCK_SIZE_Q, 1),
560
+ order=(0, 1),
561
+ )
562
+ # offsets
563
+ off_q = tl.arange(0, BLOCK_SIZE_Q) + q_start_in_seq
564
+ off_k = tl.arange(0, BLOCK_SIZE_K) * kernel_stride + kernel_size - 1
565
+ # load q, do, lse, delta, and keep in SRAM
566
+ q = tl.load(q_ptrs, boundary_check=(1, 0), padding_option="zero")
567
+ do = tl.load(do_ptrs, boundary_check=(0, 1), padding_option="zero")
568
+ lse = tl.load(lse_ptrs, boundary_check=(0, 1), padding_option="zero")
569
+ d = tl.load(d_ptrs, boundary_check=(0, 1), padding_option="zero")
570
+ # init dq
571
+ dq = tl.zeros((BLOCK_SIZE_Q, BLOCK_SIZE_D), dtype=tl.float32)
572
+ lo = 0
573
+ hi = min(k_len, (q_start_in_seq + BLOCK_SIZE_Q - kernel_size) // kernel_stride + 1)
574
+ for i in range(lo, hi, BLOCK_SIZE_K):
575
+ # load
576
+ k = tl.load(k_ptrs, boundary_check=(0, 1), padding_option="zero")
577
+ v = tl.load(v_ptrs, boundary_check=(0, 1), padding_option="zero")
578
+ # compute qk
579
+ qk = tl.zeros((BLOCK_SIZE_Q, BLOCK_SIZE_K), dtype=tl.float32)
580
+ qk += tl.where(
581
+ off_q[:, None] >= (i * kernel_stride + off_k)[None, :], 0, float("-inf")
582
+ )
583
+ qk += tl.dot(q, tl.trans(k)) * qk_scale
584
+ # compute p, ds
585
+ p = tl.exp2(qk - lse)
586
+ dp = tl.dot(do, v)
587
+ ds = sm_scale * p * (dp - d)
588
+ # cast dtype
589
+ ds = ds.to(q.dtype)
590
+ # update dq
591
+ dq += tl.dot(ds, k)
592
+ # increment pointers
593
+ k_ptrs = tl.advance(k_ptrs, (BLOCK_SIZE_K, 0))
594
+ v_ptrs = tl.advance(v_ptrs, (0, BLOCK_SIZE_K))
595
+ # save dq
596
+ tl.store(dq_ptrs, dq.to(dq_ptr.dtype.element_ty), boundary_check=(0, 1))
597
+
598
+
599
+ def _compressed_attention_fwd(
600
+ q: torch.Tensor,
601
+ k: torch.Tensor,
602
+ v: torch.Tensor,
603
+ kernel_size: int,
604
+ kernel_stride: int,
605
+ cu_seqlens_q: torch.Tensor,
606
+ cu_seqlens_k: torch.Tensor,
607
+ max_seqlen_q: torch.Tensor,
608
+ max_seqlen_k: torch.Tensor,
609
+ sm_scale: float,
610
+ ):
611
+ # dtype check
612
+ assert k.dtype == q.dtype and v.dtype == q.dtype
613
+ assert cu_seqlens_q.dtype == torch.int32 and cu_seqlens_k.dtype == torch.int32
614
+ # shape
615
+ q_len, num_q_heads, head_dim = q.shape
616
+ k_len, num_k_heads, head_dim = k.shape
617
+ v_len, num_v_heads, head_dim = v.shape
618
+ batch_size = cu_seqlens_q.shape[0] - 1
619
+ assert k_len == v_len and q_len > k_len
620
+ # gqa
621
+ assert num_k_heads == num_v_heads
622
+ assert num_q_heads % num_k_heads == 0
623
+ num_share_q_heads = num_q_heads // num_k_heads
624
+ # output tensor
625
+ o = torch.zeros_like(q)
626
+ lse = torch.full(
627
+ (num_q_heads, q_len),
628
+ fill_value=-torch.inf,
629
+ dtype=torch.float32,
630
+ device=q.device,
631
+ )
632
+ # launch kernel
633
+ grid = lambda META: (
634
+ batch_size,
635
+ num_q_heads,
636
+ triton.cdiv(max_seqlen_q, META["BLOCK_SIZE_Q"]),
637
+ )
638
+ BLOCK_SIZE_Q = 128
639
+ BLOCK_SIZE_K = 128
640
+ BLOCK_SIZE_D = triton.next_power_of_2(head_dim)
641
+ num_warps, num_stages = get_num_warps_stages(head_dim, BLOCK_SIZE_Q, IS_HOPPER_GPU)
642
+ forward_kernel[grid](
643
+ q,
644
+ k,
645
+ v,
646
+ o,
647
+ lse,
648
+ kernel_size,
649
+ kernel_stride,
650
+ cu_seqlens_q,
651
+ cu_seqlens_k,
652
+ num_k_heads,
653
+ num_share_q_heads,
654
+ head_dim,
655
+ sm_scale,
656
+ q.stride(0),
657
+ q.stride(1),
658
+ q.stride(2),
659
+ k.stride(0),
660
+ k.stride(1),
661
+ k.stride(2),
662
+ v.stride(0),
663
+ v.stride(1),
664
+ v.stride(2),
665
+ o.stride(0),
666
+ o.stride(1),
667
+ o.stride(2),
668
+ lse.stride(0),
669
+ lse.stride(1),
670
+ BLOCK_SIZE_Q=BLOCK_SIZE_Q,
671
+ BLOCK_SIZE_K=BLOCK_SIZE_K,
672
+ BLOCK_SIZE_D=BLOCK_SIZE_D,
673
+ num_warps=num_warps,
674
+ num_stages=num_stages,
675
+ )
676
+ return o, lse
677
+
678
+
679
+ def _compressed_attention_bwd(
680
+ o: torch.Tensor,
681
+ do: torch.Tensor,
682
+ lse: torch.Tensor,
683
+ q: torch.Tensor,
684
+ k: torch.Tensor,
685
+ v: torch.Tensor,
686
+ kernel_size: int,
687
+ kernel_stride: int,
688
+ cu_seqlens_q: torch.Tensor,
689
+ cu_seqlens_k: torch.Tensor,
690
+ max_seqlen_q: torch.Tensor,
691
+ max_seqlen_k: torch.Tensor,
692
+ sm_scale: float,
693
+ ):
694
+ q_len, num_q_heads, head_dim = q.shape
695
+ k_len, num_k_heads, head_dim = k.shape
696
+ v_len, num_v_heads, head_dim = v.shape
697
+ o_len, num_o_heads, head_dim = o.shape
698
+ num_share_q_heads = num_q_heads // num_k_heads
699
+ # compute D
700
+ delta = torch.zeros([num_o_heads, o_len], device=o.device, dtype=torch.float32)
701
+ grid = lambda META: (triton.cdiv(o_len, META["BLOCK_SIZE_O"]), num_o_heads)
702
+ BLOCK_SIZE_O = 256
703
+ BLOCK_SIZE_D = triton.next_power_of_2(head_dim)
704
+ num_warps, num_stages = get_num_warps_stages(head_dim, BLOCK_SIZE_O, IS_HOPPER_GPU)
705
+ backward_sum_o_do[grid](
706
+ o,
707
+ do,
708
+ delta,
709
+ o_len,
710
+ head_dim,
711
+ o.stride(0),
712
+ o.stride(1),
713
+ o.stride(2),
714
+ do.stride(0),
715
+ do.stride(1),
716
+ do.stride(2),
717
+ delta.stride(0),
718
+ delta.stride(1),
719
+ BLOCK_SIZE_O=BLOCK_SIZE_O,
720
+ BLOCK_SIZE_D=BLOCK_SIZE_D,
721
+ num_warps=num_warps,
722
+ num_stages=num_stages,
723
+ )
724
+ # compute dk dv
725
+ dk = torch.zeros(
726
+ num_share_q_heads, k_len, num_k_heads, head_dim, device=k.device, dtype=k.dtype
727
+ )
728
+ dv = torch.zeros(
729
+ num_share_q_heads, k_len, num_k_heads, head_dim, device=k.device, dtype=k.dtype
730
+ )
731
+ batch_size = cu_seqlens_q.shape[0] - 1
732
+ grid = lambda META: (
733
+ batch_size,
734
+ num_q_heads,
735
+ triton.cdiv(max_seqlen_k, META["BLOCK_SIZE_K"]),
736
+ )
737
+ BLOCK_SIZE_Q = 64
738
+ BLOCK_SIZE_K = 128
739
+ BLOCK_SIZE_D = triton.next_power_of_2(head_dim)
740
+ num_warps, num_stages = get_num_warps_stages(head_dim, BLOCK_SIZE_K, IS_HOPPER_GPU)
741
+ backward_dkdv[grid](
742
+ q,
743
+ k,
744
+ v,
745
+ lse,
746
+ delta,
747
+ do,
748
+ dk,
749
+ dv,
750
+ kernel_size,
751
+ kernel_stride,
752
+ cu_seqlens_q,
753
+ cu_seqlens_k,
754
+ num_k_heads,
755
+ num_share_q_heads,
756
+ head_dim,
757
+ sm_scale,
758
+ q.stride(0),
759
+ q.stride(1),
760
+ q.stride(2),
761
+ k.stride(0),
762
+ k.stride(1),
763
+ k.stride(2),
764
+ v.stride(0),
765
+ v.stride(1),
766
+ v.stride(2),
767
+ lse.stride(0),
768
+ lse.stride(1),
769
+ delta.stride(0),
770
+ delta.stride(1),
771
+ do.stride(0),
772
+ do.stride(1),
773
+ do.stride(2),
774
+ dk.stride(0),
775
+ dk.stride(1),
776
+ dk.stride(2),
777
+ dk.stride(3),
778
+ dv.stride(0),
779
+ dv.stride(1),
780
+ dv.stride(2),
781
+ dv.stride(3),
782
+ BLOCK_SIZE_Q=BLOCK_SIZE_Q,
783
+ BLOCK_SIZE_K=BLOCK_SIZE_K,
784
+ BLOCK_SIZE_D=BLOCK_SIZE_D,
785
+ num_warps=num_warps,
786
+ num_stages=num_stages,
787
+ )
788
+ dk = dk.sum(0)
789
+ dv = dv.sum(0)
790
+ # compute dq
791
+ dq = torch.zeros_like(q)
792
+ grid = lambda META: (
793
+ batch_size,
794
+ num_q_heads,
795
+ triton.cdiv(max_seqlen_q, META["BLOCK_SIZE_Q"]),
796
+ )
797
+ BLOCK_SIZE_Q = 128
798
+ BLOCK_SIZE_K = 64
799
+ num_warps, num_stages = get_num_warps_stages(head_dim, BLOCK_SIZE_Q, IS_HOPPER_GPU)
800
+ backward_dq[grid](
801
+ q,
802
+ k,
803
+ v,
804
+ lse,
805
+ delta,
806
+ do,
807
+ dq,
808
+ kernel_size,
809
+ kernel_stride,
810
+ cu_seqlens_q,
811
+ cu_seqlens_k,
812
+ num_k_heads,
813
+ num_share_q_heads,
814
+ head_dim,
815
+ sm_scale,
816
+ q.stride(0),
817
+ q.stride(1),
818
+ q.stride(2),
819
+ k.stride(0),
820
+ k.stride(1),
821
+ k.stride(2),
822
+ v.stride(0),
823
+ v.stride(1),
824
+ v.stride(2),
825
+ lse.stride(0),
826
+ lse.stride(1),
827
+ delta.stride(0),
828
+ delta.stride(1),
829
+ do.stride(0),
830
+ do.stride(1),
831
+ do.stride(2),
832
+ dq.stride(0),
833
+ dq.stride(1),
834
+ dq.stride(2),
835
+ BLOCK_SIZE_Q=BLOCK_SIZE_Q,
836
+ BLOCK_SIZE_K=BLOCK_SIZE_K,
837
+ BLOCK_SIZE_D=BLOCK_SIZE_D,
838
+ num_warps=num_warps,
839
+ num_stages=num_stages,
840
+ )
841
+ return dq, dk, dv
842
+
843
+
844
+ class CompressedAttention(torch.autograd.Function):
845
+ @staticmethod
846
+ def forward(
847
+ ctx,
848
+ q: torch.Tensor,
849
+ k: torch.Tensor,
850
+ v: torch.Tensor,
851
+ kernel_size: int,
852
+ kernel_stride: int,
853
+ cu_seqlens_q: torch.Tensor,
854
+ cu_seqlens_k: torch.Tensor,
855
+ max_seqlen_q: torch.Tensor,
856
+ max_seqlen_k: torch.Tensor,
857
+ sm_scale=None,
858
+ ):
859
+ # dtype check
860
+ assert q.dtype == torch.bfloat16 or q.dtype == torch.float16
861
+ assert q.dtype == k.dtype and k.dtype == v.dtype
862
+ assert cu_seqlens_q.dtype == torch.int32 and cu_seqlens_k.dtype == torch.int32
863
+ # softmax scale
864
+ if sm_scale is None:
865
+ sm_scale = 1 / math.sqrt(q.shape[-1])
866
+ o, lse = _compressed_attention_fwd(
867
+ q,
868
+ k,
869
+ v,
870
+ kernel_size,
871
+ kernel_stride,
872
+ cu_seqlens_q,
873
+ cu_seqlens_k,
874
+ max_seqlen_q,
875
+ max_seqlen_k,
876
+ sm_scale,
877
+ )
878
+ ctx.save_for_backward(q, k, v, o, lse, cu_seqlens_q, cu_seqlens_k)
879
+ ctx.sm_scale = sm_scale
880
+ ctx.max_seqlen_q = max_seqlen_q
881
+ ctx.max_seqlen_k = max_seqlen_k
882
+ ctx.kernel_size = kernel_size
883
+ ctx.kernel_stride = kernel_stride
884
+ return o, lse
885
+
886
+ @staticmethod
887
+ def backward(ctx, do: torch.Tensor, *args) -> Any:
888
+ q, k, v, o, lse, cu_seqlens_q, cu_seqlens_k = ctx.saved_tensors
889
+ max_seqlen_q = ctx.max_seqlen_q
890
+ max_seqlen_k = ctx.max_seqlen_k
891
+ sm_scale = ctx.sm_scale
892
+ kernel_size = ctx.kernel_size
893
+ kernel_stride = ctx.kernel_stride
894
+ dq, dk, dv = _compressed_attention_bwd(
895
+ o,
896
+ do,
897
+ lse,
898
+ q,
899
+ k,
900
+ v,
901
+ kernel_size,
902
+ kernel_stride,
903
+ cu_seqlens_q,
904
+ cu_seqlens_k,
905
+ max_seqlen_q,
906
+ max_seqlen_k,
907
+ sm_scale,
908
+ )
909
+ return dq, dk, dv, None, None, None, None, None, None, None
910
+
911
+
912
+ @triton.jit
913
+ def score_kernel(
914
+ q_ptr,
915
+ k_ptr,
916
+ lse_ptr,
917
+ s_ptr,
918
+ kernel_size,
919
+ kernel_stride,
920
+ # seqlens
921
+ cu_seqlens_q,
922
+ cu_seqlens_k,
923
+ # shape
924
+ NUM_KV_HEADS,
925
+ NUM_SHARE_Q_HEADS,
926
+ HEAD_DIM,
927
+ # sm_scale
928
+ sm_scale,
929
+ # stride
930
+ stride_qn,
931
+ stride_qh,
932
+ stride_qd,
933
+ stride_kn,
934
+ stride_kh,
935
+ stride_kd,
936
+ stride_lh,
937
+ stride_ln,
938
+ stride_sh,
939
+ stride_sq,
940
+ stride_sk,
941
+ # META parameters
942
+ BLOCK_SIZE_Q: tl.constexpr, # q block size
943
+ BLOCK_SIZE_K: tl.constexpr, # k block size
944
+ BLOCK_SIZE_D: tl.constexpr,
945
+ ):
946
+ qk_scale = sm_scale * 1.44269504
947
+ # get batch id and head id
948
+ pid_bkh = tl.program_id(0)
949
+ pid_b = pid_bkh // NUM_KV_HEADS
950
+ pid_kh = pid_bkh % NUM_KV_HEADS
951
+ pid_q = tl.program_id(1)
952
+ pid_k = tl.program_id(2)
953
+ # get q k start and len after rmpad
954
+ q_start = tl.load(cu_seqlens_q + pid_b)
955
+ q_len = tl.load(cu_seqlens_q + pid_b + 1) - q_start
956
+ k_start = tl.load(cu_seqlens_k + pid_b)
957
+ k_len = tl.load(cu_seqlens_k + pid_b + 1) - k_start
958
+ if pid_q * BLOCK_SIZE_Q >= q_len or pid_k * BLOCK_SIZE_K >= k_len:
959
+ return
960
+ # init k pointer and load k
961
+ k_ptrs = tl.make_block_ptr(
962
+ base=k_ptr + k_start * stride_kn + pid_kh * stride_kh,
963
+ shape=(HEAD_DIM, k_len),
964
+ strides=(stride_kd, stride_kn),
965
+ offsets=(0, pid_k * BLOCK_SIZE_K),
966
+ block_shape=(BLOCK_SIZE_D, BLOCK_SIZE_K),
967
+ order=(0, 1),
968
+ )
969
+ k = tl.load(k_ptrs, boundary_check=(0, 1), padding_option="zero")
970
+ # offsets
971
+ off_q = tl.arange(0, BLOCK_SIZE_Q) + pid_q * BLOCK_SIZE_Q
972
+ off_k = tl.arange(0, BLOCK_SIZE_K) + pid_k * BLOCK_SIZE_K
973
+ causal_mask = off_q[:, None] >= (off_k * kernel_stride + kernel_size - 1)[None, :]
974
+ # init score
975
+ s = tl.zeros((BLOCK_SIZE_Q, BLOCK_SIZE_K), dtype=tl.float32)
976
+ # loop over gqa heads
977
+ for h in range(NUM_SHARE_Q_HEADS):
978
+ pid_h = pid_kh * NUM_SHARE_Q_HEADS + h
979
+ q_ptrs = tl.make_block_ptr(
980
+ base=q_ptr + q_start * stride_qn + pid_h * stride_qh,
981
+ shape=(q_len, HEAD_DIM),
982
+ strides=(stride_qn, stride_qd),
983
+ offsets=(pid_q * BLOCK_SIZE_Q, 0),
984
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_D),
985
+ order=(1, 0),
986
+ )
987
+ lse_ptrs = tl.make_block_ptr(
988
+ base=lse_ptr + q_start * stride_ln + pid_h * stride_lh,
989
+ shape=(q_len, 1),
990
+ strides=(stride_ln, stride_lh),
991
+ offsets=(pid_q * BLOCK_SIZE_Q, 0),
992
+ block_shape=(BLOCK_SIZE_Q, 1),
993
+ order=(0, 1),
994
+ )
995
+ # load q and lse
996
+ q = tl.load(q_ptrs, boundary_check=(0, 1), padding_option="zero")
997
+ lse = tl.load(lse_ptrs, boundary_check=(0, 1), padding_option="zero")
998
+ # compute qk
999
+ qk = tl.zeros((BLOCK_SIZE_Q, BLOCK_SIZE_K), dtype=tl.float32)
1000
+ qk += tl.dot(q, k) * qk_scale
1001
+ # compute score
1002
+ s += tl.where(causal_mask, tl.exp2(qk - lse), 0)
1003
+ # save output
1004
+ s_ptrs = tl.make_block_ptr(
1005
+ base=s_ptr + pid_kh * stride_sh + q_start * stride_sq,
1006
+ shape=(q_len, k_len),
1007
+ strides=(stride_sq, stride_sk),
1008
+ offsets=(pid_q * BLOCK_SIZE_Q, pid_k * BLOCK_SIZE_K),
1009
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_K),
1010
+ order=(1, 0),
1011
+ )
1012
+ tl.store(s_ptrs, s.to(s_ptr.dtype.element_ty), boundary_check=(0, 1))
1013
+
1014
+
1015
+ def _get_attention_score(
1016
+ q: torch.Tensor, # [total_query_len, num_q_heads, head_dim]
1017
+ k: torch.Tensor, # [total_key_len, num_k_heads, head_dim]
1018
+ lse: torch.Tensor, # [num_q_heads, total_query_len]
1019
+ kernel_size: int,
1020
+ kernel_stride: int,
1021
+ cu_seqlens_q: torch.Tensor,
1022
+ cu_seqlens_k: torch.Tensor,
1023
+ max_seqlen_q: int,
1024
+ max_seqlen_k: int,
1025
+ sm_scale: float,
1026
+ ) -> torch.Tensor:
1027
+ # dtype check
1028
+ assert q.dtype == torch.bfloat16 or q.dtype == torch.float16
1029
+ assert q.dtype == k.dtype
1030
+ assert cu_seqlens_q.dtype == torch.int32 and cu_seqlens_k.dtype == torch.int32
1031
+ assert (
1032
+ lse.dtype == torch.float32
1033
+ ) # lse here is log2(sum(exp(qk*scale))), not log(sum(exp(qk*scale)))
1034
+ # shape
1035
+ q_len, num_q_heads, head_dim = q.shape
1036
+ k_len, num_k_heads, head_dim = k.shape
1037
+ batch_size = cu_seqlens_q.shape[0] - 1
1038
+ assert q_len > k_len
1039
+ if sm_scale is None:
1040
+ sm_scale = 1 / math.sqrt(head_dim)
1041
+ # gqa
1042
+ assert num_q_heads % num_k_heads == 0
1043
+ num_share_q_heads = num_q_heads // num_k_heads
1044
+ # init score
1045
+ score = torch.zeros(
1046
+ num_k_heads, q_len, max_seqlen_k, dtype=torch.float32, device=q.device
1047
+ )
1048
+ # launch kernel
1049
+ grid = lambda META: (
1050
+ batch_size * num_k_heads,
1051
+ triton.cdiv(max_seqlen_q, META["BLOCK_SIZE_Q"]),
1052
+ triton.cdiv(max_seqlen_k, META["BLOCK_SIZE_K"]),
1053
+ )
1054
+ BLOCK_SIZE_Q = 128
1055
+ BLOCK_SIZE_K = 128
1056
+ BLOCK_SIZE_D = triton.next_power_of_2(head_dim)
1057
+ score_kernel[grid](
1058
+ q,
1059
+ k,
1060
+ lse,
1061
+ score,
1062
+ kernel_size,
1063
+ kernel_stride,
1064
+ cu_seqlens_q,
1065
+ cu_seqlens_k,
1066
+ num_k_heads,
1067
+ num_share_q_heads,
1068
+ head_dim,
1069
+ sm_scale,
1070
+ q.stride(0),
1071
+ q.stride(1),
1072
+ q.stride(2),
1073
+ k.stride(0),
1074
+ k.stride(1),
1075
+ k.stride(2),
1076
+ lse.stride(0),
1077
+ lse.stride(1),
1078
+ score.stride(0),
1079
+ score.stride(1),
1080
+ score.stride(2),
1081
+ BLOCK_SIZE_Q=BLOCK_SIZE_Q,
1082
+ BLOCK_SIZE_K=BLOCK_SIZE_K,
1083
+ BLOCK_SIZE_D=BLOCK_SIZE_D,
1084
+ num_warps=8,
1085
+ num_stages=3,
1086
+ )
1087
+ return score
1088
+
1089
+
1090
+ @triton.jit
1091
+ def _transform_score_kernel(
1092
+ s_ptr, # score, shape: [num_heads, q_len, k_len]
1093
+ bs_ptr, # block wise score: [num_heads, q_len, num_k_block]
1094
+ offs,
1095
+ cu_seqlens_q,
1096
+ # shape
1097
+ num_heads,
1098
+ num_offs,
1099
+ max_k_len,
1100
+ max_blocks,
1101
+ pad_len,
1102
+ # kernel & block size
1103
+ block_size,
1104
+ block_stride, # block_size // kernel_stride
1105
+ init_blocks,
1106
+ local_blocks,
1107
+ # stride
1108
+ stride_sh,
1109
+ stride_sq,
1110
+ stride_sk,
1111
+ stride_bsh,
1112
+ stride_bsq,
1113
+ stride_bsk,
1114
+ BLOCK_SIZE_Q: tl.constexpr,
1115
+ BLOCK_SIZE_K: tl.constexpr,
1116
+ BLOCK_SIZE_O: tl.constexpr,
1117
+ ):
1118
+ pid_bh = tl.program_id(0)
1119
+ pid_b = pid_bh // num_heads
1120
+ pid_h = pid_bh % num_heads
1121
+ pid_q = tl.program_id(1)
1122
+ pid_k = tl.program_id(2)
1123
+ q_start = tl.load(cu_seqlens_q + pid_b)
1124
+ q_len = tl.load(cu_seqlens_q + pid_b + 1) - q_start
1125
+ k_start = pid_k * BLOCK_SIZE_K
1126
+ if pid_q * BLOCK_SIZE_Q >= q_len:
1127
+ return
1128
+ # load weight
1129
+ off_o = tl.arange(0, BLOCK_SIZE_O)
1130
+ w = tl.load(offs + off_o, mask=off_o < num_offs, other=0)
1131
+ # load score
1132
+ off_q = pid_q * BLOCK_SIZE_Q + tl.arange(0, BLOCK_SIZE_Q)
1133
+ off_k = (k_start + tl.arange(0, BLOCK_SIZE_K)) * block_stride - pad_len
1134
+ off_k = off_k[None, :] + off_o[:, None]
1135
+ s_ptrs = (
1136
+ s_ptr
1137
+ + q_start * stride_sq
1138
+ + pid_h * stride_sh
1139
+ + off_q[:, None, None] * stride_sq
1140
+ + off_k[None, :, :] * stride_sk
1141
+ )
1142
+ # weighted sum, [BQ, BO, BK] * [1, BO, 1] -> [BQ, BO, BK] -> [BQ, BK]
1143
+ s = tl.load(
1144
+ s_ptrs,
1145
+ mask=(off_q < q_len)[:, None, None] & (off_k >= 0) & (off_k < max_k_len),
1146
+ other=0,
1147
+ )
1148
+ s = s * w[None, :, None]
1149
+ # s = tl.sum(s, axis=1)
1150
+ s = tl.max(s, axis=1)
1151
+ # init mask and local mask
1152
+ off_bq = off_q // block_size
1153
+ off_bk = tl.arange(0, BLOCK_SIZE_K)
1154
+
1155
+ s = tl.where(
1156
+ # For local blocks: set to negative infinity (exclude from topk)
1157
+ (off_bq[:, None] >= (off_bk + k_start)[None, :]) & (off_bq[:, None] < (off_bk + k_start)[None, :] + local_blocks),
1158
+ float("-inf"),
1159
+ s,
1160
+ )
1161
+
1162
+ # Keep the original conditions for init_blocks and query location as infinity
1163
+ s = tl.where(
1164
+ (off_bk[None, :] < init_blocks - k_start)
1165
+ # Force blocks where the query is located to have infinite score (always include in topk)
1166
+ | (off_bq[:, None] == (off_bk + k_start)[None, :]),
1167
+ float("inf"),
1168
+ s,
1169
+ )
1170
+ # store block wise score
1171
+ bs_ptrs = (
1172
+ bs_ptr
1173
+ + q_start * stride_bsq
1174
+ + k_start * stride_bsk
1175
+ + pid_h * stride_bsh
1176
+ + off_q[:, None] * stride_bsq
1177
+ + off_bk[None, :] * stride_bsk
1178
+ )
1179
+ tl.store(
1180
+ bs_ptrs,
1181
+ s,
1182
+ mask=(off_q < q_len)[:, None] & (off_bk < max_blocks - k_start)[None, :],
1183
+ )
1184
+
1185
+
1186
+ def transform_score(
1187
+ score: torch.Tensor,
1188
+ kernel_size: int,
1189
+ kernel_stride: int,
1190
+ block_size: int,
1191
+ cu_seqlens_q: torch.Tensor,
1192
+ cu_seqlens_k: torch.Tensor,
1193
+ max_seqlen_q: int,
1194
+ max_seqlen_k: int,
1195
+ init_blocks: int = 1,
1196
+ local_blocks: int = 2,
1197
+ ) -> torch.Tensor:
1198
+ num_k_heads, total_query_len, max_key_len = score.shape
1199
+ batch_size = cu_seqlens_q.shape[0] - 1
1200
+ pad_len = kernel_size // kernel_stride - 1
1201
+ max_blocks = math.ceil(max_seqlen_q / block_size)
1202
+ block_score = torch.zeros(
1203
+ num_k_heads,
1204
+ total_query_len,
1205
+ max_blocks,
1206
+ dtype=torch.float32,
1207
+ device=score.device,
1208
+ )
1209
+ offs = (
1210
+ torch.arange(kernel_size // kernel_stride, device=score.device)[:, None]
1211
+ + torch.arange(block_size // kernel_stride, device=score.device)[None, :]
1212
+ ).view(-1)
1213
+ offs = torch.histc(offs, bins=offs.max() + 1, min=0, max=offs.max())
1214
+ num_offs = int(offs.shape[0])
1215
+ BLOCK_SIZE_K = min(128, triton.next_power_of_2(max_blocks))
1216
+ BLOCK_SIZE_O = triton.next_power_of_2(num_offs)
1217
+ BLOCK_SIZE_Q = 8
1218
+ grid = (
1219
+ num_k_heads * batch_size,
1220
+ triton.cdiv(total_query_len, BLOCK_SIZE_Q),
1221
+ triton.cdiv(max_blocks, BLOCK_SIZE_K),
1222
+ )
1223
+ _transform_score_kernel[grid](
1224
+ score,
1225
+ block_score,
1226
+ torch.ones_like(offs, dtype = offs.dtype, device = offs.device),
1227
+ cu_seqlens_q,
1228
+ num_k_heads,
1229
+ offs.shape[0],
1230
+ max_key_len,
1231
+ max_blocks,
1232
+ pad_len,
1233
+ block_size,
1234
+ block_size // kernel_stride,
1235
+ init_blocks,
1236
+ local_blocks,
1237
+ score.stride(0),
1238
+ score.stride(1),
1239
+ score.stride(2),
1240
+ block_score.stride(0),
1241
+ block_score.stride(1),
1242
+ block_score.stride(2),
1243
+ BLOCK_SIZE_Q=BLOCK_SIZE_Q,
1244
+ BLOCK_SIZE_K=BLOCK_SIZE_K,
1245
+ BLOCK_SIZE_O=BLOCK_SIZE_O,
1246
+ num_warps=8,
1247
+ num_stages=3,
1248
+ )
1249
+ return block_score
1250
+
1251
+
1252
+ def compressed_attention(
1253
+ q: torch.Tensor,
1254
+ k: torch.Tensor,
1255
+ v: torch.Tensor,
1256
+ kernel_size: int,
1257
+ kernel_stride: int,
1258
+ block_size: int,
1259
+ topk: int,
1260
+ cu_seqlens_q: torch.Tensor,
1261
+ cu_seqlens_k: torch.Tensor,
1262
+ max_seqlen_q: int,
1263
+ max_seqlen_k: int,
1264
+ sm_scale: float = None,
1265
+ init_blocks: int = 1,
1266
+ local_blocks: int = 2,
1267
+ parallel_topk_compute: Union[str, bool] = "auto",
1268
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
1269
+ """Attention between query and compressed key and value. Compute attention output and topk block idx used in topk_sparse_attention.
1270
+
1271
+ Args:
1272
+ q (torch.Tensor): shape [total_q_len, num_q_heads, head_dim]
1273
+ k (torch.Tensor): shape [total_kv_len, num_kv_heads, head_dim]
1274
+ v (torch.Tensor): shape [total_kv_len, num_kv_heads, head_dim]
1275
+ kernel_size (int): kernel size in compress_key_value
1276
+ kernel_stride (int): stride of compress_key_value
1277
+ block_size (int): key value block size for topk sparse attention.
1278
+ topk (int): number of blocks for each query.
1279
+ cu_seqlens_q (torch.Tensor): shape [batch_size + 1], similar to cu_seqlens_q in flash_attn_func_varlen.
1280
+ cu_seqlens_k (torch.Tensor): shape [batch_size + 1], similar to cu_seqlens_k in flash_attn_func_varlen.
1281
+ max_seqlen_q (int): max q len of the batch.
1282
+ max_seqlen_k (int): max k len of the batch.
1283
+ sm_scale (float, optional): softmax scale. Defaults to None, means 1/sqrt(head_dim).
1284
+ init_blocks (int, optional): Number of init blocks for each query. Defaults to 1.
1285
+ local_blocks (int, optional): Number of local blocks for each query. Defaults to 2.
1286
+ parallel_topk_compute (str, optional): Only set it to False when the sequence length is too long. This can avoid a current bug.
1287
+ We'll fix this issue later. Defaults to auto, it will be set to False when the sequence length is greater than 32k and True otherwise.
1288
+
1289
+ Returns:
1290
+ Tuple[torch.Tensor, torch.Tensor]: attention output and topk_idx used in topk_sparse_attention
1291
+ """
1292
+ if max_seqlen_q is None:
1293
+ max_seqlen_q = (cu_seqlens_q[1:] - cu_seqlens_q[:-1]).max().item()
1294
+ if max_seqlen_k is None:
1295
+ max_seqlen_k = (cu_seqlens_k[1:] - cu_seqlens_k[:-1]).max().item()
1296
+ attn_output, lse = CompressedAttention.apply(
1297
+ q,
1298
+ k,
1299
+ v,
1300
+ kernel_size,
1301
+ kernel_stride,
1302
+ cu_seqlens_q,
1303
+ cu_seqlens_k,
1304
+ max_seqlen_q,
1305
+ max_seqlen_k,
1306
+ sm_scale,
1307
+ )
1308
+
1309
+ # do not select topk index
1310
+ if topk <= 0:
1311
+ warnings.warn("topk <= 0, returned topk_idx will be None")
1312
+ return attn_output, None
1313
+
1314
+ assert topk >= init_blocks #+ local_blocks
1315
+ with torch.no_grad():
1316
+ num_k_heads, num_q_heads = k.shape[1], q.shape[1]
1317
+ num_shared_q_heads = num_q_heads // num_k_heads
1318
+ batch_size = cu_seqlens_q.shape[0] - 1
1319
+ q_idx = torch.cat(
1320
+ [
1321
+ torch.arange(cu_seqlens_q[i + 1] - cu_seqlens_q[i], device=q.device)
1322
+ for i in range(batch_size)
1323
+ ],
1324
+ dim=0,
1325
+ )
1326
+ q_idx = q_idx // block_size
1327
+ # whether to use parallel version
1328
+ if parallel_topk_compute == "auto":
1329
+ parallel_topk_compute = cu_seqlens_q[-1] <= 32768
1330
+ # parallel version
1331
+ if parallel_topk_compute:
1332
+ # recompute score
1333
+ score = _get_attention_score(
1334
+ q,
1335
+ k,
1336
+ lse,
1337
+ kernel_size,
1338
+ kernel_stride,
1339
+ cu_seqlens_q,
1340
+ cu_seqlens_k,
1341
+ max_seqlen_q,
1342
+ max_seqlen_k,
1343
+ sm_scale,
1344
+ )
1345
+ # transform score to block-wise score
1346
+ score = transform_score(
1347
+ score,
1348
+ kernel_size,
1349
+ kernel_stride,
1350
+ block_size,
1351
+ cu_seqlens_q,
1352
+ cu_seqlens_k,
1353
+ max_seqlen_q,
1354
+ max_seqlen_k,
1355
+ init_blocks,
1356
+ local_blocks,
1357
+ )
1358
+ # get topk
1359
+ topk = min(topk, score.shape[-1])
1360
+ topk_idx = score.topk(topk, dim=-1).indices.sort(-1).values
1361
+ # print(cu_seqlens_q)
1362
+ # breakpoint()
1363
+ topk_idx[topk_idx >= q_idx[None, :, None]] = -1
1364
+ topk_idx = topk_idx.to(torch.int32)
1365
+ # non parallel version, avoid some current bugs when sequence length is too long
1366
+ # FIXME: need to fix later
1367
+ else:
1368
+ topk_idx_list = []
1369
+ for h in range(num_k_heads):
1370
+ # recompute score
1371
+ score = _get_attention_score(
1372
+ q[:, h * num_shared_q_heads : (h + 1) * num_shared_q_heads],
1373
+ k[:, h : h + 1],
1374
+ lse[h * num_shared_q_heads : (h + 1) * num_shared_q_heads],
1375
+ kernel_size,
1376
+ kernel_stride,
1377
+ cu_seqlens_q,
1378
+ cu_seqlens_k,
1379
+ max_seqlen_q,
1380
+ max_seqlen_k,
1381
+ sm_scale,
1382
+ )
1383
+ # transform score to block-wise score
1384
+ score = transform_score(
1385
+ score,
1386
+ kernel_size,
1387
+ kernel_stride,
1388
+ block_size,
1389
+ cu_seqlens_q,
1390
+ cu_seqlens_k,
1391
+ max_seqlen_q,
1392
+ max_seqlen_k,
1393
+ init_blocks,
1394
+ local_blocks,
1395
+ )
1396
+ # get topk
1397
+ topk = min(topk, score.shape[-1])
1398
+ topk_idx = score.topk(topk, dim=-1).indices.sort(-1).values
1399
+ topk_idx[topk_idx >= q_idx[None, :, None]] = -1
1400
+ topk_idx = topk_idx.to(torch.int32)
1401
+ topk_idx_list.append(topk_idx)
1402
+ topk_idx = torch.cat(topk_idx_list, dim=0)
1403
+ return attn_output, topk_idx
1404
+
config.json ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "openbmb/CPM-2B",
3
+ "architectures": [
4
+ "SparseLlamaForCausalLM"
5
+ ],
6
+ "auto_map": {
7
+ "AutoModelForCausalLM": "modeling_llama_long_infllmv2.SparseLlamaForCausalLM"
8
+ },
9
+ "bos_token_id": 1,
10
+ "eos_token_id": [2,73440],
11
+ "pad_token_id": 2,
12
+ "hidden_act": "silu",
13
+ "hidden_size": 4096,
14
+ "initializer_range": 0.1,
15
+ "intermediate_size": 16384,
16
+ "head_dim": 128,
17
+ "max_position_embeddings": 32768,
18
+ "num_attention_heads": 32,
19
+ "num_hidden_layers": 32,
20
+ "model_type": "llama",
21
+ "num_key_value_heads": 2,
22
+ "rms_norm_eps": 1e-06,
23
+ "rope_scaling": {
24
+ "rope_type": "longrope",
25
+ "attention_factor": 1.0,
26
+ "long_factor": [0.9977997200264581, 1.014658295992452, 1.0349680404997148, 1.059429246056193, 1.0888815016813513, 1.1243301355211495, 1.166977103606075, 1.2182568066927284, 1.2798772354275727, 1.3538666751582975, 1.4426259039919596, 1.5489853358570191, 1.6762658237220625, 1.8283407612492941, 2.0096956085876183, 2.225478927469756, 2.481536379650452, 2.784415934557119, 3.1413289096347365, 3.560047844772632, 4.048719380066383, 4.615569542115128, 5.2684819496549835, 6.014438591970396, 6.858830049237097, 7.804668263503327, 8.851768731513417, 9.99600492938444, 11.228766118181639, 12.536757560834843, 13.902257701387796, 15.303885189125953, 16.717837610115794, 18.119465097853947, 19.484965238406907, 20.792956681060105, 22.02571786985731, 23.16995406772833, 24.217054535738416, 25.16289275000465, 26.007284207271347, 26.753240849586767, 27.40615325712662, 27.973003419175363, 28.461674954469114, 28.880393889607006, 29.237306864684626, 29.540186419591297, 29.79624387177199, 30.01202719065413, 30.193382037992453, 30.34545697551969, 30.47273746338473, 30.579096895249787, 30.66785612408345, 30.741845563814174, 30.80346599254902, 30.85474569563567, 30.897392663720595, 30.932841297560394, 30.962293553185553, 30.986754758742034, 31.007064503249293, 31.02392307921529],
27
+ "short_factor": [0.9977997200264581, 1.014658295992452, 1.0349680404997148, 1.059429246056193, 1.0888815016813513, 1.1243301355211495, 1.166977103606075, 1.2182568066927284, 1.2798772354275727, 1.3538666751582975, 1.4426259039919596, 1.5489853358570191, 1.6762658237220625, 1.8283407612492941, 2.0096956085876183, 2.225478927469756, 2.481536379650452, 2.784415934557119, 3.1413289096347365, 3.560047844772632, 4.048719380066383, 4.615569542115128, 5.2684819496549835, 6.014438591970396, 6.858830049237097, 7.804668263503327, 8.851768731513417, 9.99600492938444, 11.228766118181639, 12.536757560834843, 13.902257701387796, 15.303885189125953, 16.717837610115794, 18.119465097853947, 19.484965238406907, 20.792956681060105, 22.02571786985731, 23.16995406772833, 24.217054535738416, 25.16289275000465, 26.007284207271347, 26.753240849586767, 27.40615325712662, 27.973003419175363, 28.461674954469114, 28.880393889607006, 29.237306864684626, 29.540186419591297, 29.79624387177199, 30.01202719065413, 30.193382037992453, 30.34545697551969, 30.47273746338473, 30.579096895249787, 30.66785612408345, 30.741845563814174, 30.80346599254902, 30.85474569563567, 30.897392663720595, 30.932841297560394, 30.962293553185553, 30.986754758742034, 31.007064503249293, 31.02392307921529],
28
+ "original_max_position_embeddings": 32768
29
+ },
30
+ "rope_theta": 10000.0,
31
+ "torch_dtype": "bfloat16",
32
+ "transformers_version": "4.36.0",
33
+ "use_cache": false,
34
+ "vocab_size": 73448
35
+ }
configuration.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"framework":"Pytorch","task":"text-generation"}
configuration_minicpm.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 The OpenBMB Team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """ MiniCPM model configuration"""
16
+
17
+ from transformers.configuration_utils import PretrainedConfig
18
+ from transformers.utils import logging
19
+
20
+ logger = logging.get_logger(__name__)
21
+
22
+ MINICPM_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
23
+
24
+
25
+ class MiniCPMConfig(PretrainedConfig):
26
+ r"""
27
+ This is the configuration class to store the configuration of a [`MiniCPMModel`]. It is used to instantiate an MiniCPM
28
+ model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
29
+ defaults will yield a similar configuration to that of the MiniCPM-7B.
30
+
31
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
32
+ documentation from [`PretrainedConfig`] for more information.
33
+
34
+
35
+ Args:
36
+ vocab_size (`int`, *optional*, defaults to 32000):
37
+ Vocabulary size of the MiniCPM model. Defines the number of different tokens that can be represented by the
38
+ `inputs_ids` passed when calling [`MiniCPMModel`]
39
+ hidden_size (`int`, *optional*, defaults to 4096):
40
+ Dimension of the hidden representations.
41
+ intermediate_size (`int`, *optional*, defaults to 11008):
42
+ Dimension of the MLP representations.
43
+ num_hidden_layers (`int`, *optional*, defaults to 32):
44
+ Number of hidden layers in the Transformer decoder.
45
+ num_attention_heads (`int`, *optional*, defaults to 32):
46
+ Number of attention heads for each attention layer in the Transformer decoder.
47
+ num_key_value_heads (`int`, *optional*):
48
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
49
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
50
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
51
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
52
+ by meanpooling all the original heads within that group. For more details checkout [this
53
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
54
+ `num_attention_heads`.
55
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
56
+ The non-linear activation function (function or string) in the decoder.
57
+ max_position_embeddings (`int`, *optional*, defaults to 2048):
58
+ The maximum sequence length that this model might ever be used with. MiniCPM 1 supports up to 2048 tokens,
59
+ MiniCPM 2 up to 4096, CodeMiniCPM up to 16384.
60
+ initializer_range (`float`, *optional*, defaults to 0.02):
61
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
62
+ rms_norm_eps (`float`, *optional*, defaults to 1e-06):
63
+ The epsilon used by the rms normalization layers.
64
+ use_cache (`bool`, *optional*, defaults to `True`):
65
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
66
+ relevant if `config.is_decoder=True`.
67
+ pad_token_id (`int`, *optional*):
68
+ Padding token id.
69
+ bos_token_id (`int`, *optional*, defaults to 1):
70
+ Beginning of stream token id.
71
+ eos_token_id (`int`, *optional*, defaults to 2):
72
+ End of stream token id.
73
+ pretraining_tp (`int`, *optional*, defaults to 1):
74
+ Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
75
+ document](https://huggingface.co/docs/transformers/parallelism) to understand more about it. This value is
76
+ necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
77
+ issue](https://github.com/pytorch/pytorch/issues/76232).
78
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
79
+ Whether to tie weight embeddings
80
+ rope_theta (`float`, *optional*, defaults to 10000.0):
81
+ The base period of the RoPE embeddings.
82
+ rope_scaling (`Dict`, *optional*):
83
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
84
+ strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
85
+ `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
86
+ `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
87
+ these scaling strategies behave:
88
+ https://www.reddit.com/r/LocalMiniCPM/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
89
+ experimental feature, subject to breaking API changes in future versions.
90
+ attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
91
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
92
+ attention_dropout (`float`, *optional*, defaults to 0.0):
93
+ The dropout ratio for the attention probabilities.
94
+
95
+ ```python
96
+ >>> from transformers import MiniCPMModel, MiniCPMConfig
97
+
98
+ >>> # Initializing a MiniCPM minicpm-7b style configuration
99
+ >>> configuration = MiniCPMConfig()
100
+
101
+ >>> # Initializing a model from the minicpm-7b style configuration
102
+ >>> model = MiniCPMModel(configuration)
103
+
104
+ >>> # Accessing the model configuration
105
+ >>> configuration = model.config
106
+ ```"""
107
+
108
+ model_type = 'minicpm'
109
+ keys_to_ignore_at_inference = ['past_key_values']
110
+
111
+ def __init__(
112
+ self,
113
+ vocab_size=32000,
114
+ hidden_size=4096,
115
+ intermediate_size=11008,
116
+ num_hidden_layers=32,
117
+ num_attention_heads=32,
118
+ num_key_value_heads=None,
119
+ hidden_act='silu',
120
+ max_position_embeddings=2048,
121
+ initializer_range=0.02,
122
+ rms_norm_eps=1e-6,
123
+ use_cache=True,
124
+ pad_token_id=None,
125
+ bos_token_id=1,
126
+ eos_token_id=2,
127
+ pretraining_tp=1,
128
+ tie_word_embeddings=True,
129
+ rope_theta=10000.0,
130
+ rope_scaling=None,
131
+ attention_bias=False,
132
+ attention_dropout=0.0,
133
+ scale_emb=1,
134
+ dim_model_base=1,
135
+ scale_depth=1,
136
+ mup_denominator=32,
137
+ sparse_config=None,
138
+ **kwargs):
139
+
140
+ self.vocab_size = vocab_size
141
+ self.max_position_embeddings = max_position_embeddings
142
+ self.hidden_size = hidden_size
143
+ self.intermediate_size = intermediate_size
144
+ self.num_hidden_layers = num_hidden_layers
145
+ self.num_attention_heads = num_attention_heads
146
+
147
+ # for backward compatibility
148
+ if num_key_value_heads is None:
149
+ num_key_value_heads = num_attention_heads
150
+
151
+ self.num_key_value_heads = num_key_value_heads
152
+ self.hidden_act = hidden_act
153
+ self.initializer_range = initializer_range
154
+ self.rms_norm_eps = rms_norm_eps
155
+ self.pretraining_tp = pretraining_tp
156
+ self.use_cache = use_cache
157
+ self.rope_theta = rope_theta
158
+ self.rope_scaling = rope_scaling
159
+ # self._rope_scaling_validation()
160
+ self.attention_bias = attention_bias
161
+ self.attention_dropout = attention_dropout
162
+ self.scale_emb = scale_emb
163
+ self.dim_model_base = dim_model_base
164
+ self.scale_depth = scale_depth
165
+ # only used for Eagle Head
166
+ self.mup_denominator = mup_denominator
167
+
168
+ # sparse config
169
+ self.sparse_config = sparse_config
170
+
171
+ super().__init__(
172
+ pad_token_id=pad_token_id,
173
+ bos_token_id=bos_token_id,
174
+ eos_token_id=eos_token_id,
175
+ tie_word_embeddings=tie_word_embeddings,
176
+ **kwargs,
177
+ )
178
+ try:
179
+ import flash_attn
180
+ self._attn_implementation = 'flash_attention_2'
181
+ except:
182
+ pass
183
+
184
+ def _rope_scaling_validation(self):
185
+ """
186
+ Validate the `rope_scaling` configuration.
187
+ """
188
+ if self.rope_scaling is None:
189
+ return
190
+
191
+ if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
192
+ raise ValueError(
193
+ '`rope_scaling` must be a dictionary with with two fields, `type` and `factor`, '
194
+ f'got {self.rope_scaling}'
195
+ )
196
+ rope_scaling_type = self.rope_scaling.get('type', None)
197
+ rope_scaling_factor = self.rope_scaling.get('factor', None)
198
+ if rope_scaling_type is None or rope_scaling_type not in ['linear', 'dynamic']:
199
+ raise ValueError(
200
+ f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
201
+ )
202
+ if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
203
+ raise ValueError(f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}")
generation_config.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 1,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 2,
6
+ 73440
7
+ ],
8
+ "pad_token_id": 2,
9
+ "temperature": 0.8,
10
+ "top_p": 0.8,
11
+ "transformers_version": "4.46.1"
12
+ }
model-00001-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2c31724499e15eec6ce770cf90e43bc995d09bd5e22d6f9f5daa1756e11678dd
3
+ size 5335092440
model-00002-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:996d566f062ca09d8a3f1770483c46fe8adf84c846b753f6aa7ad2d677bd6484
3
+ size 5348158808
model-00003-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0d0b2bb38be19f563bff4a8a7b5be3ee86b79b781f412943e4ddeaef95b028cf
3
+ size 5285226224
model-00004-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:550703f045bdf3832a7a362516f77f70ab87152f77604cc066f4d9cba2ee85bd
3
+ size 402653560
model.safetensors.index.json ADDED
@@ -0,0 +1,491 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_parameters": 8185536640,
4
+ "total_size": 16371073280
5
+ },
6
+ "weight_map": {
7
+ "lm_head.weight": "model-00001-of-00004.safetensors",
8
+ "model.embed_tokens.weight": "model-00001-of-00004.safetensors",
9
+ "model.layers.0.input_layernorm.weight": "model-00001-of-00004.safetensors",
10
+ "model.layers.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
11
+ "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
12
+ "model.layers.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
13
+ "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
14
+ "model.layers.0.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
15
+ "model.layers.0.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
16
+ "model.layers.0.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
17
+ "model.layers.0.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
18
+ "model.layers.0.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
19
+ "model.layers.0.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
20
+ "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
21
+ "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
22
+ "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
23
+ "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
24
+ "model.layers.1.input_layernorm.weight": "model-00001-of-00004.safetensors",
25
+ "model.layers.1.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
26
+ "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
27
+ "model.layers.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
28
+ "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
29
+ "model.layers.1.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
30
+ "model.layers.1.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
31
+ "model.layers.1.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
32
+ "model.layers.1.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
33
+ "model.layers.1.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
34
+ "model.layers.1.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
35
+ "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
36
+ "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
37
+ "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
38
+ "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
39
+ "model.layers.10.input_layernorm.weight": "model-00002-of-00004.safetensors",
40
+ "model.layers.10.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
41
+ "model.layers.10.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
42
+ "model.layers.10.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
43
+ "model.layers.10.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
44
+ "model.layers.10.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
45
+ "model.layers.10.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
46
+ "model.layers.10.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
47
+ "model.layers.10.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
48
+ "model.layers.10.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
49
+ "model.layers.10.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
50
+ "model.layers.10.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
51
+ "model.layers.10.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
52
+ "model.layers.10.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
53
+ "model.layers.10.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
54
+ "model.layers.11.input_layernorm.weight": "model-00002-of-00004.safetensors",
55
+ "model.layers.11.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
56
+ "model.layers.11.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
57
+ "model.layers.11.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
58
+ "model.layers.11.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
59
+ "model.layers.11.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
60
+ "model.layers.11.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
61
+ "model.layers.11.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
62
+ "model.layers.11.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
63
+ "model.layers.11.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
64
+ "model.layers.11.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
65
+ "model.layers.11.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
66
+ "model.layers.11.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
67
+ "model.layers.11.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
68
+ "model.layers.11.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
69
+ "model.layers.12.input_layernorm.weight": "model-00002-of-00004.safetensors",
70
+ "model.layers.12.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
71
+ "model.layers.12.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
72
+ "model.layers.12.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
73
+ "model.layers.12.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
74
+ "model.layers.12.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
75
+ "model.layers.12.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
76
+ "model.layers.12.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
77
+ "model.layers.12.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
78
+ "model.layers.12.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
79
+ "model.layers.12.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
80
+ "model.layers.12.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
81
+ "model.layers.12.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
82
+ "model.layers.12.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
83
+ "model.layers.12.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
84
+ "model.layers.13.input_layernorm.weight": "model-00002-of-00004.safetensors",
85
+ "model.layers.13.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
86
+ "model.layers.13.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
87
+ "model.layers.13.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
88
+ "model.layers.13.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
89
+ "model.layers.13.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
90
+ "model.layers.13.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
91
+ "model.layers.13.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
92
+ "model.layers.13.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
93
+ "model.layers.13.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
94
+ "model.layers.13.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
95
+ "model.layers.13.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
96
+ "model.layers.13.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
97
+ "model.layers.13.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
98
+ "model.layers.13.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
99
+ "model.layers.14.input_layernorm.weight": "model-00002-of-00004.safetensors",
100
+ "model.layers.14.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
101
+ "model.layers.14.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
102
+ "model.layers.14.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
103
+ "model.layers.14.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
104
+ "model.layers.14.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
105
+ "model.layers.14.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
106
+ "model.layers.14.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
107
+ "model.layers.14.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
108
+ "model.layers.14.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
109
+ "model.layers.14.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
110
+ "model.layers.14.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
111
+ "model.layers.14.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
112
+ "model.layers.14.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
113
+ "model.layers.14.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
114
+ "model.layers.15.input_layernorm.weight": "model-00002-of-00004.safetensors",
115
+ "model.layers.15.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
116
+ "model.layers.15.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
117
+ "model.layers.15.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
118
+ "model.layers.15.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
119
+ "model.layers.15.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
120
+ "model.layers.15.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
121
+ "model.layers.15.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
122
+ "model.layers.15.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
123
+ "model.layers.15.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
124
+ "model.layers.15.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
125
+ "model.layers.15.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
126
+ "model.layers.15.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
127
+ "model.layers.15.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
128
+ "model.layers.15.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
129
+ "model.layers.16.input_layernorm.weight": "model-00002-of-00004.safetensors",
130
+ "model.layers.16.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
131
+ "model.layers.16.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
132
+ "model.layers.16.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
133
+ "model.layers.16.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
134
+ "model.layers.16.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
135
+ "model.layers.16.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
136
+ "model.layers.16.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
137
+ "model.layers.16.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
138
+ "model.layers.16.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
139
+ "model.layers.16.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
140
+ "model.layers.16.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
141
+ "model.layers.16.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
142
+ "model.layers.16.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
143
+ "model.layers.16.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
144
+ "model.layers.17.input_layernorm.weight": "model-00002-of-00004.safetensors",
145
+ "model.layers.17.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
146
+ "model.layers.17.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
147
+ "model.layers.17.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
148
+ "model.layers.17.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
149
+ "model.layers.17.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
150
+ "model.layers.17.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
151
+ "model.layers.17.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
152
+ "model.layers.17.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
153
+ "model.layers.17.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
154
+ "model.layers.17.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
155
+ "model.layers.17.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
156
+ "model.layers.17.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
157
+ "model.layers.17.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
158
+ "model.layers.17.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
159
+ "model.layers.18.input_layernorm.weight": "model-00002-of-00004.safetensors",
160
+ "model.layers.18.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
161
+ "model.layers.18.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
162
+ "model.layers.18.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
163
+ "model.layers.18.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
164
+ "model.layers.18.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
165
+ "model.layers.18.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
166
+ "model.layers.18.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
167
+ "model.layers.18.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
168
+ "model.layers.18.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
169
+ "model.layers.18.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
170
+ "model.layers.18.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
171
+ "model.layers.18.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
172
+ "model.layers.18.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
173
+ "model.layers.18.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
174
+ "model.layers.19.input_layernorm.weight": "model-00002-of-00004.safetensors",
175
+ "model.layers.19.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
176
+ "model.layers.19.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
177
+ "model.layers.19.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
178
+ "model.layers.19.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
179
+ "model.layers.19.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
180
+ "model.layers.19.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
181
+ "model.layers.19.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
182
+ "model.layers.19.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
183
+ "model.layers.19.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
184
+ "model.layers.19.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
185
+ "model.layers.19.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
186
+ "model.layers.19.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
187
+ "model.layers.19.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
188
+ "model.layers.19.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
189
+ "model.layers.2.input_layernorm.weight": "model-00001-of-00004.safetensors",
190
+ "model.layers.2.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
191
+ "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
192
+ "model.layers.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
193
+ "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
194
+ "model.layers.2.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
195
+ "model.layers.2.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
196
+ "model.layers.2.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
197
+ "model.layers.2.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
198
+ "model.layers.2.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
199
+ "model.layers.2.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
200
+ "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
201
+ "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
202
+ "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
203
+ "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
204
+ "model.layers.20.input_layernorm.weight": "model-00002-of-00004.safetensors",
205
+ "model.layers.20.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
206
+ "model.layers.20.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
207
+ "model.layers.20.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
208
+ "model.layers.20.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
209
+ "model.layers.20.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
210
+ "model.layers.20.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
211
+ "model.layers.20.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
212
+ "model.layers.20.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
213
+ "model.layers.20.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
214
+ "model.layers.20.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
215
+ "model.layers.20.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
216
+ "model.layers.20.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
217
+ "model.layers.20.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
218
+ "model.layers.20.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
219
+ "model.layers.21.input_layernorm.weight": "model-00003-of-00004.safetensors",
220
+ "model.layers.21.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
221
+ "model.layers.21.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
222
+ "model.layers.21.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
223
+ "model.layers.21.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
224
+ "model.layers.21.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
225
+ "model.layers.21.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
226
+ "model.layers.21.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
227
+ "model.layers.21.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
228
+ "model.layers.21.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
229
+ "model.layers.21.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
230
+ "model.layers.21.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
231
+ "model.layers.21.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
232
+ "model.layers.21.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
233
+ "model.layers.21.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
234
+ "model.layers.22.input_layernorm.weight": "model-00003-of-00004.safetensors",
235
+ "model.layers.22.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
236
+ "model.layers.22.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
237
+ "model.layers.22.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
238
+ "model.layers.22.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
239
+ "model.layers.22.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
240
+ "model.layers.22.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
241
+ "model.layers.22.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
242
+ "model.layers.22.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
243
+ "model.layers.22.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
244
+ "model.layers.22.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
245
+ "model.layers.22.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
246
+ "model.layers.22.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
247
+ "model.layers.22.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
248
+ "model.layers.22.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
249
+ "model.layers.23.input_layernorm.weight": "model-00003-of-00004.safetensors",
250
+ "model.layers.23.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
251
+ "model.layers.23.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
252
+ "model.layers.23.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
253
+ "model.layers.23.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
254
+ "model.layers.23.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
255
+ "model.layers.23.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
256
+ "model.layers.23.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
257
+ "model.layers.23.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
258
+ "model.layers.23.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
259
+ "model.layers.23.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
260
+ "model.layers.23.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
261
+ "model.layers.23.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
262
+ "model.layers.23.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
263
+ "model.layers.23.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
264
+ "model.layers.24.input_layernorm.weight": "model-00003-of-00004.safetensors",
265
+ "model.layers.24.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
266
+ "model.layers.24.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
267
+ "model.layers.24.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
268
+ "model.layers.24.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
269
+ "model.layers.24.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
270
+ "model.layers.24.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
271
+ "model.layers.24.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
272
+ "model.layers.24.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
273
+ "model.layers.24.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
274
+ "model.layers.24.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
275
+ "model.layers.24.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
276
+ "model.layers.24.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
277
+ "model.layers.24.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
278
+ "model.layers.24.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
279
+ "model.layers.25.input_layernorm.weight": "model-00003-of-00004.safetensors",
280
+ "model.layers.25.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
281
+ "model.layers.25.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
282
+ "model.layers.25.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
283
+ "model.layers.25.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
284
+ "model.layers.25.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
285
+ "model.layers.25.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
286
+ "model.layers.25.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
287
+ "model.layers.25.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
288
+ "model.layers.25.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
289
+ "model.layers.25.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
290
+ "model.layers.25.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
291
+ "model.layers.25.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
292
+ "model.layers.25.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
293
+ "model.layers.25.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
294
+ "model.layers.26.input_layernorm.weight": "model-00003-of-00004.safetensors",
295
+ "model.layers.26.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
296
+ "model.layers.26.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
297
+ "model.layers.26.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
298
+ "model.layers.26.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
299
+ "model.layers.26.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
300
+ "model.layers.26.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
301
+ "model.layers.26.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
302
+ "model.layers.26.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
303
+ "model.layers.26.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
304
+ "model.layers.26.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
305
+ "model.layers.26.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
306
+ "model.layers.26.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
307
+ "model.layers.26.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
308
+ "model.layers.26.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
309
+ "model.layers.27.input_layernorm.weight": "model-00003-of-00004.safetensors",
310
+ "model.layers.27.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
311
+ "model.layers.27.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
312
+ "model.layers.27.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
313
+ "model.layers.27.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
314
+ "model.layers.27.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
315
+ "model.layers.27.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
316
+ "model.layers.27.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
317
+ "model.layers.27.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
318
+ "model.layers.27.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
319
+ "model.layers.27.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
320
+ "model.layers.27.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
321
+ "model.layers.27.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
322
+ "model.layers.27.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
323
+ "model.layers.27.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
324
+ "model.layers.28.input_layernorm.weight": "model-00003-of-00004.safetensors",
325
+ "model.layers.28.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
326
+ "model.layers.28.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
327
+ "model.layers.28.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
328
+ "model.layers.28.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
329
+ "model.layers.28.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
330
+ "model.layers.28.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
331
+ "model.layers.28.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
332
+ "model.layers.28.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
333
+ "model.layers.28.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
334
+ "model.layers.28.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
335
+ "model.layers.28.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
336
+ "model.layers.28.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
337
+ "model.layers.28.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
338
+ "model.layers.28.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
339
+ "model.layers.29.input_layernorm.weight": "model-00003-of-00004.safetensors",
340
+ "model.layers.29.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
341
+ "model.layers.29.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
342
+ "model.layers.29.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
343
+ "model.layers.29.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
344
+ "model.layers.29.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
345
+ "model.layers.29.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
346
+ "model.layers.29.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
347
+ "model.layers.29.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
348
+ "model.layers.29.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
349
+ "model.layers.29.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
350
+ "model.layers.29.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
351
+ "model.layers.29.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
352
+ "model.layers.29.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
353
+ "model.layers.29.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
354
+ "model.layers.3.input_layernorm.weight": "model-00001-of-00004.safetensors",
355
+ "model.layers.3.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
356
+ "model.layers.3.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
357
+ "model.layers.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
358
+ "model.layers.3.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
359
+ "model.layers.3.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
360
+ "model.layers.3.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
361
+ "model.layers.3.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
362
+ "model.layers.3.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
363
+ "model.layers.3.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
364
+ "model.layers.3.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
365
+ "model.layers.3.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
366
+ "model.layers.3.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
367
+ "model.layers.3.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
368
+ "model.layers.3.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
369
+ "model.layers.30.input_layernorm.weight": "model-00003-of-00004.safetensors",
370
+ "model.layers.30.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
371
+ "model.layers.30.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
372
+ "model.layers.30.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
373
+ "model.layers.30.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
374
+ "model.layers.30.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
375
+ "model.layers.30.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
376
+ "model.layers.30.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
377
+ "model.layers.30.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
378
+ "model.layers.30.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
379
+ "model.layers.30.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
380
+ "model.layers.30.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
381
+ "model.layers.30.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
382
+ "model.layers.30.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
383
+ "model.layers.30.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
384
+ "model.layers.31.input_layernorm.weight": "model-00003-of-00004.safetensors",
385
+ "model.layers.31.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
386
+ "model.layers.31.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
387
+ "model.layers.31.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
388
+ "model.layers.31.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
389
+ "model.layers.31.self_attn.compress_kv.kv_compress.0.bias": "model-00003-of-00004.safetensors",
390
+ "model.layers.31.self_attn.compress_kv.kv_compress.0.weight": "model-00003-of-00004.safetensors",
391
+ "model.layers.31.self_attn.compress_kv.kv_compress.2.bias": "model-00003-of-00004.safetensors",
392
+ "model.layers.31.self_attn.compress_kv.kv_compress.2.weight": "model-00003-of-00004.safetensors",
393
+ "model.layers.31.self_attn.gate_proj.bias": "model-00003-of-00004.safetensors",
394
+ "model.layers.31.self_attn.gate_proj.weight": "model-00003-of-00004.safetensors",
395
+ "model.layers.31.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
396
+ "model.layers.31.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
397
+ "model.layers.31.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
398
+ "model.layers.31.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
399
+ "model.layers.4.input_layernorm.weight": "model-00001-of-00004.safetensors",
400
+ "model.layers.4.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
401
+ "model.layers.4.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
402
+ "model.layers.4.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
403
+ "model.layers.4.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
404
+ "model.layers.4.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
405
+ "model.layers.4.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
406
+ "model.layers.4.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
407
+ "model.layers.4.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
408
+ "model.layers.4.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
409
+ "model.layers.4.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
410
+ "model.layers.4.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
411
+ "model.layers.4.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
412
+ "model.layers.4.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
413
+ "model.layers.4.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
414
+ "model.layers.5.input_layernorm.weight": "model-00001-of-00004.safetensors",
415
+ "model.layers.5.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
416
+ "model.layers.5.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
417
+ "model.layers.5.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
418
+ "model.layers.5.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
419
+ "model.layers.5.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
420
+ "model.layers.5.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
421
+ "model.layers.5.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
422
+ "model.layers.5.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
423
+ "model.layers.5.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
424
+ "model.layers.5.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
425
+ "model.layers.5.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
426
+ "model.layers.5.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
427
+ "model.layers.5.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
428
+ "model.layers.5.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
429
+ "model.layers.6.input_layernorm.weight": "model-00001-of-00004.safetensors",
430
+ "model.layers.6.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
431
+ "model.layers.6.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
432
+ "model.layers.6.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
433
+ "model.layers.6.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
434
+ "model.layers.6.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
435
+ "model.layers.6.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
436
+ "model.layers.6.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
437
+ "model.layers.6.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
438
+ "model.layers.6.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
439
+ "model.layers.6.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
440
+ "model.layers.6.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
441
+ "model.layers.6.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
442
+ "model.layers.6.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
443
+ "model.layers.6.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
444
+ "model.layers.7.input_layernorm.weight": "model-00001-of-00004.safetensors",
445
+ "model.layers.7.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
446
+ "model.layers.7.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
447
+ "model.layers.7.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
448
+ "model.layers.7.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
449
+ "model.layers.7.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
450
+ "model.layers.7.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
451
+ "model.layers.7.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
452
+ "model.layers.7.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
453
+ "model.layers.7.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
454
+ "model.layers.7.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
455
+ "model.layers.7.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
456
+ "model.layers.7.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
457
+ "model.layers.7.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
458
+ "model.layers.7.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
459
+ "model.layers.8.input_layernorm.weight": "model-00001-of-00004.safetensors",
460
+ "model.layers.8.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
461
+ "model.layers.8.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
462
+ "model.layers.8.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
463
+ "model.layers.8.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
464
+ "model.layers.8.self_attn.compress_kv.kv_compress.0.bias": "model-00001-of-00004.safetensors",
465
+ "model.layers.8.self_attn.compress_kv.kv_compress.0.weight": "model-00001-of-00004.safetensors",
466
+ "model.layers.8.self_attn.compress_kv.kv_compress.2.bias": "model-00001-of-00004.safetensors",
467
+ "model.layers.8.self_attn.compress_kv.kv_compress.2.weight": "model-00001-of-00004.safetensors",
468
+ "model.layers.8.self_attn.gate_proj.bias": "model-00001-of-00004.safetensors",
469
+ "model.layers.8.self_attn.gate_proj.weight": "model-00001-of-00004.safetensors",
470
+ "model.layers.8.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
471
+ "model.layers.8.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
472
+ "model.layers.8.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
473
+ "model.layers.8.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
474
+ "model.layers.9.input_layernorm.weight": "model-00002-of-00004.safetensors",
475
+ "model.layers.9.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
476
+ "model.layers.9.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
477
+ "model.layers.9.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
478
+ "model.layers.9.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
479
+ "model.layers.9.self_attn.compress_kv.kv_compress.0.bias": "model-00002-of-00004.safetensors",
480
+ "model.layers.9.self_attn.compress_kv.kv_compress.0.weight": "model-00002-of-00004.safetensors",
481
+ "model.layers.9.self_attn.compress_kv.kv_compress.2.bias": "model-00002-of-00004.safetensors",
482
+ "model.layers.9.self_attn.compress_kv.kv_compress.2.weight": "model-00002-of-00004.safetensors",
483
+ "model.layers.9.self_attn.gate_proj.bias": "model-00002-of-00004.safetensors",
484
+ "model.layers.9.self_attn.gate_proj.weight": "model-00002-of-00004.safetensors",
485
+ "model.layers.9.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
486
+ "model.layers.9.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
487
+ "model.layers.9.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
488
+ "model.layers.9.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
489
+ "model.norm.weight": "model-00001-of-00004.safetensors"
490
+ }
491
+ }
modeling_minicpm.py ADDED
The diff for this file is too large to render. See raw diff
 
special_tokens_map.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_end|>",
4
+ "<|im_start|>",
5
+ "<|tool_call|>",
6
+ "<|execute_start|>",
7
+ "<|execute_end|>",
8
+ "<|fim_prefix|>",
9
+ "<|fim_middle|>",
10
+ "<|fim_suffix|>"
11
+ ],
12
+ "bos_token": {
13
+ "content": "<s>",
14
+ "lstrip": false,
15
+ "normalized": false,
16
+ "rstrip": false,
17
+ "single_word": false
18
+ },
19
+ "eos_token": {
20
+ "content": "<|im_end|>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false
25
+ },
26
+ "unk_token": {
27
+ "content": "<unk>",
28
+ "lstrip": false,
29
+ "normalized": false,
30
+ "rstrip": false,
31
+ "single_word": false
32
+ }
33
+ }
stage1.py ADDED
@@ -0,0 +1,341 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from typing import Any, Tuple, Union
3
+ from collections import Counter
4
+ import torch
5
+ import triton
6
+ import triton.language as tl
7
+ import warnings
8
+ from native_sparse_attention.ops.triton.utils import get_num_warps_stages, is_hopper_gpu
9
+
10
+
11
+ IS_HOPPER_GPU = is_hopper_gpu()
12
+
13
+
14
+ @triton.jit
15
+ def forward_kernel(
16
+ q_ptr, # Q: n x h x d
17
+ k_ptr, # K: n x h x d
18
+ attn_score_ptr, # S: n x h x d
19
+ # size and stride at compresstion
20
+ kernel_size,
21
+ kernel_stride,
22
+ # seqlens
23
+ cu_seqlens_q,
24
+ cu_seqlens_k,
25
+ # shape
26
+ NUM_KV_HEADS,
27
+ NUM_SHARE_Q_HEADS,
28
+ HEAD_DIM,
29
+ # sm_scale
30
+ sm_scale,
31
+ # stride
32
+ stride_qn,
33
+ stride_qh,
34
+ stride_qd,
35
+ stride_kn,
36
+ stride_kh,
37
+ stride_kd,
38
+ stride_sh,
39
+ stride_sq,
40
+ stride_sk,
41
+ # META parameters
42
+ BLOCK_SIZE_Q: tl.constexpr, # q block size
43
+ BLOCK_SIZE_K: tl.constexpr, # k block size
44
+ BLOCK_SIZE_D: tl.constexpr,
45
+ ):
46
+ qk_scale = sm_scale
47
+ # get batch id and head id
48
+ pid_b = tl.program_id(0)
49
+ pid_h = tl.program_id(1)
50
+ pid_q = tl.program_id(2)
51
+ pid_kh = pid_h // NUM_SHARE_Q_HEADS
52
+ # get q k start and len after rmpad
53
+ q_start = tl.load(cu_seqlens_q + pid_b)
54
+ q_len = tl.load(cu_seqlens_q + pid_b + 1) - q_start
55
+ k_start = tl.load(cu_seqlens_k + pid_b)
56
+ k_len = tl.load(cu_seqlens_k + pid_b + 1) - k_start
57
+ # skip first kernel_size query block, because they do no attend to any keys
58
+ q_start_in_seq = pid_q * BLOCK_SIZE_Q + kernel_size - 1
59
+ if q_start_in_seq >= q_len:
60
+ return
61
+ # init qkv pointer
62
+ q_ptrs = tl.make_block_ptr(
63
+ base=q_ptr + q_start * stride_qn + pid_h * stride_qh,
64
+ shape=(q_len, HEAD_DIM),
65
+ strides=(stride_qn, stride_qd),
66
+ offsets=(q_start_in_seq, 0),
67
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_D),
68
+ order=(1, 0),
69
+ )
70
+ k_ptrs = tl.make_block_ptr(
71
+ base=k_ptr + k_start * stride_kn + pid_kh * stride_kh,
72
+ shape=(HEAD_DIM, k_len),
73
+ strides=(stride_kd, stride_kn),
74
+ offsets=(0, 0),
75
+ block_shape=(BLOCK_SIZE_D, BLOCK_SIZE_K),
76
+ order=(0, 1),
77
+ )
78
+ s_ptrs = tl.make_block_ptr(
79
+ base=attn_score_ptr + pid_h * stride_sh + q_start * stride_sq + 0 * stride_sk,
80
+ shape=(q_len, k_len),
81
+ strides=(stride_sq, stride_sk),
82
+ offsets=(q_start_in_seq, 0),
83
+ block_shape=(BLOCK_SIZE_Q, BLOCK_SIZE_K),
84
+ order=(1, 0),
85
+ )
86
+ # load q
87
+ q = tl.load(q_ptrs, boundary_check=(0, 1), padding_option="zero")
88
+ # init statistics
89
+ off_q = tl.arange(0, BLOCK_SIZE_Q) + q_start_in_seq
90
+ off_k = tl.arange(0, BLOCK_SIZE_K) * kernel_stride + kernel_size - 1
91
+ # attention
92
+ lo = 0
93
+ hi = min(k_len, (q_start_in_seq + BLOCK_SIZE_Q - kernel_size) // kernel_stride + 1)
94
+ for i in range(lo, hi, BLOCK_SIZE_K):
95
+ i = tl.multiple_of(i, BLOCK_SIZE_K)
96
+ # load k
97
+ k = tl.load(k_ptrs, boundary_check=(1, 0), padding_option="zero")
98
+ # compute qk
99
+ qk = tl.zeros((BLOCK_SIZE_Q, BLOCK_SIZE_K), dtype=tl.float32)
100
+ qk += tl.where(
101
+ off_q[:, None] >= (i * kernel_stride + off_k)[None, :], 0, float("-inf")
102
+ )
103
+ qk += tl.dot(q, k) * qk_scale
104
+ # store s
105
+ tl.store(s_ptrs, qk.to(tl.bfloat16), boundary_check=(0, 1))
106
+ # update ptrs
107
+ k_ptrs = tl.advance(k_ptrs, (0, BLOCK_SIZE_K))
108
+ s_ptrs = tl.advance(s_ptrs, (0, BLOCK_SIZE_K))
109
+
110
+
111
+ def compressed_attention_fwd(
112
+ q: torch.Tensor,
113
+ k: torch.Tensor,
114
+ kernel_size: int,
115
+ kernel_stride: int,
116
+ cu_seqlens_q: torch.Tensor,
117
+ cu_seqlens_k: torch.Tensor,
118
+ max_seqlen_q: int,
119
+ max_seqlen_k: int,
120
+ sm_scale: float,
121
+ ):
122
+ # dtype check
123
+ assert k.dtype == q.dtype
124
+ assert cu_seqlens_q.dtype == torch.int32 and cu_seqlens_k.dtype == torch.int32
125
+ # shape
126
+ q_len, num_q_heads, head_dim = q.shape
127
+ k_len, num_k_heads, head_dim = k.shape
128
+ batch_size = cu_seqlens_q.shape[0] - 1
129
+ assert q_len > k_len
130
+ # gqa
131
+ assert num_q_heads % num_k_heads == 0
132
+ num_share_q_heads = num_q_heads // num_k_heads
133
+ # output tensor
134
+ attn_score = torch.full((num_q_heads, q_len, max_seqlen_k), float('-inf'), dtype=q.dtype, device=q.device)
135
+ # launch kernel
136
+ grid = lambda META: (
137
+ batch_size,
138
+ num_q_heads,
139
+ triton.cdiv(max_seqlen_q, META["BLOCK_SIZE_Q"]),
140
+ )
141
+ BLOCK_SIZE_Q = 128
142
+ BLOCK_SIZE_K = 128
143
+ BLOCK_SIZE_D = triton.next_power_of_2(head_dim)
144
+ num_warps, num_stages = get_num_warps_stages(head_dim, BLOCK_SIZE_Q, IS_HOPPER_GPU)
145
+ forward_kernel[grid](
146
+ q,
147
+ k,
148
+ attn_score,
149
+ kernel_size,
150
+ kernel_stride,
151
+ cu_seqlens_q,
152
+ cu_seqlens_k,
153
+ num_k_heads,
154
+ num_share_q_heads,
155
+ head_dim,
156
+ sm_scale,
157
+ q.stride(0),
158
+ q.stride(1),
159
+ q.stride(2),
160
+ k.stride(0),
161
+ k.stride(1),
162
+ k.stride(2),
163
+ attn_score.stride(0),
164
+ attn_score.stride(1),
165
+ attn_score.stride(2),
166
+ BLOCK_SIZE_Q=BLOCK_SIZE_Q,
167
+ BLOCK_SIZE_K=BLOCK_SIZE_K,
168
+ BLOCK_SIZE_D=BLOCK_SIZE_D,
169
+ num_warps=num_warps,
170
+ num_stages=num_stages,
171
+ )
172
+ return attn_score
173
+
174
+ def reference_attn_score(
175
+ q, k,
176
+ kernel_size, kernel_stride,
177
+ cu_seqlens_q, cu_seqlens_k,
178
+ sm_scale,
179
+ ):
180
+ # q: [total_q, Hq, D], k: [total_k, Hk, D]
181
+ total_q, Hq, D = q.shape
182
+ total_k, Hk, _ = k.shape
183
+ B = cu_seqlens_q.numel() - 1
184
+ share = Hq // Hk
185
+ qk_scale = sm_scale
186
+
187
+ out = torch.full((Hq, total_q, total_k), float("-inf"), device=q.device, dtype=torch.float32)
188
+
189
+ for b in range(B):
190
+ qs = int(cu_seqlens_q[b].item()); qe = int(cu_seqlens_q[b+1].item())
191
+ ks = int(cu_seqlens_k[b].item()); ke = int(cu_seqlens_k[b+1].item())
192
+ q_len = qe - qs
193
+ k_len = ke - ks
194
+
195
+ q_b = q[qs:qe].float() # [q_len, Hq, D]
196
+ k_b = k[ks:ke].float() # [k_len, Hk, D]
197
+
198
+ # key position in original sequence for compressed k index j
199
+ key_pos = torch.arange(k_len, device=q.device) * kernel_stride + (kernel_size - 1) # [k_len]
200
+
201
+ for hq in range(Hq):
202
+ hk = hq // share
203
+ # [q_len, D] @ [D, k_len] -> [q_len, k_len]
204
+ scores = (q_b[:, hq, :] @ k_b[:, hk, :].T) * qk_scale
205
+
206
+ q_pos = torch.arange(q_len, device=q.device) + (kernel_size - 1) # 注意:你 kernel 的 q_start_in_seq 起点偏移
207
+ # 这里要严格模拟 kernel:kernel 从 q_pos = kernel_size-1 开始写,其它保持 -inf
208
+ # 所以我们把 full q_len 的 scores 先置 -inf,再对可写区间写入
209
+ full_scores = torch.full((q_len, k_len), float("-inf"), device=q.device, dtype=torch.float32)
210
+ valid_q = torch.arange(q_len, device=q.device) >= (kernel_size - 1)
211
+ # causal mask: q_pos >= key_pos
212
+ causal = (q_pos[:, None] >= key_pos[None, :])
213
+ full_scores[valid_q] = torch.where(causal[valid_q], scores[valid_q], float("-inf"))
214
+
215
+ out[hq, qs:qe, ks:ke] = full_scores
216
+
217
+ return out
218
+
219
+
220
+ def reference_attn_score(
221
+ q, k,
222
+ kernel_size, kernel_stride,
223
+ cu_seqlens_q, cu_seqlens_k,
224
+ sm_scale,
225
+ ):
226
+ total_q, Hq, D = q.shape
227
+ total_k, Hk, _ = k.shape
228
+ B = cu_seqlens_q.numel() - 1
229
+ share = Hq // Hk
230
+ qk_scale = sm_scale
231
+
232
+ out = torch.full((Hq, total_q, total_k), float("-inf"), device=q.device, dtype=torch.bfloat16)
233
+
234
+ for b in range(B):
235
+ qs = int(cu_seqlens_q[b]); qe = int(cu_seqlens_q[b+1])
236
+ ks = int(cu_seqlens_k[b]); ke = int(cu_seqlens_k[b+1])
237
+ q_len = qe - qs
238
+ k_len = ke - ks
239
+
240
+ q_b = q[qs:qe].float()
241
+ k_b = k[ks:ke].float()
242
+
243
+ key_pos = torch.arange(k_len, device=q.device) * kernel_stride + (kernel_size - 1) # [k_len]
244
+ q_pos = torch.arange(q_len, device=q.device) # ✅ 不要 + (kernel_size-1)
245
+ valid_q = q_pos >= (kernel_size - 1)
246
+
247
+ causal = (q_pos[:, None] >= key_pos[None, :]) # [q_len, k_len]
248
+
249
+ for hq in range(Hq):
250
+ hk = hq // share
251
+ scores = (q_b[:, hq, :] @ k_b[:, hk, :].T) * qk_scale # [q_len, k_len]
252
+
253
+ full_scores = torch.full((q_len, k_len), float("-inf"), device=q.device, dtype=torch.float32)
254
+ full_scores[valid_q] = torch.where(causal[valid_q], scores[valid_q], float("-inf"))
255
+ out[hq, qs:qe, ks:ke] = full_scores.to(torch.bfloat16)
256
+
257
+ return out
258
+
259
+
260
+ def test_compressed_attention_fwd(
261
+ device="cuda",
262
+ dtype=torch.bfloat16,
263
+ B=2,
264
+ q_lens=(257, 193),
265
+ k_lens=(129, 97),
266
+ Hq=16,
267
+ Hk=2,
268
+ D=128,
269
+ kernel_size=32,
270
+ kernel_stride=16,
271
+ sm_scale=None,
272
+ atol=2e-2,
273
+ ):
274
+ assert Hq % Hk == 0
275
+ if sm_scale is None:
276
+ sm_scale = 1.0 / math.sqrt(D)
277
+
278
+ # build cu_seqlens and packed q/k
279
+ cu_q = [0]
280
+ cu_k = [0]
281
+ for i in range(B):
282
+ cu_q.append(cu_q[-1] + q_lens[i])
283
+ cu_k.append(cu_k[-1] + k_lens[i])
284
+ cu_seqlens_q = torch.tensor(cu_q, device=device, dtype=torch.int32)
285
+ cu_seqlens_k = torch.tensor(cu_k, device=device, dtype=torch.int32)
286
+
287
+ total_q = cu_q[-1]
288
+ total_k = cu_k[-1]
289
+
290
+ q = torch.randn((total_q, Hq, D), device=device, dtype=dtype)
291
+ k = torch.randn((total_k, Hk, D), device=device, dtype=dtype)
292
+
293
+ max_seqlen_q = max(q_lens)
294
+ max_seqlen_k = max(k_lens)
295
+
296
+ # run triton
297
+ attn_triton = compressed_attention_fwd(
298
+ q, k,
299
+ kernel_size, kernel_stride,
300
+ cu_seqlens_q, cu_seqlens_k,
301
+ max_seqlen_q, max_seqlen_k,
302
+ sm_scale,
303
+ ) # 你需要把 compressed_attention_fwd 修成 return attn_score
304
+
305
+ # reference
306
+ ref = reference_attn_score(
307
+ q, k,
308
+ kernel_size, kernel_stride,
309
+ cu_seqlens_q, cu_seqlens_k,
310
+ sm_scale,
311
+ ) # fp32
312
+
313
+ from infllm_v2 import infllmv2_attn_stage1
314
+
315
+ attn_cuda = infllmv2_attn_stage1(
316
+ q.repeat_interleave(2, dim=1).contiguous(),
317
+ k.contiguous(),
318
+ k.contiguous(),
319
+ cu_seqlens_q=cu_seqlens_q,
320
+ cu_seqlens_k=cu_seqlens_k,
321
+ max_seqlen_q=max_seqlen_q,
322
+ max_seqlen_k=max_seqlen_k,
323
+ causal=True
324
+ ) / 2
325
+ _attn_triton = attn_triton.exp() / (attn_triton.exp().sum(dim=-1, keepdim=True) + 1e-8)
326
+ _attn_triton = _attn_triton.reshape(Hk, -1, _attn_triton.shape[-2], _attn_triton.shape[-1])
327
+ _attn_triton = _attn_triton.sum(dim=1)
328
+
329
+ # compare (ignore -inf)
330
+ attn_t = attn_triton.float()
331
+ mask = torch.isfinite(ref)
332
+ if mask.any():
333
+ max_err = (attn_t[mask] - ref[mask]).abs().max().item()
334
+ else:
335
+ max_err = 0.0
336
+
337
+ print(f"max_abs_err={max_err}")
338
+ assert max_err <= atol, f"too large error: {max_err} > {atol}"
339
+
340
+ if __name__ == "__main__":
341
+ test_compressed_attention_fwd()
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bb74d51116831c3bf65db812c553f94ab0c88dcf97a5bbb37e3504f6d359c530
3
+ size 1181204
tokenizer_config.json ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "add_prefix_space": null,
5
+ "added_tokens_decoder": {
6
+ "0": {
7
+ "content": "<unk>",
8
+ "lstrip": false,
9
+ "normalized": false,
10
+ "rstrip": false,
11
+ "single_word": false,
12
+ "special": true
13
+ },
14
+ "1": {
15
+ "content": "<s>",
16
+ "lstrip": false,
17
+ "normalized": false,
18
+ "rstrip": false,
19
+ "single_word": false,
20
+ "special": true
21
+ },
22
+ "2": {
23
+ "content": "</s>",
24
+ "lstrip": false,
25
+ "normalized": false,
26
+ "rstrip": false,
27
+ "single_word": false,
28
+ "special": true
29
+ },
30
+ "73440": {
31
+ "content": "<|im_end|>",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false,
36
+ "special": true
37
+ },
38
+ "73441": {
39
+ "content": "<|im_start|>",
40
+ "lstrip": false,
41
+ "normalized": false,
42
+ "rstrip": false,
43
+ "single_word": false,
44
+ "special": true
45
+ },
46
+ "73442": {
47
+ "content": "<|tool_call|>",
48
+ "lstrip": false,
49
+ "normalized": false,
50
+ "rstrip": false,
51
+ "single_word": false,
52
+ "special": true
53
+ },
54
+ "73443": {
55
+ "content": "<|execute_start|>",
56
+ "lstrip": false,
57
+ "normalized": false,
58
+ "rstrip": false,
59
+ "single_word": false,
60
+ "special": true
61
+ },
62
+ "73444": {
63
+ "content": "<|execute_end|>",
64
+ "lstrip": false,
65
+ "normalized": false,
66
+ "rstrip": false,
67
+ "single_word": false,
68
+ "special": true
69
+ },
70
+ "73445": {
71
+ "content": "<|fim_prefix|>",
72
+ "lstrip": false,
73
+ "normalized": false,
74
+ "rstrip": false,
75
+ "single_word": false,
76
+ "special": true
77
+ },
78
+ "73446": {
79
+ "content": "<|fim_middle|>",
80
+ "lstrip": false,
81
+ "normalized": false,
82
+ "rstrip": false,
83
+ "single_word": false,
84
+ "special": true
85
+ },
86
+ "73447": {
87
+ "content": "<|fim_suffix|>",
88
+ "lstrip": false,
89
+ "normalized": false,
90
+ "rstrip": false,
91
+ "single_word": false,
92
+ "special": true
93
+ }
94
+ },
95
+ "additional_special_tokens": [
96
+ "<|im_end|>",
97
+ "<|im_start|>",
98
+ "<|tool_call|>",
99
+ "<|execute_start|>",
100
+ "<|execute_end|>",
101
+ "<|fim_prefix|>",
102
+ "<|fim_middle|>",
103
+ "<|fim_suffix|>"
104
+ ],
105
+ "bos_token": "<s>",
106
+ "chat_template": "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% if enable_thinking is defined and enable_thinking is false %}{{ '<think>\n\n</think>\n' }}{% endif %}{% endif %}",
107
+ "clean_up_tokenization_spaces": false,
108
+ "eos_token": "<|im_end|>",
109
+ "legacy": true,
110
+ "model_max_length": 1000000000000000019884624838656,
111
+ "pad_token": null,
112
+ "sp_model_kwargs": {},
113
+ "spaces_between_special_tokens": false,
114
+ "tokenizer_class": "LlamaTokenizer",
115
+ "unk_token": "<unk>",
116
+ "use_default_system_prompt": false
117
+ }