test-quantize-fns.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. const float MAX_QUANTIZATION_REFERENCE_ERROR = 0.0001f;
  13. const float MAX_QUANTIZATION_TOTAL_ERROR = 0.002f;
  14. const float MAX_QUANTIZATION_TOTAL_ERROR_2BITS = 0.0075f;
  15. const float MAX_QUANTIZATION_TOTAL_ERROR_3BITS = 0.0040f;
  16. const float MAX_DOT_PRODUCT_ERROR = 0.02f;
  17. const char* RESULT_STR[] = {"ok", "FAILED"};
  18. // Generate synthetic data
  19. void generate_data(float offset, size_t n, float * dst) {
  20. for (size_t i = 0; i < n; i++) {
  21. dst[i] = 0.1 + 2*cosf(i + offset);
  22. }
  23. }
  24. // Calculate RMSE between two float arrays
  25. float array_rmse(const float * a1, const float * a2, size_t n) {
  26. double sum = 0;
  27. for (size_t i = 0; i < n; i++) {
  28. double diff = a1[i] - a2[i];
  29. sum += diff * diff;
  30. }
  31. return sqrtf(sum) / n;
  32. }
  33. // Total quantization error on test data
  34. float total_quantization_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data) {
  35. std::vector<uint8_t> tmp_q(2*test_size);
  36. std::vector<float> tmp_out(test_size);
  37. qfns.from_float(test_data, tmp_q.data(), test_size);
  38. qfns.to_float(tmp_q.data(), tmp_out.data(), test_size);
  39. return array_rmse(test_data, tmp_out.data(), test_size);
  40. }
  41. // Total quantization error on test data
  42. float reference_quantization_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data) {
  43. std::vector<uint8_t> tmp_q(2*test_size);
  44. std::vector<float> tmp_out(test_size);
  45. std::vector<float> tmp_out_ref(test_size);
  46. qfns.from_float(test_data, tmp_q.data(), test_size);
  47. qfns.to_float(tmp_q.data(), tmp_out.data(), test_size);
  48. qfns.from_float_reference(test_data, tmp_q.data(), test_size);
  49. qfns.to_float(tmp_q.data(), tmp_out_ref.data(), test_size);
  50. return array_rmse(tmp_out.data(), tmp_out_ref.data(), test_size);
  51. }
  52. float dot_product(const float * a1, const float * a2, size_t test_size) {
  53. double sum = 0;
  54. for (size_t i = 0; i < test_size; i++) {
  55. sum += a1[i] * a2[i];
  56. }
  57. return sum;
  58. }
  59. // Total dot product error
  60. float dot_product_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data1, const float *test_data2) {
  61. std::vector<uint8_t> tmp_q1(2*test_size);
  62. std::vector<uint8_t> tmp_q2(2*test_size);
  63. auto vdot = ggml_internal_get_type_traits(qfns.vec_dot_type);
  64. qfns.from_float(test_data1, tmp_q1.data(), test_size);
  65. vdot.from_float(test_data2, tmp_q2.data(), test_size);
  66. float result = INFINITY;
  67. qfns.vec_dot(test_size, &result, tmp_q1.data(), tmp_q2.data());
  68. const float dot_ref = dot_product(test_data1, test_data2, test_size);
  69. return fabsf(result - dot_ref) / test_size;
  70. }
  71. int main(int argc, char * argv[]) {
  72. bool verbose = false;
  73. const size_t test_size = 32 * 128;
  74. std::string arg;
  75. for (int i = 1; i < argc; i++) {
  76. arg = argv[i];
  77. if (arg == "-v") {
  78. verbose = true;
  79. } else {
  80. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  81. return 1;
  82. }
  83. }
  84. std::vector<float> test_data(test_size);
  85. std::vector<float> test_data2(test_size);
  86. generate_data(0.0, test_data.size(), test_data.data());
  87. generate_data(1.0, test_data2.size(), test_data2.data());
  88. // Initialize GGML, ensures float conversion tables are initialized
  89. struct ggml_init_params ggml_params = {
  90. /* .mem_size = */ 1*1024,
  91. /* .mem_buffer = */ NULL,
  92. /* .no_alloc = */ true,
  93. };
  94. struct ggml_context * ctx = ggml_init(ggml_params);
  95. int num_failed = 0;
  96. bool failed = false;
  97. for (int i = 0; i < GGML_TYPE_COUNT; i++) {
  98. ggml_type type = (ggml_type) i;
  99. ggml_type_traits_t qfns = ggml_internal_get_type_traits(type);
  100. if (qfns.from_float && qfns.to_float) {
  101. const float total_error = total_quantization_error(qfns, test_size, test_data.data());
  102. const float max_quantization_error =
  103. type == GGML_TYPE_Q2_K ? MAX_QUANTIZATION_TOTAL_ERROR_2BITS :
  104. type == GGML_TYPE_Q3_K ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS : MAX_QUANTIZATION_TOTAL_ERROR;
  105. failed = !(total_error < max_quantization_error);
  106. num_failed += failed;
  107. if (failed || verbose) {
  108. printf("%5s absolute quantization error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], total_error);
  109. }
  110. const float reference_error = reference_quantization_error(qfns, test_size, test_data.data());
  111. failed = !(reference_error < MAX_QUANTIZATION_REFERENCE_ERROR);
  112. num_failed += failed;
  113. if (failed || verbose) {
  114. printf("%5s reference implementation error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], reference_error);
  115. }
  116. const float vec_dot_error = dot_product_error(qfns, test_size, test_data.data(), test_data2.data());
  117. failed = !(vec_dot_error < MAX_DOT_PRODUCT_ERROR);
  118. num_failed += failed;
  119. if (failed || verbose) {
  120. printf("%5s dot product error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], vec_dot_error);
  121. }
  122. }
  123. }
  124. if (num_failed || verbose) {
  125. printf("%d tests failed\n", num_failed);
  126. }
  127. ggml_free(ctx);
  128. return num_failed > 0;
  129. }