|
| 1 | +#version 450 |
| 2 | + |
| 3 | +#include "types.comp" |
| 4 | +#include "generic_unary_head.comp" |
| 5 | + |
| 6 | +#if defined(DATA_A_IQ4_NL) |
| 7 | +// 16 invocations needed for init_iq4nl_shmem |
| 8 | +layout(local_size_x = 16, local_size_y = 1, local_size_z = 1) in; |
| 9 | +#else |
| 10 | +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; |
| 11 | +#endif |
| 12 | + |
| 13 | +layout (binding = 0) readonly buffer S {float data_s[];}; |
| 14 | +layout (binding = 1) writeonly buffer Q {A_TYPE data_q[];}; |
| 15 | + |
| 16 | +#if defined(DATA_A_Q4_0) |
| 17 | +void quantize(uint dst_idx, uint src_idx) |
| 18 | +{ |
| 19 | + float amax = 0.0; |
| 20 | + float vmax = 0.0; |
| 21 | + |
| 22 | + [[unroll]] for (int j = 0; j < QUANT_K_Q4_0; ++j) { |
| 23 | + const float v = data_s[src_idx + j]; |
| 24 | + if (amax < abs(v)) { |
| 25 | + amax = abs(v); |
| 26 | + vmax = v; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + const float d = vmax / -8; |
| 31 | + const float id = (d != 0.0) ? 1.0/d : 0.0; |
| 32 | + |
| 33 | + data_q[dst_idx].d = float16_t(d); |
| 34 | + |
| 35 | + [[unroll]] for (int j = 0; j < QUANT_K_Q4_0/2; ++j) { |
| 36 | + const float x0 = data_s[src_idx + 0 + j]*id; |
| 37 | + const float x1 = data_s[src_idx + QUANT_K_Q4_0/2 + j]*id; |
| 38 | + |
| 39 | + const uint xi0 = min(15, int(x0 + 8.5)); |
| 40 | + const uint xi1 = min(15, int(x1 + 8.5)); |
| 41 | + |
| 42 | + data_q[dst_idx].qs[j] = uint8_t(xi0 | (xi1 << 4)); |
| 43 | + } |
| 44 | +} |
| 45 | +#endif |
| 46 | + |
| 47 | +#if defined(DATA_A_Q4_1) |
| 48 | +void quantize(uint dst_idx, uint src_idx) |
| 49 | +{ |
| 50 | + float vmin = 1.0/0.0; |
| 51 | + float vmax = -vmin; |
| 52 | + |
| 53 | + [[unroll]] for (int j = 0; j < QUANT_K_Q4_1; ++j) { |
| 54 | + const float v = data_s[src_idx + j]; |
| 55 | + |
| 56 | + if (v < vmin) vmin = v; |
| 57 | + if (v > vmax) vmax = v; |
| 58 | + } |
| 59 | + |
| 60 | + const float d = (vmax - vmin) / ((1 << 4) - 1); |
| 61 | + const float id = (d != 0.0) ? 1.0/d : 0.0; |
| 62 | + |
| 63 | + data_q[dst_idx].d = float16_t(d); |
| 64 | + data_q[dst_idx].m = float16_t(vmin); |
| 65 | + |
| 66 | + [[unroll]] for (int j = 0; j < QUANT_K_Q4_1/2; ++j) { |
| 67 | + const float x0 = (data_s[src_idx + 0 + j] - vmin)*id; |
| 68 | + const float x1 = (data_s[src_idx + QUANT_K_Q4_1/2 + j] - vmin)*id; |
| 69 | + |
| 70 | + const uint xi0 = min(15, int(x0 + 0.5)); |
| 71 | + const uint xi1 = min(15, int(x1 + 0.5)); |
| 72 | + |
| 73 | + data_q[dst_idx].qs[j] = uint8_t(xi0 | (xi1 << 4)); |
| 74 | + } |
| 75 | +} |
| 76 | +#endif |
| 77 | + |
| 78 | +#if defined(DATA_A_Q5_0) |
| 79 | +void quantize(uint dst_idx, uint src_idx) |
| 80 | +{ |
| 81 | + float amax = 0.0; |
| 82 | + float vmax = 0.0; |
| 83 | + |
| 84 | + [[unroll]] for (int j = 0; j < QUANT_K_Q5_0; ++j) { |
| 85 | + const float v = data_s[src_idx + j]; |
| 86 | + if (amax < abs(v)) { |
| 87 | + amax = abs(v); |
| 88 | + vmax = v; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + const float d = vmax / -16; |
| 93 | + const float id = (d != 0.0) ? 1.0/d : 0.0; |
| 94 | + |
| 95 | + data_q[dst_idx].d = float16_t(d); |
| 96 | + |
| 97 | + uint32_t qh = 0; |
| 98 | + [[unroll]] for (int j = 0; j < QUANT_K_Q5_0/2; ++j) { |
| 99 | + const float x0 = data_s[src_idx + 0 + j]*id; |
| 100 | + const float x1 = data_s[src_idx + QUANT_K_Q5_0/2 + j]*id; |
| 101 | + |
| 102 | + const uint xi0 = min(31, int(x0 + 16.5)); |
| 103 | + const uint xi1 = min(31, int(x1 + 16.5)); |
| 104 | + |
| 105 | + data_q[dst_idx].qs[j] = uint8_t((xi0 & 0xf) | ((xi1 & 0xf) << 4)); |
| 106 | + qh |= ((xi0 & 0x10u) >> 4) << (j + 0); |
| 107 | + qh |= ((xi1 & 0x10u) >> 4) << (j + QUANT_K_Q5_0/2); |
| 108 | + } |
| 109 | + data_q[dst_idx].qh[0] = uint16_t(qh & 0xFFFF); |
| 110 | + data_q[dst_idx].qh[1] = uint16_t(qh >> 16); |
| 111 | +} |
| 112 | +#endif |
| 113 | + |
| 114 | +#if defined(DATA_A_Q5_1) |
| 115 | +void quantize(uint dst_idx, uint src_idx) |
| 116 | +{ |
| 117 | + float min = data_s[src_idx + 0]; |
| 118 | + float max = min; |
| 119 | + |
| 120 | + [[unroll]] for (int j = 1; j < QUANT_K_Q5_1; ++j) { |
| 121 | + const float v = data_s[src_idx + j]; |
| 122 | + min = v < min ? v : min; |
| 123 | + max = v > max ? v : max; |
| 124 | + } |
| 125 | + |
| 126 | + const float d = (max - min) / 31; |
| 127 | + const float id = (d != 0) ? 1.0/d : 0.0; |
| 128 | + |
| 129 | + data_q[dst_idx].d = float16_t(d); |
| 130 | + data_q[dst_idx].m = float16_t(min); |
| 131 | + |
| 132 | + uint32_t qh = 0; |
| 133 | + [[unroll]] for (int j = 0; j < QUANT_K_Q5_1/2; ++j) { |
| 134 | + const float x0 = (data_s[src_idx + 0 + j] - min)*id; |
| 135 | + const float x1 = (data_s[src_idx + QUANT_K_Q5_1/2 + j] - min)*id; |
| 136 | + |
| 137 | + const uint xi0 = uint(x0 + 0.5); |
| 138 | + const uint xi1 = uint(x1 + 0.5); |
| 139 | + |
| 140 | + data_q[dst_idx].qs[j] = uint8_t((xi0 & 0xf) | ((xi1 & 0xf) << 4)); |
| 141 | + qh |= ((xi0 & 0x10u) >> 4) << (j + 0); |
| 142 | + qh |= ((xi1 & 0x10u) >> 4) << (j + QUANT_K_Q5_1/2); |
| 143 | + } |
| 144 | + data_q[dst_idx].qh = qh; |
| 145 | +} |
| 146 | +#endif |
| 147 | + |
| 148 | +#if defined(DATA_A_Q8_0) |
| 149 | +void quantize(uint dst_idx, uint src_idx) |
| 150 | +{ |
| 151 | + float amax = 0.0; // absolute max |
| 152 | + |
| 153 | + [[unroll]] for (int j = 0; j < QUANT_K_Q8_0; j++) { |
| 154 | + const float v = data_s[src_idx + j]; |
| 155 | + amax = max(amax, abs(v)); |
| 156 | + } |
| 157 | + |
| 158 | + const float d = amax / ((1 << 7) - 1); |
| 159 | + const float id = (d != 0.0) ? 1.0/d : 0.0; |
| 160 | + |
| 161 | + data_q[dst_idx].d = float16_t(d); |
| 162 | + |
| 163 | + [[unroll]] for (int j = 0; j < QUANT_K_Q8_0; ++j) { |
| 164 | + const float x0 = data_s[src_idx + j]*id; |
| 165 | + |
| 166 | + data_q[dst_idx].qs[j] = int8_t(round(x0)); |
| 167 | + } |
| 168 | +} |
| 169 | +#endif |
| 170 | + |
| 171 | +#if defined(DATA_A_IQ4_NL) |
| 172 | +uint best_index(float x) { |
| 173 | + if (x <= kvalues_iq4nl[0]) return 0; |
| 174 | + if (x >= kvalues_iq4nl[15]) return 15; |
| 175 | + int ml = 0, mu = 15; |
| 176 | + while (mu-ml > 1) { |
| 177 | + int mav = (ml+mu)/2; |
| 178 | + if (x < kvalues_iq4nl[mav]) mu = mav; else ml = mav; |
| 179 | + } |
| 180 | + return x - kvalues_iq4nl[mu-1] < kvalues_iq4nl[mu] - x ? mu-1 : mu; |
| 181 | +} |
| 182 | + |
| 183 | +void quantize(uint dst_idx, uint src_idx) |
| 184 | +{ |
| 185 | + float amax = 0.0; |
| 186 | + float vmax = 0.0; |
| 187 | + |
| 188 | + [[unroll]] for (int j = 0; j < QUANT_K_IQ4_NL; ++j) { |
| 189 | + const float v = data_s[src_idx + j]; |
| 190 | + if (amax < abs(v)) { |
| 191 | + amax = abs(v); |
| 192 | + vmax = v; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + float d = vmax / kvalues_iq4nl[0]; |
| 197 | + const float id = (d != 0.0) ? 1.0/d : 0.0; |
| 198 | + |
| 199 | + float sumqx = 0, sumq2 = 0; |
| 200 | + [[unroll]] for (int j = 0; j < QUANT_K_IQ4_NL/2; ++j) { |
| 201 | + const float x0 = data_s[src_idx + 0 + j]*id; |
| 202 | + const float x1 = data_s[src_idx + QUANT_K_IQ4_NL/2 + j]*id; |
| 203 | + const uint xi0 = best_index(x0); |
| 204 | + const uint xi1 = best_index(x1); |
| 205 | + data_q[dst_idx].qs[j] = uint8_t(xi0 | (xi1 << 4)); |
| 206 | + const float v0 = kvalues_iq4nl[xi0]; |
| 207 | + const float v1 = kvalues_iq4nl[xi1]; |
| 208 | + const float w0 = data_s[src_idx + 0 + j]*data_s[src_idx + 0 + j]; |
| 209 | + const float w1 = data_s[src_idx + QUANT_K_IQ4_NL/2 + j]*data_s[src_idx + QUANT_K_IQ4_NL/2 + j]; |
| 210 | + sumqx += w0*v0*data_s[src_idx + j] + w1*v1*data_s[src_idx + QUANT_K_IQ4_NL/2 + j]; |
| 211 | + sumq2 += w0*v0*v0 + w1*v1*v1; |
| 212 | + } |
| 213 | + |
| 214 | + data_q[dst_idx].d = float16_t(sumq2 > 0 ? sumqx/sumq2 : d); |
| 215 | + |
| 216 | +} |
| 217 | +#endif |
| 218 | + |
| 219 | +void main() { |
| 220 | +#if defined(DATA_A_IQ4_NL) |
| 221 | + init_iq4nl_shmem(); |
| 222 | + if (gl_LocalInvocationIndex.x != 0) { |
| 223 | + return; |
| 224 | + } |
| 225 | +#endif |
| 226 | + |
| 227 | + const uint idx = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x * QUANT_K; |
| 228 | + |
| 229 | + if (idx >= p.ne) { |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + uint dst_idx = dst_idx_quant(idx, QUANT_K); |
| 234 | + uint src_idx = get_aoffset() + src0_idx(idx); |
| 235 | + |
| 236 | + quantize(dst_idx, src_idx); |
| 237 | +} |
0 commit comments