Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-sycl/fattn.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-sycl / fattn.cpp` 498 lines · 20487 bytes Raw Blame History 1 // 2 // MIT license 3 // Copyright (C) 2025 Intel Corporation 4 // SPDX-License-Identifier: MIT 5 // 6 7 // 8 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 9 // See https://llvm.org/LICENSE.txt for license information. 10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 11 // 12 13 14 #include 15 #include "dpct/helper.hpp" 16 #include "common.hpp" 17 #include "fattn-common.hpp" 18 #include "fattn-tile.hpp" 19 #include "fattn-vec.hpp" 20 #include "fattn.hpp" 21 #include "fattn-onednn.hpp" 22 #include "fattn-sparse.hpp" 23 #include "fattn-dpas.hpp" 24 25 #define FATTN_VEC_CASE(D, type_K, type_V) \ 26 { \ 27 const bool type_K_okay = K->type == (type_K) || (K->type == GGML_TYPE_F32 && (type_K) == GGML_TYPE_F16); \ 28 const bool type_V_okay = V->type == (type_V) || (V->type == GGML_TYPE_F32 && (type_V) == GGML_TYPE_F16); \ 29 if (Q->ne[0] == (D) && type_K_okay && type_V_okay) { \ 30 ggml_sycl_flash_attn_ext_vec_case(ctx, dst); \ 31 return; \ 32 } \ 33 } \ 34 35 #define FATTN_VEC_CASES_ALL_D(type_K, type_V) \ 36 FATTN_VEC_CASE( 64, type_K, type_V) \ 37 FATTN_VEC_CASE(128, type_K, type_V) \ 38 FATTN_VEC_CASE(256, type_K, type_V) \ 39 FATTN_VEC_CASE(512, type_K, type_V) \ 40 41 static void ggml_sycl_flash_attn_ext_vec(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 42 ggml_tensor * Q = dst->src[0]; 43 ggml_tensor * K = dst->src[1]; 44 ggml_tensor * V = dst->src[2]; 45 46 #ifdef GGML_SYCL_FA_ALL_QUANTS 47 FATTN_VEC_CASES_ALL_D(GGML_TYPE_F16, GGML_TYPE_F16) 48 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_0, GGML_TYPE_F16) 49 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_1, GGML_TYPE_F16) 50 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_0, GGML_TYPE_F16) 51 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_1, GGML_TYPE_F16) 52 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q8_0, GGML_TYPE_F16) 53 54 FATTN_VEC_CASES_ALL_D(GGML_TYPE_F16, GGML_TYPE_Q4_0) 55 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_0, GGML_TYPE_Q4_0) 56 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_1, GGML_TYPE_Q4_0) 57 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_0, GGML_TYPE_Q4_0) 58 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_1, GGML_TYPE_Q4_0) 59 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q8_0, GGML_TYPE_Q4_0) 60 61 FATTN_VEC_CASES_ALL_D(GGML_TYPE_F16, GGML_TYPE_Q4_1) 62 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_0, GGML_TYPE_Q4_1) 63 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_1, GGML_TYPE_Q4_1) 64 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_0, GGML_TYPE_Q4_1) 65 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_1, GGML_TYPE_Q4_1) 66 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q8_0, GGML_TYPE_Q4_1) 67 68 FATTN_VEC_CASES_ALL_D(GGML_TYPE_F16, GGML_TYPE_Q5_0) 69 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_0, GGML_TYPE_Q5_0) 70 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_1, GGML_TYPE_Q5_0) 71 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_0, GGML_TYPE_Q5_0) 72 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_1, GGML_TYPE_Q5_0) 73 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q8_0, GGML_TYPE_Q5_0) 74 75 FATTN_VEC_CASES_ALL_D(GGML_TYPE_F16, GGML_TYPE_Q5_1) 76 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_0, GGML_TYPE_Q5_1) 77 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_1, GGML_TYPE_Q5_1) 78 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_0, GGML_TYPE_Q5_1) 79 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_1, GGML_TYPE_Q5_1) 80 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q8_0, GGML_TYPE_Q5_1) 81 82 FATTN_VEC_CASES_ALL_D(GGML_TYPE_F16, GGML_TYPE_Q8_0) 83 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_0, GGML_TYPE_Q8_0) 84 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_1, GGML_TYPE_Q8_0) 85 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_0, GGML_TYPE_Q8_0) 86 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q5_1, GGML_TYPE_Q8_0) 87 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q8_0, GGML_TYPE_Q8_0) 88 #else 89 FATTN_VEC_CASES_ALL_D(GGML_TYPE_F16, GGML_TYPE_F16) 90 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q4_0, GGML_TYPE_Q4_0) 91 FATTN_VEC_CASES_ALL_D(GGML_TYPE_Q8_0, GGML_TYPE_Q8_0) 92 #endif // GGML_SYCL_FA_ALL_QUANTS 93 94 GGML_ABORT("Not match KV type in vec"); 95 } 96 97 // Best FlashAttention kernel for a specific GPU: 98 enum best_fattn_kernel { 99 BEST_FATTN_KERNEL_NONE = 0, 100 BEST_FATTN_KERNEL_VEC = 100, 101 BEST_FATTN_KERNEL_DPAS = 120, // q4_0 / q8_0 K/V decode on Xe2 XMX 102 BEST_FATTN_KERNEL_ONEDNN = 150, // oneDNN SDPA: native F16 (PR #25222) 103 BEST_FATTN_KERNEL_TILE = 200, 104 BEST_FATTN_KERNEL_MKL = 300, 105 }; 106 107 108 static best_fattn_kernel ggml_sycl_get_best_fattn_kernel(const int device, const ggml_tensor * dst) { 109 #ifndef SYCL_FLASH_ATTN 110 GGML_UNUSED(dst); 111 return BEST_FATTN_KERNEL_NONE; 112 #endif// SYCL_FLASH_ATTN 113 114 if(!g_ggml_sycl_enable_flash_attention) return BEST_FATTN_KERNEL_NONE; 115 116 const ggml_tensor * KQV = dst; 117 const ggml_tensor * Q = dst->src[0]; 118 const ggml_tensor * K = dst->src[1]; 119 const ggml_tensor * V = dst->src[2]; 120 const ggml_tensor * mask = dst->src[3]; 121 const ggml_tensor * sinks = dst->src[4]; 122 123 const int gqa_ratio = Q->ne[2] / K->ne[2]; 124 GGML_ASSERT(Q->ne[2] % K->ne[2] == 0); 125 126 float max_bias = 0.0f; 127 memcpy(&max_bias, (const float *) KQV->op_params + 1, sizeof(float)); 128 129 float logit_softcap = 0.0f; 130 memcpy(&logit_softcap, (const float *) KQV->op_params + 2, sizeof(float)); 131 132 bool gqa_opt_applies = gqa_ratio >= 2 && mask && max_bias == 0.0f && K->ne[1] % FATTN_KQ_STRIDE == 0; 133 134 // XMX-accelerated path: oneDNN SDPA (native F16 and dequant+non-F16). 135 // ONEDNN requires min 32 query tokens — short-circuit decode to avoid 136 // calling _supported() on every decode FA call. 137 if (Q->ne[1] >= 32 138 && ggml_sycl_flash_attn_ext_onednn_supported(dst)) { 139 return BEST_FATTN_KERNEL_ONEDNN; 140 } 141 142 // MKL path: XMX-accelerated GEMM for prompt processing (all KV cache types). 143 // The MKL kernel converts non-F16 K/V to F16 via to_fp16_sycl before GEMM, 144 // so quantized, F16, BF16, and F32 caches all benefit from XMX acceleration. 145 // Activates automatically when flash-attn is enabled (--flash-attn on or -fa) 146 // and n_kv >= 1024. Falls through to TILE/VEC for ALiBi, logit softcap, 147 // and mismatched batch dimensions (unsupported by the MKL kernel). 148 // Set GGML_SYCL_ENABLE_MKL_FA=0 to force TILE/VEC path for A/B testing. 149 // Example: GGML_SYCL_ENABLE_MKL_FA=0 llama-cli -m model.gguf -fa -ngl 99 ... 150 // Note: MKL GEMM calls are incompatible with SYCL graph capture replay. 151 const bool standard_shape = Q->ne[0] >= 64 && Q->ne[0] <= 512 && 152 Q->ne[0] % 64 == 0 && Q->ne[0] == V->ne[0]; 153 // GLM-4.7 Flash's MLA shape is already expressible by the MKL pipeline: 154 // KQ GEMM uses DKQ=576 while VKQ and output use DV=512. Keep this narrow 155 // until other mismatched K/V shapes have independent correctness data. 156 const bool glm_mla_shape = Q->ne[0] == 576 && K->ne[0] == 576 && 157 V->ne[0] == 512 && gqa_ratio == 20 && 158 K->type == GGML_TYPE_F16 && V->type == GGML_TYPE_F16; 159 160 if (g_ggml_sycl_enable_mkl_fa == 1 && mask && !sinks && gqa_ratio >= 2 && 161 (standard_shape || glm_mla_shape) && 162 Q->ne[1] >= 32 && K->ne[1] >= 1024 && 163 max_bias == 0.0f && logit_softcap == 0.0f && 164 (Q->ne[3] == K->ne[3] || K->ne[3] == 1)) { 165 // F16 K/V strides must be a multiple of ne[0]*2 (the natural row size 166 // in bytes). This passes both dense (nb1 == ne0*2) and interleaved 167 // (nb1 == H * ne0*2). Only pathological test strides like nb1=32 or 168 // nb1=75 for ne0=40 fall through to TILE. 169 bool kv_strides_ok = true; 170 for (const ggml_tensor * t : {K, V}) { 171 const bool glm_v_stride = glm_mla_shape && t == V && 172 V->view_src && V->nb[1] == K->nb[1]; 173 if (!glm_v_stride && t->type == GGML_TYPE_F16 && 174 t->nb[1] % (t->ne[0] * 2) != 0) { 175 kv_strides_ok = false; 176 break; 177 } 178 } 179 if (kv_strides_ok) { 180 return BEST_FATTN_KERNEL_MKL; 181 } 182 } 183 for (const ggml_tensor * t : {Q, K, V, mask}) { 184 if (t == nullptr || ggml_is_quantized(t->type)) { 185 continue; 186 } 187 for (size_t i = 1; i < GGML_MAX_DIMS; ++i) { 188 if (t->nb[i] % 16 != 0) { 189 gqa_opt_applies = false; 190 break; 191 } 192 } 193 } 194 195 switch (K->ne[0]) { 196 case 40: 197 case 64: 198 case 72: 199 case 80: 200 case 96: 201 case 128: 202 case 112: 203 case 256: 204 case 512: 205 if (V->ne[0] != K->ne[0]) { 206 return BEST_FATTN_KERNEL_NONE; 207 } 208 break; 209 case 576: 210 if (V->ne[0] != 512) { 211 return BEST_FATTN_KERNEL_NONE; 212 } 213 if (!gqa_opt_applies) { 214 return BEST_FATTN_KERNEL_NONE; 215 } 216 break; 217 default: 218 return BEST_FATTN_KERNEL_NONE; 219 } 220 221 #ifndef GGML_SYCL_FA_ALL_QUANTS 222 if (K->type != V->type) { 223 return BEST_FATTN_KERNEL_NONE; 224 } 225 #endif // GGML_SYCL_FA_ALL_QUANTS 226 227 switch (K->type) { 228 case GGML_TYPE_F32: 229 case GGML_TYPE_F16: 230 case GGML_TYPE_BF16: 231 break; 232 case GGML_TYPE_Q4_1: 233 case GGML_TYPE_Q5_0: 234 case GGML_TYPE_Q5_1: 235 #ifndef GGML_SYCL_FA_ALL_QUANTS 236 return BEST_FATTN_KERNEL_NONE; 237 #endif // GGML_SYCL_FA_ALL_QUANTS 238 case GGML_TYPE_Q4_0: 239 case GGML_TYPE_Q8_0: 240 break; 241 default: 242 return BEST_FATTN_KERNEL_NONE; 243 } 244 245 if (mask && mask->ne[2] != 1) { 246 return BEST_FATTN_KERNEL_NONE; 247 } 248 249 // q4_0 / q8_0 K/V decode and small batches on Xe2: read the cache directly, no F16 copy of K and V 250 if (ggml_sycl_flash_attn_ext_dpas_supported(device, dst)) { 251 return BEST_FATTN_KERNEL_DPAS; 252 } 253 254 // For small batch sizes the vector kernel may be preferable over the kernels optimized for large batch sizes. 255 // BF16 is excluded: the VEC kernel has no BF16 template (it needs GGML_SYCL_FA_ALL_QUANTS for non-F16/Q4_0/Q8_0). 256 const bool has_bf16 = (K->type == GGML_TYPE_BF16 || V->type == GGML_TYPE_BF16); 257 const bool can_use_vector_kernel = Q->ne[0] <= 512 && Q->ne[0] % 64 == 0 && K->ne[1] % FATTN_KQ_STRIDE == 0 258 && !has_bf16; 259 260 // Fused-XMX path: oneDNN Graph SDPA (flash attention). Strictly 261 // additive -- taken only when statically supported, otherwise falls through to VEC/TILE below. 262 if (ggml_sycl_flash_attn_ext_onednn_supported(dst)) { 263 return BEST_FATTN_KERNEL_ONEDNN; 264 } 265 266 // If there are no tensor cores available, use the generic tile kernel: 267 if (can_use_vector_kernel) { 268 if (!ggml_is_quantized(K->type) && !ggml_is_quantized(V->type)) { 269 if (Q->ne[1] == 1) { 270 if (!gqa_opt_applies) { 271 return BEST_FATTN_KERNEL_VEC; 272 } 273 } 274 } else { 275 if (Q->ne[1] <= 2) { 276 // TILE is faster for quantized KV decode on Xe2 (BMG); keep VEC on untested archs 277 const gpu_arch arch = ggml_sycl_info().devices[device].hw_info.arch; 278 if (arch == gpu_arch::intel_gpu_bmg_g21 || arch == gpu_arch::intel_gpu_bmg_g31) { 279 return BEST_FATTN_KERNEL_TILE; 280 } 281 return BEST_FATTN_KERNEL_VEC; 282 } 283 } 284 } 285 return BEST_FATTN_KERNEL_TILE; 286 } 287 288 void ggml_sycl_flash_attn_ext(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 289 ggml_sycl_set_device(ctx.device); 290 291 ggml_tensor * Q = dst->src[0]; 292 ggml_tensor * K = dst->src[1]; 293 ggml_tensor * V = dst->src[2]; 294 if (K && Q && K->ne[0] != Q->ne[0] && Q->ne[0] > 0 && K->ne[0] % Q->ne[0] == 0) { 295 const int64_t d = Q->ne[0]; 296 const int64_t n_heads = (K->ne[0] * K->ne[2]) / d; 297 K->ne[0] = d; 298 K->ne[2] = n_heads; 299 } 300 if (V && Q && V->ne[0] != Q->ne[0] && Q->ne[0] > 0 && V->ne[0] % Q->ne[0] == 0) { 301 const int64_t d = Q->ne[0]; 302 const int64_t n_heads = (V->ne[0] * V->ne[2]) / d; 303 V->ne[0] = d; 304 V->ne[2] = n_heads; 305 } 306 307 // sparse nodes are gathered down to n_kv_max rows and re-dispatched here 308 if (ggml_sycl_flash_attn_ext_sparse(ctx, dst)) { 309 return; 310 } 311 312 // n_kv watchdog: log when n_kv differs from the last FA call with 313 // the same D — helps detect cache-truncation issues. 314 static int nkv_debug = ggml_sycl_get_env("GGML_SYCL_MKL_FA_DEBUG", 0); 315 if (nkv_debug == 1) { 316 const ggml_tensor * K_dbg = dst->src[1]; 317 const ggml_tensor * V_dbg = dst->src[2]; 318 static int64_t last_nkv_d256 = 0, last_nkv_d512 = 0; 319 static int fa_call_seq = 0; 320 fa_call_seq++; 321 int64_t cur_nkv = K_dbg->ne[1]; 322 int Dk = (int)K_dbg->ne[0]; 323 const char * kname = "TILE"; 324 best_fattn_kernel k = ggml_sycl_get_best_fattn_kernel(ctx.device, dst); 325 if (k == BEST_FATTN_KERNEL_MKL) kname = "MKL"; 326 if (k == BEST_FATTN_KERNEL_ONEDNN) kname = "ONEDNN"; 327 if (k == BEST_FATTN_KERNEL_VEC) kname = "VEC"; 328 if (k == BEST_FATTN_KERNEL_DPAS) kname = "DPAS"; 329 int64_t delta = 0; 330 if (Dk == 256) { 331 delta = cur_nkv - last_nkv_d256; 332 last_nkv_d256 = cur_nkv; 333 } else if (Dk == 512) { 334 delta = cur_nkv - last_nkv_d512; 335 last_nkv_d512 = cur_nkv; 336 } 337 GGML_LOG_INFO("[FA-DISP] #%d %s D=%d n_kv=%lld delta=%lld " 338 "V_ne1=%lld\n", 339 fa_call_seq, kname, Dk, 340 (long long)cur_nkv, (long long)delta, 341 (long long)V_dbg->ne[1]); 342 } 343 344 const best_fattn_kernel fk = ggml_sycl_get_best_fattn_kernel(ggml_sycl_get_device(), dst); 345 switch (fk) { 346 case BEST_FATTN_KERNEL_NONE: 347 GGML_ABORT("Not support Flash-Attention"); 348 case BEST_FATTN_KERNEL_ONEDNN: 349 // guarded: ggml_sycl_flash_attn_ext_onednn() is only defined under GGML_SYCL_DNNL; 350 // the reference must be compiled out here or the GGML_SYCL_DNNL=0 build fails to link. 351 #if GGML_SYCL_DNNL 352 ggml_sycl_flash_attn_ext_onednn(ctx, dst); 353 #endif 354 break; 355 case BEST_FATTN_KERNEL_TILE: 356 ggml_sycl_flash_attn_ext_tile(ctx, dst); 357 break; 358 case BEST_FATTN_KERNEL_VEC: 359 ggml_sycl_flash_attn_ext_vec(ctx, dst); 360 break; 361 case BEST_FATTN_KERNEL_DPAS: 362 ggml_sycl_flash_attn_ext_dpas(ctx, dst); 363 break; 364 case BEST_FATTN_KERNEL_MKL: 365 ggml_sycl_flash_attn_ext_mkl(ctx, dst); 366 break; 367 } 368 369 // --- Output fingerprint (GGML_SYCL_MKL_FA_DIAG=1) --- 370 // Copy first 64 float output values to host for fingerprinting. 371 // Compare MKL vs TILE (GGML_SYCL_ENABLE_MKL_FA=0) to detect divergence. 372 // Only fingerprints the first 6 FA calls with n_kv >= 1024. 373 static int fa_diag = ggml_sycl_get_env("GGML_SYCL_MKL_FA_DIAG", 0); 374 static int fa_diag_count = 0; 375 if (fa_diag == 1 && fa_diag_count < 6) { 376 const ggml_tensor * K_diag = dst->src[1]; 377 const ggml_tensor * V_diag = dst->src[2]; 378 const ggml_tensor * Q_diag = dst->src[0]; 379 dpct::queue_ptr q = ctx.stream(); 380 if (K_diag->ne[1] >= 1024 && q->ext_oneapi_get_state() != sycl::ext::oneapi::experimental::queue_state::recording) { 381 fa_diag_count++; 382 float diag_buf[64]; 383 q->memcpy(diag_buf, dst->data, 64 * sizeof(float)); 384 q->wait(); 385 const char * kname = "???"; 386 best_fattn_kernel kb = ggml_sycl_get_best_fattn_kernel(ctx.device, dst); 387 if (kb == BEST_FATTN_KERNEL_ONEDNN) kname = "ONEDNN"; 388 if (kb == BEST_FATTN_KERNEL_MKL) kname = "MKL"; 389 if (kb == BEST_FATTN_KERNEL_TILE) kname = "TILE"; 390 if (kb == BEST_FATTN_KERNEL_VEC) kname = "VEC"; 391 if (kb == BEST_FATTN_KERNEL_DPAS) kname = "DPAS"; 392 GGML_LOG_INFO("[FA-DIAG] #%d %s D=%d n_kv=%lld n_q=%lld " 393 "n_qh=%lld n_kvh=%lld K=%s V=%s " 394 "nb1=%zu nb2=%zu first 64 floats:\n", 395 fa_diag_count, kname, 396 (int)K_diag->ne[0], (long long)K_diag->ne[1], 397 (long long)Q_diag->ne[1], 398 (long long)Q_diag->ne[2], (long long)K_diag->ne[2], 399 ggml_type_name(K_diag->type), 400 ggml_type_name(V_diag->type), 401 K_diag->nb[1], K_diag->nb[2]); 402 for (int i = 0; i < 64; i += 8) { 403 GGML_LOG_INFO(" [%2d] %08x %08x %08x %08x %08x %08x %08x %08x\n", 404 i, 405 *(unsigned *)&diag_buf[i+0], *(unsigned *)&diag_buf[i+1], 406 *(unsigned *)&diag_buf[i+2], *(unsigned *)&diag_buf[i+3], 407 *(unsigned *)&diag_buf[i+4], *(unsigned *)&diag_buf[i+5], 408 *(unsigned *)&diag_buf[i+6], *(unsigned *)&diag_buf[i+7]); 409 } 410 } 411 } 412 413 } 414 415 bool ggml_sycl_flash_attn_ext_supported(int device, const ggml_tensor * dst) { 416 return ggml_sycl_get_best_fattn_kernel(device, dst) != BEST_FATTN_KERNEL_NONE; 417 } 418 419 bool ggml_sycl_flash_attn_ext_needs_sync(int device, const ggml_tensor * dst) { 420 return ggml_sycl_get_best_fattn_kernel(device, dst) == BEST_FATTN_KERNEL_MKL; 421 } 422 423 bool ggml_sycl_flash_attn_ext_uses_onednn(int device, const ggml_tensor * dst) { 424 return ggml_sycl_get_best_fattn_kernel(device, dst) == BEST_FATTN_KERNEL_ONEDNN; 425 } 426 427 static uintptr_t ggml_sycl_fattn_reserve_halves(ggml_sycl_fattn_extra & extra, size_t n_halves) { 428 if (n_halves == 0) { 429 return 0; 430 } 431 extra.end = GGML_PAD(extra.end, SYCL_BUFFER_ALIGNMENT); 432 const uintptr_t block = extra.end; 433 extra.end += n_halves * sizeof(sycl::half); 434 return block; 435 } 436 437 ggml_sycl_fattn_extra ggml_sycl_fattn_get_extra(const ggml_tensor * dst) { 438 ggml_sycl_fattn_extra extra; 439 440 extra.end = (uintptr_t) dst->data + ggml_nbytes(dst); 441 442 if (dst->op != GGML_OP_FLASH_ATTN_EXT) { 443 return extra; 444 } 445 446 const ggml_tensor * Q = dst->src[0]; 447 const ggml_tensor * K = dst->src[1]; 448 const ggml_tensor * V = dst->src[2]; 449 if (!Q || !K || !V) { 450 return extra; 451 } 452 453 const int64_t d = K->ne[0]; 454 const int64_t H = Q->ne[2]; 455 const int64_t q = Q->ne[1]; 456 457 // calculate the worst-case memory consumption across all kernels 458 const bool onednn_supported = ggml_sycl_flash_attn_ext_onednn_supported(dst, /* use_shape_limit */ false); 459 460 const bool tile_needs_K = K->type != GGML_TYPE_F16; 461 const bool tile_needs_V = V->type != GGML_TYPE_F16; 462 463 const bool V_is_K_view = V->view_src && 464 (V->view_src == K || (V->view_src == K->view_src && V->view_offs == K->view_offs)); 465 466 size_t need_K = 0, need_V = 0, need_Q = 0, need_out = 0, need_scale = 0; 467 if (onednn_supported) { 468 need_Q = (size_t) H * q * d; 469 need_out = (size_t) H * q * d; 470 need_scale = 1; 471 // an f16 cache is bound in place, so it needs no staging copy 472 if (!ggml_sycl_fattn_onednn_binds_kv(K, V)) { 473 need_K = (size_t) ggml_nelements(K); 474 need_V = (size_t) ggml_nelements(V); 475 } 476 } 477 if (tile_needs_K) { 478 need_K = std::max(need_K, (size_t) ggml_nelements(K)); 479 } 480 if (tile_needs_V) { 481 need_V = std::max(need_V, (size_t) ggml_nelements(V)); 482 } 483 484 extra.Q_buffer_ptr = ggml_sycl_fattn_reserve_halves(extra, need_Q); 485 extra.K_buffer_ptr = ggml_sycl_fattn_reserve_halves(extra, need_K); 486 extra.V_buffer_ptr = (V_is_K_view && !onednn_supported && need_V) 487 ? extra.K_buffer_ptr 488 : ggml_sycl_fattn_reserve_halves(extra, need_V); 489 extra.scale_buffer_ptr = ggml_sycl_fattn_reserve_halves(extra, need_scale); 490 extra.out_buffer_ptr = ggml_sycl_fattn_reserve_halves(extra, need_out); 491 492 return extra; 493 } 494 495 size_t ggml_sycl_flash_attn_ext_get_alloc_size(const ggml_tensor * dst) { 496 const ggml_sycl_fattn_extra extra = ggml_sycl_fattn_get_extra(dst); 497 return (size_t) (extra.end - (uintptr_t) dst->data); 498 }