Bug Summary

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