benchmark-matmult.cpp 9.0 KB

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