interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / tests / test-quantize-fns.cpp

458 lines · 19214 bytes Raw Blame History
1// Unit tests for quantization specific functions - quantize, dequantize and dot product
2
3#include "ggml.h"
4#include "ggml-cpu.h"
5
6#undef NDEBUG
7#include <assert.h>
8#include <algorithm>
9#include <cmath>
10#include <limits>
11#include <math.h>
12#include <stdio.h>
13#include <string>
14#include <vector>
15
16#if defined(_MSC_VER)
17#pragma warning(disable: 4244 4267) // possible loss of data
18#endif
19
20constexpr float MAX_QUANTIZATION_REFERENCE_ERROR = 0.0001f;
21constexpr float MAX_QUANTIZATION_TOTAL_ERROR = 0.002f;
22constexpr float MAX_QUANTIZATION_TOTAL_ERROR_BINARY = 0.025f;
23constexpr float MAX_QUANTIZATION_TOTAL_ERROR_TERNARY = 0.01f;
24constexpr float MAX_QUANTIZATION_TOTAL_ERROR_2BITS = 0.0075f;
25constexpr float MAX_QUANTIZATION_TOTAL_ERROR_3BITS = 0.0040f;
26constexpr float MAX_QUANTIZATION_TOTAL_ERROR_3BITS_XXS = 0.0050f;
27constexpr float MAX_QUANTIZATION_TOTAL_ERROR_FP4 = 0.0030f;
28constexpr float MAX_DOT_PRODUCT_ERROR = 0.02f;
29constexpr float MAX_DOT_PRODUCT_ERROR_LOWBIT = 0.04f;
30constexpr float MAX_DOT_PRODUCT_ERROR_FP4 = 0.03f;
31constexpr float MAX_DOT_PRODUCT_ERROR_BINARY = 0.40f;
32constexpr float MAX_DOT_PRODUCT_ERROR_TERNARY = 0.15f;
33
34static const char* RESULT_STR[] = {"ok", "FAILED"};
35
36
37// Generate synthetic data
38static void generate_data(float offset, size_t n, float * dst, float amplitude = 2.0f) {
39 for (size_t i = 0; i < n; i++) {
40 dst[i] = 0.1 + amplitude*cosf(i + offset);
41 }
42}
43
44// Calculate RMSE between two float arrays
45static float array_rmse(const float * a1, const float * a2, size_t n) {
46 double sum = 0;
47 for (size_t i = 0; i < n; i++) {
48 double diff = a1[i] - a2[i];
49 sum += diff * diff;
50 }
51 return sqrtf(sum) / n;
52}
53
54// Total quantization error on test data
55static float total_quantization_error(const ggml_type_traits * qfns, const ggml_type_traits_cpu * qfns_cpu, size_t test_size, const float * test_data) {
56 std::vector<uint8_t> tmp_q(2*test_size);
57 std::vector<float> tmp_out(test_size);
58
59 qfns_cpu->from_float(test_data, tmp_q.data(), test_size);
60 qfns->to_float(tmp_q.data(), tmp_out.data(), test_size);
61 return array_rmse(test_data, tmp_out.data(), test_size);
62}
63
64// Total quantization error on test data
65static float reference_quantization_error(const ggml_type_traits * qfns, const ggml_type_traits_cpu * qfns_cpu, size_t test_size, const float * test_data) {
66 std::vector<uint8_t> tmp_q(2*test_size);
67 std::vector<float> tmp_out(test_size);
68 std::vector<float> tmp_out_ref(test_size);
69
70 // FIXME: why is done twice?
71 qfns_cpu->from_float(test_data, tmp_q.data(), test_size);
72 qfns->to_float(tmp_q.data(), tmp_out.data(), test_size);
73
74 qfns->from_float_ref(test_data, tmp_q.data(), test_size);
75 qfns->to_float(tmp_q.data(), tmp_out_ref.data(), test_size);
76
77 return array_rmse(tmp_out.data(), tmp_out_ref.data(), test_size);
78}
79
80static float dot_product(const float * a1, const float * a2, size_t test_size) {
81 double sum = 0;
82 for (size_t i = 0; i < test_size; i++) {
83 sum += a1[i] * a2[i];
84 }
85 return sum;
86}
87
88// Total dot product error
89static float dot_product_error(const ggml_type_traits_cpu * qfns_cpu, ggml_type src0_type, size_t test_size,
90 const float * test_data1, const float * test_data2,
91 const float * test_data3, const float * test_data4,
92 const int nrc) {
93 const auto * vdot = ggml_get_type_traits_cpu(qfns_cpu->vec_dot_type);
94 const size_t pad = 64;
95 const size_t bx = ggml_row_size(src0_type, test_size) + pad;
96 const size_t by = ggml_row_size(qfns_cpu->vec_dot_type, test_size) + pad;
97
98 std::vector<uint8_t> tmp_q1(bx * nrc);
99 std::vector<uint8_t> tmp_q2(by * nrc);
100
101 qfns_cpu->from_float(test_data1, tmp_q1.data(), test_size);
102 vdot->from_float(test_data2, tmp_q2.data(), test_size);
103
104 if (nrc == 1) {
105 float result = INFINITY;
106 qfns_cpu->vec_dot(test_size, &result, 0, tmp_q1.data(), 0, tmp_q2.data(), 0, 1);
107
108 const float dot_ref = dot_product(test_data1, test_data2, test_size);
109 return fabsf(result - dot_ref) / test_size;
110 }
111
112 // nrc == 2: kernel computes a 2x2 dot product matrix
113 // Output layout: s[0]=dot(vx0,vy0), s[1]=dot(vx1,vy0), s[bs]=dot(vx0,vy1), s[bs+1]=dot(vx1,vy1)
114 // row and output strides are padded, same as in the mul_mat path
115 qfns_cpu->from_float(test_data3, tmp_q1.data() + bx, test_size);
116 vdot->from_float(test_data4, tmp_q2.data() + by, test_size);
117
118 const size_t bs = 16;
119 std::vector<float> result(bs + 2, INFINITY);
120 qfns_cpu->vec_dot(test_size, result.data(), bs, tmp_q1.data(), bx, tmp_q2.data(), by, 2);
121
122 const float ref00 = dot_product(test_data1, test_data2, test_size);
123 const float ref10 = dot_product(test_data3, test_data2, test_size);
124 const float ref01 = dot_product(test_data1, test_data4, test_size);
125 const float ref11 = dot_product(test_data3, test_data4, test_size);
126
127 const auto err = [test_size](float val, float ref) {
128 const float e = fabsf(val - ref) / test_size;
129 return std::isfinite(e) ? e : INFINITY;
130 };
131
132 return std::max({err(result[0], ref00), err(result[1], ref10), err(result[bs], ref01), err(result[bs + 1], ref11)});
133}
134
135static int test_vec_dot_f32(bool verbose) {
136 const auto * f32 = ggml_get_type_traits_cpu(GGML_TYPE_F32);
137 int num_failed = 0;
138 for (int n : {1, 2, 3, 5, 7, 8, 15, 16, 17, 31, 33, 63, 67, 127, 129, 193, 255, 1023}) {
139 std::vector<float> a(n);
140 std::vector<float> b(n);
141 generate_data(0.0, n, a.data());
142 generate_data(1.0, n, b.data());
143
144 float result = 0.0f;
145 f32->vec_dot(n, &result, 0, a.data(), 0, b.data(), 0, 1);
146 const float ref = dot_product(a.data(), b.data(), n);
147 const float error = fabsf(result - ref) / n;
148
149 const bool failed = !(error < MAX_QUANTIZATION_REFERENCE_ERROR);
150 num_failed += failed;
151 if (failed || verbose) {
152 printf(" f32 vec_dot n=%4d: %s (ref=%f got=%f err=%f)\n",
153 n, RESULT_STR[failed], ref, result, error);
154 }
155 }
156 return num_failed;
157}
158
159// Positive finite E4M3 values in encoding order, from OCP FP8 rev. 1.1, section 5.1.
160static const float F8_E4M3_VALUES[] = {
161 0.0f, 0.001953125f, 0.00390625f, 0.005859375f, 0.0078125f, 0.009765625f, 0.01171875f, 0.013671875f,
162 0.015625f, 0.017578125f, 0.01953125f, 0.021484375f, 0.0234375f, 0.025390625f, 0.02734375f, 0.029296875f,
163 0.03125f, 0.03515625f, 0.0390625f, 0.04296875f, 0.046875f, 0.05078125f, 0.0546875f, 0.05859375f,
164 0.0625f, 0.0703125f, 0.078125f, 0.0859375f, 0.09375f, 0.1015625f, 0.109375f, 0.1171875f,
165 0.125f, 0.140625f, 0.15625f, 0.171875f, 0.1875f, 0.203125f, 0.21875f, 0.234375f,
166 0.25f, 0.28125f, 0.3125f, 0.34375f, 0.375f, 0.40625f, 0.4375f, 0.46875f,
167 0.5f, 0.5625f, 0.625f, 0.6875f, 0.75f, 0.8125f, 0.875f, 0.9375f,
168 1.0f, 1.125f, 1.25f, 1.375f, 1.5f, 1.625f, 1.75f, 1.875f,
169 2.0f, 2.25f, 2.5f, 2.75f, 3.0f, 3.25f, 3.5f, 3.75f,
170 4.0f, 4.5f, 5.0f, 5.5f, 6.0f, 6.5f, 7.0f, 7.5f,
171 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f,
172 16.0f, 18.0f, 20.0f, 22.0f, 24.0f, 26.0f, 28.0f, 30.0f,
173 32.0f, 36.0f, 40.0f, 44.0f, 48.0f, 52.0f, 56.0f, 60.0f,
174 64.0f, 72.0f, 80.0f, 88.0f, 96.0f, 104.0f, 112.0f, 120.0f,
175 128.0f, 144.0f, 160.0f, 176.0f, 192.0f, 208.0f, 224.0f, 240.0f,
176 256.0f, 288.0f, 320.0f, 352.0f, 384.0f, 416.0f, 448.0f,
177};
178
179static uint8_t fp32_to_f8_e4m3_ref(float value) {
180 if (isnan(value)) {
181 return 0x7F;
182 }
183 const uint8_t sign = signbit(value) ? 0x80 : 0;
184 const double magnitude = fabs((double) value);
185 if (magnitude >= F8_E4M3_VALUES[126]) {
186 return sign | 126;
187 }
188 int nearest = 0;
189 double distance = magnitude;
190 for (int i = 1; i < 127; ++i) {
191 const double candidate = fabs(magnitude - F8_E4M3_VALUES[i]);
192 if (candidate < distance || (candidate == distance && (i & 1) == 0)) {
193 nearest = i;
194 distance = candidate;
195 }
196 }
197 return sign | (uint8_t) nearest;
198}
199
200static int test_f8_e4m3(bool verbose) {
201 static_assert(sizeof(ggml_fp8_e4m3_t) == 1);
202
203 const auto * traits = ggml_get_type_traits(GGML_TYPE_F8_E4M3);
204 const auto * traits_cpu = ggml_get_type_traits_cpu(GGML_TYPE_F8_E4M3);
205 std::vector<uint8_t> encoded(256);
206 std::vector<uint8_t> roundtrip(256);
207 std::vector<float> decoded(256);
208 int num_failed = 0;
209
210 const bool traits_failed = traits->blck_size != 1 || traits->type_size != sizeof(ggml_fp8_e4m3_t) || traits->is_quantized;
211 num_failed += traits_failed;
212 if (verbose || traits_failed) {
213 printf("f8_e4m3 scalar type traits: %s\n", RESULT_STR[traits_failed]);
214 }
215
216 for (int i = 0; i < 256; ++i) {
217 encoded[i] = (uint8_t) i;
218 }
219 traits->to_float(encoded.data(), decoded.data(), decoded.size());
220 traits_cpu->from_float(decoded.data(), roundtrip.data(), decoded.size());
221
222 for (int i = 0; i < 256; ++i) {
223 const bool is_nan = (i & 0x7F) == 0x7F;
224 const float expected = is_nan ? NAN : copysignf(F8_E4M3_VALUES[i & 0x7F], i & 0x80 ? -1.0f : 1.0f);
225 const bool decode_failed = is_nan ? !isnan(decoded[i])
226 : decoded[i] != expected || (expected == 0.0f && signbit(decoded[i]) != signbit(expected));
227 const bool encode_failed = !is_nan && roundtrip[i] != encoded[i];
228 if (decode_failed || encode_failed) {
229 num_failed++;
230 if (verbose) {
231 printf("f8_e4m3 code 0x%02x failed: decoded=%f expected=%f roundtrip=0x%02x\n",
232 i, decoded[i], expected, roundtrip[i]);
233 }
234 }
235 }
236
237 std::vector<float> inputs;
238 for (float value : F8_E4M3_VALUES) {
239 inputs.push_back(value);
240 inputs.push_back(-value);
241 }
242 for (int i = 0; i < 126; ++i) {
243 const float midpoint = (F8_E4M3_VALUES[i] + F8_E4M3_VALUES[i + 1]) / 2.0f;
244 for (float value : {nextafterf(midpoint, -INFINITY), midpoint, nextafterf(midpoint, INFINITY)}) {
245 inputs.push_back(value);
246 inputs.push_back(-value);
247 }
248 }
249 for (float value : {std::numeric_limits<float>::denorm_min(), std::numeric_limits<float>::max(),
250 nextafterf(448.0f, INFINITY), nextafterf(464.0f, -INFINITY), 464.0f,
251 nextafterf(464.0f, INFINITY), INFINITY, NAN}) {
252 inputs.push_back(value);
253 inputs.push_back(-value);
254 }
255
256 std::vector<uint8_t> quantized(inputs.size());
257 std::vector<uint8_t> quantized_ref(inputs.size());
258 traits_cpu->from_float(inputs.data(), quantized.data(), inputs.size());
259 traits->from_float_ref(inputs.data(), quantized_ref.data(), inputs.size());
260 for (size_t i = 0; i < inputs.size(); ++i) {
261 const uint8_t expected = fp32_to_f8_e4m3_ref(inputs[i]);
262 if (quantized[i] != expected || quantized_ref[i] != expected) {
263 num_failed++;
264 if (verbose) {
265 printf("f8_e4m3 input %a failed: encoded=0x%02x reference=0x%02x expected=0x%02x\n",
266 inputs[i], quantized[i], quantized_ref[i], expected);
267 }
268 }
269 }
270
271 for (int n : {0, 1, 7, 8, 15, 16, 31, 32, 63, 64, 65, 129, 257}) {
272 std::vector<uint8_t> x(n + 1);
273 std::vector<float> y(n + 1);
274 double expected = 0.0;
275 for (int i = 0; i < n; ++i) {
276 const uint8_t code = (uint8_t) (i % 254 + (i % 254 >= 127));
277 x[i + 1] = code;
278 y[i + 1] = (i % 7 - 3) * 0.125f;
279 expected += copysignf(F8_E4M3_VALUES[code & 0x7F], code & 0x80 ? -1.0f : 1.0f) * y[i + 1];
280 }
281 float actual;
282 traits_cpu->vec_dot(n, &actual, 0, x.data() + 1, 0, y.data() + 1, 0, 1);
283 const bool failed = fabs(actual - expected) > 1e-5 * std::max(1.0, fabs(expected));
284 num_failed += failed;
285 if (verbose || failed) {
286 printf("f8_e4m3 dot n=%d: %s (ref=%f got=%f)\n", n, RESULT_STR[failed], expected, actual);
287 }
288 }
289
290 for (uint8_t code : {0x7F, 0xFF}) {
291 std::vector<uint8_t> x(65, 0);
292 std::vector<float> y(65, 1.0f);
293 x[0] = x.back() = code;
294 float actual;
295 traits_cpu->vec_dot(x.size(), &actual, 0, x.data(), 0, y.data(), 0, 1);
296 num_failed += !isnan(actual);
297 }
298
299 if (verbose || num_failed) {
300 printf("f8_e4m3 exhaustive conversion: %s (%d failures)\n", RESULT_STR[num_failed != 0], num_failed);
301 }
302 return num_failed;
303}
304
305static int test_vec_dot_q(bool verbose) {
306 int num_failed = 0;
307
308 const size_t test_size = 32 * 128;
309
310 std::vector<float> test_data(test_size);
311 std::vector<float> test_data2(test_size);
312 std::vector<float> test_data3(test_size);
313 std::vector<float> test_data4(test_size);
314
315 generate_data(0.0, test_data.size(), test_data.data());
316 generate_data(1.0, test_data2.size(), test_data2.data());
317 generate_data(3.0, test_data3.size(), test_data3.data(), 1.0f);
318 generate_data(4.0, test_data4.size(), test_data4.data(), 1.5f);
319
320 for (int i = 0; i < GGML_TYPE_COUNT; i++) {
321 ggml_type type = (ggml_type) i;
322 const auto * qfns = ggml_get_type_traits(type);
323 const auto * qfns_cpu = ggml_get_type_traits_cpu(type);
324
325 // deprecated - skip
326 if (qfns->blck_size == 0) {
327 continue;
328 }
329
330 const ggml_type ei = (ggml_type)i;
331
332 printf("Testing %s\n", ggml_type_name((ggml_type) i));
333 ggml_quantize_init(ei);
334
335 if (qfns_cpu->from_float && qfns->to_float) {
336 const float total_error = total_quantization_error(qfns, qfns_cpu, test_size, test_data.data());
337 const float max_quantization_error =
338 type == GGML_TYPE_Q1_0 ? MAX_QUANTIZATION_TOTAL_ERROR_BINARY :
339 type == GGML_TYPE_TQ1_0 ? MAX_QUANTIZATION_TOTAL_ERROR_TERNARY :
340 type == GGML_TYPE_TQ2_0 ? MAX_QUANTIZATION_TOTAL_ERROR_TERNARY :
341 type == GGML_TYPE_Q2_0 ? MAX_QUANTIZATION_TOTAL_ERROR_TERNARY :
342 type == GGML_TYPE_Q2_K ? MAX_QUANTIZATION_TOTAL_ERROR_2BITS :
343 type == GGML_TYPE_IQ2_S ? MAX_QUANTIZATION_TOTAL_ERROR_2BITS :
344 type == GGML_TYPE_Q3_K ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS :
345 type == GGML_TYPE_IQ3_S ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS :
346 type == GGML_TYPE_IQ3_XXS ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS_XXS :
347 type == GGML_TYPE_NVFP4 ? MAX_QUANTIZATION_TOTAL_ERROR_FP4 : MAX_QUANTIZATION_TOTAL_ERROR;
348 bool failed = !(total_error < max_quantization_error);
349 num_failed += failed;
350 if (failed || verbose) {
351 printf("%5s absolute quantization error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], total_error);
352 }
353
354 const float reference_error = reference_quantization_error(qfns, qfns_cpu, test_size, test_data.data());
355 failed = !(reference_error < MAX_QUANTIZATION_REFERENCE_ERROR);
356 num_failed += failed;
357 if (failed || verbose) {
358 printf("%5s reference implementation error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], reference_error);
359 }
360
361 const float vec_dot_error = dot_product_error(qfns_cpu, type, test_size, test_data.data(), test_data2.data(), nullptr, nullptr, 1);
362 const float max_allowed_error = type == GGML_TYPE_Q2_K || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ2_XXS ||
363 type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S || type == GGML_TYPE_IQ2_S
364 ? MAX_DOT_PRODUCT_ERROR_LOWBIT
365 : type == GGML_TYPE_Q1_0
366 ? MAX_DOT_PRODUCT_ERROR_BINARY
367 : type == GGML_TYPE_TQ1_0 || type == GGML_TYPE_TQ2_0 || type == GGML_TYPE_Q2_0
368 ? MAX_DOT_PRODUCT_ERROR_TERNARY
369 : type == GGML_TYPE_NVFP4
370 ? MAX_DOT_PRODUCT_ERROR_FP4
371 : MAX_DOT_PRODUCT_ERROR;
372 failed = !(vec_dot_error < max_allowed_error);
373 num_failed += failed;
374 if (failed || verbose) {
375 printf("%5s dot product error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], vec_dot_error);
376 }
377
378 // Test nrc=2 path for types that support it
379 if (qfns_cpu->nrows == 2) {
380 const float vec_dot_error_nrc2 = dot_product_error(qfns_cpu, type, test_size, test_data.data(), test_data2.data(), test_data3.data(), test_data4.data(), 2);
381 failed = !(vec_dot_error_nrc2 < max_allowed_error);
382 num_failed += failed;
383 if (failed || verbose) {
384 printf("%5s dot product error (nrc=2): %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], vec_dot_error_nrc2);
385 }
386 }
387 }
388 }
389
390 return num_failed;
391}
392
393// In every group, all values with importance are equal, and the max (0) has none.
394// The scale search in make_qkx3_quants then fits min == max and passes inf/nan to nearest_int (#29804).
395static int test_quantize_imatrix_degenerate(bool verbose) {
396 const int64_t n = 256;
397 std::vector<float> x(n);
398 std::vector<float> imatrix(n);
399 std::vector<float> out(n);
400 int num_failed = 0;
401
402 printf("Testing degenerate imatrix:\n");
403 for (ggml_type type : {GGML_TYPE_Q2_K, GGML_TYPE_Q4_K, GGML_TYPE_Q5_K, GGML_TYPE_Q4_1, GGML_TYPE_Q5_1}) {
404 const int64_t group = type == GGML_TYPE_Q2_K ? 16 : 32;
405 for (int64_t i = 0; i < n; ++i) {
406 const int64_t g = i / group;
407 const int64_t p = i % group;
408 const bool important = p % 3 == 1;
409 x[i] = important ? -0.02f*(g + 1) : (p % 2 ? -1.0f : 0.0f);
410 imatrix[i] = important ? 1.0f : 0.0f;
411 }
412 printf(" - %s\n", ggml_type_name(type));
413
414 std::vector<uint8_t> q(ggml_row_size(type, n));
415 ggml_quantize_init(type);
416 ggml_quantize_chunk(type, x.data(), q.data(), 0, 1, n, imatrix.data());
417 ggml_get_type_traits(type)->to_float(q.data(), out.data(), n);
418
419 const bool failed = !std::all_of(out.begin(), out.end(), [](float v) { return std::isfinite(v); });
420 num_failed += failed;
421 if (failed || verbose) {
422 printf("%5s imatrix degenerate groups: %s\n", ggml_type_name(type), RESULT_STR[failed]);
423 }
424 }
425
426 return num_failed;
427}
428
429int main(int argc, char * argv[]) {
430 bool verbose = false;
431
432 std::string arg;
433 for (int i = 1; i < argc; i++) {
434 arg = argv[i];
435
436 if (arg == "-v") {
437 verbose = true;
438 } else {
439 fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
440 return 1;
441 }
442 }
443
444 ggml_cpu_init();
445
446 int num_failed = 0;
447
448 num_failed += test_vec_dot_f32(verbose);
449 num_failed += test_f8_e4m3(verbose);
450 num_failed += test_vec_dot_q(verbose);
451 num_failed += test_quantize_imatrix_degenerate(verbose);
452
453 if (num_failed || verbose) {
454 printf("%d tests failed\n", num_failed);
455 }
456
457 return num_failed > 0;
458}