| File: | nnc/ccv_nnc_8i_rowwise.c |
| Warning: | line 1664, column 12 Assigned value is garbage or undefined |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include "ccv_nnc.h" | |||
| 2 | #include "ccv_nnc_internal.h" | |||
| 3 | #include "ccv_nnc_easy.h" | |||
| 4 | #include <float.h> | |||
| 5 | #include <limits.h> | |||
| 6 | #include "ccv_nnc_8i_rowwise_packed_grids.inc" | |||
| 7 | #ifdef USE_DISPATCH | |||
| 8 | #include <dispatch/dispatch.h> | |||
| 9 | #endif | |||
| 10 | #ifdef HAVE_CUDA1 | |||
| 11 | #include "gpu/ccv_nnc_compat.h" | |||
| 12 | #elif defined(HAVE_MPS) | |||
| 13 | #include "mps/ccv_nnc_mps.h" | |||
| 14 | #endif | |||
| 15 | ||||
| 16 | enum { | |||
| 17 | CCV_NNC_8I_ROWWISE_X_REFINEMENT_STEPS = 8, | |||
| 18 | CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES = 16, | |||
| 19 | CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES = 24, | |||
| 20 | }; | |||
| 21 | ||||
| 22 | static size_t _ccv_nnc_8i_rowwise_packed_scale_offset(const int format, const size_t input_length, const size_t row_length) | |||
| 23 | { | |||
| 24 | assert(row_length > 0)((void) sizeof ((row_length > 0) ? 1 : 0), __extension__ ( { if (row_length > 0) ; else __assert_fail ("row_length > 0" , "ccv_nnc_8i_rowwise.c", 24, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 25 | assert(input_length % row_length == 0)((void) sizeof ((input_length % row_length == 0) ? 1 : 0), __extension__ ({ if (input_length % row_length == 0) ; else __assert_fail ( "input_length % row_length == 0", "ccv_nnc_8i_rowwise.c", 25, __extension__ __PRETTY_FUNCTION__); })); | |||
| 26 | const size_t row_count = input_length / row_length; | |||
| 27 | const size_t group_size = ccv_nnc_8i_rowwise_x_group_size(format); | |||
| 28 | const size_t groups_per_row = (row_length + group_size - 1) / group_size; | |||
| 29 | const size_t group_bits = ccv_nnc_8i_rowwise_x_group_bits(format); | |||
| 30 | const size_t payload_size = (row_count * groups_per_row * group_bits + 7) / 8; | |||
| 31 | return (payload_size + 127) & -128; | |||
| 32 | } | |||
| 33 | ||||
| 34 | CCV_WARN_UNUSED(size_t)size_t __attribute__((warn_unused_result)) ccv_nnc_8i_rowwise_x_data_size(const int format, const int datatype, const size_t input_length, const size_t row_length) | |||
| 35 | { | |||
| 36 | if (row_length == 0 || input_length % row_length != 0 || | |||
| 37 | ((format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) && row_length % 256 != 0)) | |||
| 38 | return 0; | |||
| 39 | assert(datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F)((void) sizeof ((datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ? 1 : 0), __extension__ ({ if (datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ; else __assert_fail ("datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F" , "ccv_nnc_8i_rowwise.c", 39, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 40 | assert(row_length > 0)((void) sizeof ((row_length > 0) ? 1 : 0), __extension__ ( { if (row_length > 0) ; else __assert_fail ("row_length > 0" , "ccv_nnc_8i_rowwise.c", 40, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 41 | assert(input_length % row_length == 0)((void) sizeof ((input_length % row_length == 0) ? 1 : 0), __extension__ ({ if (input_length % row_length == 0) ; else __assert_fail ( "input_length % row_length == 0", "ccv_nnc_8i_rowwise.c", 41, __extension__ __PRETTY_FUNCTION__); })); | |||
| 42 | const size_t row_count = input_length / row_length; | |||
| 43 | const size_t scale_offset = _ccv_nnc_8i_rowwise_packed_scale_offset(format, input_length, row_length); | |||
| 44 | return scale_offset + row_count * CCV_GET_DATA_TYPE_SIZE(datatype)_ccv_get_data_type_size[((datatype) & 0xFF000) >> 12 ]; | |||
| 45 | } | |||
| 46 | ||||
| 47 | static void _ccv_nnc_8i_rowwise_packed_write_bits(uint8_t* const data, const size_t bit_offset, const uint32_t value, const int bits) | |||
| 48 | { | |||
| 49 | int i; | |||
| 50 | for (i = 0; i < bits; i++) | |||
| 51 | if (value & (1u << i)) | |||
| 52 | data[(bit_offset + i) >> 3] |= (uint8_t)(1u << ((bit_offset + i) & 7)); | |||
| 53 | } | |||
| 54 | ||||
| 55 | static uint32_t _ccv_nnc_8i_rowwise_packed_read_bits(const uint8_t* const data, const size_t bit_offset, const int bits) | |||
| 56 | { | |||
| 57 | uint32_t value = 0; | |||
| 58 | int i; | |||
| 59 | for (i = 0; i < bits; i++) | |||
| 60 | if (data[(bit_offset + i) >> 3] & (uint8_t)(1u << ((bit_offset + i) & 7))) | |||
| 61 | value |= (1u << i); | |||
| 62 | return value; | |||
| 63 | } | |||
| 64 | ||||
| 65 | static inline int _ccv_nnc_8i_rowwise_packed_sign_extend(const uint32_t value, const int bits) | |||
| 66 | { | |||
| 67 | const uint32_t sign = 1u << (bits - 1); | |||
| 68 | return (value & sign) ? (int)value - (int)(1u << bits) : (int)value; | |||
| 69 | } | |||
| 70 | ||||
| 71 | static inline int _ccv_nnc_8i_rowwise_packed_floor_div(const int numerator, const int denominator) | |||
| 72 | { | |||
| 73 | assert(denominator > 0)((void) sizeof ((denominator > 0) ? 1 : 0), __extension__ ( { if (denominator > 0) ; else __assert_fail ("denominator > 0" , "ccv_nnc_8i_rowwise.c", 73, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 74 | return numerator >= 0 ? numerator / denominator : -((-numerator + denominator - 1) / denominator); | |||
| 75 | } | |||
| 76 | ||||
| 77 | static inline int _ccv_nnc_8i_rowwise_packed_ceil_div(const int numerator, const int denominator) | |||
| 78 | { | |||
| 79 | assert(denominator > 0)((void) sizeof ((denominator > 0) ? 1 : 0), __extension__ ( { if (denominator > 0) ; else __assert_fail ("denominator > 0" , "ccv_nnc_8i_rowwise.c", 79, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 80 | return numerator >= 0 ? (numerator + denominator - 1) / denominator : -((-numerator) / denominator); | |||
| 81 | } | |||
| 82 | ||||
| 83 | static double _ccv_nnc_8i_rowwise_packed_stored_scale(const double scale, const int datatype) | |||
| 84 | { | |||
| 85 | if (datatype == CCV_16F) | |||
| 86 | { | |||
| 87 | const float scale_f = (float)scale; | |||
| 88 | uint16_t scale_h; | |||
| 89 | float stored_scale; | |||
| 90 | ccv_float_to_half_precision(&scale_f, &scale_h, 1); | |||
| 91 | ccv_half_precision_to_float(&scale_h, &stored_scale, 1); | |||
| 92 | return stored_scale; | |||
| 93 | } else if (datatype == CCV_16BF) { | |||
| 94 | const float scale_f = (float)scale; | |||
| 95 | uint16_t scale_bf; | |||
| 96 | float stored_scale; | |||
| 97 | ccv_float_to_bfloat(&scale_f, &scale_bf, 1); | |||
| 98 | ccv_bfloat_to_float(&scale_bf, &stored_scale, 1); | |||
| 99 | return stored_scale; | |||
| 100 | } else if (datatype == CCV_32F) | |||
| 101 | return (float)scale; | |||
| 102 | return scale; | |||
| 103 | } | |||
| 104 | ||||
| 105 | static void _ccv_nnc_8i_rowwise_packed_store_scale(uint8_t* const scales, const int datatype, const size_t i, const double scale) | |||
| 106 | { | |||
| 107 | if (datatype == CCV_16F) | |||
| 108 | { | |||
| 109 | const float scale_f = (float)scale; | |||
| 110 | ccv_float_to_half_precision(&scale_f, (uint16_t*)scales + i, 1); | |||
| 111 | } else if (datatype == CCV_16BF) { | |||
| 112 | const float scale_f = (float)scale; | |||
| 113 | ccv_float_to_bfloat(&scale_f, (uint16_t*)scales + i, 1); | |||
| 114 | } else if (datatype == CCV_32F) | |||
| 115 | ((float*)scales)[i] = (float)scale; | |||
| 116 | else | |||
| 117 | ((double*)scales)[i] = scale; | |||
| 118 | } | |||
| 119 | ||||
| 120 | static double _ccv_nnc_8i_rowwise_packed_load_scale(const uint8_t* const scales, const int datatype, const size_t i) | |||
| 121 | { | |||
| 122 | if (datatype == CCV_16F) | |||
| 123 | { | |||
| 124 | float scale_f; | |||
| 125 | ccv_half_precision_to_float((const uint16_t*)scales + i, &scale_f, 1); | |||
| 126 | return scale_f; | |||
| 127 | } else if (datatype == CCV_16BF) { | |||
| 128 | float scale_f; | |||
| 129 | ccv_bfloat_to_float((const uint16_t*)scales + i, &scale_f, 1); | |||
| 130 | return scale_f; | |||
| 131 | } else if (datatype == CCV_32F) | |||
| 132 | return ((const float*)scales)[i]; | |||
| 133 | return ((const double*)scales)[i]; | |||
| 134 | } | |||
| 135 | ||||
| 136 | static void _ccv_nnc_8i_rowwise_packed_read_row(const void* const input, const int datatype, const size_t row_start, const size_t row_length, const size_t padded_row_length, double* const row) | |||
| 137 | { | |||
| 138 | size_t j; | |||
| 139 | if (datatype
| |||
| 140 | { | |||
| 141 | const uint16_t* const f16 = (const uint16_t*)input + row_start; | |||
| 142 | for (j = 0; j < row_length; j++) | |||
| 143 | { | |||
| 144 | float v; | |||
| 145 | ccv_half_precision_to_float(f16 + j, &v, 1); | |||
| 146 | row[j] = v; | |||
| 147 | } | |||
| 148 | } else if (datatype
| |||
| 149 | const uint16_t* const bf16 = (const uint16_t*)input + row_start; | |||
| 150 | for (j = 0; j < row_length; j++) | |||
| 151 | { | |||
| 152 | float v; | |||
| 153 | ccv_bfloat_to_float(bf16 + j, &v, 1); | |||
| 154 | row[j] = v; | |||
| 155 | } | |||
| 156 | } else if (datatype
| |||
| 157 | const float* const f32 = (const float*)input + row_start; | |||
| 158 | for (j = 0; j < row_length; j++) | |||
| 159 | row[j] = f32[j]; | |||
| 160 | } else { | |||
| 161 | const double* const f64 = (const double*)input + row_start; | |||
| 162 | for (j = 0; j
| |||
| 163 | row[j] = f64[j]; | |||
| 164 | } | |||
| 165 | for (; j < padded_row_length; j++) | |||
| 166 | row[j] = 0; | |||
| 167 | } | |||
| 168 | ||||
| 169 | static void _ccv_nnc_8i_rowwise_packed_write_value(void* const output, const int datatype, const size_t j, const double v) | |||
| 170 | { | |||
| 171 | if (datatype == CCV_16F) | |||
| 172 | { | |||
| 173 | const float v_f = (float)v; | |||
| 174 | ccv_float_to_half_precision(&v_f, (uint16_t*)output + j, 1); | |||
| 175 | } else if (datatype == CCV_16BF) { | |||
| 176 | const float v_f = (float)v; | |||
| 177 | ccv_float_to_bfloat(&v_f, (uint16_t*)output + j, 1); | |||
| 178 | } else if (datatype == CCV_32F) | |||
| 179 | ((float*)output)[j] = (float)v; | |||
| 180 | else | |||
| 181 | ((double*)output)[j] = v; | |||
| 182 | } | |||
| 183 | ||||
| 184 | static inline double _ccv_nnc_8i_rowwise_weight(const float* const imatrix, const size_t j) | |||
| 185 | { | |||
| 186 | return imatrix ? ccv_max((double)imatrix[j], 0.)({ typeof ((double)imatrix[j]) _a = ((double)imatrix[j]); typeof (0.) _b = (0.); (_a > _b) ? _a : _b; }) : 1.; | |||
| 187 | } | |||
| 188 | ||||
| 189 | static inline int _ccv_nnc_8i_rowwise_imatrix_is_valid(const float* const imatrix, const size_t imatrix_length, const size_t row_length, const size_t row_count) | |||
| 190 | { | |||
| 191 | if (!imatrix) | |||
| 192 | return 1; | |||
| 193 | if (imatrix_length < row_length || imatrix_length % row_length != 0) | |||
| 194 | return 0; | |||
| 195 | const size_t imatrix_slices = imatrix_length / row_length; | |||
| 196 | return imatrix_slices > 0 && row_count % imatrix_slices == 0; | |||
| 197 | } | |||
| 198 | ||||
| 199 | static inline const float* _ccv_nnc_8i_rowwise_imatrix_for_row(const float* const imatrix, const size_t imatrix_length, const size_t row_length, const size_t row_count, const size_t row_idx) | |||
| 200 | { | |||
| 201 | if (!imatrix) | |||
| 202 | return 0; | |||
| 203 | const size_t imatrix_slices = imatrix_length / row_length; | |||
| 204 | if (imatrix_slices == 1) | |||
| 205 | return imatrix; | |||
| 206 | const size_t rows_per_slice = row_count / imatrix_slices; | |||
| 207 | return imatrix + (row_idx / rows_per_slice) * row_length; | |||
| 208 | } | |||
| 209 | ||||
| 210 | typedef struct { | |||
| 211 | int q[32]; | |||
| 212 | int q8[32]; | |||
| 213 | int m; | |||
| 214 | int b; | |||
| 215 | int z; | |||
| 216 | int scale; | |||
| 217 | int grid[4]; | |||
| 218 | uint32_t signs; | |||
| 219 | } ccv_nnc_8i_rowwise_packed_group_t; | |||
| 220 | ||||
| 221 | static void _ccv_nnc_8i_rowwise_packed_nearest_candidates(const uint8_t* const grid, const int grid_size, const int lanes, const int levels, const int code_count, const int candidate_count, uint16_t* const candidates) | |||
| 222 | { | |||
| 223 | assert(candidate_count <= CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES)((void) sizeof ((candidate_count <= CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES ) ? 1 : 0), __extension__ ({ if (candidate_count <= CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES ) ; else __assert_fail ("candidate_count <= CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES" , "ccv_nnc_8i_rowwise.c", 223, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 224 | assert(candidate_count <= grid_size)((void) sizeof ((candidate_count <= grid_size) ? 1 : 0), __extension__ ({ if (candidate_count <= grid_size) ; else __assert_fail ("candidate_count <= grid_size", "ccv_nnc_8i_rowwise.c", 224 , __extension__ __PRETTY_FUNCTION__); })); | |||
| 225 | int code; | |||
| 226 | for (code = 0; code < code_count; code++) | |||
| 227 | { | |||
| 228 | uint8_t target[8]; | |||
| 229 | int value = code; | |||
| 230 | int j; | |||
| 231 | for (j = 0; j < lanes; j++) | |||
| 232 | { | |||
| 233 | target[j] = (uint8_t)(value % levels); | |||
| 234 | value /= levels; | |||
| 235 | } | |||
| 236 | int best_distance[CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES]; | |||
| 237 | int k; | |||
| 238 | for (k = 0; k < candidate_count; k++) | |||
| 239 | { | |||
| 240 | best_distance[k] = INT_MAX2147483647; | |||
| 241 | candidates[(size_t)code * candidate_count + k] = 0; | |||
| 242 | } | |||
| 243 | int index; | |||
| 244 | for (index = 0; index < grid_size; index++) | |||
| 245 | { | |||
| 246 | int distance = 0; | |||
| 247 | for (j = 0; j < lanes; j++) | |||
| 248 | { | |||
| 249 | const int d = (int)grid[(size_t)index * lanes + j] - target[j]; | |||
| 250 | distance += d * d; | |||
| 251 | } | |||
| 252 | if (distance >= best_distance[candidate_count - 1]) | |||
| 253 | continue; | |||
| 254 | k = candidate_count - 1; | |||
| 255 | while (k > 0 && distance < best_distance[k - 1]) | |||
| 256 | { | |||
| 257 | best_distance[k] = best_distance[k - 1]; | |||
| 258 | candidates[(size_t)code * candidate_count + k] = candidates[(size_t)code * candidate_count + k - 1]; | |||
| 259 | --k; | |||
| 260 | } | |||
| 261 | best_distance[k] = distance; | |||
| 262 | candidates[(size_t)code * candidate_count + k] = (uint16_t)index; | |||
| 263 | } | |||
| 264 | } | |||
| 265 | } | |||
| 266 | ||||
| 267 | static void _ccv_nnc_8i_rowwise_packed_quant_q5(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 268 | { | |||
| 269 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 270 | int best_q[16] = {0}; | |||
| 271 | int best_q8[16] = {0}; | |||
| 272 | int best_m = 1, best_b = 0; | |||
| 273 | int m, b, j; | |||
| 274 | for (m = 1; m <= 8; m++) | |||
| 275 | for (b = -16; b <= 15; b++) | |||
| 276 | { | |||
| 277 | if (-16 * m + b < -127 || 15 * m + b > 127) | |||
| 278 | continue; | |||
| 279 | double sse = 0; | |||
| 280 | int q[16]; | |||
| 281 | int q8[16]; | |||
| 282 | for (j = 0; j < 16; j++) | |||
| 283 | { | |||
| 284 | q[j] = ccv_clamp((int)lrint((y[j] - b) / m), -16, 15)({ typeof (-16) _a = (-16); typeof (15) _b = (15); typeof ((int )lrint((y[j] - b) / m)) _x = ((int)lrint((y[j] - b) / m)); (_x < _a) ? _a : ((_x > _b) ? _b : _x); }); | |||
| 285 | q8[j] = q[j] * m + b; | |||
| 286 | const double d = q8[j] - y[j]; | |||
| 287 | sse += w[j] * d * d; | |||
| 288 | } | |||
| 289 | if (sse < best_sse) | |||
| 290 | { | |||
| 291 | best_sse = sse; | |||
| 292 | best_m = m; | |||
| 293 | best_b = b; | |||
| 294 | memcpy(best_q, q, sizeof(best_q)); | |||
| 295 | memcpy(best_q8, q8, sizeof(best_q8)); | |||
| 296 | } | |||
| 297 | } | |||
| 298 | group->m = best_m; | |||
| 299 | group->b = best_b; | |||
| 300 | memcpy(group->q, best_q, sizeof(best_q)); | |||
| 301 | memcpy(group->q8, best_q8, sizeof(best_q8)); | |||
| 302 | } | |||
| 303 | ||||
| 304 | static void _ccv_nnc_8i_rowwise_packed_quant_q6(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 305 | { | |||
| 306 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 307 | int best_q[8] = {0}; | |||
| 308 | int best_q8[8] = {0}; | |||
| 309 | int best_m = 1, best_b = 0; | |||
| 310 | int m, b, j; | |||
| 311 | for (m = 1; m <= 4; m++) | |||
| 312 | for (b = -2; b <= 1; b++) | |||
| 313 | { | |||
| 314 | const int qmin = ccv_max(-32, _ccv_nnc_8i_rowwise_packed_ceil_div(-127 - b, m))({ typeof (-32) _a = (-32); typeof (_ccv_nnc_8i_rowwise_packed_ceil_div (-127 - b, m)) _b = (_ccv_nnc_8i_rowwise_packed_ceil_div(-127 - b, m)); (_a > _b) ? _a : _b; }); | |||
| 315 | const int qmax = ccv_min(31, _ccv_nnc_8i_rowwise_packed_floor_div(127 - b, m))({ typeof (31) _a = (31); typeof (_ccv_nnc_8i_rowwise_packed_floor_div (127 - b, m)) _b = (_ccv_nnc_8i_rowwise_packed_floor_div(127 - b, m)); (_a < _b) ? _a : _b; }); | |||
| 316 | if (qmin > qmax) | |||
| 317 | continue; | |||
| 318 | double sse = 0; | |||
| 319 | int q[8]; | |||
| 320 | int q8[8]; | |||
| 321 | for (j = 0; j < 8; j++) | |||
| 322 | { | |||
| 323 | q[j] = ccv_clamp((int)lrint((y[j] - b) / m), qmin, qmax)({ typeof (qmin) _a = (qmin); typeof (qmax) _b = (qmax); typeof ((int)lrint((y[j] - b) / m)) _x = ((int)lrint((y[j] - b) / m )); (_x < _a) ? _a : ((_x > _b) ? _b : _x); }); | |||
| 324 | q8[j] = q[j] * m + b; | |||
| 325 | const double d = q8[j] - y[j]; | |||
| 326 | sse += w[j] * d * d; | |||
| 327 | } | |||
| 328 | if (sse < best_sse) | |||
| 329 | { | |||
| 330 | best_sse = sse; | |||
| 331 | best_m = m; | |||
| 332 | best_b = b; | |||
| 333 | memcpy(best_q, q, sizeof(best_q)); | |||
| 334 | memcpy(best_q8, q8, sizeof(best_q8)); | |||
| 335 | } | |||
| 336 | } | |||
| 337 | group->m = best_m; | |||
| 338 | group->b = best_b; | |||
| 339 | memcpy(group->q, best_q, sizeof(best_q)); | |||
| 340 | memcpy(group->q8, best_q8, sizeof(best_q8)); | |||
| 341 | } | |||
| 342 | ||||
| 343 | static void _ccv_nnc_8i_rowwise_packed_quant_q4(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 344 | { | |||
| 345 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 346 | int best_q[16] = {0}; | |||
| 347 | int best_q8[16] = {0}; | |||
| 348 | int best_m = 1, best_b = 0; | |||
| 349 | int m, b, j; | |||
| 350 | for (m = 1; m <= 16; m++) | |||
| 351 | for (b = -8; b <= 7; b++) | |||
| 352 | { | |||
| 353 | if (-8 * m + b < -127 || 7 * m + b > 127) | |||
| 354 | continue; | |||
| 355 | double sse = 0; | |||
| 356 | int q[16]; | |||
| 357 | int q8[16]; | |||
| 358 | for (j = 0; j < 16; j++) | |||
| 359 | { | |||
| 360 | q[j] = ccv_clamp((int)lrint((y[j] - b) / m), -8, 7)({ typeof (-8) _a = (-8); typeof (7) _b = (7); typeof ((int)lrint ((y[j] - b) / m)) _x = ((int)lrint((y[j] - b) / m)); (_x < _a) ? _a : ((_x > _b) ? _b : _x); }); | |||
| 361 | q8[j] = q[j] * m + b; | |||
| 362 | const double d = q8[j] - y[j]; | |||
| 363 | sse += w[j] * d * d; | |||
| 364 | } | |||
| 365 | if (sse < best_sse) | |||
| 366 | { | |||
| 367 | best_sse = sse; | |||
| 368 | best_m = m; | |||
| 369 | best_b = b; | |||
| 370 | memcpy(best_q, q, sizeof(best_q)); | |||
| 371 | memcpy(best_q8, q8, sizeof(best_q8)); | |||
| 372 | } | |||
| 373 | } | |||
| 374 | group->m = best_m; | |||
| 375 | group->b = best_b; | |||
| 376 | memcpy(group->q, best_q, sizeof(best_q)); | |||
| 377 | memcpy(group->q8, best_q8, sizeof(best_q8)); | |||
| 378 | } | |||
| 379 | ||||
| 380 | static void _ccv_nnc_8i_rowwise_packed_quant_q3(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 381 | { | |||
| 382 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 383 | int best_q[16] = {0}; | |||
| 384 | int best_q8[16] = {0}; | |||
| 385 | int best_m = 1, best_b = 0; | |||
| 386 | int m, b, j; | |||
| 387 | for (m = 1; m <= 32; m++) | |||
| 388 | for (b = -8; b <= 6; b += 2) | |||
| 389 | { | |||
| 390 | if (-4 * m + b < -127 || 3 * m + b > 127) | |||
| 391 | continue; | |||
| 392 | double sse = 0; | |||
| 393 | int q[16]; | |||
| 394 | int q8[16]; | |||
| 395 | for (j = 0; j < 16; j++) | |||
| 396 | { | |||
| 397 | q[j] = ccv_clamp((int)lrint((y[j] - b) / m), -4, 3)({ typeof (-4) _a = (-4); typeof (3) _b = (3); typeof ((int)lrint ((y[j] - b) / m)) _x = ((int)lrint((y[j] - b) / m)); (_x < _a) ? _a : ((_x > _b) ? _b : _x); }); | |||
| 398 | q8[j] = q[j] * m + b; | |||
| 399 | const double d = q8[j] - y[j]; | |||
| 400 | sse += w[j] * d * d; | |||
| 401 | } | |||
| 402 | if (sse < best_sse) | |||
| 403 | { | |||
| 404 | best_sse = sse; | |||
| 405 | best_m = m; | |||
| 406 | best_b = b; | |||
| 407 | memcpy(best_q, q, sizeof(best_q)); | |||
| 408 | memcpy(best_q8, q8, sizeof(best_q8)); | |||
| 409 | } | |||
| 410 | } | |||
| 411 | group->m = best_m; | |||
| 412 | group->b = best_b; | |||
| 413 | memcpy(group->q, best_q, sizeof(best_q)); | |||
| 414 | memcpy(group->q8, best_q8, sizeof(best_q8)); | |||
| 415 | } | |||
| 416 | ||||
| 417 | static void _ccv_nnc_8i_rowwise_packed_quant_q2(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 418 | { | |||
| 419 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 420 | int best_q[16] = {0}; | |||
| 421 | int best_q8[16] = {0}; | |||
| 422 | int best_m = 1, best_z = 0; | |||
| 423 | int m, z, j; | |||
| 424 | for (m = 1; m <= 64; m++) | |||
| 425 | for (z = 0; z <= 120; z += 8) | |||
| 426 | { | |||
| 427 | if (3 * m - z > 127) | |||
| 428 | continue; | |||
| 429 | double sse = 0; | |||
| 430 | int q[16]; | |||
| 431 | int q8[16]; | |||
| 432 | for (j = 0; j < 16; j++) | |||
| 433 | { | |||
| 434 | q[j] = ccv_clamp((int)lrint((y[j] + z) / m), 0, 3)({ typeof (0) _a = (0); typeof (3) _b = (3); typeof ((int)lrint ((y[j] + z) / m)) _x = ((int)lrint((y[j] + z) / m)); (_x < _a) ? _a : ((_x > _b) ? _b : _x); }); | |||
| 435 | q8[j] = q[j] * m - z; | |||
| 436 | const double d = q8[j] - y[j]; | |||
| 437 | sse += w[j] * d * d; | |||
| 438 | } | |||
| 439 | if (sse < best_sse) | |||
| 440 | { | |||
| 441 | best_sse = sse; | |||
| 442 | best_m = m; | |||
| 443 | best_z = z; | |||
| 444 | memcpy(best_q, q, sizeof(best_q)); | |||
| 445 | memcpy(best_q8, q8, sizeof(best_q8)); | |||
| 446 | } | |||
| 447 | } | |||
| 448 | group->m = best_m; | |||
| 449 | group->z = best_z; | |||
| 450 | memcpy(group->q, best_q, sizeof(best_q)); | |||
| 451 | memcpy(group->q8, best_q8, sizeof(best_q8)); | |||
| 452 | } | |||
| 453 | ||||
| 454 | static int _ccv_nnc_8i_rowwise_packed_iq2_value(const uint64_t* const grid, const int index, const int lane) | |||
| 455 | { | |||
| 456 | const int v = (int)((grid[index] >> (lane * 8)) & 0xff); | |||
| 457 | if (v == 8) | |||
| 458 | return 1; | |||
| 459 | if (v == 25) | |||
| 460 | return 3; | |||
| 461 | assert(v == 43)((void) sizeof ((v == 43) ? 1 : 0), __extension__ ({ if (v == 43) ; else __assert_fail ("v == 43", "ccv_nnc_8i_rowwise.c", 461, __extension__ __PRETTY_FUNCTION__); })); | |||
| 462 | return 5; | |||
| 463 | } | |||
| 464 | ||||
| 465 | static int _ccv_nnc_8i_rowwise_packed_iq2xxs_value(const int index, const int lane) | |||
| 466 | { | |||
| 467 | const int v = (int)((ccv_nnc_8i_rowwise_packed_iq2xxs_grid[index] >> (lane * 2)) & 3); | |||
| 468 | assert(v < 3)((void) sizeof ((v < 3) ? 1 : 0), __extension__ ({ if (v < 3) ; else __assert_fail ("v < 3", "ccv_nnc_8i_rowwise.c", 468, __extension__ __PRETTY_FUNCTION__); })); | |||
| 469 | return 1 + v * 2; | |||
| 470 | } | |||
| 471 | ||||
| 472 | enum { | |||
| 473 | CCV_NNC_8I_ROWWISE_PACKED_IQ2XXS_GRID_SIZE = 256, | |||
| 474 | CCV_NNC_8I_ROWWISE_PACKED_IQ2S_GRID_SIZE = 1024, | |||
| 475 | CCV_NNC_8I_ROWWISE_PACKED_IQ2_CODE_COUNT = 6561, | |||
| 476 | }; | |||
| 477 | ||||
| 478 | static const int ccv_nnc_8i_rowwise_packed_iq2xxs_scales[16] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32}; | |||
| 479 | static int ccv_nnc_8i_rowwise_packed_iq2_scale_initialized = 0; | |||
| 480 | static uint8_t ccv_nnc_8i_rowwise_packed_iq2xxs_scale_level[33][3]; | |||
| 481 | static int ccv_nnc_8i_rowwise_packed_iq2xxs_initialized = 0; | |||
| 482 | static int ccv_nnc_8i_rowwise_packed_iq2xxs_candidates_initialized = 0; | |||
| 483 | static uint8_t ccv_nnc_8i_rowwise_packed_iq2xxs_level[CCV_NNC_8I_ROWWISE_PACKED_IQ2XXS_GRID_SIZE][8]; | |||
| 484 | static uint8_t ccv_nnc_8i_rowwise_packed_iq2xxs_scaled_value[33][CCV_NNC_8I_ROWWISE_PACKED_IQ2XXS_GRID_SIZE][8]; | |||
| 485 | static uint16_t ccv_nnc_8i_rowwise_packed_iq2xxs_candidates[CCV_NNC_8I_ROWWISE_PACKED_IQ2_CODE_COUNT][CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES]; | |||
| 486 | ||||
| 487 | static void _ccv_nnc_8i_rowwise_packed_iq2_scale_init(void) | |||
| 488 | { | |||
| 489 | if (ccv_nnc_8i_rowwise_packed_iq2_scale_initialized) | |||
| 490 | return; | |||
| 491 | int scale, level; | |||
| 492 | for (scale = 1; scale <= 32; scale++) | |||
| 493 | for (level = 0; level < 3; level++) | |||
| 494 | ccv_nnc_8i_rowwise_packed_iq2xxs_scale_level[scale][level] = (uint8_t)ccv_min((1 + level * 2) * scale, 127)({ typeof ((1 + level * 2) * scale) _a = ((1 + level * 2) * scale ); typeof (127) _b = (127); (_a < _b) ? _a : _b; }); | |||
| 495 | ccv_nnc_8i_rowwise_packed_iq2_scale_initialized = 1; | |||
| 496 | } | |||
| 497 | ||||
| 498 | static void _ccv_nnc_8i_rowwise_packed_iq2xxs_init(void) | |||
| 499 | { | |||
| 500 | if (ccv_nnc_8i_rowwise_packed_iq2xxs_initialized) | |||
| 501 | return; | |||
| 502 | _ccv_nnc_8i_rowwise_packed_iq2_scale_init(); | |||
| 503 | int index, j, scale; | |||
| 504 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2XXS_GRID_SIZE; index++) | |||
| 505 | for (j = 0; j < 8; j++) | |||
| 506 | ccv_nnc_8i_rowwise_packed_iq2xxs_level[index][j] = (uint8_t)((_ccv_nnc_8i_rowwise_packed_iq2xxs_value(index, j) - 1) / 2); | |||
| 507 | for (scale = 1; scale <= 32; scale++) | |||
| 508 | { | |||
| 509 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2XXS_GRID_SIZE; index++) | |||
| 510 | for (j = 0; j < 8; j++) | |||
| 511 | ccv_nnc_8i_rowwise_packed_iq2xxs_scaled_value[scale][index][j] = ccv_nnc_8i_rowwise_packed_iq2xxs_scale_level[scale][ccv_nnc_8i_rowwise_packed_iq2xxs_level[index][j]]; | |||
| 512 | } | |||
| 513 | ccv_nnc_8i_rowwise_packed_iq2xxs_initialized = 1; | |||
| 514 | } | |||
| 515 | ||||
| 516 | static void _ccv_nnc_8i_rowwise_packed_iq2xxs_candidates_init(void) | |||
| 517 | { | |||
| 518 | if (ccv_nnc_8i_rowwise_packed_iq2xxs_candidates_initialized) | |||
| 519 | return; | |||
| 520 | _ccv_nnc_8i_rowwise_packed_iq2xxs_init(); | |||
| 521 | _ccv_nnc_8i_rowwise_packed_nearest_candidates(&ccv_nnc_8i_rowwise_packed_iq2xxs_level[0][0], CCV_NNC_8I_ROWWISE_PACKED_IQ2XXS_GRID_SIZE, 8, 3, CCV_NNC_8I_ROWWISE_PACKED_IQ2_CODE_COUNT, CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES, &ccv_nnc_8i_rowwise_packed_iq2xxs_candidates[0][0]); | |||
| 522 | ccv_nnc_8i_rowwise_packed_iq2xxs_candidates_initialized = 1; | |||
| 523 | } | |||
| 524 | ||||
| 525 | enum { | |||
| 526 | CCV_NNC_8I_ROWWISE_PACKED_IQ2XS_GRID_SIZE = 512, | |||
| 527 | }; | |||
| 528 | ||||
| 529 | static int ccv_nnc_8i_rowwise_packed_iq2xs_initialized = 0; | |||
| 530 | static int ccv_nnc_8i_rowwise_packed_iq2xs_candidates_initialized = 0; | |||
| 531 | static uint8_t ccv_nnc_8i_rowwise_packed_iq2xs_level[CCV_NNC_8I_ROWWISE_PACKED_IQ2XS_GRID_SIZE][8]; | |||
| 532 | static uint8_t ccv_nnc_8i_rowwise_packed_iq2xs_scaled_value[16][CCV_NNC_8I_ROWWISE_PACKED_IQ2XS_GRID_SIZE][8]; | |||
| 533 | static uint16_t ccv_nnc_8i_rowwise_packed_iq2xs_candidates[CCV_NNC_8I_ROWWISE_PACKED_IQ2_CODE_COUNT][CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES]; | |||
| 534 | ||||
| 535 | static void _ccv_nnc_8i_rowwise_packed_iq2xs_init(void) | |||
| 536 | { | |||
| 537 | if (ccv_nnc_8i_rowwise_packed_iq2xs_initialized) | |||
| 538 | return; | |||
| 539 | _ccv_nnc_8i_rowwise_packed_iq2_scale_init(); | |||
| 540 | int index, j, scale_code; | |||
| 541 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2XS_GRID_SIZE; index++) | |||
| 542 | for (j = 0; j < 8; j++) | |||
| 543 | ccv_nnc_8i_rowwise_packed_iq2xs_level[index][j] = (uint8_t)((_ccv_nnc_8i_rowwise_packed_iq2_value(ccv_nnc_8i_rowwise_packed_iq2xs_grid, index, j) - 1) / 2); | |||
| 544 | for (scale_code = 0; scale_code < 16; scale_code++) | |||
| 545 | { | |||
| 546 | const int scale = ccv_nnc_8i_rowwise_packed_iq2xxs_scales[scale_code]; | |||
| 547 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2XS_GRID_SIZE; index++) | |||
| 548 | for (j = 0; j < 8; j++) | |||
| 549 | ccv_nnc_8i_rowwise_packed_iq2xs_scaled_value[scale_code][index][j] = ccv_nnc_8i_rowwise_packed_iq2xxs_scale_level[scale][ccv_nnc_8i_rowwise_packed_iq2xs_level[index][j]]; | |||
| 550 | } | |||
| 551 | ccv_nnc_8i_rowwise_packed_iq2xs_initialized = 1; | |||
| 552 | } | |||
| 553 | ||||
| 554 | static void _ccv_nnc_8i_rowwise_packed_iq2xs_candidates_init(void) | |||
| 555 | { | |||
| 556 | if (ccv_nnc_8i_rowwise_packed_iq2xs_candidates_initialized) | |||
| 557 | return; | |||
| 558 | _ccv_nnc_8i_rowwise_packed_iq2xs_init(); | |||
| 559 | _ccv_nnc_8i_rowwise_packed_nearest_candidates(&ccv_nnc_8i_rowwise_packed_iq2xs_level[0][0], CCV_NNC_8I_ROWWISE_PACKED_IQ2XS_GRID_SIZE, 8, 3, CCV_NNC_8I_ROWWISE_PACKED_IQ2_CODE_COUNT, CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES, &ccv_nnc_8i_rowwise_packed_iq2xs_candidates[0][0]); | |||
| 560 | ccv_nnc_8i_rowwise_packed_iq2xs_candidates_initialized = 1; | |||
| 561 | } | |||
| 562 | ||||
| 563 | static int ccv_nnc_8i_rowwise_packed_iq2s_initialized = 0; | |||
| 564 | static uint8_t ccv_nnc_8i_rowwise_packed_iq2s_level[CCV_NNC_8I_ROWWISE_PACKED_IQ2S_GRID_SIZE][8]; | |||
| 565 | static uint8_t ccv_nnc_8i_rowwise_packed_iq2s_scale_level[65][3]; | |||
| 566 | static uint16_t ccv_nnc_8i_rowwise_packed_iq2s_scale_level2[65][3]; | |||
| 567 | static uint8_t ccv_nnc_8i_rowwise_packed_iq2s_scaled_value[65][CCV_NNC_8I_ROWWISE_PACKED_IQ2S_GRID_SIZE][8]; | |||
| 568 | ||||
| 569 | static void _ccv_nnc_8i_rowwise_packed_iq2s_init(void) | |||
| 570 | { | |||
| 571 | if (ccv_nnc_8i_rowwise_packed_iq2s_initialized) | |||
| 572 | return; | |||
| 573 | int index, j, scale; | |||
| 574 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2S_GRID_SIZE; index++) | |||
| 575 | for (j = 0; j < 8; j++) | |||
| 576 | { | |||
| 577 | const int v = _ccv_nnc_8i_rowwise_packed_iq2_value(ccv_nnc_8i_rowwise_packed_iq2s_grid, index, j); | |||
| 578 | ccv_nnc_8i_rowwise_packed_iq2s_level[index][j] = (uint8_t)((v - 1) / 2); | |||
| 579 | } | |||
| 580 | for (scale = 1; scale <= 64; scale++) | |||
| 581 | { | |||
| 582 | for (j = 0; j < 3; j++) | |||
| 583 | { | |||
| 584 | const int v = ccv_min((1 + j * 2) * scale, 127)({ typeof ((1 + j * 2) * scale) _a = ((1 + j * 2) * scale); typeof (127) _b = (127); (_a < _b) ? _a : _b; }); | |||
| 585 | ccv_nnc_8i_rowwise_packed_iq2s_scale_level[scale][j] = (uint8_t)v; | |||
| 586 | ccv_nnc_8i_rowwise_packed_iq2s_scale_level2[scale][j] = (uint16_t)(v * v); | |||
| 587 | } | |||
| 588 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2S_GRID_SIZE; index++) | |||
| 589 | for (j = 0; j < 8; j++) | |||
| 590 | ccv_nnc_8i_rowwise_packed_iq2s_scaled_value[scale][index][j] = ccv_nnc_8i_rowwise_packed_iq2s_scale_level[scale][ccv_nnc_8i_rowwise_packed_iq2s_level[index][j]]; | |||
| 591 | } | |||
| 592 | ccv_nnc_8i_rowwise_packed_iq2s_initialized = 1; | |||
| 593 | } | |||
| 594 | ||||
| 595 | static double _ccv_nnc_8i_rowwise_packed_iq2s_sse(const double* const ay, const double* const w, const int lane, const int scale, const int index) | |||
| 596 | { | |||
| 597 | const uint8_t* const mag = ccv_nnc_8i_rowwise_packed_iq2s_scaled_value[scale][index]; | |||
| 598 | double d = (double)mag[0] - ay[lane]; | |||
| 599 | double sse = w[lane] * d * d; | |||
| 600 | d = (double)mag[1] - ay[lane + 1]; | |||
| 601 | sse += w[lane + 1] * d * d; | |||
| 602 | d = (double)mag[2] - ay[lane + 2]; | |||
| 603 | sse += w[lane + 2] * d * d; | |||
| 604 | d = (double)mag[3] - ay[lane + 3]; | |||
| 605 | sse += w[lane + 3] * d * d; | |||
| 606 | d = (double)mag[4] - ay[lane + 4]; | |||
| 607 | sse += w[lane + 4] * d * d; | |||
| 608 | d = (double)mag[5] - ay[lane + 5]; | |||
| 609 | sse += w[lane + 5] * d * d; | |||
| 610 | d = (double)mag[6] - ay[lane + 6]; | |||
| 611 | sse += w[lane + 6] * d * d; | |||
| 612 | d = (double)mag[7] - ay[lane + 7]; | |||
| 613 | sse += w[lane + 7] * d * d; | |||
| 614 | return sse; | |||
| 615 | } | |||
| 616 | ||||
| 617 | static int _ccv_nnc_8i_rowwise_packed_iq3xxs_value(const int index, const int lane) | |||
| 618 | { | |||
| 619 | const int v = (int)((ccv_nnc_8i_rowwise_packed_iq3xxs_grid[index] >> (lane * 8)) & 0xff); | |||
| 620 | switch (v) | |||
| 621 | { | |||
| 622 | case 4: return 1; | |||
| 623 | case 12: return 3; | |||
| 624 | case 20: return 5; | |||
| 625 | case 28: return 7; | |||
| 626 | case 36: return 9; | |||
| 627 | case 44: return 11; | |||
| 628 | case 52: return 13; | |||
| 629 | default: | |||
| 630 | assert(v == 62)((void) sizeof ((v == 62) ? 1 : 0), __extension__ ({ if (v == 62) ; else __assert_fail ("v == 62", "ccv_nnc_8i_rowwise.c", 630, __extension__ __PRETTY_FUNCTION__); })); | |||
| 631 | return 15; | |||
| 632 | } | |||
| 633 | } | |||
| 634 | ||||
| 635 | static int _ccv_nnc_8i_rowwise_packed_iq3s_value(const int index, const int lane) | |||
| 636 | { | |||
| 637 | return (int)((ccv_nnc_8i_rowwise_packed_iq3s_grid[index] >> (lane * 8)) & 0xff); | |||
| 638 | } | |||
| 639 | ||||
| 640 | #define CCV_NNC_8I_ROWWISE_PACKED_IQ3S_GRID_SIZE(512) (512) | |||
| 641 | #define CCV_NNC_8I_ROWWISE_PACKED_IQ3XXS_GRID_SIZE(256) (256) | |||
| 642 | #define CCV_NNC_8I_ROWWISE_PACKED_IQ3_CODE_COUNT(4096) (4096) | |||
| 643 | ||||
| 644 | static int ccv_nnc_8i_rowwise_packed_iq3s_initialized = 0; | |||
| 645 | static uint8_t ccv_nnc_8i_rowwise_packed_iq3s_scaled_value[17][CCV_NNC_8I_ROWWISE_PACKED_IQ3S_GRID_SIZE(512)][4]; | |||
| 646 | ||||
| 647 | static int ccv_nnc_8i_rowwise_packed_iq3xxs_initialized = 0; | |||
| 648 | static int ccv_nnc_8i_rowwise_packed_iq3xxs_candidates_initialized = 0; | |||
| 649 | static uint8_t ccv_nnc_8i_rowwise_packed_iq3xxs_level[CCV_NNC_8I_ROWWISE_PACKED_IQ3XXS_GRID_SIZE(256)][4]; | |||
| 650 | static uint8_t ccv_nnc_8i_rowwise_packed_iq3xxs_scaled_value[17][CCV_NNC_8I_ROWWISE_PACKED_IQ3XXS_GRID_SIZE(256)][4]; | |||
| 651 | static uint16_t ccv_nnc_8i_rowwise_packed_iq3xxs_candidates[CCV_NNC_8I_ROWWISE_PACKED_IQ3_CODE_COUNT(4096)][CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES]; | |||
| 652 | ||||
| 653 | static void _ccv_nnc_8i_rowwise_packed_iq3s_init(void) | |||
| 654 | { | |||
| 655 | if (ccv_nnc_8i_rowwise_packed_iq3s_initialized) | |||
| 656 | return; | |||
| 657 | int index, j, scale; | |||
| 658 | for (scale = 1; scale <= 16; scale++) | |||
| 659 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ3S_GRID_SIZE(512); index++) | |||
| 660 | for (j = 0; j < 4; j++) | |||
| 661 | ccv_nnc_8i_rowwise_packed_iq3s_scaled_value[scale][index][j] = (uint8_t)ccv_min(_ccv_nnc_8i_rowwise_packed_iq3s_value(index, j) * scale, 127)({ typeof (_ccv_nnc_8i_rowwise_packed_iq3s_value(index, j) * scale ) _a = (_ccv_nnc_8i_rowwise_packed_iq3s_value(index, j) * scale ); typeof (127) _b = (127); (_a < _b) ? _a : _b; }); | |||
| 662 | ccv_nnc_8i_rowwise_packed_iq3s_initialized = 1; | |||
| 663 | } | |||
| 664 | ||||
| 665 | static void _ccv_nnc_8i_rowwise_packed_iq3xxs_init(void) | |||
| 666 | { | |||
| 667 | if (ccv_nnc_8i_rowwise_packed_iq3xxs_initialized) | |||
| 668 | return; | |||
| 669 | int index, j, scale; | |||
| 670 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ3XXS_GRID_SIZE(256); index++) | |||
| 671 | for (j = 0; j < 4; j++) | |||
| 672 | ccv_nnc_8i_rowwise_packed_iq3xxs_level[index][j] = (uint8_t)((_ccv_nnc_8i_rowwise_packed_iq3xxs_value(index, j) - 1) / 2); | |||
| 673 | for (scale = 1; scale <= 16; scale++) | |||
| 674 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ3XXS_GRID_SIZE(256); index++) | |||
| 675 | for (j = 0; j < 4; j++) | |||
| 676 | ccv_nnc_8i_rowwise_packed_iq3xxs_scaled_value[scale][index][j] = (uint8_t)ccv_min((1 + ccv_nnc_8i_rowwise_packed_iq3xxs_level[index][j] * 2) * scale, 127)({ typeof ((1 + ccv_nnc_8i_rowwise_packed_iq3xxs_level[index] [j] * 2) * scale) _a = ((1 + ccv_nnc_8i_rowwise_packed_iq3xxs_level [index][j] * 2) * scale); typeof (127) _b = (127); (_a < _b ) ? _a : _b; }); | |||
| 677 | ccv_nnc_8i_rowwise_packed_iq3xxs_initialized = 1; | |||
| 678 | } | |||
| 679 | ||||
| 680 | static void _ccv_nnc_8i_rowwise_packed_iq3xxs_candidates_init(void) | |||
| 681 | { | |||
| 682 | if (ccv_nnc_8i_rowwise_packed_iq3xxs_candidates_initialized) | |||
| 683 | return; | |||
| 684 | _ccv_nnc_8i_rowwise_packed_iq3xxs_init(); | |||
| 685 | _ccv_nnc_8i_rowwise_packed_nearest_candidates(&ccv_nnc_8i_rowwise_packed_iq3xxs_level[0][0], CCV_NNC_8I_ROWWISE_PACKED_IQ3XXS_GRID_SIZE(256), 4, 8, CCV_NNC_8I_ROWWISE_PACKED_IQ3_CODE_COUNT(4096), CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES, &ccv_nnc_8i_rowwise_packed_iq3xxs_candidates[0][0]); | |||
| 686 | ccv_nnc_8i_rowwise_packed_iq3xxs_candidates_initialized = 1; | |||
| 687 | } | |||
| 688 | ||||
| 689 | static double _ccv_nnc_8i_rowwise_packed_iq3s_sse(const double* const ay, const double* const w, const int lane, const int scale, const int index) | |||
| 690 | { | |||
| 691 | const uint8_t* const mag = ccv_nnc_8i_rowwise_packed_iq3s_scaled_value[scale][index]; | |||
| 692 | double d = (double)mag[0] - ay[lane]; | |||
| 693 | double sse = w[lane] * d * d; | |||
| 694 | d = (double)mag[1] - ay[lane + 1]; | |||
| 695 | sse += w[lane + 1] * d * d; | |||
| 696 | d = (double)mag[2] - ay[lane + 2]; | |||
| 697 | sse += w[lane + 2] * d * d; | |||
| 698 | d = (double)mag[3] - ay[lane + 3]; | |||
| 699 | sse += w[lane + 3] * d * d; | |||
| 700 | return sse; | |||
| 701 | } | |||
| 702 | ||||
| 703 | static inline double _ccv_nnc_8i_rowwise_packed_iq2_cost(const double cost[8][3], const uint8_t* const levels) | |||
| 704 | { | |||
| 705 | double sse = 0; | |||
| 706 | int j; | |||
| 707 | for (j = 0; j < 8; j++) | |||
| 708 | sse += cost[j][levels[j]]; | |||
| 709 | return sse; | |||
| 710 | } | |||
| 711 | ||||
| 712 | static inline double _ccv_nnc_8i_rowwise_packed_safe_lower_bound(const double value) | |||
| 713 | { | |||
| 714 | return nextafter(value - (fabs(value) + 1) * (64 * DBL_EPSILON2.2204460492503131e-16), -INFINITY(__builtin_inff ())); | |||
| 715 | } | |||
| 716 | ||||
| 717 | static inline double _ccv_nnc_8i_rowwise_packed_iq2xxs_cost(const double cost[8][3], const double flipped_cost[8][3], const double flip_metric[8][3], const uint8_t* const levels, const uint8_t signs, const int flip_sign, int* const sign_index) | |||
| 718 | { | |||
| 719 | int best_flip = -1; | |||
| 720 | int j; | |||
| 721 | if (flip_sign) | |||
| 722 | { | |||
| 723 | double best_flip_metric = DBL_MAX1.7976931348623157e+308; | |||
| 724 | for (j = 0; j < 8; j++) | |||
| 725 | { | |||
| 726 | const double metric = flip_metric[j][levels[j]]; | |||
| 727 | if (metric < best_flip_metric) | |||
| 728 | { | |||
| 729 | best_flip_metric = metric; | |||
| 730 | best_flip = j; | |||
| 731 | } | |||
| 732 | } | |||
| 733 | } | |||
| 734 | double sse = 0; | |||
| 735 | for (j = 0; j < 8; j++) | |||
| 736 | sse += j == best_flip ? flipped_cost[j][levels[j]] : cost[j][levels[j]]; | |||
| 737 | *sign_index = (signs ^ (best_flip >= 0 ? (uint8_t)(1u << best_flip) : 0)) & 0x7f; | |||
| 738 | return sse; | |||
| 739 | } | |||
| 740 | ||||
| 741 | static void _ccv_nnc_8i_rowwise_packed_quant_iq2_xxs(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 742 | { | |||
| 743 | assert(ccv_nnc_8i_rowwise_packed_iq2xxs_initialized)((void) sizeof ((ccv_nnc_8i_rowwise_packed_iq2xxs_initialized ) ? 1 : 0), __extension__ ({ if (ccv_nnc_8i_rowwise_packed_iq2xxs_initialized ) ; else __assert_fail ("ccv_nnc_8i_rowwise_packed_iq2xxs_initialized" , "ccv_nnc_8i_rowwise.c", 743, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 744 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 745 | int best_scale_code = 0; | |||
| 746 | int best_grid[4] = {0}; | |||
| 747 | int best_sign[4] = {0}; | |||
| 748 | int scale_code; | |||
| 749 | for (scale_code = 0; scale_code < 16; scale_code++) | |||
| 750 | { | |||
| 751 | const int scale = ccv_nnc_8i_rowwise_packed_iq2xxs_scales[scale_code]; | |||
| 752 | double group_sse = 0; | |||
| 753 | int group_grid[4] = {0}; | |||
| 754 | int group_sign[4] = {0}; | |||
| 755 | int sg; | |||
| 756 | for (sg = 0; sg < 4; sg++) | |||
| 757 | { | |||
| 758 | double best_sub_sse = DBL_MAX1.7976931348623157e+308; | |||
| 759 | int best_sub_grid = 0; | |||
| 760 | int best_sub_sign = 0; | |||
| 761 | const int lane = sg * 8; | |||
| 762 | uint8_t signs = 0; | |||
| 763 | int negative_count = 0; | |||
| 764 | double cost[8][3]; | |||
| 765 | double flipped_cost[8][3]; | |||
| 766 | double flip_metric[8][3]; | |||
| 767 | int j; | |||
| 768 | for (j = 0; j < 8; j++) | |||
| 769 | { | |||
| 770 | if (y[lane + j] < 0) | |||
| 771 | { | |||
| 772 | signs |= (uint8_t)(1u << j); | |||
| 773 | negative_count++; | |||
| 774 | } | |||
| 775 | int level; | |||
| 776 | for (level = 0; level < 3; level++) | |||
| 777 | { | |||
| 778 | const int mag = ccv_nnc_8i_rowwise_packed_iq2xxs_scale_level[scale][level]; | |||
| 779 | const int q8 = (signs & (1u << j)) ? -mag : mag; | |||
| 780 | double d = (double)q8 - y[lane + j]; | |||
| 781 | cost[j][level] = w[lane + j] * d * d; | |||
| 782 | d = (double)-q8 - y[lane + j]; | |||
| 783 | flipped_cost[j][level] = w[lane + j] * d * d; | |||
| 784 | flip_metric[j][level] = w[lane + j] * (double)mag * fabs(y[lane + j]); | |||
| 785 | } | |||
| 786 | } | |||
| 787 | int target_code = 0; | |||
| 788 | int code_multiplier = 1; | |||
| 789 | uint8_t target_levels[8]; | |||
| 790 | double lower_bound_base = 0; | |||
| 791 | double lower_bound_ratio = DBL_MAX1.7976931348623157e+308; | |||
| 792 | double lower_bound_flip = DBL_MAX1.7976931348623157e+308; | |||
| 793 | for (j = 0; j < 8; j++) | |||
| 794 | { | |||
| 795 | int target_level = 0; | |||
| 796 | if (cost[j][1] < cost[j][target_level]) | |||
| 797 | target_level = 1; | |||
| 798 | if (cost[j][2] < cost[j][target_level]) | |||
| 799 | target_level = 2; | |||
| 800 | target_levels[j] = (uint8_t)target_level; | |||
| 801 | target_code += target_level * code_multiplier; | |||
| 802 | code_multiplier *= 3; | |||
| 803 | lower_bound_base += cost[j][target_level]; | |||
| 804 | int level; | |||
| 805 | for (level = 0; level < 3; level++) | |||
| 806 | { | |||
| 807 | if (level != target_level) | |||
| 808 | { | |||
| 809 | const int d = level - target_level; | |||
| 810 | lower_bound_ratio = ccv_min(lower_bound_ratio, (cost[j][level] - cost[j][target_level]) / (double)(d * d))({ typeof (lower_bound_ratio) _a = (lower_bound_ratio); typeof ((cost[j][level] - cost[j][target_level]) / (double)(d * d)) _b = ((cost[j][level] - cost[j][target_level]) / (double)(d * d)); (_a < _b) ? _a : _b; }); | |||
| 811 | } | |||
| 812 | lower_bound_flip = ccv_min(lower_bound_flip, flipped_cost[j][level] - cost[j][level])({ typeof (lower_bound_flip) _a = (lower_bound_flip); typeof ( flipped_cost[j][level] - cost[j][level]) _b = (flipped_cost[j ][level] - cost[j][level]); (_a < _b) ? _a : _b; }); | |||
| 813 | } | |||
| 814 | } | |||
| 815 | int candidate; | |||
| 816 | for (candidate = 0; candidate < CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES; candidate++) | |||
| 817 | { | |||
| 818 | const int index = ccv_nnc_8i_rowwise_packed_iq2xxs_candidates[target_code][candidate]; | |||
| 819 | const uint8_t* const levels = ccv_nnc_8i_rowwise_packed_iq2xxs_level[index]; | |||
| 820 | int sign_index; | |||
| 821 | const double sse = _ccv_nnc_8i_rowwise_packed_iq2xxs_cost(cost, flipped_cost, flip_metric, levels, signs, negative_count & 1, &sign_index); | |||
| 822 | if (sse < best_sub_sse || (sse == best_sub_sse && index < best_sub_grid)) | |||
| 823 | { | |||
| 824 | best_sub_sse = sse; | |||
| 825 | best_sub_grid = index; | |||
| 826 | best_sub_sign = sign_index; | |||
| 827 | } | |||
| 828 | } | |||
| 829 | const uint8_t* const cutoff_levels = ccv_nnc_8i_rowwise_packed_iq2xxs_level[ccv_nnc_8i_rowwise_packed_iq2xxs_candidates[target_code][CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES - 1]]; | |||
| 830 | int cutoff_distance = 0; | |||
| 831 | for (j = 0; j < 8; j++) | |||
| 832 | { | |||
| 833 | const int d = (int)cutoff_levels[j] - target_levels[j]; | |||
| 834 | cutoff_distance += d * d; | |||
| 835 | } | |||
| 836 | const double safe_ratio = nextafter(lower_bound_ratio, 0); | |||
| 837 | const double lower_bound = _ccv_nnc_8i_rowwise_packed_safe_lower_bound(lower_bound_base + safe_ratio * cutoff_distance + ((negative_count & 1) ? nextafter(lower_bound_flip, -INFINITY(__builtin_inff ())) : 0)); | |||
| 838 | if (!(best_sub_sse < lower_bound)) | |||
| 839 | { | |||
| 840 | int index; | |||
| 841 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2XXS_GRID_SIZE; index++) | |||
| 842 | { | |||
| 843 | int sign_index; | |||
| 844 | const double sse = _ccv_nnc_8i_rowwise_packed_iq2xxs_cost(cost, flipped_cost, flip_metric, ccv_nnc_8i_rowwise_packed_iq2xxs_level[index], signs, negative_count & 1, &sign_index); | |||
| 845 | if (sse < best_sub_sse || (sse == best_sub_sse && index < best_sub_grid)) | |||
| 846 | { | |||
| 847 | best_sub_sse = sse; | |||
| 848 | best_sub_grid = index; | |||
| 849 | best_sub_sign = sign_index; | |||
| 850 | } | |||
| 851 | } | |||
| 852 | } | |||
| 853 | group_sse += best_sub_sse; | |||
| 854 | group_grid[sg] = best_sub_grid; | |||
| 855 | group_sign[sg] = best_sub_sign; | |||
| 856 | } | |||
| 857 | if (group_sse < best_sse) | |||
| 858 | { | |||
| 859 | best_sse = group_sse; | |||
| 860 | best_scale_code = scale_code; | |||
| 861 | memcpy(best_grid, group_grid, sizeof(best_grid)); | |||
| 862 | memcpy(best_sign, group_sign, sizeof(best_sign)); | |||
| 863 | } | |||
| 864 | } | |||
| 865 | group->scale = best_scale_code; | |||
| 866 | group->signs = 0; | |||
| 867 | memcpy(group->grid, best_grid, sizeof(best_grid)); | |||
| 868 | int j; | |||
| 869 | for (j = 0; j < 4; j++) | |||
| 870 | group->signs |= (uint32_t)best_sign[j] << (j * 7); | |||
| 871 | for (j = 0; j < 32; j++) | |||
| 872 | { | |||
| 873 | const int sg = j >> 3; | |||
| 874 | const int lane = j & 7; | |||
| 875 | const uint8_t signs = ccv_nnc_8i_rowwise_packed_iq2xxs_ksigns[best_sign[sg]]; | |||
| 876 | const int mag = ccv_nnc_8i_rowwise_packed_iq2xxs_scaled_value[ccv_nnc_8i_rowwise_packed_iq2xxs_scales[best_scale_code]][best_grid[sg]][lane]; | |||
| 877 | group->q8[j] = (signs & (1u << lane)) ? -mag : mag; | |||
| 878 | } | |||
| 879 | } | |||
| 880 | ||||
| 881 | static void _ccv_nnc_8i_rowwise_packed_quant_iq2_s(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 882 | { | |||
| 883 | assert(ccv_nnc_8i_rowwise_packed_iq2s_initialized)((void) sizeof ((ccv_nnc_8i_rowwise_packed_iq2s_initialized) ? 1 : 0), __extension__ ({ if (ccv_nnc_8i_rowwise_packed_iq2s_initialized ) ; else __assert_fail ("ccv_nnc_8i_rowwise_packed_iq2s_initialized" , "ccv_nnc_8i_rowwise.c", 883, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 884 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 885 | int best_scale = 1; | |||
| 886 | int best_grid[2] = {0}; | |||
| 887 | double ay[16]; | |||
| 888 | double wy[16]; | |||
| 889 | uint32_t signs = 0; | |||
| 890 | int j; | |||
| 891 | for (j = 0; j < 16; j++) | |||
| 892 | { | |||
| 893 | ay[j] = fabs(y[j]); | |||
| 894 | wy[j] = w[j] * ay[j]; | |||
| 895 | if (y[j] < 0) | |||
| 896 | signs |= (1u << j); | |||
| 897 | } | |||
| 898 | double sub_sse[2][65]; | |||
| 899 | int sub_grid[2][65]; | |||
| 900 | int sg, scale; | |||
| 901 | for (sg = 0; sg < 2; sg++) | |||
| 902 | for (scale = 1; scale <= 64; scale++) | |||
| 903 | { | |||
| 904 | sub_sse[sg][scale] = DBL_MAX1.7976931348623157e+308; | |||
| 905 | sub_grid[sg][scale] = 0; | |||
| 906 | } | |||
| 907 | for (sg = 0; sg < 2; sg++) | |||
| 908 | { | |||
| 909 | const int lane = sg * 8; | |||
| 910 | double sum_y2 = 0; | |||
| 911 | for (j = 0; j < 8; j++) | |||
| 912 | sum_y2 += w[lane + j] * ay[lane + j] * ay[lane + j]; | |||
| 913 | int index; | |||
| 914 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2S_GRID_SIZE; index++) | |||
| 915 | { | |||
| 916 | double sw[3] = {0}; | |||
| 917 | double swy[3] = {0}; | |||
| 918 | for (j = 0; j < 8; j++) | |||
| 919 | { | |||
| 920 | const int level = ccv_nnc_8i_rowwise_packed_iq2s_level[index][j]; | |||
| 921 | sw[level] += w[lane + j]; | |||
| 922 | swy[level] += wy[lane + j]; | |||
| 923 | } | |||
| 924 | for (scale = 1; scale <= 64; scale++) | |||
| 925 | { | |||
| 926 | const double sse = sum_y2 + | |||
| 927 | sw[0] * (double)ccv_nnc_8i_rowwise_packed_iq2s_scale_level2[scale][0] - 2 * swy[0] * (double)ccv_nnc_8i_rowwise_packed_iq2s_scale_level[scale][0] + | |||
| 928 | sw[1] * (double)ccv_nnc_8i_rowwise_packed_iq2s_scale_level2[scale][1] - 2 * swy[1] * (double)ccv_nnc_8i_rowwise_packed_iq2s_scale_level[scale][1] + | |||
| 929 | sw[2] * (double)ccv_nnc_8i_rowwise_packed_iq2s_scale_level2[scale][2] - 2 * swy[2] * (double)ccv_nnc_8i_rowwise_packed_iq2s_scale_level[scale][2]; | |||
| 930 | if (sub_sse[sg][scale] == DBL_MAX1.7976931348623157e+308 || sse <= sub_sse[sg][scale] + ccv_max(1., fabs(sub_sse[sg][scale]))({ typeof (1.) _a = (1.); typeof (fabs(sub_sse[sg][scale])) _b = (fabs(sub_sse[sg][scale])); (_a > _b) ? _a : _b; }) * 1e-9) | |||
| 931 | { | |||
| 932 | const double exact_sse = _ccv_nnc_8i_rowwise_packed_iq2s_sse(ay, w, lane, scale, index); | |||
| 933 | if (exact_sse < sub_sse[sg][scale]) | |||
| 934 | { | |||
| 935 | sub_sse[sg][scale] = exact_sse; | |||
| 936 | sub_grid[sg][scale] = index; | |||
| 937 | } | |||
| 938 | } | |||
| 939 | } | |||
| 940 | } | |||
| 941 | } | |||
| 942 | for (scale = 1; scale <= 64; scale++) | |||
| 943 | { | |||
| 944 | const double group_sse = sub_sse[0][scale] + sub_sse[1][scale]; | |||
| 945 | if (group_sse < best_sse) | |||
| 946 | { | |||
| 947 | best_sse = group_sse; | |||
| 948 | best_scale = scale; | |||
| 949 | best_grid[0] = sub_grid[0][scale]; | |||
| 950 | best_grid[1] = sub_grid[1][scale]; | |||
| 951 | } | |||
| 952 | } | |||
| 953 | group->scale = best_scale; | |||
| 954 | group->signs = signs; | |||
| 955 | memcpy(group->grid, best_grid, sizeof(best_grid)); | |||
| 956 | for (j = 0; j < 16; j++) | |||
| 957 | { | |||
| 958 | const int sg = j >> 3; | |||
| 959 | const int lane = j & 7; | |||
| 960 | const int mag = ccv_nnc_8i_rowwise_packed_iq2s_scaled_value[best_scale][best_grid[sg]][lane]; | |||
| 961 | group->q8[j] = (signs & (1u << j)) ? -mag : mag; | |||
| 962 | } | |||
| 963 | } | |||
| 964 | ||||
| 965 | static void _ccv_nnc_8i_rowwise_packed_quant_iq2_xs(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 966 | { | |||
| 967 | assert(ccv_nnc_8i_rowwise_packed_iq2xs_initialized)((void) sizeof ((ccv_nnc_8i_rowwise_packed_iq2xs_initialized) ? 1 : 0), __extension__ ({ if (ccv_nnc_8i_rowwise_packed_iq2xs_initialized ) ; else __assert_fail ("ccv_nnc_8i_rowwise_packed_iq2xs_initialized" , "ccv_nnc_8i_rowwise.c", 967, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 968 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 969 | int best_scale_code = 0; | |||
| 970 | int best_grid = 0; | |||
| 971 | uint32_t signs = 0; | |||
| 972 | int j; | |||
| 973 | for (j = 0; j < 8; j++) | |||
| 974 | if (y[j] < 0) | |||
| 975 | signs |= (1u << j); | |||
| 976 | int scale_code; | |||
| 977 | for (scale_code = 0; scale_code < 16; scale_code++) | |||
| 978 | { | |||
| 979 | double cost[8][3]; | |||
| 980 | for (j = 0; j < 8; j++) | |||
| 981 | { | |||
| 982 | int level; | |||
| 983 | for (level = 0; level < 3; level++) | |||
| 984 | { | |||
| 985 | const int mag = ccv_nnc_8i_rowwise_packed_iq2xxs_scale_level[ccv_nnc_8i_rowwise_packed_iq2xxs_scales[scale_code]][level]; | |||
| 986 | const int q8 = (signs & (1u << j)) ? -mag : mag; | |||
| 987 | const double d = (double)q8 - y[j]; | |||
| 988 | cost[j][level] = w[j] * d * d; | |||
| 989 | } | |||
| 990 | } | |||
| 991 | int target_code = 0; | |||
| 992 | int code_multiplier = 1; | |||
| 993 | uint8_t target_levels[8]; | |||
| 994 | double lower_bound_base = 0; | |||
| 995 | double lower_bound_ratio = DBL_MAX1.7976931348623157e+308; | |||
| 996 | for (j = 0; j < 8; j++) | |||
| 997 | { | |||
| 998 | int target_level = 0; | |||
| 999 | if (cost[j][1] < cost[j][target_level]) | |||
| 1000 | target_level = 1; | |||
| 1001 | if (cost[j][2] < cost[j][target_level]) | |||
| 1002 | target_level = 2; | |||
| 1003 | target_levels[j] = (uint8_t)target_level; | |||
| 1004 | target_code += target_level * code_multiplier; | |||
| 1005 | code_multiplier *= 3; | |||
| 1006 | lower_bound_base += cost[j][target_level]; | |||
| 1007 | int level; | |||
| 1008 | for (level = 0; level < 3; level++) | |||
| 1009 | if (level != target_level) | |||
| 1010 | { | |||
| 1011 | const int d = level - target_level; | |||
| 1012 | lower_bound_ratio = ccv_min(lower_bound_ratio, (cost[j][level] - cost[j][target_level]) / (double)(d * d))({ typeof (lower_bound_ratio) _a = (lower_bound_ratio); typeof ((cost[j][level] - cost[j][target_level]) / (double)(d * d)) _b = ((cost[j][level] - cost[j][target_level]) / (double)(d * d)); (_a < _b) ? _a : _b; }); | |||
| 1013 | } | |||
| 1014 | } | |||
| 1015 | int candidate; | |||
| 1016 | for (candidate = 0; candidate < CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES; candidate++) | |||
| 1017 | { | |||
| 1018 | const int index = ccv_nnc_8i_rowwise_packed_iq2xs_candidates[target_code][candidate]; | |||
| 1019 | const uint8_t* const levels = ccv_nnc_8i_rowwise_packed_iq2xs_level[index]; | |||
| 1020 | const double sse = _ccv_nnc_8i_rowwise_packed_iq2_cost(cost, levels); | |||
| 1021 | if (sse < best_sse || (sse == best_sse && scale_code == best_scale_code && index < best_grid)) | |||
| 1022 | { | |||
| 1023 | best_sse = sse; | |||
| 1024 | best_scale_code = scale_code; | |||
| 1025 | best_grid = index; | |||
| 1026 | } | |||
| 1027 | } | |||
| 1028 | const uint8_t* const cutoff_levels = ccv_nnc_8i_rowwise_packed_iq2xs_level[ccv_nnc_8i_rowwise_packed_iq2xs_candidates[target_code][CCV_NNC_8I_ROWWISE_IQ2_CANDIDATES - 1]]; | |||
| 1029 | int cutoff_distance = 0; | |||
| 1030 | for (j = 0; j < 8; j++) | |||
| 1031 | { | |||
| 1032 | const int d = (int)cutoff_levels[j] - target_levels[j]; | |||
| 1033 | cutoff_distance += d * d; | |||
| 1034 | } | |||
| 1035 | const double lower_bound = _ccv_nnc_8i_rowwise_packed_safe_lower_bound(lower_bound_base + nextafter(lower_bound_ratio, 0) * cutoff_distance); | |||
| 1036 | if (!(best_sse < lower_bound)) | |||
| 1037 | { | |||
| 1038 | int index; | |||
| 1039 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ2XS_GRID_SIZE; index++) | |||
| 1040 | { | |||
| 1041 | const double sse = _ccv_nnc_8i_rowwise_packed_iq2_cost(cost, ccv_nnc_8i_rowwise_packed_iq2xs_level[index]); | |||
| 1042 | if (sse < best_sse || (sse == best_sse && scale_code == best_scale_code && index < best_grid)) | |||
| 1043 | { | |||
| 1044 | best_sse = sse; | |||
| 1045 | best_scale_code = scale_code; | |||
| 1046 | best_grid = index; | |||
| 1047 | } | |||
| 1048 | } | |||
| 1049 | } | |||
| 1050 | } | |||
| 1051 | group->scale = best_scale_code; | |||
| 1052 | group->grid[0] = best_grid; | |||
| 1053 | group->signs = signs; | |||
| 1054 | memset(group->q8, 0, sizeof(group->q8)); | |||
| 1055 | for (j = 0; j < 8; j++) | |||
| 1056 | { | |||
| 1057 | const int mag = ccv_nnc_8i_rowwise_packed_iq2xs_scaled_value[best_scale_code][best_grid][j]; | |||
| 1058 | group->q8[j] = (signs & (1u << j)) ? -mag : mag; | |||
| 1059 | } | |||
| 1060 | } | |||
| 1061 | ||||
| 1062 | static void _ccv_nnc_8i_rowwise_packed_quant_iq3_s(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 1063 | { | |||
| 1064 | assert(ccv_nnc_8i_rowwise_packed_iq3s_initialized)((void) sizeof ((ccv_nnc_8i_rowwise_packed_iq3s_initialized) ? 1 : 0), __extension__ ({ if (ccv_nnc_8i_rowwise_packed_iq3s_initialized ) ; else __assert_fail ("ccv_nnc_8i_rowwise_packed_iq3s_initialized" , "ccv_nnc_8i_rowwise.c", 1064, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1065 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1066 | int best_scale = 1; | |||
| 1067 | int best_grid[4] = {0}; | |||
| 1068 | double ay[16]; | |||
| 1069 | uint32_t signs = 0; | |||
| 1070 | int j; | |||
| 1071 | for (j = 0; j < 16; j++) | |||
| 1072 | { | |||
| 1073 | ay[j] = fabs(y[j]); | |||
| 1074 | if (y[j] < 0) | |||
| 1075 | signs |= (1u << j); | |||
| 1076 | } | |||
| 1077 | int scale; | |||
| 1078 | for (scale = 1; scale <= 16; scale++) | |||
| 1079 | { | |||
| 1080 | double group_sse = 0; | |||
| 1081 | int group_grid[4] = {0}; | |||
| 1082 | int sg; | |||
| 1083 | for (sg = 0; sg < 4; sg++) | |||
| 1084 | { | |||
| 1085 | double best_sub_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1086 | int best_sub_grid = 0; | |||
| 1087 | const int lane = sg * 4; | |||
| 1088 | int index; | |||
| 1089 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ3S_GRID_SIZE(512); index++) | |||
| 1090 | { | |||
| 1091 | const double sse = _ccv_nnc_8i_rowwise_packed_iq3s_sse(ay, w, lane, scale, index); | |||
| 1092 | if (sse < best_sub_sse) | |||
| 1093 | { | |||
| 1094 | best_sub_sse = sse; | |||
| 1095 | best_sub_grid = index; | |||
| 1096 | } | |||
| 1097 | } | |||
| 1098 | group_sse += best_sub_sse; | |||
| 1099 | group_grid[sg] = best_sub_grid; | |||
| 1100 | } | |||
| 1101 | if (group_sse < best_sse) | |||
| 1102 | { | |||
| 1103 | best_sse = group_sse; | |||
| 1104 | best_scale = scale; | |||
| 1105 | memcpy(best_grid, group_grid, sizeof(best_grid)); | |||
| 1106 | } | |||
| 1107 | } | |||
| 1108 | group->scale = best_scale; | |||
| 1109 | group->signs = signs; | |||
| 1110 | memcpy(group->grid, best_grid, sizeof(best_grid)); | |||
| 1111 | for (j = 0; j < 16; j++) | |||
| 1112 | { | |||
| 1113 | const int sg = j >> 2; | |||
| 1114 | const int lane = j & 3; | |||
| 1115 | const int mag = ccv_nnc_8i_rowwise_packed_iq3s_scaled_value[best_scale][best_grid[sg]][lane]; | |||
| 1116 | group->q8[j] = (signs & (1u << j)) ? -mag : mag; | |||
| 1117 | } | |||
| 1118 | } | |||
| 1119 | ||||
| 1120 | static void _ccv_nnc_8i_rowwise_packed_quant_iq3_xxs(const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 1121 | { | |||
| 1122 | assert(ccv_nnc_8i_rowwise_packed_iq3xxs_initialized)((void) sizeof ((ccv_nnc_8i_rowwise_packed_iq3xxs_initialized ) ? 1 : 0), __extension__ ({ if (ccv_nnc_8i_rowwise_packed_iq3xxs_initialized ) ; else __assert_fail ("ccv_nnc_8i_rowwise_packed_iq3xxs_initialized" , "ccv_nnc_8i_rowwise.c", 1122, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1123 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1124 | int best_scale = 1; | |||
| 1125 | int best_grid[2] = {0}; | |||
| 1126 | double ay[8]; | |||
| 1127 | uint32_t signs = 0; | |||
| 1128 | int j; | |||
| 1129 | for (j = 0; j < 8; j++) | |||
| 1130 | { | |||
| 1131 | ay[j] = fabs(y[j]); | |||
| 1132 | if (y[j] < 0) | |||
| 1133 | signs |= (1u << j); | |||
| 1134 | } | |||
| 1135 | int scale; | |||
| 1136 | for (scale = 1; scale <= 16; scale++) | |||
| 1137 | { | |||
| 1138 | double group_sse = 0; | |||
| 1139 | int group_grid[2] = {0}; | |||
| 1140 | int sg; | |||
| 1141 | for (sg = 0; sg < 2; sg++) | |||
| 1142 | { | |||
| 1143 | double best_sub_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1144 | int best_sub_grid = 0; | |||
| 1145 | const int lane = sg * 4; | |||
| 1146 | double cost[4][8]; | |||
| 1147 | for (j = 0; j < 4; j++) | |||
| 1148 | { | |||
| 1149 | int level; | |||
| 1150 | for (level = 0; level < 8; level++) | |||
| 1151 | { | |||
| 1152 | const int mag = ccv_min((1 + level * 2) * scale, 127)({ typeof ((1 + level * 2) * scale) _a = ((1 + level * 2) * scale ); typeof (127) _b = (127); (_a < _b) ? _a : _b; }); | |||
| 1153 | const double d = (double)mag - ay[lane + j]; | |||
| 1154 | cost[j][level] = w[lane + j] * d * d; | |||
| 1155 | } | |||
| 1156 | } | |||
| 1157 | int target_code = 0; | |||
| 1158 | uint8_t target_levels[4]; | |||
| 1159 | double lower_bound_base = 0; | |||
| 1160 | double lower_bound_ratio = DBL_MAX1.7976931348623157e+308; | |||
| 1161 | for (j = 0; j < 4; j++) | |||
| 1162 | { | |||
| 1163 | int target_level = 0; | |||
| 1164 | int level; | |||
| 1165 | for (level = 1; level < 8; level++) | |||
| 1166 | if (cost[j][level] < cost[j][target_level]) | |||
| 1167 | target_level = level; | |||
| 1168 | target_levels[j] = (uint8_t)target_level; | |||
| 1169 | target_code |= target_level << (j * 3); | |||
| 1170 | lower_bound_base += cost[j][target_level]; | |||
| 1171 | for (level = 0; level < 8; level++) | |||
| 1172 | if (level != target_level) | |||
| 1173 | { | |||
| 1174 | const int d = level - target_level; | |||
| 1175 | lower_bound_ratio = ccv_min(lower_bound_ratio, (cost[j][level] - cost[j][target_level]) / (double)(d * d))({ typeof (lower_bound_ratio) _a = (lower_bound_ratio); typeof ((cost[j][level] - cost[j][target_level]) / (double)(d * d)) _b = ((cost[j][level] - cost[j][target_level]) / (double)(d * d)); (_a < _b) ? _a : _b; }); | |||
| 1176 | } | |||
| 1177 | } | |||
| 1178 | int candidate; | |||
| 1179 | for (candidate = 0; candidate < CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES; candidate++) | |||
| 1180 | { | |||
| 1181 | const int index = ccv_nnc_8i_rowwise_packed_iq3xxs_candidates[target_code][candidate]; | |||
| 1182 | const uint8_t* const levels = ccv_nnc_8i_rowwise_packed_iq3xxs_level[index]; | |||
| 1183 | const double sse = cost[0][levels[0]] + cost[1][levels[1]] + cost[2][levels[2]] + cost[3][levels[3]]; | |||
| 1184 | if (sse < best_sub_sse || (sse == best_sub_sse && index < best_sub_grid)) | |||
| 1185 | { | |||
| 1186 | best_sub_sse = sse; | |||
| 1187 | best_sub_grid = index; | |||
| 1188 | } | |||
| 1189 | } | |||
| 1190 | const uint8_t* const cutoff_levels = ccv_nnc_8i_rowwise_packed_iq3xxs_level[ccv_nnc_8i_rowwise_packed_iq3xxs_candidates[target_code][CCV_NNC_8I_ROWWISE_IQ3_CANDIDATES - 1]]; | |||
| 1191 | int cutoff_distance = 0; | |||
| 1192 | for (j = 0; j < 4; j++) | |||
| 1193 | { | |||
| 1194 | const int d = (int)cutoff_levels[j] - target_levels[j]; | |||
| 1195 | cutoff_distance += d * d; | |||
| 1196 | } | |||
| 1197 | const double lower_bound = _ccv_nnc_8i_rowwise_packed_safe_lower_bound(lower_bound_base + nextafter(lower_bound_ratio, 0) * cutoff_distance); | |||
| 1198 | if (!(best_sub_sse < lower_bound)) | |||
| 1199 | { | |||
| 1200 | int index; | |||
| 1201 | for (index = 0; index < CCV_NNC_8I_ROWWISE_PACKED_IQ3XXS_GRID_SIZE(256); index++) | |||
| 1202 | { | |||
| 1203 | const uint8_t* const levels = ccv_nnc_8i_rowwise_packed_iq3xxs_level[index]; | |||
| 1204 | const double sse = cost[0][levels[0]] + cost[1][levels[1]] + cost[2][levels[2]] + cost[3][levels[3]]; | |||
| 1205 | if (sse < best_sub_sse || (sse == best_sub_sse && index < best_sub_grid)) | |||
| 1206 | { | |||
| 1207 | best_sub_sse = sse; | |||
| 1208 | best_sub_grid = index; | |||
| 1209 | } | |||
| 1210 | } | |||
| 1211 | } | |||
| 1212 | group_sse += best_sub_sse; | |||
| 1213 | group_grid[sg] = best_sub_grid; | |||
| 1214 | } | |||
| 1215 | if (group_sse < best_sse) | |||
| 1216 | { | |||
| 1217 | best_sse = group_sse; | |||
| 1218 | best_scale = scale; | |||
| 1219 | memcpy(best_grid, group_grid, sizeof(best_grid)); | |||
| 1220 | } | |||
| 1221 | } | |||
| 1222 | group->scale = best_scale; | |||
| 1223 | group->signs = signs; | |||
| 1224 | memcpy(group->grid, best_grid, sizeof(best_grid)); | |||
| 1225 | memset(group->q8, 0, sizeof(group->q8)); | |||
| 1226 | for (j = 0; j < 8; j++) | |||
| 1227 | { | |||
| 1228 | const int sg = j >> 2; | |||
| 1229 | const int lane = j & 3; | |||
| 1230 | const int mag = ccv_nnc_8i_rowwise_packed_iq3xxs_scaled_value[best_scale][best_grid[sg]][lane]; | |||
| 1231 | group->q8[j] = (signs & (1u << j)) ? -mag : mag; | |||
| 1232 | } | |||
| 1233 | } | |||
| 1234 | ||||
| 1235 | static void _ccv_nnc_8i_rowwise_packed_quant_group(const int format, const double* const y, const double* const w, ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 1236 | { | |||
| 1237 | switch (format) | |||
| 1238 | { | |||
| 1239 | case CCV_NNC_QX_8I_ROWWISE_Q5_K: | |||
| 1240 | _ccv_nnc_8i_rowwise_packed_quant_q5(y, w, group); | |||
| 1241 | break; | |||
| 1242 | case CCV_NNC_QX_8I_ROWWISE_Q6_K: | |||
| 1243 | _ccv_nnc_8i_rowwise_packed_quant_q6(y, w, group); | |||
| 1244 | break; | |||
| 1245 | case CCV_NNC_QX_8I_ROWWISE_Q4_K: | |||
| 1246 | _ccv_nnc_8i_rowwise_packed_quant_q4(y, w, group); | |||
| 1247 | break; | |||
| 1248 | case CCV_NNC_QX_8I_ROWWISE_Q3_K: | |||
| 1249 | _ccv_nnc_8i_rowwise_packed_quant_q3(y, w, group); | |||
| 1250 | break; | |||
| 1251 | case CCV_NNC_QX_8I_ROWWISE_Q2_K: | |||
| 1252 | _ccv_nnc_8i_rowwise_packed_quant_q2(y, w, group); | |||
| 1253 | break; | |||
| 1254 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XXS: | |||
| 1255 | _ccv_nnc_8i_rowwise_packed_quant_iq2_xxs(y, w, group); | |||
| 1256 | break; | |||
| 1257 | case CCV_NNC_QX_8I_ROWWISE_IQ2_S: | |||
| 1258 | _ccv_nnc_8i_rowwise_packed_quant_iq2_s(y, w, group); | |||
| 1259 | break; | |||
| 1260 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XS: | |||
| 1261 | _ccv_nnc_8i_rowwise_packed_quant_iq2_xs(y, w, group); | |||
| 1262 | break; | |||
| 1263 | case CCV_NNC_QX_8I_ROWWISE_IQ3_S: | |||
| 1264 | _ccv_nnc_8i_rowwise_packed_quant_iq3_s(y, w, group); | |||
| 1265 | break; | |||
| 1266 | case CCV_NNC_QX_8I_ROWWISE_IQ3_XXS: | |||
| 1267 | _ccv_nnc_8i_rowwise_packed_quant_iq3_xxs(y, w, group); | |||
| 1268 | break; | |||
| 1269 | default: | |||
| 1270 | assert(0)((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "ccv_nnc_8i_rowwise.c", 1270, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1271 | } | |||
| 1272 | } | |||
| 1273 | ||||
| 1274 | static void _ccv_nnc_8i_rowwise_packed_pack_group(uint8_t* const output, const size_t group_index, const int format, const ccv_nnc_8i_rowwise_packed_group_t* const group) | |||
| 1275 | { | |||
| 1276 | const size_t bit_offset = group_index * ccv_nnc_8i_rowwise_x_group_bits(format); | |||
| 1277 | size_t bit = bit_offset; | |||
| 1278 | int j; | |||
| 1279 | switch (format) | |||
| 1280 | { | |||
| 1281 | case CCV_NNC_QX_8I_ROWWISE_Q5_K: | |||
| 1282 | for (j = 0; j < 16; j++, bit += 5) | |||
| 1283 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->q[j] + 16), 5); | |||
| 1284 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->m - 1), 3); | |||
| 1285 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 3, (uint32_t)(group->b + 16), 5); | |||
| 1286 | break; | |||
| 1287 | case CCV_NNC_QX_8I_ROWWISE_Q6_K: | |||
| 1288 | for (j = 0; j < 8; j++, bit += 6) | |||
| 1289 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->q[j] & 0x3f), 6); | |||
| 1290 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->m - 1), 2); | |||
| 1291 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 2, (uint32_t)(group->b & 3), 2); | |||
| 1292 | break; | |||
| 1293 | case CCV_NNC_QX_8I_ROWWISE_Q4_K: | |||
| 1294 | for (j = 0; j < 16; j++, bit += 4) | |||
| 1295 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->q[j] + 8), 4); | |||
| 1296 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->m - 1), 4); | |||
| 1297 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 4, (uint32_t)(group->b + 8), 4); | |||
| 1298 | break; | |||
| 1299 | case CCV_NNC_QX_8I_ROWWISE_Q3_K: | |||
| 1300 | for (j = 0; j < 16; j++, bit += 3) | |||
| 1301 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->q[j] + 4), 3); | |||
| 1302 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->m - 1), 5); | |||
| 1303 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 5, (uint32_t)(group->b / 2 + 4), 3); | |||
| 1304 | break; | |||
| 1305 | case CCV_NNC_QX_8I_ROWWISE_Q2_K: | |||
| 1306 | for (j = 0; j < 16; j++, bit += 2) | |||
| 1307 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)group->q[j], 2); | |||
| 1308 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)(group->m - 1), 6); | |||
| 1309 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 6, (uint32_t)(group->z >> 3), 4); | |||
| 1310 | break; | |||
| 1311 | case CCV_NNC_QX_8I_ROWWISE_IQ2_S: | |||
| 1312 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)group->grid[0], 10); | |||
| 1313 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 10, (uint32_t)group->grid[1], 10); | |||
| 1314 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 20, group->signs, 16); | |||
| 1315 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 36, (uint32_t)(group->scale - 1), 6); | |||
| 1316 | break; | |||
| 1317 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XS: | |||
| 1318 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)group->grid[0], 9); | |||
| 1319 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 9, group->signs, 8); | |||
| 1320 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 17, (uint32_t)group->scale, 4); | |||
| 1321 | break; | |||
| 1322 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XXS: | |||
| 1323 | for (j = 0; j < 4; j++) | |||
| 1324 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + j * 8, (uint32_t)group->grid[j], 8); | |||
| 1325 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 32, group->signs, 28); | |||
| 1326 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 60, (uint32_t)group->scale, 4); | |||
| 1327 | break; | |||
| 1328 | case CCV_NNC_QX_8I_ROWWISE_IQ3_S: | |||
| 1329 | for (j = 0; j < 4; j++) | |||
| 1330 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + j * 9, (uint32_t)group->grid[j], 9); | |||
| 1331 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 36, group->signs, 16); | |||
| 1332 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 52, (uint32_t)(group->scale - 1), 4); | |||
| 1333 | break; | |||
| 1334 | case CCV_NNC_QX_8I_ROWWISE_IQ3_XXS: | |||
| 1335 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit, (uint32_t)group->grid[0], 8); | |||
| 1336 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 8, (uint32_t)group->grid[1], 8); | |||
| 1337 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 16, group->signs, 8); | |||
| 1338 | _ccv_nnc_8i_rowwise_packed_write_bits(output, bit + 24, (uint32_t)(group->scale - 1), 4); | |||
| 1339 | break; | |||
| 1340 | default: | |||
| 1341 | assert(0)((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "ccv_nnc_8i_rowwise.c", 1341, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1342 | } | |||
| 1343 | } | |||
| 1344 | ||||
| 1345 | static void _ccv_nnc_8i_rowwise_packed_decode_group(const uint8_t* const input, const size_t group_index, const int format, int* const q8) | |||
| 1346 | { | |||
| 1347 | static const int q2_xs_scales[16] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32}; | |||
| 1348 | const size_t bit_offset = group_index * ccv_nnc_8i_rowwise_x_group_bits(format); | |||
| 1349 | size_t bit = bit_offset; | |||
| 1350 | int j; | |||
| 1351 | switch (format) | |||
| 1352 | { | |||
| 1353 | case CCV_NNC_QX_8I_ROWWISE_Q5_K: { | |||
| 1354 | int q[16]; | |||
| 1355 | for (j = 0; j < 16; j++, bit += 5) | |||
| 1356 | q[j] = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 5) - 16; | |||
| 1357 | const int m = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 3) + 1; | |||
| 1358 | const int b = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 3, 5) - 16; | |||
| 1359 | for (j = 0; j < 16; j++) | |||
| 1360 | q8[j] = q[j] * m + b; | |||
| 1361 | break; | |||
| 1362 | } | |||
| 1363 | case CCV_NNC_QX_8I_ROWWISE_Q6_K: { | |||
| 1364 | int q[8]; | |||
| 1365 | for (j = 0; j < 8; j++, bit += 6) | |||
| 1366 | q[j] = _ccv_nnc_8i_rowwise_packed_sign_extend(_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 6), 6); | |||
| 1367 | const int m = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 2) + 1; | |||
| 1368 | const int b = _ccv_nnc_8i_rowwise_packed_sign_extend(_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 2, 2), 2); | |||
| 1369 | for (j = 0; j < 8; j++) | |||
| 1370 | q8[j] = q[j] * m + b; | |||
| 1371 | break; | |||
| 1372 | } | |||
| 1373 | case CCV_NNC_QX_8I_ROWWISE_Q4_K: { | |||
| 1374 | int q[16]; | |||
| 1375 | for (j = 0; j < 16; j++, bit += 4) | |||
| 1376 | q[j] = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 4) - 8; | |||
| 1377 | const int m = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 4) + 1; | |||
| 1378 | const int b = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 4, 4) - 8; | |||
| 1379 | for (j = 0; j < 16; j++) | |||
| 1380 | q8[j] = q[j] * m + b; | |||
| 1381 | break; | |||
| 1382 | } | |||
| 1383 | case CCV_NNC_QX_8I_ROWWISE_Q3_K: { | |||
| 1384 | int q[16]; | |||
| 1385 | for (j = 0; j < 16; j++, bit += 3) | |||
| 1386 | q[j] = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 3) - 4; | |||
| 1387 | const int m = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 5) + 1; | |||
| 1388 | const int b = ((int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 5, 3) - 4) << 1; | |||
| 1389 | for (j = 0; j < 16; j++) | |||
| 1390 | q8[j] = q[j] * m + b; | |||
| 1391 | break; | |||
| 1392 | } | |||
| 1393 | case CCV_NNC_QX_8I_ROWWISE_Q2_K: { | |||
| 1394 | int q[16]; | |||
| 1395 | for (j = 0; j < 16; j++, bit += 2) | |||
| 1396 | q[j] = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 2); | |||
| 1397 | const int m = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 6) + 1; | |||
| 1398 | const int z = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 6, 4) << 3; | |||
| 1399 | for (j = 0; j < 16; j++) | |||
| 1400 | q8[j] = q[j] * m - z; | |||
| 1401 | break; | |||
| 1402 | } | |||
| 1403 | case CCV_NNC_QX_8I_ROWWISE_IQ2_S: { | |||
| 1404 | const int grid0 = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 10); | |||
| 1405 | const int grid1 = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 10, 10); | |||
| 1406 | const uint32_t signs = _ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 20, 16); | |||
| 1407 | const int scale = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 36, 6) + 1; | |||
| 1408 | for (j = 0; j < 8; j++) | |||
| 1409 | { | |||
| 1410 | const int mag0 = ccv_min(_ccv_nnc_8i_rowwise_packed_iq2_value(ccv_nnc_8i_rowwise_packed_iq2s_grid, grid0, j) * scale, 127)({ typeof (_ccv_nnc_8i_rowwise_packed_iq2_value(ccv_nnc_8i_rowwise_packed_iq2s_grid , grid0, j) * scale) _a = (_ccv_nnc_8i_rowwise_packed_iq2_value (ccv_nnc_8i_rowwise_packed_iq2s_grid, grid0, j) * scale); typeof (127) _b = (127); (_a < _b) ? _a : _b; }); | |||
| 1411 | const int mag1 = ccv_min(_ccv_nnc_8i_rowwise_packed_iq2_value(ccv_nnc_8i_rowwise_packed_iq2s_grid, grid1, j) * scale, 127)({ typeof (_ccv_nnc_8i_rowwise_packed_iq2_value(ccv_nnc_8i_rowwise_packed_iq2s_grid , grid1, j) * scale) _a = (_ccv_nnc_8i_rowwise_packed_iq2_value (ccv_nnc_8i_rowwise_packed_iq2s_grid, grid1, j) * scale); typeof (127) _b = (127); (_a < _b) ? _a : _b; }); | |||
| 1412 | q8[j] = (signs & (1u << j)) ? -mag0 : mag0; | |||
| 1413 | q8[8 + j] = (signs & (1u << (8 + j))) ? -mag1 : mag1; | |||
| 1414 | } | |||
| 1415 | break; | |||
| 1416 | } | |||
| 1417 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XS: { | |||
| 1418 | const int grid0 = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 9); | |||
| 1419 | const uint32_t signs = _ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 9, 8); | |||
| 1420 | const int scale = q2_xs_scales[_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 17, 4)]; | |||
| 1421 | for (j = 0; j < 8; j++) | |||
| 1422 | { | |||
| 1423 | const int mag = ccv_min(_ccv_nnc_8i_rowwise_packed_iq2_value(ccv_nnc_8i_rowwise_packed_iq2xs_grid, grid0, j) * scale, 127)({ typeof (_ccv_nnc_8i_rowwise_packed_iq2_value(ccv_nnc_8i_rowwise_packed_iq2xs_grid , grid0, j) * scale) _a = (_ccv_nnc_8i_rowwise_packed_iq2_value (ccv_nnc_8i_rowwise_packed_iq2xs_grid, grid0, j) * scale); typeof (127) _b = (127); (_a < _b) ? _a : _b; }); | |||
| 1424 | q8[j] = (signs & (1u << j)) ? -mag : mag; | |||
| 1425 | } | |||
| 1426 | break; | |||
| 1427 | } | |||
| 1428 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XXS: { | |||
| 1429 | int grid[4]; | |||
| 1430 | for (j = 0; j < 4; j++) | |||
| 1431 | grid[j] = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + j * 8, 8); | |||
| 1432 | const uint32_t sign_codes = _ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 32, 28); | |||
| 1433 | const int scale = ccv_nnc_8i_rowwise_packed_iq2xxs_scales[_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 60, 4)]; | |||
| 1434 | int sg; | |||
| 1435 | for (sg = 0; sg < 4; sg++) | |||
| 1436 | { | |||
| 1437 | const uint8_t signs = ccv_nnc_8i_rowwise_packed_iq2xxs_ksigns[(sign_codes >> (sg * 7)) & 0x7f]; | |||
| 1438 | for (j = 0; j < 8; j++) | |||
| 1439 | { | |||
| 1440 | const int lane = sg * 8 + j; | |||
| 1441 | const int mag = ccv_min(_ccv_nnc_8i_rowwise_packed_iq2xxs_value(grid[sg], j) * scale, 127)({ typeof (_ccv_nnc_8i_rowwise_packed_iq2xxs_value(grid[sg], j ) * scale) _a = (_ccv_nnc_8i_rowwise_packed_iq2xxs_value(grid [sg], j) * scale); typeof (127) _b = (127); (_a < _b) ? _a : _b; }); | |||
| 1442 | q8[lane] = (signs & (1u << j)) ? -mag : mag; | |||
| 1443 | } | |||
| 1444 | } | |||
| 1445 | break; | |||
| 1446 | } | |||
| 1447 | case CCV_NNC_QX_8I_ROWWISE_IQ3_S: { | |||
| 1448 | int grid[4]; | |||
| 1449 | for (j = 0; j < 4; j++) | |||
| 1450 | grid[j] = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + j * 9, 9); | |||
| 1451 | const uint32_t signs = _ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 36, 16); | |||
| 1452 | const int scale = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 52, 4) + 1; | |||
| 1453 | int sg; | |||
| 1454 | for (sg = 0; sg < 4; sg++) | |||
| 1455 | for (j = 0; j < 4; j++) | |||
| 1456 | { | |||
| 1457 | const int lane = sg * 4 + j; | |||
| 1458 | const int mag = ccv_min(_ccv_nnc_8i_rowwise_packed_iq3s_value(grid[sg], j) * scale, 127)({ typeof (_ccv_nnc_8i_rowwise_packed_iq3s_value(grid[sg], j) * scale) _a = (_ccv_nnc_8i_rowwise_packed_iq3s_value(grid[sg ], j) * scale); typeof (127) _b = (127); (_a < _b) ? _a : _b ; }); | |||
| 1459 | q8[lane] = (signs & (1u << lane)) ? -mag : mag; | |||
| 1460 | } | |||
| 1461 | break; | |||
| 1462 | } | |||
| 1463 | case CCV_NNC_QX_8I_ROWWISE_IQ3_XXS: { | |||
| 1464 | const int grid0 = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit, 8); | |||
| 1465 | const int grid1 = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 8, 8); | |||
| 1466 | const uint32_t signs = _ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 16, 8); | |||
| 1467 | const int scale = (int)_ccv_nnc_8i_rowwise_packed_read_bits(input, bit + 24, 4) + 1; | |||
| 1468 | for (j = 0; j < 4; j++) | |||
| 1469 | { | |||
| 1470 | const int mag0 = ccv_min(_ccv_nnc_8i_rowwise_packed_iq3xxs_value(grid0, j) * scale, 127)({ typeof (_ccv_nnc_8i_rowwise_packed_iq3xxs_value(grid0, j) * scale) _a = (_ccv_nnc_8i_rowwise_packed_iq3xxs_value(grid0, j ) * scale); typeof (127) _b = (127); (_a < _b) ? _a : _b; } ); | |||
| 1471 | const int mag1 = ccv_min(_ccv_nnc_8i_rowwise_packed_iq3xxs_value(grid1, j) * scale, 127)({ typeof (_ccv_nnc_8i_rowwise_packed_iq3xxs_value(grid1, j) * scale) _a = (_ccv_nnc_8i_rowwise_packed_iq3xxs_value(grid1, j ) * scale); typeof (127) _b = (127); (_a < _b) ? _a : _b; } ); | |||
| 1472 | q8[j] = (signs & (1u << j)) ? -mag0 : mag0; | |||
| 1473 | q8[4 + j] = (signs & (1u << (4 + j))) ? -mag1 : mag1; | |||
| 1474 | } | |||
| 1475 | break; | |||
| 1476 | } | |||
| 1477 | default: | |||
| 1478 | assert(0)((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "ccv_nnc_8i_rowwise.c", 1478, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1479 | } | |||
| 1480 | } | |||
| 1481 | ||||
| 1482 | // Regular normalized H256, matching NAInt8MatMul activation staging. This is | |||
| 1483 | // not the Sylvester WHT. Transform the existing floating row before fitting scales. | |||
| 1484 | static void _ccv_nnc_8i_rowwise_hadamard_256(double* const row, const size_t row_length) | |||
| 1485 | { | |||
| 1486 | size_t base; | |||
| 1487 | for (base = 0; base < row_length; base += 256) | |||
| 1488 | { | |||
| 1489 | int stride; | |||
| 1490 | for (stride = 1; stride < 256; stride *= 4) | |||
| 1491 | { | |||
| 1492 | int i, j; | |||
| 1493 | for (i = 0; i < 256; i += stride * 4) | |||
| 1494 | for (j = 0; j < stride; j++) | |||
| 1495 | { | |||
| 1496 | double* const v = row + base + i + j; | |||
| 1497 | const double a = v[0], b = v[stride], c = v[stride * 2], d = v[stride * 3]; | |||
| 1498 | v[0] = a + b + c - d; | |||
| 1499 | v[stride] = a + b - c + d; | |||
| 1500 | v[stride * 2] = a - b + c + d; | |||
| 1501 | v[stride * 3] = -a + b + c + d; | |||
| 1502 | } | |||
| 1503 | } | |||
| 1504 | int i; | |||
| 1505 | for (i = 0; i < 256; i++) | |||
| 1506 | row[base + i] *= 1. / 16.; | |||
| 1507 | } | |||
| 1508 | } | |||
| 1509 | ||||
| 1510 | CCV_WARN_UNUSED(size_t)size_t __attribute__((warn_unused_result)) ccv_nnc_quantize_8i_rowwise_x(const void* input, const int datatype, const int memory_type, const size_t input_length, const size_t row_length, const int format, const float* const imatrix, const size_t imatrix_length, void* output, const size_t output_length) | |||
| 1511 | { | |||
| 1512 | if (row_length == 0 || input_length % row_length != 0 || | |||
| ||||
| 1513 | ((format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) && row_length % 256 != 0)) | |||
| 1514 | return 0; | |||
| 1515 | assert(datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F)((void) sizeof ((datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ? 1 : 0), __extension__ ({ if (datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ; else __assert_fail ("datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F" , "ccv_nnc_8i_rowwise.c", 1515, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1516 | assert(memory_type == CCV_TENSOR_CPU_MEMORY)((void) sizeof ((memory_type == CCV_TENSOR_CPU_MEMORY) ? 1 : 0 ), __extension__ ({ if (memory_type == CCV_TENSOR_CPU_MEMORY) ; else __assert_fail ("memory_type == CCV_TENSOR_CPU_MEMORY" , "ccv_nnc_8i_rowwise.c", 1516, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1517 | assert(row_length > 0)((void) sizeof ((row_length > 0) ? 1 : 0), __extension__ ( { if (row_length > 0) ; else __assert_fail ("row_length > 0" , "ccv_nnc_8i_rowwise.c", 1517, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1518 | assert(input_length % row_length == 0)((void) sizeof ((input_length % row_length == 0) ? 1 : 0), __extension__ ({ if (input_length % row_length == 0) ; else __assert_fail ( "input_length % row_length == 0", "ccv_nnc_8i_rowwise.c", 1518 , __extension__ __PRETTY_FUNCTION__); })); | |||
| 1519 | const int codec = format & CCV_NNC_QX_8I_ROWWISE_FORMAT_MASK; | |||
| 1520 | const size_t row_count = input_length / row_length; | |||
| 1521 | if (!_ccv_nnc_8i_rowwise_imatrix_is_valid(imatrix, imatrix_length, row_length, row_count)) | |||
| 1522 | return 0; | |||
| 1523 | const size_t group_size = ccv_nnc_8i_rowwise_x_group_size(format); | |||
| 1524 | const int group_bits = ccv_nnc_8i_rowwise_x_group_bits(format); | |||
| 1525 | const size_t groups_per_row = (row_length + group_size - 1) / group_size; | |||
| 1526 | const size_t padded_row_length = groups_per_row * group_size; | |||
| 1527 | const size_t scale_offset = _ccv_nnc_8i_rowwise_packed_scale_offset(format, input_length, row_length); | |||
| 1528 | const size_t output_size = scale_offset + row_count * CCV_GET_DATA_TYPE_SIZE(datatype)_ccv_get_data_type_size[((datatype) & 0xFF000) >> 12 ]; | |||
| 1529 | if (output_length < output_size) | |||
| 1530 | return 0; | |||
| 1531 | switch (codec) | |||
| 1532 | { | |||
| 1533 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XXS: | |||
| 1534 | _ccv_nnc_8i_rowwise_packed_iq2xxs_candidates_init(); | |||
| 1535 | break; | |||
| 1536 | case CCV_NNC_QX_8I_ROWWISE_IQ2_XS: | |||
| 1537 | _ccv_nnc_8i_rowwise_packed_iq2xs_candidates_init(); | |||
| 1538 | break; | |||
| 1539 | case CCV_NNC_QX_8I_ROWWISE_IQ2_S: | |||
| 1540 | _ccv_nnc_8i_rowwise_packed_iq2s_init(); | |||
| 1541 | break; | |||
| 1542 | case CCV_NNC_QX_8I_ROWWISE_IQ3_S: | |||
| 1543 | _ccv_nnc_8i_rowwise_packed_iq3s_init(); | |||
| 1544 | break; | |||
| 1545 | case CCV_NNC_QX_8I_ROWWISE_IQ3_XXS: | |||
| 1546 | _ccv_nnc_8i_rowwise_packed_iq3xxs_candidates_init(); | |||
| 1547 | break; | |||
| 1548 | } | |||
| 1549 | uint8_t* const u8 = (uint8_t*)output; | |||
| 1550 | uint8_t* const scales = u8 + scale_offset; | |||
| 1551 | memset(u8, 0, scale_offset); | |||
| 1552 | const size_t row_bits = groups_per_row * group_bits; | |||
| 1553 | size_t rows_per_chunk; | |||
| 1554 | switch (row_bits & 7) | |||
| 1555 | { | |||
| 1556 | case 0: | |||
| 1557 | rows_per_chunk = 1; | |||
| 1558 | break; | |||
| 1559 | case 4: | |||
| 1560 | rows_per_chunk = 2; | |||
| 1561 | break; | |||
| 1562 | case 2: | |||
| 1563 | case 6: | |||
| 1564 | rows_per_chunk = 4; | |||
| 1565 | break; | |||
| 1566 | default: | |||
| 1567 | rows_per_chunk = 8; | |||
| 1568 | break; | |||
| 1569 | } | |||
| 1570 | rows_per_chunk = ccv_max(rows_per_chunk, (size_t)8)({ typeof (rows_per_chunk) _a = (rows_per_chunk); typeof ((size_t )8) _b = ((size_t)8); (_a > _b) ? _a : _b; }); | |||
| 1571 | const size_t row_chunks = (row_count + rows_per_chunk - 1) / rows_per_chunk; | |||
| 1572 | #ifdef USE_DISPATCH | |||
| 1573 | dispatch_apply(row_chunks, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t chunk_idx) { | |||
| 1574 | #else | |||
| 1575 | parallel_for(chunk_idx, (int)row_chunks){ int chunk_idx; for ((chunk_idx) = 0; (chunk_idx) < ((int )row_chunks); (chunk_idx)++) { { | |||
| 1576 | #endif | |||
| 1577 | const size_t chunk_begin = (size_t)chunk_idx * rows_per_chunk; | |||
| 1578 | const size_t chunk_end = ccv_min(chunk_begin + rows_per_chunk, row_count)({ typeof (chunk_begin + rows_per_chunk) _a = (chunk_begin + rows_per_chunk ); typeof (row_count) _b = (row_count); (_a < _b) ? _a : _b ; }); | |||
| 1579 | double* const row = (double*)ccmallocmalloc(sizeof(double) * padded_row_length); | |||
| 1580 | double* const weights = (double*)ccmallocmalloc(sizeof(double) * padded_row_length); | |||
| 1581 | ccv_nnc_8i_rowwise_packed_group_t* const groups = (ccv_nnc_8i_rowwise_packed_group_t*)ccmallocmalloc(sizeof(ccv_nnc_8i_rowwise_packed_group_t) * groups_per_row); | |||
| 1582 | ccv_nnc_8i_rowwise_packed_group_t* const best_groups = (ccv_nnc_8i_rowwise_packed_group_t*)ccmallocmalloc(sizeof(ccv_nnc_8i_rowwise_packed_group_t) * groups_per_row); | |||
| 1583 | size_t i; | |||
| 1584 | for (i = chunk_begin; i < chunk_end; i++) | |||
| 1585 | { | |||
| 1586 | const size_t row_start = i * row_length; | |||
| 1587 | const float* const row_imatrix = _ccv_nnc_8i_rowwise_imatrix_for_row(imatrix, imatrix_length, row_length, row_count, i); | |||
| 1588 | _ccv_nnc_8i_rowwise_packed_read_row(input, datatype, row_start, row_length, padded_row_length, row); | |||
| 1589 | if (format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) | |||
| 1590 | _ccv_nnc_8i_rowwise_hadamard_256(row, row_length); | |||
| 1591 | double max_abs = 0; | |||
| 1592 | size_t j; | |||
| 1593 | for (j = 0; j < row_length; j++) | |||
| 1594 | { | |||
| 1595 | max_abs = ccv_max(max_abs, fabs(row[j]))({ typeof (max_abs) _a = (max_abs); typeof (fabs(row[j])) _b = (fabs(row[j])); (_a > _b) ? _a : _b; }); | |||
| 1596 | weights[j] = _ccv_nnc_8i_rowwise_weight(row_imatrix, j); | |||
| 1597 | } | |||
| 1598 | for (; j
| |||
| 1599 | weights[j] = 0; | |||
| 1600 | double scale = max_abs / 127.; | |||
| 1601 | double best_scale = 0; | |||
| 1602 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1603 | int has_best = 0; | |||
| 1604 | int k; | |||
| 1605 | for (k = 0; k < CCV_NNC_8I_ROWWISE_X_REFINEMENT_STEPS; k++) | |||
| 1606 | { | |||
| 1607 | const double stored_scale = _ccv_nnc_8i_rowwise_packed_stored_scale(scale, datatype); | |||
| 1608 | if (!(stored_scale > 0)) | |||
| 1609 | break; | |||
| 1610 | double sse = 0; | |||
| 1611 | double sum_qx = 0; | |||
| 1612 | double sum_qq = 0; | |||
| 1613 | size_t g; | |||
| 1614 | for (g = 0; g < groups_per_row; g++) | |||
| 1615 | { | |||
| 1616 | double y[32] = {0}; | |||
| 1617 | double w[32] = {0}; | |||
| 1618 | for (j = 0; j < group_size; j++) | |||
| 1619 | { | |||
| 1620 | y[j] = row[g * group_size + j] / stored_scale; | |||
| 1621 | w[j] = weights[g * group_size + j]; | |||
| 1622 | } | |||
| 1623 | ccv_nnc_8i_rowwise_packed_group_t group; | |||
| 1624 | _ccv_nnc_8i_rowwise_packed_quant_group(codec, y, w, &group); | |||
| 1625 | groups[g] = group; | |||
| 1626 | const size_t group_start = g * group_size; | |||
| 1627 | const size_t group_end = ccv_min(group_start + group_size, row_length)({ typeof (group_start + group_size) _a = (group_start + group_size ); typeof (row_length) _b = (row_length); (_a < _b) ? _a : _b; }); | |||
| 1628 | for (j = group_start; j < group_end; j++) | |||
| 1629 | { | |||
| 1630 | const int q8 = group.q8[j - group_start]; | |||
| 1631 | const double d = row[j] - stored_scale * q8; | |||
| 1632 | sse += weights[j] * d * d; | |||
| 1633 | sum_qx += weights[j] * q8 * row[j]; | |||
| 1634 | sum_qq += weights[j] * q8 * q8; | |||
| 1635 | } | |||
| 1636 | } | |||
| 1637 | if (sse < best_sse) | |||
| 1638 | { | |||
| 1639 | best_sse = sse; | |||
| 1640 | best_scale = stored_scale; | |||
| 1641 | has_best = 1; | |||
| 1642 | memcpy(best_groups, groups, sizeof(ccv_nnc_8i_rowwise_packed_group_t) * groups_per_row); | |||
| 1643 | } | |||
| 1644 | if (!(sum_qq > 0) || !(sum_qx > 0)) | |||
| 1645 | break; | |||
| 1646 | const double next_scale = sum_qx / sum_qq; | |||
| 1647 | if (_ccv_nnc_8i_rowwise_packed_stored_scale(next_scale, datatype) == stored_scale) | |||
| 1648 | break; | |||
| 1649 | scale = next_scale; | |||
| 1650 | } | |||
| 1651 | _ccv_nnc_8i_rowwise_packed_store_scale(scales, datatype, i, best_scale); | |||
| 1652 | size_t g; | |||
| 1653 | if (has_best
| |||
| 1654 | { | |||
| 1655 | for (g = 0; g < groups_per_row; g++) | |||
| 1656 | _ccv_nnc_8i_rowwise_packed_pack_group(u8, i * groups_per_row + g, codec, best_groups + g); | |||
| 1657 | } else { | |||
| 1658 | for (g = 0; g < groups_per_row; g++) | |||
| 1659 | { | |||
| 1660 | double y[32] = {0}; | |||
| 1661 | double w[32] = {0}; | |||
| 1662 | for (j = 0; j < group_size; j++) | |||
| 1663 | { | |||
| 1664 | y[j] = row[g * group_size + j]; | |||
| ||||
| 1665 | w[j] = weights[g * group_size + j]; | |||
| 1666 | } | |||
| 1667 | ccv_nnc_8i_rowwise_packed_group_t group; | |||
| 1668 | _ccv_nnc_8i_rowwise_packed_quant_group(codec, y, w, &group); | |||
| 1669 | _ccv_nnc_8i_rowwise_packed_pack_group(u8, i * groups_per_row + g, codec, &group); | |||
| 1670 | } | |||
| 1671 | } | |||
| 1672 | } | |||
| 1673 | ccfreefree(best_groups); | |||
| 1674 | ccfreefree(groups); | |||
| 1675 | ccfreefree(weights); | |||
| 1676 | ccfreefree(row); | |||
| 1677 | #ifdef USE_DISPATCH | |||
| 1678 | }); | |||
| 1679 | #else | |||
| 1680 | } parallel_endfor} } | |||
| 1681 | #endif | |||
| 1682 | return output_size; | |||
| 1683 | } | |||
| 1684 | ||||
| 1685 | void ccv_nnc_dequantize_8i_rowwise_x(const void* input, const int datatype, const int memory_type, const size_t input_length, const size_t row_length, const int format, void* output, const size_t output_length) | |||
| 1686 | { | |||
| 1687 | assert(datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F)((void) sizeof ((datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ? 1 : 0), __extension__ ({ if (datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ; else __assert_fail ("datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F" , "ccv_nnc_8i_rowwise.c", 1687, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1688 | assert(memory_type == CCV_TENSOR_CPU_MEMORY || memory_type == CCV_TENSOR_GPU_MEMORY)((void) sizeof ((memory_type == CCV_TENSOR_CPU_MEMORY || memory_type == CCV_TENSOR_GPU_MEMORY) ? 1 : 0), __extension__ ({ if (memory_type == CCV_TENSOR_CPU_MEMORY || memory_type == CCV_TENSOR_GPU_MEMORY ) ; else __assert_fail ("memory_type == CCV_TENSOR_CPU_MEMORY || memory_type == CCV_TENSOR_GPU_MEMORY" , "ccv_nnc_8i_rowwise.c", 1688, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1689 | assert(row_length > 0)((void) sizeof ((row_length > 0) ? 1 : 0), __extension__ ( { if (row_length > 0) ; else __assert_fail ("row_length > 0" , "ccv_nnc_8i_rowwise.c", 1689, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1690 | assert(output_length % row_length == 0)((void) sizeof ((output_length % row_length == 0) ? 1 : 0), __extension__ ({ if (output_length % row_length == 0) ; else __assert_fail ("output_length % row_length == 0", "ccv_nnc_8i_rowwise.c", 1690 , __extension__ __PRETTY_FUNCTION__); })); | |||
| 1691 | assert(!(format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) || row_length % 256 == 0)((void) sizeof ((!(format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256 ) || row_length % 256 == 0) ? 1 : 0), __extension__ ({ if (!( format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) || row_length % 256 == 0) ; else __assert_fail ("!(format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) || row_length % 256 == 0" , "ccv_nnc_8i_rowwise.c", 1691, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1692 | if (memory_type != CCV_TENSOR_CPU_MEMORY) | |||
| 1693 | { | |||
| 1694 | #ifdef HAVE_CUDA1 | |||
| 1695 | ccv_nnc_compat_dequantize_8i_rowwise_x_fp(input, datatype, input_length, row_length, format, output, output_length, 0); | |||
| 1696 | #elif defined(HAVE_MPS) | |||
| 1697 | assert(datatype != CCV_64F)((void) sizeof ((datatype != CCV_64F) ? 1 : 0), __extension__ ({ if (datatype != CCV_64F) ; else __assert_fail ("datatype != CCV_64F" , "ccv_nnc_8i_rowwise.c", 1697, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1698 | ccv_nnc_mps_dequantize_8i_rowwise_x(input, datatype, input_length, row_length, format, output, output_length, 0); | |||
| 1699 | #else | |||
| 1700 | assert(memory_type == CCV_TENSOR_CPU_MEMORY)((void) sizeof ((memory_type == CCV_TENSOR_CPU_MEMORY) ? 1 : 0 ), __extension__ ({ if (memory_type == CCV_TENSOR_CPU_MEMORY) ; else __assert_fail ("memory_type == CCV_TENSOR_CPU_MEMORY" , "ccv_nnc_8i_rowwise.c", 1700, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1701 | #endif | |||
| 1702 | return; | |||
| 1703 | } | |||
| 1704 | const size_t row_count = output_length / row_length; | |||
| 1705 | const size_t group_size = ccv_nnc_8i_rowwise_x_group_size(format); | |||
| 1706 | const size_t groups_per_row = (row_length + group_size - 1) / group_size; | |||
| 1707 | const size_t scale_offset = _ccv_nnc_8i_rowwise_packed_scale_offset(format, output_length, row_length); | |||
| 1708 | assert(input_length >= scale_offset + row_count * CCV_GET_DATA_TYPE_SIZE(datatype))((void) sizeof ((input_length >= scale_offset + row_count * _ccv_get_data_type_size[((datatype) & 0xFF000) >> 12 ]) ? 1 : 0), __extension__ ({ if (input_length >= scale_offset + row_count * _ccv_get_data_type_size[((datatype) & 0xFF000 ) >> 12]) ; else __assert_fail ("input_length >= scale_offset + row_count * CCV_GET_DATA_TYPE_SIZE(datatype)" , "ccv_nnc_8i_rowwise.c", 1708, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 1709 | const uint8_t* const u8 = (const uint8_t*)input; | |||
| 1710 | const uint8_t* const scales = u8 + scale_offset; | |||
| 1711 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 1712 | const double scale = _ccv_nnc_8i_rowwise_packed_load_scale(scales, datatype, i); | |||
| 1713 | if (format & CCV_NNC_QX_8I_ROWWISE_HADAMARD_256) | |||
| 1714 | { | |||
| 1715 | // Transform decoded integers before scaling / rounding to the destination | |||
| 1716 | // precision. H256 is its own inverse and acts on independent blocks. | |||
| 1717 | double block[256]; | |||
| 1718 | size_t base; | |||
| 1719 | for (base = 0; base < row_length; base += 256) | |||
| 1720 | { | |||
| 1721 | size_t g; | |||
| 1722 | for (g = 0; g < 256 / group_size; g++) | |||
| 1723 | { | |||
| 1724 | int q8[32] = {0}; | |||
| 1725 | _ccv_nnc_8i_rowwise_packed_decode_group(u8, (size_t)i * groups_per_row + base / group_size + g, format & CCV_NNC_QX_8I_ROWWISE_FORMAT_MASK, q8); | |||
| 1726 | size_t j; | |||
| 1727 | for (j = 0; j < group_size; j++) | |||
| 1728 | block[g * group_size + j] = q8[j]; | |||
| 1729 | } | |||
| 1730 | _ccv_nnc_8i_rowwise_hadamard_256(block, 256); | |||
| 1731 | size_t j; | |||
| 1732 | for (j = 0; j < 256; j++) | |||
| 1733 | _ccv_nnc_8i_rowwise_packed_write_value(output, datatype, (size_t)i * row_length + base + j, scale * block[j]); | |||
| 1734 | } | |||
| 1735 | } else { | |||
| 1736 | size_t g; | |||
| 1737 | for (g = 0; g < groups_per_row; g++) | |||
| 1738 | { | |||
| 1739 | int q8[32] = {0}; | |||
| 1740 | _ccv_nnc_8i_rowwise_packed_decode_group(u8, (size_t)i * groups_per_row + g, format, q8); | |||
| 1741 | size_t j; | |||
| 1742 | for (j = 0; j < group_size; j++) | |||
| 1743 | { | |||
| 1744 | const size_t col = g * group_size + j; | |||
| 1745 | if (col < row_length) | |||
| 1746 | _ccv_nnc_8i_rowwise_packed_write_value(output, datatype, (size_t)i * row_length + col, scale * q8[j]); | |||
| 1747 | } | |||
| 1748 | } | |||
| 1749 | } | |||
| 1750 | } parallel_endfor} } | |||
| 1751 | } | |||
| 1752 | ||||
| 1753 | static inline int _ccv_nnc_8i_rowwise_quantize(const double v, const double inv_scale) | |||
| 1754 | { | |||
| 1755 | const int q = (int)lrint(v * inv_scale); | |||
| 1756 | return ccv_clamp(q, -127, 127)({ typeof (-127) _a = (-127); typeof (127) _b = (127); typeof (q) _x = (q); (_x < _a) ? _a : ((_x > _b) ? _b : _x); } ); | |||
| 1757 | } | |||
| 1758 | ||||
| 1759 | static float _ccv_nnc_quantize_8i_rowwise_16f(const uint16_t* const row, const size_t row_length, const float* const imatrix, int8_t* const q) | |||
| 1760 | { | |||
| 1761 | size_t j; | |||
| 1762 | double max_abs = 0; | |||
| 1763 | for (j = 0; j < row_length; j++) | |||
| 1764 | { | |||
| 1765 | float v; | |||
| 1766 | ccv_half_precision_to_float(row + j, &v, 1); | |||
| 1767 | max_abs = ccv_max(max_abs, fabs(v))({ typeof (max_abs) _a = (max_abs); typeof (fabs(v)) _b = (fabs (v)); (_a > _b) ? _a : _b; }); | |||
| 1768 | } | |||
| 1769 | if (max_abs == 0) | |||
| 1770 | { | |||
| 1771 | memset(q, 0, row_length); | |||
| 1772 | return 0; | |||
| 1773 | } | |||
| 1774 | double scale = max_abs / 127.; | |||
| 1775 | float best_scale = 0; | |||
| 1776 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1777 | int k; | |||
| 1778 | for (k = 0; k < 8; k++) | |||
| 1779 | { | |||
| 1780 | // Round with the scale that will actually be stored, then refit scale by least squares. | |||
| 1781 | const float scale_f = (float)scale; | |||
| 1782 | uint16_t scale_h; | |||
| 1783 | float stored_scale; | |||
| 1784 | ccv_float_to_half_precision(&scale_f, &scale_h, 1); | |||
| 1785 | ccv_half_precision_to_float(&scale_h, &stored_scale, 1); | |||
| 1786 | if (!(stored_scale > 0)) | |||
| 1787 | break; | |||
| 1788 | const double inv_scale = 1. / stored_scale; | |||
| 1789 | double sum_qx = 0; | |||
| 1790 | double sum_qq = 0; | |||
| 1791 | double sse = 0; | |||
| 1792 | for (j = 0; j < row_length; j++) | |||
| 1793 | { | |||
| 1794 | const double w = _ccv_nnc_8i_rowwise_weight(imatrix, j); | |||
| 1795 | float v_f; | |||
| 1796 | ccv_half_precision_to_float(row + j, &v_f, 1); | |||
| 1797 | const double v = v_f; | |||
| 1798 | const int qj = _ccv_nnc_8i_rowwise_quantize(v, inv_scale); | |||
| 1799 | const double d = v - stored_scale * qj; | |||
| 1800 | sse += w * d * d; | |||
| 1801 | sum_qx += w * qj * v; | |||
| 1802 | sum_qq += w * qj * qj; | |||
| 1803 | } | |||
| 1804 | if (sse < best_sse) | |||
| 1805 | { | |||
| 1806 | best_sse = sse; | |||
| 1807 | best_scale = stored_scale; | |||
| 1808 | } | |||
| 1809 | if (!(sum_qq > 0) || !(sum_qx > 0)) | |||
| 1810 | break; | |||
| 1811 | const double next_scale = sum_qx / sum_qq; | |||
| 1812 | const float next_scale_f = (float)next_scale; | |||
| 1813 | uint16_t next_scale_h; | |||
| 1814 | float next_stored_scale; | |||
| 1815 | ccv_float_to_half_precision(&next_scale_f, &next_scale_h, 1); | |||
| 1816 | ccv_half_precision_to_float(&next_scale_h, &next_stored_scale, 1); | |||
| 1817 | if (next_stored_scale == stored_scale) | |||
| 1818 | break; | |||
| 1819 | scale = next_scale; | |||
| 1820 | } | |||
| 1821 | if (!(best_scale > 0)) | |||
| 1822 | { | |||
| 1823 | memset(q, 0, row_length); | |||
| 1824 | return 0; | |||
| 1825 | } | |||
| 1826 | const double inv_scale = 1. / best_scale; | |||
| 1827 | for (j = 0; j < row_length; j++) | |||
| 1828 | { | |||
| 1829 | float v; | |||
| 1830 | ccv_half_precision_to_float(row + j, &v, 1); | |||
| 1831 | q[j] = (int8_t)_ccv_nnc_8i_rowwise_quantize(v, inv_scale); | |||
| 1832 | } | |||
| 1833 | return best_scale; | |||
| 1834 | } | |||
| 1835 | ||||
| 1836 | static float _ccv_nnc_quantize_8i_rowwise_16bf(const uint16_t* const row, const size_t row_length, const float* const imatrix, int8_t* const q) | |||
| 1837 | { | |||
| 1838 | size_t j; | |||
| 1839 | double max_abs = 0; | |||
| 1840 | for (j = 0; j < row_length; j++) | |||
| 1841 | { | |||
| 1842 | float v; | |||
| 1843 | ccv_bfloat_to_float(row + j, &v, 1); | |||
| 1844 | max_abs = ccv_max(max_abs, fabs(v))({ typeof (max_abs) _a = (max_abs); typeof (fabs(v)) _b = (fabs (v)); (_a > _b) ? _a : _b; }); | |||
| 1845 | } | |||
| 1846 | if (max_abs == 0) | |||
| 1847 | { | |||
| 1848 | memset(q, 0, row_length); | |||
| 1849 | return 0; | |||
| 1850 | } | |||
| 1851 | double scale = max_abs / 127.; | |||
| 1852 | float best_scale = 0; | |||
| 1853 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1854 | int k; | |||
| 1855 | for (k = 0; k < 8; k++) | |||
| 1856 | { | |||
| 1857 | const float scale_f = (float)scale; | |||
| 1858 | uint16_t scale_bf; | |||
| 1859 | float stored_scale; | |||
| 1860 | ccv_float_to_bfloat(&scale_f, &scale_bf, 1); | |||
| 1861 | ccv_bfloat_to_float(&scale_bf, &stored_scale, 1); | |||
| 1862 | if (!(stored_scale > 0)) | |||
| 1863 | break; | |||
| 1864 | const double inv_scale = 1. / stored_scale; | |||
| 1865 | double sum_qx = 0; | |||
| 1866 | double sum_qq = 0; | |||
| 1867 | double sse = 0; | |||
| 1868 | for (j = 0; j < row_length; j++) | |||
| 1869 | { | |||
| 1870 | const double w = _ccv_nnc_8i_rowwise_weight(imatrix, j); | |||
| 1871 | float v_f; | |||
| 1872 | ccv_bfloat_to_float(row + j, &v_f, 1); | |||
| 1873 | const double v = v_f; | |||
| 1874 | const int qj = _ccv_nnc_8i_rowwise_quantize(v, inv_scale); | |||
| 1875 | const double d = v - stored_scale * qj; | |||
| 1876 | sse += w * d * d; | |||
| 1877 | sum_qx += w * qj * v; | |||
| 1878 | sum_qq += w * qj * qj; | |||
| 1879 | } | |||
| 1880 | if (sse < best_sse) | |||
| 1881 | { | |||
| 1882 | best_sse = sse; | |||
| 1883 | best_scale = stored_scale; | |||
| 1884 | } | |||
| 1885 | if (!(sum_qq > 0) || !(sum_qx > 0)) | |||
| 1886 | break; | |||
| 1887 | const double next_scale = sum_qx / sum_qq; | |||
| 1888 | const float next_scale_f = (float)next_scale; | |||
| 1889 | uint16_t next_scale_bf; | |||
| 1890 | float next_stored_scale; | |||
| 1891 | ccv_float_to_bfloat(&next_scale_f, &next_scale_bf, 1); | |||
| 1892 | ccv_bfloat_to_float(&next_scale_bf, &next_stored_scale, 1); | |||
| 1893 | if (next_stored_scale == stored_scale) | |||
| 1894 | break; | |||
| 1895 | scale = next_scale; | |||
| 1896 | } | |||
| 1897 | if (!(best_scale > 0)) | |||
| 1898 | { | |||
| 1899 | memset(q, 0, row_length); | |||
| 1900 | return 0; | |||
| 1901 | } | |||
| 1902 | const double inv_scale = 1. / best_scale; | |||
| 1903 | for (j = 0; j < row_length; j++) | |||
| 1904 | { | |||
| 1905 | float v; | |||
| 1906 | ccv_bfloat_to_float(row + j, &v, 1); | |||
| 1907 | q[j] = (int8_t)_ccv_nnc_8i_rowwise_quantize(v, inv_scale); | |||
| 1908 | } | |||
| 1909 | return best_scale; | |||
| 1910 | } | |||
| 1911 | ||||
| 1912 | static float _ccv_nnc_quantize_8i_rowwise_32f(const float* const row, const size_t row_length, const float* const imatrix, int8_t* const q) | |||
| 1913 | { | |||
| 1914 | size_t j; | |||
| 1915 | double max_abs = 0; | |||
| 1916 | for (j = 0; j < row_length; j++) | |||
| 1917 | max_abs = ccv_max(max_abs, fabs(row[j]))({ typeof (max_abs) _a = (max_abs); typeof (fabs(row[j])) _b = (fabs(row[j])); (_a > _b) ? _a : _b; }); | |||
| 1918 | if (max_abs == 0) | |||
| 1919 | { | |||
| 1920 | memset(q, 0, row_length); | |||
| 1921 | return 0; | |||
| 1922 | } | |||
| 1923 | double scale = max_abs / 127.; | |||
| 1924 | float best_scale = 0; | |||
| 1925 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1926 | int k; | |||
| 1927 | for (k = 0; k < 8; k++) | |||
| 1928 | { | |||
| 1929 | const float stored_scale = (float)scale; | |||
| 1930 | if (!(stored_scale > 0)) | |||
| 1931 | break; | |||
| 1932 | const double inv_scale = 1. / stored_scale; | |||
| 1933 | double sum_qx = 0; | |||
| 1934 | double sum_qq = 0; | |||
| 1935 | double sse = 0; | |||
| 1936 | for (j = 0; j < row_length; j++) | |||
| 1937 | { | |||
| 1938 | const double w = _ccv_nnc_8i_rowwise_weight(imatrix, j); | |||
| 1939 | const double v = row[j]; | |||
| 1940 | const int qj = _ccv_nnc_8i_rowwise_quantize(v, inv_scale); | |||
| 1941 | const double d = v - stored_scale * qj; | |||
| 1942 | sse += w * d * d; | |||
| 1943 | sum_qx += w * qj * v; | |||
| 1944 | sum_qq += w * qj * qj; | |||
| 1945 | } | |||
| 1946 | if (sse < best_sse) | |||
| 1947 | { | |||
| 1948 | best_sse = sse; | |||
| 1949 | best_scale = stored_scale; | |||
| 1950 | } | |||
| 1951 | if (!(sum_qq > 0) || !(sum_qx > 0)) | |||
| 1952 | break; | |||
| 1953 | const double next_scale = sum_qx / sum_qq; | |||
| 1954 | if ((float)next_scale == stored_scale) | |||
| 1955 | break; | |||
| 1956 | scale = next_scale; | |||
| 1957 | } | |||
| 1958 | if (!(best_scale > 0)) | |||
| 1959 | { | |||
| 1960 | memset(q, 0, row_length); | |||
| 1961 | return 0; | |||
| 1962 | } | |||
| 1963 | const double inv_scale = 1. / best_scale; | |||
| 1964 | for (j = 0; j < row_length; j++) | |||
| 1965 | q[j] = (int8_t)_ccv_nnc_8i_rowwise_quantize(row[j], inv_scale); | |||
| 1966 | return best_scale; | |||
| 1967 | } | |||
| 1968 | ||||
| 1969 | static double _ccv_nnc_quantize_8i_rowwise_64f(const double* const row, const size_t row_length, const float* const imatrix, int8_t* const q) | |||
| 1970 | { | |||
| 1971 | size_t j; | |||
| 1972 | double max_abs = 0; | |||
| 1973 | for (j = 0; j < row_length; j++) | |||
| 1974 | max_abs = ccv_max(max_abs, fabs(row[j]))({ typeof (max_abs) _a = (max_abs); typeof (fabs(row[j])) _b = (fabs(row[j])); (_a > _b) ? _a : _b; }); | |||
| 1975 | if (max_abs == 0) | |||
| 1976 | { | |||
| 1977 | memset(q, 0, row_length); | |||
| 1978 | return 0; | |||
| 1979 | } | |||
| 1980 | double scale = max_abs / 127.; | |||
| 1981 | double best_scale = 0; | |||
| 1982 | double best_sse = DBL_MAX1.7976931348623157e+308; | |||
| 1983 | int k; | |||
| 1984 | for (k = 0; k < 8; k++) | |||
| 1985 | { | |||
| 1986 | const double stored_scale = scale; | |||
| 1987 | if (!(stored_scale > 0)) | |||
| 1988 | break; | |||
| 1989 | const double inv_scale = 1. / stored_scale; | |||
| 1990 | double sum_qx = 0; | |||
| 1991 | double sum_qq = 0; | |||
| 1992 | double sse = 0; | |||
| 1993 | for (j = 0; j < row_length; j++) | |||
| 1994 | { | |||
| 1995 | const double w = _ccv_nnc_8i_rowwise_weight(imatrix, j); | |||
| 1996 | const double v = row[j]; | |||
| 1997 | const int qj = _ccv_nnc_8i_rowwise_quantize(v, inv_scale); | |||
| 1998 | const double d = v - stored_scale * qj; | |||
| 1999 | sse += w * d * d; | |||
| 2000 | sum_qx += w * qj * v; | |||
| 2001 | sum_qq += w * qj * qj; | |||
| 2002 | } | |||
| 2003 | if (sse < best_sse) | |||
| 2004 | { | |||
| 2005 | best_sse = sse; | |||
| 2006 | best_scale = stored_scale; | |||
| 2007 | } | |||
| 2008 | if (!(sum_qq > 0) || !(sum_qx > 0)) | |||
| 2009 | break; | |||
| 2010 | const double next_scale = sum_qx / sum_qq; | |||
| 2011 | if (next_scale == stored_scale) | |||
| 2012 | break; | |||
| 2013 | scale = next_scale; | |||
| 2014 | } | |||
| 2015 | if (!(best_scale > 0)) | |||
| 2016 | { | |||
| 2017 | memset(q, 0, row_length); | |||
| 2018 | return 0; | |||
| 2019 | } | |||
| 2020 | const double inv_scale = 1. / best_scale; | |||
| 2021 | for (j = 0; j < row_length; j++) | |||
| 2022 | q[j] = (int8_t)_ccv_nnc_8i_rowwise_quantize(row[j], inv_scale); | |||
| 2023 | return best_scale; | |||
| 2024 | } | |||
| 2025 | ||||
| 2026 | CCV_WARN_UNUSED(size_t)size_t __attribute__((warn_unused_result)) ccv_nnc_quantize_8i_rowwise(const void* input, const int datatype, const int memory_type, const size_t input_length, const size_t row_length, const float* const imatrix, const size_t imatrix_length, void* output, const size_t output_length) | |||
| 2027 | { | |||
| 2028 | assert(datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F)((void) sizeof ((datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ? 1 : 0), __extension__ ({ if (datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ; else __assert_fail ("datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F" , "ccv_nnc_8i_rowwise.c", 2028, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2029 | assert(memory_type == CCV_TENSOR_CPU_MEMORY)((void) sizeof ((memory_type == CCV_TENSOR_CPU_MEMORY) ? 1 : 0 ), __extension__ ({ if (memory_type == CCV_TENSOR_CPU_MEMORY) ; else __assert_fail ("memory_type == CCV_TENSOR_CPU_MEMORY" , "ccv_nnc_8i_rowwise.c", 2029, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2030 | assert(row_length > 0)((void) sizeof ((row_length > 0) ? 1 : 0), __extension__ ( { if (row_length > 0) ; else __assert_fail ("row_length > 0" , "ccv_nnc_8i_rowwise.c", 2030, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2031 | assert(input_length % row_length == 0)((void) sizeof ((input_length % row_length == 0) ? 1 : 0), __extension__ ({ if (input_length % row_length == 0) ; else __assert_fail ( "input_length % row_length == 0", "ccv_nnc_8i_rowwise.c", 2031 , __extension__ __PRETTY_FUNCTION__); })); | |||
| 2032 | const size_t row_count = input_length / row_length; | |||
| 2033 | if (!_ccv_nnc_8i_rowwise_imatrix_is_valid(imatrix, imatrix_length, row_length, row_count)) | |||
| 2034 | return 0; | |||
| 2035 | const size_t scale_offset = (input_length + 127) & -128; | |||
| 2036 | const size_t scale_size = row_count * CCV_GET_DATA_TYPE_SIZE(datatype)_ccv_get_data_type_size[((datatype) & 0xFF000) >> 12 ]; | |||
| 2037 | assert(output_length >= scale_offset + scale_size)((void) sizeof ((output_length >= scale_offset + scale_size ) ? 1 : 0), __extension__ ({ if (output_length >= scale_offset + scale_size) ; else __assert_fail ("output_length >= scale_offset + scale_size" , "ccv_nnc_8i_rowwise.c", 2037, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2038 | int8_t* const q = (int8_t*)output; | |||
| 2039 | uint8_t* const u8 = (uint8_t*)output; | |||
| 2040 | if (datatype == CCV_16F) | |||
| 2041 | { | |||
| 2042 | const uint16_t* const f16 = (const uint16_t*)input; | |||
| 2043 | uint16_t* const scales = (uint16_t*)(u8 + scale_offset); | |||
| 2044 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 2045 | const size_t row_start = (size_t)i * row_length; | |||
| 2046 | const float* const row_imatrix = _ccv_nnc_8i_rowwise_imatrix_for_row(imatrix, imatrix_length, row_length, row_count, (size_t)i); | |||
| 2047 | const float scale_f = _ccv_nnc_quantize_8i_rowwise_16f(f16 + row_start, row_length, row_imatrix, q + row_start); | |||
| 2048 | ccv_float_to_half_precision(&scale_f, scales + i, 1); | |||
| 2049 | } parallel_endfor} } | |||
| 2050 | } else if (datatype == CCV_16BF) { | |||
| 2051 | const uint16_t* const bf16 = (const uint16_t*)input; | |||
| 2052 | uint16_t* const scales = (uint16_t*)(u8 + scale_offset); | |||
| 2053 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 2054 | const size_t row_start = (size_t)i * row_length; | |||
| 2055 | const float* const row_imatrix = _ccv_nnc_8i_rowwise_imatrix_for_row(imatrix, imatrix_length, row_length, row_count, (size_t)i); | |||
| 2056 | const float scale_f = _ccv_nnc_quantize_8i_rowwise_16bf(bf16 + row_start, row_length, row_imatrix, q + row_start); | |||
| 2057 | ccv_float_to_bfloat(&scale_f, scales + i, 1); | |||
| 2058 | } parallel_endfor} } | |||
| 2059 | } else if (datatype == CCV_32F) { | |||
| 2060 | const float* const f32 = (const float*)input; | |||
| 2061 | float* const scales = (float*)(u8 + scale_offset); | |||
| 2062 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 2063 | const size_t row_start = (size_t)i * row_length; | |||
| 2064 | const float* const row_imatrix = _ccv_nnc_8i_rowwise_imatrix_for_row(imatrix, imatrix_length, row_length, row_count, (size_t)i); | |||
| 2065 | scales[i] = _ccv_nnc_quantize_8i_rowwise_32f(f32 + row_start, row_length, row_imatrix, q + row_start); | |||
| 2066 | } parallel_endfor} } | |||
| 2067 | } else { | |||
| 2068 | assert(datatype == CCV_64F)((void) sizeof ((datatype == CCV_64F) ? 1 : 0), __extension__ ({ if (datatype == CCV_64F) ; else __assert_fail ("datatype == CCV_64F" , "ccv_nnc_8i_rowwise.c", 2068, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2069 | const double* const f64 = (const double*)input; | |||
| 2070 | double* const scales = (double*)(u8 + scale_offset); | |||
| 2071 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 2072 | const size_t row_start = (size_t)i * row_length; | |||
| 2073 | const float* const row_imatrix = _ccv_nnc_8i_rowwise_imatrix_for_row(imatrix, imatrix_length, row_length, row_count, (size_t)i); | |||
| 2074 | scales[i] = _ccv_nnc_quantize_8i_rowwise_64f(f64 + row_start, row_length, row_imatrix, q + row_start); | |||
| 2075 | } parallel_endfor} } | |||
| 2076 | } | |||
| 2077 | return scale_offset + scale_size; | |||
| 2078 | } | |||
| 2079 | ||||
| 2080 | void ccv_nnc_dequantize_8i_rowwise(const void* input, const int datatype, const int memory_type, const size_t input_length, const size_t row_length, void* output, const size_t output_length) | |||
| 2081 | { | |||
| 2082 | assert(datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F)((void) sizeof ((datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ? 1 : 0), __extension__ ({ if (datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F) ; else __assert_fail ("datatype == CCV_16F || datatype == CCV_16BF || datatype == CCV_32F || datatype == CCV_64F" , "ccv_nnc_8i_rowwise.c", 2082, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2083 | assert(memory_type == CCV_TENSOR_CPU_MEMORY || memory_type == CCV_TENSOR_GPU_MEMORY)((void) sizeof ((memory_type == CCV_TENSOR_CPU_MEMORY || memory_type == CCV_TENSOR_GPU_MEMORY) ? 1 : 0), __extension__ ({ if (memory_type == CCV_TENSOR_CPU_MEMORY || memory_type == CCV_TENSOR_GPU_MEMORY ) ; else __assert_fail ("memory_type == CCV_TENSOR_CPU_MEMORY || memory_type == CCV_TENSOR_GPU_MEMORY" , "ccv_nnc_8i_rowwise.c", 2083, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2084 | assert(row_length > 0)((void) sizeof ((row_length > 0) ? 1 : 0), __extension__ ( { if (row_length > 0) ; else __assert_fail ("row_length > 0" , "ccv_nnc_8i_rowwise.c", 2084, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2085 | assert(output_length % row_length == 0)((void) sizeof ((output_length % row_length == 0) ? 1 : 0), __extension__ ({ if (output_length % row_length == 0) ; else __assert_fail ("output_length % row_length == 0", "ccv_nnc_8i_rowwise.c", 2085 , __extension__ __PRETTY_FUNCTION__); })); | |||
| 2086 | if (memory_type != CCV_TENSOR_CPU_MEMORY) | |||
| 2087 | { | |||
| 2088 | #ifdef HAVE_CUDA1 | |||
| 2089 | ccv_nnc_compat_dequantize_8i_rowwise(input, datatype, input_length, row_length, output, output_length, 0); | |||
| 2090 | #elif defined(HAVE_MPS) | |||
| 2091 | assert(datatype != CCV_64F)((void) sizeof ((datatype != CCV_64F) ? 1 : 0), __extension__ ({ if (datatype != CCV_64F) ; else __assert_fail ("datatype != CCV_64F" , "ccv_nnc_8i_rowwise.c", 2091, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2092 | ccv_nnc_mps_dequantize_8i_rowwise(input, datatype, input_length, row_length, output, output_length, 0); | |||
| 2093 | #else | |||
| 2094 | assert(memory_type == CCV_TENSOR_CPU_MEMORY)((void) sizeof ((memory_type == CCV_TENSOR_CPU_MEMORY) ? 1 : 0 ), __extension__ ({ if (memory_type == CCV_TENSOR_CPU_MEMORY) ; else __assert_fail ("memory_type == CCV_TENSOR_CPU_MEMORY" , "ccv_nnc_8i_rowwise.c", 2094, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2095 | #endif | |||
| 2096 | return; | |||
| 2097 | } | |||
| 2098 | const size_t row_count = output_length / row_length; | |||
| 2099 | const size_t scale_offset = (output_length + 127) & -128; | |||
| 2100 | assert(input_length >= scale_offset + row_count * CCV_GET_DATA_TYPE_SIZE(datatype))((void) sizeof ((input_length >= scale_offset + row_count * _ccv_get_data_type_size[((datatype) & 0xFF000) >> 12 ]) ? 1 : 0), __extension__ ({ if (input_length >= scale_offset + row_count * _ccv_get_data_type_size[((datatype) & 0xFF000 ) >> 12]) ; else __assert_fail ("input_length >= scale_offset + row_count * CCV_GET_DATA_TYPE_SIZE(datatype)" , "ccv_nnc_8i_rowwise.c", 2100, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2101 | const int8_t* const q = (const int8_t*)input; | |||
| 2102 | const uint8_t* const u8 = (const uint8_t*)input; | |||
| 2103 | if (datatype == CCV_16F) | |||
| 2104 | { | |||
| 2105 | uint16_t* const f16 = (uint16_t*)output; | |||
| 2106 | const uint16_t* const scales = (const uint16_t*)(u8 + scale_offset); | |||
| 2107 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 2108 | const size_t row_start = (size_t)i * row_length; | |||
| 2109 | float scale_f; | |||
| 2110 | ccv_half_precision_to_float(scales + i, &scale_f, 1); | |||
| 2111 | size_t j; | |||
| 2112 | for (j = 0; j < row_length; j++) | |||
| 2113 | { | |||
| 2114 | const float v = q[row_start + j] * scale_f; | |||
| 2115 | ccv_float_to_half_precision(&v, f16 + row_start + j, 1); | |||
| 2116 | } | |||
| 2117 | } parallel_endfor} } | |||
| 2118 | } else if (datatype == CCV_16BF) { | |||
| 2119 | uint16_t* const bf16 = (uint16_t*)output; | |||
| 2120 | const uint16_t* const scales = (const uint16_t*)(u8 + scale_offset); | |||
| 2121 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 2122 | const size_t row_start = (size_t)i * row_length; | |||
| 2123 | float scale_f; | |||
| 2124 | ccv_bfloat_to_float(scales + i, &scale_f, 1); | |||
| 2125 | size_t j; | |||
| 2126 | for (j = 0; j < row_length; j++) | |||
| 2127 | { | |||
| 2128 | const float v = q[row_start + j] * scale_f; | |||
| 2129 | ccv_float_to_bfloat(&v, bf16 + row_start + j, 1); | |||
| 2130 | } | |||
| 2131 | } parallel_endfor} } | |||
| 2132 | } else if (datatype == CCV_32F) { | |||
| 2133 | float* const f32 = (float*)output; | |||
| 2134 | const float* const scales = (const float*)(u8 + scale_offset); | |||
| 2135 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 2136 | const size_t row_start = (size_t)i * row_length; | |||
| 2137 | const float scale = scales[i]; | |||
| 2138 | size_t j; | |||
| 2139 | for (j = 0; j < row_length; j++) | |||
| 2140 | f32[row_start + j] = q[row_start + j] * scale; | |||
| 2141 | } parallel_endfor} } | |||
| 2142 | } else { | |||
| 2143 | assert(datatype == CCV_64F)((void) sizeof ((datatype == CCV_64F) ? 1 : 0), __extension__ ({ if (datatype == CCV_64F) ; else __assert_fail ("datatype == CCV_64F" , "ccv_nnc_8i_rowwise.c", 2143, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 2144 | double* const f64 = (double*)output; | |||
| 2145 | const double* const scales = (const double*)(u8 + scale_offset); | |||
| 2146 | parallel_for(i, (int)row_count){ int i; for ((i) = 0; (i) < ((int)row_count); (i)++) { { | |||
| 2147 | const size_t row_start = (size_t)i * row_length; | |||
| 2148 | const double scale = scales[i]; | |||
| 2149 | size_t j; | |||
| 2150 | for (j = 0; j < row_length; j++) | |||
| 2151 | f64[row_start + j] = q[row_start + j] * scale; | |||
| 2152 | } parallel_endfor} } | |||
| 2153 | } | |||
| 2154 | } |