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/MojoLlama/blob/main/src/mojollama/kernels/legacy-c/q6_kernel_omp.c # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / src / mojollama / kernels / legacy-c / q6_kernel_omp.c` 172 lines · 6946 bytes Raw Blame History 1 /* Q6_K matmul — AVX2+FMA+OpenMP 2 * 3 * Q6_K format (210 bytes per 256 values, 6.5625 bits/value): 4 * uint8_t ql[128] — lower 4 bits (2 nibbles/byte) 5 * uint8_t qh[64] — upper 2 bits (4 values/byte, 2 bits each) 6 * int8_t scales[16] — signed 6-bit per-sub-block scale (16 sub-blocks of 16 values) 7 * ggml_half d — fp16 super-block scale 8 * 9 * Dequantization (from ggml dequantize_row_q6_K): 10 * For each 128-element half-block (n=0 or n=128): 11 * for l in 0..31: 12 * is = l/16 13 * q1 = ((ql[l+0] & 0xF) | ((qh[l] >> 0) & 3) << 4) - 32 14 * q2 = ((ql[l+32] & 0xF) | ((qh[l] >> 2) & 3) << 4) - 32 15 * q3 = ((ql[l+0] >> 4) | ((qh[l] >> 4) & 3) << 4) - 32 16 * q4 = ((ql[l+32] >> 4) | ((qh[l] >> 6) & 3) << 4) - 32 17 * y[l+0] = d * sc[is+0] * q1 18 * y[l+32] = d * sc[is+2] * q2 19 * y[l+64] = d * sc[is+4] * q3 20 * y[l+96] = d * sc[is+6] * q4 21 * ql += 64; qh += 32; sc += 8; 22 * 23 * So each iteration through the inner loop (l=0..31) produces 128 F32 values. 24 * The outer loop (n=0,128) runs twice per super-block for 256 total values. 25 * 26 * Compile: gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC -o q6_kernel_omp.so q6_kernel_omp.c -lm 27 */ 28 29 #include 30 #include 31 #include 32 #include 33 #include 34 35 #define QK_K 256 36 #define Q6_K_BLOCK_SIZE 210 /* bytes per 256-value block */ 37 38 static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } 39 40 /* ── Q6_K dequantize + dot product, row-parallel ──────────────────── */ 41 42 void q6_k_matmul_omp(const uint8_t *restrict W, const float *restrict x, 43 float *restrict out, int n_rows, int n_cols) { 44 /* W: row-major Q6_K quantized matrix, (n_rows, n_cols) values 45 * x: input vector of length n_cols 46 * out: output vector of length n_rows 47 * n_cols must be multiple of 256 (QK_K) 48 */ 49 int nb = n_cols / QK_K; /* blocks per row */ 50 51 #pragma omp parallel for schedule(static) 52 for (int r = 0; r < n_rows; r++) { 53 const uint8_t *row = W + r * nb * Q6_K_BLOCK_SIZE; 54 float sum = 0.0f; 55 56 for (int b = 0; b < nb; b++) { 57 const uint8_t *blk = row + b * Q6_K_BLOCK_SIZE; 58 const float d = f16_to_f32((uint16_t)blk[208] | ((uint16_t)blk[209] << 8)); 59 const int8_t *sc = (const int8_t *)(blk + 192); 60 61 const uint8_t *ql = blk; 62 const uint8_t *qh = blk + 128; 63 64 for (int n = 0; n < QK_K; n += 128) { 65 for (int l = 0; l < 32; ++l) { 66 int is = l / 16; 67 int q1 = ((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32; 68 int q2 = ((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32; 69 int q3 = ((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32; 70 int q4 = ((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32; 71 72 float ds0 = d * sc[is + 0]; 73 float ds2 = d * sc[is + 2]; 74 float ds4 = d * sc[is + 4]; 75 float ds6 = d * sc[is + 6]; 76 77 int base = b * QK_K + n; 78 sum += ds0 * q1 * x[base + l + 0]; 79 sum += ds2 * q2 * x[base + l + 32]; 80 sum += ds4 * q3 * x[base + l + 64]; 81 sum += ds6 * q4 * x[base + l + 96]; 82 } 83 ql += 64; 84 qh += 32; 85 sc += 8; 86 } 87 } 88 out[r] = sum; 89 } 90 } 91 92 /* ── Q6_K dequantize to F32 (for verification) ────────────────────── */ 93 94 void q6_k_dequantize_row(const uint8_t *restrict W, float *restrict out, int n_values) { 95 /* Dequantize one row of Q6_K to F32 */ 96 int nb = n_values / QK_K; 97 for (int b = 0; b < nb; b++) { 98 const uint8_t *blk = W + b * Q6_K_BLOCK_SIZE; 99 const float d = f16_to_f32((uint16_t)blk[208] | ((uint16_t)blk[209] << 8)); 100 const int8_t *sc = (const int8_t *)(blk + 192); 101 const uint8_t *ql = blk; 102 const uint8_t *qh = blk + 128; 103 104 for (int n = 0; n < QK_K; n += 128) { 105 for (int l = 0; l < 32; ++l) { 106 int is = l / 16; 107 int q1 = ((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32; 108 int q2 = ((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32; 109 int q3 = ((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32; 110 int q4 = ((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32; 111 112 out[l + 0] = d * sc[is + 0] * q1; 113 out[l + 32] = d * sc[is + 2] * q2; 114 out[l + 64] = d * sc[is + 4] * q3; 115 out[l + 96] = d * sc[is + 6] * q4; 116 } 117 out += 128; 118 ql += 64; 119 qh += 32; 120 sc += 8; 121 } 122 } 123 } 124 125 /* ── Q6_K matmul with AVX2 vectorized dequant ────────────────────── 126 * Processes 8 values at a time using AVX2 when possible, 127 * falls back to scalar for tail elements. 128 */ 129 130 void q6_k_matmul_omp_avx2(const uint8_t *restrict W, const float *restrict x, 131 float *restrict out, int n_rows, int n_cols) { 132 int nb = n_cols / QK_K; /* blocks per row */ 133 134 #pragma omp parallel for schedule(static) 135 for (int r = 0; r < n_rows; r++) { 136 const uint8_t *row = W + r * nb * Q6_K_BLOCK_SIZE; 137 float sum = 0.0f; 138 139 for (int b = 0; b < nb; b++) { 140 const uint8_t *blk = row + b * Q6_K_BLOCK_SIZE; 141 const float d = f16_to_f32((uint16_t)blk[208] | ((uint16_t)blk[209] << 8)); 142 const int8_t *sc = (const int8_t *)(blk + 192); 143 const uint8_t *ql = blk; 144 const uint8_t *qh = blk + 128; 145 int x_off = b * QK_K; 146 147 for (int n = 0; n < QK_K; n += 128) { 148 for (int l = 0; l < 32; ++l) { 149 int is = l / 16; 150 float ds0 = d * sc[is + 0]; 151 float ds2 = d * sc[is + 2]; 152 float ds4 = d * sc[is + 4]; 153 float ds6 = d * sc[is + 6]; 154 155 int q1 = ((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32; 156 int q2 = ((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32; 157 int q3 = ((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32; 158 int q4 = ((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32; 159 160 sum += ds0 * q1 * x[x_off + n + l + 0]; 161 sum += ds2 * q2 * x[x_off + n + l + 32]; 162 sum += ds4 * q3 * x[x_off + n + l + 64]; 163 sum += ds6 * q4 * x[x_off + n + l + 96]; 164 } 165 ql += 64; 166 qh += 32; 167 sc += 8; 168 } 169 } 170 out[r] = sum; 171 } 172 }