Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cuda/vecdotq.cuh # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / vecdotq.cuh` 1443 lines ยท 52877 bytes Raw Blame History 1 #pragma once 2 3 #include "common.cuh" 4 5 #include 6 7 static __device__ __forceinline__ int get_int_b1(const void * x, const int & i32) { 8 const uint8_t * x8 = (const uint8_t *) x; 9 10 int x32 = x8[4*i32 + 0] << 0; 11 x32 |= x8[4*i32 + 1] << 8; 12 x32 |= x8[4*i32 + 2] << 16; 13 x32 |= x8[4*i32 + 3] << 24; 14 15 return x32; 16 } 17 18 static __device__ __forceinline__ int get_int_b2(const void * x, const int & i32) { 19 const uint16_t * x16 = (const uint16_t *) x; // assume at least 2 byte alignment 20 21 int x32 = x16[2*i32 + 0] << 0; 22 x32 |= x16[2*i32 + 1] << 16; 23 24 return x32; 25 } 26 27 static __device__ __forceinline__ int get_int_b4(const void * x, const int & i32) { 28 return ((const int *) x)[i32]; // assume at least 4 byte alignment 29 } 30 31 // q4 contains 8 indices with 4 bit each. 32 // This function selects those bytes from table that are at those indices and returns them as int2. 33 // The first int contains the bytes with even indices in q4, the second int contains the bytes with odd indices in q4. 34 static __device__ __forceinline__ int2 get_int_from_table_16(const int & q4, const int8_t * table) { 35 #if defined(GGML_USE_HIP) 36 // Load the 16-byte table into four 32-bit unsigned integers. 37 const uint32_t *values = (const uint32_t *)table; 38 39 const uint32_t q_even = q4; 40 const uint32_t q_odd = (q4 >> 4); 41 42 // Perform lookups in the lower half of the table (indices 0-7). 43 uint32_t v_even_low = ggml_hip_perm(values[1], values[0], q_even & 0x07070707); 44 uint32_t v_odd_low = ggml_hip_perm(values[1], values[0], q_odd & 0x07070707); 45 46 // Perform lookups in the upper half of the table (indices 8-15). 47 uint32_t v_even_high = ggml_hip_perm(values[3], values[2], q_even & 0x07070707); 48 uint32_t v_odd_high = ggml_hip_perm(values[3], values[2], q_odd & 0x07070707); 49 50 // Select between the low and high results based on the MSB of each index nibble. 51 uint32_t mask_even = 0x03020100 | ((q_even & 0x08080808) >> 1); 52 uint32_t res_x = ggml_hip_perm(v_even_high, v_even_low, mask_even); 53 uint32_t mask_odd = 0x03020100 | ((q_odd & 0x08080808) >> 1); 54 uint32_t res_y = ggml_hip_perm(v_odd_high, v_odd_low, mask_odd); 55 56 return make_int2(res_x, res_y); 57 #elif !defined(GGML_USE_MUSA) 58 // CUDA does not have an instruction for selecting bytes with 4 bit indices. 59 // However, __byte_perm is an instruction that selects bytes with 3 bit indices that can be used instead. 60 const uint32_t * table32 = (const uint32_t *) table; 61 62 // __byte_perm selects bytes based on the lower 16 bits in its third argument. 63 // Therefore, do 2 iterations over the 32 bits in q4 with 0 and 16 shift. 64 // To handle the fourth bit, first call _byte_perm both for the low and the high 64 bit of table, using the low 3 bits. 65 // Then, call __byte_perm again to select from the low and high bytes based on the fourth bit. 66 uint32_t tmp[2]; 67 const uint32_t low_high_selection_indices = (0x32103210 | ((q4 & 0x88888888) >> 1)); 68 #pragma unroll 69 for (uint32_t i = 0; i < 2; ++i) { 70 const uint32_t shift = 16 * i; 71 72 const uint32_t low = __byte_perm(table32[0], table32[1], q4 >> shift); 73 const uint32_t high = __byte_perm(table32[2], table32[3], q4 >> shift); 74 tmp[i] = __byte_perm(low, high, low_high_selection_indices >> shift); 75 } 76 77 // tmp contains the bytes from tyble in the same order as the 4 bit indices in q4. 78 // However, for the result we need ints with all even/odd 4 bit indices in q4. 79 // Therefore, 2 more calls to __byte_perm to put the bytes in the correct order. 80 return make_int2(__byte_perm(tmp[0], tmp[1], 0x6420), __byte_perm(tmp[0], tmp[1], 0x7531)); 81 #else 82 // Generic implementation. 83 const int q0_32 = (q4 >> 0) & 0x0F0F0F0F; 84 const int8_t * q0_8 = (const int8_t *) &q0_32; 85 const char4 val0_8 = make_char4( 86 table[q0_8[0]], table[q0_8[1]], table[q0_8[2]], table[q0_8[3]]); 87 88 const int q1_32 = (q4 >> 4) & 0x0F0F0F0F; 89 const int8_t * q1_8 = (const int8_t *) &q1_32; 90 const char4 val1_8 = make_char4( 91 table[q1_8[0]], table[q1_8[1]], table[q1_8[2]], table[q1_8[3]]); 92 93 return make_int2(*((const int *) &val0_8), *((const int *) &val1_8)); 94 #endif 95 } 96 97 static __device__ __forceinline__ uint32_t unpack_ksigns(const uint8_t v) { 98 // v is a 7 bit int, with the 8th sign being encodable as popcnt 99 // with xor we can "correct" the bit instead of having to mask 100 const uint32_t p = __popc(v) & 1; 101 const uint32_t s = v ^ p << 7; 102 // broadcast over uint to allow for 0x08040201 / 0x80402010 as selectors 103 return s * 0x01010101; 104 } 105 106 // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called 107 // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q 108 109 #define VDR_Q1_0_Q8_1_MMVQ 1 // Process one 32-element chunk at a time for parallelism 110 #define VDR_Q1_0_Q8_1_MMQ 4 // Q1_0 has 128 bits (4 ints) per block 111 112 #define VDR_Q2_0_Q8_1_MMVQ 1 // Process one 32-element chunk at a time for parallelism 113 #define VDR_Q2_0_Q8_1_MMQ 2 // Q2_0 group 64: 128 bits (4 ints) per block, 2 32-element chunks 114 115 #define VDR_Q4_0_Q8_1_MMVQ 2 116 #define VDR_Q4_0_Q8_1_MMQ 4 117 118 template static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl( 119 const int * v, const int * u, const float & d4, const half2 & ds8) { 120 121 int sumi = 0; 122 123 #pragma unroll 124 for (int i = 0; i < vdr; ++i) { 125 const int vi0 = (v[i] >> 0) & 0x0F0F0F0F; 126 const int vi1 = (v[i] >> 4) & 0x0F0F0F0F; 127 128 // SIMD dot product of quantized values 129 sumi = ggml_cuda_dp4a(vi0, u[2*i+0], sumi); 130 sumi = ggml_cuda_dp4a(vi1, u[2*i+1], sumi); 131 } 132 133 const float2 ds8f = __half22float2(ds8); 134 135 // second part effectively subtracts 8 from each quant value 136 return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y); 137 } 138 139 #define VDR_Q4_1_Q8_1_MMVQ 2 140 #define VDR_Q4_1_Q8_1_MMQ 4 141 142 template static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl( 143 const int * v, const int * u, const half2 & dm4, const half2 & ds8) { 144 145 int sumi = 0; 146 147 #pragma unroll 148 for (int i = 0; i < vdr; ++i) { 149 const int vi0 = (v[i] >> 0) & 0x0F0F0F0F; 150 const int vi1 = (v[i] >> 4) & 0x0F0F0F0F; 151 152 // SIMD dot product of quantized values 153 sumi = ggml_cuda_dp4a(vi0, u[2*i+0], sumi); 154 sumi = ggml_cuda_dp4a(vi1, u[2*i+1], sumi); 155 } 156 157 #ifdef FAST_FP16_AVAILABLE 158 const float2 tmp = __half22float2(__hmul2(dm4, ds8)); 159 const float d4d8 = tmp.x; 160 const float m4s8 = tmp.y; 161 #else 162 const float2 dm4f = __half22float2(dm4); 163 const float2 ds8f = __half22float2(ds8); 164 const float d4d8 = dm4f.x * ds8f.x; 165 const float m4s8 = dm4f.y * ds8f.y; 166 #endif // FAST_FP16_AVAILABLE 167 168 // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it 169 return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1)); 170 } 171 172 #define VDR_Q5_0_Q8_1_MMVQ 2 173 #define VDR_Q5_0_Q8_1_MMQ 4 174 175 template static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl( 176 const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) { 177 178 int sumi = 0; 179 180 #pragma unroll 181 for (int i = 0; i < vdr; ++i) { 182 int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits 183 vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4 184 vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12 185 vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20 186 vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28 187 sumi = ggml_cuda_dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values 188 189 int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits 190 vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4 191 vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12 192 vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20 193 vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28 194 sumi = ggml_cuda_dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values 195 } 196 197 const float2 ds8f = __half22float2(ds8); 198 199 // second part effectively subtracts 16 from each quant value 200 return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y); 201 } 202 203 #define VDR_Q5_1_Q8_1_MMVQ 2 204 #define VDR_Q5_1_Q8_1_MMQ 4 205 206 template static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl( 207 const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) { 208 209 int sumi = 0; 210 211 #pragma unroll 212 for (int i = 0; i < vdr; ++i) { 213 int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits 214 vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4 215 vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12 216 vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20 217 vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28 218 sumi = ggml_cuda_dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values 219 220 int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits 221 vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4 222 vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12 223 vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20 224 vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28 225 sumi = ggml_cuda_dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values 226 } 227 228 #ifdef FAST_FP16_AVAILABLE 229 const float2 tmp = __half22float2(__hmul2(dm5, ds8)); 230 const float d5d8 = tmp.x; 231 const float m5s8 = tmp.y; 232 #else 233 const float2 dm5f = __half22float2(dm5); 234 const float2 ds8f = __half22float2(ds8); 235 const float d5d8 = dm5f.x * ds8f.x; 236 const float m5s8 = dm5f.y * ds8f.y; 237 #endif // FAST_FP16_AVAILABLE 238 239 // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it 240 return sumi*d5d8 + m5s8 / (QI5_1 / vdr); 241 } 242 243 #define VDR_Q8_0_Q8_1_MMVQ 2 244 #define VDR_Q8_0_Q8_1_MMQ 8 245 246 template static __device__ __forceinline__ T vec_dot_q8_0_q8_1_impl( 247 const int * v, const int * u, const T & d8_0, const T & d8_1) { 248 249 int sumi = 0; 250 251 #pragma unroll 252 for (int i = 0; i < vdr; ++i) { 253 // SIMD dot product of quantized values 254 sumi = ggml_cuda_dp4a(v[i], u[i], sumi); 255 } 256 257 return d8_0*d8_1 * ((T) sumi); 258 } 259 260 template static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl( 261 const int * v, const int * u, const half2 & dm8, const half2 & ds8) { 262 263 int sumi = 0; 264 265 #pragma unroll 266 for (int i = 0; i < vdr; ++i) { 267 // SIMD dot product of quantized values 268 sumi = ggml_cuda_dp4a(v[i], u[i], sumi); 269 } 270 271 #ifdef FAST_FP16_AVAILABLE 272 const float2 tmp = __half22float2(__hmul2(dm8, ds8)); 273 const float d8d8 = tmp.x; 274 const float m8s8 = tmp.y; 275 #else 276 const float2 dm8f = __half22float2(dm8); 277 const float2 ds8f = __half22float2(ds8); 278 const float d8d8 = dm8f.x * ds8f.x; 279 const float m8s8 = dm8f.y * ds8f.y; 280 #endif // FAST_FP16_AVAILABLE 281 282 // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it 283 return sumi*d8d8 + m8s8 / (QI8_1 / vdr); 284 } 285 286 template static __device__ __forceinline__ float vec_dot_q8_0_16_q8_1_impl( 287 const int * v, const int * u, const float * d8_0, const float & d8_1) { 288 289 float sumf = 0.0f; 290 291 #pragma unroll 292 for (int i0 = 0; i0 < vdr; i0 += QI8_0/2) { 293 int sumi = 0; 294 295 #pragma unroll 296 for (int i = i0; i < i0 + QI8_0/2; ++i) { 297 // SIMD dot product of quantized values 298 sumi = ggml_cuda_dp4a(v[i], u[i], sumi); 299 } 300 301 sumf += d8_0[i0/(QI8_0/2)]*sumi; 302 } 303 304 return d8_1*sumf; 305 } 306 307 #define VDR_MXFP4_Q8_1_MMVQ 2 308 #define VDR_MXFP4_Q8_1_MMQ 4 309 310 static __device__ __forceinline__ float vec_dot_mxfp4_q8_1( 311 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 312 313 const block_mxfp4 * bq4 = (const block_mxfp4 *) vbq + kbx; 314 315 const int * q8 = (const int *) bq8_1->qs + iqs; 316 317 int sumi = 0; 318 #pragma unroll 319 for (int l = 0; l < VDR_MXFP4_Q8_1_MMVQ; ++l) { 320 const int aux_q4 = get_int_b1(bq4->qs, iqs + l); 321 const int2 v = get_int_from_table_16(aux_q4, kvalues_mxfp4); 322 323 sumi = ggml_cuda_dp4a(v.x, q8[l + 0], sumi); 324 sumi = ggml_cuda_dp4a(v.y, q8[l + 4], sumi); 325 } 326 327 const float d = ggml_cuda_e8m0_to_fp32(bq4->e) * 0.5f * __low2float(bq8_1->ds); 328 return d * sumi; 329 } 330 331 #define VDR_NVFP4_Q8_1_MMVQ 4 332 #define VDR_NVFP4_Q8_1_MMQ 8 333 334 static __device__ __forceinline__ float vec_dot_nvfp4_q8_1( 335 const void * __restrict__ vbq, 336 const block_q8_1 * __restrict__ bq8_1, 337 const int32_t & kbx, 338 const int32_t & iqs) { 339 340 const block_nvfp4 * bq4 = (const block_nvfp4 *) vbq + kbx; 341 float sum = 0.0f; 342 #pragma unroll 343 for (int i = 0; i < VDR_NVFP4_Q8_1_MMVQ/2; i++) { 344 const int32_t iqs0 = iqs + 2*i; 345 const int32_t iqs1 = iqs0 + 1; 346 const int32_t is = iqs0 >> 1; 347 const int2 v0 = get_int_from_table_16(get_int_b4(bq4->qs, iqs0), kvalues_mxfp4); 348 const int2 v1 = get_int_from_table_16(get_int_b4(bq4->qs, iqs1), kvalues_mxfp4); 349 const block_q8_1 * bq8 = bq8_1 + (is >> 1); 350 const int32_t i8 = ((is & 1) << 2); 351 352 int sumi = ggml_cuda_dp4a(v0.x, get_int_b4(bq8->qs, i8 + 0), 0); 353 sumi = ggml_cuda_dp4a(v0.y, get_int_b4(bq8->qs, i8 + 2), sumi); 354 sumi = ggml_cuda_dp4a(v1.x, get_int_b4(bq8->qs, i8 + 1), sumi); 355 sumi = ggml_cuda_dp4a(v1.y, get_int_b4(bq8->qs, i8 + 3), sumi); 356 357 const float d = ggml_cuda_ue4m3_to_fp32(bq4->d[is]) * __low2float(bq8->ds); 358 sum += d * float(sumi); 359 } 360 361 return sum; 362 } 363 #define VDR_Q2_K_Q8_1_MMVQ 2 364 #define VDR_Q2_K_Q8_1_MMQ 4 365 366 // contiguous v/x values 367 static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq( 368 const int * __restrict__ v, const int * __restrict__ u, const int * __restrict__ scales, 369 const half2 & dm2, const float * __restrict__ d8) { 370 371 float sumf_d = 0.0f; 372 float sumf_m = 0.0f; 373 374 #pragma unroll 375 for (int i = 0; i < QR2_K; ++i) { 376 const int sc = scales[i/2] >> (16*(i%2)); 377 378 int sumi_d = 0; 379 int sumi_m = 0; 380 #pragma unroll 381 for (int j = 0; j < VDR_Q2_K_Q8_1_MMVQ; ++j) { 382 const int vi = (v[j] >> (2*i)) & 0x03030303; 383 384 sumi_d = ggml_cuda_dp4a(vi, u[VDR_Q2_K_Q8_1_MMVQ*i + j], sumi_d); // SIMD dot product 385 sumi_m = ggml_cuda_dp4a(0x01010101, u[VDR_Q2_K_Q8_1_MMVQ*i + j], sumi_m); // sum of q8_1 values 386 } 387 388 sumf_d += d8[i] * (sumi_d * (sc & 0xF)); 389 sumf_m += d8[i] * (sumi_m * ((sc >> 4) & 0xF)); // multiply constant q2_K part with sum of q8_1 values 390 } 391 392 const float2 dm2f = __half22float2(dm2); 393 394 return dm2f.x*sumf_d - dm2f.y*sumf_m; 395 } 396 397 // contiguous v/x + u/y values 398 template 399 static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq( 400 const int * __restrict__ v, const int * __restrict__ u, const half2 * dm2, const float & d8, const half2 * s8) { 401 402 float sumf = 0.0f; 403 float sumf_d8 = 0.0f; 404 405 #pragma unroll 406 for (int i0 = 0; i0 < QR2_K*VDR_Q2_K_Q8_1_MMQ; i0 += QI8_1) { 407 const float2 dm2f0 = __half22float2(dm2[i0/(QI8_1/2) + 0]); 408 int sumi_d0 = 0; 409 410 const float2 dm2f1 = __half22float2(dm2[i0/(QI8_1/2) + 1]); 411 int sumi_d1 = 0; 412 413 #pragma unroll 414 for (int i = i0; i < i0 + QI8_1/2; ++i) { 415 sumi_d0 = ggml_cuda_dp4a(v[i], u[i], sumi_d0); 416 } 417 sumf_d8 += dm2f0.x * sumi_d0; 418 419 #pragma unroll 420 for (int i = i0 + QI8_1/2; i < i0 + QI8_1; ++i) { 421 sumi_d1 = ggml_cuda_dp4a(v[i], u[i], sumi_d1); 422 } 423 sumf_d8 += dm2f1.x * sumi_d1; 424 425 if (i0/QI8_1 < ns8) { 426 const float2 s8f = __half22float2(s8[i0/QI8_1]); 427 sumf -= dm2f0.y*s8f.x; 428 sumf -= dm2f1.y*s8f.y; 429 } else { 430 int sumi_m0 = 0; 431 #pragma unroll 432 for (int i = i0; i < i0 + QI8_1/2; ++i) { 433 sumi_m0 = ggml_cuda_dp4a(0x01010101, u[i], sumi_m0); 434 } 435 sumf_d8 -= dm2f0.y * sumi_m0; 436 437 int sumi_m1 = 0; 438 #pragma unroll 439 for (int i = i0 + QI8_1/2; i < i0 + QI8_1; ++i) { 440 sumi_m1 = ggml_cuda_dp4a(0x01010101, u[i], sumi_m1); 441 } 442 sumf_d8 -= dm2f1.y * sumi_m1; 443 } 444 } 445 446 return sumf + d8*sumf_d8; 447 } 448 449 #define VDR_Q3_K_Q8_1_MMVQ 2 450 #define VDR_Q3_K_Q8_1_MMQ 2 451 452 // contiguous v/x values 453 static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq( 454 const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const int * __restrict__ scales, 455 const float & d3, const float * __restrict__ d8) { 456 457 float sumf = 0.0f; 458 459 #pragma unroll 460 for (int i = 0; i < QR3_K; ++i) { 461 // 4 low bits from scales[0..7], 2 high bits from scales[8..11] 462 const int sc_low = (scales[i/2] >> (16*(i%2))) & 0x0F; 463 const int sc_high = (scales[2] >> (16*(i%2) + 2*(i/2))) & 0x03; 464 const int sc = (sc_low | (sc_high << 4)) - 32; 465 466 int sumi = 0; 467 #pragma unroll 468 for (int j = 0; j < VDR_Q3_K_Q8_1_MMVQ; ++j) { 469 const int vil = (vl[j] >> (2*i)) & 0x03030303; 470 const int vih = ((vh[j] >> i) << 2) & 0x04040404; 471 472 // (vil | vih) - 4 for each byte, the - 4 is done by the second dp4a 473 sumi = ggml_cuda_dp4a(vil | vih, u[VDR_Q3_K_Q8_1_MMVQ*i + j], sumi); // SIMD dot product 474 sumi = ggml_cuda_dp4a(0xFCFCFCFC, u[VDR_Q3_K_Q8_1_MMVQ*i + j], sumi); 475 } 476 477 sumf += d8[i] * (sumi * sc); 478 } 479 480 return d3 * sumf; 481 } 482 483 // contiguous v/x + u/y values 484 static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq( 485 const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales, 486 const float & d3, const float & d8) { 487 488 int sumi = 0; 489 490 #pragma unroll 491 for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) { 492 int sumi_sc = 0; 493 494 #pragma unroll 495 for (int i = i0; i < i0 + QI8_1/2; ++i) { 496 sumi_sc = ggml_cuda_dp4a(v[i], u[i], sumi_sc); // SIMD dot product 497 } 498 499 sumi += sumi_sc * scales[i0 / (QI8_1/2)]; 500 } 501 502 return d3*d8 * sumi; 503 } 504 505 #define VDR_Q4_K_Q8_1_MMVQ 2 506 #define VDR_Q4_K_Q8_1_MMQ 8 507 508 // contiguous v/x values 509 static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq( 510 const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc, 511 const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) { 512 513 float sumf_d = 0.0f; 514 float sumf_m = 0.0f; 515 516 #pragma unroll 517 for (int i = 0; i < QR4_K; ++i) { 518 const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F; 519 const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F; 520 521 const int dot1 = ggml_cuda_dp4a(v1i, u[2*i+1], ggml_cuda_dp4a(v0i, u[2*i+0], 0)); // SIMD dot product 522 const int dot2 = ggml_cuda_dp4a(0x01010101, u[2*i+1], ggml_cuda_dp4a(0x01010101, u[2*i+0], 0)); // sum of u 523 524 sumf_d += d8[i] * (dot1 * sc[i]); 525 sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values 526 } 527 528 const float2 dm4f = __half22float2(dm4); 529 530 return dm4f.x*sumf_d - dm4f.y*sumf_m; 531 } 532 533 // contiguous v/x + u/y values 534 static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq( 535 const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc, 536 const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) { 537 538 float sumf_d = 0.0f; 539 float sumf_m = 0.0f; 540 541 #pragma unroll 542 for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) { 543 int sumi_d = 0; 544 545 #pragma unroll 546 for (int j = 0; j < QI8_1; ++j) { 547 sumi_d = ggml_cuda_dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product 548 } 549 550 const float2 ds8f = __half22float2(ds8[i]); 551 552 sumf_d += ds8f.x * (sc[i] * sumi_d); 553 sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val 554 } 555 556 const float2 dm4f = __half22float2(dm4); 557 558 return dm4f.x*sumf_d - dm4f.y*sumf_m; 559 } 560 561 #define VDR_Q5_K_Q8_1_MMVQ 2 562 #define VDR_Q5_K_Q8_1_MMQ 8 563 564 // contiguous v/x values 565 static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq( 566 const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc, 567 const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) { 568 569 float sumf_d = 0.0f; 570 float sumf_m = 0.0f; 571 572 #pragma unroll 573 for (int i = 0; i < QR5_K; ++i) { 574 const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F; 575 const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F; 576 577 const int vh0i = ((vh[0] >> i) << 4) & 0x10101010; 578 const int vh1i = ((vh[1] >> i) << 4) & 0x10101010; 579 580 const int v0i = vl0i | vh0i; 581 const int v1i = vl1i | vh1i; 582 583 const int dot1 = ggml_cuda_dp4a(v0i, u[2*i+0], ggml_cuda_dp4a(v1i, u[2*i+1], 0)); // SIMD dot product 584 const int dot2 = ggml_cuda_dp4a(0x01010101, u[2*i+0], ggml_cuda_dp4a(0x01010101, u[2*i+1], 0)); // sum of u 585 586 sumf_d += d8[i] * (dot1 * sc[i]); 587 sumf_m += d8[i] * (dot2 * m[i]); 588 589 } 590 591 const float2 dm5f = __half22float2(dm5); 592 593 return dm5f.x*sumf_d - dm5f.y*sumf_m; 594 } 595 596 // contiguous v/x + u/y values 597 static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq( 598 const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc, 599 const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) { 600 601 float sumf_d = 0.0f; 602 float sumf_m = 0.0f; 603 604 #pragma unroll 605 for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) { 606 int sumi_d = 0; 607 608 #pragma unroll 609 for (int j = 0; j < QI8_1; ++j) { 610 sumi_d = ggml_cuda_dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product 611 } 612 613 const float2 ds8f = __half22float2(ds8[i]); 614 615 sumf_d += ds8f.x * (sc[i] * sumi_d); 616 sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val 617 } 618 619 const float2 dm4f = __half22float2(dm4); 620 621 return dm4f.x*sumf_d - dm4f.y*sumf_m; 622 } 623 624 #define VDR_Q6_K_Q8_1_MMVQ 2 625 #define VDR_Q6_K_Q8_1_MMQ 8 626 627 // contiguous v/x values 628 static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq( 629 const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const int8_t * __restrict__ scales, 630 const float & d, const float * __restrict__ d8) { 631 632 float sumf = 0.0f; 633 634 #pragma unroll 635 for (int i = 0; i < QR6_K; ++i) { 636 const int sc = scales[4*i]; 637 638 int sumi = 0; 639 #pragma unroll 640 for (int j = 0; j < VDR_Q6_K_Q8_1_MMVQ; ++j) { 641 const int vil = (vl[j] >> (4*i)) & 0x0F0F0F0F; 642 const int vih = ((vh[j] >> (4*i)) << 4) & 0x30303030; 643 644 // (vil | vih) - 32 for each byte, the - 32 is done by the second dp4a 645 sumi = ggml_cuda_dp4a(vil | vih, u[VDR_Q6_K_Q8_1_MMVQ*i + j], sumi); // SIMD dot product 646 sumi = ggml_cuda_dp4a(0xE0E0E0E0, u[VDR_Q6_K_Q8_1_MMVQ*i + j], sumi); 647 } 648 649 sumf += d8[i] * (sumi * sc); 650 } 651 652 return d*sumf; 653 } 654 655 // contiguous v/x + u/y values 656 static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq( 657 const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc, 658 const float & d6, const float * __restrict__ d8) { 659 660 float sumf_d = 0.0f; 661 662 const int sc_packed = get_int_b4(sc, 0); 663 const int8_t * sc_reg = (const int8_t *) &sc_packed; 664 665 #pragma unroll 666 for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) { 667 int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale 668 669 #pragma unroll 670 for (int i = i0; i < i0 + 2; ++i) { 671 sumi_d.x = ggml_cuda_dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product 672 sumi_d.x = ggml_cuda_dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product 673 674 sumi_d.y = ggml_cuda_dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product 675 sumi_d.y = ggml_cuda_dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product 676 } 677 678 sumf_d += d8[i0/4] * (sc_reg[i0/2+0]*sumi_d.x + sc_reg[i0/2+1]*sumi_d.y); 679 } 680 681 return d6 * sumf_d; 682 } 683 684 static __device__ __forceinline__ float vec_dot_q1_0_q8_1( 685 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 686 687 const block_q1_0 * bq1_0 = (const block_q1_0 *) vbq + kbx; 688 689 // Q1_0: 128 elements with ONE scale 690 // Q8_1: 32 elements per block with individual scales 691 // iqs selects which of the 4 chunks of 32 elements to process (0-3) 692 693 const float d1 = bq1_0->d; 694 const int16_t * qs = (const int16_t *) bq1_0->qs + iqs * 2; 695 696 // Process only the chunk specified by iqs 697 const block_q8_1 * bq8_1_chunk = bq8_1 + iqs; 698 699 int sumi = 0; 700 #pragma unroll 701 for (int j = 0; j < 2; ++j) { 702 const int q = qs[j]; 703 704 const int u0 = get_int_b4(bq8_1_chunk->qs, j*4+0); 705 const int u1 = get_int_b4(bq8_1_chunk->qs, j*4+1); 706 const int u2 = get_int_b4(bq8_1_chunk->qs, j*4+2); 707 const int u3 = get_int_b4(bq8_1_chunk->qs, j*4+3); 708 709 #if defined(GGML_USE_HIP) 710 const uint32_t q_bits = (uint16_t) q; 711 712 // Expand each group of four Q1_0 bits into byte selectors for v_perm_b32. 713 const uint32_t q0_bits = q_bits >> 0; 714 const uint32_t q1_bits = q_bits >> 4; 715 const uint32_t q2_bits = q_bits >> 8; 716 const uint32_t q3_bits = q_bits >> 12; 717 718 const uint32_t q0_indices = (q0_bits & 0x01) | ((q0_bits & 0x02) << 7) | ((q0_bits & 0x04) << 14) | ((q0_bits & 0x08) << 21); 719 const uint32_t q1_indices = (q1_bits & 0x01) | ((q1_bits & 0x02) << 7) | ((q1_bits & 0x04) << 14) | ((q1_bits & 0x08) << 21); 720 const uint32_t q2_indices = (q2_bits & 0x01) | ((q2_bits & 0x02) << 7) | ((q2_bits & 0x04) << 14) | ((q2_bits & 0x08) << 21); 721 const uint32_t q3_indices = (q3_bits & 0x01) | ((q3_bits & 0x02) << 7) | ((q3_bits & 0x04) << 14) | ((q3_bits & 0x08) << 21); 722 723 const int v0 = ggml_hip_perm(0x000001FF, 0x000001FF, q0_indices); 724 const int v1 = ggml_hip_perm(0x000001FF, 0x000001FF, q1_indices); 725 const int v2 = ggml_hip_perm(0x000001FF, 0x000001FF, q2_indices); 726 const int v3 = ggml_hip_perm(0x000001FF, 0x000001FF, q3_indices); 727 #else 728 // unpack crumbs into nibble indices 729 const int n0 = __byte_perm(0x11100100, 0x11100100, q >> 0); // [0, 1, 4, 5] [ 8, 9, 12, 13] 730 const int n1 = __byte_perm(0x11100100, 0x11100100, q >> 2); // [2, 3, 6, 7] [10, 11, 14, 15] 731 // unpack nibbles into byte values 732 const int s0 = __byte_perm(0x01FF, 0x01FF, n0 >> 0); 733 const int s1 = __byte_perm(0x01FF, 0x01FF, n1 >> 0); 734 const int s2 = __byte_perm(0x01FF, 0x01FF, n0 >> 16); 735 const int s3 = __byte_perm(0x01FF, 0x01FF, n1 >> 16); 736 // unshuffle values 737 const int v0 = __byte_perm(s0, s1, 0x5410); 738 const int v1 = __byte_perm(s0, s1, 0x7632); 739 const int v2 = __byte_perm(s2, s3, 0x5410); 740 const int v3 = __byte_perm(s2, s3, 0x7632); 741 #endif // defined(GGML_USE_HIP) 742 743 sumi = ggml_cuda_dp4a(v0, u0, sumi); 744 sumi = ggml_cuda_dp4a(v1, u1, sumi); 745 sumi = ggml_cuda_dp4a(v2, u2, sumi); 746 sumi = ggml_cuda_dp4a(v3, u3, sumi); 747 } 748 749 // Apply Q1_0's single scale and this chunk's Q8_1 scale 750 const float d8 = __low2float(bq8_1_chunk->ds); 751 return d1 * d8 * sumi; 752 } 753 754 static __device__ __forceinline__ float vec_dot_q2_0_q8_1( 755 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 756 757 const block_q2_0 * bq2_0 = (const block_q2_0 *) vbq + kbx; 758 759 // Q2_0 (group 64): 64 elements with ONE scale, 2 bits per element (4 elements per byte) 760 // Q8_1: 32 elements per block with individual scales 761 // iqs selects which of the 2 chunks of 32 elements to process (0-1) 762 763 const float d2 = bq2_0->d; 764 const int16_t * qs = (const int16_t *) bq2_0->qs + iqs * 4; 765 766 // Process only the chunk specified by iqs 767 const block_q8_1 * bq8_1_chunk = bq8_1 + iqs; 768 769 int sumi = 0; 770 #pragma unroll 771 for (int j = 0; j < 4; ++j) { 772 const int q = qs[j]; 773 const int u = get_int_b4(bq8_1_chunk->qs, j*2+0); 774 const int v = get_int_b4(bq8_1_chunk->qs, j*2+1); 775 776 #if defined(GGML_USE_HIP) 777 const uint32_t qx_indices = (q & 0x03) | ((q & 0x0C) << 6) | ((q & 0x30) << 12) | ((q & 0xC0) << 18); 778 const uint32_t qy_bits = q >> 8; 779 const uint32_t qy_indices = (qy_bits & 0x03) | ((qy_bits & 0x0C) << 6) | ((qy_bits & 0x30) << 12) | ((qy_bits & 0xC0) << 18); 780 const int qx = ggml_hip_perm(0x020100FF, 0x020100FF, qx_indices); 781 const int qy = ggml_hip_perm(0x020100FF, 0x020100FF, qy_indices); 782 #else 783 // unpack even and odd crumbs into byte values 784 const int qe = __byte_perm(0x020100FF, 0x020100FF, q >> 0); 785 const int qo = __byte_perm(0x020100FF, 0x020100FF, q >> 2); 786 // unshuffle values 787 const int qx = __byte_perm(qe, qo, 0x5140); 788 const int qy = __byte_perm(qe, qo, 0x7362); 789 #endif // defined(GGML_USE_HIP) 790 791 sumi = ggml_cuda_dp4a(u, qx, sumi); 792 sumi = ggml_cuda_dp4a(v, qy, sumi); 793 } 794 795 // Apply Q2_0's single scale and this chunk's Q8_1 scale 796 const float d8 = __low2float(bq8_1_chunk->ds); 797 return d2 * d8 * sumi; 798 } 799 800 static __device__ __forceinline__ float vec_dot_q4_0_q8_1( 801 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 802 803 const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq + kbx; 804 805 int v[VDR_Q4_0_Q8_1_MMVQ]; 806 int u[2*VDR_Q4_0_Q8_1_MMVQ]; 807 808 #pragma unroll 809 for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) { 810 v[i] = get_int_b2(bq4_0->qs, iqs + i); 811 u[2*i+0] = get_int_b4(bq8_1->qs, iqs + i); 812 u[2*i+1] = get_int_b4(bq8_1->qs, iqs + i + QI4_0); 813 } 814 815 return vec_dot_q4_0_q8_1_impl(v, u, bq4_0->d, bq8_1->ds); 816 } 817 818 819 static __device__ __forceinline__ float vec_dot_q4_1_q8_1( 820 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 821 822 const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq + kbx; 823 824 int v[VDR_Q4_1_Q8_1_MMVQ]; 825 int u[2*VDR_Q4_1_Q8_1_MMVQ]; 826 827 #pragma unroll 828 for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) { 829 v[i] = get_int_b4(bq4_1->qs, iqs + i); 830 u[2*i+0] = get_int_b4(bq8_1->qs, iqs + i); 831 u[2*i+1] = get_int_b4(bq8_1->qs, iqs + i + QI4_1); 832 } 833 834 return vec_dot_q4_1_q8_1_impl(v, u, bq4_1->dm, bq8_1->ds); 835 } 836 837 static __device__ __forceinline__ float vec_dot_q5_0_q8_1( 838 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 839 840 const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq + kbx; 841 842 int vl[VDR_Q5_0_Q8_1_MMVQ]; 843 int vh[VDR_Q5_0_Q8_1_MMVQ]; 844 int u[2*VDR_Q5_0_Q8_1_MMVQ]; 845 846 #pragma unroll 847 for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) { 848 vl[i] = get_int_b2(bq5_0->qs, iqs + i); 849 vh[i] = get_int_b2(bq5_0->qh, 0) >> (4 * (iqs + i)); 850 u[2*i+0] = get_int_b4(bq8_1->qs, iqs + i); 851 u[2*i+1] = get_int_b4(bq8_1->qs, iqs + i + QI5_0); 852 } 853 854 return vec_dot_q5_0_q8_1_impl(vl, vh, u, bq5_0->d, bq8_1->ds); 855 } 856 857 static __device__ __forceinline__ float vec_dot_q5_1_q8_1( 858 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 859 860 const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq + kbx; 861 862 int vl[VDR_Q5_1_Q8_1_MMVQ]; 863 int vh[VDR_Q5_1_Q8_1_MMVQ]; 864 int u[2*VDR_Q5_1_Q8_1_MMVQ]; 865 866 #pragma unroll 867 for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) { 868 vl[i] = get_int_b4(bq5_1->qs, iqs + i); 869 vh[i] = get_int_b4(bq5_1->qh, 0) >> (4 * (iqs + i)); 870 u[2*i+0] = get_int_b4(bq8_1->qs, iqs + i); 871 u[2*i+1] = get_int_b4(bq8_1->qs, iqs + i + QI5_1); 872 } 873 874 return vec_dot_q5_1_q8_1_impl(vl, vh, u, bq5_1->dm, bq8_1->ds); 875 } 876 877 static __device__ __forceinline__ float vec_dot_q8_0_q8_1( 878 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 879 880 const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq + kbx; 881 882 int v[VDR_Q8_0_Q8_1_MMVQ]; 883 int u[VDR_Q8_0_Q8_1_MMVQ]; 884 885 #pragma unroll 886 for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) { 887 v[i] = get_int_b2(bq8_0->qs, iqs + i); 888 u[i] = get_int_b4(bq8_1->qs, iqs + i); 889 } 890 891 return vec_dot_q8_0_q8_1_impl(v, u, bq8_0->d, __low2half(bq8_1->ds)); 892 } 893 894 static __device__ __forceinline__ float vec_dot_q2_K_q8_1( 895 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 896 897 const block_q2_K * bq2_K = (const block_q2_K *) vbq + kbx; 898 899 // iqs is in 0,2..14, each 32 bytes of qs hold 4 q8_1 blocks (one per 2 bit shift) 900 const int bq8_offset = QR2_K * (iqs / QI8_1); 901 const int iqs8 = iqs % QI8_1; 902 903 int v[VDR_Q2_K_Q8_1_MMVQ]; 904 #pragma unroll 905 for (int j = 0; j < VDR_Q2_K_Q8_1_MMVQ; ++j) { 906 v[j] = get_int_b4(bq2_K->qs, iqs + j); 907 } 908 909 // scale of shift i is byte 2*i + (iqs8 >= QI8_1/2) of the 8 bytes for this half 910 const int sc_shift = 8 * (iqs8 / (QI8_1/2)); 911 int scales[2]; 912 scales[0] = get_int_b4(bq2_K->scales, 2*(iqs / QI8_1) + 0) >> sc_shift; 913 scales[1] = get_int_b4(bq2_K->scales, 2*(iqs / QI8_1) + 1) >> sc_shift; 914 915 int u[VDR_Q2_K_Q8_1_MMVQ*QR2_K]; 916 float d8[QR2_K]; 917 918 #pragma unroll 919 for (int i = 0; i < QR2_K; ++i) { 920 #pragma unroll 921 for (int j = 0; j < VDR_Q2_K_Q8_1_MMVQ; ++j) { 922 u[VDR_Q2_K_Q8_1_MMVQ*i + j] = get_int_b4(bq8_1[bq8_offset + i].qs, iqs8 + j); 923 } 924 d8[i] = __low2float(bq8_1[bq8_offset + i].ds); 925 } 926 927 return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8); 928 } 929 930 static __device__ __forceinline__ float vec_dot_q3_K_q8_1( 931 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 932 933 const block_q3_K * bq3_K = (const block_q3_K *) vbq + kbx; 934 935 // iqs is in 0,2..14, each 32 bytes of qs hold 4 q8_1 blocks (one per 2 bit shift) 936 const int bq8_offset = QR3_K * (iqs / (QI3_K/2)); 937 const int iqs8 = iqs % QI8_1; 938 939 const float d = bq3_K->d; 940 941 int vl[VDR_Q3_K_Q8_1_MMVQ]; 942 int vh[VDR_Q3_K_Q8_1_MMVQ]; 943 #pragma unroll 944 for (int j = 0; j < VDR_Q3_K_Q8_1_MMVQ; ++j) { 945 vl[j] = get_int_b2(bq3_K->qs, iqs + j); 946 vh[j] = get_int_b2(bq3_K->hmask, iqs8 + j) >> bq8_offset; 947 } 948 949 // sub-block 8*(iqs/8) + 2*i + (iqs8 >= QI8_1/2) for shift i: 950 // 4 low bits in nibble iqs/8 of bytes, 2 high bits at bit 4*(iqs/8) + 2*(i/2) of bytes 8..11 951 const int sc_shift = 8 * (iqs8 / (QI8_1/2)) + 4 * (iqs / (QI3_K/2)); 952 int scales[3]; 953 #pragma unroll 954 for (int l = 0; l < 3; ++l) { 955 scales[l] = get_int_b2(bq3_K->scales, l) >> sc_shift; 956 } 957 958 int u[VDR_Q3_K_Q8_1_MMVQ*QR3_K]; 959 float d8[QR3_K]; 960 961 #pragma unroll 962 for (int i = 0; i < QR3_K; ++i) { 963 #pragma unroll 964 for (int j = 0; j < VDR_Q3_K_Q8_1_MMVQ; ++j) { 965 u[VDR_Q3_K_Q8_1_MMVQ*i + j] = get_int_b4(bq8_1[bq8_offset + i].qs, iqs8 + j); 966 } 967 d8[i] = __low2float(bq8_1[bq8_offset + i].ds); 968 } 969 970 return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, scales, d, d8); 971 } 972 973 static __device__ __forceinline__ float vec_dot_q4_K_q8_1( 974 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 975 976 const block_q4_K * bq4_K = (const block_q4_K *) vbq + kbx; 977 978 int v[2]; 979 int u[2*QR4_K]; 980 float d8[QR4_K]; 981 982 // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6 983 const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2)); 984 985 // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12 986 // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44 987 // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76 988 // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108 989 990 const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4)); 991 v[0] = q4[0]; 992 v[1] = q4[4]; 993 994 // branchless so nvcc can hoist this out of the ncols_dst loop 995 const uint16_t * scales = (const uint16_t *)bq4_K->scales; 996 const int j = bq8_offset/2; 997 const int jm = j & 1; 998 999 const uint32_t s0 = scales[jm + 0]; 1000 const uint32_t s2 = scales[jm + 2]; 1001 const uint32_t s4 = scales[jm + 4]; 1002 1003 const uint32_t hi = (uint32_t) -(int32_t) (j >= 2); 1004 1005 uint16_t aux[2]; 1006 aux[0] = (uint16_t) (((s0 & 0x3f3f) & ~hi) | ((((s4 >> 0) & 0x0f0f) | ((s0 & 0xc0c0) >> 2)) & hi)); 1007 aux[1] = (uint16_t) (((s2 & 0x3f3f) & ~hi) | ((((s4 >> 4) & 0x0f0f) | ((s2 & 0xc0c0) >> 2)) & hi)); 1008 const uint8_t * sc = (const uint8_t *)aux; 1009 const uint8_t * m = sc + 2; 1010 1011 for (int i = 0; i < QR4_K; ++i) { 1012 const block_q8_1 * bq8i = bq8_1 + bq8_offset + i; 1013 d8[i] = __low2float(bq8i->ds); 1014 1015 const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4); 1016 u[2*i+0] = q8[0]; 1017 u[2*i+1] = q8[4]; 1018 } 1019 1020 return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8); 1021 } 1022 1023 static __device__ __forceinline__ float vec_dot_q5_K_q8_1( 1024 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1025 1026 const block_q5_K * bq5_K = (const block_q5_K *) vbq + kbx; 1027 1028 int vl[2]; 1029 int vh[2]; 1030 int u[2*QR5_K]; 1031 float d8[QR5_K]; 1032 1033 const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2)); 1034 const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4)); 1035 const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4)); 1036 1037 vl[0] = ql[0]; 1038 vl[1] = ql[4]; 1039 1040 vh[0] = qh[0] >> bq8_offset; 1041 vh[1] = qh[4] >> bq8_offset; 1042 1043 // same as q4_K 1044 const uint16_t * scales = (const uint16_t *)bq5_K->scales; 1045 const int j = bq8_offset/2; 1046 const int jm = j & 1; 1047 1048 const uint32_t s0 = scales[jm + 0]; 1049 const uint32_t s2 = scales[jm + 2]; 1050 const uint32_t s4 = scales[jm + 4]; 1051 1052 const uint32_t hi = (uint32_t) -(int32_t) (j >= 2); 1053 1054 uint16_t aux[2]; 1055 aux[0] = (uint16_t) (((s0 & 0x3f3f) & ~hi) | ((((s4 >> 0) & 0x0f0f) | ((s0 & 0xc0c0) >> 2)) & hi)); 1056 aux[1] = (uint16_t) (((s2 & 0x3f3f) & ~hi) | ((((s4 >> 4) & 0x0f0f) | ((s2 & 0xc0c0) >> 2)) & hi)); 1057 1058 const uint8_t * sc = (const uint8_t *)aux; 1059 const uint8_t * m = sc + 2; 1060 1061 #pragma unroll 1062 for (int i = 0; i < QR5_K; ++i) { 1063 const block_q8_1 * bq8i = bq8_1 + bq8_offset + i; 1064 d8[i] = __low2float(bq8i->ds); 1065 1066 const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4); 1067 u[2*i+0] = q8[0]; 1068 u[2*i+1] = q8[4]; 1069 } 1070 1071 return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8); 1072 } 1073 1074 static __device__ __forceinline__ float vec_dot_q6_K_q8_1( 1075 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1076 1077 const block_q6_K * bq6_K = (const block_q6_K *) vbq + kbx; 1078 1079 const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4); 1080 const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8); 1081 const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4)); 1082 1083 int vl[VDR_Q6_K_Q8_1_MMVQ]; 1084 int vh[VDR_Q6_K_Q8_1_MMVQ]; 1085 #pragma unroll 1086 for (int j = 0; j < VDR_Q6_K_Q8_1_MMVQ; ++j) { 1087 vl[j] = get_int_b2(bq6_K->ql, iqs + j); 1088 vh[j] = get_int_b2(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4) + j) >> vh_shift; 1089 } 1090 1091 const int8_t * scales = bq6_K->scales + scale_offset; 1092 1093 int u[VDR_Q6_K_Q8_1_MMVQ*QR6_K]; 1094 float d8[QR6_K]; 1095 1096 #pragma unroll 1097 for (int i = 0; i < QR6_K; ++i) { 1098 #pragma unroll 1099 for (int j = 0; j < VDR_Q6_K_Q8_1_MMVQ; ++j) { 1100 u[VDR_Q6_K_Q8_1_MMVQ*i + j] = get_int_b4(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1 + j); 1101 } 1102 d8[i] = __low2float(bq8_1[bq8_offset + 2*i].ds); 1103 } 1104 1105 return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8); 1106 } 1107 1108 #define VDR_IQ2_XXS_Q8_1_MMVQ 2 1109 #define VDR_IQ2_XXS_Q8_1_MMQ 2 1110 1111 static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1( 1112 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1113 1114 const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq + kbx; 1115 1116 const int q2 = get_int_b2(bq2->qs, iqs); 1117 const uint8_t * aux8 = (const uint8_t *) &q2; 1118 const uint32_t aux32 = get_int_b2(bq2->qs, iqs + 1); 1119 1120 int sumi = 0; 1121 #pragma unroll 1122 for (int k0 = 0; k0 < 8; k0 += 2) { 1123 const uint2 grid_pos = ((const uint2*)iq2xxs_grid)[aux8[k0/2]]; 1124 const uint32_t signs = unpack_ksigns(aux32 >> (7 * k0 / 2)); 1125 1126 const int signs0 = __vcmpne4(signs & 0x08040201, 0); 1127 const int grid0 = __vsub4(grid_pos.x ^ signs0, signs0); 1128 const int u0 = get_int_b4(bq8_1[iqs/2].qs, k0 + 0); 1129 sumi = ggml_cuda_dp4a(grid0, u0, sumi); 1130 1131 const int signs1 = __vcmpne4(signs & 0x80402010, 0); 1132 const int grid1 = __vsub4(grid_pos.y ^ signs1, signs1); 1133 const int u1 = get_int_b4(bq8_1[iqs/2].qs, k0 + 1); 1134 sumi = ggml_cuda_dp4a(grid1, u1, sumi); 1135 } 1136 1137 const int ls = aux32 >> 27 | 1; // (scale * 2 + 1) 1138 sumi = sumi * ls / 8; // (sumi * scale + sumi / 2) / 4 1139 const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds); 1140 return d * sumi; 1141 } 1142 1143 #define VDR_IQ2_XS_Q8_1_MMVQ 2 1144 #define VDR_IQ2_XS_Q8_1_MMQ 2 1145 1146 static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1( 1147 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1148 1149 const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq + kbx; 1150 1151 const int2 q2_packed = make_int2(get_int_b2(bq2->qs, iqs + 0), get_int_b2(bq2->qs, iqs + 1)); 1152 const uint16_t * q2 = (const uint16_t *) &q2_packed; 1153 const int ls0 = bq2->scales[iqs/2] & 0x0F; 1154 const int ls1 = bq2->scales[iqs/2] >> 4; 1155 1156 int sumi0 = 0; 1157 int sumi1 = 0; 1158 #pragma unroll 1159 for (int l0 = 0; l0 < 8; l0 += 2) { 1160 const uint2 grid_pos = ((const uint2*)iq2xs_grid)[q2[l0/2] & 0x1FF]; 1161 const uint32_t signs = unpack_ksigns(q2[l0/2] >> 9); 1162 1163 const int signs0 = __vcmpne4(signs & 0x08040201, 0); 1164 const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0); 1165 const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0); 1166 1167 const int signs1 = __vcmpne4(signs & 0x80402010, 0); 1168 const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1); 1169 const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1); 1170 1171 if (l0 < 4) { 1172 sumi0 = ggml_cuda_dp4a(grid_l, u0, sumi0); 1173 sumi0 = ggml_cuda_dp4a(grid_h, u1, sumi0); 1174 } else { 1175 sumi1 = ggml_cuda_dp4a(grid_l, u0, sumi1); 1176 sumi1 = ggml_cuda_dp4a(grid_h, u1, sumi1); 1177 } 1178 } 1179 const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4; 1180 const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds); 1181 return d * sumi; 1182 } 1183 1184 #define VDR_IQ2_S_Q8_1_MMVQ 2 1185 #define VDR_IQ2_S_Q8_1_MMQ 2 1186 1187 static __device__ __forceinline__ float vec_dot_iq2_s_q8_1( 1188 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1189 1190 const block_iq2_s * bq2 = (const block_iq2_s *) vbq + kbx; 1191 1192 const int qs_packed = get_int_b2(bq2->qs, iqs/2); 1193 const uint8_t * qs = (const uint8_t *) &qs_packed; 1194 1195 const int qh = bq2->qh[iqs/2]; 1196 1197 const int signs_packed_32 = get_int_b2(bq2->qs, QK_K/32 + iqs/2); 1198 const uint8_t * signs_packed_8 = (const uint8_t *) &signs_packed_32; 1199 1200 const int ls0 = bq2->scales[iqs/2] & 0x0F; 1201 const int ls1 = bq2->scales[iqs/2] >> 4; 1202 1203 int sumi0 = 0; 1204 int sumi1 = 0; 1205 #pragma unroll 1206 for (int l0 = 0; l0 < 8; l0 += 2) { 1207 const int * grid_pos = (const int *)(iq2s_grid + (qs[l0/2] | ((qh << (8-l0)) & 0x300))); 1208 1209 const int signs0 = __vcmpne4(((signs_packed_8[l0/2] & 0x03) << 7) | ((signs_packed_8[l0/2] & 0x0C) << 21), 0x00000000); 1210 const int signs1 = __vcmpne4(((signs_packed_8[l0/2] & 0x30) << 3) | ((signs_packed_8[l0/2] & 0xC0) << 17), 0x00000000); 1211 1212 const int grid_l = __vsub4(grid_pos[0] ^ signs0, signs0); 1213 const int grid_h = __vsub4(grid_pos[1] ^ signs1, signs1); 1214 1215 const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0); 1216 const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1); 1217 1218 if (l0 < 4) { 1219 sumi0 = ggml_cuda_dp4a(grid_l, u0, sumi0); 1220 sumi0 = ggml_cuda_dp4a(grid_h, u1, sumi0); 1221 } else { 1222 sumi1 = ggml_cuda_dp4a(grid_l, u0, sumi1); 1223 sumi1 = ggml_cuda_dp4a(grid_h, u1, sumi1); 1224 } 1225 } 1226 const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4; 1227 1228 const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds); 1229 return d * sumi; 1230 } 1231 1232 #define VDR_IQ3_XXS_Q8_1_MMVQ 2 1233 #define VDR_IQ3_XXS_Q8_1_MMQ 2 1234 1235 static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1( 1236 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1237 1238 const block_iq3_xxs * bq3 = (const block_iq3_xxs *) vbq + kbx; 1239 1240 const int2 q3_packed = make_int2(get_int_b2(bq3->qs, iqs), get_int_b2(bq3->qs, iqs+1)); 1241 const uint8_t * q3 = (const uint8_t *) &q3_packed; 1242 const uint32_t aux32 = get_int_b2(bq3->qs, QK_K/16 + iqs/2); 1243 1244 int sumi = 0; 1245 #pragma unroll 1246 for (int l0 = 0; l0 < 8; l0 += 2) { 1247 const int2 grid_pos = make_int2(iq3xxs_grid[q3[l0 + 0]], iq3xxs_grid[q3[l0 + 1]]); 1248 const uint32_t signs = unpack_ksigns(aux32 >> (7*l0/2)); 1249 1250 const int signs0 = __vcmpne4(signs & 0x08040201, 0); 1251 const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0); 1252 1253 const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0); 1254 1255 const int signs1 = __vcmpne4(signs & 0x80402010, 0); 1256 const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1); 1257 1258 const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1); 1259 1260 sumi = ggml_cuda_dp4a(grid_l, u0, sumi); 1261 sumi = ggml_cuda_dp4a(grid_h, u1, sumi); 1262 } 1263 1264 const int ls = aux32 >> 28; 1265 sumi = (ls*sumi + sumi/2)/2; 1266 const float d = __half2float(bq3->d) * __low2float(bq8_1[iqs/2].ds); 1267 return d * sumi; 1268 } 1269 1270 #define VDR_IQ3_S_Q8_1_MMVQ 2 1271 #define VDR_IQ3_S_Q8_1_MMQ 2 1272 1273 // TODO: don't use lookup table for signs 1274 static __device__ __forceinline__ float vec_dot_iq3_s_q8_1( 1275 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1276 1277 const block_iq3_s * bq3 = (const block_iq3_s *) vbq + kbx; 1278 1279 const int2 qs_packed = make_int2(get_int_b2(bq3->qs, iqs + 0), get_int_b2(bq3->qs, iqs + 1)); 1280 const uint8_t * qs = (const uint8_t *) &qs_packed; 1281 1282 const int qh = bq3->qh[iqs/2]; 1283 1284 const int signs_packed_32 = get_int_b2(bq3->signs, iqs/2); 1285 const uint8_t * signs_packed_8 = (const uint8_t *) &signs_packed_32; 1286 1287 int sumi = 0; 1288 #pragma unroll 1289 for (int l0 = 0; l0 < 8; l0 += 2) { 1290 const int2 grid_pos = make_int2( 1291 iq3s_grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)], 1292 iq3s_grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]); 1293 1294 const int signs0 = __vcmpne4(((signs_packed_8[l0/2] & 0x03) << 7) | ((signs_packed_8[l0/2] & 0x0C) << 21), 0x00000000); 1295 const int signs1 = __vcmpne4(((signs_packed_8[l0/2] & 0x30) << 3) | ((signs_packed_8[l0/2] & 0xC0) << 17), 0x00000000); 1296 1297 const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0); 1298 const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1); 1299 1300 const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0); 1301 const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1); 1302 1303 sumi = ggml_cuda_dp4a(grid_l, u0, sumi); 1304 sumi = ggml_cuda_dp4a(grid_h, u1, sumi); 1305 } 1306 1307 sumi *= 1 + 2*((bq3->scales[iqs/4] >> ((iqs << 1) & 0x04)) & 0x0F); 1308 1309 const float d = __half2float(bq3->d) * __low2float(bq8_1[iqs/2].ds); 1310 return d * sumi; 1311 } 1312 1313 #define VDR_IQ1_S_Q8_1_MMVQ 1 1314 #define VDR_IQ1_S_Q8_1_MMQ 1 1315 1316 static __device__ __forceinline__ float vec_dot_iq1_s_q8_1( 1317 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1318 const block_iq1_s * bq1 = (const block_iq1_s *) vbq + kbx; 1319 1320 const int qs_packed = get_int_b2(bq1->qs, iqs); 1321 const uint8_t * qs = (const uint8_t *) &qs_packed; 1322 1323 const int qh = bq1->qh[iqs]; 1324 1325 int sumi = 0; 1326 #pragma unroll 1327 for (int l0 = 0; l0 < 8; l0 += 2) { 1328 const int grid = iq1s_grid_gpu[qs[l0/2] | (((qh >> 3*(l0/2)) & 0x07) << 8)]; 1329 1330 const int grid0 = (grid >> 0) & 0x0F0F0F0F; 1331 const int grid1 = (grid >> 4) & 0x0F0F0F0F; 1332 1333 const int u0 = get_int_b4(bq8_1[iqs].qs, l0 + 0); 1334 const int u1 = get_int_b4(bq8_1[iqs].qs, l0 + 1); 1335 1336 sumi = ggml_cuda_dp4a(grid0, u0, sumi); 1337 sumi = ggml_cuda_dp4a(grid1, u1, sumi); 1338 } 1339 1340 const float d1q = __half2float(bq1->d) * (((qh >> 11) & 0x0E) + 1); 1341 const float delta = -1.0f + IQ1S_DELTA - (qh & 0x8000) * (2.0f*IQ1S_DELTA/0x8000); 1342 const float2 ds = __half22float2(bq8_1[iqs].ds); 1343 return d1q * (ds.x*sumi + ds.y*delta); 1344 } 1345 1346 #define VDR_IQ1_M_Q8_1_MMVQ 1 1347 #define VDR_IQ1_M_Q8_1_MMQ 1 1348 1349 static __device__ __forceinline__ float vec_dot_iq1_m_q8_1( 1350 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1351 1352 const block_iq1_m * bq1 = (const block_iq1_m *) vbq + kbx; 1353 1354 const int qs_packed = get_int_b4(bq1->qs, iqs); 1355 const uint8_t * qs = (const uint8_t *) &qs_packed; 1356 1357 int sumi[2] = {0}; 1358 float sumf[2] = {0.0f}; 1359 #pragma unroll 1360 for (int l0 = 0; l0 < 8; l0 += 2) { 1361 const int qhl = bq1->qh[2*iqs + l0/4] >> (4 * ((l0/2) % 2)); 1362 1363 const int grid = iq1s_grid_gpu[qs[l0/2] | ((qhl & 0x07) << 8)]; 1364 1365 const int grid0 = (grid >> 0) & 0x0F0F0F0F; 1366 const int grid1 = (grid >> 4) & 0x0F0F0F0F; 1367 1368 const int u0 = get_int_b4(bq8_1[iqs].qs, l0 + 0); 1369 const int u1 = get_int_b4(bq8_1[iqs].qs, l0 + 1); 1370 1371 sumi[l0/4] = ggml_cuda_dp4a(grid0, u0, sumi[l0/4]); 1372 sumi[l0/4] = ggml_cuda_dp4a(grid1, u1, sumi[l0/4]); 1373 1374 const float delta = -1.0f + IQ1M_DELTA - (qhl & 0x08) * (2.0f*IQ1M_DELTA/0x08); 1375 int sumy = 0; 1376 sumy = ggml_cuda_dp4a(u0, 0x01010101, sumy); 1377 sumy = ggml_cuda_dp4a(u1, 0x01010101, sumy); 1378 sumf[l0/4] += delta*sumy; 1379 } 1380 1381 const uint16_t * sc = (const uint16_t *) bq1->scales; 1382 1383 iq1m_scale_t scale; 1384 scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00F0) | ((sc[2] >> 4) & 0x0F00) | (sc[3] & 0xF000); 1385 const float d = __half2float(scale.f16) * __low2float(bq8_1[iqs].ds); 1386 1387 const int tmp = sc[iqs/2] >> (6*(iqs%2)); 1388 const int sc0 = 2*((tmp >> 0) & 0x07) + 1; 1389 const int sc1 = 2*((tmp >> 3) & 0x07) + 1; 1390 return d * ((sumi[0] + sumf[0]) * sc0 + (sumi[1] + sumf[1]) * sc1); 1391 } 1392 1393 #define VDR_IQ4_NL_Q8_1_MMVQ 2 1394 #define VDR_IQ4_NL_Q8_1_MMQ 4 1395 1396 static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1( 1397 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1398 1399 const block_iq4_nl * bq4 = (const block_iq4_nl *) vbq + kbx; 1400 1401 const int * q8 = (const int *) bq8_1->qs + iqs; 1402 1403 int sumi = 0; 1404 #pragma unroll 1405 for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) { 1406 const int aux_q4 = get_int_b2(bq4->qs, iqs + l); 1407 const int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl); 1408 1409 sumi = ggml_cuda_dp4a(v.x, q8[l + 0], sumi); 1410 sumi = ggml_cuda_dp4a(v.y, q8[l + 4], sumi); 1411 } 1412 1413 const float d = __half2float(bq4->d) * __low2float(bq8_1->ds); 1414 return d * sumi; 1415 } 1416 1417 #define VDR_IQ4_XS_Q8_1_MMVQ 4 1418 #define VDR_IQ4_XS_Q8_1_MMQ 4 1419 1420 static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1( 1421 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { 1422 1423 const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq + kbx; 1424 1425 int sumi = 0; 1426 #pragma unroll 1427 for (int j = 0; j < 4; ++j) { 1428 const int aux_q4 = get_int_b4(bq4->qs, iqs + j); 1429 const int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl); 1430 1431 const int u0 = get_int_b4(bq8_1[iqs/4].qs, j + 0); 1432 const int u1 = get_int_b4(bq8_1[iqs/4].qs, j + 4); 1433 1434 sumi = ggml_cuda_dp4a(v.x, u0, sumi); 1435 sumi = ggml_cuda_dp4a(v.y, u1, sumi); 1436 } 1437 1438 const int ls = ((bq4->scales_l[iqs/8] >> (iqs & 0x04)) & 0x0F) | (((bq4->scales_h >> (iqs/2)) & 0x03) << 4); 1439 sumi *= ls - 32; 1440 1441 const float d = __half2float(bq4->d) * __low2float(bq8_1[iqs/4].ds); 1442 return d * sumi; 1443 }