Arrcttacsrks commited on
Commit
0cdb0d4
·
verified ·
1 Parent(s): c64a3bf

Upload llama.cpp/ggml/src/ggml-cuda/fattn-tile-f16.cu with huggingface_hub

Browse files
llama.cpp/ggml/src/ggml-cuda/fattn-tile-f16.cu ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "common.cuh"
2
+ #include "fattn-common.cuh"
3
+ #include "fattn-tile-f16.cuh"
4
+
5
+ #define FATTN_KQ_STRIDE_TILE_F16 64
6
+
7
+ template<int D, int ncols, int nwarps, int parallel_blocks, bool use_logit_softcap> // D == head size
8
+ #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
9
+ __launch_bounds__(nwarps*WARP_SIZE, 1)
10
+ #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
11
+ static __global__ void flash_attn_tile_ext_f16(
12
+ const char * __restrict__ Q,
13
+ const char * __restrict__ K,
14
+ const char * __restrict__ V,
15
+ const char * __restrict__ mask,
16
+ float * __restrict__ dst,
17
+ float2 * __restrict__ dst_meta,
18
+ const float scale,
19
+ const float max_bias,
20
+ const float m0,
21
+ const float m1,
22
+ const uint32_t n_head_log2,
23
+ const float logit_softcap,
24
+ const int ne00,
25
+ const int ne01,
26
+ const int ne02,
27
+ const int ne03,
28
+ const int ne10,
29
+ const int ne11,
30
+ const int ne12,
31
+ const int ne13,
32
+ const int ne31,
33
+ const int nb31,
34
+ const int nb01,
35
+ const int nb02,
36
+ const int nb03,
37
+ const int nb11,
38
+ const int nb12,
39
+ const int nb13,
40
+ const int nb21,
41
+ const int nb22,
42
+ const int nb23,
43
+ const int ne0,
44
+ const int ne1,
45
+ const int ne2,
46
+ const int ne3) {
47
+ #ifdef FP16_AVAILABLE
48
+ // Skip unused kernel variants for faster compilation:
49
+ if (use_logit_softcap && !(D == 128 || D == 256)) {
50
+ NO_DEVICE_CODE;
51
+ return;
52
+ }
53
+
54
+ //In this kernel Q, K, V are matrices while i, j, k are matrix indices.
55
+
56
+ const int ic0 = (blockIdx.x / parallel_blocks) * ncols; // Index of the Q/QKV column to work on.
57
+ const int ip = blockIdx.x % parallel_blocks; // Index in group of blocks running for the same column in parallel.
58
+
59
+ const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
60
+ const float2 * Q_f2 = (const float2 *) (Q + nb02* blockIdx.y + nb01*ic0);
61
+ const half2 * K_h2 = (const half2 *) (K + nb12*(blockIdx.y / gqa_ratio));
62
+ const half2 * V_h2 = (const half2 *) (V + nb12*(blockIdx.y / gqa_ratio)); // K and V have same shape
63
+ const half * maskh = (const half *) mask + ne11*ic0;
64
+
65
+ const int stride_KV2 = nb11 / sizeof(half2);
66
+
67
+ const float slopef = get_alibi_slope(max_bias, blockIdx.y, n_head_log2, m0, m1);
68
+ const half slopeh = __float2half(slopef);
69
+
70
+ static_assert(D % (2*WARP_SIZE) == 0, "D not divisible by 2*WARP_SIZE == 64.");
71
+
72
+ __shared__ half KQ[ncols*FATTN_KQ_STRIDE_TILE_F16];
73
+ half2 * KQ2 = (half2 *) KQ;
74
+
75
+ __shared__ half2 KV_tmp[FATTN_KQ_STRIDE_TILE_F16][D/2 + 1]; // Pad D to avoid memory bank conflicts.
76
+
77
+ half kqmax[ncols/nwarps];
78
+ #pragma unroll
79
+ for (int j0 = 0; j0 < ncols; j0 += nwarps) {
80
+ kqmax[j0/nwarps] = -HALF_MAX_HALF;
81
+ }
82
+ half2 kqsum[ncols/nwarps] = {{0.0f, 0.0f}};
83
+
84
+ half2 VKQ[ncols/nwarps][(D/2)/WARP_SIZE] = {{{0.0f, 0.0f}}};
85
+
86
+ // Convert Q to half2 and store in registers:
87
+ __shared__ half2 Q_h2[ncols][D/2];
88
+ #pragma unroll
89
+ for (int j0 = 0; j0 < ncols; j0 += nwarps) {
90
+ const int j = j0 + threadIdx.y;
91
+
92
+ #pragma unroll
93
+ for (int i0 = 0; i0 < D/2; i0 += WARP_SIZE) {
94
+ const int i = i0 + threadIdx.x;
95
+
96
+ const float2 tmp = ic0 + j < ne01 ? Q_f2[j*(nb01/sizeof(float2)) + i] : make_float2(0.0f, 0.0f);
97
+ Q_h2[j][i] = make_half2(scale, scale) * make_half2(tmp.x, tmp.y);
98
+ }
99
+ }
100
+
101
+ __syncthreads();
102
+
103
+ const int k_start = parallel_blocks == 1 ? 0 : ip*FATTN_KQ_STRIDE_TILE_F16;
104
+ for (int k_VKQ_0 = k_start; k_VKQ_0 < ne11; k_VKQ_0 += parallel_blocks*FATTN_KQ_STRIDE_TILE_F16) {
105
+ // Calculate KQ tile and keep track of new maximum KQ values:
106
+
107
+ half kqmax_new[ncols/nwarps];
108
+ #pragma unroll
109
+ for (int j = 0; j < ncols/nwarps; ++j) {
110
+ kqmax_new[j] = kqmax[j];
111
+ }
112
+
113
+ #pragma unroll
114
+ for (int i_KQ_0 = 0; i_KQ_0 < FATTN_KQ_STRIDE_TILE_F16; i_KQ_0 += nwarps) {
115
+ const int i_KQ = i_KQ_0 + threadIdx.y;
116
+
117
+ #pragma unroll
118
+ for (int k_KQ_0 = 0; k_KQ_0 < D/2; k_KQ_0 += WARP_SIZE) {
119
+ const int k_KQ = k_KQ_0 + threadIdx.x;
120
+
121
+ KV_tmp[i_KQ][k_KQ] = K_h2[(k_VKQ_0 + i_KQ)*stride_KV2 + k_KQ];
122
+ }
123
+ }
124
+
125
+ __syncthreads();
126
+
127
+ half2 sum2[FATTN_KQ_STRIDE_TILE_F16/WARP_SIZE][ncols/nwarps] = {{{0.0f, 0.0f}}};
128
+
129
+ #pragma unroll
130
+ for (int k_KQ = 0; k_KQ < D/2; ++k_KQ) {
131
+ half2 K_k[FATTN_KQ_STRIDE_TILE_F16/WARP_SIZE];
132
+ half2 Q_k[ncols/nwarps];
133
+
134
+ #pragma unroll
135
+ for (int i_KQ_0 = 0; i_KQ_0 < FATTN_KQ_STRIDE_TILE_F16; i_KQ_0 += WARP_SIZE) {
136
+ const int i_KQ = i_KQ_0 + threadIdx.x;
137
+
138
+ K_k[i_KQ_0/WARP_SIZE] = KV_tmp[i_KQ][k_KQ];
139
+ }
140
+ #pragma unroll
141
+ for (int j_KQ_0 = 0; j_KQ_0 < ncols; j_KQ_0 += nwarps) {
142
+ const int j_KQ = j_KQ_0 + threadIdx.y;
143
+
144
+ Q_k[j_KQ_0/nwarps] = Q_h2[j_KQ][k_KQ];
145
+ }
146
+
147
+ #pragma unroll
148
+ for (int i_KQ_0 = 0; i_KQ_0 < FATTN_KQ_STRIDE_TILE_F16; i_KQ_0 += WARP_SIZE) {
149
+ #pragma unroll
150
+ for (int j_KQ_0 = 0; j_KQ_0 < ncols; j_KQ_0 += nwarps) {
151
+ sum2[i_KQ_0/WARP_SIZE][j_KQ_0/nwarps] += K_k[i_KQ_0/WARP_SIZE]*Q_k[j_KQ_0/nwarps];
152
+ }
153
+ }
154
+ }
155
+
156
+ #pragma unroll
157
+ for (int i_KQ_0 = 0; i_KQ_0 < FATTN_KQ_STRIDE_TILE_F16; i_KQ_0 += WARP_SIZE) {
158
+ const int i_KQ = i_KQ_0 + threadIdx.x;
159
+
160
+ #pragma unroll
161
+ for (int j_KQ_0 = 0; j_KQ_0 < ncols; j_KQ_0 += nwarps) {
162
+ const int j_KQ = j_KQ_0 + threadIdx.y;
163
+
164
+ half sum;
165
+ if (use_logit_softcap) {
166
+ const float2 tmp = __half22float2(sum2[i_KQ_0/WARP_SIZE][j_KQ_0/nwarps]);
167
+ sum = logit_softcap * tanhf(tmp.x + tmp.y);
168
+ } else {
169
+ sum = __low2half(sum2[i_KQ_0/WARP_SIZE][j_KQ_0/nwarps]) + __high2half(sum2[i_KQ_0/WARP_SIZE][j_KQ_0/nwarps]);
170
+ }
171
+ sum += mask ? slopeh*maskh[j_KQ*ne11 + k_VKQ_0 + i_KQ] : __float2half(0.0f);
172
+
173
+ kqmax_new[j_KQ_0/nwarps] = ggml_cuda_hmax(kqmax_new[j_KQ_0/nwarps], sum);
174
+
175
+ KQ[j_KQ*FATTN_KQ_STRIDE_TILE_F16 + i_KQ] = sum;
176
+ }
177
+ }
178
+
179
+ __syncthreads();
180
+
181
+ #pragma unroll
182
+ for (int j0 = 0; j0 < ncols; j0 += nwarps) {
183
+ const int j = j0 + threadIdx.y;
184
+
185
+ kqmax_new[j0/nwarps] = warp_reduce_max(kqmax_new[j0/nwarps]);
186
+ const half2 KQ_max_scale = __half2half2(hexp(kqmax[j0/nwarps] - kqmax_new[j0/nwarps]));
187
+ kqmax[j0/nwarps] = kqmax_new[j0/nwarps];
188
+
189
+ #pragma unroll
190
+ for (int i0 = 0; i0 < FATTN_KQ_STRIDE_TILE_F16/2; i0 += WARP_SIZE) {
191
+ const int i = i0 + threadIdx.x;
192
+
193
+ const half2 diff = KQ2[j*(FATTN_KQ_STRIDE_TILE_F16/2) + i] - __half2half2(kqmax[j0/nwarps]);
194
+ const half2 val = h2exp(diff);
195
+ kqsum[j0/nwarps] = kqsum[j0/nwarps]*KQ_max_scale + val;
196
+ KQ2[j*(FATTN_KQ_STRIDE_TILE_F16/2) + i] = val;
197
+ }
198
+
199
+ #pragma unroll
200
+ for (int i0 = 0; i0 < D/2; i0 += WARP_SIZE) {
201
+ VKQ[j0/nwarps][i0/WARP_SIZE] *= KQ_max_scale;
202
+ }
203
+ }
204
+
205
+ __syncthreads();
206
+
207
+ #pragma unroll
208
+ for (int k0 = 0; k0 < FATTN_KQ_STRIDE_TILE_F16; k0 += nwarps) {
209
+ const int k = k0 + threadIdx.y;
210
+
211
+ #pragma unroll
212
+ for (int i0 = 0; i0 < D/2; i0 += WARP_SIZE) {
213
+ const int i = i0 + threadIdx.x;
214
+
215
+ KV_tmp[k][i] = V_h2[(k_VKQ_0 + k)*stride_KV2 + i];
216
+ }
217
+ }
218
+
219
+ __syncthreads();
220
+
221
+ #pragma unroll
222
+ for (int k0 = 0; k0 < FATTN_KQ_STRIDE_TILE_F16; k0 += 2) {
223
+ half2 V_k[(D/2)/WARP_SIZE][2];
224
+ half2 KQ_k[ncols/nwarps];
225
+
226
+ #pragma unroll
227
+ for (int i0 = 0; i0 < D/2; i0 += WARP_SIZE) {
228
+ const int i = i0 + threadIdx.x;
229
+
230
+ V_k[i0/WARP_SIZE][0] = KV_tmp[k0 + 0][i];
231
+ V_k[i0/WARP_SIZE][1] = KV_tmp[k0 + 1][i];
232
+ }
233
+ #pragma unroll
234
+ for (int j0 = 0; j0 < ncols; j0 += nwarps) {
235
+ const int j = j0 + threadIdx.y;
236
+
237
+ KQ_k[j0/nwarps] = KQ2[j*(FATTN_KQ_STRIDE_TILE_F16/2) + k0/2];
238
+ }
239
+
240
+ #pragma unroll
241
+ for (int i0 = 0; i0 < D/2; i0 += WARP_SIZE) {
242
+ #pragma unroll
243
+ for (int j0 = 0; j0 < ncols; j0 += nwarps) {
244
+ VKQ[j0/nwarps][i0/WARP_SIZE] += V_k[i0/WARP_SIZE][0]* __low2half2(KQ_k[j0/nwarps]);
245
+ VKQ[j0/nwarps][i0/WARP_SIZE] += V_k[i0/WARP_SIZE][1]*__high2half2(KQ_k[j0/nwarps]);
246
+ }
247
+ }
248
+ }
249
+
250
+ __syncthreads();
251
+ }
252
+
253
+ #pragma unroll
254
+ for (int j_VKQ_0 = 0; j_VKQ_0 < ncols; j_VKQ_0 += nwarps) {
255
+ const int j_VKQ = j_VKQ_0 + threadIdx.y;
256
+
257
+ if (ic0 + j_VKQ >= ne01) {
258
+ return;
259
+ }
260
+
261
+ half kqsum_j = __low2half(kqsum[j_VKQ_0/nwarps]) + __high2half(kqsum[j_VKQ_0/nwarps]);
262
+ kqsum_j = warp_reduce_sum((float)kqsum_j);
263
+
264
+ #pragma unroll
265
+ for (int i00 = 0; i00 < D; i00 += 2*WARP_SIZE) {
266
+ const int i0 = i00 + 2*threadIdx.x;
267
+
268
+ half2 dst_val = VKQ[j_VKQ_0/nwarps][i0/(2*WARP_SIZE)];
269
+ if (parallel_blocks == 1) {
270
+ dst_val /= __half2half2(kqsum_j);
271
+ }
272
+ const int j_dst = (ic0 + j_VKQ)*parallel_blocks + ip;
273
+ dst[j_dst*D*gridDim.y + D*blockIdx.y + i0 + 0] = __low2float(dst_val);
274
+ dst[j_dst*D*gridDim.y + D*blockIdx.y + i0 + 1] = __high2float(dst_val);
275
+ }
276
+
277
+ if (parallel_blocks != 1 && threadIdx.x == 0) {
278
+ dst_meta[(ic0 + j_VKQ)*gridDim.y*parallel_blocks + blockIdx.y*parallel_blocks + ip] = make_float2(kqmax[j_VKQ_0/nwarps], kqsum_j);
279
+ }
280
+ }
281
+ #else
282
+ NO_DEVICE_CODE;
283
+ #endif // FP16_AVAILABLE
284
+ }
285
+
286
+ template <int cols_per_block, int parallel_blocks, bool use_logit_softcap>
287
+ void launch_fattn_tile_f16_64_128(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
288
+ const ggml_tensor * Q = dst->src[0];
289
+ switch (Q->ne[0]) {
290
+ case 64: {
291
+ constexpr int D = 64;
292
+ constexpr int nwarps = 8;
293
+ fattn_kernel_t fattn_kernel = flash_attn_tile_ext_f16<D, cols_per_block, nwarps, parallel_blocks, use_logit_softcap>;
294
+ launch_fattn<D, parallel_blocks>(ctx, dst, fattn_kernel, nwarps, cols_per_block, true, true);
295
+ } break;
296
+ case 128: {
297
+ constexpr int D = 128;
298
+ constexpr int nwarps = 8;
299
+ fattn_kernel_t fattn_kernel = flash_attn_tile_ext_f16<D, cols_per_block, nwarps, parallel_blocks, use_logit_softcap>;
300
+ launch_fattn<D, parallel_blocks>(ctx, dst, fattn_kernel, nwarps, cols_per_block, true, true);
301
+ } break;
302
+ default: {
303
+ GGML_ABORT("FlashAttention without tensor cores only supports head sizes 64 and 128.");
304
+ } break;
305
+ }
306
+ }
307
+
308
+ void ggml_cuda_flash_attn_ext_tile_f16(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
309
+ const ggml_tensor * KQV = dst;
310
+ const ggml_tensor * Q = dst->src[0];
311
+
312
+ const int32_t precision = KQV->op_params[3];
313
+ GGML_ASSERT(precision == GGML_PREC_DEFAULT);
314
+
315
+ float logit_softcap;
316
+ memcpy(&logit_softcap, (const float *) KQV->op_params + 2, sizeof(float));
317
+
318
+ if (Q->ne[1] <= 16) {
319
+ constexpr int cols_per_block = 16;
320
+ constexpr int parallel_blocks = 4;
321
+ if (logit_softcap == 0.0f) {
322
+ constexpr bool use_logit_softcap = false;
323
+ launch_fattn_tile_f16_64_128<cols_per_block, parallel_blocks, use_logit_softcap>(ctx, dst);
324
+ } else {
325
+ constexpr bool use_logit_softcap = true;
326
+ launch_fattn_tile_f16_64_128<cols_per_block, parallel_blocks, use_logit_softcap>(ctx, dst);
327
+ }
328
+ return;
329
+ }
330
+
331
+ if (Q->ne[1] <= 32) {
332
+ constexpr int cols_per_block = 32;
333
+ constexpr int parallel_blocks = 4;
334
+ if (logit_softcap == 0.0f) {
335
+ constexpr bool use_logit_softcap = false;
336
+ launch_fattn_tile_f16_64_128<cols_per_block, parallel_blocks, use_logit_softcap>(ctx, dst);
337
+ } else {
338
+ constexpr bool use_logit_softcap = true;
339
+ launch_fattn_tile_f16_64_128<cols_per_block, parallel_blocks, use_logit_softcap>(ctx, dst);
340
+ }
341
+ return;
342
+ }
343
+
344
+ constexpr int cols_per_block = 32;
345
+ constexpr int parallel_blocks = 1;
346
+ if (logit_softcap == 0.0f) {
347
+ constexpr bool use_logit_softcap = false;
348
+ launch_fattn_tile_f16_64_128<cols_per_block, parallel_blocks, use_logit_softcap>(ctx, dst);
349
+ } else {
350
+ constexpr bool use_logit_softcap = true;
351
+ launch_fattn_tile_f16_64_128<cols_per_block, parallel_blocks, use_logit_softcap>(ctx, dst);
352
+ }
353
+ }