Bug Summary

File:nnc/ccv_nnc_8i_rowwise.c
Warning:line 1676, column 12
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

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