test-quantize-perf.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 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. 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. float gigabytes_per_second(size_t bytes, int64_t usecs) {
  57. return bytes / (float) usecs * 1000000 / (1024*1024*1024);
  58. }
  59. 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. void benchmark_function(size_t size, size_t q_size, int64_t iterations, const std::function<size_t(void)> & function) {
  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. function();
  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. function();
  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. 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 opration 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. ggml_type_traits_t qfns = ggml_internal_get_type_traits(type);
  103. if (ggml_type_name(type) != NULL) {
  104. if (qfns.from_float && qfns.to_float) {
  105. printf(" %s", ggml_type_name(type));
  106. }
  107. }
  108. }
  109. printf(" (all)\n");
  110. printf(" --alignment-offset OFFSET\n");
  111. printf(" set alignment offset as OFFSET (0)\n");
  112. printf(" -i NUM, --iterations NUM\n");
  113. printf(" set test iteration number (%d)\n", ITERATIONS);
  114. }
  115. int main(int argc, char * argv[]) {
  116. quantize_perf_params params {};
  117. // read command line
  118. bool invalid_param = false;
  119. std::string arg;
  120. for (int i = 1; i < argc; i++) {
  121. arg = argv[i];
  122. if (arg == "--size") {
  123. if (++i >= argc) {
  124. invalid_param = true;
  125. break;
  126. }
  127. size_t size = std::stoi(argv[i]);
  128. if (size % 32 != 0) {
  129. fprintf(stderr, "error: size %zu not divisible by 32\n", size);
  130. invalid_param = true;
  131. break;
  132. }
  133. params.test_sizes.push_back(size);
  134. } else if (arg == "-3") {
  135. // quick select sizes that probably fit in CPU caches
  136. params.test_sizes.push_back(L1_SIZE);
  137. params.test_sizes.push_back(L2_SIZE);
  138. params.test_sizes.push_back(L3_SIZE);
  139. } else if (arg == "-4") {
  140. // quick select cache sizes + memory
  141. params.test_sizes.push_back(L1_SIZE);
  142. params.test_sizes.push_back(L2_SIZE);
  143. params.test_sizes.push_back(L3_SIZE);
  144. params.test_sizes.push_back(MEM_SIZE);
  145. } else if (arg == "--op") {
  146. if (++i >= argc) {
  147. invalid_param = true;
  148. break;
  149. }
  150. std::string op {argv[i]};
  151. if (op == "quantize_row_q_reference") {
  152. params.op_quantize_row_q_reference = true;
  153. } else if (op == "quantize_row_q") {
  154. params.op_quantize_row_q = true;
  155. } else if (op == "dequantize_row_q") {
  156. params.op_dequantize_row_q = true;
  157. } else if (op == "quantize_row_q_dot") {
  158. params.op_quantize_row_q_dot = true;
  159. } else if (op == "vec_dot_q") {
  160. params.op_vec_dot_q = true;
  161. } else {
  162. invalid_param = true;
  163. break;
  164. }
  165. } else if (arg == "--type") {
  166. if (++i >= argc) {
  167. invalid_param = true;
  168. break;
  169. }
  170. params.include_types.push_back(argv[i]);
  171. } else if (arg == "--alignment-offset") {
  172. if (++i >= argc) {
  173. invalid_param = true;
  174. break;
  175. }
  176. int alignment = std::stoi(argv[i]);
  177. if (alignment < 0 || alignment > MAX_ALIGNMENT) {
  178. fprintf(stderr, "error: aligment-offset must be less than %d\n", MAX_ALIGNMENT);
  179. invalid_param = true;
  180. break;
  181. }
  182. params.alignment_offset = alignment;
  183. } else if ((arg == "-i") || (arg == "--iterations")) {
  184. if (++i >= argc) {
  185. invalid_param = true;
  186. break;
  187. }
  188. int number = std::stoi(argv[i]);
  189. if (number < 0 || number > MAX_ITERATIONS) {
  190. fprintf(stderr, "error: iterations must be less than %d\n", MAX_ITERATIONS);
  191. invalid_param = true;
  192. break;
  193. }
  194. params.iterations = number;
  195. } else if ((arg == "-h") || (arg == "--help")) {
  196. usage(argv);
  197. return 1;
  198. } else {
  199. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  200. return 1;
  201. }
  202. }
  203. if (invalid_param) {
  204. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  205. return 1;
  206. }
  207. if (params.test_sizes.empty()) {
  208. params.test_sizes.push_back(L1_SIZE);
  209. }
  210. 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)) {
  211. 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;
  212. }
  213. std::sort(params.test_sizes.begin(), params.test_sizes.end());
  214. size_t largest = params.test_sizes.back();
  215. std::vector<uint8_t> test_data1_v(largest*4 + MAX_ALIGNMENT*2);
  216. std::vector<uint8_t> test_data2_v(largest*4 + MAX_ALIGNMENT*2);
  217. std::vector<uint8_t> test_q1_v(largest*4 + MAX_ALIGNMENT*2);
  218. std::vector<uint8_t> test_q2_v(largest*4 + MAX_ALIGNMENT*2);
  219. std::vector<uint8_t> test_out_v(largest*4 + MAX_ALIGNMENT*2);
  220. float * test_data1 = (float *) align_with_offset(test_data1_v.data(), params.alignment_offset);
  221. float * test_data2 = (float *) align_with_offset(test_data2_v.data(), params.alignment_offset);
  222. float * test_q1 = (float *) align_with_offset(test_q1_v.data(), params.alignment_offset);
  223. float * test_q2 = (float *) align_with_offset(test_q2_v.data(), params.alignment_offset);
  224. float * test_out = (float *) align_with_offset(test_out_v.data(), params.alignment_offset);
  225. generate_data(0, largest, test_data1);
  226. generate_data(1, largest, test_data2);
  227. int64_t iterations = params.iterations;
  228. // Initialize GGML, ensures float conversion tables are initialized
  229. struct ggml_init_params ggml_params = {
  230. /* .mem_size = */ 1*1024,
  231. /* .mem_buffer = */ NULL,
  232. /* .no_alloc = */ true,
  233. };
  234. struct ggml_context * ctx = ggml_init(ggml_params);
  235. for (int i = 0; i < GGML_TYPE_COUNT; i++) {
  236. ggml_type type = (ggml_type) i;
  237. ggml_type_traits_t qfns = ggml_internal_get_type_traits(type);
  238. 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()) {
  239. continue;
  240. }
  241. if (qfns.from_float && qfns.to_float) {
  242. printf("%s\n", ggml_type_name(type));
  243. if (params.op_quantize_row_q_reference) {
  244. printf(" quantize_row_q_reference\n");
  245. for (size_t size : params.test_sizes) {
  246. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  247. auto quantize_fn = [&](void ) {
  248. qfns.from_float_reference(test_data1, test_q1, size);
  249. return test_q1[0];
  250. };
  251. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  252. benchmark_function(size, quantized_size, iterations, quantize_fn);
  253. }
  254. printf("\n");
  255. }
  256. if (params.op_quantize_row_q) {
  257. printf(" quantize_row_q\n");
  258. for (size_t size : params.test_sizes) {
  259. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  260. auto quantize_fn = [&](void ) {
  261. qfns.from_float(test_data1, test_q1, size);
  262. return test_q1[0];
  263. };
  264. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  265. benchmark_function(size, quantized_size, iterations, quantize_fn);
  266. }
  267. printf("\n");
  268. }
  269. if (params.op_dequantize_row_q) {
  270. printf(" dequantize_row_q\n");
  271. qfns.from_float(test_data1, test_q1, largest);
  272. for (size_t size : params.test_sizes) {
  273. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  274. auto quantize_fn = [&](void ) {
  275. qfns.to_float(test_q1, test_out, size);
  276. return test_out[0];
  277. };
  278. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  279. benchmark_function(size, quantized_size, iterations, quantize_fn);
  280. }
  281. printf("\n");
  282. }
  283. if (params.op_quantize_row_q_dot) {
  284. printf(" quantize_row_q_dot\n");
  285. for (size_t size : params.test_sizes) {
  286. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  287. auto quantize_fn = [&](void ) {
  288. auto vdot = ggml_internal_get_type_traits(qfns.vec_dot_type);
  289. vdot.from_float(test_data1, test_q1, size);
  290. return test_q1[0];
  291. };
  292. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  293. benchmark_function(size, quantized_size, iterations, quantize_fn);
  294. }
  295. printf("\n");
  296. }
  297. if (params.op_vec_dot_q) {
  298. printf(" vec_dot_q\n");
  299. qfns.from_float(test_data1, test_q1, largest);
  300. qfns.from_float(test_data2, test_q2, largest);
  301. for (size_t size : params.test_sizes) {
  302. printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
  303. auto quantize_fn = [&](void ) {
  304. float result;
  305. qfns.vec_dot(size, &result, test_q1, test_q2);
  306. return result;
  307. };
  308. size_t quantized_size = size / ggml_blck_size(type) * ggml_type_size(type);
  309. benchmark_function(size, quantized_size, iterations, quantize_fn);
  310. }
  311. printf("\n");
  312. }
  313. }
  314. }
  315. ggml_free(ctx);
  316. return 0;
  317. }