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-dpas.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-dpas.cpp` 722 lines ยท 34467 bytes Raw Blame History 1 #include 2 #include 3 #include "dpct/helper.hpp" 4 #include "common.hpp" 5 #include "fattn-common.hpp" 6 #include "fattn-dpas.hpp" 7 8 // GGML_SYCL_SUPPORT_XMX=OFF builds skip the DPAS below, as do spir64_gen builds for sub-group 8 9 // targets (Xe-HPG) or mixed target lists, which cannot compile it 10 #if !defined(GGML_SYCL_SUPPORT_XMX) || (defined(GGML_SYCL_XMX_AOT_SG) && GGML_SYCL_XMX_AOT_SG != 16) 11 #define GGML_SYCL_FA_DPAS_BUILD 0 12 #else 13 #define GGML_SYCL_FA_DPAS_BUILD 1 14 #endif 15 16 #if GGML_SYCL_FA_DPAS_BUILD 17 static constexpr int FA_DPAS_WG = 256; // work-group size 18 static constexpr int FA_DPAS_NSG = 16; // sub-groups of 16 lanes 19 static constexpr int FA_DPAS_MAX_Q = 31; // larger batches go to the prefill kernels 20 static constexpr int FA_DPAS_MAX_G = 32; // query heads per KV head 21 static constexpr float FA_DPAS_MINF = -1.0e30f; // finite start value of the running max 22 23 static bool fattn_dpas_support_arch(gpu_arch arch) { 24 return arch == gpu_arch::intel_gpu_bmg_g21 || arch == gpu_arch::intel_gpu_bmg_g31 || 25 arch == gpu_arch::intel_gpu_lnl_m || arch == gpu_arch::intel_gpu_ptl_h || 26 arch == gpu_arch::intel_gpu_ptl_u || arch == gpu_arch::intel_gpu_wcl; 27 } 28 29 // head sizes with a compiled kernel (multiples of the 32-dim q4_0 / q8_0 block) 30 static bool fattn_dpas_head_ok(int64_t d) { 31 return d == 64 || d == 96 || d == 128 || d == 256 || d == 512; 32 } 33 #endif // GGML_SYCL_FA_DPAS_BUILD 34 35 bool ggml_sycl_flash_attn_ext_dpas_supported(int device, const ggml_tensor * dst) { 36 #if !GGML_SYCL_FA_DPAS_BUILD 37 GGML_UNUSED(device); 38 GGML_UNUSED(dst); 39 return false; 40 #else 41 if (!g_ggml_sycl_fa_dpas || !fattn_dpas_support_arch(ggml_sycl_info().devices[device].hw_info.arch)) { 42 return false; 43 } 44 45 const ggml_tensor * Q = dst->src[0]; 46 const ggml_tensor * K = dst->src[1]; 47 const ggml_tensor * V = dst->src[2]; 48 const ggml_tensor * mask = dst->src[3]; 49 const ggml_tensor * sinks = dst->src[4]; 50 51 if (Q->type != GGML_TYPE_F32 || (K->type != GGML_TYPE_Q4_0 && K->type != GGML_TYPE_Q8_0) || V->type != K->type || 52 (sinks && sinks->type != GGML_TYPE_F32)) { 53 return false; 54 } 55 if (!fattn_dpas_head_ok(K->ne[0]) || V->ne[0] != K->ne[0] || Q->ne[0] != K->ne[0]) { 56 return false; 57 } 58 if (Q->ne[1] < 1 || Q->ne[1] > FA_DPAS_MAX_Q || K->ne[1] < 1) { 59 return false; 60 } 61 if (K->ne[2] < 1 || V->ne[2] != K->ne[2] || Q->ne[2] % K->ne[2] != 0 || Q->ne[2] / K->ne[2] > FA_DPAS_MAX_G) { 62 return false; 63 } 64 if (Q->ne[3] != K->ne[3] || V->ne[3] != K->ne[3]) { 65 return false; 66 } 67 68 float max_bias = 0.0f; 69 float logit_softcap = 0.0f; 70 memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float)); 71 memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float)); 72 if (max_bias != 0.0f || logit_softcap != 0.0f) { 73 return false; 74 } 75 76 if (mask && (mask->type != GGML_TYPE_F16 || mask->ne[2] != 1 || mask->nb[0] != sizeof(sycl::half) || 77 mask->ne[0] < K->ne[1] || mask->ne[1] < Q->ne[1])) { 78 return false; 79 } 80 if (sinks && (sinks->nb[0] != sizeof(float) || sinks->ne[0] < Q->ne[2])) { 81 return false; 82 } 83 if (Q->nb[0] != sizeof(float)) { 84 return false; 85 } 86 // q4_0 / q8_0 rows are read as dwords from the dword at or before the row start: rows must start on 2-byte bounds 87 // (head sizes with an odd block count, e.g. 96, put every other head 2 bytes into a dword) 88 // so a row's first/last dword may hold 2 bytes outside it; aligned dwords never cross the 128-byte buffer alignment 89 for (const ggml_tensor * t : { K, V }) { 90 if (t->nb[0] != ggml_type_size(t->type) || t->nb[1] % 2 != 0 || t->nb[2] % 2 != 0 || t->nb[3] % 2 != 0 || 91 (uintptr_t) t->data % 2 != 0) { 92 return false; 93 } 94 } 95 return true; 96 #endif 97 } 98 99 #if GGML_SYCL_FA_DPAS_BUILD 100 101 typedef short fa_dpas_short8 __attribute__((ext_vector_type(8))); 102 typedef int fa_dpas_int4 __attribute__((ext_vector_type(4))); 103 typedef int fa_dpas_int8 __attribute__((ext_vector_type(8))); 104 typedef float fa_dpas_float8 __attribute__((ext_vector_type(8))); 105 106 // Xe2 DPAS (SIMD16, systolic depth 8, repeat count 8) as inline vISA: joint_matrix has no s4 type. 107 // d = a x b. a: 8 rows x 32 s8, 2 per lane per row. b: 32 x 16 s4, 16 bytes per lane, one column per lane. 108 static inline void fa_dpas_s8_s4(const fa_dpas_short8 & a, const fa_dpas_int4 & b, fa_dpas_int8 & d) { 109 #ifdef __SYCL_DEVICE_ONLY__ 110 __asm__("{\n" 111 ".decl DB v_type=G type=ud num_elts=64 align=GRF alias=<%1,0>\n" 112 ".decl DA v_type=G type=ud num_elts=64 align=GRF alias=<%2,0>\n" 113 "dpas.s4.s8.8.8 (M1, 16) %0.0 %%null.0 DB.0 DA(0,0)\n" 114 "}\n" 115 : "=rw"(d) 116 : "rw"(b), "rw"(a)); 117 #else 118 d = 0; 119 GGML_UNUSED(a); 120 GGML_UNUSED(b); 121 #endif 122 } 123 124 // d = a x b. a: 8 rows x 32 s8, 2 per lane per row. b: 32 x 16 s8, 32 bytes per lane, one column per lane. 125 static inline void fa_dpas_s8_s8(const fa_dpas_short8 & a, const fa_dpas_int8 & b, fa_dpas_int8 & d) { 126 #ifdef __SYCL_DEVICE_ONLY__ 127 __asm__("{\n" 128 ".decl DB v_type=G type=ud num_elts=128 align=GRF alias=<%1,0>\n" 129 ".decl DA v_type=G type=ud num_elts=64 align=GRF alias=<%2,0>\n" 130 "dpas.s8.s8.8.8 (M1, 16) %0.0 %%null.0 DB.0 DA(0,0)\n" 131 "}\n" 132 : "=rw"(d) 133 : "rw"(b), "rw"(a)); 134 #else 135 d = 0; 136 GGML_UNUSED(a); 137 GGML_UNUSED(b); 138 #endif 139 } 140 141 // acc += a x b. a: 8 rows x 16 f16, one per lane per row. b: 16 x 16 f16 as pairs, 8 dwords per lane, one column per lane. 142 static inline void fa_dpas_f16(const fa_dpas_short8 & a, const fa_dpas_int8 & b, fa_dpas_float8 & acc) { 143 #ifdef __SYCL_DEVICE_ONLY__ 144 __asm__("{\n" 145 ".decl DB v_type=G type=ud num_elts=128 align=GRF alias=<%1,0>\n" 146 ".decl DA v_type=G type=ud num_elts=64 align=GRF alias=<%2,0>\n" 147 "dpas.hf.hf.8.8 (M1, 16) %0.0 %0.0 DB.0 DA(0,0)\n" 148 "}\n" 149 : "+rw"(acc) 150 : "rw"(b), "rw"(a)); 151 #else 152 GGML_UNUSED(a); 153 GGML_UNUSED(b); 154 GGML_UNUSED(acc); 155 #endif 156 } 157 158 // Block b of a row = f16 scale + NQ dwords of quants (q4_0: 18 bytes, NQ 4; q8_0: 34 bytes, NQ 8), at byte (2 + 4 NQ) b. 159 // w = the NQ + 1 dwords from that byte / 4. An odd block starts 2 bytes into w[0]. 160 template static inline void fa_dpas_split_block(const uint32_t * w, bool odd, uint32_t & sc, uint32_t * q) { 161 if (!odd) { 162 sc = w[0] & 0xFFFFu; 163 #pragma unroll 164 for (int i = 0; i < NQ; ++i) { 165 q[i] = (w[i] >> 16) | (w[i + 1] << 16); 166 } 167 } else { 168 sc = w[0] >> 16; 169 #pragma unroll 170 for (int i = 0; i < NQ; ++i) { 171 q[i] = w[i + 1]; 172 } 173 } 174 } 175 176 struct fa_dpas_params { 177 const char * Q; 178 const char * K; 179 const char * V; 180 const char * mask; 181 float * dst; 182 float * parts; // per-split partial results, nsplit > 1 only 183 sycl::float2 * meta; // per-split (max, sum), nsplit > 1 only 184 float scale; 185 const float * sinks; // per query head softmax sink logit, or null 186 int G; // query heads per KV head 187 int nq; // query tokens per work-group 188 int nqc; // token chunks 189 int ne01; 190 int ne02; 191 int ne11; 192 int ne33; 193 int nsplit; 194 int chunk; // keys per split 195 int64_t nb01, nb02, nb03; 196 int64_t nb11, nb12, nb13; 197 int64_t nb21, nb22, nb23; 198 int64_t nb31, nb33; 199 }; 200 201 // One work-group = one KV head x nq query tokens x one slice of the keys: S = Q K^T by s8 x s4 / s8 x s8 DPAS on the 202 // raw K blocks, online softmax, O^T += V^T P^T by f16 DPAS. ST: stage K/V tiles in SLM (1: 16-byte, 2: dword loads). 203 // LG: 256-register file (no spills at 32 rows, same occupancy). Q8: K/V are q8_0, else q4_0. 204 template 205 static void fattn_dpas_kernel(const fa_dpas_params p, const int nkvh, const int ne03, dpct::queue_ptr stream) { 206 constexpr int RMAX = 16 * NR16; 207 constexpr int RTMAX = RMAX / 8; // 8-row tiles in Q.K 208 constexpr int TK = 16 * KG; // keys per tile 209 static_assert(QK4_0 == QK8_0, "q4_0 and q8_0 blocks hold the same number of dims"); 210 constexpr int BB = Q8 ? (int) sizeof(block_q8_0) : (int) sizeof(block_q4_0); // bytes per block 211 constexpr int NQ = (BB - 2) / 4; // quant dwords per block 212 constexpr int NB = D / QK4_0; // blocks per row 213 constexpr int DPS = D <= 128 ? 8 : D <= 256 ? 16 : 32; // head dims per sub-group in P.V, inside one 32-dim block 214 constexpr int NPV = D / DPS; // sub-groups doing P.V, the rest only reach its barrier 215 constexpr bool ALLPV = NPV == FA_DPAS_NSG; // every sub-group does P.V: no runtime check in the loops 216 constexpr int XT = DPS / 8; // 8-dim tiles per sub-group in P.V 217 constexpr int PS = TK + 16; // f16 row stride of P 218 constexpr int RW = NB * BB / 4; // dwords per row 219 constexpr bool ODD = (NB * BB) % 4 != 0; // odd block count: rows may start 2 bytes into a dword 220 constexpr int SW = RW + 1; // odd SLM row stride: no bank conflicts for lane = key 221 static_assert(D % QK4_0 == 0 && D % DPS == 0 && NPV <= FA_DPAS_NSG && QK4_0 % DPS == 0, "unsupported head size"); 222 static_assert(ST != 1 || RW % 4 == 0, "16-byte staging needs rows of 16-byte multiples"); 223 224 stream->submit([&](sycl::handler & cgh) { 225 sycl::local_accessor sQ8(sycl::range<1>(RTMAX * NB * 16), cgh); // Q s8 [tile][block][lane] 226 sycl::local_accessor sDQ(sycl::range<1>(RTMAX * NB), cgh); // Q scales [tile][block] 227 sycl::local_accessor sS(sycl::range<1>(RMAX * TK), cgh); // S [row][key] 228 sycl::local_accessor sP(sycl::range<1>(RMAX * PS / 16), cgh); // P f16 [row][key] 229 sycl::local_accessor sM(sycl::range<1>(RMAX), cgh); 230 sycl::local_accessor sL(sycl::range<1>(RMAX), cgh); 231 sycl::local_accessor sA(sycl::range<1>(RMAX), cgh); 232 sycl::local_accessor sKV(sycl::range<1>(ST ? TK * SW : 1), cgh); 233 const sycl::nd_range<3> ndr(sycl::range<3>((size_t) ne03 * p.nqc, nkvh, (size_t) p.nsplit * FA_DPAS_WG), 234 sycl::range<3>(1, 1, FA_DPAS_WG)); 235 auto kern = [=](sycl::nd_item<3> it) [[sycl::reqd_sub_group_size(16)]] { 236 const int tid = it.get_local_id(2); 237 const int split = it.get_group(2); 238 const int kvh = it.get_group(1); 239 const int seq = it.get_group(0) / p.nqc; 240 const int c0 = (it.get_group(0) % p.nqc) * p.nq; 241 const int G = p.G; 242 const int R = p.nq * G; 243 const int RT = (R + 7) / 8; 244 auto sg = it.get_sub_group(); 245 const int w = tid / 16; 246 const int lane = tid % 16; 247 short * sQs = (short *) &sQ8[0]; 248 short * sPs = (short *) &sP[0]; 249 250 // Q -> s8, one scale per row and block. Lane l holds the 2 dims of K column elements 2 l, 2 l + 1: 251 // q4_0 packs dims l and l + 16 into byte l, q8_0 stores dims in order 252 for (int e = tid; e < RT * 8 * NB; e += FA_DPAS_WG) { 253 const int r = e / NB; 254 const int b = e % NB; 255 const int c = c0 + r / G; 256 const bool real = r < R && c < p.ne01; 257 const float * qr = (const float *) (p.Q + seq * p.nb03 + (int64_t) (real ? c : 0) * p.nb01 + 258 (int64_t) (kvh * G + (real ? r % G : 0)) * p.nb02) + b * QK4_0; 259 float x[QK4_0]; 260 float amax = 0.0f; 261 #pragma unroll 262 for (int i = 0; i < QK4_0; ++i) { 263 x[i] = real ? qr[i] * p.scale : 0.0f; 264 amax = sycl::fmax(amax, sycl::fabs(x[i])); 265 } 266 const float dq = amax / 127.0f; 267 const float iq = amax > 0.0f ? 127.0f / amax : 0.0f; 268 const int t = r / 8; 269 const int m = r % 8; 270 ((float *) &sDQ[0])[(t * NB + b) * 8 + m] = dq; 271 #pragma unroll 272 for (int l = 0; l < 16; ++l) { 273 const int lo = (int) sycl::rint(x[Q8 ? 2 * l : l] * iq); 274 const int hi = (int) sycl::rint(x[Q8 ? 2 * l + 1 : l + 16] * iq); 275 sQs[((t * NB + b) * 16 + l) * 8 + m] = (short) ((lo & 0xFF) | ((hi & 0xFF) << 8)); 276 } 277 } 278 // rows without a query keep P = 0 279 for (int e = tid; e < RMAX * PS; e += FA_DPAS_WG) { 280 sPs[e] = 0; 281 } 282 if (tid < RMAX) { 283 // the sink is one more key with logit sinks[head] and no value; the first key slice owns it 284 const bool sink = p.sinks && split == 0 && tid < R; 285 sM[tid] = sink ? p.sinks[kvh * G + tid % G] : FA_DPAS_MINF; 286 sL[tid] = sink ? 1.0f : 0.0f; 287 sA[tid] = 1.0f; 288 } 289 it.barrier(sycl::access::fence_space::local_space); 290 291 const int k_begin = split * p.chunk; 292 const int k_end = sycl::min(k_begin + p.chunk, p.ne11); 293 const char * Kh = p.K + seq * p.nb13 + kvh * p.nb12; 294 const char * Vh = p.V + seq * p.nb23 + kvh * p.nb22; 295 const sycl::half * mrow = p.mask ? (const sycl::half *) (p.mask + (seq % p.ne33) * p.nb33) : nullptr; 296 297 // O^T: lane = query row, element m = head dim 298 fa_dpas_float8 o[NR16][XT]; 299 #pragma unroll 300 for (int t = 0; t < NR16; ++t) { 301 #pragma unroll 302 for (int x = 0; x < XT; ++x) { 303 o[t][x] = 0.0f; 304 } 305 } 306 // the dims of this sub-group lie in one V block 307 const int vb = (DPS * w) / QK4_0; 308 const int vd0 = (DPS * w) % QK4_0; 309 310 // keys past k_end repeat key k_begin, their scores are -inf 311 auto stage = [&](const char * base, int64_t nb, int t0) { 312 if constexpr (ST == 2) { 313 for (int e = tid; e < TK * RW; e += FA_DPAS_WG) { 314 const int key = e / RW; 315 const int q = e - key * RW; 316 const int j = t0 + key < k_end ? t0 + key : k_begin; 317 sKV[key * SW + q] = ((const uint32_t *) (base + (int64_t) j * nb))[q]; 318 } 319 return; 320 } 321 constexpr int RV = ST == 1 ? RW / 4 : 1; 322 constexpr int NV = TK * RV; 323 constexpr int PER = (NV + FA_DPAS_WG - 1) / FA_DPAS_WG; 324 sycl::uint4 v[PER]; 325 #pragma unroll 326 for (int i = 0; i < PER; ++i) { 327 const int e = tid + i * FA_DPAS_WG; 328 if (e < NV) { 329 const int key = e / RV; 330 const int q = e - key * RV; 331 const int j = t0 + key < k_end ? t0 + key : k_begin; 332 v[i] = ((const sycl::uint4 *) (base + (int64_t) j * nb))[q]; 333 } 334 } 335 #pragma unroll 336 for (int i = 0; i < PER; ++i) { 337 const int e = tid + i * FA_DPAS_WG; 338 if (e < NV) { 339 const int key = e / RV; 340 const int q = e - key * RV; 341 const int o = key * SW + 4 * q; 342 sKV[o + 0] = v[i][0]; 343 sKV[o + 1] = v[i][1]; 344 sKV[o + 2] = v[i][2]; 345 sKV[o + 3] = v[i][3]; 346 } 347 } 348 }; 349 350 // the NQ + 1 dwords of block b: of tile key kk in SLM, or of a row that starts 2 ph bytes into dword row[0] 351 auto load_block = [&](const uint32_t * row, int ph, int kk, int b, uint32_t * wv) { 352 const int wd = (2 * ph + BB * b) / 4; 353 if constexpr (ST) { 354 GGML_UNUSED(row); 355 #pragma unroll 356 for (int i = 0; i < NQ + 1; ++i) { 357 wv[i] = sKV[kk * SW + wd + i]; 358 } 359 } else { 360 GGML_UNUSED(kk); 361 #pragma unroll 362 for (int i = 0; i < NQ + 1; ++i) { 363 wv[i] = row[wd + i]; 364 } 365 } 366 }; 367 368 // start loading the rows of keys t0 .. t0 + TK - 1 into L1 while the current phase computes 369 auto prefetch_rows = [&](const char * base, int64_t nb, int t0) { 370 constexpr int NL = RW * 4 / 64 + 1; // cache lines per row 371 for (int e = tid; e < TK * NL; e += FA_DPAS_WG) { 372 const int j = t0 + e / NL; 373 if (j < k_end) { 374 char * pa = const_cast(base) + (int64_t) j * nb + (e % NL) * 64; 375 sycl::ext::oneapi::experimental::prefetch(pa, sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::prefetch_hint_L1 }); 376 } 377 } 378 }; 379 380 for (int t0 = k_begin; t0 < k_end; t0 += TK) { 381 if constexpr (ST) { 382 stage(Kh, p.nb11, t0); 383 it.barrier(sycl::access::fence_space::local_space); 384 } 385 prefetch_rows(Vh, p.nb21, t0); 386 387 // S = Q K^T, job = (key group, TC row tiles) 388 const int nch = (RT + TC - 1) / TC; 389 for (int job = w; job < KG * nch; job += FA_DPAS_NSG) { 390 const int kg = job % KG; 391 const int ch = job / KG; 392 const int j = t0 + kg * 16 + lane; 393 const bool jv = j < k_end; 394 const char * kb = Kh + (int64_t) (jv ? j : k_begin) * p.nb11; 395 const int kp = (ST || !ODD) ? 0 : (int) (((uintptr_t) kb >> 1) & 1); // row starts 2 bytes into a dword 396 const uint32_t * kr = (const uint32_t *) (kb - 2 * kp); 397 fa_dpas_float8 s[TC]; 398 #pragma unroll 399 for (int tt = 0; tt < TC; ++tt) { 400 s[tt] = 0.0f; 401 } 402 #pragma unroll 403 for (int b = 0; b < NB; ++b) { 404 uint32_t wv[NQ + 1]; 405 uint32_t sc; 406 uint32_t q[NQ]; 407 load_block(kr, kp, kg * 16 + lane, b, wv); 408 fa_dpas_split_block(wv, (b + kp) % 2 != 0, sc, q); 409 const float dk = static_cast(sycl::bit_cast((uint16_t) sc)); 410 #pragma unroll 411 for (int tt = 0; tt < TC; ++tt) { 412 const int t = ch * TC + tt; 413 if (t < RT) { 414 fa_dpas_int8 ia; 415 if constexpr (Q8) { 416 const fa_dpas_int8 bv = { (int) q[0], (int) q[1], (int) q[2], (int) q[3], 417 (int) q[4], (int) q[5], (int) q[6], (int) q[7] }; 418 fa_dpas_s8_s8(sQ8[(t * NB + b) * 16 + lane], bv, ia); 419 } else { 420 // u4 (n + 8) -> s4 n 421 const fa_dpas_int4 bv = { (int) (q[0] ^ 0x88888888u), (int) (q[1] ^ 0x88888888u), 422 (int) (q[2] ^ 0x88888888u), (int) (q[3] ^ 0x88888888u) }; 423 fa_dpas_s8_s4(sQ8[(t * NB + b) * 16 + lane], bv, ia); 424 } 425 const fa_dpas_float8 ds = sDQ[t * NB + b] * dk; 426 s[tt] += __builtin_convertvector(ia, fa_dpas_float8) * ds; 427 } 428 } 429 } 430 #pragma unroll 431 for (int tt = 0; tt < TC; ++tt) { 432 const int t = ch * TC + tt; 433 if (t < RT) { 434 #pragma unroll 435 for (int m = 0; m < 8; ++m) { 436 const int r = t * 8 + m; 437 const int c = c0 + r / G; 438 if (r < R) { 439 const bool ok = jv && c < p.ne01; 440 const float mv = (ok && mrow) ? static_cast(mrow[((int64_t) c * p.nb31) / (int64_t) sizeof(sycl::half) + j]) : 0.0f; 441 sS[r * TK + kg * 16 + lane] = ok ? s[tt][m] + mv : -INFINITY; 442 } 443 } 444 } 445 } 446 } 447 it.barrier(sycl::access::fence_space::local_space); 448 if constexpr (ST) { 449 stage(Vh, p.nb21, t0); // the softmax does not use sKV 450 } 451 452 // online softmax per row -> P 453 for (int r = w; r < R; r += FA_DPAS_NSG) { 454 float sv[KG]; 455 float mt = -INFINITY; 456 #pragma unroll 457 for (int i = 0; i < KG; ++i) { 458 sv[i] = sS[r * TK + i * 16 + lane]; 459 mt = sycl::fmax(mt, sv[i]); 460 } 461 mt = sycl::reduce_over_group(sg, mt, sycl::maximum()); 462 const float m_old = sM[r]; 463 const float m_new = sycl::fmax(m_old, mt); 464 float lt = 0.0f; 465 #pragma unroll 466 for (int i = 0; i < KG; ++i) { 467 const float pv = sycl::native::exp(sv[i] - m_new); 468 sPs[r * PS + i * 16 + lane] = (short) sycl::bit_cast(sycl::half(pv)); 469 lt += pv; 470 } 471 lt = sycl::reduce_over_group(sg, lt, sycl::plus()); 472 if (lane == 0) { 473 const float a = sycl::native::exp(m_old - m_new); 474 sA[r] = a; 475 sL[r] = sL[r] * a + lt; 476 sM[r] = m_new; 477 } 478 } 479 it.barrier(sycl::access::fence_space::local_space); 480 481 prefetch_rows(Kh, p.nb11, t0 + TK); 482 483 // O^T = diag(alpha) O^T + V^T P^T. A = V^T (8 dims x 16 keys, lane = key), B = P^T (16 keys x 16 rows) 484 #pragma unroll 485 for (int t = 0; t < NR16; ++t) { 486 const float a = sA[t * 16 + lane]; 487 #pragma unroll 488 for (int x = 0; x < XT; ++x) { 489 o[t][x] *= a; 490 } 491 } 492 #pragma unroll 1 493 for (int kg = 0; kg < ((ALLPV || w < NPV) ? KG : 0); ++kg) { 494 const int j = t0 + kg * 16 + lane; 495 const bool jv = j < k_end; 496 const char * vbp = Vh + (int64_t) (jv ? j : k_begin) * p.nb21; 497 const int vp = (ST || !ODD) ? 0 : (int) (((uintptr_t) vbp >> 1) & 1); 498 const uint32_t * vr = (const uint32_t *) (vbp - 2 * vp); 499 uint32_t wv[NQ + 1]; 500 uint32_t sc; 501 uint32_t q[NQ]; 502 load_block(vr, vp, kg * 16 + lane, vb, wv); 503 fa_dpas_split_block(wv, (vb + vp) % 2 != 0, sc, q); 504 const float dv = jv ? static_cast(sycl::bit_cast((uint16_t) sc)) : 0.0f; 505 fa_dpas_short8 va[XT]; 506 #pragma unroll 507 for (int x = 0; x < XT; ++x) { 508 #pragma unroll 509 for (int m = 0; m < 8; ++m) { 510 const int d = vd0 + x * 8 + m; 511 int v; 512 if constexpr (Q8) { 513 v = (int8_t) ((q[d / 4] >> (8 * (d % 4))) & 0xFF); 514 } else { 515 const int byte = d % 16; 516 v = (int) ((q[byte / 4] >> (8 * (byte % 4) + 4 * (d / 16))) & 0xF) - 8; 517 } 518 va[x][m] = (short) sycl::bit_cast(sycl::half((float) v * dv)); 519 } 520 } 521 #pragma unroll 522 for (int t = 0; t < NR16; ++t) { 523 const fa_dpas_int8 pb = sP[((t * 16 + lane) * PS + kg * 16) / 16]; 524 #pragma unroll 525 for (int x = 0; x < XT; ++x) { 526 fa_dpas_f16(va[x], pb, o[t][x]); 527 } 528 } 529 } 530 it.barrier(sycl::access::fence_space::local_space); 531 } 532 533 #pragma unroll 534 for (int t = 0; t < NR16; ++t) { 535 const int r = t * 16 + lane; 536 const int c = c0 + r / G; 537 if ((ALLPV || w < NPV) && r < R && c < p.ne01) { 538 const int64_t jdu = ((int64_t) seq * p.ne01 + c) * p.ne02 + kvh * G + r % G; 539 const float l = sL[r]; 540 #pragma unroll 541 for (int x = 0; x < XT; ++x) { 542 #pragma unroll 543 for (int m = 0; m < 8; ++m) { 544 const int d = w * DPS + x * 8 + m; 545 if (p.nsplit == 1) { 546 p.dst[jdu * D + d] = l > 0.0f ? o[t][x][m] / l : 0.0f; 547 } else { 548 p.parts[(jdu * p.nsplit + split) * D + d] = o[t][x][m]; 549 } 550 } 551 } 552 } 553 } 554 if (p.nsplit > 1 && tid < R && c0 + tid / G < p.ne01) { 555 const int64_t jdu = ((int64_t) seq * p.ne01 + c0 + tid / G) * p.ne02 + kvh * G + tid % G; 556 p.meta[jdu * p.nsplit + split] = sycl::float2(sM[tid], sL[tid]); 557 } 558 }; 559 if constexpr (LG) { 560 cgh.parallel_for(ndr, sycl::ext::oneapi::experimental::properties{ sycl::ext::intel::experimental::grf_size<256> }, kern); 561 } else { 562 cgh.parallel_for(ndr, kern); 563 } 564 }); 565 } 566 567 template 568 static void fattn_dpas_case(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 569 constexpr int TK = 16 * KG; 570 571 const ggml_tensor * Q = dst->src[0]; 572 const ggml_tensor * K = dst->src[1]; 573 const ggml_tensor * V = dst->src[2]; 574 const ggml_tensor * mask = dst->src[3]; 575 576 const int ne01 = Q->ne[1]; 577 const int ne11 = K->ne[1]; 578 const int nkvh = K->ne[2]; 579 const int ne03 = Q->ne[3]; 580 581 fa_dpas_params p; 582 p.Q = (const char *) Q->data; 583 p.K = (const char *) K->data; 584 p.V = (const char *) V->data; 585 p.mask = mask ? (const char *) mask->data : nullptr; 586 p.sinks = dst->src[4] ? (const float *) dst->src[4]->data : nullptr; 587 p.dst = (float *) dst->data; 588 memcpy(&p.scale, (const float *) dst->op_params + 0, sizeof(float)); 589 p.G = Q->ne[2] / K->ne[2]; 590 p.nq = std::max(1, std::min(ne01, 16 * NR16 / p.G)); 591 p.nqc = (ne01 + p.nq - 1) / p.nq; 592 p.ne01 = ne01; 593 p.ne02 = Q->ne[2]; 594 p.ne11 = ne11; 595 p.ne33 = mask ? mask->ne[3] : 1; 596 p.nb01 = Q->nb[1]; 597 p.nb02 = Q->nb[2]; 598 p.nb03 = Q->nb[3]; 599 p.nb11 = K->nb[1]; 600 p.nb12 = K->nb[2]; 601 p.nb13 = K->nb[3]; 602 p.nb21 = V->nb[1]; 603 p.nb22 = V->nb[2]; 604 p.nb23 = V->nb[3]; 605 p.nb31 = mask ? mask->nb[1] : 0; 606 p.nb33 = mask ? mask->nb[3] : 0; 607 608 // split the keys so that all work-groups run in one wave 609 // SLM fits about 4 work-groups per Xe2 core with 16 rows, 2 with 32. Xe2 core = 8 EUs, nsm = EUs / 16. 610 const int xe_cores = std::max(1, ggml_sycl_info().devices[ctx.device].nsm * 2); 611 const int wave = xe_cores * (NR16 == 1 ? 4 : 2); 612 const int ntiles = (ne11 + TK - 1) / TK; 613 int nsplit = std::max(1, std::min(wave / (nkvh * ne03 * p.nqc), ntiles)); 614 p.chunk = ((ntiles + nsplit - 1) / nsplit) * TK; 615 nsplit = (ne11 + p.chunk - 1) / p.chunk; 616 p.nsplit = nsplit; 617 618 ggml_sycl_pool_alloc parts(ctx.pool()); 619 ggml_sycl_pool_alloc meta(ctx.pool()); 620 if (nsplit > 1) { 621 parts.alloc((size_t) nsplit * ggml_nelements(dst)); 622 meta.alloc((size_t) nsplit * ggml_nrows(dst)); 623 } 624 p.parts = parts.get(); 625 p.meta = meta.get(); 626 627 dpct::queue_ptr stream = ctx.stream(); 628 // the large register file pays off for 32-row launches from about 4K keys 629 if (NR16 == 2 && ne11 >= 4096) { 630 fattn_dpas_kernel(p, nkvh, ne03, stream); 631 } else { 632 fattn_dpas_kernel(p, nkvh, ne03, stream); 633 } 634 635 if (nsplit > 1) { 636 float * parts_p = parts.get(); 637 sycl::float2 * meta_p = meta.get(); 638 float * dst_p = (float *) dst->data; 639 stream->submit([&](sycl::handler & cgh) { 640 sycl::local_accessor lm(sycl::range<1>(nsplit * sizeof(sycl::float2)), cgh); 641 cgh.parallel_for( 642 sycl::nd_range<3>(sycl::range<3>(Q->ne[3], Q->ne[2], (size_t) ne01 * D), sycl::range<3>(1, 1, D)), 643 [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(16)]] { 644 flash_attn_combine_results(parts_p, meta_p, dst_p, nsplit, 645 lm.get_multi_ptr().get()); 646 }); 647 }); 648 } 649 } 650 651 template static void fattn_dpas_dispatch(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 652 const ggml_tensor * Q = dst->src[0]; 653 const ggml_tensor * K = dst->src[1]; 654 const ggml_tensor * V = dst->src[2]; 655 656 const int D = K->ne[0]; 657 const int G = Q->ne[2] / K->ne[2]; 658 const int ne01 = Q->ne[1]; 659 660 bool aligned16 = true; 661 for (const ggml_tensor * t : { K, V }) { 662 aligned16 = aligned16 && t->nb[1] % 16 == 0 && t->nb[2] % 16 == 0 && t->nb[3] % 16 == 0 && 663 (uintptr_t) t->data % 16 == 0; 664 } 665 666 // single token decode: staged K/V tiles are faster at long context 667 if (D == 256 && ne01 == 1 && G <= 16 && aligned16) { 668 fattn_dpas_case<256, 1, 8, 1, 1, Q8>(ctx, dst); 669 return; 670 } 671 const bool rows16 = ne01 * G <= 16; 672 // 256-key tiles for one row tile; 128 at head size 512 so S and the Q copy stay in SLM for the planned waves 673 #define FATTN_DPAS_D(D_, KG16_) \ 674 case D_: \ 675 if (rows16) { \ 676 fattn_dpas_case(ctx, dst); \ 677 } else { \ 678 fattn_dpas_case(ctx, dst); \ 679 } \ 680 break; 681 // q8_0 at head size 128: its 136-byte rows gather poorly per lane, stage them in SLM by coalesced dword loads 682 bool aligned4 = true; 683 for (const ggml_tensor * t : { K, V }) { 684 aligned4 = aligned4 && t->nb[1] % 4 == 0 && t->nb[2] % 4 == 0 && t->nb[3] % 4 == 0 && (uintptr_t) t->data % 4 == 0; 685 } 686 if (Q8 && D == 128 && aligned4) { 687 if (rows16) { 688 fattn_dpas_case<128, 1, 16, 1, 2, Q8>(ctx, dst); 689 } else { 690 fattn_dpas_case<128, 2, 8, 2, 2, Q8>(ctx, dst); 691 } 692 return; 693 } 694 switch (D) { 695 FATTN_DPAS_D( 64, 16) 696 FATTN_DPAS_D( 96, 16) 697 FATTN_DPAS_D(128, 16) 698 FATTN_DPAS_D(256, 16) 699 FATTN_DPAS_D(512, 8) 700 default: 701 GGML_ABORT("fattn-dpas: unsupported head size %d", D); 702 } 703 #undef FATTN_DPAS_D 704 } 705 706 void ggml_sycl_flash_attn_ext_dpas(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 707 if (dst->src[1]->type == GGML_TYPE_Q8_0) { 708 fattn_dpas_dispatch(ctx, dst); 709 } else { 710 fattn_dpas_dispatch(ctx, dst); 711 } 712 } 713 714 #else 715 716 void ggml_sycl_flash_attn_ext_dpas(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 717 GGML_UNUSED(ctx); 718 GGML_UNUSED(dst); 719 GGML_ABORT("fattn-dpas: not built for this target"); 720 } 721 722 #endif // GGML_SYCL_FA_DPAS_BUILD