← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
gemm_kernels_q5_1_q8_1.c File Reference

Q5_1 x Q8_1 contract kernels used for ggml parity (Gemma-sensitive path) More...

#include <stdint.h>
#include <string.h>
#include <math.h>
#include "ckernel_quant.h"

Go to the source code of this file.

Macros

#define CK_Q51_STACK_Q8_BLOCKS   256
 
#define QK8_1   32
 

Functions

static float dot_q5_1_q8_1_block (const block_q5_1 *w, const block_q8_1 *x)
 
void gemm_nt_q5_1_q8_1 (const float *A, const void *B, const float *bias, float *C, int M, int N, int K)
 
void gemm_nt_q5_1_q8_1_m4 (const float *A, const void *B, const float *bias, float *C, int M, int N, int K)
 
void gemm_nt_q5_1_q8_1_m8 (const float *A, const void *B, const float *bias, float *C, int M, int N, int K)
 
void gemm_nt_q5_1_q8_1_ref (const void *A_q8, const void *B, const float *bias, float *C, int M, int N, int K)
 
void gemv_q5_1_q8_1 (float *y, const void *W, const float *x, int M, int K)
 
void gemv_q5_1_q8_1_ref (float *y, const void *W, const void *x_q8, int M, int K)
 
static void quantize_row_q8_1_scalar (const float *x, block_q8_1 *y, int k)
 

Detailed Description

Q5_1 x Q8_1 contract kernels used for ggml parity (Gemma-sensitive path)

Definition in file gemm_kernels_q5_1_q8_1.c.

Macro Definition Documentation

◆ CK_Q51_STACK_Q8_BLOCKS

#define CK_Q51_STACK_Q8_BLOCKS   256

Definition at line 16 of file gemm_kernels_q5_1_q8_1.c.

◆ QK8_1

#define QK8_1   32

Definition at line 22 of file gemm_kernels_q5_1_q8_1.c.

Function Documentation

◆ dot_q5_1_q8_1_block()

static float dot_q5_1_q8_1_block ( const block_q5_1 w,
const block_q8_1 *  x 
)
static

Definition at line 193 of file gemm_kernels_q5_1_q8_1.c.

193 {
194#if defined(__AVX2__)
195 return dot_q5_1_q8_1_block_avx2(w, x);
196#else
197 uint32_t qh;
198 memcpy(&qh, w->qh, sizeof(qh));
199
200 int sumi0 = 0;
201 int sumi1 = 0;
202 for (int j = 0; j < QK5_1 / 2; ++j) {
203 const uint8_t xh0 = (uint8_t)(((qh >> (j + 0)) << 4) & 0x10);
204 const uint8_t xh1 = (uint8_t)(((qh >> (j + 12)) ) & 0x10);
205 const int32_t q0 = (int32_t)((w->qs[j] & 0x0F) | xh0);
206 const int32_t q1 = (int32_t)((w->qs[j] >> 4) | xh1);
207 sumi0 += q0 * (int32_t)x->qs[j];
208 sumi1 += q1 * (int32_t)x->qs[j + QK5_1 / 2];
209 }
210
211 const float wd = CK_FP16_TO_FP32(w->d);
212 const float wm = CK_FP16_TO_FP32(w->m);
213 const float xd = CK_FP16_TO_FP32(x->d);
214 const float xs = CK_FP16_TO_FP32(x->s);
215 return (wd * xd) * (float)(sumi0 + sumi1) + wm * xs;
216#endif
217}
#define CK_FP16_TO_FP32(x)
#define QK5_1
uint8_t qs[32/2]
uint8_t qh[4]

References CK_FP16_TO_FP32, block_q5_1::d, block_q5_1::m, block_q5_1::qh, QK5_1, and block_q5_1::qs.

Referenced by gemm_nt_q5_1_q8_1(), gemm_nt_q5_1_q8_1_ref(), and gemv_q5_1_q8_1_ref().

◆ gemm_nt_q5_1_q8_1()

void gemm_nt_q5_1_q8_1 ( const float *  A,
const void *  B,
const float *  bias,
float *  C,
int  M,
int  N,
int  K 
)

Definition at line 289 of file gemm_kernels_q5_1_q8_1.c.

296{
297 if (!A || !B || !C || M <= 0 || N <= 0 || K <= 0 || (K % QK5_1) != 0) {
298 return;
299 }
300
301 const int blocks_per_row = K / QK5_1;
302 if (blocks_per_row > CK_Q51_STACK_Q8_BLOCKS) {
303 return;
304 }
305
306 const block_q5_1 *W = (const block_q5_1 *)B;
307
308 for (int m = 0; m < M; ++m) {
309 block_q8_1 a_q8[CK_Q51_STACK_Q8_BLOCKS];
310 quantize_row_q8_1_scalar(&A[m * K], a_q8, K);
311 float *c_row = &C[(size_t)m * (size_t)N];
312
313 int n = 0;
314 for (; n + 7 < N; n += 8) {
315 const block_q5_1 *w0 = &W[(size_t)(n + 0) * (size_t)blocks_per_row];
316 const block_q5_1 *w1 = &W[(size_t)(n + 1) * (size_t)blocks_per_row];
317 const block_q5_1 *w2 = &W[(size_t)(n + 2) * (size_t)blocks_per_row];
318 const block_q5_1 *w3 = &W[(size_t)(n + 3) * (size_t)blocks_per_row];
319 const block_q5_1 *w4 = &W[(size_t)(n + 4) * (size_t)blocks_per_row];
320 const block_q5_1 *w5 = &W[(size_t)(n + 5) * (size_t)blocks_per_row];
321 const block_q5_1 *w6 = &W[(size_t)(n + 6) * (size_t)blocks_per_row];
322 const block_q5_1 *w7 = &W[(size_t)(n + 7) * (size_t)blocks_per_row];
323 float s0 = 0.0f;
324 float s1 = 0.0f;
325 float s2 = 0.0f;
326 float s3 = 0.0f;
327 float s4 = 0.0f;
328 float s5 = 0.0f;
329 float s6 = 0.0f;
330 float s7 = 0.0f;
331
332 for (int b = 0; b < blocks_per_row; ++b) {
333 const block_q8_1 *x = &a_q8[b];
334 s0 += dot_q5_1_q8_1_block(&w0[b], x);
335 s1 += dot_q5_1_q8_1_block(&w1[b], x);
336 s2 += dot_q5_1_q8_1_block(&w2[b], x);
337 s3 += dot_q5_1_q8_1_block(&w3[b], x);
338 s4 += dot_q5_1_q8_1_block(&w4[b], x);
339 s5 += dot_q5_1_q8_1_block(&w5[b], x);
340 s6 += dot_q5_1_q8_1_block(&w6[b], x);
341 s7 += dot_q5_1_q8_1_block(&w7[b], x);
342 }
343
344 c_row[n + 0] = s0 + (bias ? bias[n + 0] : 0.0f);
345 c_row[n + 1] = s1 + (bias ? bias[n + 1] : 0.0f);
346 c_row[n + 2] = s2 + (bias ? bias[n + 2] : 0.0f);
347 c_row[n + 3] = s3 + (bias ? bias[n + 3] : 0.0f);
348 c_row[n + 4] = s4 + (bias ? bias[n + 4] : 0.0f);
349 c_row[n + 5] = s5 + (bias ? bias[n + 5] : 0.0f);
350 c_row[n + 6] = s6 + (bias ? bias[n + 6] : 0.0f);
351 c_row[n + 7] = s7 + (bias ? bias[n + 7] : 0.0f);
352 }
353
354 for (; n < N; ++n) {
355 const block_q5_1 *w_row = &W[(size_t)n * (size_t)blocks_per_row];
356 float sum = 0.0f;
357 for (int b = 0; b < blocks_per_row; ++b) {
358 sum += dot_q5_1_q8_1_block(&w_row[b], &a_q8[b]);
359 }
360 c_row[n] = sum + (bias ? bias[n] : 0.0f);
361 }
362 }
363}
static void quantize_row_q8_1_scalar(const float *x, block_q8_1 *y, int k)
#define CK_Q51_STACK_Q8_BLOCKS
static float dot_q5_1_q8_1_block(const block_q5_1 *w, const block_q8_1 *x)
#define C(color)
Definition show_config.c:39

References C, CK_Q51_STACK_Q8_BLOCKS, dot_q5_1_q8_1_block(), QK5_1, and quantize_row_q8_1_scalar().

Referenced by gemm_nt_q5_1_q8_1_m4().

◆ gemm_nt_q5_1_q8_1_m4()

void gemm_nt_q5_1_q8_1_m4 ( const float *  A,
const void *  B,
const float *  bias,
float *  C,
int  M,
int  N,
int  K 
)

Definition at line 365 of file gemm_kernels_q5_1_q8_1.c.

372{
373#if defined(__AVX2__)
374 if (!A || !B || !C || M <= 0 || N <= 0 || K <= 0 || (K % QK5_1) != 0) {
375 return;
376 }
377
378 const int blocks_per_row = K / QK5_1;
379 if (blocks_per_row > CK_Q51_STACK_Q8_BLOCKS) {
380 gemm_nt_q5_1_q8_1(A, B, bias, C, M, N, K);
381 return;
382 }
383
384 const block_q5_1 *weights = (const block_q5_1 *)B;
385 int m = 0;
386 for (; m + 3 < M; m += 4) {
387 block_q8_1 activation_q8[4][CK_Q51_STACK_Q8_BLOCKS];
388 for (int row = 0; row < 4; ++row) {
390 &A[(size_t)(m + row) * (size_t)K], activation_q8[row], K);
391 }
392
393 for (int n = 0; n < N; ++n) {
394 const block_q5_1 *weight_row =
395 &weights[(size_t)n * (size_t)blocks_per_row];
396 float sums[4] = {0.0f, 0.0f, 0.0f, 0.0f};
397 for (int block = 0; block < blocks_per_row; ++block) {
398 float partial[4];
399 dot_q5_1_q8_1_block_m4_avx2(
400 &weight_row[block],
401 &activation_q8[0][block], &activation_q8[1][block],
402 &activation_q8[2][block], &activation_q8[3][block], partial);
403 sums[0] += partial[0];
404 sums[1] += partial[1];
405 sums[2] += partial[2];
406 sums[3] += partial[3];
407 }
408 const float add = bias ? bias[n] : 0.0f;
409 C[(size_t)(m + 0) * (size_t)N + n] = sums[0] + add;
410 C[(size_t)(m + 1) * (size_t)N + n] = sums[1] + add;
411 C[(size_t)(m + 2) * (size_t)N + n] = sums[2] + add;
412 C[(size_t)(m + 3) * (size_t)N + n] = sums[3] + add;
413 }
414 }
415
416 if (m < M) {
418 A + (size_t)m * (size_t)K, B, bias,
419 C + (size_t)m * (size_t)N, M - m, N, K);
420 }
421#else
422 gemm_nt_q5_1_q8_1(A, B, bias, C, M, N, K);
423#endif
424}
void gemm_nt_q5_1_q8_1(const float *A, const void *B, const float *bias, float *C, int M, int N, int K)

References C, CK_Q51_STACK_Q8_BLOCKS, gemm_nt_q5_1_q8_1(), QK5_1, and quantize_row_q8_1_scalar().

Referenced by gemm_nt_q5_1_q8_1_m8().

◆ gemm_nt_q5_1_q8_1_m8()

void gemm_nt_q5_1_q8_1_m8 ( const float *  A,
const void *  B,
const float *  bias,
float *  C,
int  M,
int  N,
int  K 
)

Definition at line 426 of file gemm_kernels_q5_1_q8_1.c.

433{
434#if defined(__AVX2__)
435 if (!A || !B || !C || M <= 0 || N <= 0 || K <= 0 || (K % QK5_1) != 0) {
436 return;
437 }
438
439 const int blocks_per_row = K / QK5_1;
440 if (blocks_per_row > CK_Q51_STACK_Q8_BLOCKS) {
441 gemm_nt_q5_1_q8_1_m4(A, B, bias, C, M, N, K);
442 return;
443 }
444
445 const block_q5_1 *weights = (const block_q5_1 *)B;
446 int m = 0;
447 for (; m + 7 < M; m += 8) {
448 block_q8_1 activation_q8[8][CK_Q51_STACK_Q8_BLOCKS];
449 for (int row = 0; row < 8; ++row) {
451 &A[(size_t)(m + row) * (size_t)K], activation_q8[row], K);
452 }
453
454 for (int n = 0; n < N; ++n) {
455 const block_q5_1 *weight_row =
456 &weights[(size_t)n * (size_t)blocks_per_row];
457 float sums[8] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
458 for (int block = 0; block < blocks_per_row; ++block) {
459 const block_q8_1 *rows[8] = {
460 &activation_q8[0][block], &activation_q8[1][block],
461 &activation_q8[2][block], &activation_q8[3][block],
462 &activation_q8[4][block], &activation_q8[5][block],
463 &activation_q8[6][block], &activation_q8[7][block],
464 };
465 float partial[8];
466 dot_q5_1_q8_1_block_m8_avx2(&weight_row[block], rows, partial);
467 for (int row = 0; row < 8; ++row) {
468 sums[row] += partial[row];
469 }
470 }
471 const float add = bias ? bias[n] : 0.0f;
472 for (int row = 0; row < 8; ++row) {
473 C[(size_t)(m + row) * (size_t)N + n] = sums[row] + add;
474 }
475 }
476 }
477
478 if (m < M) {
480 A + (size_t)m * (size_t)K, B, bias,
481 C + (size_t)m * (size_t)N, M - m, N, K);
482 }
483#else
484 gemm_nt_q5_1_q8_1_m4(A, B, bias, C, M, N, K);
485#endif
486}
void gemm_nt_q5_1_q8_1_m4(const float *A, const void *B, const float *bias, float *C, int M, int N, int K)

References C, CK_Q51_STACK_Q8_BLOCKS, gemm_nt_q5_1_q8_1_m4(), QK5_1, and quantize_row_q8_1_scalar().

◆ gemm_nt_q5_1_q8_1_ref()

void gemm_nt_q5_1_q8_1_ref ( const void *  A_q8,
const void *  B,
const float *  bias,
float *  C,
int  M,
int  N,
int  K 
)

Definition at line 242 of file gemm_kernels_q5_1_q8_1.c.

247{
248 if (!A_q8 || !B || !C || M <= 0 || N <= 0 || K <= 0 || (K % QK5_1) != 0) {
249 return;
250 }
251
252 const block_q8_1 *A = (const block_q8_1 *)A_q8;
253 const block_q5_1 *W = (const block_q5_1 *)B;
254 const int blocks_per_row = K / QK5_1;
255
256 for (int m = 0; m < M; ++m) {
257 const block_q8_1 *a_row = &A[m * blocks_per_row];
258 for (int n = 0; n < N; ++n) {
259 const block_q5_1 *w_row = &W[n * blocks_per_row];
260 float sum = 0.0f;
261 for (int b = 0; b < blocks_per_row; ++b) {
262 sum += dot_q5_1_q8_1_block(&w_row[b], &a_row[b]);
263 }
264 C[m * N + n] = sum + (bias ? bias[n] : 0.0f);
265 }
266 }
267}

References C, dot_q5_1_q8_1_block(), and QK5_1.

◆ gemv_q5_1_q8_1()

void gemv_q5_1_q8_1 ( float *  y,
const void *  W,
const float *  x,
int  M,
int  K 
)

Definition at line 269 of file gemm_kernels_q5_1_q8_1.c.

274{
275 if (!y || !W || !x || M <= 0 || K <= 0 || (K % QK5_1) != 0) {
276 return;
277 }
278
279 const int blocks_per_row = K / QK5_1;
280 if (blocks_per_row > CK_Q51_STACK_Q8_BLOCKS) {
281 return;
282 }
283
284 block_q8_1 x_q8[CK_Q51_STACK_Q8_BLOCKS];
285 quantize_row_q8_1_scalar(x, x_q8, K);
286 gemv_q5_1_q8_1_ref(y, W, x_q8, M, K);
287}
void gemv_q5_1_q8_1_ref(float *y, const void *W, const void *x_q8, int M, int K)

References CK_Q51_STACK_Q8_BLOCKS, gemv_q5_1_q8_1_ref(), QK5_1, and quantize_row_q8_1_scalar().

◆ gemv_q5_1_q8_1_ref()

void gemv_q5_1_q8_1_ref ( float *  y,
const void *  W,
const void *  x_q8,
int  M,
int  K 
)

Definition at line 219 of file gemm_kernels_q5_1_q8_1.c.

223{
224 if (!y || !W || !x_q8 || M <= 0 || K <= 0 || (K % QK5_1) != 0) {
225 return;
226 }
227
228 const block_q5_1 *blocks = (const block_q5_1 *)W;
229 const block_q8_1 *x = (const block_q8_1 *)x_q8;
230 const int blocks_per_row = K / QK5_1;
231
232 for (int row = 0; row < M; ++row) {
233 const block_q5_1 *w_row = &blocks[row * blocks_per_row];
234 float sum = 0.0f;
235 for (int b = 0; b < blocks_per_row; ++b) {
236 sum += dot_q5_1_q8_1_block(&w_row[b], &x[b]);
237 }
238 y[row] = sum;
239 }
240}

References dot_q5_1_q8_1_block(), and QK5_1.

Referenced by gemv_q5_1_q8_1().

◆ quantize_row_q8_1_scalar()

static void quantize_row_q8_1_scalar ( const float *  x,
block_q8_1 *  y,
int  k 
)
static

Definition at line 63 of file gemm_kernels_q5_1_q8_1.c.

63 {
64 const int nb = k / QK8_1;
65 for (int b = 0; b < nb; ++b) {
66 const float *xb = x + (size_t)b * QK8_1;
67 float amax = 0.0f;
68 for (int j = 0; j < QK8_1; ++j) {
69 float av = xb[j] >= 0.0f ? xb[j] : -xb[j];
70 if (av > amax) amax = av;
71 }
72
73 const float d = amax / 127.0f;
74 const float id = (d != 0.0f) ? (1.0f / d) : 0.0f;
75 y[b].d = CK_FP32_TO_FP16(d);
76
77 int sum = 0;
78 for (int j = 0; j < QK8_1; ++j) {
79 int q = (int)roundf(xb[j] * id);
80 y[b].qs[j] = (int8_t)q;
81 sum += q;
82 }
83 y[b].s = CK_FP32_TO_FP16((float)sum * d);
84 }
85}
#define CK_FP32_TO_FP16(x)
#define QK8_1

References CK_FP32_TO_FP16, and QK8_1.

Referenced by gemm_nt_q5_1_q8_1(), gemm_nt_q5_1_q8_1_m4(), gemm_nt_q5_1_q8_1_m8(), and gemv_q5_1_q8_1().