interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cuda / lightning-indexer.cu

795 lines · 33654 bytes Raw Blame History
1#include "common.cuh"
2#include "lightning-indexer.cuh"
3#include "fattn-common.cuh"
4#include "convert.cuh"
5
6#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
7#if defined(TURING_MMA_AVAILABLE)
8
9typedef union {
10 int2 i2;
11 half2 h2[2];
12} half4;
13
14// TODO add support for AMD cards via rocWMMA
15#include <mma.h>
16namespace wmma = nvcuda::wmma;
17
18template <int WARPS_PER_BLOCK, int K_VECS_PER_BLOCK, int64_t N_EMBD, int64_t N_HEAD, ggml_type TYPE_K>
19static __global__ void lightning_indexer_kernel_wmma(
20 const float * Q, const char * K, const float * W, const half * M, float * dst,
21 int64_t n_stream, int64_t n_batch, int64_t n_kv,
22 size_t nb1, size_t nb2, size_t nb3,
23 size_t nbq1, size_t nbq2, size_t nbq3,
24 size_t nbk1, size_t nbk2, size_t nbk3,
25 size_t nbw1, size_t nbw2, size_t nbw3,
26 size_t nbm1, size_t nbm2, size_t nbm3,
27 int64_t nem3
28 ) {
29
30 constexpr int THREADS_PER_BLOCK = WARPS_PER_BLOCK * WARP_SIZE;
31 constexpr int HEADS_PER_INNER_LOOP = 8;
32 constexpr int K_EMBD_PER_INNER_LOOP = 16;
33 constexpr int N_EMBD_PADDED = N_EMBD + 8;
34
35 const int i_batch = blockIdx.y;
36 const int i_stream = blockIdx.z;
37 const int i_warp = threadIdx.y;
38 const int i_lane = threadIdx.x;
39 const int tid = i_warp * WARP_SIZE + i_lane;
40
41 // each block processes K_VECS_PER_BLOCK K vectors
42 const int start_kv = blockIdx.x * K_VECS_PER_BLOCK;
43
44 const char * q_base = (const char *) Q + i_batch*nbq2 + i_stream*nbq3;
45 const float * w_base = (const float *) ((const char *) W + i_batch*nbw1 + i_stream*nbw3);
46
47 // phase 1 - load weights and first Q tile to shared memory
48
49 __shared__ float w_shared[N_HEAD];
50 __shared__ int2 q_shared_h[HEADS_PER_INNER_LOOP][N_EMBD_PADDED / 4];
51
52 if (tid < N_HEAD) {
53 w_shared[tid] = w_base[tid];
54 }
55
56 // total number of half4 elements in HEADS_PER_INNER_LOOP x N_EMBD Q tile
57 constexpr int N_Q_TILE = HEADS_PER_INNER_LOOP * (N_EMBD / 4);
58 // number of registers needed in each thread to store Q tile in thread block
59 constexpr int N_Q_NEXT = (N_Q_TILE + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK;
60
61#pragma unroll
62 for (int i_q = tid; i_q < N_Q_TILE; i_q += THREADS_PER_BLOCK) {
63 const int i_head = i_q / (N_EMBD / 4);
64 const int i_embd = i_q % (N_EMBD / 4);
65 const float4 q = *(const float4 *) (q_base + i_head*nbq1 + i_embd*sizeof(float4));
66 half4 q_packed;
67 q_packed.h2[0] = __float22half2_rn(make_float2(q.x, q.y));
68 q_packed.h2[1] = __float22half2_rn(make_float2(q.z, q.w));
69 q_shared_h[i_head][i_embd] = q_packed.i2;
70 }
71
72 // phase 2 - load (and dequantize if needed) K to shared mem
73
74 __shared__ half2 k_shared_h[K_VECS_PER_BLOCK][N_EMBD_PADDED / 4][2];
75
76 constexpr int n_k = K_VECS_PER_BLOCK * (N_EMBD / 4);
77
78 if constexpr (TYPE_K == GGML_TYPE_F16) {
79#pragma unroll
80 for (int i_k = tid; i_k < n_k; i_k += THREADS_PER_BLOCK) {
81 const int i_k_vec = i_k / (N_EMBD / 4);
82 const int i_embd = i_k % (N_EMBD / 4);
83 const int i_kv = start_kv + i_k_vec;
84 if (i_kv < n_kv) {
85 const int2 * k_base = (const int2 *) ((const char *) K + i_kv*nbk2 + i_stream*nbk3);
86 *(int2*) &k_shared_h[i_k_vec][i_embd] = k_base[i_embd];
87 } else {
88 *(int2*) &k_shared_h[i_k_vec][i_embd] = make_int2(0, 0);
89 }
90 }
91 } else {
92 constexpr dequantize_V_t dequantize_k = get_dequantize_V<TYPE_K, half, 4>();
93#pragma unroll
94 for (int i_k = tid; i_k < n_k; i_k += THREADS_PER_BLOCK) {
95 const int i_k_vec = i_k / (N_EMBD / 4);
96 const int i_embd = i_k % (N_EMBD / 4);
97 const int i_kv = start_kv + i_k_vec;
98 if (i_kv < n_kv) {
99 const void * k_base = (const void *) ((const char *) K + i_kv*nbk2 + i_stream*nbk3);
100 dequantize_k(k_base, &k_shared_h[i_k_vec][i_embd][0], i_embd * 4);
101 } else {
102 *(int2*) &k_shared_h[i_k_vec][i_embd] = make_int2(0, 0);
103 }
104 }
105 }
106
107 __syncthreads();
108
109 // phase 3 - calculate lightning indexer scores
110
111 __shared__ float qk_shared[WARPS_PER_BLOCK][HEADS_PER_INNER_LOOP][K_VECS_PER_BLOCK];
112
113 // load K fragment
114 wmma::fragment<wmma::matrix_b, HEADS_PER_INNER_LOOP, K_VECS_PER_BLOCK, K_EMBD_PER_INNER_LOOP, half, wmma::col_major> frag_k;
115 wmma::load_matrix_sync(frag_k, (half*) &k_shared_h[0][i_warp * K_EMBD_PER_INNER_LOOP / 4], N_EMBD_PADDED);
116
117 float score_k = 0.0f;
118
119 for (int i_head_0 = 0; i_head_0 < N_HEAD; i_head_0 += HEADS_PER_INNER_LOOP) {
120 const int i_head_next = i_head_0 + HEADS_PER_INNER_LOOP;
121
122 // we don't use accumulator for anything, fill it with zeros
123 wmma::fragment<wmma::accumulator, HEADS_PER_INNER_LOOP, K_VECS_PER_BLOCK, K_EMBD_PER_INNER_LOOP, float> frag_acc;
124 wmma::fill_fragment(frag_acc, 0.0f);
125
126 // load Q fragment
127 wmma::fragment<wmma::matrix_a, HEADS_PER_INNER_LOOP, K_VECS_PER_BLOCK, K_EMBD_PER_INNER_LOOP, half, wmma::row_major> frag_q;
128 wmma::load_matrix_sync(frag_q, (half*) &q_shared_h[0][i_warp * K_EMBD_PER_INNER_LOOP / 4], N_EMBD_PADDED);
129
130 // preload next Q tile to registers during matrix multiplication
131 float4 q_next[N_Q_NEXT];
132
133 if (i_head_next < N_HEAD) {
134#pragma unroll
135 for (int i_q = tid, i_q_next = 0; i_q < N_Q_TILE; i_q += THREADS_PER_BLOCK) {
136 const int i_head = i_head_next + i_q / (N_EMBD / 4);
137 const int i_embd = i_q % (N_EMBD / 4);
138 q_next[i_q_next++] = *(const float4 *) (q_base + i_head*nbq1 + i_embd*sizeof(float4));
139 }
140 }
141
142 // perform matrix multiplication
143 wmma::mma_sync(frag_acc, frag_q, frag_k, frag_acc);
144 wmma::store_matrix_sync((float*) &qk_shared[i_warp][0][0], frag_acc, K_VECS_PER_BLOCK, wmma::mem_row_major);
145
146 // make sure all threads finished using q_shared_h so we can store next tile
147 __syncthreads();
148
149 // write preloaded Q tile to shared memory
150 if (i_head_next < N_HEAD) {
151#pragma unroll
152 for (int i_q = tid, i_q_next = 0; i_q < N_Q_TILE; i_q += THREADS_PER_BLOCK) {
153 const int i_head = i_q / (N_EMBD / 4);
154 const int i_embd = i_q % (N_EMBD / 4);
155 half4 q_packed;
156 q_packed.h2[0] = __float22half2_rn(make_float2(q_next[i_q_next].x, q_next[i_q_next].y));
157 q_packed.h2[1] = __float22half2_rn(make_float2(q_next[i_q_next].z, q_next[i_q_next].w));
158 q_shared_h[i_head][i_embd] = q_packed.i2;
159 ++i_q_next;
160 }
161 }
162
163 // accumulate QK multiplication results from all block warps
164 // (there are 256 threads in block and 256 matmul outputs)
165 // TODO it will break if WARP_SIZE is not 32
166 const int h = tid / K_VECS_PER_BLOCK;
167 const int k = tid % K_VECS_PER_BLOCK;
168 const float w_val = w_shared[i_head_0 + h];
169
170 float sum = 0.0f;
171#pragma unroll
172 for (int w = 0; w < WARPS_PER_BLOCK; ++w) {
173 sum += qk_shared[w][h][k];
174 }
175
176 // ReLU, weight
177 sum = sum > 0.0f ? sum : 0.0f;
178 sum *= w_val;
179
180 // wait until qk_shared[0] is no longer used
181 __syncthreads();
182
183 // reuse qk_shared[0] for storing partial results
184 qk_shared[0][h][k] = sum;
185
186 // wait until all threads write their results
187 __syncthreads();
188
189 // accumulate result over heads
190 if (tid < K_VECS_PER_BLOCK) {
191#pragma unroll
192 for (int i_head = 0; i_head < HEADS_PER_INNER_LOOP; ++i_head) {
193 score_k += qk_shared[0][i_head][tid];
194 }
195 }
196
197 // make sure all threads finished using qk_shared
198 __syncthreads();
199 }
200
201 // phase 4 - store output to VRAM
202
203 if (tid < K_VECS_PER_BLOCK) {
204 const int i_kv = start_kv + tid;
205 if (i_kv < n_kv) {
206 const half * m_base = (const half *) ((const char *) M + i_batch*nbm1 + (i_stream%nem3)*nbm3);
207 float * dst_base = (float *) ((char *) dst + i_batch*nb1 + i_stream*nb3);
208 dst_base[i_kv] = score_k + __half2float(m_base[i_kv]);
209 }
210 }
211}
212
213#else // defined(TURING_MMA_AVAILABLE)
214
215template <int WARPS_PER_BLOCK, int K_VECS_PER_BLOCK, int64_t N_EMBD, int64_t N_HEAD, ggml_type TYPE_K>
216static __global__ void lightning_indexer_kernel_wmma(
217 const float * Q, const char * K, const float * W, const half * M, float * dst,
218 int64_t n_stream, int64_t n_batch, int64_t n_kv,
219 size_t nb1, size_t nb2, size_t nb3,
220 size_t nbq1, size_t nbq2, size_t nbq3,
221 size_t nbk1, size_t nbk2, size_t nbk3,
222 size_t nbw1, size_t nbw2, size_t nbw3,
223 size_t nbm1, size_t nbm2, size_t nbm3,
224 int64_t nem3
225 ) {
226 GGML_UNUSED_VARS(Q, K, W, M, dst,
227 n_stream, n_batch, n_kv,
228 nb1, nb2, nb3,
229 nbq1, nbq2, nbq3,
230 nbk1, nbk2, nbk3,
231 nbw1, nbw2, nbw3,
232 nem3);
233 NO_DEVICE_CODE;
234}
235
236#endif // defined(TURING_MMA_AVAILABLE)
237#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
238
239// tokens scored per block by the tile kernel
240#define LIGHTNING_INDEXER_TILE_TOKENS 8
241
242// heads whose queries the tile kernel stages per pass, MUSA arch 21 caps static shared memory
243// at 28 KB and the queries of four heads do not fit there next to the key tile
244#if defined(GGML_USE_MUSA) && defined(__MUSA_ARCH__) && __MUSA_ARCH__ < 220
245#define LIGHTNING_INDEXER_TILE_HEADS_PER_PASS 2
246#else
247#define LIGHTNING_INDEXER_TILE_HEADS_PER_PASS 4
248#endif
249
250// TODO there is one ugly assumption used in this kernel - that WARP_SIZE is equal to 32
251// thanks to that one warp operating on float4 processes whole indexer K/Q vectors
252// 32 * 4 = 128 (N_EMBD)
253
254template <int WARPS_PER_BLOCK, int K_VECS_PER_BLOCK, int64_t N_EMBD, int64_t N_HEAD, ggml_type TYPE_K>
255static __global__ void lightning_indexer_kernel_vec(
256 const float * Q, const char * K, const float * W, const half * M, float * dst,
257 int64_t n_stream, int64_t n_batch, int64_t n_kv,
258 size_t nb1, size_t nb2, size_t nb3,
259 size_t nbq1, size_t nbq2, size_t nbq3,
260 size_t nbk1, size_t nbk2, size_t nbk3,
261 size_t nbw1, size_t nbw2, size_t nbw3,
262 size_t nbm1, size_t nbm2, size_t nbm3,
263 int64_t nem3
264 ) {
265
266 constexpr int K_VECS_PER_WARP = K_VECS_PER_BLOCK / WARPS_PER_BLOCK;
267 constexpr int THREADS_PER_BLOCK = WARPS_PER_BLOCK * WARP_SIZE;
268
269 const int i_batch = blockIdx.y;
270 const int i_stream = blockIdx.z;
271 const int i_warp = threadIdx.y;
272 const int i_lane = threadIdx.x;
273 const int tid = i_warp * WARP_SIZE + i_lane;
274
275 // each warp processes K_VECS_PER_WARP K vectors
276 const int start_kv_block = blockIdx.x * K_VECS_PER_BLOCK;
277 const int start_kv = start_kv_block + i_warp * K_VECS_PER_WARP;
278
279 const char * q_base = (const char *) Q + i_batch*nbq2 + i_stream*nbq3;
280 const float * w_base = (const float *) ((const char *) W + i_batch*nbw1 + i_stream*nbw3);
281
282 // phase 1 - load (and dequantize if needed) K to registers
283
284 float4 k_reg_f[K_VECS_PER_WARP];
285
286 if constexpr (TYPE_K == GGML_TYPE_F32) {
287 // direct copy of float4
288#pragma unroll
289 for (int k = 0; k < K_VECS_PER_WARP; ++k) {
290 int i_kv = start_kv + k;
291 if (i_kv < n_kv) {
292 const float4 * k_base = (const float4 *) ((const char *) K + i_kv*nbk2 + i_stream*nbk3);
293 k_reg_f[k] = k_base[i_lane];
294 } else {
295 k_reg_f[k] = make_float4(0, 0, 0, 0);
296 }
297 }
298 } else {
299 // dequantize remaining types to float
300 constexpr dequantize_V_t dequantize_k = get_dequantize_V<TYPE_K, float, 4>();
301#pragma unroll
302 for (int k = 0; k < K_VECS_PER_WARP; ++k) {
303 int i_kv = start_kv + k;
304 if (i_kv < n_kv) {
305 const void * k_base = (const void *) ((const char *) K + i_kv*nbk2 + i_stream*nbk3);
306 dequantize_k(k_base, &k_reg_f[k], i_lane * 4);
307 } else {
308 k_reg_f[k] = make_float4(0, 0, 0, 0);
309 }
310 }
311 }
312
313 float score_k[K_VECS_PER_WARP] = { 0.0f };
314
315 // load weights and Q only for N_HEAD_INNER heads at once to reduce shared memory usage
316 constexpr int N_HEAD_INNER = N_HEAD / 4;
317
318 for (int i_head_0 = 0; i_head_0 < N_HEAD; i_head_0 += N_HEAD_INNER) {
319 // phase 2 - load weights and Q to shared memory
320
321 __shared__ float w_shared[N_HEAD_INNER];
322 __shared__ float4 q_shared_f[N_HEAD_INNER][N_EMBD / 4];
323
324 if (tid < N_HEAD_INNER) {
325 w_shared[tid] = w_base[i_head_0 + tid];
326 }
327
328 constexpr int n_q = N_HEAD_INNER * (N_EMBD / 4);
329#pragma unroll
330 for (int i_q = tid; i_q < n_q; i_q += THREADS_PER_BLOCK) {
331 const int i_head_inner = i_q / (N_EMBD / 4);
332 const int i_head = i_head_0 + i_head_inner;
333 const int i_embd = i_q % (N_EMBD / 4);
334 q_shared_f[i_head_inner][i_embd] = *(const float4 *) (q_base + i_head*nbq1 + i_embd*sizeof(float4));
335 }
336
337 __syncthreads();
338
339 // phase 3 - calculate lightning indexer scores
340
341 for (int i_head_inner = 0; i_head_inner < N_HEAD_INNER; ++i_head_inner) {
342 const float w_val = w_shared[i_head_inner];
343 float qk[K_VECS_PER_WARP] = { 0.0f };
344
345 // dot product of floats
346 const float4 q_vec = q_shared_f[i_head_inner][i_lane];
347
348#pragma unroll
349 for (int k = 0; k < K_VECS_PER_WARP; ++k) {
350 ggml_cuda_mad(qk[k], q_vec.x, k_reg_f[k].x);
351 ggml_cuda_mad(qk[k], q_vec.y, k_reg_f[k].y);
352 ggml_cuda_mad(qk[k], q_vec.z, k_reg_f[k].z);
353 ggml_cuda_mad(qk[k], q_vec.w, k_reg_f[k].w);
354 }
355
356#pragma unroll
357 for (int k = 0; k < K_VECS_PER_WARP; ++k) {
358 float sum = warp_reduce_sum(qk[k]);
359
360 // ReLU, weight
361 if (i_lane == 0) {
362 sum = (sum > 0.0f) ? sum : 0.0f;
363 score_k[k] += sum * w_val;
364 }
365 }
366 }
367
368 __syncthreads();
369 }
370
371 // phase 4 - store outputs to shared memory
372
373 __shared__ float dst_shared[K_VECS_PER_BLOCK];
374
375 if (i_lane == 0) {
376#pragma unroll
377 for (int k = 0; k < K_VECS_PER_WARP; ++k) {
378 dst_shared[i_warp * K_VECS_PER_WARP + k] = score_k[k];
379 }
380 }
381
382 __syncthreads();
383
384 // phase 5 - write from shared memory to VRAM in coalesced manner
385
386 if (tid < K_VECS_PER_BLOCK) {
387 int i_kv = start_kv_block + tid;
388 if (i_kv < n_kv) {
389 const half * m_base = (const half *) ((const char *) M + i_batch*nbm1 + (i_stream%nem3)*nbm3);
390 float * dst_base = (float *) ((char *) dst + i_batch*nb1 + i_stream*nb3);
391 dst_base[i_kv] = dst_shared[tid] + __half2float(m_base[i_kv]);
392 }
393 }
394}
395
396// one block scores a tile of K_VECS_PER_BLOCK keys against TOKENS_PER_BLOCK tokens: the keys are
397// staged in half precision and the queries of every head in float, each thread owns KEYS_PER_THREAD
398// keys for one token, a warp shares its token so the query reads are broadcasts, and every key
399// element is widened once for all heads, so no dot product needs a cross thread reduction
400template <int WARPS_PER_BLOCK, int K_VECS_PER_BLOCK, int64_t N_EMBD, int64_t N_HEAD, ggml_type TYPE_K>
401static __global__ void lightning_indexer_kernel_tile(
402 const float * Q, const char * K, const float * W, const half * M, float * dst,
403 int64_t n_stream, int64_t n_batch, int64_t n_kv,
404 size_t nb1, size_t nb2, size_t nb3,
405 size_t nbq1, size_t nbq2, size_t nbq3,
406 size_t nbk1, size_t nbk2, size_t nbk3,
407 size_t nbw1, size_t nbw2, size_t nbw3,
408 size_t nbm1, size_t nbm2, size_t nbm3,
409 int64_t nem3
410 ) {
411
412 constexpr int THREADS_PER_BLOCK = WARPS_PER_BLOCK * WARP_SIZE;
413 constexpr int TOKENS_PER_BLOCK = LIGHTNING_INDEXER_TILE_TOKENS;
414 constexpr int KEY_LANES = THREADS_PER_BLOCK / TOKENS_PER_BLOCK;
415 constexpr int KEYS_PER_THREAD = K_VECS_PER_BLOCK / KEY_LANES;
416 constexpr int N_EMBD_H2 = N_EMBD / 2;
417 constexpr int HEADS_PER_PASS = N_HEAD < LIGHTNING_INDEXER_TILE_HEADS_PER_PASS ? N_HEAD : LIGHTNING_INDEXER_TILE_HEADS_PER_PASS;
418
419 static_assert(THREADS_PER_BLOCK % TOKENS_PER_BLOCK == 0, "threads must cover the token tile");
420 static_assert(K_VECS_PER_BLOCK % KEY_LANES == 0, "key lanes must cover the key tile");
421 static_assert(N_HEAD % HEADS_PER_PASS == 0, "head passes must cover the heads");
422
423 const int tid = threadIdx.y * WARP_SIZE + threadIdx.x;
424 const int start_kv = blockIdx.x * K_VECS_PER_BLOCK;
425 const int start_batch = blockIdx.y * TOKENS_PER_BLOCK;
426 const int i_stream = blockIdx.z;
427
428 // the row padding keeps the keys of consecutive threads in distinct banks
429 __shared__ half2 k_shared[K_VECS_PER_BLOCK][N_EMBD_H2 + 1];
430 __shared__ float2 q_shared[HEADS_PER_PASS][TOKENS_PER_BLOCK][N_EMBD_H2];
431 __shared__ float w_shared[N_HEAD][TOKENS_PER_BLOCK];
432
433 // phase 1 - stage the key tile four elements at a time, rows past n_kv are zero
434
435#pragma unroll
436 for (int i = tid; i < K_VECS_PER_BLOCK * (N_EMBD / 4); i += THREADS_PER_BLOCK) {
437 const int r = i / (N_EMBD / 4);
438 const int c4 = i % (N_EMBD / 4);
439
440 half2 lo = make_half2(0.0f, 0.0f);
441 half2 hi = lo;
442 if (start_kv + r < n_kv) {
443 const char * k_row = K + (start_kv + r)*nbk2 + i_stream*nbk3;
444 if constexpr (TYPE_K == GGML_TYPE_F16) {
445 lo = ((const half2 *) k_row)[2*c4 + 0];
446 hi = ((const half2 *) k_row)[2*c4 + 1];
447 } else {
448 float4 v;
449 if constexpr (TYPE_K == GGML_TYPE_F32) {
450 v = ((const float4 *) k_row)[c4];
451 } else {
452 constexpr dequantize_V_t dequantize_k = get_dequantize_V<TYPE_K, float, 4>();
453 dequantize_k(k_row, &v, c4 * 4);
454 }
455 lo = make_half2(v.x, v.y);
456 hi = make_half2(v.z, v.w);
457 }
458 }
459
460 k_shared[r][2*c4 + 0] = lo;
461 k_shared[r][2*c4 + 1] = hi;
462 }
463
464 // phase 2 - stage the weights of every head, tokens past n_batch are zero
465
466 if (tid < N_HEAD * TOKENS_PER_BLOCK) {
467 const int h = tid / TOKENS_PER_BLOCK;
468 const int r = tid % TOKENS_PER_BLOCK;
469 w_shared[h][r] = start_batch + r < n_batch ?
470 ((const float *) ((const char *) W + (start_batch + r)*nbw1 + i_stream*nbw3))[h] : 0.0f;
471 }
472
473 const int kl = tid % KEY_LANES;
474 const int tl = tid / KEY_LANES;
475
476 float qk[N_HEAD][KEYS_PER_THREAD] = { { 0.0f } };
477
478#pragma unroll
479 for (int h0 = 0; h0 < N_HEAD; h0 += HEADS_PER_PASS) {
480 // the previous pass is fully consumed before its queries are replaced
481 if (h0 > 0) {
482 __syncthreads();
483 }
484
485 // phase 3 - stage the queries of the heads of this pass, tokens past n_batch are zero
486
487#pragma unroll
488 for (int i = tid; i < HEADS_PER_PASS * TOKENS_PER_BLOCK * (N_EMBD / 4); i += THREADS_PER_BLOCK) {
489 const int h = i / (TOKENS_PER_BLOCK * (N_EMBD / 4));
490 const int r = i / (N_EMBD / 4) % TOKENS_PER_BLOCK;
491 const int c4 = i % (N_EMBD / 4);
492
493 float4 v = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
494 if (start_batch + r < n_batch) {
495 v = *(const float4 *) ((const char *) Q + (h0 + h)*nbq1 + (start_batch + r)*nbq2 + i_stream*nbq3 + c4*sizeof(float4));
496 }
497
498 q_shared[h][r][2*c4 + 0] = make_float2(v.x, v.y);
499 q_shared[h][r][2*c4 + 1] = make_float2(v.z, v.w);
500 }
501
502 __syncthreads();
503
504 // phase 4 - float products of the widened keys for the heads of this pass
505
506#pragma unroll 8
507 for (int c = 0; c < N_EMBD_H2; ++c) {
508 float2 k_val[KEYS_PER_THREAD];
509#pragma unroll
510 for (int j = 0; j < KEYS_PER_THREAD; ++j) {
511 k_val[j] = __half22float2(k_shared[kl + j*KEY_LANES][c]);
512 }
513#pragma unroll
514 for (int h = 0; h < HEADS_PER_PASS; ++h) {
515 const float2 q_val = q_shared[h][tl][c];
516#pragma unroll
517 for (int j = 0; j < KEYS_PER_THREAD; ++j) {
518 qk[h0 + h][j] = fmaf(k_val[j].x, q_val.x, qk[h0 + h][j]);
519 qk[h0 + h][j] = fmaf(k_val[j].y, q_val.y, qk[h0 + h][j]);
520 }
521 }
522 }
523 }
524
525 // phase 5 - ReLU, weight, add the mask and write, consecutive threads write consecutive keys
526
527 float score[KEYS_PER_THREAD] = { 0.0f };
528
529#pragma unroll
530 for (int h = 0; h < N_HEAD; ++h) {
531#pragma unroll
532 for (int j = 0; j < KEYS_PER_THREAD; ++j) {
533 score[j] += fmaxf(qk[h][j], 0.0f) * w_shared[h][tl];
534 }
535 }
536
537 const int i_batch = start_batch + tl;
538 if (i_batch >= n_batch) {
539 return;
540 }
541
542 const half * m_base = (const half *) ((const char *) M + i_batch*nbm1 + (i_stream%nem3)*nbm3);
543 float * dst_base = (float *) ((char *) dst + i_batch*nb1 + i_stream*nb3);
544
545#pragma unroll
546 for (int j = 0; j < KEYS_PER_THREAD; ++j) {
547 const int i_kv = start_kv + kl + j*KEY_LANES;
548 if (i_kv < n_kv) {
549 dst_base[i_kv] = score[j] + __half2float(m_base[i_kv]);
550 }
551 }
552}
553
554#define LIGHTNING_INDEXER_CASE(lightning_indexer_kernel, n_embd, n_head, K, type_K) \
555 if (K->type == (type_K)) { \
556 lightning_indexer_kernel<WARPS_PER_BLOCK, K_VECS_PER_BLOCK, n_embd, n_head, type_K> \
557 <<<grid, block, 0, ctx.stream()>>>( \
558 q_d, k_d, w_d, m_d, dst_d, \
559 n_stream, n_batch, n_kv, \
560 nb1, nb2, nb3, \
561 nbq1, nbq2, nbq3, \
562 nbk1, nbk2, nbk3, \
563 nbw1, nbw2, nbw3, \
564 nbm1, nbm2, nbm3, \
565 nem3 \
566 ); \
567 } else
568
569void ggml_cuda_lightning_indexer(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
570 const ggml_tensor * q = dst->src[0];
571 const ggml_tensor * k = dst->src[1];
572 const ggml_tensor * w = dst->src[2]; // weights
573 const ggml_tensor * m = dst->src[3]; // mask
574
575 GGML_ASSERT(dst->type == GGML_TYPE_F32);
576 GGML_ASSERT( q->type == GGML_TYPE_F32);
577 GGML_ASSERT( w->type == GGML_TYPE_F32);
578 GGML_ASSERT( m->type == GGML_TYPE_F16);
579
580 GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
581 GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
582 GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
583 GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
584 GGML_TENSOR_LOCALS(int64_t, new, w, ne)
585 GGML_TENSOR_LOCALS(size_t, nbw, w, nb)
586 GGML_TENSOR_LOCALS(int64_t, nem, m, ne)
587 GGML_TENSOR_LOCALS(size_t, nbm, m, nb)
588 GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
589 GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
590
591 // input tensor rows must be contiguous
592 GGML_ASSERT(nbq0 == ggml_type_size(q->type));
593 GGML_ASSERT(nbk0 == ggml_type_size(k->type));
594 GGML_ASSERT(nbw0 == ggml_type_size(w->type));
595 GGML_ASSERT(nbm0 == ggml_type_size(m->type));
596
597 // dst cannot be transposed or permuted
598 GGML_ASSERT(nb0 == sizeof(float));
599 GGML_ASSERT(nb0 <= nb1);
600 GGML_ASSERT(nb1 <= nb2);
601 GGML_ASSERT(nb2 <= nb3);
602
603 const int n_embd = q->ne[0];
604 const int n_head = q->ne[1];
605 const int n_batch = q->ne[2];
606 const int n_stream = q->ne[3];
607 const int n_kv = k->ne[2];
608
609 const float * q_d = (const float *) q->data;
610 const char * k_d = (const char *) k->data;
611 const float * w_d = (const float *) w->data;
612 const half * m_d = (const half *) m->data;
613 float * dst_d = ( float *) dst->data;
614
615 const int device = ggml_cuda_get_device();
616 const int cc = ggml_cuda_info().devices[device].cc;
617
618 if (n_embd == 128 && n_head == 64) {
619#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
620 if (GGML_CUDA_CC_IS_NVIDIA(cc) && turing_mma_available(cc) && k->type != GGML_TYPE_F32 && k->type != GGML_TYPE_BF16) {
621 // use wmma kernel
622 constexpr int K_VECS_PER_BLOCK = 32;
623 constexpr int WARPS_PER_BLOCK = 8;
624
625 dim3 block(32, WARPS_PER_BLOCK);
626 int num_kv_blocks = (n_kv + (K_VECS_PER_BLOCK) - 1) / (K_VECS_PER_BLOCK);
627 dim3 grid(num_kv_blocks, n_batch, n_stream);
628
629 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 64, k, GGML_TYPE_F16)
630 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 64, k, GGML_TYPE_Q4_0)
631 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 64, k, GGML_TYPE_Q4_1)
632 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 64, k, GGML_TYPE_Q5_0)
633 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 64, k, GGML_TYPE_Q5_1)
634 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 64, k, GGML_TYPE_Q8_0)
635 GGML_ABORT("fatal error");
636 } else {
637#else // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
638 {
639#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
640 // use vector kernel
641 constexpr int K_VECS_PER_WARP = 8;
642 constexpr int WARPS_PER_BLOCK = 8;
643 constexpr int K_VECS_PER_BLOCK = K_VECS_PER_WARP * WARPS_PER_BLOCK;
644
645 dim3 block(32, WARPS_PER_BLOCK);
646 int num_kv_blocks = (n_kv + (K_VECS_PER_BLOCK) - 1) / (K_VECS_PER_BLOCK);
647 dim3 grid(num_kv_blocks, n_batch, n_stream);
648
649 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 64, k, GGML_TYPE_F16)
650 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 64, k, GGML_TYPE_Q4_0)
651 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 64, k, GGML_TYPE_Q4_1)
652 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 64, k, GGML_TYPE_Q5_0)
653 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 64, k, GGML_TYPE_Q5_1)
654 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 64, k, GGML_TYPE_Q8_0)
655 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 64, k, GGML_TYPE_BF16)
656 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 64, k, GGML_TYPE_F32)
657 GGML_ABORT("fatal error");
658 }
659 } else if (n_embd == 128 && n_head == 32) {
660#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
661 if (GGML_CUDA_CC_IS_NVIDIA(cc) && turing_mma_available(cc) && k->type != GGML_TYPE_F32 && k->type != GGML_TYPE_BF16) {
662 // use wmma kernel
663 constexpr int K_VECS_PER_BLOCK = 32;
664 constexpr int WARPS_PER_BLOCK = 8;
665
666 dim3 block(32, WARPS_PER_BLOCK);
667 int num_kv_blocks = (n_kv + (K_VECS_PER_BLOCK) - 1) / (K_VECS_PER_BLOCK);
668 dim3 grid(num_kv_blocks, n_batch, n_stream);
669
670 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 32, k, GGML_TYPE_F16)
671 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 32, k, GGML_TYPE_Q4_0)
672 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 32, k, GGML_TYPE_Q4_1)
673 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 32, k, GGML_TYPE_Q5_0)
674 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 32, k, GGML_TYPE_Q5_1)
675 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_wmma, 128, 32, k, GGML_TYPE_Q8_0)
676 GGML_ABORT("fatal error");
677 } else {
678#else // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
679 {
680#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
681 // use vector kernel
682 constexpr int K_VECS_PER_WARP = 8;
683 constexpr int WARPS_PER_BLOCK = 8;
684 constexpr int K_VECS_PER_BLOCK = K_VECS_PER_WARP * WARPS_PER_BLOCK;
685
686 dim3 block(32, WARPS_PER_BLOCK);
687 int num_kv_blocks = (n_kv + (K_VECS_PER_BLOCK) - 1) / (K_VECS_PER_BLOCK);
688 dim3 grid(num_kv_blocks, n_batch, n_stream);
689
690 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_F16)
691 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_Q4_0)
692 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_Q4_1)
693 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_Q5_0)
694 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_Q5_1)
695 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_Q8_0)
696 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_BF16)
697 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_F32)
698 GGML_ABORT("fatal error");
699 }
700 } else if (n_embd == 128 && n_head == 4 && n_batch >= LIGHTNING_INDEXER_TILE_TOKENS) {
701 // too few heads for a wmma tile, the tile kernel shares the keys across the tokens
702 constexpr int WARPS_PER_BLOCK = 8;
703 constexpr int K_VECS_PER_BLOCK = 64;
704
705 dim3 block(32, WARPS_PER_BLOCK);
706 int num_kv_blocks = (n_kv + (K_VECS_PER_BLOCK) - 1) / (K_VECS_PER_BLOCK);
707 int num_batch_blocks = (n_batch + LIGHTNING_INDEXER_TILE_TOKENS - 1) / LIGHTNING_INDEXER_TILE_TOKENS;
708 dim3 grid(num_kv_blocks, num_batch_blocks, n_stream);
709
710 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_tile, 128, 4, k, GGML_TYPE_F16)
711 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_tile, 128, 4, k, GGML_TYPE_Q4_0)
712 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_tile, 128, 4, k, GGML_TYPE_Q4_1)
713 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_tile, 128, 4, k, GGML_TYPE_Q5_0)
714 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_tile, 128, 4, k, GGML_TYPE_Q5_1)
715 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_tile, 128, 4, k, GGML_TYPE_Q8_0)
716 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_tile, 128, 4, k, GGML_TYPE_BF16)
717 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_tile, 128, 4, k, GGML_TYPE_F32)
718 GGML_ABORT("fatal error");
719 } else if (n_embd == 128 && n_head == 4) {
720 // a batch smaller than a token tile, use vector kernel
721 constexpr int K_VECS_PER_WARP = 8;
722 constexpr int WARPS_PER_BLOCK = 8;
723 constexpr int K_VECS_PER_BLOCK = K_VECS_PER_WARP * WARPS_PER_BLOCK;
724
725 dim3 block(32, WARPS_PER_BLOCK);
726 int num_kv_blocks = (n_kv + (K_VECS_PER_BLOCK) - 1) / (K_VECS_PER_BLOCK);
727 dim3 grid(num_kv_blocks, n_batch, n_stream);
728
729 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_F16)
730 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q4_0)
731 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q4_1)
732 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q5_0)
733 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q5_1)
734 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q8_0)
735 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_BF16)
736 LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_F32)
737 GGML_ABORT("fatal error");
738 } else {
739 GGML_ABORT("fatal error");
740 }
741}
742
743bool ggml_cuda_lightning_indexer_supported(int device, const ggml_tensor * dst) {
744 GGML_UNUSED(device);
745
746 const ggml_tensor * q = dst->src[0];
747 const ggml_tensor * k = dst->src[1];
748 const ggml_tensor * w = dst->src[2]; // weights
749 const ggml_tensor * m = dst->src[3]; // mask
750
751 GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
752 GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
753 GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
754 GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
755 GGML_TENSOR_LOCALS(int64_t, new, w, ne)
756 GGML_TENSOR_LOCALS(size_t, nbw, w, nb)
757 GGML_TENSOR_LOCALS(int64_t, nem, m, ne)
758 GGML_TENSOR_LOCALS(size_t, nbm, m, nb)
759 GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
760 GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
761
762 if (neq0 != 128) {
763 return false;
764 }
765
766 if (neq1 != 64 && neq1 != 32 && neq1 != 4) {
767 return false;
768 }
769
770 // alignment checks
771 for (const ggml_tensor * t : {q, k}) {
772 if (ggml_is_quantized(t->type)) {
773 continue;
774 }
775 for (size_t i = 1; i < GGML_MAX_DIMS; ++i) {
776 if (t->nb[i] % 16 != 0) {
777 return false;
778 }
779 }
780 }
781
782 switch(k->type) {
783 case GGML_TYPE_F32:
784 case GGML_TYPE_BF16:
785 case GGML_TYPE_F16:
786 case GGML_TYPE_Q8_0:
787 case GGML_TYPE_Q5_1:
788 case GGML_TYPE_Q5_0:
789 case GGML_TYPE_Q4_1:
790 case GGML_TYPE_Q4_0:
791 return true;
792 default:
793 return false;
794 }
795}