Bug Summary

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