test-quantize-perf.cpp 14 KB

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