benchmark-matmult.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include <locale.h>
  2. #include "ggml.h"
  3. #include "build-info.h"
  4. #include <assert.h>
  5. #include <math.h>
  6. #include <cstring>
  7. #include <cstdio>
  8. #include <cinttypes>
  9. #include <unordered_map>
  10. #include <queue>
  11. #include <string.h>
  12. #include <cassert>
  13. #include <fstream>
  14. #include <string>
  15. #include <iterator>
  16. #include <algorithm>
  17. float tensor_sum_elements(struct ggml_tensor * tensor) {
  18. float sum = 0;
  19. if (tensor->type==GGML_TYPE_F32) {
  20. for (int j = 0; j < tensor->ne[1]; j++) {
  21. for (int k = 0; k < tensor->ne[0]; k++) {
  22. sum += ((float *) tensor->data)[j*tensor->ne[0]+k];
  23. }
  24. }
  25. }
  26. return sum;
  27. }
  28. /*
  29. These are mapping to unknown
  30. GGML_TYPE_I8,
  31. GGML_TYPE_I16,
  32. GGML_TYPE_I32,
  33. GGML_TYPE_COUNT,
  34. */
  35. #define TENSOR_TYPE_AS_STR(TYPE) TYPE == GGML_TYPE_F32 ? "FP32" : TYPE == GGML_TYPE_F16 ? "FP16" : TYPE == GGML_TYPE_Q4_0 ? "Q4_0" : TYPE == GGML_TYPE_Q4_1 ? "Q4_1" : "UNKNOWN"
  36. #define TENSOR_DUMP(TENSOR) printf("%15s: type = %i (%5s) ne = %5d x %5d x %5d, nb = (%5li, %5li, %5li) - ", #TENSOR, \
  37. TENSOR->type,TENSOR_TYPE_AS_STR(TENSOR->type),\
  38. (int) TENSOR->ne[0], (int) TENSOR->ne[1], (int) TENSOR->ne[2], TENSOR->nb[0], TENSOR->nb[1], TENSOR->nb[2]); \
  39. { float sum = tensor_sum_elements(TENSOR); printf("Sum of tensor %s is %6.2f\n",#TENSOR, sum); }
  40. struct benchmark_params_struct {
  41. int32_t n_threads = 1;
  42. int32_t n_iterations = 10;
  43. };
  44. void print_usage(int /*argc*/, char ** argv, struct benchmark_params_struct params) {
  45. fprintf(stderr, "usage: %s [options]\n", argv[0]);
  46. fprintf(stderr, "\n");
  47. fprintf(stderr, "options:\n");
  48. fprintf(stderr, " -h, --help show this help message and exit\n");
  49. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  50. fprintf(stderr, " -i N, --iter N number of iterations to use during computation (default: %d)\n", params.n_iterations);
  51. fprintf(stderr, "\n");
  52. }
  53. int main(int argc, char ** argv) {
  54. struct benchmark_params_struct benchmark_params;
  55. bool invalid_param = false;
  56. std::string arg;
  57. for (int i = 1; i < argc; i++) {
  58. arg = argv[i];
  59. if (arg == "-t" || arg == "--threads") {
  60. if (++i >= argc) {
  61. invalid_param = true;
  62. break;
  63. }
  64. benchmark_params.n_threads = std::stoi(argv[i]);
  65. } else if (arg == "-i" || arg == "--iter") {
  66. if (++i >= argc) {
  67. invalid_param = true;
  68. break;
  69. }
  70. benchmark_params.n_iterations = std::stoi(argv[i]);
  71. } else if (arg == "-h" || arg == "--help") {
  72. print_usage(argc, argv, benchmark_params);
  73. exit(0);
  74. }
  75. if (invalid_param) {
  76. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  77. print_usage(argc, argv, benchmark_params);
  78. exit(1);
  79. }
  80. }
  81. fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
  82. printf("Starting Test\n");
  83. // create the ggml context
  84. struct ggml_context * ctx;
  85. //const int sizex = 4096;
  86. //const int sizey = 11008;
  87. #undef VERBOSE_DEBUGGING
  88. #ifndef VERBOSE_DEBUGGING
  89. const int sizey = 4096;
  90. const int sizex = 11008;
  91. const int sizez = 128;
  92. #else
  93. /* Working - let's increase size */
  94. const int sizey = 1;
  95. const int sizex = (8*32);
  96. const int sizez = 1;
  97. /*const int sizey = 1;
  98. const int sizex = 3*(8*32);
  99. const int sizez = 1;*/
  100. #endif
  101. //printf("Memsize required = %i\n", sizex*sizex);
  102. size_t ctx_size = 0;
  103. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32);
  104. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32);
  105. ctx_size += sizex*sizez*ggml_type_sizef(GGML_TYPE_F32);
  106. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_Q4_0);
  107. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_Q4_0);
  108. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32); // BLAS
  109. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32); // BLAS
  110. ctx_size += 1024*1024*16;
  111. printf("Allocating Memory of size %li bytes, %li MB\n",ctx_size, (ctx_size/1024/1024));
  112. struct ggml_init_params params = {
  113. /*.mem_size =*/ ctx_size,
  114. /*.mem_buffer =*/ NULL,
  115. /* no_alloc =*/ 0
  116. };
  117. ctx = ggml_init(params);
  118. if (!ctx) {
  119. fprintf(stderr, "%s: ggml_init() failed\n", __func__);
  120. return 1;
  121. }
  122. printf("Creating new tensors\n");
  123. // printf("Creating new tensor m1\n");
  124. struct ggml_tensor * m11 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
  125. ggml_set_f32(m11, 1.0f);
  126. // printf("Creating new tensor m1\n");
  127. struct ggml_tensor * m12 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
  128. ggml_set_f32(m12, 1.5f);
  129. // printf("Creating new tensor m2\n");
  130. struct ggml_tensor * m2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizez);
  131. ggml_set_f32(m2, 2.0f);
  132. printf("\n------ Test 1 - Matrix Mult via F32 code ------------------------------------------------------------------------------\n");
  133. // printf("Creating new tensor m11xm2\n");
  134. struct ggml_tensor * m11xm2 = ggml_mul_mat(ctx, m11, m2);
  135. // printf("Creating compute graph\n");
  136. struct ggml_cgraph gf = ggml_build_forward(m11xm2);
  137. gf.n_threads=benchmark_params.n_threads;
  138. printf("cgraph->n_threads=%i\n",gf.n_threads);
  139. TENSOR_DUMP(m11);
  140. TENSOR_DUMP(m2);
  141. ggml_graph_compute(ctx, &gf);
  142. TENSOR_DUMP(gf.nodes[0]);
  143. printf("\n------ Test 2 - Matrix Mult via Q4_0 code ------------------------------------------------------------------------------\n");
  144. int32_t nelements = sizex*sizey;
  145. int32_t ne[2] = { sizex, sizey };
  146. std::vector<int64_t> hist_cur(1 << 4, 0);
  147. // Set up a the benchmark matrices
  148. // printf("Creating new tensor q11 & Running quantize\n");
  149. struct ggml_tensor * q11 = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, sizex, sizey);
  150. ggml_quantize_q4_0((const float *) m11->data, q11->data, nelements, ne[0], hist_cur.data());
  151. // Set up a the compute graph
  152. // printf("Creating new tensor q31\n");
  153. struct ggml_tensor * q31 = ggml_mul_mat(ctx, q11, m2);
  154. // printf("Creating compute graph\n");
  155. struct ggml_cgraph gf31 = ggml_build_forward(q31);
  156. gf31.n_threads=benchmark_params.n_threads;
  157. // Set up a second graph computation to make sure we override the CPU cache lines
  158. // printf("Creating new tensor q12 & Running quantize\n");
  159. struct ggml_tensor * q12 = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, sizex, sizey);
  160. ggml_quantize_q4_0((const float *) m12->data, q12->data, nelements, ne[0], hist_cur.data());
  161. // printf("Creating new tensor q32\n");
  162. struct ggml_tensor * q32 = ggml_mul_mat(ctx, q12, m2);
  163. //printf("Creating compute graph\n");
  164. struct ggml_cgraph gf32 = ggml_build_forward(q32);
  165. gf32.n_threads=benchmark_params.n_threads;
  166. printf("cgraph->n_threads=%i\n",gf31.n_threads);
  167. const int dimx = sizex;
  168. const int dimy = sizey;
  169. const int dimz = sizez;
  170. long long int flops_per_dot_product = dimy + dimy;
  171. long long int flops_per_matrix = flops_per_dot_product * dimx * dimz; ;
  172. printf("Matrix Multiplication of (%i,%i,%i) x (%i,%i,%i) - about %6.2f gFLOPS\n\n", sizex, sizey, 1, sizex, sizez, 1, 1.0f*flops_per_matrix / 1000 / 1000 / 1000);
  173. // Let's use the F32 result from above as a reference for the q4_0 multiplication
  174. float sum_of_F32_reference = tensor_sum_elements(gf.nodes[0]);
  175. printf("Iteration;NThreads; SizeX; SizeY; SizeZ; Required_FLOPS; Elapsed_u_Seconds; FLOPS_per_u_Second\n");
  176. printf("==============================================================================================\n");
  177. for (int i=0;i<benchmark_params.n_iterations ;i++) {
  178. long long int start = ggml_time_us();
  179. //printf("Running ggml_graph_compute\n");
  180. ggml_graph_compute(ctx, &gf31);
  181. long long int stop = ggml_time_us();
  182. long long int usec = stop-start;
  183. float flops_per_usec = (1.0f*flops_per_matrix)/usec;
  184. printf("%9i;%8i;%6i;%6i;%6i;%15lli;%18lli;%19.2f\n",
  185. i,
  186. gf31.n_threads,
  187. sizex, sizey, sizez, flops_per_matrix,
  188. usec,flops_per_usec);
  189. #ifdef VERBOSE_DEBUGGING
  190. TENSOR_DUMP("res",gf31.nodes[0])
  191. #endif
  192. // Check that the matrix multiplication result is in the right ballpark
  193. // We cannot use the exact value from the F32 multiplication because the quantizuation will be slightly different
  194. float sum_of_Q4_result = tensor_sum_elements(gf31.nodes[0]);
  195. float delta = abs(sum_of_Q4_result - sum_of_F32_reference);
  196. float allowed_delta = (sum_of_F32_reference) / 1000 / 1000; // Let's accept an epsilon of 10^-6
  197. if (delta > allowed_delta) {
  198. printf("\nABORT - ERROR in Matrix Multiplication result - expected %6.2f, got %6.2f (delta %6.2f > allowed_delta %6.2f)\n",
  199. sum_of_F32_reference,
  200. sum_of_Q4_result,
  201. delta,
  202. allowed_delta
  203. );
  204. exit(0);
  205. }
  206. // Running a different graph computation to make sure we override the CPU cache lines
  207. ggml_graph_compute(ctx, &gf32);
  208. }
  209. }