test-quantize-perf.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Benchmark quantization specific functions on synthetic data
  2. #include "ggml.h"
  3. #include "ggml-cpu.h"
  4. #undef NDEBUG
  5. #include <algorithm>
  6. #include <assert.h>
  7. #include <functional>
  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 MAX_ITERATIONS 100000000
  21. #define L1_SIZE 32*128
  22. #define L2_SIZE 32*2048
  23. #define L3_SIZE 32*20480
  24. #define MEM_SIZE 32*2048000
  25. struct quantize_perf_params {
  26. std::vector<std::string> include_types;
  27. std::vector<size_t> test_sizes;
  28. size_t alignment_offset = 0;
  29. bool op_quantize_row_q_reference = false;
  30. bool op_quantize_row_q = false;
  31. bool op_dequantize_row_q = false;
  32. bool op_quantize_row_q_dot = false;
  33. bool op_vec_dot_q = false;
  34. int64_t iterations = ITERATIONS;
  35. };
  36. #if defined(__x86_64__) || defined(__i386__)
  37. #include <x86intrin.h>
  38. inline int64_t cpu_cycles() {
  39. // Rough way to detect new-ish CPUs
  40. #ifdef __POPCNT__
  41. unsigned int dummy;
  42. return __rdtscp(&dummy);
  43. #else
  44. return __rdtsc();
  45. #endif
  46. }
  47. #else
  48. #define cpu_cycles() 0
  49. #endif
  50. // Generate synthetic data
  51. static void generate_data(float offset, size_t n, float * dst) {
  52. for (size_t i = 0; i < n; i++) {
  53. dst[i] = 0.1 + 2*cosf(i + offset);
  54. }
  55. }
  56. static float gigabytes_per_second(size_t bytes, int64_t usecs) {
  57. return bytes / (float) usecs * 1000000 / (1024*1024*1024);
  58. }
  59. static void * align_with_offset(void * ptr, int offset) {
  60. size_t dummy_size = MAX_ALIGNMENT * 4;
  61. return (char *) std::align(MAX_ALIGNMENT, MAX_ALIGNMENT, ptr, dummy_size) + offset;
  62. }
  63. static void benchmark_function(size_t size, size_t q_size, int64_t iterations, const std::function<float(void)> & func) {
  64. int64_t min_time_us = INT64_MAX;
  65. int64_t total_time_us = 0;
  66. int64_t min_time_cycles = INT64_MAX;
  67. int64_t total_time_cycles = 0;
  68. for (int i = 0; i < WARMUP; i++) {
  69. func();
  70. }
  71. for (int i = 0; i < iterations; i++) {
  72. const int64_t start_time = ggml_time_us();
  73. const int64_t start_cycles = cpu_cycles();
  74. func();
  75. const int64_t end_cycles = cpu_cycles();
  76. const int64_t end_time = ggml_time_us();
  77. total_time_cycles += end_cycles - start_cycles;
  78. min_time_cycles = std::min(min_time_cycles, end_cycles - start_cycles);
  79. total_time_us += end_time - start_time;
  80. min_time_us = std::min(min_time_us, end_time - start_time);
  81. }
  82. printf(" min cycles/%d vals : %9.2f\n", QK, QK * min_time_cycles / (float) size);
  83. printf(" avg cycles/%d vals : %9.2f\n", QK, QK * total_time_cycles / (float) (size * iterations));
  84. printf(" float32 throughput : %9.2f GB/s\n", gigabytes_per_second(4 * size * iterations, total_time_us));
  85. printf(" quantized throughput : %9.2f GB/s\n", gigabytes_per_second(q_size * iterations, total_time_us));
  86. }
  87. static void usage(char * argv[]) {
  88. printf("Benchmark quantization specific functions on synthetic data\n");
  89. printf("\n");
  90. printf("usage: %s [options]\n", argv[0]);
  91. printf("\n");
  92. printf("options: (default)\n");
  93. printf(" -h, --help show this help message and exit\n");
  94. printf(" --size SIZE set test size, divisible by 32 (L1_SIZE:%d)\n", L1_SIZE);
  95. printf(" -3 use size as L1, L2, L3 sizes (L1:%d L2:%d L3:%d)\n", L1_SIZE, L2_SIZE, L3_SIZE);
  96. printf(" -4 use size as L1, L2, L3, MEM sizes (L1:%d L2:%d L3:%d MEM:%d)\n", L1_SIZE, L2_SIZE, L3_SIZE, MEM_SIZE);
  97. printf(" --op OP set test operation as quantize_row_q_reference, quantize_row_q, dequantize_row_q,\n");
  98. printf(" quantize_row_q_dot, vec_dot_q (all)\n");
  99. printf(" --type TYPE set test type as");
  100. for (int i = 0; i < GGML_TYPE_COUNT; i++) {
  101. ggml_type type = (ggml_type) i;
  102. const auto * qfns = ggml_get_type_traits(type);
  103. const auto * qfns_cpu = ggml_get_type_traits_cpu(type);
  104. if (ggml_type_name(type) != NULL) {
  105. if (qfns_cpu->from_float && qfns->to_float) {
  106. printf(" %s", ggml_type_name(type));
  107. }
  108. }
  109. }
  110. printf(" (all)\n");
  111. printf(" --alignment-offset OFFSET\n");
  112. printf(" set alignment offset as OFFSET (0)\n");
  113. printf(" -i NUM, --iterations NUM\n");
  114. printf(" set test iteration number (%d)\n", ITERATIONS);
  115. }
  116. int main(int argc, char * argv[]) {
  117. quantize_perf_params params {};
  118. // read command line
  119. bool invalid_param = false;
  120. std::string arg;
  121. for (int i = 1; i < argc; i++) {
  122. arg = argv[i];
  123. if (arg == "--size") {
  124. if (++i >= argc) {
  125. invalid_param = true;
  126. break;
  127. }
  128. size_t size = std::stoi(argv[i]);
  129. if (size % 32 != 0) {
  130. fprintf(stderr, "error: size %zu not divisible by 32\n", size);
  131. invalid_param = true;
  132. break;
  133. }
  134. params.test_sizes.push_back(size);
  135. } else if (arg == "-3") {
  136. // quick select sizes that probably fit in CPU caches
  137. params.test_sizes.push_back(L1_SIZE);
  138. params.test_sizes.push_back(L2_SIZE);
  139. params.test_sizes.push_back(L3_SIZE);
  140. } else if (arg == "-4") {
  141. // quick select cache sizes + memory
  142. params.test_sizes.push_back(L1_SIZE);
  143. params.test_sizes.push_back(L2_SIZE);
  144. params.test_sizes.push_back(L3_SIZE);
  145. params.test_sizes.push_back(MEM_SIZE);
  146. } else if (arg == "--op") {
  147. if (++i >= argc) {
  148. invalid_param = true;
  149. break;
  150. }
  151. std::string op {argv[i]};
  152. if (op == "quantize_row_q_reference") {
  153. params.op_quantize_row_q_reference = true;
  154. } else if (op == "quantize_row_q") {
  155. params.op_quantize_row_q = true;
  156. } else if (op == "dequantize_row_q") {
  157. params.op_dequantize_row_q = true;
  158. } else if (op == "quantize_row_q_dot") {
  159. params.op_quantize_row_q_dot = true;
  160. } else if (op == "vec_dot_q") {
  161. params.op_vec_dot_q = true;
  162. } else {
  163. invalid_param = true;
  164. break;
  165. }
  166. } else if (arg == "--type") {
  167. if (++i >= argc) {
  168. invalid_param = true;
  169. break;
  170. }
  171. params.include_types.push_back(argv[i]);
  172. } else if (arg == "--alignment-offset") {
  173. if (++i >= argc) {
  174. invalid_param = true;
  175. break;
  176. }
  177. int alignment = std::stoi(argv[i]);
  178. if (alignment < 0 || alignment > MAX_ALIGNMENT) {
  179. fprintf(stderr, "error: alignment-offset must be less than %d\n", MAX_ALIGNMENT);
  180. invalid_param = true;
  181. break;
  182. }
  183. params.alignment_offset = alignment;
  184. } else if ((arg == "-i") || (arg == "--iterations")) {
  185. if (++i >= argc) {
  186. invalid_param = true;
  187. break;
  188. }
  189. int number = std::stoi(argv[i]);
  190. if (number < 0 || number > MAX_ITERATIONS) {
  191. fprintf(stderr, "error: iterations must be less than %d\n", MAX_ITERATIONS);
  192. invalid_param = true;
  193. break;
  194. }
  195. params.iterations = number;
  196. } else if ((arg == "-h") || (arg == "--help")) {
  197. usage(argv);
  198. return 1;
  199. } else {
  200. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  201. return 1;
  202. }
  203. }
  204. if (invalid_param) {
  205. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  206. return 1;
  207. }
  208. if (params.test_sizes.empty()) {
  209. params.test_sizes.push_back(L1_SIZE);
  210. }
  211. 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)) {
  212. 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;
  213. }
  214. std::sort(params.test_sizes.begin(), params.test_sizes.end());
  215. size_t largest = params.test_sizes.back();
  216. std::vector<uint8_t> test_data1_v(largest*4 + MAX_ALIGNMENT*2);
  217. std::vector<uint8_t> test_data2_v(largest*4 + MAX_ALIGNMENT*2);
  218. std::vector<uint8_t> test_q1_v (largest*4 + MAX_ALIGNMENT*2);
  219. std::vector<uint8_t> test_q2_v (largest*4 + MAX_ALIGNMENT*2);
  220. std::vector<uint8_t> test_out_v (largest*4 + MAX_ALIGNMENT*2);
  221. float * test_data1 = (float *) align_with_offset(test_data1_v.data(), params.alignment_offset);
  222. float * test_data2 = (float *) align_with_offset(test_data2_v.data(), params.alignment_offset);
  223. float * test_q1 = (float *) align_with_offset(test_q1_v.data(), params.alignment_offset);
  224. float * test_q2 = (float *) align_with_offset(test_q2_v.data(), params.alignment_offset);
  225. float * test_out = (float *) align_with_offset(test_out_v.data(), params.alignment_offset);
  226. generate_data(0, largest, test_data1);
  227. generate_data(1, largest, test_data2);
  228. int64_t iterations = params.iterations;
  229. ggml_cpu_init();
  230. for (int i = 0; i < GGML_TYPE_COUNT; i++) {
  231. ggml_type type = (ggml_type) i;
  232. const auto * qfns = ggml_get_type_traits(type);
  233. const auto * qfns_cpu = ggml_get_type_traits_cpu(type);
  234. if (!params.include_types.empty() && ggml_type_name(type) && std::find(params.include_types.begin(), params.include_types.end(), ggml_type_name(type)) == params.include_types.end()) {
  235. continue;
  236. }
  237. if (qfns_cpu->from_float && qfns->to_float) {
  238. printf("%s\n", ggml_type_name(type));
  239. ggml_quantize_init(type);
  240. if (params.op_quantize_row_q_reference) {
  241. printf(" quantize_row_q_reference\n");
  242. for (size_t size : params.test_sizes) {
  243. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  244. auto quantize_fn = [&](void) -> float {
  245. qfns->from_float_ref(test_data1, test_q1, size);
  246. return test_q1[0];
  247. };
  248. size_t quantized_size = ggml_row_size(type, size);
  249. benchmark_function(size, quantized_size, iterations, quantize_fn);
  250. }
  251. printf("\n");
  252. }
  253. if (params.op_quantize_row_q) {
  254. printf(" quantize_row_q\n");
  255. for (size_t size : params.test_sizes) {
  256. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  257. auto quantize_fn = [&](void) -> float {
  258. qfns_cpu->from_float(test_data1, test_q1, size);
  259. return test_q1[0];
  260. };
  261. size_t quantized_size = ggml_row_size(type, size);
  262. benchmark_function(size, quantized_size, iterations, quantize_fn);
  263. }
  264. printf("\n");
  265. }
  266. if (params.op_dequantize_row_q) {
  267. printf(" dequantize_row_q\n");
  268. qfns_cpu->from_float(test_data1, test_q1, largest);
  269. for (size_t size : params.test_sizes) {
  270. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  271. auto quantize_fn = [&](void) -> float {
  272. qfns->to_float(test_q1, test_out, size);
  273. return test_out[0];
  274. };
  275. size_t quantized_size = ggml_row_size(type, size);
  276. benchmark_function(size, quantized_size, iterations, quantize_fn);
  277. }
  278. printf("\n");
  279. }
  280. if (params.op_quantize_row_q_dot) {
  281. printf(" quantize_row_q_dot\n");
  282. for (size_t size : params.test_sizes) {
  283. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  284. auto quantize_fn = [&](void) -> float {
  285. const auto * vdot = ggml_get_type_traits_cpu(qfns_cpu->vec_dot_type);
  286. vdot->from_float(test_data1, test_q1, size);
  287. return test_q1[0];
  288. };
  289. size_t quantized_size = ggml_row_size(type, size);
  290. benchmark_function(size, quantized_size, iterations, quantize_fn);
  291. }
  292. printf("\n");
  293. }
  294. if (params.op_vec_dot_q) {
  295. printf(" vec_dot_q\n");
  296. qfns_cpu->from_float(test_data1, test_q1, largest);
  297. qfns_cpu->from_float(test_data2, test_q2, largest);
  298. for (size_t size : params.test_sizes) {
  299. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  300. auto quantize_fn = [&](void) -> float {
  301. float result;
  302. qfns_cpu->vec_dot(size, &result, 0, test_q1, 0, test_q2, 0, 1);
  303. return result;
  304. };
  305. size_t quantized_size = ggml_row_size(type, size);
  306. benchmark_function(size, quantized_size, iterations, quantize_fn);
  307. }
  308. printf("\n");
  309. }
  310. }
  311. }
  312. return 0;
  313. }