ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / vecdotq.hpp
| 1 | // |
| 2 | // MIT license |
| 3 | // Copyright (C) 2025 Intel Corporation |
| 4 | // SPDX-License-Identifier: MIT |
| 5 | // |
| 6 | |
| 7 | // |
| 8 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 9 | // See https://llvm.org/LICENSE.txt for license information. |
| 10 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 11 | // |
| 12 | |
| 13 | #ifndef GGML_SYCL_VECDOTQ_HPP |
| 14 | #define GGML_SYCL_VECDOTQ_HPP |
| 15 | |
| 16 | #include "dpct/helper.hpp" |
| 17 | #include "ggml.h" |
| 18 | #include "type.hpp" |
| 19 | #include "quants.hpp" |
| 20 | |
| 21 | typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, |
| 22 | const int & iqs); |
| 23 | |
| 24 | static __dpct_inline__ int get_int_b1(const void * x, const int & i32) { |
| 25 | const uint8_t * x8 = (const uint8_t *) x; |
| 26 | |
| 27 | int x32 = x8[4*i32 + 0] << 0; |
| 28 | x32 |= x8[4*i32 + 1] << 8; |
| 29 | x32 |= x8[4*i32 + 2] << 16; |
| 30 | x32 |= x8[4*i32 + 3] << 24; |
| 31 | |
| 32 | return x32; |
| 33 | } |
| 34 | |
| 35 | static __dpct_inline__ int get_int_b2(const void * x, const int & i32) { |
| 36 | const uint16_t * x16 = (const uint16_t *) x; // assume at least 2 byte alignment |
| 37 | |
| 38 | int x32 = x16[2*i32 + 0] << 0; |
| 39 | x32 |= x16[2*i32 + 1] << 16; |
| 40 | |
| 41 | return x32; |
| 42 | } |
| 43 | |
| 44 | static __dpct_inline__ int get_int_b4(const void * x, const int & i32) { |
| 45 | return ((const int *) x)[i32]; // assume at least 4 byte alignment |
| 46 | } |
| 47 | |
| 48 | static __dpct_inline__ int get_int_from_int8(const int8_t* x8, const int& i32) { |
| 49 | const uint16_t* x16 = |
| 50 | (const uint16_t*)(x8 + sizeof(int) * i32); // assume at least 2 byte |
| 51 | // alignment |
| 52 | |
| 53 | int x32 = 0; |
| 54 | x32 |= x16[0] << 0; |
| 55 | x32 |= x16[1] << 16; |
| 56 | |
| 57 | return x32; |
| 58 | } |
| 59 | |
| 60 | static __dpct_inline__ int get_int_from_uint8( |
| 61 | const uint8_t* x8, |
| 62 | const int& i32) { |
| 63 | const uint16_t* x16 = |
| 64 | (const uint16_t*)(x8 + sizeof(int) * i32); // assume at least 2 byte |
| 65 | // alignment |
| 66 | |
| 67 | int x32 = 0; |
| 68 | x32 |= x16[0] << 0; |
| 69 | x32 |= x16[1] << 16; |
| 70 | |
| 71 | return x32; |
| 72 | } |
| 73 | |
| 74 | static __dpct_inline__ int get_int_from_int8_aligned( |
| 75 | const int8_t* x8, |
| 76 | const int& i32) { |
| 77 | return *( |
| 78 | (const int*)(x8 + sizeof(int) * i32)); // assume at least 4 byte alignment |
| 79 | } |
| 80 | |
| 81 | static __dpct_inline__ int get_int_from_uint8_aligned( |
| 82 | const uint8_t* x8, |
| 83 | const int& i32) { |
| 84 | return *( |
| 85 | (const int*)(x8 + sizeof(int) * i32)); // assume at least 4 byte alignment |
| 86 | } |
| 87 | |
| 88 | static __dpct_inline__ int byte_sub_4(const int a, const int b) { |
| 89 | const uint32_t ua = static_cast<uint32_t>(a); |
| 90 | const uint32_t ub = static_cast<uint32_t>(b); |
| 91 | return static_cast<int>(((ua | 0x80808080u) - ub) ^ 0x80808080u); |
| 92 | } |
| 93 | |
| 94 | static __dpct_inline__ float vec_dot_q6_K_q8_1_impl_mmvq_scalar( |
| 95 | const int vl, const int vh, const int u0, const int u1, const int8_t sc0, |
| 96 | const int8_t sc1, const float d, const float d80, const float d81) { |
| 97 | static_assert(QR6_K == 2, "q6_K MMVQ scalar fast path assumes QR6_K == 2"); |
| 98 | |
| 99 | const int vil0 = (vl >> 0) & 0x0F0F0F0F; |
| 100 | const int vih0 = ((vh >> 0) << 4) & 0x30303030; |
| 101 | const int vi0 = byte_sub_4(vil0 | vih0, 0x20202020); |
| 102 | |
| 103 | const int vil1 = (vl >> 4) & 0x0F0F0F0F; |
| 104 | const int vih1 = ((vh >> 4) << 4) & 0x30303030; |
| 105 | const int vi1 = byte_sub_4(vil1 | vih1, 0x20202020); |
| 106 | |
| 107 | const float sumf = |
| 108 | d80 * (dpct::dp4a(vi0, u0, 0) * sc0) + |
| 109 | d81 * (dpct::dp4a(vi1, u1, 0) * sc1); |
| 110 | |
| 111 | return d * sumf; |
| 112 | } |
| 113 | |
| 114 | static __dpct_inline__ void get_int_from_table_16(const uint32_t &q4, |
| 115 | const uint8_t *values, |
| 116 | int &val1, int &val2) { |
| 117 | |
| 118 | uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32; |
| 119 | aux32 = q4 & 0x0f0f0f0f; |
| 120 | uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8); |
| 121 | uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8); |
| 122 | val1 = v1 | (v2 << 16); |
| 123 | aux32 = (q4 >> 4) & 0x0f0f0f0f; |
| 124 | v1 = values[q8[0]] | (values[q8[1]] << 8); |
| 125 | v2 = values[q8[2]] | (values[q8[3]] << 8); |
| 126 | val2 = v1 | (v2 << 16); |
| 127 | } |
| 128 | |
| 129 | static __dpct_inline__ sycl::int2 get_int_from_table_16( |
| 130 | const int& q4, const int8_t* table) { |
| 131 | const uint32_t* table32 = (const uint32_t*)table; |
| 132 | uint32_t tmp[2]; |
| 133 | const uint32_t low_high_selection_indices = |
| 134 | (0x32103210 | ((q4 & 0x88888888) >> 1)); |
| 135 | #pragma unroll |
| 136 | for (uint32_t i = 0; i < 2; ++i) { |
| 137 | const uint32_t shift = 16 * i; |
| 138 | |
| 139 | const uint32_t low = |
| 140 | dpct::byte_level_permute(table32[0], table32[1], q4 >> shift); |
| 141 | const uint32_t high = |
| 142 | dpct::byte_level_permute(table32[2], table32[3], q4 >> shift); |
| 143 | tmp[i] = dpct::byte_level_permute( |
| 144 | low, high, low_high_selection_indices >> shift); |
| 145 | } |
| 146 | return sycl::int2( |
| 147 | dpct::byte_level_permute(tmp[0], tmp[1], 0x6420), |
| 148 | dpct::byte_level_permute(tmp[0], tmp[1], 0x7531)); |
| 149 | } |
| 150 | |
| 151 | // Four E2M1 codes (one per byte, bits 0..3) to their kvalues_mxfp4 int8 values. SWAR arithmetic |
| 152 | // replaces get_int_from_table_16 for MXFP4: dpct::byte_level_permute is emulated with 64-bit shifts, |
| 153 | // eight per int, which made the MXFP4 GEMV compute-bound on Intel GPUs. |
| 154 | // Magnitudes 0,1,2,3,4,6,8,12 = m + [m>=5] + [m>=6] + 3*[m>=7]; each byte stays below 256, so the |
| 155 | // byte-wise adds never carry. -0 (code 8) is left as 0 so the two's-complement +1 cannot carry either. |
| 156 | static __dpct_inline__ int mxfp4_codes_to_int8(const uint32_t x) { |
| 157 | const uint32_t m = x & 0x07070707u; |
| 158 | const uint32_t ge5 = ((m + 0x03030303u) >> 3) & 0x01010101u; |
| 159 | const uint32_t ge6 = ((m + 0x02020202u) >> 3) & 0x01010101u; |
| 160 | const uint32_t ge7 = ((m + 0x01010101u) >> 3) & 0x01010101u; |
| 161 | const uint32_t mag = m + ge5 + ge6 + 3u * ge7; |
| 162 | const uint32_t nz = ((mag + 0x7f7f7f7fu) >> 7) & 0x01010101u; |
| 163 | const uint32_t neg = (x >> 3) & nz & 0x01010101u; |
| 164 | return (int) ((mag ^ (neg * 0xffu)) + neg); |
| 165 | } |
| 166 | |
| 167 | // Same result as get_int_from_table_16(q4, kvalues_mxfp4): x = low nibbles, y = high nibbles. |
| 168 | static __dpct_inline__ sycl::int2 get_int_from_mxfp4(const int q4) { |
| 169 | return sycl::int2(mxfp4_codes_to_int8((uint32_t) q4 & 0x0f0f0f0fu), |
| 170 | mxfp4_codes_to_int8(((uint32_t) q4 >> 4) & 0x0f0f0f0fu)); |
| 171 | } |
| 172 | |
| 173 | #define VDR_Q2_K_Q8_1_MMVQ 1 |
| 174 | |
| 175 | // contiguous v/x values |
| 176 | static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq( |
| 177 | const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales, |
| 178 | const sycl::half2 &dm2, const float *__restrict__ d8) { |
| 179 | |
| 180 | float sumf_d = 0.0f; |
| 181 | float sumf_m = 0.0f; |
| 182 | |
| 183 | #pragma unroll |
| 184 | for (int i = 0; i < QR2_K; ++i) { |
| 185 | const int sc = scales[2*i]; |
| 186 | |
| 187 | const int vi = (v >> (2*i)) & 0x03030303; |
| 188 | |
| 189 | sumf_d += |
| 190 | d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product |
| 191 | |
| 192 | // fill int with 4x m |
| 193 | int m = sc >> 4; |
| 194 | m |= m << 8; |
| 195 | m |= m << 16; |
| 196 | sumf_m += d8[i] * |
| 197 | dpct::dp4a( |
| 198 | m, u[i], |
| 199 | 0); // multiply constant q2_K part with sum of q8_1 values |
| 200 | } |
| 201 | |
| 202 | const sycl::float2 dm2f = |
| 203 | dm2.convert<float, sycl::rounding_mode::automatic>(); |
| 204 | |
| 205 | return dm2f.x() * sumf_d - dm2f.y() * sumf_m; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | #define VDR_Q3_K_Q8_1_MMVQ 1 |
| 210 | |
| 211 | // contiguous v/x values |
| 212 | static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq( |
| 213 | const int &vl, const int &vh, const int *__restrict__ u, |
| 214 | const uint8_t *__restrict__ scales, const int &scale_offset, |
| 215 | const float &d3, const float *__restrict__ d8) { |
| 216 | |
| 217 | float sumf = 0.0f; |
| 218 | |
| 219 | #pragma unroll |
| 220 | for (int i = 0; i < QR3_K; ++i) { |
| 221 | const int isc = scale_offset + 2*i; |
| 222 | |
| 223 | const int isc_low = isc % (QK_K/32); |
| 224 | const int sc_shift_low = 4 * (isc / (QK_K/32)); |
| 225 | const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF; |
| 226 | |
| 227 | const int isc_high = isc % (QK_K/64); |
| 228 | const int sc_shift_high = 2 * (isc / (QK_K/64)); |
| 229 | const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4; |
| 230 | |
| 231 | const int sc = (sc_low | sc_high) - 32; |
| 232 | |
| 233 | const int vil = (vl >> (2*i)) & 0x03030303; |
| 234 | |
| 235 | const int vih = ((vh >> i) << 2) & 0x04040404; |
| 236 | |
| 237 | const int vi = |
| 238 | dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat()); |
| 239 | |
| 240 | sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product |
| 241 | } |
| 242 | |
| 243 | return d3 * sumf; |
| 244 | } |
| 245 | |
| 246 | #define VDR_Q4_K_Q8_1_MMVQ 2 |
| 247 | |
| 248 | // contiguous v/x values |
| 249 | static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq( |
| 250 | const int *__restrict__ v, const int *__restrict__ u, |
| 251 | const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m, |
| 252 | const sycl::half2 &dm4, const float *__restrict__ d8) { |
| 253 | |
| 254 | float sumf_d = 0.0f; |
| 255 | float sumf_m = 0.0f; |
| 256 | |
| 257 | #pragma unroll |
| 258 | for (int i = 0; i < QR4_K; ++i) { |
| 259 | const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F; |
| 260 | const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F; |
| 261 | |
| 262 | const int dot1 = |
| 263 | dpct::dp4a(v1i, u[2 * i + 1], |
| 264 | dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product |
| 265 | const int dot2 = |
| 266 | dpct::dp4a(0x01010101, u[2 * i + 1], |
| 267 | dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u |
| 268 | |
| 269 | sumf_d += d8[i] * (dot1 * sc[i]); |
| 270 | sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values |
| 271 | } |
| 272 | |
| 273 | const sycl::float2 dm4f = |
| 274 | dm4.convert<float, sycl::rounding_mode::automatic>(); |
| 275 | |
| 276 | return dm4f.x() * sumf_d - dm4f.y() * sumf_m; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | #define VDR_Q5_K_Q8_1_MMVQ 2 |
| 281 | |
| 282 | // contiguous v/x values |
| 283 | static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq( |
| 284 | const int *__restrict__ vl, const int *__restrict__ vh, |
| 285 | const int *__restrict__ u, const uint8_t *__restrict__ sc, |
| 286 | const uint8_t *__restrict__ m, const sycl::half2 &dm5, |
| 287 | const float *__restrict__ d8) { |
| 288 | |
| 289 | float sumf_d = 0.0f; |
| 290 | float sumf_m = 0.0f; |
| 291 | |
| 292 | #pragma unroll |
| 293 | for (int i = 0; i < QR5_K; ++i) { |
| 294 | const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F; |
| 295 | const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F; |
| 296 | |
| 297 | const int vh0i = ((vh[0] >> i) << 4) & 0x10101010; |
| 298 | const int vh1i = ((vh[1] >> i) << 4) & 0x10101010; |
| 299 | |
| 300 | const int v0i = vl0i | vh0i; |
| 301 | const int v1i = vl1i | vh1i; |
| 302 | |
| 303 | const int dot1 = |
| 304 | dpct::dp4a(v0i, u[2 * i + 0], |
| 305 | dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product |
| 306 | const int dot2 = |
| 307 | dpct::dp4a(0x01010101, u[2 * i + 0], |
| 308 | dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u |
| 309 | |
| 310 | sumf_d += d8[i] * (dot1 * sc[i]); |
| 311 | sumf_m += d8[i] * (dot2 * m[i]); |
| 312 | |
| 313 | } |
| 314 | |
| 315 | const sycl::float2 dm5f = |
| 316 | dm5.convert<float, sycl::rounding_mode::automatic>(); |
| 317 | |
| 318 | return dm5f.x() * sumf_d - dm5f.y() * sumf_m; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | #define VDR_Q6_K_Q8_1_MMVQ 1 |
| 323 | |
| 324 | // contiguous v/x values |
| 325 | static __dpct_inline__ float |
| 326 | vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh, |
| 327 | const int *__restrict__ u, |
| 328 | const int8_t *__restrict__ scales, const float &d, |
| 329 | const float *__restrict__ d8) { |
| 330 | return vec_dot_q6_K_q8_1_impl_mmvq_scalar( |
| 331 | vl, vh, u[0], u[1], scales[0], scales[4], d, d8[0], d8[1]); |
| 332 | } |
| 333 | |
| 334 | #define VDR_Q1_0_Q8_1_MMVQ 1 |
| 335 | #define VDR_Q1_0_Q8_1_MMQ 4 |
| 336 | |
| 337 | static __dpct_inline__ float |
| 338 | vec_dot_q1_0_q8_1(const void *__restrict__ vbq, |
| 339 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 340 | |
| 341 | const block_q1_0 * bq1_0 = (const block_q1_0 *) vbq; |
| 342 | |
| 343 | const block_q8_1 * bq8_1_chunk = bq8_1 + iqs; |
| 344 | const float d1 = bq1_0->d; |
| 345 | const int v = get_int_from_uint8_aligned(bq1_0->qs, iqs); |
| 346 | |
| 347 | int vi_bytes[8]; |
| 348 | #pragma unroll |
| 349 | for (int j = 0; j < 8; ++j) { |
| 350 | const int shift = j * 4; |
| 351 | const int bits4 = (v >> shift) & 0x0F; |
| 352 | const int b0 = (bits4 & 0x01) ? 1 : -1; |
| 353 | const int b1 = (bits4 & 0x02) ? 1 : -1; |
| 354 | const int b2 = (bits4 & 0x04) ? 1 : -1; |
| 355 | const int b3 = (bits4 & 0x08) ? 1 : -1; |
| 356 | vi_bytes[j] = (b0 & 0xFF) | ((b1 & 0xFF) << 8) | ((b2 & 0xFF) << 16) | ((b3 & 0xFF) << 24); |
| 357 | } |
| 358 | |
| 359 | int sumi = 0; |
| 360 | #pragma unroll |
| 361 | for (int j = 0; j < 8; ++j) { |
| 362 | const int u = get_int_from_int8_aligned(bq8_1_chunk->qs, j); |
| 363 | sumi = ggml_sycl_dp4a(vi_bytes[j], u, sumi); |
| 364 | } |
| 365 | |
| 366 | return d1 * bq8_1_chunk->ds[0] * sumi; |
| 367 | } |
| 368 | |
| 369 | // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called |
| 370 | // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q |
| 371 | |
| 372 | template <ggml_type T> struct reorder_vec_dot_q_sycl { |
| 373 | static_assert(T != T, "ggml_type for reorder vecdot not implemented"); |
| 374 | }; |
| 375 | |
| 376 | // For some types the weight side of the dot product does not depend on the destination column, so a |
| 377 | // multi-column mul_mat_vec can unpack it once per block instead of once per column. Such a type adds |
| 378 | // load() and dot() next to operator() and opts in here. See reorder_vec_dot_q_sycl<GGML_TYPE_Q4_K>. |
| 379 | template <ggml_type T> struct reorder_vec_dot_shared_weights { |
| 380 | static constexpr bool value = false; |
| 381 | }; |
| 382 | |
| 383 | template <> struct reorder_vec_dot_shared_weights<GGML_TYPE_Q4_K> { |
| 384 | static constexpr bool value = true; |
| 385 | }; |
| 386 | |
| 387 | template <> struct reorder_vec_dot_shared_weights<GGML_TYPE_Q5_K> { |
| 388 | static constexpr bool value = true; |
| 389 | }; |
| 390 | |
| 391 | template <ggml_type T> struct reorder_vec_dot_shared_activations { |
| 392 | static constexpr bool value = false; |
| 393 | }; |
| 394 | |
| 395 | template <> struct reorder_vec_dot_shared_activations<GGML_TYPE_Q4_K> { |
| 396 | static constexpr bool value = true; |
| 397 | }; |
| 398 | |
| 399 | template <> struct reorder_vec_dot_shared_activations<GGML_TYPE_Q5_K> { |
| 400 | static constexpr bool value = true; |
| 401 | }; |
| 402 | |
| 403 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_Q4_0> { |
| 404 | static constexpr ggml_type gtype = GGML_TYPE_Q4_0; |
| 405 | |
| 406 | using q4_0_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_Q4_0>; |
| 407 | using q4_0_traits = typename q4_0_block::traits; |
| 408 | |
| 409 | __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int * v, const int * u, const float & d4, const sycl::half2 & ds8) { |
| 410 | int sumi = 0; |
| 411 | |
| 412 | #pragma unroll |
| 413 | for (size_t i = 0; i < q4_0_traits::vdr_mmvq; ++i) { |
| 414 | const int vi0 = (v[i] >> 0) & 0x0F0F0F0F; |
| 415 | const int vi1 = (v[i] >> 4) & 0x0F0F0F0F; |
| 416 | |
| 417 | // SIMD dot product of quantized values |
| 418 | sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi); |
| 419 | sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi); |
| 420 | } |
| 421 | |
| 422 | const sycl::float2 ds8f = ds8.convert<float, sycl::rounding_mode::automatic>(); |
| 423 | |
| 424 | // second part effectively subtracts 8 from each quant value |
| 425 | return d4 * (sumi * ds8f.x() - (8 * q4_0_traits::vdr_mmvq / q4_0_traits::qi) * ds8f.y()); |
| 426 | } |
| 427 | |
| 428 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 429 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 430 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 431 | const uint8_t * bq4_0 = static_cast<const uint8_t *>(vbq) + ibx_offset.first; |
| 432 | const ggml_half d = *(reinterpret_cast<const ggml_half *>(static_cast<const uint8_t *>(vbq) + d_offset.first)); |
| 433 | int v[q4_0_traits::vdr_mmvq]; |
| 434 | int u[2 * q4_0_traits::vdr_mmvq]; |
| 435 | |
| 436 | |
| 437 | #pragma unroll |
| 438 | for (size_t i = 0; i < q4_0_traits::vdr_mmvq; ++i) { |
| 439 | v[i] = get_int_from_uint8(bq4_0, iqs + i); |
| 440 | u[2 * i + 0] = get_int_from_int8_aligned(q8_1_quant_ptr, iqs + i); |
| 441 | u[2 * i + 1] = get_int_from_int8_aligned(q8_1_quant_ptr, iqs + i + q4_0_traits::qi); |
| 442 | } |
| 443 | |
| 444 | return vec_dot_q4_0_q8_1_impl(v, u, d, *q8_1_ds); |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | // Load four contiguous dwords per operand instead of loading each value separately. |
| 449 | struct reorder_vec_dot_q8_0_wide { |
| 450 | static constexpr ggml_type gtype = GGML_TYPE_Q8_0; |
| 451 | |
| 452 | using q8_0_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_Q8_0>; |
| 453 | using q8_0_traits = typename q8_0_block::traits; |
| 454 | |
| 455 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 456 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 457 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 458 | static_assert(q8_0_traits::vdr_mmvq == 4, "the wide load moves exactly four dwords"); |
| 459 | |
| 460 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 461 | const int8_t * qs = reinterpret_cast<const int8_t *>(base + ibx_offset.first); |
| 462 | const ggml_half d = *reinterpret_cast<const ggml_half *>(base + d_offset.first); |
| 463 | |
| 464 | const sycl::int4 v = *reinterpret_cast<const sycl::int4 *>(qs + sizeof(int) * iqs); |
| 465 | const sycl::int4 u = *reinterpret_cast<const sycl::int4 *>(q8_1_quant_ptr + sizeof(int) * iqs); |
| 466 | |
| 467 | int sumi = 0; |
| 468 | #pragma unroll |
| 469 | for (int i = 0; i < 4; ++i) { |
| 470 | sumi = dpct::dp4a(v[i], u[i], sumi); |
| 471 | } |
| 472 | |
| 473 | const sycl::half2 ds_values = *q8_1_ds; |
| 474 | return static_cast<float>(d) * static_cast<float>(ds_values[0]) * sumi; |
| 475 | } |
| 476 | }; |
| 477 | |
| 478 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_Q8_0> { |
| 479 | static constexpr ggml_type gtype = GGML_TYPE_Q8_0; |
| 480 | |
| 481 | using q8_0_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_Q8_0>; |
| 482 | using q8_0_traits = typename q8_0_block::traits; |
| 483 | |
| 484 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 485 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 486 | const sycl::half2 * q8_1_ds, const int & iqs, |
| 487 | const int vdr = q8_0_traits::vdr_mmvq) { |
| 488 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 489 | const int8_t * qs = reinterpret_cast<const int8_t *>(base + ibx_offset.first); |
| 490 | const ggml_half d = *reinterpret_cast<const ggml_half *>(base + d_offset.first); |
| 491 | |
| 492 | int v[q8_0_traits::vdr_mmvq]; |
| 493 | int u[q8_0_traits::vdr_mmvq]; |
| 494 | |
| 495 | #pragma unroll |
| 496 | for (int i = 0; i < vdr; ++i) { |
| 497 | v[i] = get_int_from_int8(qs, iqs + i); |
| 498 | u[i] = get_int_from_int8_aligned(q8_1_quant_ptr, iqs + i); |
| 499 | } |
| 500 | |
| 501 | int sumi = 0; |
| 502 | #pragma unroll |
| 503 | for (int i = 0; i < vdr; ++i) { |
| 504 | sumi = dpct::dp4a(v[i], u[i], sumi); |
| 505 | } |
| 506 | |
| 507 | const sycl::half2 ds_values = *q8_1_ds; |
| 508 | return static_cast<float>(d) * static_cast<float>(ds_values[0]) * sumi; |
| 509 | } |
| 510 | }; |
| 511 | |
| 512 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_Q2_K> { |
| 513 | static constexpr ggml_type gtype = GGML_TYPE_Q2_K; |
| 514 | |
| 515 | using q2_k_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_Q2_K>; |
| 516 | using q2_k_traits = typename q2_k_block::traits; |
| 517 | |
| 518 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 519 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 520 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 521 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 522 | const uint8_t * qs = base + ibx_offset.first; |
| 523 | const uint8_t * scales = base + d_offset.first; |
| 524 | const ggml_half2 * dm = reinterpret_cast<const ggml_half2 *>(base + d_offset.second); |
| 525 | |
| 526 | const int bq8_offset = QR2_K * (iqs / QI8_1); |
| 527 | const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1 / 2); |
| 528 | |
| 529 | const int v = get_int_from_uint8_aligned(qs, iqs); |
| 530 | |
| 531 | int u[QR2_K]; |
| 532 | float d8[QR2_K]; |
| 533 | |
| 534 | #pragma unroll |
| 535 | for (int i = 0; i < QR2_K; ++i) { |
| 536 | const int8_t * quant_base_ptr = q8_1_quant_ptr + (bq8_offset + i) * QK8_1; |
| 537 | u[i] = get_int_from_int8_aligned(quant_base_ptr, iqs % QI8_1); |
| 538 | d8[i] = (*(q8_1_ds + bq8_offset + i))[0]; |
| 539 | } |
| 540 | |
| 541 | return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales + scale_offset, *dm, d8); |
| 542 | } |
| 543 | }; |
| 544 | |
| 545 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_Q3_K> { |
| 546 | static constexpr ggml_type gtype = GGML_TYPE_Q3_K; |
| 547 | |
| 548 | using q3_k_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_Q3_K>; |
| 549 | using q3_k_traits = typename q3_k_block::traits; |
| 550 | |
| 551 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 552 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 553 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 554 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 555 | const uint8_t * qs = base + ibx_offset.first; |
| 556 | const uint8_t * hmask = base + ibx_offset.second; |
| 557 | const uint8_t * scales = base + d_offset.first; |
| 558 | const ggml_half d = *reinterpret_cast<const ggml_half *>(base + d_offset.second); |
| 559 | |
| 560 | const int bq8_offset = QR3_K * (iqs / (QI3_K / 2)); |
| 561 | const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1 / 2); |
| 562 | |
| 563 | const int vl = get_int_from_uint8(qs, iqs); |
| 564 | const int vh = ~get_int_from_uint8(hmask, iqs % (QI3_K / 2)) >> bq8_offset; |
| 565 | |
| 566 | int u[QR3_K]; |
| 567 | float d8[QR3_K]; |
| 568 | |
| 569 | #pragma unroll |
| 570 | for (int i = 0; i < QR3_K; ++i) { |
| 571 | const int8_t * quant_base_ptr = q8_1_quant_ptr + (bq8_offset + i) * QK8_1; |
| 572 | u[i] = get_int_from_int8_aligned(quant_base_ptr, iqs % QI8_1); |
| 573 | d8[i] = (*(q8_1_ds + bq8_offset + i))[0]; |
| 574 | } |
| 575 | |
| 576 | return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, scales, scale_offset, static_cast<float>(d), d8); |
| 577 | } |
| 578 | }; |
| 579 | |
| 580 | static inline float vec_dot_q4_K_q8_1_common(const int * __restrict__ q4, const uint16_t * __restrict__ scales, |
| 581 | const ggml_half2 & dm, const block_q8_1 * __restrict__ bq8_1, |
| 582 | const int & iqs) { |
| 583 | int v[2]; |
| 584 | int u[2 * QR4_K]; |
| 585 | float d8[QR4_K]; |
| 586 | |
| 587 | v[0] = q4[0]; |
| 588 | v[1] = q4[4]; |
| 589 | |
| 590 | uint16_t aux[2]; |
| 591 | const int j = (QR4_K * ((iqs / 2) / (QI8_1 / 2))) / 2; |
| 592 | if (j < 2) { |
| 593 | aux[0] = scales[j + 0] & 0x3f3f; |
| 594 | aux[1] = scales[j + 2] & 0x3f3f; |
| 595 | } else { |
| 596 | aux[0] = ((scales[j + 2] >> 0) & 0x0f0f) | ((scales[j - 2] & 0xc0c0) >> 2); |
| 597 | aux[1] = ((scales[j + 2] >> 4) & 0x0f0f) | ((scales[j - 0] & 0xc0c0) >> 2); |
| 598 | } |
| 599 | |
| 600 | const uint8_t * sc = (const uint8_t *) aux; |
| 601 | const uint8_t * m = sc + 2; |
| 602 | |
| 603 | const int bq8_offset = QR4_K * ((iqs / 2) / (QI8_1 / 2)); |
| 604 | |
| 605 | for (int i = 0; i < QR4_K; ++i) { |
| 606 | const block_q8_1 * bq8i = bq8_1 + bq8_offset + i; |
| 607 | d8[i] = bq8i->ds[0]; |
| 608 | |
| 609 | const int * q8 = (const int *) bq8i->qs + ((iqs / 2) % 4); |
| 610 | u[2 * i + 0] = q8[0]; |
| 611 | u[2 * i + 1] = q8[4]; |
| 612 | } |
| 613 | |
| 614 | return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, dm, d8); |
| 615 | } |
| 616 | |
| 617 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_Q4_K> { |
| 618 | static constexpr ggml_type gtype = GGML_TYPE_Q4_K; |
| 619 | |
| 620 | using q4_k_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_Q4_K>; |
| 621 | using q4_k_traits = typename q4_k_block::traits; |
| 622 | |
| 623 | struct weights { |
| 624 | int v[2]; |
| 625 | uint16_t aux[2]; |
| 626 | ggml_half2 dm; |
| 627 | int bq8_offset; |
| 628 | }; |
| 629 | |
| 630 | struct activations { |
| 631 | int u[2 * QR4_K]; |
| 632 | float d8[QR4_K]; |
| 633 | }; |
| 634 | |
| 635 | __dpct_inline__ static weights load(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 636 | const std::pair<int, int> d_offset, const int & iqs) { |
| 637 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 638 | const uint8_t * qs = base + ibx_offset.first; |
| 639 | const uint8_t * scs = base + d_offset.first; |
| 640 | const ggml_half2 * dms = reinterpret_cast<const ggml_half2 *>(base + d_offset.second); |
| 641 | |
| 642 | weights w; |
| 643 | w.bq8_offset = QR4_K * ((iqs / 2) / (QI8_1 / 2)); |
| 644 | |
| 645 | const int * q4 = (const int *) (qs + 16 * w.bq8_offset + 4 * ((iqs / 2) % 4)); |
| 646 | const uint16_t * scales = (const uint16_t *) scs; |
| 647 | |
| 648 | w.v[0] = q4[0]; |
| 649 | w.v[1] = q4[4]; |
| 650 | |
| 651 | const int j = (QR4_K * ((iqs / 2) / (QI8_1 / 2))) / 2; |
| 652 | if (j < 2) { |
| 653 | w.aux[0] = scales[j + 0] & 0x3f3f; |
| 654 | w.aux[1] = scales[j + 2] & 0x3f3f; |
| 655 | } else { |
| 656 | w.aux[0] = ((scales[j + 2] >> 0) & 0x0f0f) | ((scales[j - 2] & 0xc0c0) >> 2); |
| 657 | w.aux[1] = ((scales[j + 2] >> 4) & 0x0f0f) | ((scales[j - 0] & 0xc0c0) >> 2); |
| 658 | } |
| 659 | |
| 660 | w.dm = *dms; |
| 661 | |
| 662 | return w; |
| 663 | } |
| 664 | |
| 665 | __dpct_inline__ static activations load_activations(const int8_t * q8_1_quant_ptr, |
| 666 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 667 | activations a; |
| 668 | const int bq8_offset = QR4_K * ((iqs / 2) / (QI8_1 / 2)); |
| 669 | for (int i = 0; i < QR4_K; ++i) { |
| 670 | const int8_t * quant_base_ptr = q8_1_quant_ptr + (bq8_offset + i) * QK8_1; |
| 671 | sycl::half2 ds_values = *(q8_1_ds + bq8_offset + i); |
| 672 | |
| 673 | a.d8[i] = ds_values[0]; |
| 674 | |
| 675 | const int * q8 = (const int *) quant_base_ptr + ((iqs / 2) % 4); |
| 676 | a.u[2 * i + 0] = q8[0]; |
| 677 | a.u[2 * i + 1] = q8[4]; |
| 678 | } |
| 679 | |
| 680 | return a; |
| 681 | } |
| 682 | |
| 683 | __dpct_inline__ static float apply(const weights & w, const activations & a) { |
| 684 | const uint8_t * sc = (const uint8_t *) w.aux; |
| 685 | const uint8_t * m = sc + 2; |
| 686 | |
| 687 | return vec_dot_q4_K_q8_1_impl_vmmq(w.v, a.u, sc, m, w.dm, a.d8); |
| 688 | } |
| 689 | |
| 690 | __dpct_inline__ static float dot(const weights & w, const int8_t * q8_1_quant_ptr, |
| 691 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 692 | const auto a = load_activations(q8_1_quant_ptr, q8_1_ds, iqs); |
| 693 | |
| 694 | return apply(w, a); |
| 695 | } |
| 696 | |
| 697 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 698 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 699 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 700 | return dot(load(vbq, ibx_offset, d_offset, iqs), q8_1_quant_ptr, q8_1_ds, iqs); |
| 701 | } |
| 702 | }; |
| 703 | |
| 704 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_Q5_K> { |
| 705 | static constexpr ggml_type gtype = GGML_TYPE_Q5_K; |
| 706 | |
| 707 | using q5_k_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_Q5_K>; |
| 708 | using q5_k_traits = typename q5_k_block::traits; |
| 709 | |
| 710 | struct weights { |
| 711 | int vl[2]; |
| 712 | int vh[2]; |
| 713 | uint16_t aux[2]; |
| 714 | ggml_half2 dm; |
| 715 | }; |
| 716 | |
| 717 | // same activation layout as Q4_K |
| 718 | static_assert(QR5_K == QR4_K); |
| 719 | using activations = reorder_vec_dot_q_sycl<GGML_TYPE_Q4_K>::activations; |
| 720 | |
| 721 | __dpct_inline__ static weights load(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 722 | const std::pair<int, int> d_offset, const int & iqs) { |
| 723 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 724 | const uint8_t * qs = base + ibx_offset.first; // low 4 bits |
| 725 | const uint8_t * qh_base = base + ibx_offset.second; // high bit |
| 726 | const uint8_t * scs = base + d_offset.first; |
| 727 | const ggml_half2 * dms = reinterpret_cast<const ggml_half2 *>(base + d_offset.second); |
| 728 | |
| 729 | const int bq8_offset = QR5_K * ((iqs / 2) / (QI8_1 / 2)); |
| 730 | const int * ql_ptr = (const int *) (qs + 16 * bq8_offset + 4 * ((iqs / 2) % 4)); |
| 731 | const int * qh_ptr = (const int *) (qh_base + 4 * ((iqs / 2) % 4)); |
| 732 | const uint16_t * scales = (const uint16_t *) scs; |
| 733 | |
| 734 | weights w; |
| 735 | w.vl[0] = ql_ptr[0]; |
| 736 | w.vl[1] = ql_ptr[4]; |
| 737 | |
| 738 | w.vh[0] = qh_ptr[0] >> bq8_offset; |
| 739 | w.vh[1] = qh_ptr[4] >> bq8_offset; |
| 740 | |
| 741 | const int j = (QR5_K * ((iqs / 2) / (QI8_1 / 2))) / 2; |
| 742 | if (j < 2) { |
| 743 | w.aux[0] = scales[j + 0] & 0x3f3f; |
| 744 | w.aux[1] = scales[j + 2] & 0x3f3f; |
| 745 | } else { |
| 746 | w.aux[0] = ((scales[j + 2] >> 0) & 0x0f0f) | ((scales[j - 2] & 0xc0c0) >> 2); |
| 747 | w.aux[1] = ((scales[j + 2] >> 4) & 0x0f0f) | ((scales[j - 0] & 0xc0c0) >> 2); |
| 748 | } |
| 749 | |
| 750 | w.dm = *dms; |
| 751 | |
| 752 | return w; |
| 753 | } |
| 754 | |
| 755 | __dpct_inline__ static activations load_activations(const int8_t * q8_1_quant_ptr, |
| 756 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 757 | return reorder_vec_dot_q_sycl<GGML_TYPE_Q4_K>::load_activations(q8_1_quant_ptr, q8_1_ds, iqs); |
| 758 | } |
| 759 | |
| 760 | __dpct_inline__ static float apply(const weights & w, const activations & a) { |
| 761 | const uint8_t * sc = (const uint8_t *) w.aux; |
| 762 | const uint8_t * m = sc + 2; |
| 763 | |
| 764 | return vec_dot_q5_K_q8_1_impl_vmmq(w.vl, w.vh, a.u, sc, m, w.dm, a.d8); |
| 765 | } |
| 766 | |
| 767 | __dpct_inline__ static float dot(const weights & w, const int8_t * q8_1_quant_ptr, |
| 768 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 769 | return apply(w, load_activations(q8_1_quant_ptr, q8_1_ds, iqs)); |
| 770 | } |
| 771 | |
| 772 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 773 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 774 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 775 | return dot(load(vbq, ibx_offset, d_offset, iqs), q8_1_quant_ptr, q8_1_ds, iqs); |
| 776 | } |
| 777 | }; |
| 778 | |
| 779 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_Q6_K> { |
| 780 | static constexpr ggml_type gtype = GGML_TYPE_Q6_K; |
| 781 | |
| 782 | using q6_k_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_Q6_K>; |
| 783 | using q6_k_traits = typename q6_k_block::traits; |
| 784 | |
| 785 | __dpct_inline__ float vec_dot_q6_K_q8_1_impl_mmvq(const int vl, const int vh, const int * __restrict__ u, |
| 786 | const int8_t * __restrict__ scales, const float d, |
| 787 | const float * __restrict__ d8) { |
| 788 | return vec_dot_q6_K_q8_1_impl_mmvq_scalar( |
| 789 | vl, vh, u[0], u[1], scales[0], scales[4], d, d8[0], d8[1]); |
| 790 | } |
| 791 | |
| 792 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 793 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, const sycl::half2 * q8_1_ds, |
| 794 | const int iqs) { |
| 795 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 796 | const uint8_t * ql = base + ibx_offset.first; |
| 797 | const uint8_t * qh = base + ibx_offset.second; |
| 798 | const int8_t * scales = reinterpret_cast<const int8_t *>(base + d_offset.first); |
| 799 | const ggml_half * d = (const ggml_half *) (base + d_offset.second); |
| 800 | |
| 801 | const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K / 2)) + (iqs % (QI6_K / 2)) / (QI6_K / 4); |
| 802 | const int scale_offset = (QI6_K / 4) * (iqs / (QI6_K / 2)) + (iqs % (QI6_K / 2)) / (QI6_K / 8); |
| 803 | const int vh_shift = 2 * ((iqs % (QI6_K / 2)) / (QI6_K / 4)); |
| 804 | |
| 805 | const int vl = get_int_from_uint8(ql, iqs); |
| 806 | const int vh = get_int_from_uint8(qh, (QI6_K / 4) * (iqs / (QI6_K / 2)) + iqs % (QI6_K / 4)) >> vh_shift; |
| 807 | |
| 808 | const int8_t * scs = scales + scale_offset; |
| 809 | |
| 810 | const int u0 = get_int_from_int8_aligned( |
| 811 | q8_1_quant_ptr + bq8_offset * QK8_1, iqs % QI8_1); |
| 812 | const int u1 = get_int_from_int8_aligned( |
| 813 | q8_1_quant_ptr + (bq8_offset + 2) * QK8_1, iqs % QI8_1); |
| 814 | const float d80 = (*(q8_1_ds + bq8_offset + 0))[0]; |
| 815 | const float d81 = (*(q8_1_ds + bq8_offset + 2))[0]; |
| 816 | |
| 817 | return vec_dot_q6_K_q8_1_impl_mmvq_scalar( |
| 818 | vl, vh, u0, u1, scs[0], scs[4], *d, d80, d81); |
| 819 | } |
| 820 | }; |
| 821 | |
| 822 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_MXFP4> { |
| 823 | static constexpr ggml_type gtype = GGML_TYPE_MXFP4; |
| 824 | |
| 825 | using mxfp4_block = ggml_sycl_reordered::block_q_t<GGML_TYPE_MXFP4>; |
| 826 | using mxfp4_traits = typename mxfp4_block::traits; |
| 827 | |
| 828 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 829 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 830 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 831 | static_assert(mxfp4_traits::vdr_mmvq == 2, "vector load assumes vdr_mmvq == 2"); |
| 832 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 833 | |
| 834 | // Reordered nibble blocks are 16 contiguous bytes and iqs is 0 or 2, so each lane's two |
| 835 | // weight ints are one aligned 8-byte load (the AoS layout needed eight byte loads). |
| 836 | const sycl::int2 q4 = *reinterpret_cast<const sycl::int2 *>(base + ibx_offset.first + sizeof(int) * iqs); |
| 837 | const uint8_t e = base[d_offset.first]; |
| 838 | |
| 839 | // Low nibbles pair with q8_1 ints iqs..iqs+1, high nibbles with iqs+4..iqs+5. |
| 840 | const sycl::int2 u_lo = *reinterpret_cast<const sycl::int2 *>(q8_1_quant_ptr + sizeof(int) * iqs); |
| 841 | const sycl::int2 u_hi = *reinterpret_cast<const sycl::int2 *>(q8_1_quant_ptr + sizeof(int) * (iqs + 4)); |
| 842 | |
| 843 | const sycl::int2 v0 = get_int_from_mxfp4(q4.x()); |
| 844 | const sycl::int2 v1 = get_int_from_mxfp4(q4.y()); |
| 845 | |
| 846 | int sumi = 0; |
| 847 | sumi = ggml_sycl_dp4a(v0.x(), u_lo.x(), sumi); |
| 848 | sumi = ggml_sycl_dp4a(v0.y(), u_hi.x(), sumi); |
| 849 | sumi = ggml_sycl_dp4a(v1.x(), u_lo.y(), sumi); |
| 850 | sumi = ggml_sycl_dp4a(v1.y(), u_hi.y(), sumi); |
| 851 | |
| 852 | const float d = ggml_sycl_e8m0_to_fp32(e) * 0.5f * static_cast<float>((*q8_1_ds)[0]); |
| 853 | return d * sumi; |
| 854 | } |
| 855 | }; |
| 856 | #define VDR_Q4_0_Q8_1_MMVQ 2 |
| 857 | #define VDR_Q4_0_Q8_1_MMQ 4 |
| 858 | |
| 859 | #define VDR_Q2_0_Q8_1_MMVQ 1 |
| 860 | |
| 861 | template <int vdr> |
| 862 | static __dpct_inline__ float vec_dot_q2_0_q8_1_impl( |
| 863 | const int * v, |
| 864 | const int * u, |
| 865 | const float & d2, |
| 866 | const sycl::half2 & ds8) { |
| 867 | int sumi = 0; |
| 868 | |
| 869 | #pragma unroll |
| 870 | for (int i = 0; i < vdr; ++i) { |
| 871 | #pragma unroll |
| 872 | for (int j = 0; j < 4; ++j) { |
| 873 | const uint8_t q = (uint8_t) ((uint32_t) v[i] >> (8 * j)); |
| 874 | |
| 875 | // unpack 2-bit values to byte lanes (0..3), then apply zero-point |
| 876 | // correction with ds8f.y() below, mirroring the q4_0 style. |
| 877 | int vi = 0; |
| 878 | vi |= (((q >> 0) & 0x3) & 0xFF) << 0; |
| 879 | vi |= (((q >> 2) & 0x3) & 0xFF) << 8; |
| 880 | vi |= (((q >> 4) & 0x3) & 0xFF) << 16; |
| 881 | vi |= (((q >> 6) & 0x3) & 0xFF) << 24; |
| 882 | |
| 883 | sumi = dpct::dp4a(vi, u[4 * i + j], sumi); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | const sycl::float2 ds8f = ds8.convert<float, sycl::rounding_mode::automatic>(); |
| 888 | // q2_0 has zero-point 1. Scale ds8f.y() by processed-lane ratio, |
| 889 | // consistent with q4_0's explicit zero-point subtraction style. |
| 890 | return d2 * (sumi * ds8f.x() - ((float) vdr / (float) QI2_0) * ds8f.y()); |
| 891 | } |
| 892 | |
| 893 | template <int vdr> |
| 894 | static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int * v, const int * u, const float & d4, |
| 895 | const sycl::half2 & ds8) { |
| 896 | int sumi = 0; |
| 897 | #pragma unroll |
| 898 | for (int i = 0; i < vdr; ++i) { |
| 899 | const int vi0 = (v[i] >> 0) & 0x0F0F0F0F; |
| 900 | const int vi1 = (v[i] >> 4) & 0x0F0F0F0F; |
| 901 | |
| 902 | // SIMD dot product of quantized values |
| 903 | sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi); |
| 904 | sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi); |
| 905 | } |
| 906 | |
| 907 | const sycl::float2 ds8f = ds8.convert<float, sycl::rounding_mode::automatic>(); |
| 908 | |
| 909 | // second part effectively subtracts 8 from each quant value |
| 910 | return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y()); |
| 911 | } |
| 912 | |
| 913 | #define VDR_Q4_1_Q8_1_MMVQ 2 |
| 914 | #define VDR_Q4_1_Q8_1_MMQ 4 |
| 915 | |
| 916 | template <int vdr> |
| 917 | static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u, |
| 918 | const sycl::half2 &dm4, |
| 919 | const sycl::half2 &ds8) { |
| 920 | |
| 921 | int sumi = 0; |
| 922 | |
| 923 | #pragma unroll |
| 924 | for (int i = 0; i < vdr; ++i) { |
| 925 | const int vi0 = (v[i] >> 0) & 0x0F0F0F0F; |
| 926 | const int vi1 = (v[i] >> 4) & 0x0F0F0F0F; |
| 927 | |
| 928 | // SIMD dot product of quantized values |
| 929 | sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi); |
| 930 | sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi); |
| 931 | } |
| 932 | |
| 933 | #ifdef GGML_SYCL_F16 |
| 934 | const sycl::float2 tmp = |
| 935 | (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>(); |
| 936 | const float d4d8 = tmp.x(); |
| 937 | const float m4s8 = tmp.y(); |
| 938 | #else |
| 939 | const sycl::float2 dm4f = |
| 940 | dm4.convert<float, sycl::rounding_mode::automatic>(); |
| 941 | const sycl::float2 ds8f = |
| 942 | ds8.convert<float, sycl::rounding_mode::automatic>(); |
| 943 | const float d4d8 = dm4f.x() * ds8f.x(); |
| 944 | const float m4s8 = dm4f.y() * ds8f.y(); |
| 945 | #endif // GGML_SYCL_F16 |
| 946 | |
| 947 | // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it |
| 948 | return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1)); |
| 949 | } |
| 950 | |
| 951 | #define VDR_Q5_0_Q8_1_MMVQ 2 |
| 952 | #define VDR_Q5_0_Q8_1_MMQ 4 |
| 953 | |
| 954 | template <int vdr> |
| 955 | static __dpct_inline__ float |
| 956 | vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u, |
| 957 | const float &d5, const sycl::half2 &ds8) { |
| 958 | int sumi = 0; |
| 959 | |
| 960 | #pragma unroll |
| 961 | for (int i = 0; i < vdr; ++i) { |
| 962 | int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits |
| 963 | vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4 |
| 964 | vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12 |
| 965 | vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20 |
| 966 | vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28 |
| 967 | sumi = dpct::dp4a(vi0, u[2 * i + 0], |
| 968 | sumi); // SIMD dot product of quantized values |
| 969 | |
| 970 | int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits |
| 971 | vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4 |
| 972 | vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12 |
| 973 | vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20 |
| 974 | vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28 |
| 975 | sumi = dpct::dp4a(vi1, u[2 * i + 1], |
| 976 | sumi); // SIMD dot product of quantized values |
| 977 | } |
| 978 | |
| 979 | const sycl::float2 ds8f = |
| 980 | ds8.convert<float, sycl::rounding_mode::automatic>(); |
| 981 | |
| 982 | // second part effectively subtracts 16 from each quant value |
| 983 | return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y()); |
| 984 | } |
| 985 | |
| 986 | #define VDR_Q5_1_Q8_1_MMVQ 2 |
| 987 | #define VDR_Q5_1_Q8_1_MMQ 4 |
| 988 | |
| 989 | template <int vdr> |
| 990 | static __dpct_inline__ float |
| 991 | vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u, |
| 992 | const sycl::half2 &dm5, const sycl::half2 &ds8) { |
| 993 | |
| 994 | int sumi = 0; |
| 995 | |
| 996 | #pragma unroll |
| 997 | for (int i = 0; i < vdr; ++i) { |
| 998 | int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits |
| 999 | vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4 |
| 1000 | vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12 |
| 1001 | vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20 |
| 1002 | vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28 |
| 1003 | sumi = dpct::dp4a(vi0, u[2 * i + 0], |
| 1004 | sumi); // SIMD dot product of quantized values |
| 1005 | |
| 1006 | int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits |
| 1007 | vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4 |
| 1008 | vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12 |
| 1009 | vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20 |
| 1010 | vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28 |
| 1011 | sumi = dpct::dp4a(vi1, u[2 * i + 1], |
| 1012 | sumi); // SIMD dot product of quantized values |
| 1013 | } |
| 1014 | |
| 1015 | #ifdef GGML_SYCL_F16 |
| 1016 | const sycl::float2 tmp = |
| 1017 | (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>(); |
| 1018 | const float d5d8 = tmp.x(); |
| 1019 | const float m5s8 = tmp.y(); |
| 1020 | |
| 1021 | |
| 1022 | #else |
| 1023 | const sycl::float2 dm5f = |
| 1024 | dm5.convert<float, sycl::rounding_mode::automatic>(); |
| 1025 | const sycl::float2 ds8f = |
| 1026 | ds8.convert<float, sycl::rounding_mode::automatic>(); |
| 1027 | const float d5d8 = dm5f.x() * ds8f.x(); |
| 1028 | const float m5s8 = dm5f.y() * ds8f.y(); |
| 1029 | #endif // GGML_SYCL_F16 |
| 1030 | |
| 1031 | // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it |
| 1032 | return sumi*d5d8 + m5s8 / (QI5_1 / vdr); |
| 1033 | } |
| 1034 | |
| 1035 | #define VDR_Q8_0_Q8_1_MMVQ 2 |
| 1036 | #define VDR_Q8_0_Q8_1_MMQ 8 |
| 1037 | |
| 1038 | template <int vdr> |
| 1039 | static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u, |
| 1040 | const float &d8_0, |
| 1041 | const float &d8_1) { |
| 1042 | |
| 1043 | int sumi = 0; |
| 1044 | |
| 1045 | #pragma unroll |
| 1046 | for (int i = 0; i < vdr; ++i) { |
| 1047 | // SIMD dot product of quantized values |
| 1048 | sumi = dpct::dp4a(v[i], u[i], sumi); |
| 1049 | } |
| 1050 | |
| 1051 | return d8_0*d8_1 * sumi; |
| 1052 | } |
| 1053 | |
| 1054 | template <typename T, int vdr> |
| 1055 | static __dpct_inline__ T vec_dot_q8_0_q8_1_impl(const int * v, const int * u, const T & d8_0, const T & d8_1) { |
| 1056 | int sumi = 0; |
| 1057 | |
| 1058 | #pragma unroll |
| 1059 | for (int i = 0; i < vdr; ++i) { |
| 1060 | // SIMD dot product of quantized values |
| 1061 | sumi = ggml_sycl_dp4a(v[i], u[i], sumi); |
| 1062 | } |
| 1063 | |
| 1064 | return d8_0*d8_1 * ((T) sumi); |
| 1065 | } |
| 1066 | |
| 1067 | template <int vdr> |
| 1068 | static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u, |
| 1069 | const sycl::half2 &dm8, |
| 1070 | const sycl::half2 &ds8) { |
| 1071 | |
| 1072 | int sumi = 0; |
| 1073 | |
| 1074 | #pragma unroll |
| 1075 | for (int i = 0; i < vdr; ++i) { |
| 1076 | // SIMD dot product of quantized values |
| 1077 | sumi = dpct::dp4a(v[i], u[i], sumi); |
| 1078 | } |
| 1079 | |
| 1080 | #ifdef GGML_SYCL_F16 |
| 1081 | const sycl::float2 tmp = |
| 1082 | (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>(); |
| 1083 | const float d8d8 = tmp.x(); |
| 1084 | const float m8s8 = tmp.y(); |
| 1085 | #else |
| 1086 | const sycl::float2 dm8f = |
| 1087 | dm8.convert<float, sycl::rounding_mode::automatic>(); |
| 1088 | const sycl::float2 ds8f = |
| 1089 | ds8.convert<float, sycl::rounding_mode::automatic>(); |
| 1090 | const float d8d8 = dm8f.x() * ds8f.x(); |
| 1091 | const float m8s8 = dm8f.y() * ds8f.y(); |
| 1092 | #endif // GGML_SYCL_F16 |
| 1093 | |
| 1094 | // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it |
| 1095 | return sumi*d8d8 + m8s8 / (QI8_1 / vdr); |
| 1096 | } |
| 1097 | |
| 1098 | static __dpct_inline__ float |
| 1099 | vec_dot_q4_0_q8_1(const void *__restrict__ vbq, |
| 1100 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1101 | |
| 1102 | const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq; |
| 1103 | |
| 1104 | int v[VDR_Q4_0_Q8_1_MMVQ]; |
| 1105 | int u[2 * VDR_Q4_0_Q8_1_MMVQ]; |
| 1106 | |
| 1107 | #pragma unroll |
| 1108 | for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) { |
| 1109 | v[i] = get_int_from_uint8(bq4_0->qs, iqs + i); |
| 1110 | u[2 * i + 0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i); |
| 1111 | u[2 * i + 1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0); |
| 1112 | } |
| 1113 | |
| 1114 | return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds); |
| 1115 | } |
| 1116 | |
| 1117 | static __dpct_inline__ float |
| 1118 | vec_dot_q2_0_q8_1(const void *__restrict__ vbq, |
| 1119 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1120 | |
| 1121 | const block_q2_0 * bq2_0 = (const block_q2_0 *) vbq; |
| 1122 | |
| 1123 | int v[2 * VDR_Q2_0_Q8_1_MMVQ]; |
| 1124 | int u[8 * VDR_Q2_0_Q8_1_MMVQ]; |
| 1125 | |
| 1126 | #pragma unroll |
| 1127 | for (int i = 0; i < VDR_Q2_0_Q8_1_MMVQ; ++i) { |
| 1128 | const int base = 4 * (iqs + i); |
| 1129 | |
| 1130 | // Q2_0 has QK2_0 = 64 and uses 2 x QK8_1 blocks on the RHS. |
| 1131 | v[2 * i + 0] = get_int_from_uint8(bq2_0->qs, iqs + i); |
| 1132 | v[2 * i + 1] = get_int_from_uint8(bq2_0->qs, iqs + i + QI2_0); |
| 1133 | |
| 1134 | u[8 * i + 0] = get_int_from_int8_aligned(bq8_1[0].qs, base + 0); |
| 1135 | u[8 * i + 1] = get_int_from_int8_aligned(bq8_1[0].qs, base + 1); |
| 1136 | u[8 * i + 2] = get_int_from_int8_aligned(bq8_1[0].qs, base + 2); |
| 1137 | u[8 * i + 3] = get_int_from_int8_aligned(bq8_1[0].qs, base + 3); |
| 1138 | |
| 1139 | u[8 * i + 4] = get_int_from_int8_aligned(bq8_1[1].qs, base + 0); |
| 1140 | u[8 * i + 5] = get_int_from_int8_aligned(bq8_1[1].qs, base + 1); |
| 1141 | u[8 * i + 6] = get_int_from_int8_aligned(bq8_1[1].qs, base + 2); |
| 1142 | u[8 * i + 7] = get_int_from_int8_aligned(bq8_1[1].qs, base + 3); |
| 1143 | } |
| 1144 | |
| 1145 | const float sum0 = vec_dot_q2_0_q8_1_impl<VDR_Q2_0_Q8_1_MMVQ>( |
| 1146 | v + 0, u + 0, bq2_0->d, bq8_1[0].ds); |
| 1147 | const float sum1 = vec_dot_q2_0_q8_1_impl<VDR_Q2_0_Q8_1_MMVQ>( |
| 1148 | v + VDR_Q2_0_Q8_1_MMVQ, u + 4 * VDR_Q2_0_Q8_1_MMVQ, bq2_0->d, bq8_1[1].ds); |
| 1149 | return sum0 + sum1; |
| 1150 | } |
| 1151 | |
| 1152 | static __dpct_inline__ float |
| 1153 | vec_dot_q4_1_q8_1(const void *__restrict__ vbq, |
| 1154 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1155 | |
| 1156 | const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq; |
| 1157 | |
| 1158 | int v[VDR_Q4_1_Q8_1_MMVQ]; |
| 1159 | int u[2*VDR_Q4_1_Q8_1_MMVQ]; |
| 1160 | |
| 1161 | #pragma unroll |
| 1162 | for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) { |
| 1163 | v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i); |
| 1164 | u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i); |
| 1165 | u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1); |
| 1166 | } |
| 1167 | |
| 1168 | return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds); |
| 1169 | } |
| 1170 | |
| 1171 | #define VDR_MXFP4_Q8_1_MMVQ 2 |
| 1172 | #define VDR_MXFP4_Q8_1_MMQ 4 |
| 1173 | |
| 1174 | static __dpct_inline__ float vec_dot_mxfp4_q8_1(const void * __restrict__ vbq, |
| 1175 | const block_q8_1 * __restrict__ bq8_1, |
| 1176 | const int & iqs) { |
| 1177 | const block_mxfp4 * bq4 = (const block_mxfp4 *) vbq; |
| 1178 | |
| 1179 | const int * q8 = (const int *) bq8_1->qs + iqs; |
| 1180 | |
| 1181 | int sumi = 0; |
| 1182 | #pragma unroll |
| 1183 | for (int l = 0; l < VDR_MXFP4_Q8_1_MMVQ; ++l) { |
| 1184 | const int aux_q4 = get_int_b1(bq4->qs, iqs + l); |
| 1185 | const sycl::int2 v = get_int_from_mxfp4(aux_q4); |
| 1186 | sumi = ggml_sycl_dp4a(v.x(), q8[l + 0], sumi); |
| 1187 | sumi = ggml_sycl_dp4a(v.y(), q8[l + 4], sumi); |
| 1188 | } |
| 1189 | |
| 1190 | const float d = ggml_sycl_e8m0_to_fp32(bq4->e) * 0.5f * (bq8_1->ds)[0]; |
| 1191 | return d * sumi; |
| 1192 | } |
| 1193 | |
| 1194 | #define VDR_NVFP4_Q8_1_MMVQ 4 |
| 1195 | #define VDR_NVFP4_Q8_1_MMQ 8 |
| 1196 | |
| 1197 | static __dpct_inline__ float vec_dot_nvfp4_q8_1(const void * __restrict__ vbq, |
| 1198 | const block_q8_1 * __restrict__ bq8_1, |
| 1199 | const int32_t & iqs) { |
| 1200 | const block_nvfp4 * bq4 = (const block_nvfp4 *) vbq; |
| 1201 | float sum = 0.0f; |
| 1202 | #pragma unroll |
| 1203 | for (int i = 0; i < VDR_NVFP4_Q8_1_MMVQ/2; i++) { |
| 1204 | const int32_t iqs0 = iqs + 2*i; |
| 1205 | const int32_t iqs1 = iqs0 + 1; |
| 1206 | const int32_t is = iqs0 >> 1; |
| 1207 | const sycl::int2 v0 = get_int_from_table_16(get_int_b4(bq4->qs, iqs0), kvalues_mxfp4); |
| 1208 | const sycl::int2 v1 = get_int_from_table_16(get_int_b4(bq4->qs, iqs1), kvalues_mxfp4); |
| 1209 | const block_q8_1 * bq8 = bq8_1 + (is >> 1); |
| 1210 | const int32_t i8 = ((is & 1) << 2); |
| 1211 | |
| 1212 | int sumi = ggml_sycl_dp4a(v0.x(), get_int_b4(bq8->qs, i8 + 0), 0); |
| 1213 | sumi = ggml_sycl_dp4a(v0.y(), get_int_b4(bq8->qs, i8 + 2), sumi); |
| 1214 | sumi = ggml_sycl_dp4a(v1.x(), get_int_b4(bq8->qs, i8 + 1), sumi); |
| 1215 | sumi = ggml_sycl_dp4a(v1.y(), get_int_b4(bq8->qs, i8 + 3), sumi); |
| 1216 | |
| 1217 | const float d = ggml_sycl_ue4m3_to_fp32(bq4->d[is]) * (bq8->ds)[0]; |
| 1218 | sum += d * float(sumi); |
| 1219 | } |
| 1220 | |
| 1221 | return sum; |
| 1222 | } |
| 1223 | |
| 1224 | static __dpct_inline__ float |
| 1225 | vec_dot_q5_0_q8_1(const void *__restrict__ vbq, |
| 1226 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1227 | |
| 1228 | const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq; |
| 1229 | |
| 1230 | int vl[VDR_Q5_0_Q8_1_MMVQ]; |
| 1231 | int vh[VDR_Q5_0_Q8_1_MMVQ]; |
| 1232 | int u[2*VDR_Q5_0_Q8_1_MMVQ]; |
| 1233 | |
| 1234 | #pragma unroll |
| 1235 | for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) { |
| 1236 | vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i); |
| 1237 | vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i)); |
| 1238 | u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i); |
| 1239 | u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0); |
| 1240 | } |
| 1241 | |
| 1242 | return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds); |
| 1243 | } |
| 1244 | |
| 1245 | static __dpct_inline__ float |
| 1246 | vec_dot_q5_1_q8_1(const void *__restrict__ vbq, |
| 1247 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1248 | |
| 1249 | const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq; |
| 1250 | |
| 1251 | int vl[VDR_Q5_1_Q8_1_MMVQ]; |
| 1252 | int vh[VDR_Q5_1_Q8_1_MMVQ]; |
| 1253 | int u[2*VDR_Q5_1_Q8_1_MMVQ]; |
| 1254 | |
| 1255 | #pragma unroll |
| 1256 | for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) { |
| 1257 | vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i); |
| 1258 | vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i)); |
| 1259 | u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i); |
| 1260 | u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1); |
| 1261 | } |
| 1262 | |
| 1263 | return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds); |
| 1264 | } |
| 1265 | |
| 1266 | static __dpct_inline__ float |
| 1267 | vec_dot_q8_0_q8_1(const void *__restrict__ vbq, |
| 1268 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1269 | |
| 1270 | const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq; |
| 1271 | |
| 1272 | int v[VDR_Q8_0_Q8_1_MMVQ]; |
| 1273 | int u[VDR_Q8_0_Q8_1_MMVQ]; |
| 1274 | |
| 1275 | #pragma unroll |
| 1276 | for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) { |
| 1277 | v[i] = get_int_from_int8(bq8_0->qs, iqs + i); |
| 1278 | u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i); |
| 1279 | } |
| 1280 | |
| 1281 | return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, |
| 1282 | bq8_1->ds[0]); |
| 1283 | } |
| 1284 | |
| 1285 | static __dpct_inline__ float |
| 1286 | vec_dot_q2_K_q8_1(const void *__restrict__ vbq, |
| 1287 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1288 | |
| 1289 | const block_q2_K * bq2_K = (const block_q2_K *) vbq; |
| 1290 | |
| 1291 | const int bq8_offset = QR2_K * (iqs / QI8_1); |
| 1292 | const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2); |
| 1293 | |
| 1294 | const uint8_t * scales = bq2_K->scales + scale_offset; |
| 1295 | |
| 1296 | const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs); |
| 1297 | int u[QR2_K]; |
| 1298 | float d8[QR2_K]; |
| 1299 | |
| 1300 | #pragma unroll |
| 1301 | for (int i = 0; i < QR2_K; ++ i) { |
| 1302 | u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1); |
| 1303 | d8[i] = bq8_1[bq8_offset + i].ds[0]; |
| 1304 | } |
| 1305 | |
| 1306 | return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8); |
| 1307 | } |
| 1308 | |
| 1309 | static __dpct_inline__ float |
| 1310 | vec_dot_q3_K_q8_1(const void *__restrict__ vbq, |
| 1311 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1312 | |
| 1313 | const block_q3_K * bq3_K = (const block_q3_K *) vbq; |
| 1314 | |
| 1315 | const int bq8_offset = QR3_K * (iqs / (QI3_K/2)); |
| 1316 | const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2); |
| 1317 | |
| 1318 | const float d = bq3_K->d; |
| 1319 | |
| 1320 | const int vl = get_int_from_uint8(bq3_K->qs, iqs); |
| 1321 | |
| 1322 | // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted |
| 1323 | const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset; |
| 1324 | |
| 1325 | int u[QR3_K]; |
| 1326 | float d8[QR3_K]; |
| 1327 | |
| 1328 | #pragma unroll |
| 1329 | for (int i = 0; i < QR3_K; ++i) { |
| 1330 | u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1); |
| 1331 | d8[i] = bq8_1[bq8_offset + i].ds[0]; |
| 1332 | } |
| 1333 | |
| 1334 | return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8); |
| 1335 | } |
| 1336 | |
| 1337 | static __dpct_inline__ float vec_dot_q4_K_q8_1(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, |
| 1338 | const int & iqs) { |
| 1339 | #ifndef GGML_QKK_64 |
| 1340 | |
| 1341 | const block_q4_K * bq4_K = (const block_q4_K *) vbq; |
| 1342 | |
| 1343 | const int bq8_offset = QR4_K * ((iqs / 2) / (QI8_1 / 2)); |
| 1344 | const int * q4 = (const int *) (bq4_K->qs + 16 * bq8_offset + 4 * ((iqs / 2) % 4)); |
| 1345 | const uint16_t * scales = (const uint16_t *) bq4_K->scales; |
| 1346 | |
| 1347 | return vec_dot_q4_K_q8_1_common(q4, scales, bq4_K->dm, bq8_1, iqs); |
| 1348 | |
| 1349 | #else |
| 1350 | |
| 1351 | #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics |
| 1352 | const block_q4_K * bq4_K = (const block_q4_K *) vbq; |
| 1353 | |
| 1354 | float sumf_d = 0.0f; |
| 1355 | float sumf_m = 0.0f; |
| 1356 | |
| 1357 | uint16_t aux16[2]; |
| 1358 | const uint8_t * s = (const uint8_t *)aux16; |
| 1359 | |
| 1360 | const uint16_t * a = (const uint16_t *)bq4_K->scales; |
| 1361 | aux16[0] = a[0] & 0x0f0f; |
| 1362 | aux16[1] = (a[0] >> 4) & 0x0f0f; |
| 1363 | |
| 1364 | const float dall = bq4_K->dm[0]; |
| 1365 | const float dmin = bq4_K->dm[1]; |
| 1366 | |
| 1367 | const float d8_1 = bq8_1[0].ds[0]; |
| 1368 | const float d8_2 = bq8_1[1].ds[1]; |
| 1369 | |
| 1370 | const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2)); |
| 1371 | const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4); |
| 1372 | const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2)); |
| 1373 | const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4); |
| 1374 | |
| 1375 | const int * q4 = (const int *)bq4_K->qs + (iqs/2); |
| 1376 | const int v1 = q4[0]; |
| 1377 | const int v2 = q4[4]; |
| 1378 | |
| 1379 | const int dot1 = dpct::dp4a(ui2, v2 & 0x0f0f0f0f, dpct::dp4a(ui1, v1 & 0x0f0f0f0f, 0)); |
| 1380 | const int dot2 = dpct::dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, dpct::dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0)); |
| 1381 | const int dot3 = dpct::dp4a(0x01010101, ui2, dpct::dp4a(0x01010101, ui1, 0)); |
| 1382 | const int dot4 = dpct::dp4a(0x01010101, ui4, dpct::dp4a(0x01010101, ui3, 0)); |
| 1383 | |
| 1384 | sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]); |
| 1385 | sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]); |
| 1386 | |
| 1387 | return dall * sumf_d - dmin * sumf_m; |
| 1388 | |
| 1389 | #else |
| 1390 | bad_arch(); |
| 1391 | #endif // __SYCL_ARCH__ >= VER_4VEC |
| 1392 | |
| 1393 | #endif |
| 1394 | } |
| 1395 | |
| 1396 | static __dpct_inline__ float |
| 1397 | vec_dot_q5_K_q8_1(const void *__restrict__ vbq, |
| 1398 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1399 | |
| 1400 | #ifndef GGML_QKK_64 |
| 1401 | const block_q5_K * bq5_K = (const block_q5_K *) vbq; |
| 1402 | |
| 1403 | int vl[2]; |
| 1404 | int vh[2]; |
| 1405 | int u[2*QR5_K]; |
| 1406 | float d8[QR5_K]; |
| 1407 | |
| 1408 | const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2)); |
| 1409 | const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4)); |
| 1410 | const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4)); |
| 1411 | |
| 1412 | vl[0] = ql[0]; |
| 1413 | vl[1] = ql[4]; |
| 1414 | |
| 1415 | vh[0] = qh[0] >> bq8_offset; |
| 1416 | vh[1] = qh[4] >> bq8_offset; |
| 1417 | |
| 1418 | const uint16_t * scales = (const uint16_t *)bq5_K->scales; |
| 1419 | uint16_t aux[2]; |
| 1420 | const int j = bq8_offset/2; |
| 1421 | if (j < 2) { |
| 1422 | aux[0] = scales[j+0] & 0x3f3f; |
| 1423 | aux[1] = scales[j+2] & 0x3f3f; |
| 1424 | } else { |
| 1425 | aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2); |
| 1426 | aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2); |
| 1427 | } |
| 1428 | const uint8_t * sc = (const uint8_t *)aux; |
| 1429 | const uint8_t * m = sc + 2; |
| 1430 | |
| 1431 | #pragma unroll |
| 1432 | for (int i = 0; i < QR5_K; ++i) { |
| 1433 | const block_q8_1 * bq8i = bq8_1 + bq8_offset + i; |
| 1434 | d8[i] = bq8i->ds[0]; |
| 1435 | |
| 1436 | const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4); |
| 1437 | u[2*i+0] = q8[0]; |
| 1438 | u[2*i+1] = q8[4]; |
| 1439 | } |
| 1440 | |
| 1441 | return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8); |
| 1442 | |
| 1443 | #else |
| 1444 | |
| 1445 | #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics |
| 1446 | const block_q5_K * bq5_K = (const block_q5_K *) vbq; |
| 1447 | |
| 1448 | const int8_t * s = bq5_K->scales; |
| 1449 | |
| 1450 | const float d = bq5_K->d; |
| 1451 | |
| 1452 | const float d8_1 = bq8_1[0].ds[0]; |
| 1453 | const float d8_2 = bq8_1[1].ds[1]; |
| 1454 | |
| 1455 | const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2)); |
| 1456 | const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4); |
| 1457 | const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2)); |
| 1458 | const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4); |
| 1459 | |
| 1460 | const int * ql = (const int *)bq5_K->qs + (iqs/2); |
| 1461 | const int vl1 = ql[0]; |
| 1462 | const int vl2 = ql[4]; |
| 1463 | |
| 1464 | const int step = 4 * (iqs/2); // 0, 4, 8, 12 |
| 1465 | const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6 |
| 1466 | const int in = step%8; // 0, 4, 0, 4 |
| 1467 | const int vh = (*((const int *)(bq5_K->qh + in))) >> im; |
| 1468 | |
| 1469 | const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f); |
| 1470 | const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f); |
| 1471 | const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f); |
| 1472 | const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f); |
| 1473 | |
| 1474 | const float sumf_d = d8_1 * (dpct::dp4a(ui1, v1, 0) * s[0] + dpct::dp4a(ui2, v2, 0) * s[1]) |
| 1475 | + d8_2 * (dpct::dp4a(ui3, v3, 0) * s[2] + dpct::dp4a(ui4, v4, 0) * s[3]); |
| 1476 | |
| 1477 | return d * sumf_d; |
| 1478 | |
| 1479 | #else |
| 1480 | bad_arch(); |
| 1481 | #endif // __SYCL_ARCH__ >= VER_4VEC |
| 1482 | |
| 1483 | #endif |
| 1484 | } |
| 1485 | |
| 1486 | static __dpct_inline__ float |
| 1487 | vec_dot_q6_K_q8_1(const void *__restrict__ vbq, |
| 1488 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1489 | |
| 1490 | const block_q6_K * bq6_K = (const block_q6_K *) vbq; |
| 1491 | |
| 1492 | const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4); |
| 1493 | const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8); |
| 1494 | const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4)); |
| 1495 | |
| 1496 | const int vl = get_int_from_uint8(bq6_K->ql, iqs); |
| 1497 | const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift; |
| 1498 | |
| 1499 | const int8_t * scales = bq6_K->scales + scale_offset; |
| 1500 | |
| 1501 | const int u0 = get_int_from_int8_aligned( |
| 1502 | bq8_1[bq8_offset + 0].qs, iqs % QI8_1); |
| 1503 | const int u1 = get_int_from_int8_aligned( |
| 1504 | bq8_1[bq8_offset + 2].qs, iqs % QI8_1); |
| 1505 | const float d80 = bq8_1[bq8_offset + 0].ds[0]; |
| 1506 | const float d81 = bq8_1[bq8_offset + 2].ds[0]; |
| 1507 | |
| 1508 | return vec_dot_q6_K_q8_1_impl_mmvq_scalar( |
| 1509 | vl, vh, u0, u1, scales[0], scales[4], bq6_K->d, d80, d81); |
| 1510 | } |
| 1511 | |
| 1512 | |
| 1513 | // NOTE: the VDR_IQ*_Q8_1_MMVQ values deliberately differ from the identically named CUDA constants |
| 1514 | // (vecdotq.cuh): the SYCL kernels pair them with a halved qi (e.g. QI3_S/2), so the values are not |
| 1515 | // interchangeable and must not be copied across backends. |
| 1516 | #define VDR_IQ2_XXS_Q8_1_MMVQ 1 |
| 1517 | |
| 1518 | static __dpct_inline__ uint32_t iq_sign_mask(const uint32_t signs) { |
| 1519 | const uint32_t bits = |
| 1520 | (signs & 0x01) | ((signs & 0x02) << 7) | |
| 1521 | ((signs & 0x04) << 14) | ((signs & 0x08) << 21); |
| 1522 | |
| 1523 | return bits * 0xff; |
| 1524 | } |
| 1525 | |
| 1526 | static __dpct_inline__ uint32_t iq_sign_bits(const uint32_t encoded) { |
| 1527 | const uint32_t signs = encoded & 127; |
| 1528 | |
| 1529 | return signs | ((sycl::popcount(signs) & 1) << 7); |
| 1530 | } |
| 1531 | |
| 1532 | static __dpct_inline__ float |
| 1533 | vec_dot_iq2_xxs_q8_1(const void *__restrict__ vbq, |
| 1534 | const block_q8_1 *__restrict__ bq8_1, const int &iqs, |
| 1535 | const uint64_t *iq2xxs_grid, const uint8_t *ksigns_iq2xs, |
| 1536 | const uint8_t *kmask_iq2xs) { |
| 1537 | #if QK_K == 256 |
| 1538 | const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq; |
| 1539 | |
| 1540 | const int ib32 = iqs; |
| 1541 | const uint16_t * q2 = bq2->qs + 4*ib32; |
| 1542 | const uint8_t * aux8 = (const uint8_t *)q2; |
| 1543 | const int8_t * q8 = bq8_1[ib32].qs; |
| 1544 | uint32_t aux32 = q2[2] | (q2[3] << 16); |
| 1545 | int sumi = 0; |
| 1546 | for (int l = 0; l < 4; ++l) { |
| 1547 | const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]); |
| 1548 | const uint8_t signs = ksigns_iq2xs[aux32 & 127]; |
| 1549 | for (int j = 0; j < 8; ++j) { |
| 1550 | sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1); |
| 1551 | } |
| 1552 | q8 += 8; |
| 1553 | aux32 >>= 7; |
| 1554 | } |
| 1555 | const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.25f; |
| 1556 | return d * sumi; |
| 1557 | #else |
| 1558 | assert(false); |
| 1559 | return 0.f; |
| 1560 | #endif |
| 1561 | } |
| 1562 | |
| 1563 | #define VDR_IQ2_XS_Q8_1_MMVQ 1 |
| 1564 | |
| 1565 | static __dpct_inline__ float |
| 1566 | vec_dot_iq2_xs_q8_1(const void *__restrict__ vbq, |
| 1567 | const block_q8_1 *__restrict__ bq8_1, const int &iqs, |
| 1568 | const uint64_t *iq2xs_grid) { |
| 1569 | #if DPCT_COMPATIBILITY_TEMP >= \ |
| 1570 | MIN_CC_DP4A // lowest compute capability for integer intrinsics |
| 1571 | #if QK_K == 256 |
| 1572 | const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq; |
| 1573 | |
| 1574 | const int ib32 = iqs; |
| 1575 | const uint16_t * q2 = bq2->qs + 4*ib32; |
| 1576 | const int8_t * q8 = bq8_1[ib32].qs; |
| 1577 | const uint8_t ls1 = bq2->scales[ib32] & 0xf; |
| 1578 | const uint8_t ls2 = bq2->scales[ib32] >> 4; |
| 1579 | int sumi1 = 0; |
| 1580 | #pragma unroll |
| 1581 | for (int l = 0; l < 2; ++l) { |
| 1582 | const uint64_t grid = iq2xs_grid[q2[l] & 511]; |
| 1583 | const uint32_t sign_bits = iq_sign_bits(q2[l] >> 9); |
| 1584 | const uint32_t signs0 = iq_sign_mask(sign_bits); |
| 1585 | const uint32_t signs1 = iq_sign_mask(sign_bits >> 4); |
| 1586 | const int grid_l = ((uint32_t)grid ^ signs0) + (signs0 & 0x01010101); |
| 1587 | const int grid_h = ((uint32_t)(grid >> 32) ^ signs1) + (signs1 & 0x01010101); |
| 1588 | sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1); |
| 1589 | sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1); |
| 1590 | q8 += 8; |
| 1591 | } |
| 1592 | int sumi2 = 0; |
| 1593 | #pragma unroll |
| 1594 | for (int l = 2; l < 4; ++l) { |
| 1595 | const uint64_t grid = iq2xs_grid[q2[l] & 511]; |
| 1596 | const uint32_t sign_bits = iq_sign_bits(q2[l] >> 9); |
| 1597 | const uint32_t signs0 = iq_sign_mask(sign_bits); |
| 1598 | const uint32_t signs1 = iq_sign_mask(sign_bits >> 4); |
| 1599 | const int grid_l = ((uint32_t)grid ^ signs0) + (signs0 & 0x01010101); |
| 1600 | const int grid_h = ((uint32_t)(grid >> 32) ^ signs1) + (signs1 & 0x01010101); |
| 1601 | sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2); |
| 1602 | sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2); |
| 1603 | q8 += 8; |
| 1604 | } |
| 1605 | const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f; |
| 1606 | return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2); |
| 1607 | #else |
| 1608 | assert(false); |
| 1609 | return 0.f; |
| 1610 | #endif |
| 1611 | #else |
| 1612 | assert(false); |
| 1613 | return 0.f; |
| 1614 | #endif |
| 1615 | } |
| 1616 | |
| 1617 | #define VDR_IQ2_S_Q8_1_MMVQ 1 |
| 1618 | |
| 1619 | static __dpct_inline__ float |
| 1620 | vec_dot_iq2_s_q8_1(const void *__restrict__ vbq, |
| 1621 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1622 | #if QK_K == 256 |
| 1623 | const block_iq2_s * bq2 = (const block_iq2_s *) vbq; |
| 1624 | |
| 1625 | const int ib32 = iqs; |
| 1626 | const int8_t * q8 = bq8_1[ib32].qs; |
| 1627 | const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32; |
| 1628 | const uint8_t ls1 = bq2->scales[ib32] & 0xf; |
| 1629 | const uint8_t ls2 = bq2->scales[ib32] >> 4; |
| 1630 | int sumi1 = 0; |
| 1631 | for (int l = 0; l < 2; ++l) { |
| 1632 | const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300))); |
| 1633 | const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>( |
| 1634 | ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201, |
| 1635 | std::equal_to<>()); |
| 1636 | const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>( |
| 1637 | ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201, |
| 1638 | std::equal_to<>()); |
| 1639 | const int grid_l = dpct::vectorized_binary<sycl::uchar4>( |
| 1640 | grid[0] ^ signs0, signs0, std::minus<>()); |
| 1641 | const int grid_h = dpct::vectorized_binary<sycl::uchar4>( |
| 1642 | grid[1] ^ signs1, signs1, std::minus<>()); |
| 1643 | sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1); |
| 1644 | sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1); |
| 1645 | q8 += 8; |
| 1646 | } |
| 1647 | int sumi2 = 0; |
| 1648 | for (int l = 2; l < 4; ++l) { |
| 1649 | const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300))); |
| 1650 | const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>( |
| 1651 | ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201, |
| 1652 | std::equal_to<>()); |
| 1653 | const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>( |
| 1654 | ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201, |
| 1655 | std::equal_to<>()); |
| 1656 | const int grid_l = dpct::vectorized_binary<sycl::uchar4>( |
| 1657 | grid[0] ^ signs0, signs0, std::minus<>()); |
| 1658 | const int grid_h = dpct::vectorized_binary<sycl::uchar4>( |
| 1659 | grid[1] ^ signs1, signs1, std::minus<>()); |
| 1660 | sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2); |
| 1661 | sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2); |
| 1662 | q8 += 8; |
| 1663 | } |
| 1664 | const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f; |
| 1665 | return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2); |
| 1666 | #else |
| 1667 | assert(false); |
| 1668 | #endif |
| 1669 | } |
| 1670 | |
| 1671 | #define VDR_IQ3_XXS_Q8_1_MMVQ 1 |
| 1672 | |
| 1673 | static __dpct_inline__ float |
| 1674 | vec_dot_iq3_xxs_q8_1(const void *__restrict__ vbq, |
| 1675 | const block_q8_1 *__restrict__ bq8_1, const int &iqs, |
| 1676 | const uint32_t *iq3xxs_grid) { |
| 1677 | #if DPCT_COMPATIBILITY_TEMP >= \ |
| 1678 | MIN_CC_DP4A // lowest compute capability for integer intrinsics |
| 1679 | #if QK_K == 256 |
| 1680 | const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq; |
| 1681 | |
| 1682 | const int ib32 = iqs; |
| 1683 | const uint8_t * q3 = bq2->qs + 8*ib32; |
| 1684 | const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32; |
| 1685 | const int8_t * q8 = bq8_1[ib32].qs; |
| 1686 | uint32_t aux32 = gas[0] | (gas[1] << 16); |
| 1687 | int sumi = 0; |
| 1688 | #pragma unroll |
| 1689 | for (int l = 0; l < 4; ++l) { |
| 1690 | const uint32_t grid1 = iq3xxs_grid[q3[2*l+0]]; |
| 1691 | const uint32_t grid2 = iq3xxs_grid[q3[2*l+1]]; |
| 1692 | const uint32_t sign_bits = iq_sign_bits(aux32); |
| 1693 | const uint32_t signs0 = iq_sign_mask(sign_bits); |
| 1694 | const uint32_t signs1 = iq_sign_mask(sign_bits >> 4); |
| 1695 | const int grid_l = (grid1 ^ signs0) + (signs0 & 0x01010101); |
| 1696 | const int grid_h = (grid2 ^ signs1) + (signs1 & 0x01010101); |
| 1697 | sumi = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi); |
| 1698 | sumi = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi); |
| 1699 | q8 += 8; |
| 1700 | aux32 >>= 7; |
| 1701 | } |
| 1702 | const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.5f; |
| 1703 | return d * sumi; |
| 1704 | #else |
| 1705 | assert(false); |
| 1706 | return 0.f; |
| 1707 | #endif |
| 1708 | #else |
| 1709 | assert(false); |
| 1710 | return 0.f; |
| 1711 | #endif |
| 1712 | } |
| 1713 | |
| 1714 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_IQ3_XXS> { |
| 1715 | static constexpr ggml_type gtype = GGML_TYPE_IQ3_XXS; |
| 1716 | |
| 1717 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 1718 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 1719 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 1720 | #if DPCT_COMPATIBILITY_TEMP >= MIN_CC_DP4A && QK_K == 256 |
| 1721 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 1722 | const uint8_t * qs = base + ibx_offset.first; |
| 1723 | const uint8_t * q3 = qs + 8 * iqs; |
| 1724 | const uint16_t * gas = reinterpret_cast<const uint16_t *>(qs + QK_K / 4) + 2 * iqs; |
| 1725 | const int8_t * q8 = q8_1_quant_ptr + iqs * QK8_1; |
| 1726 | uint32_t aux32 = gas[0] | (gas[1] << 16); |
| 1727 | |
| 1728 | int sumi = 0; |
| 1729 | #pragma unroll |
| 1730 | for (int l = 0; l < 4; ++l) { |
| 1731 | const uint32_t grid1 = iq3xxs_grid[q3[2*l+0]]; |
| 1732 | const uint32_t grid2 = iq3xxs_grid[q3[2*l+1]]; |
| 1733 | const uint32_t sign_bits = iq_sign_bits(aux32); |
| 1734 | const uint32_t signs0 = iq_sign_mask(sign_bits); |
| 1735 | const uint32_t signs1 = iq_sign_mask(sign_bits >> 4); |
| 1736 | const int grid_l = (grid1 ^ signs0) + (signs0 & 0x01010101); |
| 1737 | const int grid_h = (grid2 ^ signs1) + (signs1 & 0x01010101); |
| 1738 | sumi = dpct::dp4a(grid_l, *((const int *) q8 + 0), sumi); |
| 1739 | sumi = dpct::dp4a(grid_h, *((const int *) q8 + 1), sumi); |
| 1740 | q8 += 8; |
| 1741 | aux32 >>= 7; |
| 1742 | } |
| 1743 | |
| 1744 | const ggml_half d = *reinterpret_cast<const ggml_half *>(base + d_offset.first); |
| 1745 | return (float) d * (0.5f + aux32) * q8_1_ds[iqs][0] * 0.5f * sumi; |
| 1746 | #else |
| 1747 | GGML_UNUSED(vbq); |
| 1748 | GGML_UNUSED(ibx_offset); |
| 1749 | GGML_UNUSED(d_offset); |
| 1750 | GGML_UNUSED(q8_1_quant_ptr); |
| 1751 | GGML_UNUSED(q8_1_ds); |
| 1752 | GGML_UNUSED(iqs); |
| 1753 | GGML_ABORT("IQ3_XXS reorder vec dot requires dp4a and QK_K == 256"); |
| 1754 | #endif |
| 1755 | } |
| 1756 | }; |
| 1757 | |
| 1758 | #define VDR_IQ3_S_Q8_1_MMVQ 1 |
| 1759 | |
| 1760 | static __dpct_inline__ float |
| 1761 | vec_dot_iq3_s_q8_1(const void *__restrict__ vbq, |
| 1762 | const block_q8_1 *__restrict__ bq8_1, const int &iqs, |
| 1763 | const uint32_t *iq3s_grid) { |
| 1764 | #if QK_K == 256 |
| 1765 | const block_iq3_s * bq2 = (const block_iq3_s *) vbq; |
| 1766 | |
| 1767 | const int ib32 = iqs; |
| 1768 | const uint8_t * qs = bq2->qs + 8*ib32; |
| 1769 | const int8_t * q8 = bq8_1[ib32].qs; |
| 1770 | int sumi = 0; |
| 1771 | for (int l = 0; l < 4; ++l) { |
| 1772 | const uint32_t grid1 = iq3s_grid[qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256)]; |
| 1773 | const uint32_t grid2 = iq3s_grid[qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256)]; |
| 1774 | const uint32_t signs = bq2->signs[4 * ib32 + l]; |
| 1775 | const uint32_t signs0 = iq_sign_mask(signs); |
| 1776 | const uint32_t signs1 = iq_sign_mask(signs >> 4); |
| 1777 | const int grid_l = (grid1 ^ signs0) + (signs0 & 0x01010101); |
| 1778 | const int grid_h = (grid2 ^ signs1) + (signs1 & 0x01010101); |
| 1779 | sumi = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi); |
| 1780 | sumi = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi); |
| 1781 | q8 += 8; |
| 1782 | } |
| 1783 | const float d = |
| 1784 | (float)bq2->d * |
| 1785 | (1 + 2 * ((bq2->scales[ib32 / 2] >> 4 * (ib32 % 2)) & 0xf)) * |
| 1786 | bq8_1[ib32].ds[0]; |
| 1787 | return d * sumi; |
| 1788 | #else |
| 1789 | assert(false); |
| 1790 | #endif |
| 1791 | } |
| 1792 | |
| 1793 | template <> struct reorder_vec_dot_q_sycl<GGML_TYPE_IQ3_S> { |
| 1794 | static constexpr ggml_type gtype = GGML_TYPE_IQ3_S; |
| 1795 | |
| 1796 | __dpct_inline__ float operator()(const void * __restrict__ vbq, const std::pair<int, int> ibx_offset, |
| 1797 | const std::pair<int, int> d_offset, const int8_t * q8_1_quant_ptr, |
| 1798 | const sycl::half2 * q8_1_ds, const int & iqs) { |
| 1799 | #if QK_K == 256 |
| 1800 | const uint8_t * base = static_cast<const uint8_t *>(vbq); |
| 1801 | const uint8_t * qs = base + ibx_offset.first + 8 * iqs; |
| 1802 | const uint8_t * qh = base + ibx_offset.second; |
| 1803 | const uint8_t * signs = base + d_offset.first + 4 * iqs; |
| 1804 | const uint8_t * metadata = base + d_offset.second; |
| 1805 | const ggml_half d = *reinterpret_cast<const ggml_half *>(metadata); |
| 1806 | const uint8_t * scales = metadata + sizeof(ggml_half); |
| 1807 | const int8_t * q8 = q8_1_quant_ptr + iqs * QK8_1; |
| 1808 | |
| 1809 | int sumi = 0; |
| 1810 | #pragma unroll |
| 1811 | for (int l = 0; l < 4; ++l) { |
| 1812 | const uint32_t grid1 = iq3s_grid[qs[2*l+0] | ((qh[iqs] << (8 - 2*l)) & 256)]; |
| 1813 | const uint32_t grid2 = iq3s_grid[qs[2*l+1] | ((qh[iqs] << (7 - 2*l)) & 256)]; |
| 1814 | const uint32_t signs0 = iq_sign_mask(signs[l]); |
| 1815 | const uint32_t signs1 = iq_sign_mask(signs[l] >> 4); |
| 1816 | const int grid_l = (grid1 ^ signs0) + (signs0 & 0x01010101); |
| 1817 | const int grid_h = (grid2 ^ signs1) + (signs1 & 0x01010101); |
| 1818 | sumi = dpct::dp4a(grid_l, *((const int *) q8 + 0), sumi); |
| 1819 | sumi = dpct::dp4a(grid_h, *((const int *) q8 + 1), sumi); |
| 1820 | q8 += 8; |
| 1821 | } |
| 1822 | |
| 1823 | const float scale = 1 + 2 * ((scales[iqs / 2] >> (4 * (iqs % 2))) & 0xf); |
| 1824 | return (float) d * scale * q8_1_ds[iqs][0] * sumi; |
| 1825 | #else |
| 1826 | GGML_UNUSED(vbq); |
| 1827 | GGML_UNUSED(ibx_offset); |
| 1828 | GGML_UNUSED(d_offset); |
| 1829 | GGML_UNUSED(q8_1_quant_ptr); |
| 1830 | GGML_UNUSED(q8_1_ds); |
| 1831 | GGML_UNUSED(iqs); |
| 1832 | GGML_ABORT("IQ3_S reorder vec dot not supported for QK_K != 256"); |
| 1833 | #endif |
| 1834 | } |
| 1835 | }; |
| 1836 | |
| 1837 | #define VDR_IQ1_S_Q8_1_MMVQ 1 |
| 1838 | |
| 1839 | static __dpct_inline__ float |
| 1840 | vec_dot_iq1_s_q8_1(const void *__restrict__ vbq, |
| 1841 | const block_q8_1 *__restrict__ bq8_1, const int &iqs, |
| 1842 | const uint32_t *iq1s_grid_gpu) { |
| 1843 | #if QK_K == 256 |
| 1844 | const block_iq1_s * bq1 = (const block_iq1_s *) vbq; |
| 1845 | |
| 1846 | const int ib32 = iqs; |
| 1847 | int sumi = 0; |
| 1848 | const int * q8 = (const int *)bq8_1[ib32].qs; |
| 1849 | for (int l = 0; l < 4; ++l) { |
| 1850 | const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8))); |
| 1851 | int grid0 = grid[0] & 0x0f0f0f0f; |
| 1852 | int grid1 = (grid[0] >> 4) & 0x0f0f0f0f; |
| 1853 | sumi = dpct::dp4a(q8[2 * l + 1], grid1, |
| 1854 | dpct::dp4a(q8[2 * l + 0], grid0, sumi)); |
| 1855 | } |
| 1856 | |
| 1857 | const float delta = bq1->qh[ib32] & 0x8000 ? -1-IQ1S_DELTA : -1+IQ1S_DELTA; |
| 1858 | const float d1q = (float)bq1->d * (2*((bq1->qh[ib32] >> 12) & 7) + 1); |
| 1859 | const float d = d1q * bq8_1[ib32].ds[0]; |
| 1860 | const float m = d1q * bq8_1[ib32].ds[1]; |
| 1861 | return d * sumi + m * delta; |
| 1862 | #else |
| 1863 | assert(false); |
| 1864 | #endif |
| 1865 | } |
| 1866 | |
| 1867 | #define VDR_IQ1_M_Q8_1_MMVQ 1 |
| 1868 | |
| 1869 | static __dpct_inline__ float |
| 1870 | vec_dot_iq1_m_q8_1(const void *__restrict__ vbq, |
| 1871 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1872 | #if QK_K == 256 |
| 1873 | const block_iq1_m * bq1 = (const block_iq1_m *) vbq; |
| 1874 | |
| 1875 | const int ib32 = iqs; |
| 1876 | int sumi[2] = {0, 0}; |
| 1877 | float sumf[2] = {0.f, 0.f}; |
| 1878 | |
| 1879 | const int * q8 = (const int *)bq8_1[ib32].qs; |
| 1880 | for (int l = 0; l < 4; ++l) { |
| 1881 | const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 7) << 8))); |
| 1882 | int grid0 = grid[0] & 0x0f0f0f0f; |
| 1883 | int grid1 = (grid[0] >> 4) & 0x0f0f0f0f; |
| 1884 | sumi[l / 2] = dpct::dp4a(q8[2 * l + 1], grid1, |
| 1885 | dpct::dp4a(q8[2 * l + 0], grid0, sumi[l / 2])); |
| 1886 | const float delta = (bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 0x08 ? -1-IQ1M_DELTA : -1+IQ1M_DELTA; |
| 1887 | const int sumy = dpct::dp4a(q8[2 * l + 1], 0x01010101, |
| 1888 | dpct::dp4a(q8[2 * l + 0], 0x01010101, 0)); |
| 1889 | sumf[l/2] += delta*sumy; |
| 1890 | } |
| 1891 | |
| 1892 | iq1m_scale_t scale; |
| 1893 | const uint16_t * sc = (const uint16_t *)bq1->scales; |
| 1894 | scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000); |
| 1895 | const float d = (float)scale.f16 * bq8_1[ib32].ds[0]; |
| 1896 | return d * ((sumi[0] + sumf[0]) * (2*((sc[ib32/2] >> 6*(ib32%2)) & 0x7) + 1) + (sumi[1] + sumf[1]) * (2*((sc[ib32/2] >> (6*(ib32%2)+3)) & 0x7) + 1)); |
| 1897 | #else |
| 1898 | assert(false); |
| 1899 | #endif |
| 1900 | } |
| 1901 | |
| 1902 | |
| 1903 | #define VDR_IQ4_NL_Q8_1_MMVQ 2 |
| 1904 | |
| 1905 | static __dpct_inline__ float |
| 1906 | vec_dot_iq4_nl_q8_1(const void *__restrict__ vbq, |
| 1907 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1908 | |
| 1909 | const block_iq4_nl * bq = (const block_iq4_nl *) vbq; |
| 1910 | |
| 1911 | const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs; |
| 1912 | const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs; |
| 1913 | |
| 1914 | const uint8_t * values = (const uint8_t *)kvalues_iq4nl; |
| 1915 | |
| 1916 | int v1, v2; |
| 1917 | int sumi1 = 0, sumi2 = 0; |
| 1918 | for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) { |
| 1919 | const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16); |
| 1920 | get_int_from_table_16(aux, values, v1, v2); |
| 1921 | sumi1 = dpct::dp4a(v1, q8[l + 0], sumi1); |
| 1922 | sumi2 = dpct::dp4a(v2, q8[l + 4], sumi2); |
| 1923 | } |
| 1924 | |
| 1925 | const float d = (float)bq->d * bq8_1->ds[0]; |
| 1926 | return d * (sumi1 + sumi2); |
| 1927 | } |
| 1928 | |
| 1929 | |
| 1930 | #define VDR_IQ4_XS_Q8_1_MMVQ 1 |
| 1931 | |
| 1932 | static __dpct_inline__ float |
| 1933 | vec_dot_iq4_xs_q8_1(const void *__restrict__ vbq, |
| 1934 | const block_q8_1 *__restrict__ bq8_1, const int &iqs) { |
| 1935 | |
| 1936 | #if QK_K == 256 |
| 1937 | const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq; |
| 1938 | const uint8_t * values = (const uint8_t *)kvalues_iq4nl; |
| 1939 | |
| 1940 | // iqs is 0...7 |
| 1941 | const int ib32 = iqs; |
| 1942 | const int32_t * q8 = (const int *)bq8_1[ib32].qs; |
| 1943 | const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32; |
| 1944 | const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4); |
| 1945 | const float d = (float)bq4->d * (ls - 32) * bq8_1[ib32].ds[0]; |
| 1946 | int v1, v2; |
| 1947 | int sumi1 = 0, sumi2 = 0; |
| 1948 | for (int j = 0; j < 4; ++j) { |
| 1949 | get_int_from_table_16(q4[j], values, v1, v2); |
| 1950 | sumi1 = dpct::dp4a(v1, q8[j + 0], sumi1); |
| 1951 | sumi2 = dpct::dp4a(v2, q8[j + 4], sumi2); |
| 1952 | } |
| 1953 | return d * (sumi1 + sumi2); |
| 1954 | #else |
| 1955 | assert(false); |
| 1956 | #endif |
| 1957 | } |
| 1958 | |
| 1959 | #endif // GGML_SYCL_VECDOTQ_HPP |