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-cpu/tiled/tiled-kernel.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-cpu / tiled / tiled-kernel.cpp` 714 lines ยท 36765 bytes Raw Blame History 1 // mulmat microtile kernels 2 3 #include "tiled-kernel.h" 4 5 #include "ggml.h" 6 7 #include 8 9 #if defined(__AVX512VNNI__) || defined(__AVX2__) || defined(__AVX__) 10 #include 11 #endif 12 13 14 // Reference implementation, slower than existing vec_dot approach 15 template 16 static void tiled_run_microtile_scalar(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 17 int i0, int j0, int num_k, int slab, float * buf, int buf_stride) { 18 constexpr int NB = TILED_TILE_K / SUBBLK; // subblocks per 256-K block 19 constexpr int NS = SUBBLK / 16; // per-16 bsums per subblock 20 21 // num_k/slab: the narrow path holds num_k slabs at row stride num_k*256 (num_k=1, slab=0 = standard) 22 // NK: 1 = standard single-slab (offsets 0, strides compile-time); 0 = runtime 23 // (num_k/slab from the args). The narrow path is memory-bound, so one runtime 24 // version serves all of it. 25 const int nkr = (NK > 0) ? NK : num_k; 26 const int seff = (NK > 0) ? 0 : slab; 27 const int qk_stride = nkr * TILED_TILE_K; 28 const int qk_off = seff * TILED_TILE_K; 29 const int nb_stride = NB * nkr; 30 const int nb_off = seff * NB; 31 const int bs_stride = (nkr == 1) ? TILED_TILE_ROWS : TILED_MICRO; 32 const int bs_off = seff * TILED_MICRO; 33 34 float acc[TILED_MICRO][TILED_MICRO]; 35 memset(acc, 0, sizeof(acc)); 36 37 // subdots at subblock granularity over the 256-K block, exact integer math 38 for (int s = 0; s < NB; s++) { 39 for (int j = 0; j < TILED_MICRO; j++) { 40 const int br = j0 + j; 41 const int8_t * q1 = &src1.q[br * qk_stride + qk_off + s * SUBBLK]; 42 int32_t bsum = 0; 43 for (int u = 0; u < NS; u++) { 44 bsum += src1.bsums[(bs_off + s * NS + u) * bs_stride + br]; 45 } 46 47 for (int i = 0; i < TILED_MICRO; i++) { 48 const int ar = i0 + i; 49 const int d_off = seff * TILED_MICRO + ar; 50 const uint8_t * q0 = &src0.q[ar * qk_stride + qk_off + s * SUBBLK]; 51 52 int32_t raw = 0; 53 for (int e = 0; e < SUBBLK; e++) { 54 raw += (int32_t) q0[e] * (int32_t) q1[e]; 55 } 56 57 // BIAS: subtract BIAS*bsum (src1's per-subblock code sum) from the exact int raw 58 int32_t corr = raw; 59 if constexpr (BIAS != 0) { 60 corr -= BIAS * bsum; 61 } 62 const int32_t scales_raw = (int32_t) src0.scales[ar * nb_stride + nb_off + s] * corr; 63 // d is NOT applied here: it is constant over the s-loop, so we apply it last before write-out 64 if constexpr (HAS_MIN) { 65 const int32_t mins_bsum = (int32_t) src0.mins[ar * nb_stride + nb_off + s] * bsum; 66 acc[i][j] += (float) src0.d[d_off] * (float) scales_raw 67 - (float) src0.dmin[d_off] * (float) mins_bsum; 68 } else { 69 acc[i][j] += (float) src0.d[d_off] * (float) scales_raw; 70 } 71 } 72 } 73 } 74 75 // Apply d and write out to buf 76 for (int i = 0; i < TILED_MICRO; i++) { 77 for (int j = 0; j < TILED_MICRO; j++) { 78 buf[(i0 + i) * buf_stride + (j0 + j)] += src1.d[seff * TILED_MICRO + j0 + j] * acc[i][j]; 79 } 80 } 81 } 82 83 #if defined(__AVX512VNNI__) && defined(__AVX512VL__) && defined(__AVX512DQ__) 84 85 86 // VNNI microkernel 87 // 8x16 band pass: 8 src0 rows (i0, i0+8) x 16 src1 cols (j0, j0+16) 88 // Math: s1_acc += scales_s*raw_s, s2_acc += mins_s*bsums_s per subblock; the row result 89 // is d0*s1_acc - dmin*s2_acc (f32, exact in range), applied once per row. 90 // 91 // Register pressure: the band pass holds acc16 + s1_acc = 16 zmm for the 92 // 8-row band (s2_acc has no dpbusd dependency, so it is computed after the 93 // band pass and adds no register pressure); 94 template 95 static void tiled_run_micro_vnni_8x16(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 96 int i0, int j0, int num_k, int slab, float * buf, int buf_stride) { 97 constexpr int NB = TILED_TILE_K / SUBBLK; 98 constexpr int NS = SUBBLK / 16; 99 constexpr int NG = SUBBLK / 4; 100 101 // band width, see the register-pressure note above 102 constexpr int NUM_ROWS = 8; 103 104 // num_k = K-blocks per row (the narrow path holds num_k slabs at row stride num_k*256 so 105 // each weight row is one long stream); slab = this call's slab. num_k=1, slab=0 = standard. 106 // NK: 1 = standard single-slab (offsets 0, strides compile-time); 0 = runtime 107 // (num_k/slab from the args). The narrow path is memory-bound, so one runtime 108 // version serves all of it. 109 const int nkr = (NK > 0) ? NK : num_k; 110 const int seff = (NK > 0) ? 0 : slab; 111 const int qk_stride = nkr * TILED_TILE_K; 112 const int qk_off = seff * TILED_TILE_K; 113 const int nb_stride = NB * nkr; 114 const int nb_off = seff * NB; 115 const int bs_stride = (nkr == 1) ? TILED_TILE_ROWS : TILED_MICRO; 116 const int bs_off = seff * TILED_MICRO; 117 118 const __m512 d1_vec = _mm512_load_ps(&src1.d[seff * TILED_MICRO + j0]); 119 120 __m512i s1_acc[NUM_ROWS]; 121 for (int t = 0; t < NUM_ROWS; t++) { s1_acc[t] = _mm512_setzero_si512(); } 122 __m512i s2_acc[NUM_ROWS]; 123 if constexpr (HAS_MIN) { 124 for (int t = 0; t < NUM_ROWS; t++) { s2_acc[t] = _mm512_setzero_si512(); } 125 } 126 127 const int32_t * bsums = src1.bsums; // int32 per-16 sums; NS > 1 combines NS lanes per subblock 128 for (int s = 0; s < NB; s++) { 129 __m512i bsums32 = _mm512_load_si512((const __m512i *) &bsums[(bs_off + s * NS) * bs_stride + j0]); 130 for (int u = 1; u < NS; u++) { 131 bsums32 = _mm512_add_epi32(bsums32, _mm512_load_si512((const __m512i *) &bsums[(bs_off + s * NS + u) * bs_stride + j0])); 132 } 133 134 __m512i bias32 = _mm512_setzero_si512(); 135 if constexpr (BIAS != 0) { 136 bias32 = _mm512_mullo_epi32(bsums32, _mm512_set1_epi32(BIAS)); 137 } 138 139 __m512i acc16[NUM_ROWS]; 140 for (int t = 0; t < NUM_ROWS; t++) { acc16[t] = _mm512_setzero_si512(); } 141 142 #ifdef __GNUC__ 143 #pragma GCC unroll 8 // pragma unrolled justified by measuing with/without 144 #endif 145 for (int g = 0; g < NG; g++) { 146 const int kg = s * NG + g; 147 // in-place interleave layout: [kg%16 @ TILED_TILE_K][kg/16 @ 64][row @ 4] 148 const __m512i codes = _mm512_load_si512((const __m512i *) &src1.q[(j0 / TILED_MICRO) * (TILED_MICRO * qk_stride) 149 + qk_off + (kg % TILED_MICRO) * qk_stride + (kg / TILED_MICRO) * (TILED_MICRO * 4)]); 150 for (int t = 0; t < NUM_ROWS; t++) { 151 const uint32_t u4 = *(const uint32_t *) &src0.q[(i0 + t) * qk_stride + qk_off + kg * 4]; 152 const __m512i u4b = _mm512_set1_epi32((int) u4); 153 acc16[t] = _mm512_dpbusd_epi32(acc16[t], u4b, codes); 154 } 155 } 156 157 // int correction: s1_acc += scales*(raw-BIAS*bsums) 158 for (int t = 0; t < NUM_ROWS; t++) { 159 const int ar = i0 + t; 160 __m512i rawi = acc16[t]; 161 if constexpr (BIAS != 0) { 162 rawi = _mm512_sub_epi32(rawi, bias32); 163 } 164 s1_acc[t] = _mm512_add_epi32(s1_acc[t], 165 _mm512_mullo_epi32(rawi, _mm512_set1_epi32(src0.scales[ar * nb_stride + nb_off + s]))); 166 } 167 } 168 169 // s2_acc += mins*bsums; independent of the dpbusd results, so it runs here instead of in the 170 // band pass: the band pass keeps the register budget for the 8-row band 171 if constexpr (HAS_MIN) { 172 for (int s = 0; s < NB; s++) { 173 __m512i bsums32 = _mm512_load_si512((const __m512i *) &bsums[(bs_off + s * NS) * bs_stride + j0]); 174 for (int u = 1; u < NS; u++) { 175 bsums32 = _mm512_add_epi32(bsums32, _mm512_load_si512((const __m512i *) &bsums[(bs_off + s * NS + u) * bs_stride + j0])); 176 } 177 for (int t = 0; t < NUM_ROWS; t++) { 178 s2_acc[t] = _mm512_add_epi32(s2_acc[t], 179 _mm512_mullo_epi32(bsums32, _mm512_set1_epi32(src0.mins[(i0 + t) * nb_stride + nb_off + s]))); 180 } 181 } 182 } 183 184 // epilogue: int->float, apply per-row scales, store to buf 185 for (int t = 0; t < NUM_ROWS; t++) { 186 const int ar = i0 + t; 187 const int d_off = seff * TILED_MICRO + ar; 188 __m512 f1 = _mm512_cvtepi32_ps(s1_acc[t]); 189 __m512 result = _mm512_mul_ps(f1, _mm512_set1_ps(src0.d[d_off])); 190 if constexpr (HAS_MIN) { 191 __m512 f2 = _mm512_cvtepi32_ps(s2_acc[t]); 192 result = _mm512_fnmadd_ps(_mm512_set1_ps(src0.dmin[d_off]), f2, result); 193 } 194 float * p = &buf[(i0 + t) * buf_stride + j0]; 195 _mm512_store_ps(p, _mm512_add_ps(_mm512_load_ps(p), _mm512_mul_ps(result, d1_vec))); 196 } 197 } 198 199 // 16x16 microtile as two explicit 8x16 band passes 200 template 201 static void tiled_run_microtile_vnni(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 202 int i0, int j0, int num_k, int slab, float * buf, int buf_stride) { 203 tiled_run_micro_vnni_8x16(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 204 tiled_run_micro_vnni_8x16(src0, src1, i0 + 8, j0, num_k, slab, buf, buf_stride); 205 } 206 207 #endif // __AVX512VNNI__ && __AVX512VL__ 208 209 #if defined(__AVX2__) 210 211 // AVX2 kernel. 212 // We're effectively applying the existing vec_dot algorithms to an 8x16 block here. 213 // Different paths based on subblock size as it affects when/where we multiply in scales and apply mins 214 // SUBBLK=32: one 256-bit maddubs per (s, column), a 256-bit set1 scale, 215 // one 8-lane i32 accumulator per column. 216 // SUBBLK=16: two subblocks per 32B load. The 256-bit maddubs product 217 // still gives 16 i16 lanes (0..7 = subblock sp, 8..15 = sp+1), but the 218 // scale is applied per 128-bit half. 219 // With BIAS != 0 the q0 codes are biased (up to 241) and a maddubs i16 pair 220 // overflows, so the weight is debiased (w = q0 - BIAS, small). Two MACs: 221 // ACTBIAS: activation pre-biased (+128) by repack, one maddubs(q1b, w); the +128 is 222 // corrected out in the epilogue (128*sum_s scales[s]*w_bsum[s], precomputed in repack_src0). 223 // Fits only when the debiased weight magnitude Wmax <= 64 (2*255*Wmax < 32767). 224 // else (iq4_xs, Wmax = 127): the signed-int8 sign trick, ax = |w|, sy = q1*sign(w), 225 // one maddubs (2*127*127 < 32767) so it always fits. 226 template 227 static void tiled_run_microtile_avx2(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 228 int i0, int j0, int num_k, int slab, float * buf, int buf_stride) { 229 constexpr int NB = TILED_TILE_K / SUBBLK; 230 constexpr int GROUP = 8; // Process 8 src1 columns concurrently (1 YMM vector) 231 232 static_assert(SUBBLK == 16 || SUBBLK == 32, "unsupported SUBBLK"); 233 234 // num_k/slab: the narrow path holds num_k slabs at row stride num_k*256 (num_k=1, slab=0 = standard) 235 // NK: 1 = standard single-slab (offsets 0, strides compile-time); 0 = runtime 236 // (num_k/slab from the args). The narrow path is memory-bound, so one runtime 237 // version serves all of it. 238 const int nkr = (NK > 0) ? NK : num_k; 239 const int seff = (NK > 0) ? 0 : slab; 240 const int qk_stride = nkr * TILED_TILE_K; 241 const int qk_off = seff * TILED_TILE_K; 242 const int nb_stride = NB * nkr; 243 const int nb_off = seff * NB; 244 const int bs_stride = (nkr == 1) ? TILED_TILE_ROWS : TILED_MICRO; 245 const int bs_off = seff * TILED_MICRO; 246 247 for (int i = 0; i < TILED_MICRO; i++) { 248 const int ar = i0 + i; 249 const int d_off = seff * TILED_MICRO + ar; 250 const float d0 = src0.d[d_off]; 251 const float dmin0 = src0.dmin[d_off]; 252 const uint8_t * q0 = &src0.q[ar * qk_stride + qk_off]; 253 const int32_t * scales_row = &src0.scales[ar * nb_stride + nb_off]; 254 const int32_t * mins_row = &src0.mins[ar * nb_stride + nb_off]; 255 256 for (int g = 0; g < TILED_MICRO; g += GROUP) { 257 // Pre-calculate weight pointers for this column group 258 const int8_t * q1_ptr[GROUP]; 259 for (int t = 0; t < GROUP; t++) { 260 q1_ptr[t] = &src1.q[(j0 + g + t) * qk_stride + qk_off]; 261 } 262 263 __m256i s2 = _mm256_setzero_si256(); 264 265 __m256i acc[GROUP]; 266 for (int t = 0; t < GROUP; t++) { 267 acc[t] = _mm256_setzero_si256(); 268 } 269 270 if constexpr (SUBBLK == 32) { 271 for (int s = 0; s < NB; s++) { 272 // OPTIMIZATION 1: Use aligned loads (_mm256_load_si256) 273 const __m256i q0_32 = _mm256_load_si256((const __m256i *) &q0[s * SUBBLK]); 274 const __m256i scales16 = _mm256_set1_epi16(scales_row[s]); 275 276 if constexpr (BIAS != 0) { 277 const __m256i w = _mm256_sub_epi8(q0_32, _mm256_set1_epi8((int8_t) BIAS)); // debiased weight 278 if constexpr (ACTBIAS) { 279 // activation pre-biased (+128) by repack; q1b unsigned, w signed 280 #pragma GCC unroll 8 281 for (int t = 0; t < GROUP; t++) { 282 const __m256i q1b_32 = _mm256_load_si256((const __m256i *) &q1_ptr[t][s * SUBBLK]); 283 acc[t] = _mm256_add_epi32(acc[t], 284 _mm256_madd_epi16(scales16, _mm256_maddubs_epi16(q1b_32, w))); 285 } 286 } else { 287 // iq4_xs (Wmax > 64 overflows act-bias): sign trick, both operands <= 127 288 const __m256i ax = _mm256_sign_epi8(w, w); 289 #pragma GCC unroll 8 290 for (int t = 0; t < GROUP; t++) { 291 const __m256i q1_32 = _mm256_load_si256((const __m256i *) &q1_ptr[t][s * SUBBLK]); 292 acc[t] = _mm256_add_epi32(acc[t], 293 _mm256_madd_epi16(scales16, _mm256_maddubs_epi16(ax, _mm256_sign_epi8(q1_32, w)))); 294 } 295 } 296 } else { 297 #pragma GCC unroll 8 298 for (int t = 0; t < GROUP; t++) { 299 const __m256i q1_32 = _mm256_load_si256((const __m256i *) &q1_ptr[t][s * SUBBLK]); 300 acc[t] = _mm256_add_epi32(acc[t], 301 _mm256_madd_epi16(scales16, _mm256_maddubs_epi16(q0_32, q1_32))); 302 } 303 } 304 305 if constexpr (HAS_MIN) { 306 const __m256i bsums_v = _mm256_add_epi32( 307 _mm256_load_si256((const __m256i *) &src1.bsums[(bs_off + s * 2) * bs_stride + j0 + g]), 308 _mm256_load_si256((const __m256i *) &src1.bsums[(bs_off + s * 2 + 1) * bs_stride + j0 + g])); 309 s2 = _mm256_add_epi32(s2, _mm256_mullo_epi32(bsums_v, _mm256_set1_epi32(mins_row[s]))); 310 } 311 } 312 } else { // SUBBLK == 16 313 for (int sp = 0; sp < NB; sp += 2) { 314 const __m256i q0_32 = _mm256_load_si256((const __m256i *) &q0[sp * SUBBLK]); 315 const __m256i scalesv = _mm256_set_m128i(_mm_set1_epi16(scales_row[sp + 1]), _mm_set1_epi16(scales_row[sp])); 316 if constexpr (BIAS != 0) { 317 const __m256i w = _mm256_sub_epi8(q0_32, _mm256_set1_epi8((int8_t) BIAS)); // debiased weight 318 if constexpr (ACTBIAS) { 319 // activation pre-biased (+128) by repack; q1b unsigned, w signed 320 #pragma GCC unroll 8 321 for (int t = 0; t < GROUP; t++) { 322 const __m256i q1b_32 = _mm256_load_si256((const __m256i *) &q1_ptr[t][sp * SUBBLK]); 323 acc[t] = _mm256_add_epi32(acc[t], _mm256_madd_epi16( 324 scalesv, _mm256_maddubs_epi16(q1b_32, w))); 325 } 326 } else { 327 // iq4_xs (Wmax > 64 overflows act-bias): sign trick, both operands <= 127 328 const __m256i ax = _mm256_sign_epi8(w, w); 329 #pragma GCC unroll 8 330 for (int t = 0; t < GROUP; t++) { 331 const __m256i q1_32 = _mm256_load_si256((const __m256i *) &q1_ptr[t][sp * SUBBLK]); 332 acc[t] = _mm256_add_epi32(acc[t], _mm256_madd_epi16( 333 scalesv, _mm256_maddubs_epi16(ax, _mm256_sign_epi8(q1_32, w)))); 334 } 335 } 336 } else { 337 #pragma GCC unroll 8 338 for (int t = 0; t < GROUP; t++) { 339 const __m256i q1_32 = _mm256_load_si256((const __m256i *) &q1_ptr[t][sp * SUBBLK]); 340 acc[t] = _mm256_add_epi32(acc[t], _mm256_madd_epi16( 341 scalesv, _mm256_maddubs_epi16(q0_32, q1_32))); 342 } 343 } 344 345 if constexpr (HAS_MIN) { 346 const __m256i bsums0_v = _mm256_load_si256((const __m256i *) &src1.bsums[(bs_off + sp) * bs_stride + j0 + g]); 347 const __m256i bsums1_v = _mm256_load_si256((const __m256i *) &src1.bsums[(bs_off + sp + 1) * bs_stride + j0 + g]); 348 s2 = _mm256_add_epi32(s2, _mm256_add_epi32( 349 _mm256_mullo_epi32(bsums0_v, _mm256_set1_epi32(mins_row[sp])), 350 _mm256_mullo_epi32(bsums1_v, _mm256_set1_epi32(mins_row[sp + 1])))); 351 } 352 } 353 } 354 355 // Reduce the eight accumulators to column sums in acc[0]..acc[7] order, with additions modulo 2^32. 356 const __m256i ab = _mm256_hadd_epi32(acc[0], acc[1]); 357 const __m256i cd = _mm256_hadd_epi32(acc[2], acc[3]); 358 const __m256i ef = _mm256_hadd_epi32(acc[4], acc[5]); 359 const __m256i gh = _mm256_hadd_epi32(acc[6], acc[7]); 360 361 // Each 128-bit half now holds four column sums over input lanes 0..3 or 4..7. 362 const __m256i abcd = _mm256_hadd_epi32(ab, cd); 363 const __m256i efgh = _mm256_hadd_epi32(ef, gh); 364 const __m256i lo = _mm256_permute2x128_si256(abcd, efgh, 0x20); 365 const __m256i hi = _mm256_permute2x128_si256(abcd, efgh, 0x31); 366 __m256i s1_vec = _mm256_add_epi32(lo, hi); 367 368 // OPTIMIZATION 2: Vectorized Epilogue with FMA3 gating 369 if constexpr (BIAS != 0 && ACTBIAS) { 370 // act-bias correction: raw = desired + 128*sum_s scales[s]*w_bsum[s]; corr precomputed in repack_src0 371 s1_vec = _mm256_sub_epi32(s1_vec, _mm256_set1_epi32(mins_row[0])); 372 } 373 374 const __m256 src1_d_vec = _mm256_load_ps(&src1.d[seff * TILED_MICRO + j0 + g]); 375 __m256 res_vec = _mm256_mul_ps(_mm256_set1_ps(d0), _mm256_cvtepi32_ps(s1_vec)); 376 377 if constexpr (HAS_MIN) { 378 #if defined(__FMA__) 379 // FMA3 implementation: res_vec = res_vec - (dmin0 * s2) 380 res_vec = _mm256_fnmadd_ps(_mm256_set1_ps(dmin0), _mm256_cvtepi32_ps(s2), res_vec); 381 #else 382 // Non-FMA fallback 383 res_vec = _mm256_sub_ps(res_vec, _mm256_mul_ps(_mm256_set1_ps(dmin0), _mm256_cvtepi32_ps(s2))); 384 #endif 385 } 386 387 float * buf_ptr = &buf[ar * buf_stride + j0 + g]; 388 __m256 current_buf = _mm256_loadu_ps(buf_ptr); 389 390 #if defined(__FMA__) 391 // FMA3 accumulation: current_buf + (res_vec * src1_d_vec) 392 __m256 updated_buf = _mm256_fmadd_ps(res_vec, src1_d_vec, current_buf); 393 #else 394 __m256 updated_buf = _mm256_add_ps(current_buf, _mm256_mul_ps(res_vec, src1_d_vec)); 395 #endif 396 397 _mm256_storeu_ps(buf_ptr, updated_buf); 398 } 399 } 400 } 401 402 #endif // __AVX2__ 403 404 #if defined(__AVX__) && !defined(__AVX2__) 405 template 406 static void tiled_run_microtile_avx(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 407 int i0, int j0, int num_k, int slab, float * buf, int buf_stride) { 408 constexpr int NB = TILED_TILE_K / SUBBLK; 409 constexpr int NS = SUBBLK / 16; 410 constexpr int GROUP = 4; // src1 columns per group: one acc32 per column 411 412 // num_k/slab: the narrow path holds num_k slabs at row stride num_k*256 (num_k=1, slab=0 = standard) 413 // NK: 1 = standard single-slab (offsets 0, strides compile-time); 0 = runtime 414 // (num_k/slab from the args). The narrow path is memory-bound, so one runtime 415 // version serves all of it. 416 const int nkr = (NK > 0) ? NK : num_k; 417 const int seff = (NK > 0) ? 0 : slab; 418 const int qk_stride = nkr * TILED_TILE_K; 419 const int qk_off = seff * TILED_TILE_K; 420 const int nb_stride = NB * nkr; 421 const int nb_off = seff * NB; 422 const int bs_stride = (nkr == 1) ? TILED_TILE_ROWS : TILED_MICRO; 423 const int bs_off = seff * TILED_MICRO; 424 425 for (int i = 0; i < TILED_MICRO; i++) { 426 const int ar = i0 + i; 427 const int d_off = seff * TILED_MICRO + ar; 428 const float d0 = src0.d[d_off]; 429 const float dmin0 = src0.dmin[d_off]; 430 const uint8_t * q0 = &src0.q[ar * qk_stride + qk_off]; 431 const int32_t * scales_row = &src0.scales[ar * nb_stride + nb_off]; 432 const int32_t * mins_row = &src0.mins[ar * nb_stride + nb_off]; 433 434 for (int g = 0; g < TILED_MICRO; g += GROUP) { 435 const int8_t * q1g[GROUP]; 436 __m128i acc[GROUP]; 437 __m128i s2 = _mm_setzero_si128(); // sum_s mins_s * bsums_s 438 for (int t = 0; t < GROUP; t++) { 439 q1g[t] = &src1.q[(j0 + g + t) * qk_stride + qk_off]; 440 acc[t] = _mm_setzero_si128(); 441 } 442 443 for (int s = 0; s < NB; s++) { 444 const __m128i scales16 = _mm_set1_epi16(scales_row[s]); 445 if constexpr (HAS_MIN) { 446 __m128i bsums_v = _mm_setzero_si128(); // per-col bsums as a 4-lane i32 vector 447 for (int u = 0; u < NS; u++) { 448 bsums_v = _mm_add_epi32(bsums_v, _mm_loadu_si128( 449 (const __m128i *) &src1.bsums[(bs_off + s * NS + u) * bs_stride + j0 + g])); 450 } 451 s2 = _mm_add_epi32(s2, _mm_mullo_epi32(bsums_v, _mm_set1_epi32(mins_row[s]))); 452 } 453 for (int u = 0; u < NS; u++) { 454 const __m128i a16 = _mm_loadu_si128((const __m128i *) &q0[s * SUBBLK + u * 16]); 455 if constexpr (BIAS != 0) { 456 // debiased weight w = a16 - BIAS; signed dot via the sign trick (see header) 457 const __m128i a16s = _mm_sub_epi8(a16, _mm_set1_epi8((int8_t) BIAS)); 458 const __m128i ax = _mm_sign_epi8(a16s, a16s); 459 for (int t = 0; t < GROUP; t++) { 460 const __m128i q1_16 = _mm_loadu_si128((const __m128i *) &q1g[t][s * SUBBLK + u * 16]); 461 acc[t] = _mm_add_epi32(acc[t], 462 _mm_madd_epi16(scales16, _mm_maddubs_epi16(ax, _mm_sign_epi8(q1_16, a16s)))); 463 } 464 } else { 465 for (int t = 0; t < GROUP; t++) { 466 acc[t] = _mm_add_epi32(acc[t], _mm_madd_epi16(scales16, 467 _mm_maddubs_epi16(a16, 468 _mm_loadu_si128((const __m128i *) &q1g[t][s * SUBBLK + u * 16])))); 469 } 470 } 471 } 472 } 473 474 int32_t s2_s[GROUP] = { 0, 0, 0, 0 }; 475 if constexpr (HAS_MIN) { 476 _mm_storeu_si128((__m128i *) s2_s, s2); 477 } 478 479 for (int t = 0; t < GROUP; t++) { 480 // 4 i32 lanes -> scalar 481 __m128i v = _mm_shuffle_epi32(acc[t], _MM_SHUFFLE(2, 3, 0, 1)); 482 acc[t] = _mm_add_epi32(acc[t], v); 483 v = _mm_shuffle_epi32(acc[t], _MM_SHUFFLE(1, 0, 3, 2)); 484 acc[t] = _mm_add_epi32(acc[t], v); 485 const int32_t s1 = _mm_cvtsi128_si32(acc[t]); 486 487 float res = d0 * (float) s1; 488 if constexpr (HAS_MIN) { 489 res -= dmin0 * (float) s2_s[t]; 490 } 491 buf[ar * buf_stride + j0 + g + t] += src1.d[seff * TILED_MICRO + j0 + g + t] * res; 492 } 493 } 494 } 495 } 496 497 #endif // __AVX__ && !__AVX2__ 498 499 // main microtile entry point. num_k = K-blocks per row (the narrow path reads num_k slabs at row 500 // stride num_k*256 so each weight row is one long stream); slab = this call's slab. num_k=1, 501 // slab=0 is the standard single-slab path. 502 // num_k==1 dispatches to the NK=1 instantiation, whose strides/offsets are compile-time 503 // constants (shifts/scaled-leas). num_k>1 (the narrow path) uses the NK=0 runtime-strided 504 // version; it is memory-bound, so one version serves all of it. 505 template 506 void tiled_run_microtile(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 507 int i0, int j0, int num_k, int slab, float * buf, int buf_stride) { 508 const bool standard = (num_k == 1); 509 #if defined(__AVX512VNNI__) && defined(__AVX512VL__) && defined(__AVX512DQ__) 510 if (standard) { 511 tiled_run_microtile_vnni(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 512 } else { 513 tiled_run_microtile_vnni(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 514 } 515 #elif defined(__AVX2__) 516 if (standard) { 517 tiled_run_microtile_avx2(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 518 } else { 519 tiled_run_microtile_avx2(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 520 } 521 #elif defined(__AVX__) 522 if (standard) { 523 tiled_run_microtile_avx(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 524 } else { 525 tiled_run_microtile_avx(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 526 } 527 #else 528 if (standard) { 529 tiled_run_microtile_scalar(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 530 } else { 531 tiled_run_microtile_scalar(src0, src1, i0, j0, num_k, slab, buf, buf_stride); 532 } 533 #endif 534 } 535 536 // explicit instantiations for the in-use formats (q4_K and q5_K share the constants) 537 // ACTBIAS selects the act-bias MAC (Wmax <= 64); iq4_xs (Wmax 127) keeps the sign trick 538 // q4_K / q5_K: BIAS = 0 539 template void tiled_run_microtile<32, true, 0, false>(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 540 int i0, int j0, int num_k, int slab, float * buf, int buf_stride); 541 // iq4_xs: sign trick (Wmax 127 overflows act-bias) 542 template void tiled_run_microtile<32, false, 128, false>(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 543 int i0, int j0, int num_k, int slab, float * buf, int buf_stride); 544 // iq2_xxs, iq3_xxs, iq3_s, iq1_s: act-bias (Wmax <= 62) 545 template void tiled_run_microtile<32, false, 128, true>(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 546 int i0, int j0, int num_k, int slab, float * buf, int buf_stride); 547 // iq2_xs, iq2_s, iq1_m: act-bias 548 template void tiled_run_microtile<16, false, 128, true>(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 549 int i0, int j0, int num_k, int slab, float * buf, int buf_stride); 550 // q6_K: act-bias (Wmax 32) 551 template void tiled_run_microtile<16, false, 32, true>(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 552 int i0, int j0, int num_k, int slab, float * buf, int buf_stride); 553 // q3_K: act-bias (Wmax 4) 554 template void tiled_run_microtile<16, false, 4, true>(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 555 int i0, int j0, int num_k, int slab, float * buf, int buf_stride); 556 // q2_K: BIAS = 0 557 template void tiled_run_microtile<16, true, 0, false>(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 558 int i0, int j0, int num_k, int slab, float * buf, int buf_stride); 559 560 561 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 562 563 // block_q8_K in 4-byte words, for the int32 gather indices 564 static_assert(sizeof(block_q8_K) == 292 && offsetof(block_q8_K, qs) == 4, 565 "block_q8_K layout changed, fix the src1 repack"); 566 567 // VNNI: interleave the natural [row][k] codes in-place into the group-local 568 // [kg%16][kg/16][row][4] layout. base points at row 0 (row r at base + r*row_stride); 569 // n_tiles 16x16 int32 tiles are transposed to the right (tile t occupies bytes 570 // [t*64, t*64+64) per row). Each 16-row group is self-contained in row_stride*16 bytes. 571 #if defined(__AVX512VNNI__) && defined(__AVX512VL__) && defined(__AVX512DQ__) 572 void tiled_repack_src1(tiled_tile_src1 * src1, int row0, int num_k, bool bias) { 573 GGML_UNUSED(bias); 574 const int n_tiles = num_k * (TILED_TILE_K / (TILED_MICRO * 4)); 575 const int row_stride = num_k * TILED_TILE_K; 576 uint8_t * base = (uint8_t *) src1->q + row0 * row_stride; 577 for (int t = 0; t < n_tiles; t++) { 578 uint8_t * cbase = base + t * (TILED_MICRO * 4); 579 __m512i v[16]; 580 for (int r = 0; r < 16; r++) { 581 v[r] = _mm512_load_si512((const __m512i *) (cbase + r * row_stride)); 582 } 583 584 // 16x16 int32 transpose, 4 butterfly phases 585 // Phase 1: 1-element interleave, pairs (0,1), (2,3), ..., (14,15) 586 { 587 const __m512i idx_a = _mm512_setr_epi32(0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23); 588 const __m512i idx_b = _mm512_setr_epi32(8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31); 589 for (int i = 0; i < 16; i += 2) { 590 __m512i a = v[i], b = v[i+1]; 591 v[i] = _mm512_permutex2var_epi32(a, idx_a, b); 592 v[i+1] = _mm512_permutex2var_epi32(a, idx_b, b); 593 } 594 } 595 // Phase 2: 2-element interleave, pairs (0,2), (1,3), (4,6), (5,7), ... 596 { 597 const __m512i idx_a = _mm512_setr_epi32(0, 1, 16, 17, 4, 5, 20, 21, 8, 9, 24, 25, 12, 13, 28, 29); 598 const __m512i idx_b = _mm512_setr_epi32(2, 3, 18, 19, 6, 7, 22, 23, 10, 11, 26, 27, 14, 15, 30, 31); 599 for (int i = 0; i < 16; i += 4) { 600 __m512i a = v[i], b = v[i+2]; 601 v[i] = _mm512_permutex2var_epi32(a, idx_a, b); 602 v[i+2] = _mm512_permutex2var_epi32(a, idx_b, b); 603 a = v[i+1], b = v[i+3]; 604 v[i+1] = _mm512_permutex2var_epi32(a, idx_a, b); 605 v[i+3] = _mm512_permutex2var_epi32(a, idx_b, b); 606 } 607 } 608 // Phase 3: 4-element interleave, pairs (0,4), (1,5), ..., (7,11), (8,12), ... 609 { 610 const __m512i idx_a = _mm512_setr_epi32(0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23); 611 const __m512i idx_b = _mm512_setr_epi32(8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31); 612 for (int i = 0; i < 16; i += 8) { 613 for (int j = 0; j < 4; j++) { 614 __m512i a = v[i+j], b = v[i+4+j]; 615 v[i+j] = _mm512_permutex2var_epi32(a, idx_a, b); 616 v[i+4+j] = _mm512_permutex2var_epi32(a, idx_b, b); 617 } 618 } 619 } 620 // Phase 4: 8-element interleave, pairs (0,8), (1,9), ..., (7,15) 621 { 622 const __m512i idx_a = _mm512_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23); 623 const __m512i idx_b = _mm512_setr_epi32(8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31); 624 for (int i = 0; i < 8; i++) { 625 __m512i a = v[i], b = v[i+8]; 626 v[i] = _mm512_permutex2var_epi32(a, idx_a, b); 627 v[i+8] = _mm512_permutex2var_epi32(a, idx_b, b); 628 } 629 } 630 631 // store: v[g] holds 16 int32s for k-group col_order[g], rows 0..15 632 static const int col_order[16] = {0, 8, 1, 9, 4, 12, 5, 13, 2, 10, 3, 11, 6, 14, 7, 15}; 633 for (int g = 0; g < 16; g++) { 634 _mm512_store_si512((void *) (cbase + col_order[g] * row_stride), v[g]); 635 } 636 } 637 } 638 #elif defined(__AVX2__) 639 // act-bias: pre-bias the activation in place (+128) so the MAC's maddubs(q1b, w) sees unsigned bytes; 640 // no transpose on AVX2 (natural layout). 16 rows x n_tiles*64 bytes, row r at base + r*row_stride 641 void tiled_repack_src1(tiled_tile_src1 * src1, int row0, int num_k, bool bias) { 642 if (!bias) { 643 return; 644 } 645 const int row_stride = num_k * TILED_TILE_K; 646 uint8_t * base = (uint8_t *) src1->q + row0 * row_stride; 647 const int nbytes = row_stride; // full row width 648 const __m256i b128 = _mm256_set1_epi8((int8_t) 0x80); 649 for (int r = 0; r < TILED_MICRO; r++) { 650 uint8_t * row = base + r * row_stride; 651 for (int off = 0; off < nbytes; off += 32) { 652 const __m256i v = _mm256_loadu_si256((const __m256i *) (row + off)); 653 _mm256_storeu_si256((__m256i *) (row + off), _mm256_xor_si256(v, b128)); 654 } 655 } 656 } 657 #else 658 void tiled_repack_src1(tiled_tile_src1 * src1, int row0, int num_k, bool bias) { 659 GGML_UNUSED(src1); GGML_UNUSED(row0); GGML_UNUSED(num_k); GGML_UNUSED(bias); 660 } 661 #endif 662 663 #if defined(__AVX2__) 664 // sum 32 unsigned bytes to int32 (via maddubs -> int16 pairs -> hsum) 665 static int32_t tiled_byte_sum_32(const uint8_t * p) { 666 const __m256i dot = _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i *) p), _mm256_set1_epi8(1)); 667 const __m256i sum = _mm256_madd_epi16(_mm256_set1_epi16(1), dot); // 8 int32 668 // reduce 8 lanes: lo+hi -> 4 lanes, hadd -> 2, hadd -> 1 (a single hadd pair only folds 4 lanes) 669 const __m128i t = _mm_add_epi32(_mm256_castsi256_si128(sum), _mm256_extracti128_si256(sum, 1)); 670 const __m128i h = _mm_hadd_epi32(t, t); 671 return _mm_cvtsi128_si32(_mm_hadd_epi32(h, h)); 672 } 673 // sum 16 unsigned bytes to int32 674 static int32_t tiled_byte_sum_16(const uint8_t * p) { 675 const __m128i dot = _mm_maddubs_epi16(_mm_loadu_si128((const __m128i *) p), _mm_set1_epi8(1)); 676 const __m128i sum = _mm_madd_epi16(_mm_set1_epi16(1), dot); // 4 int32 677 const __m128i h = _mm_hadd_epi32(sum, sum); 678 return _mm_cvtsi128_si32(_mm_hadd_epi32(h, h)); 679 } 680 #endif 681 682 // act-bias: precompute the per-weight-row correction corr = 128*sum_s scales[s]*w_bsum[s] where 683 // w_bsum[s] = sum of the debiased weight bytes in subblock s. Stored in mins[r][0] (unused for 684 // HAS_MIN = false, which is exactly the act-bias case). Reused across all act bands and weight groups. 685 // No-op off AVX2 (the act-bias MAC is AVX2-only) and when corr is false (iq4_xs / BIAS = 0). 686 template 687 void tiled_repack_src0(tiled_tile_src0 * tile, int n_rows, int num_k, int BIAS, bool corr) { 688 #if defined(__AVX2__) 689 if (!corr) { 690 return; 691 } 692 constexpr int NB = TILED_TILE_K / SUBBLK; 693 const int nb_stride = NB * num_k; 694 for (int slab = 0; slab < num_k; slab++) { 695 for (int r = 0; r < n_rows; r++) { 696 const uint8_t * q = &tile->q[r * (num_k * TILED_TILE_K) + slab * TILED_TILE_K]; 697 const int32_t * scales = &tile->scales[r * nb_stride + slab * NB]; 698 int32_t corr_val = 0; 699 for (int s = 0; s < NB; s++) { 700 const int32_t qsum = (SUBBLK == 32) ? tiled_byte_sum_32(&q[s * SUBBLK]) : tiled_byte_sum_16(&q[s * SUBBLK]); 701 corr_val += 128 * scales[s] * (qsum - SUBBLK * BIAS); 702 } 703 tile->mins[r * nb_stride + slab * NB] = corr_val; 704 } 705 } 706 #else 707 GGML_UNUSED(tile); GGML_UNUSED(n_rows); GGML_UNUSED(num_k); GGML_UNUSED(BIAS); GGML_UNUSED(corr); 708 #endif 709 } 710 711 template void tiled_repack_src0<16>(tiled_tile_src0 * tile, int n_rows, int num_k, int BIAS, bool corr); 712 template void tiled_repack_src0<32>(tiled_tile_src0 * tile, int n_rows, int num_k, int BIAS, bool corr); 713 714