ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-metal / kernels / quantize.h
| 1 | #pragma once |
| 2 | |
| 3 | #include "common.h" |
| 4 | |
| 5 | void quantize_q1_0(device const float * src, device block_q1_0 & dst) { |
| 6 | float sum_abs = 0.0f; |
| 7 | for (int j = 0; j < QK1_0; j++) { |
| 8 | sum_abs += fabs(src[j]); |
| 9 | } |
| 10 | dst.d = sum_abs / QK1_0; |
| 11 | |
| 12 | for (int j = 0; j < QK1_0 / 8; j++) { |
| 13 | dst.qs[j] = 0; |
| 14 | } |
| 15 | for (int j = 0; j < QK1_0; j++) { |
| 16 | if (src[j] >= 0.0f) { |
| 17 | dst.qs[j / 8] |= (1 << (j % 8)); |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | void quantize_q2_0(device const float * src, device block_q2_0 & dst) { |
| 23 | #pragma METAL fp math_mode(safe) |
| 24 | float amax = 0.0f; |
| 25 | for (int j = 0; j < QK2_0; j++) { |
| 26 | float a = fabs(src[j]); |
| 27 | if (a > amax) amax = a; |
| 28 | } |
| 29 | const float d = amax; |
| 30 | dst.d = d; |
| 31 | |
| 32 | const float id = d > 0.0f ? 1.0f / d : 0.0f; |
| 33 | |
| 34 | for (int j = 0; j < QK2_0 / 4; j++) { |
| 35 | dst.qs[j] = 0; |
| 36 | } |
| 37 | for (int j = 0; j < QK2_0; j++) { |
| 38 | int q = (int)round(src[j] * id) + 1; |
| 39 | q = max(0, min(3, q)); |
| 40 | dst.qs[j / 4] |= (q << (2 * (j % 4))); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void quantize_q4_0(device const float * src, device block_q4_0 & dst) { |
| 45 | #pragma METAL fp math_mode(safe) |
| 46 | float amax = 0.0f; // absolute max |
| 47 | float max = 0.0f; |
| 48 | |
| 49 | for (int j = 0; j < QK4_0; j++) { |
| 50 | const float v = src[j]; |
| 51 | if (amax < fabs(v)) { |
| 52 | amax = fabs(v); |
| 53 | max = v; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | const float d = max / -8; |
| 58 | const float id = d ? 1.0f/d : 0.0f; |
| 59 | |
| 60 | dst.d = d; |
| 61 | |
| 62 | for (int j = 0; j < QK4_0/2; ++j) { |
| 63 | const float x0 = src[0 + j]*id; |
| 64 | const float x1 = src[QK4_0/2 + j]*id; |
| 65 | |
| 66 | const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f)); |
| 67 | const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f)); |
| 68 | |
| 69 | dst.qs[j] = xi0; |
| 70 | dst.qs[j] |= xi1 << 4; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void quantize_q4_1(device const float * src, device block_q4_1 & dst) { |
| 75 | #pragma METAL fp math_mode(safe) |
| 76 | float min = FLT_MAX; |
| 77 | float max = -FLT_MAX; |
| 78 | |
| 79 | for (int j = 0; j < QK4_1; j++) { |
| 80 | const float v = src[j]; |
| 81 | if (min > v) min = v; |
| 82 | if (max < v) max = v; |
| 83 | } |
| 84 | |
| 85 | const float d = (max - min) / ((1 << 4) - 1); |
| 86 | const float id = d ? 1.0f/d : 0.0f; |
| 87 | |
| 88 | dst.d = d; |
| 89 | dst.m = min; |
| 90 | |
| 91 | for (int j = 0; j < QK4_1/2; ++j) { |
| 92 | const float x0 = (src[0 + j] - min)*id; |
| 93 | const float x1 = (src[QK4_1/2 + j] - min)*id; |
| 94 | |
| 95 | const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f)); |
| 96 | const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f)); |
| 97 | |
| 98 | dst.qs[j] = xi0; |
| 99 | dst.qs[j] |= xi1 << 4; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void quantize_q5_0(device const float * src, device block_q5_0 & dst) { |
| 104 | #pragma METAL fp math_mode(safe) |
| 105 | float amax = 0.0f; // absolute max |
| 106 | float max = 0.0f; |
| 107 | |
| 108 | for (int j = 0; j < QK5_0; j++) { |
| 109 | const float v = src[j]; |
| 110 | if (amax < fabs(v)) { |
| 111 | amax = fabs(v); |
| 112 | max = v; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | const float d = max / -16; |
| 117 | const float id = d ? 1.0f/d : 0.0f; |
| 118 | |
| 119 | dst.d = d; |
| 120 | |
| 121 | uint32_t qh = 0; |
| 122 | for (int j = 0; j < QK5_0/2; ++j) { |
| 123 | const float x0 = src[0 + j]*id; |
| 124 | const float x1 = src[QK5_0/2 + j]*id; |
| 125 | |
| 126 | const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f)); |
| 127 | const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f)); |
| 128 | |
| 129 | dst.qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4); |
| 130 | qh |= ((xi0 & 0x10u) >> 4) << (j + 0); |
| 131 | qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2); |
| 132 | } |
| 133 | |
| 134 | thread const uint8_t * qh8 = (thread const uint8_t *)&qh; |
| 135 | |
| 136 | for (int j = 0; j < 4; ++j) { |
| 137 | dst.qh[j] = qh8[j]; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void quantize_q5_1(device const float * src, device block_q5_1 & dst) { |
| 142 | #pragma METAL fp math_mode(safe) |
| 143 | float max = src[0]; |
| 144 | float min = src[0]; |
| 145 | |
| 146 | for (int j = 1; j < QK5_1; j++) { |
| 147 | const float v = src[j]; |
| 148 | min = v < min ? v : min; |
| 149 | max = v > max ? v : max; |
| 150 | } |
| 151 | |
| 152 | const float d = (max - min) / 31; |
| 153 | const float id = d ? 1.0f/d : 0.0f; |
| 154 | |
| 155 | dst.d = d; |
| 156 | dst.m = min; |
| 157 | |
| 158 | uint32_t qh = 0; |
| 159 | for (int j = 0; j < QK5_1/2; ++j) { |
| 160 | const float x0 = (src[0 + j] - min)*id; |
| 161 | const float x1 = (src[QK5_1/2 + j] - min)*id; |
| 162 | |
| 163 | const uint8_t xi0 = (uint8_t)(x0 + 0.5f); |
| 164 | const uint8_t xi1 = (uint8_t)(x1 + 0.5f); |
| 165 | |
| 166 | dst.qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4); |
| 167 | qh |= ((xi0 & 0x10u) >> 4) << (j + 0); |
| 168 | qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_1/2); |
| 169 | } |
| 170 | |
| 171 | thread const uint8_t * qh8 = (thread const uint8_t *)&qh; |
| 172 | |
| 173 | for (int j = 0; j < 4; ++j) { |
| 174 | dst.qh[j] = qh8[j]; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void quantize_q8_0(device const float * src, device block_q8_0 & dst) { |
| 179 | #pragma METAL fp math_mode(safe) |
| 180 | float amax = 0.0f; // absolute max |
| 181 | |
| 182 | for (int j = 0; j < QK8_0; j++) { |
| 183 | const float v = src[j]; |
| 184 | amax = MAX(amax, fabs(v)); |
| 185 | } |
| 186 | |
| 187 | const float d = amax / ((1 << 7) - 1); |
| 188 | const float id = d ? 1.0f/d : 0.0f; |
| 189 | |
| 190 | dst.d = d; |
| 191 | |
| 192 | for (int j = 0; j < QK8_0; ++j) { |
| 193 | const float x0 = src[j]*id; |
| 194 | |
| 195 | dst.qs[j] = round(x0); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void quantize_iq4_nl(device const float * src, device block_iq4_nl & dst) { |
| 200 | #pragma METAL fp math_mode(safe) |
| 201 | float amax = 0.0f; // absolute max |
| 202 | float max = 0.0f; |
| 203 | |
| 204 | for (int j = 0; j < QK4_NL; j++) { |
| 205 | const float v = src[j]; |
| 206 | if (amax < fabs(v)) { |
| 207 | amax = fabs(v); |
| 208 | max = v; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | const float d = max / kvalues_iq4nl_f[0]; |
| 213 | const float id = d ? 1.0f/d : 0.0f; |
| 214 | |
| 215 | float sumqx = 0, sumq2 = 0; |
| 216 | for (int j = 0; j < QK4_NL/2; ++j) { |
| 217 | const float x0 = src[0 + j]*id; |
| 218 | const float x1 = src[QK4_NL/2 + j]*id; |
| 219 | |
| 220 | const uint8_t xi0 = best_index_int8(16, kvalues_iq4nl_f, x0); |
| 221 | const uint8_t xi1 = best_index_int8(16, kvalues_iq4nl_f, x1); |
| 222 | |
| 223 | dst.qs[j] = xi0 | (xi1 << 4); |
| 224 | |
| 225 | const float v0 = kvalues_iq4nl_f[xi0]; |
| 226 | const float v1 = kvalues_iq4nl_f[xi1]; |
| 227 | const float w0 = src[0 + j]*src[0 + j]; |
| 228 | const float w1 = src[QK4_NL/2 + j]*src[QK4_NL/2 + j]; |
| 229 | sumqx += w0*v0*src[j] + w1*v1*src[QK4_NL/2 + j]; |
| 230 | sumq2 += w0*v0*v0 + w1*v1*v1; |
| 231 | |
| 232 | } |
| 233 | |
| 234 | dst.d = sumq2 > 0 ? sumqx/sumq2 : d; |
| 235 | } |
| 236 | |
| 237 | void quantize_tq2_0(device const float * src, device block_tq2_0 & dst) { |
| 238 | #pragma METAL fp math_mode(safe) |
| 239 | float amax = 0.0f; // absolute max |
| 240 | |
| 241 | for (int j = 0; j < QK_K; j++) { |
| 242 | const float v = src[j]; |
| 243 | amax = MAX(amax, fabs(v)); |
| 244 | } |
| 245 | |
| 246 | const float d = amax; |
| 247 | const float id = d ? 1.0f/d : 0.0f; |
| 248 | |
| 249 | dst.d = (half) d; |
| 250 | |
| 251 | for (int j = 0; j < QK_K/4; j += 32) { |
| 252 | for (int m = 0; m < 32; ++m) { |
| 253 | uint8_t q = 0; |
| 254 | for (int n = 0; n < 4; ++n) { |
| 255 | // -1, 0, 1 -> 0, 1, 2 |
| 256 | int xi = (int)round(src[m + n*32] * id) + 1; |
| 257 | q += (uint8_t)((xi & 3) << (2*n)); |
| 258 | } |
| 259 | dst.qs[j + m] = q; |
| 260 | } |
| 261 | src += 4*32; |
| 262 | } |
| 263 | } |