ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / esimd.hpp
| 1 | #ifndef GGML_SYCL_ESIMD_HPP |
| 2 | #define GGML_SYCL_ESIMD_HPP |
| 3 | |
| 4 | #include <sycl/ext/intel/esimd.hpp> |
| 5 | #include <sycl/ext/intel/esimd/xmx/dpas.hpp> |
| 6 | #include <sycl/ext/intel/experimental/grf_size_properties.hpp> |
| 7 | |
| 8 | #include "common.hpp" |
| 9 | |
| 10 | namespace ggml_sycl_esimd { |
| 11 | |
| 12 | constexpr int GGML_SYCL_DMMV_ESIMD_WG_SIZE = 4; |
| 13 | |
| 14 | // |
| 15 | // Shared ESIMD building blocks for the reordered K-quant dequantize-matvec |
| 16 | // kernels. |
| 17 | // |
| 18 | // The reordered K-quant ESIMD matvec kernels share one skeleton: per super-block, |
| 19 | // load a 256-float activation slice, load one weight block, dequantize it into 8 |
| 20 | // chunks of 32 and MAC each chunk against the matching activation slice, then |
| 21 | // reduce and run a lane-0 epilogue. |
| 22 | // |
| 23 | // Each K-quant kernel emits exactly 8 chunks of 32 mapping to activation slices |
| 24 | // 0..7, so the per-block work is captured by esimd_reorder_q_traits<T>::mac_pair, |
| 25 | // which dequantizes two weight blocks and MACs both against a shared activation |
| 26 | // vector with the two FMA chains interleaved (co-scheduled to hide FMA latency). |
| 27 | // The "pair" is the (row0,row1) row pair owned by one work-group, so the |
| 28 | // layout+dequant is written once per quant type here. |
| 29 | // |
| 30 | |
| 31 | template <ggml_type T> struct esimd_reorder_q_traits; |
| 32 | |
| 33 | // build a 32-lane vector whose low 16 lanes are `lo` and high 16 are `hi` |
| 34 | // (a super-chunk splits into two 16-wide halves with distinct scale/min codes). |
| 35 | static ESIMD_INLINE sycl::ext::intel::esimd::simd<float, 32> splat_lo_hi(float lo, float hi) { |
| 36 | using namespace sycl::ext::intel::esimd; |
| 37 | simd<float, 32> v; |
| 38 | v.select<16, 1>(0) = lo; |
| 39 | v.select<16, 1>(16) = hi; |
| 40 | return v; |
| 41 | } |
| 42 | |
| 43 | // unpack one block of Q4_K/Q5_K scale/min codes (get_scale_min_k4 layout) into 8 |
| 44 | // float scales (dall * sc) and 8 float mins (-dmin * m); the min carries the |
| 45 | // negation so the dequant epilogue adds. |
| 46 | static ESIMD_INLINE void unpack_scale_min_k4( |
| 47 | sycl::ext::intel::esimd::simd<uint8_t, 12> scales, float dall, float dmin, |
| 48 | sycl::ext::intel::esimd::simd<float, 8> & scale_f, |
| 49 | sycl::ext::intel::esimd::simd<float, 8> & min_f) { |
| 50 | using namespace sycl::ext::intel::esimd; |
| 51 | simd<uint8_t, 8> sc = 0; |
| 52 | simd<uint8_t, 8> m = 0; |
| 53 | simd<uint8_t, 4> scale_lo = scales.select<4, 1>(0); |
| 54 | simd<uint8_t, 4> min_lo = scales.select<4, 1>(4); |
| 55 | simd<uint8_t, 4> hi_bits = scales.select<4, 1>(8); |
| 56 | sc.select<4, 1>(0) = scale_lo & simd<uint8_t, 4>(0x3F); |
| 57 | sc.select<4, 1>(4) = (hi_bits & simd<uint8_t, 4>(0x0F)) | |
| 58 | ((scale_lo >> simd<uint8_t, 4>(6)) << simd<uint8_t, 4>(4)); |
| 59 | m.select<4, 1>(0) = min_lo & simd<uint8_t, 4>(0x3F); |
| 60 | m.select<4, 1>(4) = (hi_bits >> simd<uint8_t, 4>(4)) | |
| 61 | ((min_lo >> simd<uint8_t, 4>(6)) << simd<uint8_t, 4>(4)); |
| 62 | scale_f = convert<float>(sc) * dall; |
| 63 | min_f = convert<float>(m) * (-dmin); |
| 64 | } |
| 65 | |
| 66 | // --------------------------------------------------------------------------- |
| 67 | // Q2_K, SOA reorder layout produced by reorder_qw_q2_k: |
| 68 | // [qs: nb*(QK_K/4)] [scales: nb*(QK_K/16)] [dm: nb*sizeof(half2)] |
| 69 | // with nb = nrows*num_blocks_per_row. |
| 70 | // |
| 71 | // 2 bits per weight. The 8 output chunks of 32 (matching dequantize_row_q2_K) |
| 72 | // map to super-chunk s (0..7): byte base 32*(s/4) into the 64-byte qs array, |
| 73 | // bit shift 2*(s%4); the low 16 lanes use scales[2s], the high 16 use |
| 74 | // scales[2s+1], with dl = d*(sc & 0xF), ml = dmin*(sc >> 4), deq = dl*q - ml. |
| 75 | // --------------------------------------------------------------------------- |
| 76 | template <> struct esimd_reorder_q_traits<GGML_TYPE_Q2_K> { |
| 77 | struct ptrs { |
| 78 | const uint8_t * qs; |
| 79 | const uint8_t * scales; |
| 80 | const sycl::half * dm; |
| 81 | }; |
| 82 | |
| 83 | static ESIMD_INLINE ptrs make_ptrs(const void * vx, size_t nb) { |
| 84 | const uint8_t * qs = (const uint8_t *) vx; |
| 85 | const uint8_t * scales = qs + nb * (QK_K / 4); |
| 86 | const sycl::half * dm = (const sycl::half *) (scales + nb * (QK_K / 16)); |
| 87 | return { qs, scales, dm }; |
| 88 | } |
| 89 | |
| 90 | static ESIMD_INLINE void mac_pair( |
| 91 | const ptrs & pa, size_t bia, |
| 92 | const ptrs & pb, size_t bib, bool has_b, |
| 93 | sycl::ext::intel::esimd::simd<float, 256> & y_vec, |
| 94 | sycl::ext::intel::esimd::simd<float, 32> & acc_a, |
| 95 | sycl::ext::intel::esimd::simd<float, 32> & acc_b) { |
| 96 | using namespace sycl::ext::intel::esimd; |
| 97 | |
| 98 | simd<uint8_t, 64> qs_a = block_load<uint8_t, 64>(pa.qs + bia * (QK_K / 4)); |
| 99 | simd<uint8_t, 64> qs_b = 0; |
| 100 | simd<uint8_t, 16> scales_a = block_load<uint8_t, 16>(pa.scales + bia * (QK_K / 16)); |
| 101 | simd<uint8_t, 16> scales_b = 0; |
| 102 | |
| 103 | const float dall_a = (float) pa.dm[bia * 2 + 0]; |
| 104 | const float dmin_a = (float) pa.dm[bia * 2 + 1]; |
| 105 | float dall_b = 0.0f; |
| 106 | float dmin_b = 0.0f; |
| 107 | if (has_b) { |
| 108 | qs_b = block_load<uint8_t, 64>(pb.qs + bib * (QK_K / 4)); |
| 109 | scales_b = block_load<uint8_t, 16>(pb.scales + bib * (QK_K / 16)); |
| 110 | dall_b = (float) pb.dm[bib * 2 + 0]; |
| 111 | dmin_b = (float) pb.dm[bib * 2 + 1]; |
| 112 | } |
| 113 | |
| 114 | // per-chunk scale (d * (sc & 0xF)) and min (-dmin * (sc >> 4)), all 16 codes; |
| 115 | // min carries the negation so the dequant epilogue adds (matches Q4_K/Q5_K) |
| 116 | simd<float, 16> scale_f_a = convert<float>(scales_a & simd<uint8_t, 16>(0x0F)) * dall_a; |
| 117 | simd<float, 16> min_f_a = convert<float>(scales_a >> simd<uint8_t, 16>(4)) * (-dmin_a); |
| 118 | simd<float, 16> scale_f_b = convert<float>(scales_b & simd<uint8_t, 16>(0x0F)) * dall_b; |
| 119 | simd<float, 16> min_f_b = convert<float>(scales_b >> simd<uint8_t, 16>(4)) * (-dmin_b); |
| 120 | |
| 121 | #pragma unroll |
| 122 | for (int s = 0; s < 8; ++s) { |
| 123 | const int byte_base = 32 * (s / 4); |
| 124 | const uint8_t shift = (uint8_t) (2 * (s % 4)); |
| 125 | simd<float, 32> y_s = y_vec.select<32, 1>(s * 32); |
| 126 | |
| 127 | simd<uint8_t, 32> qa = (qs_a.select<32, 1>(byte_base) >> shift) & simd<uint8_t, 32>(3); |
| 128 | simd<uint8_t, 32> qb = (qs_b.select<32, 1>(byte_base) >> shift) & simd<uint8_t, 32>(3); |
| 129 | |
| 130 | const float scale_a_lo = scale_f_a[2 * s + 0]; |
| 131 | const float scale_a_hi = scale_f_a[2 * s + 1]; |
| 132 | const float min_a_lo = min_f_a[2 * s + 0]; |
| 133 | const float min_a_hi = min_f_a[2 * s + 1]; |
| 134 | const float scale_b_lo = scale_f_b[2 * s + 0]; |
| 135 | const float scale_b_hi = scale_f_b[2 * s + 1]; |
| 136 | const float min_b_lo = min_f_b[2 * s + 0]; |
| 137 | const float min_b_hi = min_f_b[2 * s + 1]; |
| 138 | |
| 139 | simd<float, 32> scale_vec_a = splat_lo_hi(scale_a_lo, scale_a_hi); |
| 140 | simd<float, 32> min_vec_a = splat_lo_hi(min_a_lo, min_a_hi); |
| 141 | simd<float, 32> scale_vec_b = splat_lo_hi(scale_b_lo, scale_b_hi); |
| 142 | simd<float, 32> min_vec_b = splat_lo_hi(min_b_lo, min_b_hi); |
| 143 | |
| 144 | simd<float, 32> deq_a = convert<float>(qa) * scale_vec_a + min_vec_a; |
| 145 | simd<float, 32> deq_b = convert<float>(qb) * scale_vec_b + min_vec_b; |
| 146 | |
| 147 | acc_a += y_s * deq_a; |
| 148 | acc_b += y_s * deq_b; |
| 149 | } |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | // --------------------------------------------------------------------------- |
| 154 | // Q3_K, SOA reorder layout produced by reorder_qw_q3_k: |
| 155 | // [qs: nb*(QK_K/4)] [hmask: nb*(QK_K/8)] [scales: nb*12] [d: nb*sizeof(half)] |
| 156 | // with nb = nrows*num_blocks_per_row. Single super-block scale d, no dmin. |
| 157 | // |
| 158 | // 3 bits per weight: 2 low bits in qs, 1 high bit in hmask. The 8 output chunks |
| 159 | // of 32 (matching dequantize_row_q3_K) map to super-chunk s (0..7): byte base |
| 160 | // 32*(s/4) into the 64-byte qs array, bit shift 2*(s%4); the low 16 lanes use |
| 161 | // scale code 2s, the high 16 use 2s+1. hmask is a 32-byte array (like Q5_K's |
| 162 | // qh) where chunk s uses bit s of the same 32 bytes, but INVERTED: the value is |
| 163 | // (q & 3) - (hmask_bit_set ? 0 : 4), i.e. (q & 3) + 4*bit - 4. |
| 164 | // |
| 165 | // The 16 6-bit scale codes are packed into 12 bytes (get_scale_min layout for |
| 166 | // Q3_K): low nibbles from bytes 0..7, high 2 bits from bytes 8..11 shifted by |
| 167 | // 0/2/4/6; the dequant scale is d * (code - 32). |
| 168 | // --------------------------------------------------------------------------- |
| 169 | template <> struct esimd_reorder_q_traits<GGML_TYPE_Q3_K> { |
| 170 | struct ptrs { |
| 171 | const uint8_t * qs; |
| 172 | const uint8_t * hmask; |
| 173 | const uint8_t * scales; |
| 174 | const sycl::half * d; |
| 175 | }; |
| 176 | |
| 177 | static ESIMD_INLINE ptrs make_ptrs(const void * vx, size_t nb) { |
| 178 | const uint8_t * qs = (const uint8_t *) vx; |
| 179 | const uint8_t * hmask = qs + nb * (QK_K / 4); |
| 180 | const uint8_t * scales = hmask + nb * (QK_K / 8); |
| 181 | const sycl::half * d = (const sycl::half *) (scales + nb * 12); |
| 182 | return { qs, hmask, scales, d }; |
| 183 | } |
| 184 | |
| 185 | // unpack the 12 packed bytes into 16 6-bit scale codes (dequantize_row_q3_K |
| 186 | // aux layout), returned as float scale = d * (code - 32). |
| 187 | // done with wide (8/16-lane) ops rather than four 4-lane groups. |
| 188 | static ESIMD_INLINE sycl::ext::intel::esimd::simd<float, 16> unpack_scales( |
| 189 | sycl::ext::intel::esimd::simd<uint8_t, 12> in, float d) { |
| 190 | using namespace sycl::ext::intel::esimd; |
| 191 | |
| 192 | // low 6-bit part: codes 0..7 = low nibble of bytes 0..7, |
| 193 | // codes 8..15 = high nibble of bytes 0..7 |
| 194 | simd<uint8_t, 8> lo8 = in.select<8, 1>(0); |
| 195 | simd<uint8_t, 16> code; |
| 196 | code.select<8, 1>(0) = lo8 & simd<uint8_t, 8>(0x0F); |
| 197 | code.select<8, 1>(8) = lo8 >> simd<uint8_t, 8>(4); |
| 198 | |
| 199 | // high 2-bit part: bytes 8..11 replicated 4x, group g (0..3) shifted 2*g |
| 200 | simd<uint8_t, 16> hib; |
| 201 | hib.select<4, 1>(0) = in.select<4, 1>(8); |
| 202 | hib.select<4, 1>(4) = in.select<4, 1>(8); |
| 203 | hib.select<4, 1>(8) = in.select<4, 1>(8); |
| 204 | hib.select<4, 1>(12) = in.select<4, 1>(8); |
| 205 | simd<uint8_t, 16> hshift; |
| 206 | hshift.select<4, 1>(0) = 0; |
| 207 | hshift.select<4, 1>(4) = 2; |
| 208 | hshift.select<4, 1>(8) = 4; |
| 209 | hshift.select<4, 1>(12) = 6; |
| 210 | hib = (hib >> hshift) & simd<uint8_t, 16>(0x03); |
| 211 | |
| 212 | code = code | (hib << simd<uint8_t, 16>(4)); |
| 213 | return (convert<float>(code) - 32.0f) * d; |
| 214 | } |
| 215 | |
| 216 | static ESIMD_INLINE void mac_pair( |
| 217 | const ptrs & pa, size_t bia, |
| 218 | const ptrs & pb, size_t bib, bool has_b, |
| 219 | sycl::ext::intel::esimd::simd<float, 256> & y_vec, |
| 220 | sycl::ext::intel::esimd::simd<float, 32> & acc_a, |
| 221 | sycl::ext::intel::esimd::simd<float, 32> & acc_b) { |
| 222 | using namespace sycl::ext::intel::esimd; |
| 223 | |
| 224 | simd<uint8_t, 64> qs_a = block_load<uint8_t, 64>(pa.qs + bia * (QK_K / 4)); |
| 225 | simd<uint8_t, 64> qs_b = 0; |
| 226 | simd<uint8_t, 32> hmask_a = block_load<uint8_t, 32>(pa.hmask + bia * (QK_K / 8)); |
| 227 | simd<uint8_t, 32> hmask_b = 0; |
| 228 | simd<uint8_t, 12> scales_a = block_load<uint8_t, 12>(pa.scales + bia * 12); |
| 229 | simd<uint8_t, 12> scales_b = 0; |
| 230 | |
| 231 | const float d_a = (float) pa.d[bia]; |
| 232 | float d_b = 0.0f; |
| 233 | if (has_b) { |
| 234 | qs_b = block_load<uint8_t, 64>(pb.qs + bib * (QK_K / 4)); |
| 235 | hmask_b = block_load<uint8_t, 32>(pb.hmask + bib * (QK_K / 8)); |
| 236 | scales_b = block_load<uint8_t, 12>(pb.scales + bib * 12); |
| 237 | d_b = (float) pb.d[bib]; |
| 238 | } |
| 239 | |
| 240 | simd<float, 16> scale_f_a = unpack_scales(scales_a, d_a); |
| 241 | simd<float, 16> scale_f_b = unpack_scales(scales_b, d_b); |
| 242 | |
| 243 | #pragma unroll |
| 244 | for (int s = 0; s < 8; ++s) { |
| 245 | const int byte_base = 32 * (s / 4); |
| 246 | const uint8_t shift = (uint8_t) (2 * (s % 4)); |
| 247 | simd<float, 32> y_s = y_vec.select<32, 1>(s * 32); |
| 248 | |
| 249 | // 2 low bits from qs, high bit from hmask (bit s of the same 32 bytes); |
| 250 | // value = (q & 3) + 4*bit - 4 (inverted hmask: subtract 4 when bit clear). |
| 251 | // merge in the integer domain: q3 = (q & 3) | (bit << 2) in {0..7}, |
| 252 | // then a single convert + subtract yields q3 - 4 (one convert, not two) |
| 253 | simd<uint16_t, 32> q3_a = convert<uint16_t>( |
| 254 | (qs_a.select<32, 1>(byte_base) >> shift) & simd<uint8_t, 32>(3)); |
| 255 | q3_a |= convert<uint16_t>( |
| 256 | ((hmask_a >> simd<uint8_t, 32>((uint8_t) s)) & simd<uint8_t, 32>(1)) << simd<uint8_t, 32>(2)); |
| 257 | simd<uint16_t, 32> q3_b = convert<uint16_t>( |
| 258 | (qs_b.select<32, 1>(byte_base) >> shift) & simd<uint8_t, 32>(3)); |
| 259 | q3_b |= convert<uint16_t>( |
| 260 | ((hmask_b >> simd<uint8_t, 32>((uint8_t) s)) & simd<uint8_t, 32>(1)) << simd<uint8_t, 32>(2)); |
| 261 | |
| 262 | simd<float, 32> qf_a = convert<float>(q3_a) - 4.0f; |
| 263 | simd<float, 32> qf_b = convert<float>(q3_b) - 4.0f; |
| 264 | |
| 265 | const float scale_a_lo = scale_f_a[2 * s + 0]; |
| 266 | const float scale_a_hi = scale_f_a[2 * s + 1]; |
| 267 | const float scale_b_lo = scale_f_b[2 * s + 0]; |
| 268 | const float scale_b_hi = scale_f_b[2 * s + 1]; |
| 269 | |
| 270 | simd<float, 32> scale_vec_a = splat_lo_hi(scale_a_lo, scale_a_hi); |
| 271 | simd<float, 32> scale_vec_b = splat_lo_hi(scale_b_lo, scale_b_hi); |
| 272 | |
| 273 | simd<float, 32> deq_a = qf_a * scale_vec_a; |
| 274 | simd<float, 32> deq_b = qf_b * scale_vec_b; |
| 275 | |
| 276 | acc_a += y_s * deq_a; |
| 277 | acc_b += y_s * deq_b; |
| 278 | } |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | // --------------------------------------------------------------------------- |
| 283 | // Q4_K, SOA reorder layout produced by reorder_qw_q4_k: |
| 284 | // [qs: nb*(QK_K/2)] [scales: nb*K_SCALE_SIZE] [dm: nb*sizeof(half2)] |
| 285 | // with nb = nrows*num_blocks_per_row. |
| 286 | // --------------------------------------------------------------------------- |
| 287 | template <> struct esimd_reorder_q_traits<GGML_TYPE_Q4_K> { |
| 288 | struct ptrs { |
| 289 | const uint8_t * qs; |
| 290 | const uint8_t * scales; |
| 291 | const sycl::half * dm; |
| 292 | }; |
| 293 | |
| 294 | static ESIMD_INLINE ptrs make_ptrs(const void * vx, size_t nb) { |
| 295 | const uint8_t * qs = (const uint8_t *) vx; |
| 296 | const uint8_t * scales = qs + nb * (QK_K / 2); |
| 297 | const sycl::half * dm = (const sycl::half *) (scales + nb * K_SCALE_SIZE); |
| 298 | return { qs, scales, dm }; |
| 299 | } |
| 300 | |
| 301 | static ESIMD_INLINE void mac_pair( |
| 302 | const ptrs & pa, size_t bia, |
| 303 | const ptrs & pb, size_t bib, bool has_b, |
| 304 | sycl::ext::intel::esimd::simd<float, 256> & y_vec, |
| 305 | sycl::ext::intel::esimd::simd<float, 32> & acc_a, |
| 306 | sycl::ext::intel::esimd::simd<float, 32> & acc_b) { |
| 307 | using namespace sycl::ext::intel::esimd; |
| 308 | |
| 309 | simd<uint8_t, 128> qs_a = block_load<uint8_t, 128>(pa.qs + bia * (QK_K / 2)); |
| 310 | simd<uint8_t, 128> qs_b = 0; |
| 311 | simd<uint8_t, 12> scales_a = block_load<uint8_t, 12>(pa.scales + bia * K_SCALE_SIZE); |
| 312 | simd<uint8_t, 12> scales_b = 0; |
| 313 | |
| 314 | const float dall_a = (float) pa.dm[bia * 2 + 0]; |
| 315 | const float dmin_a = (float) pa.dm[bia * 2 + 1]; |
| 316 | float dall_b = 0.0f; |
| 317 | float dmin_b = 0.0f; |
| 318 | if (has_b) { |
| 319 | qs_b = block_load<uint8_t, 128>(pb.qs + bib * (QK_K / 2)); |
| 320 | scales_b = block_load<uint8_t, 12>(pb.scales + bib * K_SCALE_SIZE); |
| 321 | dall_b = (float) pb.dm[bib * 2 + 0]; |
| 322 | dmin_b = (float) pb.dm[bib * 2 + 1]; |
| 323 | } |
| 324 | |
| 325 | simd<float, 8> scale_f_a, min_f_a, scale_f_b, min_f_b; |
| 326 | unpack_scale_min_k4(scales_a, dall_a, dmin_a, scale_f_a, min_f_a); |
| 327 | unpack_scale_min_k4(scales_b, dall_b, dmin_b, scale_f_b, min_f_b); |
| 328 | |
| 329 | simd<uint8_t, 128> qs_lo_a = qs_a & simd<uint8_t, 128>(0x0F); |
| 330 | simd<uint8_t, 128> qs_hi_a = qs_a >> simd<uint8_t, 128>(4); |
| 331 | simd<uint8_t, 128> qs_lo_b = qs_b & simd<uint8_t, 128>(0x0F); |
| 332 | simd<uint8_t, 128> qs_hi_b = qs_b >> simd<uint8_t, 128>(4); |
| 333 | |
| 334 | #pragma unroll |
| 335 | for (int sb = 0; sb < 8; sb += 2) { |
| 336 | const int q_offset = sb * 16; |
| 337 | simd<float, 32> y_lo = y_vec.select<32, 1>(sb * 32); |
| 338 | simd<float, 32> y_hi = y_vec.select<32, 1>((sb + 1) * 32); |
| 339 | |
| 340 | const float scale_a_lo = scale_f_a[sb]; |
| 341 | const float scale_a_hi = scale_f_a[sb + 1]; |
| 342 | const float min_a_lo = min_f_a[sb]; |
| 343 | const float min_a_hi = min_f_a[sb + 1]; |
| 344 | const float scale_b_lo = scale_f_b[sb]; |
| 345 | const float scale_b_hi = scale_f_b[sb + 1]; |
| 346 | const float min_b_lo = min_f_b[sb]; |
| 347 | const float min_b_hi = min_f_b[sb + 1]; |
| 348 | |
| 349 | simd<uint8_t, 32> qa_lo = qs_lo_a.select<32, 1>(q_offset); |
| 350 | simd<uint8_t, 32> qa_hi = qs_hi_a.select<32, 1>(q_offset); |
| 351 | simd<uint8_t, 32> qb_lo = qs_lo_b.select<32, 1>(q_offset); |
| 352 | simd<uint8_t, 32> qb_hi = qs_hi_b.select<32, 1>(q_offset); |
| 353 | |
| 354 | simd<float, 32> deq_a_lo = convert<float>(qa_lo) * scale_a_lo + min_a_lo; |
| 355 | simd<float, 32> deq_a_hi = convert<float>(qa_hi) * scale_a_hi + min_a_hi; |
| 356 | simd<float, 32> deq_b_lo = convert<float>(qb_lo) * scale_b_lo + min_b_lo; |
| 357 | simd<float, 32> deq_b_hi = convert<float>(qb_hi) * scale_b_hi + min_b_hi; |
| 358 | |
| 359 | acc_a += y_lo * deq_a_lo; |
| 360 | acc_b += y_lo * deq_b_lo; |
| 361 | acc_a += y_hi * deq_a_hi; |
| 362 | acc_b += y_hi * deq_b_hi; |
| 363 | } |
| 364 | } |
| 365 | }; |
| 366 | |
| 367 | // --------------------------------------------------------------------------- |
| 368 | // Q5_K, SOA reorder layout produced by reorder_qw_q5_k: |
| 369 | // [qs: nb*(QK_K/2)] [qh: nb*(QK_K/8)] [scales: nb*K_SCALE_SIZE] [dm: nb*sizeof(half2)] |
| 370 | // with nb = nrows*num_blocks_per_row. |
| 371 | // |
| 372 | // Identical to Q4_K except each 4-bit quant gains a 5th (high) bit from qh: |
| 373 | // output chunk c (0..7) adds 16 when bit c of qh[l] is set, where qh[l] indexes |
| 374 | // the same 32 bytes for every chunk (matches dequantize_row_q5_K). |
| 375 | // --------------------------------------------------------------------------- |
| 376 | template <> struct esimd_reorder_q_traits<GGML_TYPE_Q5_K> { |
| 377 | struct ptrs { |
| 378 | const uint8_t * qs; |
| 379 | const uint8_t * qh; |
| 380 | const uint8_t * scales; |
| 381 | const sycl::half * dm; |
| 382 | }; |
| 383 | |
| 384 | static ESIMD_INLINE ptrs make_ptrs(const void * vx, size_t nb) { |
| 385 | const uint8_t * qs = (const uint8_t *) vx; |
| 386 | const uint8_t * qh = qs + nb * (QK_K / 2); |
| 387 | const uint8_t * scales = qh + nb * (QK_K / 8); |
| 388 | const sycl::half * dm = (const sycl::half *) (scales + nb * K_SCALE_SIZE); |
| 389 | return { qs, qh, scales, dm }; |
| 390 | } |
| 391 | |
| 392 | // extract bit `bit` (0..7) of each lane and move it to bit position 4, |
| 393 | // e.g. for the 4-bit base quant's 5th (high) bit. `bit` is always a |
| 394 | // compile-time-known unrolled loop constant at call sites, so this folds |
| 395 | // to a single mask (bit==4), mask+left-shift (bit<4), or mask+right-shift |
| 396 | // (bit>4) instead of the shift+mask+shift a naive `(qh>>bit & 1) << 4` emits. |
| 397 | static ESIMD_INLINE sycl::ext::intel::esimd::simd<uint16_t, 32> extract_bit_to_pos4( |
| 398 | sycl::ext::intel::esimd::simd<uint8_t, 32> qh, int bit) { |
| 399 | using namespace sycl::ext::intel::esimd; |
| 400 | simd<uint16_t, 32> masked = convert<uint16_t>(qh & simd<uint8_t, 32>((uint8_t) (1u << bit))); |
| 401 | if (bit < 4) { |
| 402 | return masked << simd<uint16_t, 32>((uint16_t) (4 - bit)); |
| 403 | } else if (bit > 4) { |
| 404 | return masked >> simd<uint16_t, 32>((uint16_t) (bit - 4)); |
| 405 | } |
| 406 | return masked; |
| 407 | } |
| 408 | |
| 409 | static ESIMD_INLINE void mac_pair( |
| 410 | const ptrs & pa, size_t bia, |
| 411 | const ptrs & pb, size_t bib, bool has_b, |
| 412 | sycl::ext::intel::esimd::simd<float, 256> & y_vec, |
| 413 | sycl::ext::intel::esimd::simd<float, 32> & acc_a, |
| 414 | sycl::ext::intel::esimd::simd<float, 32> & acc_b) { |
| 415 | using namespace sycl::ext::intel::esimd; |
| 416 | |
| 417 | simd<uint8_t, 128> qs_a = block_load<uint8_t, 128>(pa.qs + bia * (QK_K / 2)); |
| 418 | simd<uint8_t, 128> qs_b = 0; |
| 419 | simd<uint8_t, 32> qh_a = block_load<uint8_t, 32>(pa.qh + bia * (QK_K / 8)); |
| 420 | simd<uint8_t, 32> qh_b = 0; |
| 421 | simd<uint8_t, 12> scales_a = block_load<uint8_t, 12>(pa.scales + bia * K_SCALE_SIZE); |
| 422 | simd<uint8_t, 12> scales_b = 0; |
| 423 | |
| 424 | const float dall_a = (float) pa.dm[bia * 2 + 0]; |
| 425 | const float dmin_a = (float) pa.dm[bia * 2 + 1]; |
| 426 | float dall_b = 0.0f; |
| 427 | float dmin_b = 0.0f; |
| 428 | if (has_b) { |
| 429 | qs_b = block_load<uint8_t, 128>(pb.qs + bib * (QK_K / 2)); |
| 430 | qh_b = block_load<uint8_t, 32>(pb.qh + bib * (QK_K / 8)); |
| 431 | scales_b = block_load<uint8_t, 12>(pb.scales + bib * K_SCALE_SIZE); |
| 432 | dall_b = (float) pb.dm[bib * 2 + 0]; |
| 433 | dmin_b = (float) pb.dm[bib * 2 + 1]; |
| 434 | } |
| 435 | |
| 436 | simd<float, 8> scale_f_a, min_f_a, scale_f_b, min_f_b; |
| 437 | unpack_scale_min_k4(scales_a, dall_a, dmin_a, scale_f_a, min_f_a); |
| 438 | unpack_scale_min_k4(scales_b, dall_b, dmin_b, scale_f_b, min_f_b); |
| 439 | |
| 440 | simd<uint8_t, 128> qs_lo_a = qs_a & simd<uint8_t, 128>(0x0F); |
| 441 | simd<uint8_t, 128> qs_hi_a = qs_a >> simd<uint8_t, 128>(4); |
| 442 | simd<uint8_t, 128> qs_lo_b = qs_b & simd<uint8_t, 128>(0x0F); |
| 443 | simd<uint8_t, 128> qs_hi_b = qs_b >> simd<uint8_t, 128>(4); |
| 444 | |
| 445 | #pragma unroll |
| 446 | for (int sb = 0; sb < 8; sb += 2) { |
| 447 | const int q_offset = sb * 16; |
| 448 | simd<float, 32> y_lo = y_vec.select<32, 1>(sb * 32); |
| 449 | simd<float, 32> y_hi = y_vec.select<32, 1>((sb + 1) * 32); |
| 450 | |
| 451 | const float scale_a_lo = scale_f_a[sb]; |
| 452 | const float scale_a_hi = scale_f_a[sb + 1]; |
| 453 | const float min_a_lo = min_f_a[sb]; |
| 454 | const float min_a_hi = min_f_a[sb + 1]; |
| 455 | const float scale_b_lo = scale_f_b[sb]; |
| 456 | const float scale_b_hi = scale_f_b[sb + 1]; |
| 457 | const float min_b_lo = min_f_b[sb]; |
| 458 | const float min_b_hi = min_f_b[sb + 1]; |
| 459 | |
| 460 | simd<uint8_t, 32> qa_lo_u8 = qs_lo_a.select<32, 1>(q_offset); |
| 461 | simd<uint8_t, 32> qa_hi_u8 = qs_hi_a.select<32, 1>(q_offset); |
| 462 | simd<uint8_t, 32> qb_lo_u8 = qs_lo_b.select<32, 1>(q_offset); |
| 463 | simd<uint8_t, 32> qb_hi_u8 = qs_hi_b.select<32, 1>(q_offset); |
| 464 | simd<uint16_t, 32> qa_lo = convert<uint16_t>(qa_lo_u8); |
| 465 | simd<uint16_t, 32> qa_hi = convert<uint16_t>(qa_hi_u8); |
| 466 | simd<uint16_t, 32> qb_lo = convert<uint16_t>(qb_lo_u8); |
| 467 | simd<uint16_t, 32> qb_hi = convert<uint16_t>(qb_hi_u8); |
| 468 | |
| 469 | // add the 5th bit: chunk sb uses qh bit sb, chunk sb+1 uses qh bit sb+1; |
| 470 | // qh always indexes the same 32 bytes regardless of chunk |
| 471 | qa_lo += extract_bit_to_pos4(qh_a, sb); |
| 472 | qa_hi += extract_bit_to_pos4(qh_a, sb + 1); |
| 473 | qb_lo += extract_bit_to_pos4(qh_b, sb); |
| 474 | qb_hi += extract_bit_to_pos4(qh_b, sb + 1); |
| 475 | |
| 476 | simd<float, 32> deq_a_lo = convert<float>(qa_lo) * scale_a_lo + min_a_lo; |
| 477 | simd<float, 32> deq_a_hi = convert<float>(qa_hi) * scale_a_hi + min_a_hi; |
| 478 | simd<float, 32> deq_b_lo = convert<float>(qb_lo) * scale_b_lo + min_b_lo; |
| 479 | simd<float, 32> deq_b_hi = convert<float>(qb_hi) * scale_b_hi + min_b_hi; |
| 480 | |
| 481 | acc_a += y_lo * deq_a_lo; |
| 482 | acc_b += y_lo * deq_b_lo; |
| 483 | acc_a += y_hi * deq_a_hi; |
| 484 | acc_b += y_hi * deq_b_hi; |
| 485 | } |
| 486 | } |
| 487 | }; |
| 488 | |
| 489 | // --------------------------------------------------------------------------- |
| 490 | // Q6_K, SOA reorder layout: |
| 491 | // [ql: nb*(QK_K/2)] [qh: nb*(QK_K/4)] [scales(int8): nb*(QK_K/16)] [d: nb*half] |
| 492 | // --------------------------------------------------------------------------- |
| 493 | template <> struct esimd_reorder_q_traits<GGML_TYPE_Q6_K> { |
| 494 | struct ptrs { |
| 495 | const uint8_t * ql; |
| 496 | const uint8_t * qh; |
| 497 | const int8_t * scales; |
| 498 | const sycl::half * d; |
| 499 | }; |
| 500 | |
| 501 | static ESIMD_INLINE ptrs make_ptrs(const void * vx, size_t nb) { |
| 502 | const uint8_t * ql = (const uint8_t *) vx; |
| 503 | const uint8_t * qh = ql + nb * (QK_K / 2); |
| 504 | const int8_t * scales = (const int8_t *) (qh + nb * (QK_K / 4)); |
| 505 | const sycl::half * d = (const sycl::half *) (scales + nb * (QK_K / 16)); |
| 506 | return { ql, qh, scales, d }; |
| 507 | } |
| 508 | |
| 509 | static ESIMD_INLINE void mac_pair( |
| 510 | const ptrs & pa, size_t bia, |
| 511 | const ptrs & pb, size_t bib, bool has_b, |
| 512 | sycl::ext::intel::esimd::simd<float, 256> & y_vec, |
| 513 | sycl::ext::intel::esimd::simd<float, 32> & acc_a, |
| 514 | sycl::ext::intel::esimd::simd<float, 32> & acc_b) { |
| 515 | using namespace sycl::ext::intel::esimd; |
| 516 | |
| 517 | simd<uint8_t, 128> ql_a = block_load<uint8_t, 128>(pa.ql + bia * (QK_K / 2)); |
| 518 | simd<uint8_t, 128> ql_b = 0; |
| 519 | simd<uint8_t, 64> qh_a = block_load<uint8_t, 64>(pa.qh + bia * (QK_K / 4)); |
| 520 | simd<uint8_t, 64> qh_b = 0; |
| 521 | simd<int8_t, 16> scales_a = block_load<int8_t, 16>(pa.scales + bia * (QK_K / 16)); |
| 522 | simd<int8_t, 16> scales_b = 0; |
| 523 | |
| 524 | const float d_a = (float) pa.d[bia]; |
| 525 | float d_b = 0.0f; |
| 526 | if (has_b) { |
| 527 | ql_b = block_load<uint8_t, 128>(pb.ql + bib * (QK_K / 2)); |
| 528 | qh_b = block_load<uint8_t, 64>(pb.qh + bib * (QK_K / 4)); |
| 529 | scales_b = block_load<int8_t, 16>(pb.scales + bib * (QK_K / 16)); |
| 530 | d_b = (float) pb.d[bib]; |
| 531 | } |
| 532 | |
| 533 | simd<float, 16> sc_a = convert<float>(scales_a); |
| 534 | simd<float, 16> sc_b = convert<float>(scales_b); |
| 535 | |
| 536 | #pragma unroll |
| 537 | for (int im = 0; im < 2; ++im) { |
| 538 | simd<uint8_t, 32> ql_lo_a = ql_a.select<32, 1>(64 * im); |
| 539 | simd<uint8_t, 32> ql_hi_a = ql_a.select<32, 1>(64 * im + 32); |
| 540 | simd<uint8_t, 32> qh_bits_a = qh_a.select<32, 1>(32 * im); |
| 541 | simd<uint8_t, 32> ql_lo_b = ql_b.select<32, 1>(64 * im); |
| 542 | simd<uint8_t, 32> ql_hi_b = ql_b.select<32, 1>(64 * im + 32); |
| 543 | simd<uint8_t, 32> qh_bits_b = qh_b.select<32, 1>(32 * im); |
| 544 | |
| 545 | // reconstruct each 32-wide 6-bit group (matches dequantize_row_q6_K) |
| 546 | #pragma unroll |
| 547 | for (int g = 0; g < 4; ++g) { |
| 548 | simd<float, 32> y_g = y_vec.select<32, 1>(32 * (4 * im + g)); |
| 549 | |
| 550 | const float scale_a_lo = sc_a[8 * im + 2 * g + 0] * d_a; |
| 551 | const float scale_a_hi = sc_a[8 * im + 2 * g + 1] * d_a; |
| 552 | const float scale_b_lo = sc_b[8 * im + 2 * g + 0] * d_b; |
| 553 | const float scale_b_hi = sc_b[8 * im + 2 * g + 1] * d_b; |
| 554 | |
| 555 | simd<float, 32> scale_vec_a = splat_lo_hi(scale_a_lo, scale_a_hi); |
| 556 | simd<float, 32> scale_vec_b = splat_lo_hi(scale_b_lo, scale_b_hi); |
| 557 | |
| 558 | simd<uint8_t, 32> qa; |
| 559 | simd<uint8_t, 32> qb; |
| 560 | switch (g) { |
| 561 | case 0: |
| 562 | qa = (ql_lo_a & simd<uint8_t, 32>(0x0F)) | ((qh_bits_a & simd<uint8_t, 32>(0x03)) << simd<uint8_t, 32>(4)); |
| 563 | qb = (ql_lo_b & simd<uint8_t, 32>(0x0F)) | ((qh_bits_b & simd<uint8_t, 32>(0x03)) << simd<uint8_t, 32>(4)); |
| 564 | break; |
| 565 | case 1: |
| 566 | qa = (ql_hi_a & simd<uint8_t, 32>(0x0F)) | ((qh_bits_a & simd<uint8_t, 32>(0x0C)) << simd<uint8_t, 32>(2)); |
| 567 | qb = (ql_hi_b & simd<uint8_t, 32>(0x0F)) | ((qh_bits_b & simd<uint8_t, 32>(0x0C)) << simd<uint8_t, 32>(2)); |
| 568 | break; |
| 569 | case 2: |
| 570 | qa = (ql_lo_a >> simd<uint8_t, 32>(4)) | (qh_bits_a & simd<uint8_t, 32>(0x30)); |
| 571 | qb = (ql_lo_b >> simd<uint8_t, 32>(4)) | (qh_bits_b & simd<uint8_t, 32>(0x30)); |
| 572 | break; |
| 573 | default: |
| 574 | qa = (ql_hi_a >> simd<uint8_t, 32>(4)) | ((qh_bits_a & simd<uint8_t, 32>(0xC0)) >> simd<uint8_t, 32>(2)); |
| 575 | qb = (ql_hi_b >> simd<uint8_t, 32>(4)) | ((qh_bits_b & simd<uint8_t, 32>(0xC0)) >> simd<uint8_t, 32>(2)); |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | simd<float, 32> deq_a = (convert<float>(qa) - 32.0f) * scale_vec_a; |
| 580 | simd<float, 32> deq_b = (convert<float>(qb) - 32.0f) * scale_vec_b; |
| 581 | |
| 582 | acc_a += y_g * deq_a; |
| 583 | acc_b += y_g * deq_b; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | }; |
| 588 | |
| 589 | // |
| 590 | // Multi-column mul_mat_vec for reordered K-quant weights on the XMX engines (int8 DPAS). |
| 591 | // A thread owns 16 weight rows (B operand, one dword is 4 weights of a row), the q8_1 activation |
| 592 | // columns are the A operand. A super-block is 8 groups of 32, run as 2 parts of 4 groups. A quant |
| 593 | // type supplies its loads and unpack as a traits struct. |
| 594 | // |
| 595 | using sycl::ext::intel::esimd::simd; |
| 596 | constexpr int XMX_ROWS = 16; // weight rows per thread (DPAS N) |
| 597 | constexpr int XMX_CHUNK = 64; // widest single launch, more columns run as repeated launches |
| 598 | constexpr int XMX_FUSED_CHUNK = GGML_SYCL_XMX_GLU_MAX_COLS; // widest launch of the fused gate and up kernel |
| 599 | |
| 600 | // threads that split K for one tile, sized so that the shared memory stays small |
| 601 | template <int NC> constexpr int xmx_n_split() { |
| 602 | return NC <= 8 ? 32 : (NC == 16 ? 16 : 8); |
| 603 | } |
| 604 | |
| 605 | inline bool xmx_supported(const void * vx, const int ncols) { |
| 606 | // 2D block loads need a 64 byte aligned base |
| 607 | return ncols % QK_K == 0 && (uintptr_t) vx % 64 == 0; |
| 608 | } |
| 609 | |
| 610 | // transposed 2D block load of 16 rows, dword (kd, n) lands at [kd * 16 + n] |
| 611 | // 2D block loads need rows of at least 64 bytes, shorter rows (SR) are read row by row |
| 612 | template <int W, bool SR> |
| 613 | ESIMD_INLINE simd<uint32_t, W * XMX_ROWS> xmx_load_t(const void * base, unsigned pitch, unsigned h, int x, int y) { |
| 614 | using namespace sycl::ext::intel::esimd; |
| 615 | if constexpr (SR) { |
| 616 | simd<uint32_t, W * XMX_ROWS> r; |
| 617 | for (int n = 0; n < XMX_ROWS; ++n) { |
| 618 | const unsigned row = y + n < (int) h ? y + n : h; |
| 619 | const char * p = (const char *) base + (size_t) row * (pitch + 1); |
| 620 | r.template select<W, XMX_ROWS>(n) = block_load<uint32_t, W>((const uint32_t *) p + x); |
| 621 | } |
| 622 | return r; |
| 623 | } else { |
| 624 | return load_2d<uint32_t, W, XMX_ROWS, 1, true, false>((const uint32_t *) base, pitch, h, pitch, x, y); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | // per byte (q - zero) as int8 for q in 0..127, no borrow between bytes |
| 629 | ESIMD_INLINE simd<uint32_t, 128> xmx_bytes_sub(simd<uint32_t, 128> x, uint32_t zero_x4) { |
| 630 | return ((x | 0x80808080u) - zero_x4) ^ 0x80808080u; |
| 631 | } |
| 632 | |
| 633 | // How the scales of a quant type enter the DPAS epilogue of a group of 32 (see xmx_group) |
| 634 | enum class xmx_scale_kind { |
| 635 | split, // two scales per group, one per 16 wide half: the weights are masked to one half for each DPAS |
| 636 | min, // one scale and one min per group: a second DPAS sums the activations for the min term |
| 637 | min16, // a scale and a min per 16 wide half: the scales are folded into the DPAS operands |
| 638 | plain, // one scale per group |
| 639 | }; |
| 640 | |
| 641 | // What every quant type states about itself. nc_wide is the tile width from which it takes the large register file. |
| 642 | template <bool SR, int NC_WIDE, xmx_scale_kind KIND> struct xmx_traits_base { |
| 643 | static constexpr bool short_rows = SR; |
| 644 | static constexpr int nc_wide = NC_WIDE; |
| 645 | static constexpr bool has_min = KIND == xmx_scale_kind::min; |
| 646 | static constexpr bool plain = KIND == xmx_scale_kind::plain; |
| 647 | static constexpr bool min16 = KIND == xmx_scale_kind::min16; |
| 648 | }; |
| 649 | |
| 650 | // The part of a context every quant type shares: the last row (the clamp of 2D loads), this thread's first row and |
| 651 | // the number of super-blocks per row. A type adds its array pointers and 2D load pitches. |
| 652 | struct xmx_ctx_base { |
| 653 | unsigned h; |
| 654 | int row0; |
| 655 | int bpr; |
| 656 | }; |
| 657 | |
| 658 | ESIMD_INLINE void xmx_ctx_init(xmx_ctx_base & c, int nrows, int bpr, int row0) { |
| 659 | c.h = nrows - 1; |
| 660 | c.row0 = row0; |
| 661 | c.bpr = bpr; |
| 662 | } |
| 663 | |
| 664 | // pitch of a 2D load over an array with `bytes` bytes per super-block |
| 665 | constexpr unsigned xmx_pitch(int bpr, int bytes) { |
| 666 | return bpr * bytes - 1; |
| 667 | } |
| 668 | |
| 669 | // per group row scales of the 16 rows, one vector per group |
| 670 | template <int N> struct xmx_row_scales { |
| 671 | simd<float, 16> v[N]; |
| 672 | |
| 673 | ESIMD_INLINE simd<float, 16> get(int i) { return v[i]; } |
| 674 | }; |
| 675 | |
| 676 | // the 12 packed scale bytes of a super-block (Q3_K, Q4_K, Q5_K) of the 16 rows as three dwords |
| 677 | ESIMD_INLINE void xmx_load_sc12(const uint8_t * sc, |
| 678 | const simd<uint32_t, 16> & bi, |
| 679 | simd<uint32_t, 16> & w0, |
| 680 | simd<uint32_t, 16> & w1, |
| 681 | simd<uint32_t, 16> & w2) { |
| 682 | using namespace sycl::ext::intel::esimd; |
| 683 | w0 = gather<uint32_t, 16>((const uint32_t *) sc, bi * 12); |
| 684 | w1 = gather<uint32_t, 16>((const uint32_t *) sc, bi * 12 + 4); |
| 685 | w2 = gather<uint32_t, 16>((const uint32_t *) sc, bi * 12 + 8); |
| 686 | } |
| 687 | |
| 688 | // d and dmin (a half2 per super-block, Q2_K, Q4_K, Q5_K) of the 16 rows |
| 689 | ESIMD_INLINE void xmx_load_dm(const sycl::half * dm, |
| 690 | const simd<uint32_t, 16> & bi, |
| 691 | simd<float, 16> & d, |
| 692 | simd<float, 16> & dmin) { |
| 693 | using namespace sycl::ext::intel::esimd; |
| 694 | d = convert<float>(gather<sycl::half, 16>(dm, bi * (2 * (uint32_t) sizeof(sycl::half)))); |
| 695 | dmin = convert<float>( |
| 696 | gather<sycl::half, 16>(dm, bi * (2 * (uint32_t) sizeof(sycl::half)) + (uint32_t) sizeof(sycl::half))); |
| 697 | } |
| 698 | |
| 699 | // Q6_K reorder layout: [ql: nb*128] [qh: nb*64] [scales int8: nb*16] [d: nb*2] |
| 700 | template <bool SR> struct xmx_traits_q6_k : xmx_traits_base<SR, 7, xmx_scale_kind::split> { |
| 701 | struct ctx : xmx_ctx_base { |
| 702 | const uint8_t * ql; |
| 703 | const uint8_t * qh; |
| 704 | const int8_t * scales; |
| 705 | const sycl::half * dh; |
| 706 | unsigned pl, ph, ps; |
| 707 | }; |
| 708 | |
| 709 | static ESIMD_INLINE ctx make(const void * vx, int nrows, int bpr, int row0) { |
| 710 | const size_t nb = (size_t) nrows * bpr; |
| 711 | ctx c; |
| 712 | xmx_ctx_init(c, nrows, bpr, row0); |
| 713 | c.ql = (const uint8_t *) vx; |
| 714 | c.qh = c.ql + nb * (QK_K / 2); |
| 715 | c.scales = (const int8_t *) (c.qh + nb * (QK_K / 4)); |
| 716 | c.dh = (const sycl::half *) (c.scales + nb * (QK_K / 16)); |
| 717 | c.pl = xmx_pitch(bpr, QK_K / 2); |
| 718 | c.ph = xmx_pitch(bpr, QK_K / 4); |
| 719 | c.ps = xmx_pitch(bpr, QK_K / 16); |
| 720 | return c; |
| 721 | } |
| 722 | |
| 723 | // row scales of the 16 scale groups of a super-block |
| 724 | struct scales_t { |
| 725 | simd<int8_t, 256> sc8; |
| 726 | simd<float, 16> dv; |
| 727 | |
| 728 | ESIMD_INLINE simd<float, 16> get(int gr) { |
| 729 | simd<int8_t, 16> s = sc8.template select<16, 4>(64 * (gr / 4) + (gr % 4)); |
| 730 | return sycl::ext::intel::esimd::convert<float>(s) * dv; |
| 731 | } |
| 732 | }; |
| 733 | |
| 734 | // rows: clamped row index of each of the 16 lanes |
| 735 | static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) { |
| 736 | using namespace sycl::ext::intel::esimd; |
| 737 | scales_t s; |
| 738 | s.dv = convert<float>(gather<sycl::half, 16>(c.dh, (rows * c.bpr + sb) * (uint32_t) sizeof(sycl::half))); |
| 739 | simd<uint32_t, 64> raw = xmx_load_t<4, SR>(c.scales, c.ps, c.h, sb * 4, c.row0); |
| 740 | s.sc8 = raw.template bit_cast_view<int8_t>(); |
| 741 | return s; |
| 742 | } |
| 743 | |
| 744 | struct part_t { |
| 745 | simd<uint32_t, 128> a, b, h; |
| 746 | }; |
| 747 | |
| 748 | static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) { |
| 749 | part_t p; |
| 750 | p.a = xmx_load_t<8, SR>(c.ql, c.pl, c.h, sb * 32 + part * 16, c.row0); |
| 751 | p.b = xmx_load_t<8, SR>(c.ql, c.pl, c.h, sb * 32 + part * 16 + 8, c.row0); |
| 752 | p.h = xmx_load_t<8, SR>(c.qh, c.ph, c.h, sb * 16 + part * 8, c.row0); |
| 753 | return p; |
| 754 | } |
| 755 | |
| 756 | static ESIMD_INLINE simd<uint32_t, 128> unpack(const part_t & p, int g, int /*part*/) { |
| 757 | simd<uint32_t, 128> qs = (g & 1) ? p.b : p.a; |
| 758 | simd<uint32_t, 128> x; |
| 759 | if (g == 0) { |
| 760 | x = (qs & 0x0F0F0F0Fu) | ((p.h << 4) & 0x30303030u); |
| 761 | } else if (g == 1) { |
| 762 | x = (qs & 0x0F0F0F0Fu) | ((p.h << 2) & 0x30303030u); |
| 763 | } else if (g == 2) { |
| 764 | x = ((qs >> 4) & 0x0F0F0F0Fu) | (p.h & 0x30303030u); |
| 765 | } else { |
| 766 | x = ((qs >> 4) & 0x0F0F0F0Fu) | ((p.h >> 2) & 0x30303030u); |
| 767 | } |
| 768 | return xmx_bytes_sub(x, 0x20202020u); |
| 769 | } |
| 770 | }; |
| 771 | |
| 772 | // Q3_K reorder layout: [qs: nb*64] [hmask: nb*32] [scales: nb*12] [d: nb*2] |
| 773 | // chunk s of 32: 2 bits from qs byte 32*(s/4)+l at shift 2*(s%4), high bit is hmask[l] bit s, value is q - 4 |
| 774 | template <bool SR> struct xmx_traits_q3_k : xmx_traits_base<SR, 16, xmx_scale_kind::split> { |
| 775 | struct ctx : xmx_ctx_base { |
| 776 | const uint8_t * qs; |
| 777 | const uint8_t * hm; |
| 778 | const uint8_t * sc; |
| 779 | const sycl::half * dh; |
| 780 | unsigned pq, ph; |
| 781 | }; |
| 782 | |
| 783 | static ESIMD_INLINE ctx make(const void * vx, int nrows, int bpr, int row0) { |
| 784 | const size_t nb = (size_t) nrows * bpr; |
| 785 | ctx c; |
| 786 | xmx_ctx_init(c, nrows, bpr, row0); |
| 787 | c.qs = (const uint8_t *) vx; |
| 788 | c.hm = c.qs + nb * (QK_K / 4); |
| 789 | c.sc = c.hm + nb * (QK_K / 8); |
| 790 | c.dh = (const sycl::half *) (c.sc + nb * 12); |
| 791 | c.pq = xmx_pitch(bpr, QK_K / 4); |
| 792 | c.ph = xmx_pitch(bpr, QK_K / 8); |
| 793 | return c; |
| 794 | } |
| 795 | |
| 796 | using scales_t = xmx_row_scales<16>; |
| 797 | |
| 798 | static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) { |
| 799 | using namespace sycl::ext::intel::esimd; |
| 800 | scales_t s; |
| 801 | simd<uint32_t, 16> bi = rows * c.bpr + sb; |
| 802 | simd<uint32_t, 16> w0, w1, w2; |
| 803 | xmx_load_sc12(c.sc, bi, w0, w1, w2); |
| 804 | simd<float, 16> dv = convert<float>(gather<sycl::half, 16>(c.dh, bi * (uint32_t) sizeof(sycl::half))); |
| 805 | #pragma unroll |
| 806 | for (int i = 0; i < 16; ++i) { |
| 807 | const simd<uint32_t, 16> lo = (i % 8) < 4 ? w0 : w1; |
| 808 | const int bl = i % 4; |
| 809 | simd<uint32_t, 16> nib = (lo >> (8 * bl + (i < 8 ? 0 : 4))) & 0xFu; |
| 810 | simd<uint32_t, 16> hi = ((w2 >> (8 * bl + 2 * (i / 4))) & 3u) << 4; |
| 811 | simd<int, 16> code = nib | hi; |
| 812 | s.v[i] = convert<float>(code - 32) * dv; |
| 813 | } |
| 814 | return s; |
| 815 | } |
| 816 | |
| 817 | struct part_t { |
| 818 | simd<uint32_t, 128> q, h; |
| 819 | }; |
| 820 | |
| 821 | static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) { |
| 822 | part_t p; |
| 823 | p.q = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 16 + part * 8, c.row0); |
| 824 | p.h = xmx_load_t<8, SR>(c.hm, c.ph, c.h, sb * 8, c.row0); |
| 825 | return p; |
| 826 | } |
| 827 | |
| 828 | static ESIMD_INLINE simd<uint32_t, 128> unpack(const part_t & p, int g, int part) { |
| 829 | const int s = 4 * part + g; |
| 830 | simd<uint32_t, 128> x = ((p.q >> (2 * g)) & 0x03030303u) | (((p.h >> s) & 0x01010101u) << 2); |
| 831 | return xmx_bytes_sub(x, 0x04040404u); |
| 832 | } |
| 833 | }; |
| 834 | |
| 835 | // Q4_K and Q5_K (QH) reorder layout: [qs: nb*128] ([qh: nb*32]) [scales: nb*12] [dm: nb*4] |
| 836 | // group j of 32: nibble of qs byte 32*(j/2)+l (low for even j), Q5_K adds 16 * bit j of qh[l] |
| 837 | // value is dall * sc_j * q - dmin * m_j, one scale and one min per group |
| 838 | template <bool QH, bool SR> struct xmx_traits_q45_k : xmx_traits_base<SR, QH ? 16 : 7, xmx_scale_kind::min> { |
| 839 | struct ctx : xmx_ctx_base { |
| 840 | const uint8_t * qs; |
| 841 | const uint8_t * qh; |
| 842 | const uint8_t * sc; |
| 843 | const sycl::half * dm; |
| 844 | unsigned pq, ph; |
| 845 | }; |
| 846 | |
| 847 | static ESIMD_INLINE ctx make(const void * vx, int nrows, int bpr, int row0) { |
| 848 | const size_t nb = (size_t) nrows * bpr; |
| 849 | ctx c; |
| 850 | xmx_ctx_init(c, nrows, bpr, row0); |
| 851 | c.qs = (const uint8_t *) vx; |
| 852 | c.qh = c.qs + nb * (QK_K / 2); |
| 853 | c.sc = QH ? c.qh + nb * (QK_K / 8) : c.qh; |
| 854 | c.dm = (const sycl::half *) (c.sc + nb * 12); |
| 855 | c.pq = xmx_pitch(bpr, QK_K / 2); |
| 856 | c.ph = xmx_pitch(bpr, QK_K / 8); |
| 857 | return c; |
| 858 | } |
| 859 | |
| 860 | struct scales_t { |
| 861 | xmx_row_scales<8> d; // dall * scale of each group |
| 862 | xmx_row_scales<8> m; // dmin * min of each group |
| 863 | }; |
| 864 | |
| 865 | static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) { |
| 866 | using namespace sycl::ext::intel::esimd; |
| 867 | scales_t s; |
| 868 | simd<uint32_t, 16> bi = rows * c.bpr + sb; |
| 869 | simd<uint32_t, 16> w0, w1, w2; |
| 870 | xmx_load_sc12(c.sc, bi, w0, w1, w2); |
| 871 | simd<float, 16> d, dmin; |
| 872 | xmx_load_dm(c.dm, bi, d, dmin); |
| 873 | #pragma unroll |
| 874 | for (int j = 0; j < 8; ++j) { |
| 875 | const int jj = j % 4; |
| 876 | simd<uint32_t, 16> sc, m; |
| 877 | if (j < 4) { |
| 878 | sc = (w0 >> (8 * jj)) & 63u; |
| 879 | m = (w1 >> (8 * jj)) & 63u; |
| 880 | } else { |
| 881 | sc = ((w2 >> (8 * jj)) & 0xFu) | (((w0 >> (8 * jj + 6)) & 3u) << 4); |
| 882 | m = ((w2 >> (8 * jj + 4)) & 0xFu) | (((w1 >> (8 * jj + 6)) & 3u) << 4); |
| 883 | } |
| 884 | s.d.v[j] = convert<float>(simd<int, 16>(sc)) * d; |
| 885 | s.m.v[j] = convert<float>(simd<int, 16>(m)) * dmin; |
| 886 | } |
| 887 | return s; |
| 888 | } |
| 889 | |
| 890 | struct part_t { |
| 891 | simd<uint32_t, 128> a, b, h; |
| 892 | }; |
| 893 | |
| 894 | static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) { |
| 895 | part_t p; |
| 896 | p.a = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 32 + part * 16, c.row0); |
| 897 | p.b = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 32 + part * 16 + 8, c.row0); |
| 898 | if constexpr (QH) { |
| 899 | p.h = xmx_load_t<8, SR>(c.qh, c.ph, c.h, sb * 8, c.row0); |
| 900 | } |
| 901 | return p; |
| 902 | } |
| 903 | |
| 904 | static ESIMD_INLINE simd<uint32_t, 128> unpack(const part_t & p, int g, int part) { |
| 905 | simd<uint32_t, 128> qs = (g < 2) ? p.a : p.b; |
| 906 | simd<uint32_t, 128> x = (qs >> (4 * (g & 1))) & 0x0F0F0F0Fu; |
| 907 | if constexpr (QH) { |
| 908 | x |= ((p.h >> (4 * part + g)) & 0x01010101u) << 4; |
| 909 | } |
| 910 | return x; |
| 911 | } |
| 912 | }; |
| 913 | |
| 914 | // Q2_K reorder layout: [qs: nb*64] [scales: nb*16] [dm: nb*4] |
| 915 | // chunk s of 32: 2 bits from qs byte 32*(s/4)+l at shift 2*(s%4), each 16 wide half has a 4 bit scale and min |
| 916 | // value is d * (code & 15) * q - dmin * (code >> 4) |
| 917 | template <bool SR> struct xmx_traits_q2_k : xmx_traits_base<SR, 16, xmx_scale_kind::min16> { |
| 918 | struct ctx : xmx_ctx_base { |
| 919 | const uint8_t * qs; |
| 920 | const uint8_t * sc; |
| 921 | const sycl::half * dm; |
| 922 | unsigned pq, ps; |
| 923 | }; |
| 924 | |
| 925 | static ESIMD_INLINE ctx make(const void * vx, int nrows, int bpr, int row0) { |
| 926 | const size_t nb = (size_t) nrows * bpr; |
| 927 | ctx c; |
| 928 | xmx_ctx_init(c, nrows, bpr, row0); |
| 929 | c.qs = (const uint8_t *) vx; |
| 930 | c.sc = c.qs + nb * (QK_K / 4); |
| 931 | c.dm = (const sycl::half *) (c.sc + nb * (QK_K / 16)); |
| 932 | c.pq = xmx_pitch(bpr, QK_K / 4); |
| 933 | c.ps = xmx_pitch(bpr, QK_K / 16); |
| 934 | return c; |
| 935 | } |
| 936 | |
| 937 | struct scales_t { |
| 938 | simd<uint8_t, 256> code; // 4 bit scale (low) and min (high) of each 16 wide half |
| 939 | simd<float, 16> d, dmin; |
| 940 | |
| 941 | ESIMD_INLINE simd<uint8_t, 16> byte(int gr) { return code.template select<16, 4>(64 * (gr / 4) + (gr % 4)); } |
| 942 | }; |
| 943 | |
| 944 | static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) { |
| 945 | using namespace sycl::ext::intel::esimd; |
| 946 | scales_t s; |
| 947 | simd<uint32_t, 16> bi = rows * c.bpr + sb; |
| 948 | simd<uint32_t, 64> raw = xmx_load_t<4, SR>(c.sc, c.ps, c.h, sb * 4, c.row0); |
| 949 | s.code = raw.template bit_cast_view<uint8_t>(); |
| 950 | xmx_load_dm(c.dm, bi, s.d, s.dmin); |
| 951 | return s; |
| 952 | } |
| 953 | |
| 954 | struct part_t { |
| 955 | simd<uint32_t, 128> q; |
| 956 | }; |
| 957 | |
| 958 | static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) { |
| 959 | part_t p; |
| 960 | p.q = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 16 + part * 8, c.row0); |
| 961 | return p; |
| 962 | } |
| 963 | |
| 964 | static ESIMD_INLINE simd<uint32_t, 128> unpack(const part_t & p, int g, int /*part*/) { |
| 965 | return (p.q >> (2 * g)) & 0x03030303u; |
| 966 | } |
| 967 | }; |
| 968 | |
| 969 | // Q8_0 reorder layout: [qs: nrows*K bytes] [d: one half per 32 weights] |
| 970 | // the int8 weights feed DPAS as they are, a group is one block with one scale |
| 971 | template <bool SR> struct xmx_traits_q8_0 : xmx_traits_base<SR, 16, xmx_scale_kind::plain> { |
| 972 | struct ctx : xmx_ctx_base { |
| 973 | const uint8_t * qs; |
| 974 | const sycl::half * dh; |
| 975 | unsigned pq; |
| 976 | }; |
| 977 | |
| 978 | static ESIMD_INLINE ctx make(const void * vx, int nrows, int bpr, int row0) { |
| 979 | ctx c; |
| 980 | xmx_ctx_init(c, nrows, bpr, row0); |
| 981 | c.qs = (const uint8_t *) vx; |
| 982 | c.dh = (const sycl::half *) (c.qs + (size_t) nrows * bpr * QK_K); |
| 983 | c.pq = xmx_pitch(bpr, QK_K); |
| 984 | return c; |
| 985 | } |
| 986 | |
| 987 | struct scales_t { |
| 988 | xmx_row_scales<8> d; // scale of each of the 8 blocks of a super-block |
| 989 | }; |
| 990 | |
| 991 | static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) { |
| 992 | using namespace sycl::ext::intel::esimd; |
| 993 | scales_t s; |
| 994 | simd<uint32_t, 16> blk = (rows * c.bpr + sb) * (QK_K / QK8_0); |
| 995 | #pragma unroll |
| 996 | for (int j = 0; j < 8; ++j) { |
| 997 | s.d.v[j] = convert<float>(gather<sycl::half, 16>(c.dh, (blk + j) * (uint32_t) sizeof(sycl::half))); |
| 998 | } |
| 999 | return s; |
| 1000 | } |
| 1001 | |
| 1002 | struct part_t { |
| 1003 | simd<uint32_t, 128> q[4]; |
| 1004 | }; |
| 1005 | |
| 1006 | static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) { |
| 1007 | part_t p; |
| 1008 | #pragma unroll |
| 1009 | for (int g = 0; g < 4; ++g) { |
| 1010 | p.q[g] = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 64 + 8 * (4 * part + g), c.row0); |
| 1011 | } |
| 1012 | return p; |
| 1013 | } |
| 1014 | |
| 1015 | static ESIMD_INLINE simd<uint32_t, 128> unpack(const part_t & p, int g, int /*part*/) { return p.q[g]; } |
| 1016 | }; |
| 1017 | |
| 1018 | template <bool SR> using xmx_traits_q4_k = xmx_traits_q45_k<false, SR>; |
| 1019 | template <bool SR> using xmx_traits_q5_k = xmx_traits_q45_k<true, SR>; |
| 1020 | |
| 1021 | // registers per thread, the large file halves the resident threads but avoids spills in wide tiles, the fused |
| 1022 | // kernel keeps two sets of weights and scales and always takes it |
| 1023 | template <typename T, int NC, bool FUSED> constexpr int xmx_grf() { |
| 1024 | return FUSED || NC >= T::nc_wide ? 256 : 128; |
| 1025 | } |
| 1026 | |
| 1027 | // One DPAS of a block of RC columns: the int32 products of the weights B and the activations A, as float and scaled |
| 1028 | // per weight row. Every quant type sums one or two of these per group of 32. |
| 1029 | template <int RC, int NT> |
| 1030 | ESIMD_INLINE simd<float, RC * NT> xmx_term(const simd<int8_t, 512> & B, |
| 1031 | const simd<int8_t, RC * 32> & A, |
| 1032 | const simd<float, NT> & s) { |
| 1033 | using namespace sycl::ext::intel::esimd; |
| 1034 | namespace xmx = sycl::ext::intel::esimd::xmx; |
| 1035 | |
| 1036 | simd<int, RC * NT> C = xmx::dpas<8, RC, int>(B, A); |
| 1037 | return convert<float>(C) * s.template replicate<RC>(); |
| 1038 | } |
| 1039 | |
| 1040 | // one group of 32 of one weight set, accumulated into Cf for all the columns of the tile |
| 1041 | template <typename T, int NC, typename A2T, typename D8T> |
| 1042 | ESIMD_INLINE void xmx_group(simd<float, NC * XMX_ROWS> & Cf, |
| 1043 | const typename T::part_t & pd, |
| 1044 | typename T::scales_t & sc, |
| 1045 | A2T & A2, |
| 1046 | D8T & d8f, |
| 1047 | simd<int8_t, 512> & Bl, |
| 1048 | simd<int8_t, 512> & Bh, |
| 1049 | const simd<int8_t, 512> & Ol, |
| 1050 | const int g, |
| 1051 | const int part) { |
| 1052 | using namespace sycl::ext::intel::esimd; |
| 1053 | |
| 1054 | constexpr int NT = XMX_ROWS; |
| 1055 | constexpr int RC = NC < 8 ? NC : 8; |
| 1056 | constexpr int NB = NC / RC; |
| 1057 | constexpr bool split = !T::has_min && !T::plain && !T::min16; // two scales per group of 32 |
| 1058 | constexpr bool pair = (RC * 32) % 64 == 0; // a block of RC rows fills whole registers |
| 1059 | |
| 1060 | const int gi = 4 * part + g; |
| 1061 | |
| 1062 | simd<uint32_t, 128> x = T::unpack(pd, g, part); |
| 1063 | simd<int8_t, 512> B = x.template bit_cast_view<int8_t>(); |
| 1064 | if constexpr (split) { |
| 1065 | Bl.template select<256, 1>(0) = B.template select<256, 1>(0); |
| 1066 | Bh.template select<256, 1>(256) = B.template select<256, 1>(256); |
| 1067 | } |
| 1068 | |
| 1069 | simd<float, NT> rd; |
| 1070 | simd<float, NT> rm; |
| 1071 | simd<float, NT> rs0; |
| 1072 | simd<float, NT> rs1; |
| 1073 | |
| 1074 | // Q2_K: scale and min of each 16 wide half are 4 bit codes, the weight is d * scl * q - dmin * mc. scl * q |
| 1075 | // is at most 45, so a dword multiply keeps the bytes apart and the scaled weights go to DPAS as int8. The |
| 1076 | // min term sums a * mc over K, the codes mc take the place of the weights in a second DPAS. |
| 1077 | simd<int8_t, 512> Bs; |
| 1078 | simd<int8_t, 512> Bm; |
| 1079 | if constexpr (T::min16) { |
| 1080 | const simd<uint8_t, NT> c0 = sc.byte(2 * gi); |
| 1081 | const simd<uint8_t, NT> c1 = sc.byte(2 * gi + 1); |
| 1082 | simd<uint32_t, NT> sl0 = convert<uint32_t>(simd<uint8_t, NT>(c0 & (uint8_t) 15)); |
| 1083 | simd<uint32_t, NT> sl1 = convert<uint32_t>(simd<uint8_t, NT>(c1 & (uint8_t) 15)); |
| 1084 | simd<uint32_t, NT> mc0 = convert<uint32_t>(simd<uint8_t, NT>(c0 >> (uint8_t) 4)) * 0x01010101u; |
| 1085 | simd<uint32_t, NT> mc1 = convert<uint32_t>(simd<uint8_t, NT>(c1 >> (uint8_t) 4)) * 0x01010101u; |
| 1086 | simd<uint32_t, 128> ws; |
| 1087 | simd<uint32_t, 128> wm; |
| 1088 | ws.template select<64, 1>(0) = x.template select<64, 1>(0) * sl0.template replicate<4>(); |
| 1089 | ws.template select<64, 1>(64) = x.template select<64, 1>(64) * sl1.template replicate<4>(); |
| 1090 | wm.template select<64, 1>(0) = mc0.template replicate<4>(); |
| 1091 | wm.template select<64, 1>(64) = mc1.template replicate<4>(); |
| 1092 | Bs = ws.template bit_cast_view<int8_t>(); |
| 1093 | Bm = wm.template bit_cast_view<int8_t>(); |
| 1094 | } |
| 1095 | if constexpr (T::plain) { |
| 1096 | rd = sc.d.get(gi); |
| 1097 | } else if constexpr (T::has_min) { |
| 1098 | rd = sc.d.get(gi); |
| 1099 | rm = sc.m.get(gi); |
| 1100 | } else if constexpr (T::min16) { |
| 1101 | // scales are applied through Bs and Bm |
| 1102 | } else { |
| 1103 | rs0 = sc.get(2 * gi); |
| 1104 | rs1 = sc.get(2 * gi + 1); |
| 1105 | } |
| 1106 | |
| 1107 | #pragma unroll |
| 1108 | for (int b = 0; b < NB; ++b) { |
| 1109 | simd<int8_t, RC * 32> A = |
| 1110 | A2[b].template select<RC * 8, 1>(pair ? (g % 2) * RC * 8 : 0).template bit_cast_view<int8_t>(); |
| 1111 | simd<float, RC * NT> d8r = |
| 1112 | d8f[b].template select<RC, RC == 1 ? 1 : 8>(gi).template replicate_vs_w_hs<RC, 1, NT, 0>(0); |
| 1113 | auto Cb = Cf.template select<RC * NT, 1>(b * RC * NT); |
| 1114 | |
| 1115 | simd<float, RC * NT> z; |
| 1116 | if constexpr (T::plain) { |
| 1117 | z = xmx_term<RC, NT>(B, A, rd); |
| 1118 | } else if constexpr (T::has_min) { |
| 1119 | // the second DPAS (all ones against the activations) is the exact integer sum of the activations |
| 1120 | z = xmx_term<RC, NT>(B, A, rd) - xmx_term<RC, NT>(Ol, A, rm); |
| 1121 | } else if constexpr (T::min16) { |
| 1122 | z = xmx_term<RC, NT>(Bs, A, sc.d) - xmx_term<RC, NT>(Bm, A, sc.dmin); |
| 1123 | } else { |
| 1124 | z = xmx_term<RC, NT>(Bl, A, rs0) + xmx_term<RC, NT>(Bh, A, rs1); |
| 1125 | } |
| 1126 | Cb += z * d8r; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | // activation of the GLU applied to the gate, uniform across the launch |
| 1131 | template <int N> ESIMD_INLINE simd<float, N> xmx_glu_act(const simd<float, N> & gate, const int glu_op) { |
| 1132 | using namespace sycl::ext::intel::esimd; |
| 1133 | if (glu_op == GGML_GLU_OP_SWIGLU) { |
| 1134 | return gate / (1.0f + exp(-gate)); |
| 1135 | } |
| 1136 | // GEGLU, the tanh is written with exp |
| 1137 | simd<float, N> a = 0.79788456f * gate * (1.0f + 0.044715f * gate * gate); |
| 1138 | simd<float, N> t = 1.0f - 2.0f / (exp(2.0f * a) + 1.0f); |
| 1139 | return 0.5f * gate * (1.0f + t); |
| 1140 | } |
| 1141 | |
| 1142 | // activations come in through 2D block loads, one for the (d, s) pairs of a super-block (s is unused) and one per two |
| 1143 | // groups for the quants, the DPAS A operand needs no repacking. Groups with two scales (16 wide) mask the |
| 1144 | // weights instead, zeros in B drop the other half of K. |
| 1145 | // KS is the most threads that split K for one tile, the launch may use fewer. |
| 1146 | // FUSED computes up * act(gate) from the weights vx (up) and vg (gate), reading the activations once. |
| 1147 | template <typename T, int NC, int KS, bool FUSED> |
| 1148 | ESIMD_INLINE void xmx_mul_mat(const void * vx, |
| 1149 | const void * vg, |
| 1150 | const char * vy, |
| 1151 | float * dst, |
| 1152 | const int ncols, |
| 1153 | const int nrows, |
| 1154 | const int ncv, |
| 1155 | const int stride_y_bytes, |
| 1156 | const int ldd, |
| 1157 | const int glu_op, |
| 1158 | const int tile, |
| 1159 | const int lid, |
| 1160 | const int ks) { |
| 1161 | using namespace sycl::ext::intel::esimd; |
| 1162 | |
| 1163 | constexpr int NT = XMX_ROWS; |
| 1164 | constexpr int RC = NC < 8 ? NC : 8; |
| 1165 | constexpr int NB = NC / RC; |
| 1166 | constexpr int N_ACC = FUSED ? 2 : 1; |
| 1167 | constexpr bool pair = (RC * 32) % 64 == 0; // a block of RC rows fills whole registers |
| 1168 | |
| 1169 | if constexpr (KS > 1) { |
| 1170 | slm_init<N_ACC * KS * NC * NT * sizeof(float)>(); |
| 1171 | } |
| 1172 | |
| 1173 | const int bpr = ncols / QK_K; |
| 1174 | const int row0 = tile * NT; |
| 1175 | const auto c = T::make(vx, nrows, bpr, row0); |
| 1176 | const auto cg = T::make(FUSED ? vg : vx, nrows, bpr, row0); |
| 1177 | |
| 1178 | simd<uint32_t, NT> rows; |
| 1179 | #pragma unroll |
| 1180 | for (int n = 0; n < NT; ++n) { |
| 1181 | rows[n] = row0 + n < nrows ? row0 + n : nrows - 1; |
| 1182 | } |
| 1183 | |
| 1184 | const uint32_t * yq = (const uint32_t *) vy; |
| 1185 | const uint32_t * yd = (const uint32_t *) (vy + ncols); |
| 1186 | const unsigned yqw = ncols - 1; |
| 1187 | const unsigned ydw = ncols / QK8_1 * sizeof(sycl::half2) - 1; |
| 1188 | const unsigned yh = ncv - 1; |
| 1189 | const unsigned yp = stride_y_bytes - 1; |
| 1190 | |
| 1191 | simd<float, NC * NT> Cf = 0.0f; |
| 1192 | simd<float, NC * NT> Cfg = 0.0f; // gate, only used when fused |
| 1193 | simd<int8_t, 512> Bl = int8_t(0); |
| 1194 | simd<int8_t, 512> Bh = int8_t(0); |
| 1195 | simd<int8_t, 512> Ol = int8_t(0); |
| 1196 | if constexpr (T::has_min) { |
| 1197 | Ol = int8_t(1); |
| 1198 | } |
| 1199 | |
| 1200 | for (int sb = lid; sb < bpr; sb += ks) { |
| 1201 | auto sc = T::load_scales(c, sb, rows); |
| 1202 | typename T::scales_t scg; |
| 1203 | if constexpr (FUSED) { |
| 1204 | scg = T::load_scales(cg, sb, rows); |
| 1205 | } |
| 1206 | |
| 1207 | // d of the 8 groups of the super-block, [column * 8 + group] |
| 1208 | simd<float, 8 * RC> d8f[NB]; |
| 1209 | #pragma unroll |
| 1210 | for (int b = 0; b < NB; ++b) { |
| 1211 | simd<uint32_t, 8 * RC> ds = load_2d<uint32_t, 8, RC, 1, false, false>(yd, ydw, yh, yp, sb * 8, b * RC); |
| 1212 | simd<sycl::half, 16 * RC> dh = ds.template bit_cast_view<sycl::half>(); |
| 1213 | d8f[b] = convert<float>(simd<sycl::half, 8 * RC>(dh.template select<8 * RC, 2>(0))); |
| 1214 | } |
| 1215 | |
| 1216 | #pragma unroll |
| 1217 | for (int part = 0; part < 2; ++part) { |
| 1218 | const auto pd = T::load_part(c, sb, part); |
| 1219 | typename T::part_t pdg; |
| 1220 | if constexpr (FUSED) { |
| 1221 | pdg = T::load_part(cg, sb, part); |
| 1222 | } |
| 1223 | |
| 1224 | simd<uint32_t, 16 * RC> A2[NB]; // quants of two groups |
| 1225 | |
| 1226 | #pragma unroll |
| 1227 | for (int g = 0; g < 4; ++g) { |
| 1228 | const int gi = 4 * part + g; |
| 1229 | if constexpr (pair) { |
| 1230 | if (g % 2 == 0) { |
| 1231 | #pragma unroll |
| 1232 | for (int b = 0; b < NB; ++b) { |
| 1233 | A2[b] = |
| 1234 | load_2d<uint32_t, 8, RC, 2, false, false>(yq, yqw, yh, yp, sb * 64 + gi * 8, b * RC); |
| 1235 | } |
| 1236 | } |
| 1237 | } else { |
| 1238 | #pragma unroll |
| 1239 | for (int b = 0; b < NB; ++b) { |
| 1240 | A2[b].template select<RC * 8, 1>(0) = |
| 1241 | load_2d<uint32_t, 8, RC, 1, false, false>(yq, yqw, yh, yp, sb * 64 + gi * 8, b * RC); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | xmx_group<T, NC>(Cf, pd, sc, A2, d8f, Bl, Bh, Ol, g, part); |
| 1246 | if constexpr (FUSED) { |
| 1247 | xmx_group<T, NC>(Cfg, pdg, scg, A2, d8f, Bl, Bh, Ol, g, part); |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | if constexpr (KS > 1) { |
| 1254 | slm_block_store<float, NC * NT>(lid * NC * NT * sizeof(float), Cf); |
| 1255 | if constexpr (FUSED) { |
| 1256 | slm_block_store<float, NC * NT>((KS + lid) * NC * NT * sizeof(float), Cfg); |
| 1257 | } |
| 1258 | barrier(); |
| 1259 | if (lid != 0) { |
| 1260 | return; |
| 1261 | } |
| 1262 | Cf = 0.0f; |
| 1263 | Cfg = 0.0f; |
| 1264 | #pragma unroll 4 |
| 1265 | for (int j = 0; j < ks; ++j) { |
| 1266 | Cf += slm_block_load<float, NC * NT>(j * NC * NT * sizeof(float)); |
| 1267 | if constexpr (FUSED) { |
| 1268 | Cfg += slm_block_load<float, NC * NT>((KS + j) * NC * NT * sizeof(float)); |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | if constexpr (FUSED) { |
| 1274 | Cf *= xmx_glu_act<NC * NT>(Cfg, glu_op); |
| 1275 | } |
| 1276 | |
| 1277 | if (row0 + NT <= nrows && ldd % 4 == 0 && (uintptr_t) dst % 16 == 0) { |
| 1278 | #pragma unroll |
| 1279 | for (int m = 0; m < NC; ++m) { |
| 1280 | if (NC <= 8 || m < ncv) { |
| 1281 | block_store<float, NT>(dst + (size_t) m * ldd + row0, Cf.template select<NT, 1>(m * NT)); |
| 1282 | } |
| 1283 | } |
| 1284 | } else { |
| 1285 | // tail tile or unaligned output |
| 1286 | simd<uint32_t, NT> lane(0, 1); |
| 1287 | simd_mask<NT> in_rows = (lane + row0) < (uint32_t) nrows; |
| 1288 | #pragma unroll |
| 1289 | for (int m = 0; m < NC; ++m) { |
| 1290 | if (NC <= 8 || m < ncv) { |
| 1291 | scatter<float, NT>(dst + (size_t) m * ldd + row0, lane * (uint32_t) sizeof(float), |
| 1292 | Cf.template select<NT, 1>(m * NT), in_rows); |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | // KS_MAX bounds the threads that split K for one tile, the launch uses as few as keep every thread to the same |
| 1299 | // number of super-blocks as the busiest one |
| 1300 | template <typename T, int NC, int KS_MAX, bool FUSED> |
| 1301 | static void xmx_mul_mat_launch_ks(const void * vx, |
| 1302 | const void * vg, |
| 1303 | const char * vy, |
| 1304 | float * dst, |
| 1305 | const int ncols, |
| 1306 | const int nrows, |
| 1307 | const int ncv, |
| 1308 | const int stride_y_bytes, |
| 1309 | const int ldd, |
| 1310 | const int glu_op, |
| 1311 | dpct::queue_ptr stream) { |
| 1312 | GGML_ASSERT(ncols % QK_K == 0); |
| 1313 | const size_t n_tiles = (nrows + XMX_ROWS - 1) / XMX_ROWS; |
| 1314 | const int bpr = ncols / QK_K; |
| 1315 | const int rounds = (bpr + KS_MAX - 1) / KS_MAX; |
| 1316 | const int ks = (bpr + rounds - 1) / rounds; |
| 1317 | stream->submit([&](sycl::handler & cgh) { |
| 1318 | cgh.parallel_for(sycl::nd_range<1>(sycl::range<1>(n_tiles * ks), sycl::range<1>(ks)), |
| 1319 | sycl::ext::oneapi::experimental::properties{ |
| 1320 | sycl::ext::intel::experimental::grf_size<xmx_grf<T, NC, FUSED>()> }, |
| 1321 | [=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] { |
| 1322 | xmx_mul_mat<T, NC, KS_MAX, FUSED>(vx, vg, vy, dst, ncols, nrows, ncv, stride_y_bytes, ldd, |
| 1323 | glu_op, (int) it.get_group(0), (int) it.get_local_id(0), |
| 1324 | ks); |
| 1325 | }); |
| 1326 | }); |
| 1327 | } |
| 1328 | |
| 1329 | template <typename T, int NC, bool FUSED> |
| 1330 | static void xmx_mul_mat_launch(const void * vx, |
| 1331 | const void * vg, |
| 1332 | const char * vy, |
| 1333 | float * dst, |
| 1334 | const int ncols, |
| 1335 | const int nrows, |
| 1336 | const int ncv, |
| 1337 | const int stride_y_bytes, |
| 1338 | const int ldd, |
| 1339 | const int glu_op, |
| 1340 | dpct::queue_ptr stream) { |
| 1341 | // the fused kernel keeps two sets of accumulators in shared memory, half the threads for the same size |
| 1342 | constexpr int ks_max = xmx_n_split<NC>() / (FUSED ? 2 : 1); |
| 1343 | xmx_mul_mat_launch_ks<T, NC, ks_max, FUSED>(vx, vg, vy, dst, ncols, nrows, ncv, stride_y_bytes, ldd, glu_op, |
| 1344 | stream); |
| 1345 | } |
| 1346 | |
| 1347 | // FUSED: vx and vg are the up and gate weights, dst gets up * act(gate) with glu_op a ggml_glu_op |
| 1348 | template <typename T, bool FUSED = false> |
| 1349 | static void xmx_mul_mat_ncols(const void * vx, |
| 1350 | const void * vg, |
| 1351 | const char * vy, |
| 1352 | float * dst, |
| 1353 | const int ncols, |
| 1354 | const int nrows, |
| 1355 | const int ncols_dst, |
| 1356 | const int stride_y_bytes, |
| 1357 | const int ldd, |
| 1358 | const int glu_op, |
| 1359 | dpct::queue_ptr stream) { |
| 1360 | constexpr int chunk_max = FUSED ? XMX_FUSED_CHUNK : XMX_CHUNK; |
| 1361 | for (int c0 = 0; c0 < ncols_dst; c0 += chunk_max) { |
| 1362 | const int n = std::min(chunk_max, ncols_dst - c0); |
| 1363 | const char * y = vy + (size_t) c0 * stride_y_bytes; |
| 1364 | float * d = dst + (size_t) c0 * ldd; |
| 1365 | const int nc = n > 32 ? 64 : (n > 16 ? 32 : (n > 8 ? 16 : n)); |
| 1366 | switch (nc) { |
| 1367 | #define XMX_CASE(N) \ |
| 1368 | case N: \ |
| 1369 | xmx_mul_mat_launch<T, N, FUSED>(vx, vg, y, d, ncols, nrows, n, stride_y_bytes, ldd, glu_op, stream); \ |
| 1370 | break |
| 1371 | XMX_CASE(1); |
| 1372 | XMX_CASE(2); |
| 1373 | XMX_CASE(3); |
| 1374 | XMX_CASE(4); |
| 1375 | XMX_CASE(5); |
| 1376 | XMX_CASE(6); |
| 1377 | XMX_CASE(7); |
| 1378 | XMX_CASE(8); |
| 1379 | #undef XMX_CASE |
| 1380 | // short row variants are for tests, not built for the wide tiles |
| 1381 | #define XMX_CASE_WIDE(N) \ |
| 1382 | case N: \ |
| 1383 | if constexpr (!T::short_rows && (!FUSED || N <= XMX_FUSED_CHUNK)) { \ |
| 1384 | xmx_mul_mat_launch<T, N, FUSED>(vx, vg, y, d, ncols, nrows, n, stride_y_bytes, ldd, glu_op, stream); \ |
| 1385 | } \ |
| 1386 | break |
| 1387 | XMX_CASE_WIDE(16); |
| 1388 | XMX_CASE_WIDE(32); |
| 1389 | XMX_CASE_WIDE(64); |
| 1390 | #undef XMX_CASE_WIDE |
| 1391 | default: |
| 1392 | GGML_ABORT("unsupported ncols_dst=%d for XMX MMV", n); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | } // namespace ggml_sycl_esimd |
| 1398 | |
| 1399 | #endif // GGML_SYCL_ESIMD_HPP |