Arrcttacsrks commited on
Commit
fb4c276
1 Parent(s): c2169ac

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

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