test-quantize-perf.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Benchmark quantization specific functions on synthetic data
  2. #include "ggml.h"
  3. #undef NDEBUG
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <functional>
  7. #include <inttypes.h>
  8. #include <math.h>
  9. #include <memory>
  10. #include <stdio.h>
  11. #include <string>
  12. #include <vector>
  13. #if defined(_MSC_VER)
  14. #pragma warning(disable: 4244 4267) // possible loss of data
  15. #endif
  16. #define MAX_ALIGNMENT 64
  17. #define QK 32
  18. #define WARMUP 5
  19. #define ITERATIONS 10
  20. #define L1_SIZE 32*128
  21. #define L2_SIZE 32*2048
  22. #define L3_SIZE 32*20480
  23. #define MEM_SIZE 32*2048000
  24. struct quantize_perf_params {
  25. std::vector<std::string> include_types;
  26. std::vector<size_t> test_sizes;
  27. size_t alignment_offset = 0;
  28. bool op_quantize_row_q_reference = false;
  29. bool op_quantize_row_q = false;
  30. bool op_dequantize_row_q = false;
  31. bool op_quantize_row_q_dot = false;
  32. bool op_vec_dot_q = false;
  33. };
  34. #if defined(__x86_64__) || defined(__i386__)
  35. #include <x86intrin.h>
  36. inline int64_t cpu_cycles() {
  37. // Rough way to detect new-ish CPUs
  38. #ifdef __POPCNT__
  39. unsigned int dummy;
  40. return __rdtscp(&dummy);
  41. #else
  42. return __rdtsc();
  43. #endif
  44. }
  45. #else
  46. #define cpu_cycles() 0
  47. #endif
  48. // Generate synthetic data
  49. void generate_data(float offset, size_t n, float * dst) {
  50. for (size_t i = 0; i < n; i++) {
  51. dst[i] = 0.1 + 2*cosf(i + offset);
  52. }
  53. }
  54. float gigabytes_per_second(size_t bytes, int64_t usecs) {
  55. return bytes / (float) usecs * 1000000 / (1024*1024*1024);
  56. }
  57. void * align_with_offset(void * ptr, int offset) {
  58. size_t dummy_size = MAX_ALIGNMENT * 4;
  59. return (char *) std::align(MAX_ALIGNMENT, MAX_ALIGNMENT, ptr, dummy_size) + offset;
  60. }
  61. void benchmark_function(size_t size, size_t q_size, std::function<size_t(void)> function) {
  62. int64_t min_time_us = INT64_MAX;
  63. int64_t total_time_us = 0;
  64. int64_t min_time_cycles = INT64_MAX;
  65. int64_t total_time_cycles = 0;
  66. for (int i = 0; i < WARMUP; i++) {
  67. function();
  68. }
  69. for (int i = 0; i < ITERATIONS; i++) {
  70. const int64_t start_time = ggml_time_us();
  71. const int64_t start_cycles = cpu_cycles();
  72. function();
  73. const int64_t end_cycles = cpu_cycles();
  74. const int64_t end_time = ggml_time_us();
  75. total_time_cycles += end_cycles - start_cycles;
  76. min_time_cycles = std::min(min_time_cycles, end_cycles - start_cycles);
  77. total_time_us += end_time - start_time;
  78. min_time_us = std::min(min_time_us, end_time - start_time);
  79. }
  80. printf(" min cycles/%d vals : %9.2f\n", QK, QK * min_time_cycles / (float) size);
  81. printf(" avg cycles/%d vals : %9.2f\n", QK, QK * total_time_cycles / (float) (size * ITERATIONS));
  82. printf(" float32 throughput : %9.2f GB/s\n", gigabytes_per_second(4 * size * ITERATIONS, total_time_us));
  83. printf(" quantized throughput : %9.2f GB/s\n", gigabytes_per_second(q_size * ITERATIONS, total_time_us));
  84. }
  85. int main(int argc, char * argv[]) {
  86. quantize_perf_params params {};
  87. // read command line
  88. bool invalid_param = false;
  89. std::string arg;
  90. for (int i = 1; i < argc; i++) {
  91. arg = argv[i];
  92. if (arg == "--size") {
  93. if (++i >= argc) {
  94. invalid_param = true;
  95. break;
  96. }
  97. size_t size = std::stoi(argv[i]);
  98. if (size % 32 != 0) {
  99. fprintf(stderr, "error: size %zu not divisible by 32\n", size);
  100. invalid_param = true;
  101. break;
  102. }
  103. params.test_sizes.push_back(size);
  104. } else if (arg == "-3") {
  105. // quick select sizes that probably fit in CPU caches
  106. params.test_sizes.push_back(L1_SIZE);
  107. params.test_sizes.push_back(L2_SIZE);
  108. params.test_sizes.push_back(L3_SIZE);
  109. } else if (arg == "-4") {
  110. // quick select cache sizes + memory
  111. params.test_sizes.push_back(L1_SIZE);
  112. params.test_sizes.push_back(L2_SIZE);
  113. params.test_sizes.push_back(L3_SIZE);
  114. params.test_sizes.push_back(MEM_SIZE);
  115. } else if (arg == "--op") {
  116. if (++i >= argc) {
  117. invalid_param = true;
  118. break;
  119. }
  120. std::string op {argv[i]};
  121. if (op == "quantize_row_q_reference") {
  122. params.op_quantize_row_q_reference = true;
  123. } else if (op == "quantize_row_q") {
  124. params.op_quantize_row_q = true;
  125. } else if (op == "dequantize_row_q") {
  126. params.op_dequantize_row_q = true;
  127. } else if (op == "quantize_row_q_dot") {
  128. params.op_quantize_row_q_dot = true;
  129. } else if (op == "vec_dot_q") {
  130. params.op_vec_dot_q = true;
  131. } else {
  132. invalid_param = true;
  133. break;
  134. }
  135. } else if (arg == "--type") {
  136. if (++i >= argc) {
  137. invalid_param = true;
  138. break;
  139. }
  140. params.include_types.push_back(argv[i]);
  141. } else if (arg == "--alignment-offset") {
  142. if (++i >= argc) {
  143. invalid_param = true;
  144. break;
  145. }
  146. int alignment = std::stoi(argv[i]);
  147. if (alignment < 0 || alignment > MAX_ALIGNMENT) {
  148. fprintf(stderr, "error: aligment-offset must be less than %d\n", MAX_ALIGNMENT);
  149. invalid_param = true;
  150. break;
  151. }
  152. params.alignment_offset = alignment;
  153. } else {
  154. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  155. return 1;
  156. }
  157. }
  158. if (invalid_param) {
  159. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  160. return 1;
  161. }
  162. if (params.test_sizes.empty()) {
  163. params.test_sizes.push_back(L1_SIZE);
  164. }
  165. if (!(params.op_quantize_row_q_reference || params.op_quantize_row_q || params.op_dequantize_row_q || params.op_quantize_row_q_dot || params.op_vec_dot_q)) {
  166. params.op_quantize_row_q_reference = params.op_quantize_row_q = params.op_dequantize_row_q = params.op_quantize_row_q_dot = params.op_vec_dot_q = true;
  167. }
  168. std::sort(params.test_sizes.begin(), params.test_sizes.end());
  169. size_t largest = params.test_sizes.back();
  170. std::vector<uint8_t> test_data1_v(largest*4 + MAX_ALIGNMENT*2);
  171. std::vector<uint8_t> test_data2_v(largest*4 + MAX_ALIGNMENT*2);
  172. std::vector<uint8_t> test_q1_v(largest*4 + MAX_ALIGNMENT*2);
  173. std::vector<uint8_t> test_q2_v(largest*4 + MAX_ALIGNMENT*2);
  174. std::vector<uint8_t> test_out_v(largest*4 + MAX_ALIGNMENT*2);
  175. float * test_data1 = (float *) align_with_offset(test_data1_v.data(), params.alignment_offset);
  176. float * test_data2 = (float *) align_with_offset(test_data2_v.data(), params.alignment_offset);
  177. float * test_q1 = (float *) align_with_offset(test_q1_v.data(), params.alignment_offset);
  178. float * test_q2 = (float *) align_with_offset(test_q2_v.data(), params.alignment_offset);
  179. float * test_out = (float *) align_with_offset(test_out_v.data(), params.alignment_offset);
  180. generate_data(0, largest, test_data1);
  181. generate_data(1, largest, test_data2);
  182. // Initialize GGML, ensures float conversion tables are initialized
  183. struct ggml_init_params ggml_params = {
  184. /* .mem_size = */ 1*1024,
  185. /* .mem_buffer = */ NULL,
  186. /* .no_alloc = */ true,
  187. };
  188. struct ggml_context * ctx = ggml_init(ggml_params);
  189. for (int i = 0; i < GGML_TYPE_COUNT; i++) {
  190. ggml_type type = (ggml_type) i;
  191. quantize_fns_t qfns = ggml_internal_get_quantize_fn(i);
  192. if (!params.include_types.empty() && std::find(params.include_types.begin(), params.include_types.end(), ggml_type_name(type)) == params.include_types.end()) {
  193. continue;
  194. }
  195. if (qfns.quantize_row_q && qfns.dequantize_row_q) {
  196. printf("%s\n", ggml_type_name(type));
  197. if (params.op_quantize_row_q_reference) {
  198. printf(" quantize_row_q_reference\n");
  199. for (size_t size : params.test_sizes) {
  200. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  201. auto quantize_fn = [&](void ) {
  202. qfns.quantize_row_q_reference(test_data1, test_q1, size);
  203. return test_q1[0];
  204. };
  205. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  206. benchmark_function(size, quantized_size, quantize_fn);
  207. }
  208. printf("\n");
  209. }
  210. if (params.op_quantize_row_q) {
  211. printf(" quantize_row_q\n");
  212. for (size_t size : params.test_sizes) {
  213. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  214. auto quantize_fn = [&](void ) {
  215. qfns.quantize_row_q(test_data1, test_q1, size);
  216. return test_q1[0];
  217. };
  218. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  219. benchmark_function(size, quantized_size, quantize_fn);
  220. }
  221. printf("\n");
  222. }
  223. if (params.op_dequantize_row_q) {
  224. printf(" dequantize_row_q\n");
  225. qfns.quantize_row_q(test_data1, test_q1, largest);
  226. for (size_t size : params.test_sizes) {
  227. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  228. auto quantize_fn = [&](void ) {
  229. qfns.dequantize_row_q(test_q1, test_out, size);
  230. return test_out[0];
  231. };
  232. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  233. benchmark_function(size, quantized_size, quantize_fn);
  234. }
  235. printf("\n");
  236. }
  237. if (params.op_quantize_row_q_dot) {
  238. printf(" quantize_row_q_dot\n");
  239. for (size_t size : params.test_sizes) {
  240. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  241. auto quantize_fn = [&](void ) {
  242. qfns.quantize_row_q_dot(test_data1, test_q1, size);
  243. return test_q1[0];
  244. };
  245. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  246. benchmark_function(size, quantized_size, quantize_fn);
  247. }
  248. printf("\n");
  249. }
  250. if (params.op_vec_dot_q) {
  251. printf(" vec_dot_q\n");
  252. qfns.quantize_row_q(test_data1, test_q1, largest);
  253. qfns.quantize_row_q(test_data2, test_q2, largest);
  254. for (size_t size : params.test_sizes) {
  255. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  256. auto quantize_fn = [&](void ) {
  257. float result;
  258. qfns.vec_dot_q(size, &result, test_q1, test_q2);
  259. return result;
  260. };
  261. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  262. benchmark_function(size, quantized_size, quantize_fn);
  263. }
  264. printf("\n");
  265. }
  266. }
  267. }
  268. ggml_free(ctx);
  269. return 0;
  270. }