Skip to content

Commit ebee501

Browse files
committed
Unit test for quantization functions
Use the ggml_internal_get_quantize_fn function to loop through all quantization formats and run a sanity check on the result. Also add a microbenchmark that times these functions directly without running the rest of the GGML graph.
1 parent 74f5899 commit ebee501

File tree

3 files changed

+454
-0
lines changed

3 files changed

+454
-0
lines changed

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ function(llama_add_test source)
66
endfunction()
77

88
# llama_add_test(test-double-float.c) # SLOW
9+
llama_add_test(test-quantize-fns.cpp)
10+
llama_add_test(test-quantize-perf.cpp)
911
llama_add_test(test-quantize.c)
1012
llama_add_test(test-tokenizer-0.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab.bin)

tests/test-quantize-fns.cpp

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Unit tests for quantization specific functions - quantize, dequantize and dot product
2+
3+
#include "ggml.h"
4+
5+
#undef NDEBUG
6+
#include <assert.h>
7+
#include <math.h>
8+
#include <stdio.h>
9+
#include <string>
10+
#include <vector>
11+
12+
13+
const float MAX_QUANTIZATION_REFERENCE_ERROR = 0.0001;
14+
const float MAX_QUANTIZATION_TOTAL_ERROR = 0.002;
15+
// TODO: check why q4_1 is high
16+
const float MAX_DOT_PRODUCT_ERROR = 0.02;
17+
18+
const char* RESULT_STR[] = {"ok", "FAILED"};
19+
20+
21+
// Generate synthetic data
22+
void generate_data(float offset, size_t n, float * dst) {
23+
for (size_t i = 0; i < n; i++) {
24+
dst[i] = 0.1 + 2*cosf(i + offset);
25+
}
26+
}
27+
28+
// Calculate RMSE between two float arrays
29+
float array_rmse(const float * a1, const float * a2, size_t n) {
30+
double sum = 0;
31+
for (size_t i = 0; i < n; i++) {
32+
double diff = a1[i] - a2[i];
33+
sum += diff * diff;
34+
}
35+
return sqrtf(sum) / n;
36+
}
37+
38+
// Total quantization error on test data
39+
float total_quantization_error(quantize_fns_t & qfns, size_t test_size, const float * test_data) {
40+
std::vector<uint8_t> tmp_q(test_size);
41+
std::vector<float> tmp_out(test_size);
42+
43+
qfns.quantize_row_q(test_data, tmp_q.data(), test_size);
44+
qfns.dequantize_row_q(tmp_q.data(), tmp_out.data(), test_size);
45+
return array_rmse(test_data, tmp_out.data(), test_size);
46+
}
47+
48+
// Total quantization error on test data
49+
float reference_quantization_error(quantize_fns_t & qfns, size_t test_size, const float * test_data) {
50+
std::vector<uint8_t> tmp_q(test_size);
51+
std::vector<float> tmp_out(test_size);
52+
std::vector<float> tmp_out_ref(test_size);
53+
54+
qfns.quantize_row_q(test_data, tmp_q.data(), test_size);
55+
qfns.dequantize_row_q(tmp_q.data(), tmp_out.data(), test_size);
56+
57+
qfns.quantize_row_q_reference(test_data, tmp_q.data(), test_size);
58+
qfns.dequantize_row_q(tmp_q.data(), tmp_out_ref.data(), test_size);
59+
60+
return array_rmse(tmp_out.data(), tmp_out_ref.data(), test_size);
61+
}
62+
63+
float dot_product(const float * a1, const float * a2, size_t test_size) {
64+
double sum = 0;
65+
for (size_t i = 0; i < test_size; i++) {
66+
sum += a1[i] * a2[i];
67+
}
68+
return sum;
69+
}
70+
71+
// Total dot product error
72+
float dot_product_error(quantize_fns_t & qfns, size_t test_size, const float * test_data1, const float *test_data2) {
73+
std::vector<uint8_t> tmp_q1(test_size);
74+
std::vector<uint8_t> tmp_q2(test_size);
75+
76+
qfns.quantize_row_q(test_data1, tmp_q1.data(), test_size);
77+
qfns.quantize_row_q(test_data2, tmp_q2.data(), test_size);
78+
79+
float result = INFINITY;
80+
qfns.vec_dot_q(test_size, &result, tmp_q1.data(), tmp_q2.data());
81+
82+
const float dot_ref = dot_product(test_data1, test_data2, test_size);
83+
84+
return fabsf(result - dot_ref) / test_size;
85+
}
86+
87+
int main(int argc, char * argv[]) {
88+
bool verbose = false;
89+
const size_t test_size = 32 * 100;
90+
91+
std::string arg;
92+
for (int i = 1; i < argc; i++) {
93+
arg = argv[i];
94+
95+
if (arg == "-v") {
96+
verbose = true;
97+
} else {
98+
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
99+
return 1;
100+
}
101+
}
102+
103+
std::vector<float> test_data(test_size);
104+
std::vector<float> test_data2(test_size);
105+
106+
generate_data(0.0, test_data.size(), test_data.data());
107+
generate_data(1.0, test_data2.size(), test_data2.data());
108+
109+
// Initialize GGML, ensures float conversion tables are initialized
110+
struct ggml_init_params ggml_params = {
111+
/* .mem_size = */ 1*1024,
112+
/* .mem_buffer = */ NULL,
113+
/* .no_alloc = */ true,
114+
};
115+
struct ggml_context * ctx = ggml_init(ggml_params);
116+
117+
int num_failed = 0;
118+
bool failed = false;
119+
120+
for (int i = 0; i < GGML_TYPE_COUNT; i++) {
121+
ggml_type type = (ggml_type) i;
122+
quantize_fns_t qfns = ggml_internal_get_quantize_fn(i);
123+
124+
if (qfns.quantize_row_q) {
125+
const float total_error = total_quantization_error(qfns, test_size, test_data.data());
126+
failed = !(total_error < MAX_QUANTIZATION_TOTAL_ERROR);
127+
num_failed += failed;
128+
if (failed || verbose) {
129+
printf("%5s absolute quantization error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], total_error);
130+
}
131+
132+
const float reference_error = reference_quantization_error(qfns, test_size, test_data.data());
133+
failed = !(reference_error < MAX_QUANTIZATION_REFERENCE_ERROR);
134+
num_failed += failed;
135+
if (failed || verbose) {
136+
printf("%5s reference implementation error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], reference_error);
137+
}
138+
139+
const float vec_dot_error = dot_product_error(qfns, test_size, test_data.data(), test_data2.data());
140+
failed = !(vec_dot_error < MAX_DOT_PRODUCT_ERROR);
141+
num_failed += failed;
142+
if (failed || verbose) {
143+
printf("%5s dot product error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], vec_dot_error);
144+
}
145+
}
146+
}
147+
148+
if (num_failed || verbose) {
149+
printf("%d tests failed\n", num_failed);
150+
}
151+
152+
ggml_free(ctx);
153+
154+
return num_failed > 0;
155+
}

0 commit comments

Comments
 (0)