1
0

test-quantize-perf.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. if (ggml_type_name(type) != NULL) {
  105. if (qfns->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. // Initialize GGML, ensures float conversion tables are initialized
  230. struct ggml_init_params ggml_params = {
  231. /* .mem_size = */ 1*1024,
  232. /* .mem_buffer = */ NULL,
  233. /* .no_alloc = */ true,
  234. };
  235. struct ggml_context * ctx = ggml_init(ggml_params);
  236. for (int i = 0; i < GGML_TYPE_COUNT; i++) {
  237. ggml_type type = (ggml_type) i;
  238. const auto * qfns = ggml_get_type_traits(type);
  239. const auto * qfns_cpu = ggml_get_type_traits_cpu(type);
  240. 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()) {
  241. continue;
  242. }
  243. if (qfns->from_float && qfns->to_float) {
  244. printf("%s\n", ggml_type_name(type));
  245. ggml_quantize_init(type);
  246. if (params.op_quantize_row_q_reference) {
  247. printf(" quantize_row_q_reference\n");
  248. for (size_t size : params.test_sizes) {
  249. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  250. auto quantize_fn = [&](void) -> float {
  251. qfns->from_float_ref(test_data1, test_q1, size);
  252. return test_q1[0];
  253. };
  254. size_t quantized_size = ggml_row_size(type, size);
  255. benchmark_function(size, quantized_size, iterations, quantize_fn);
  256. }
  257. printf("\n");
  258. }
  259. if (params.op_quantize_row_q) {
  260. printf(" quantize_row_q\n");
  261. for (size_t size : params.test_sizes) {
  262. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  263. auto quantize_fn = [&](void) -> float {
  264. qfns->from_float(test_data1, test_q1, size);
  265. return test_q1[0];
  266. };
  267. size_t quantized_size = ggml_row_size(type, size);
  268. benchmark_function(size, quantized_size, iterations, quantize_fn);
  269. }
  270. printf("\n");
  271. }
  272. if (params.op_dequantize_row_q) {
  273. printf(" dequantize_row_q\n");
  274. qfns->from_float(test_data1, test_q1, largest);
  275. for (size_t size : params.test_sizes) {
  276. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  277. auto quantize_fn = [&](void) -> float {
  278. qfns->to_float(test_q1, test_out, size);
  279. return test_out[0];
  280. };
  281. size_t quantized_size = ggml_row_size(type, size);
  282. benchmark_function(size, quantized_size, iterations, quantize_fn);
  283. }
  284. printf("\n");
  285. }
  286. if (params.op_quantize_row_q_dot) {
  287. printf(" quantize_row_q_dot\n");
  288. for (size_t size : params.test_sizes) {
  289. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  290. auto quantize_fn = [&](void) -> float {
  291. const auto * vdot = ggml_get_type_traits(qfns_cpu->vec_dot_type);
  292. vdot->from_float(test_data1, test_q1, size);
  293. return test_q1[0];
  294. };
  295. size_t quantized_size = ggml_row_size(type, size);
  296. benchmark_function(size, quantized_size, iterations, quantize_fn);
  297. }
  298. printf("\n");
  299. }
  300. if (params.op_vec_dot_q) {
  301. printf(" vec_dot_q\n");
  302. qfns->from_float(test_data1, test_q1, largest);
  303. qfns->from_float(test_data2, test_q2, largest);
  304. for (size_t size : params.test_sizes) {
  305. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  306. auto quantize_fn = [&](void) -> float {
  307. float result;
  308. qfns_cpu->vec_dot(size, &result, 0, test_q1, 0, test_q2, 0, 1);
  309. return result;
  310. };
  311. size_t quantized_size = ggml_row_size(type, size);
  312. benchmark_function(size, quantized_size, iterations, quantize_fn);
  313. }
  314. printf("\n");
  315. }
  316. }
  317. }
  318. ggml_free(ctx);
  319. return 0;
  320. }