test-quantize-fns.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Unit tests for quantization specific functions - quantize, dequantize and dot product
  2. #include "ggml.h"
  3. #undef NDEBUG
  4. #include <assert.h>
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <string>
  8. #include <vector>
  9. #if defined(_MSC_VER)
  10. #pragma warning(disable: 4244 4267) // possible loss of data
  11. #endif
  12. constexpr float MAX_QUANTIZATION_REFERENCE_ERROR = 0.0001f;
  13. constexpr float MAX_QUANTIZATION_TOTAL_ERROR = 0.002f;
  14. constexpr float MAX_QUANTIZATION_TOTAL_ERROR_2BITS = 0.0075f;
  15. constexpr float MAX_QUANTIZATION_TOTAL_ERROR_3BITS = 0.0040f;
  16. constexpr float MAX_QUANTIZATION_TOTAL_ERROR_3BITS_XXS = 0.0050f;
  17. constexpr float MAX_DOT_PRODUCT_ERROR = 0.02f;
  18. constexpr float MAX_DOT_PRODUCT_ERROR_LOWBIT = 0.04f;
  19. static const char* RESULT_STR[] = {"ok", "FAILED"};
  20. // Generate synthetic data
  21. static void generate_data(float offset, size_t n, float * dst) {
  22. for (size_t i = 0; i < n; i++) {
  23. dst[i] = 0.1 + 2*cosf(i + offset);
  24. }
  25. }
  26. // Calculate RMSE between two float arrays
  27. static float array_rmse(const float * a1, const float * a2, size_t n) {
  28. double sum = 0;
  29. for (size_t i = 0; i < n; i++) {
  30. double diff = a1[i] - a2[i];
  31. sum += diff * diff;
  32. }
  33. return sqrtf(sum) / n;
  34. }
  35. // Total quantization error on test data
  36. static float total_quantization_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data) {
  37. std::vector<uint8_t> tmp_q(2*test_size);
  38. std::vector<float> tmp_out(test_size);
  39. qfns.from_float(test_data, tmp_q.data(), test_size);
  40. qfns.to_float(tmp_q.data(), tmp_out.data(), test_size);
  41. return array_rmse(test_data, tmp_out.data(), test_size);
  42. }
  43. // Total quantization error on test data
  44. static float reference_quantization_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data) {
  45. std::vector<uint8_t> tmp_q(2*test_size);
  46. std::vector<float> tmp_out(test_size);
  47. std::vector<float> tmp_out_ref(test_size);
  48. qfns.from_float(test_data, tmp_q.data(), test_size);
  49. qfns.to_float(tmp_q.data(), tmp_out.data(), test_size);
  50. qfns.from_float_reference(test_data, tmp_q.data(), test_size);
  51. qfns.to_float(tmp_q.data(), tmp_out_ref.data(), test_size);
  52. return array_rmse(tmp_out.data(), tmp_out_ref.data(), test_size);
  53. }
  54. static float dot_product(const float * a1, const float * a2, size_t test_size) {
  55. double sum = 0;
  56. for (size_t i = 0; i < test_size; i++) {
  57. sum += a1[i] * a2[i];
  58. }
  59. return sum;
  60. }
  61. // Total dot product error
  62. static float dot_product_error(
  63. ggml_type_traits_t & qfns, size_t test_size, const float * test_data1, const float *test_data2
  64. ) {
  65. std::vector<uint8_t> tmp_q1(2*test_size);
  66. std::vector<uint8_t> tmp_q2(2*test_size);
  67. auto vdot = ggml_internal_get_type_traits(qfns.vec_dot_type);
  68. qfns.from_float(test_data1, tmp_q1.data(), test_size);
  69. vdot.from_float(test_data2, tmp_q2.data(), test_size);
  70. float result = INFINITY;
  71. qfns.vec_dot(test_size, &result, 0, tmp_q1.data(), 0, tmp_q2.data(), 0, 1);
  72. const float dot_ref = dot_product(test_data1, test_data2, test_size);
  73. return fabsf(result - dot_ref) / test_size;
  74. }
  75. int main(int argc, char * argv[]) {
  76. bool verbose = false;
  77. const size_t test_size = 32 * 128;
  78. std::string arg;
  79. for (int i = 1; i < argc; i++) {
  80. arg = argv[i];
  81. if (arg == "-v") {
  82. verbose = true;
  83. } else {
  84. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  85. return 1;
  86. }
  87. }
  88. std::vector<float> test_data(test_size);
  89. std::vector<float> test_data2(test_size);
  90. generate_data(0.0, test_data.size(), test_data.data());
  91. generate_data(1.0, test_data2.size(), test_data2.data());
  92. // Initialize GGML, ensures float conversion tables are initialized
  93. struct ggml_init_params ggml_params = {
  94. /* .mem_size = */ 1*1024,
  95. /* .mem_buffer = */ NULL,
  96. /* .no_alloc = */ true,
  97. };
  98. struct ggml_context * ctx = ggml_init(ggml_params);
  99. int num_failed = 0;
  100. bool failed = false;
  101. for (int i = 0; i < GGML_TYPE_COUNT; i++) {
  102. ggml_type type = (ggml_type) i;
  103. ggml_type_traits_t qfns = ggml_internal_get_type_traits(type);
  104. // deprecated - skip
  105. if (qfns.blck_size == 0) {
  106. continue;
  107. }
  108. const ggml_type ei = (ggml_type)i;
  109. if (ei == GGML_TYPE_IQ2_XXS || ei == GGML_TYPE_IQ2_XS) {
  110. printf("Skip %s due to missing quantization functionality\n", ggml_type_name(ei));
  111. continue;
  112. }
  113. printf("Testing %s\n", ggml_type_name((ggml_type) i));
  114. ggml_quantize_init(ei);
  115. if (qfns.from_float && qfns.to_float) {
  116. const float total_error = total_quantization_error(qfns, test_size, test_data.data());
  117. const float max_quantization_error =
  118. type == GGML_TYPE_Q2_K ? MAX_QUANTIZATION_TOTAL_ERROR_2BITS :
  119. type == GGML_TYPE_Q3_K ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS :
  120. type == GGML_TYPE_IQ3_S ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS :
  121. type == GGML_TYPE_IQ3_XXS ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS_XXS : MAX_QUANTIZATION_TOTAL_ERROR;
  122. failed = !(total_error < max_quantization_error);
  123. num_failed += failed;
  124. if (failed || verbose) {
  125. printf("%5s absolute quantization error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], total_error);
  126. }
  127. const float reference_error = reference_quantization_error(qfns, test_size, test_data.data());
  128. failed = !(reference_error < MAX_QUANTIZATION_REFERENCE_ERROR);
  129. num_failed += failed;
  130. if (failed || verbose) {
  131. printf("%5s reference implementation error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], reference_error);
  132. }
  133. const float vec_dot_error = dot_product_error(qfns, test_size, test_data.data(), test_data2.data());
  134. const float max_allowed_error = type == GGML_TYPE_Q2_K || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ2_XXS ||
  135. type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S ? MAX_DOT_PRODUCT_ERROR_LOWBIT
  136. : MAX_DOT_PRODUCT_ERROR;
  137. failed = !(vec_dot_error < max_allowed_error);
  138. num_failed += failed;
  139. if (failed || verbose) {
  140. printf("%5s dot product error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], vec_dot_error);
  141. }
  142. }
  143. }
  144. if (num_failed || verbose) {
  145. printf("%d tests failed\n", num_failed);
  146. }
  147. ggml_free(ctx);
  148. return num_failed > 0;
  149. }