benchmark-matmult.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "ggml.h"
  2. #include "build-info.h"
  3. #include <locale.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(const 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. void tensor_dump(const ggml_tensor * tensor, const char * name) {
  29. printf("%15s: type = %i (%5s) ne = %5d x %5d x %5d, nb = (%5li, %5li, %5li) - ", name,
  30. tensor->type, ggml_type_name(tensor->type),
  31. (int) tensor->ne[0], (int) tensor->ne[1], (int) tensor->ne[2], tensor->nb[0], tensor->nb[1], tensor->nb[2]);
  32. float sum = tensor_sum_elements(tensor);
  33. printf("Sum of tensor %s is %6.2f\n", name, sum);
  34. }
  35. #define TENSOR_DUMP(tensor) tensor_dump(tensor, #tensor)
  36. struct benchmark_params_struct {
  37. int32_t n_threads = 1;
  38. int32_t n_iterations = 10;
  39. };
  40. void print_usage(int /*argc*/, char ** argv, struct benchmark_params_struct params) {
  41. fprintf(stderr, "usage: %s [options]\n", argv[0]);
  42. fprintf(stderr, "\n");
  43. fprintf(stderr, "options:\n");
  44. fprintf(stderr, " -h, --help show this help message and exit\n");
  45. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  46. fprintf(stderr, " -i N, --iter N number of iterations to use during computation (default: %d)\n", params.n_iterations);
  47. fprintf(stderr, "\n");
  48. }
  49. int main(int argc, char ** argv) {
  50. struct benchmark_params_struct benchmark_params;
  51. bool invalid_param = false;
  52. std::string arg;
  53. for (int i = 1; i < argc; i++) {
  54. arg = argv[i];
  55. if (arg == "-t" || arg == "--threads") {
  56. if (++i >= argc) {
  57. invalid_param = true;
  58. break;
  59. }
  60. benchmark_params.n_threads = std::stoi(argv[i]);
  61. } else if (arg == "-i" || arg == "--iter") {
  62. if (++i >= argc) {
  63. invalid_param = true;
  64. break;
  65. }
  66. benchmark_params.n_iterations = std::stoi(argv[i]);
  67. } else if (arg == "-h" || arg == "--help") {
  68. print_usage(argc, argv, benchmark_params);
  69. exit(0);
  70. }
  71. }
  72. if (invalid_param) {
  73. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  74. print_usage(argc, argv, benchmark_params);
  75. exit(1);
  76. }
  77. fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
  78. printf("Starting Test\n");
  79. // create the ggml context
  80. struct ggml_context * ctx;
  81. //const int sizex = 4096;
  82. //const int sizey = 11008;
  83. #undef VERBOSE_DEBUGGING
  84. #ifndef VERBOSE_DEBUGGING
  85. const int sizey = 4096;
  86. const int sizex = 11008;
  87. const int sizez = 128;
  88. #else
  89. /* Working - let's increase size */
  90. const int sizey = 1;
  91. const int sizex = (8*32);
  92. const int sizez = 1;
  93. /*const int sizey = 1;
  94. const int sizex = 3*(8*32);
  95. const int sizez = 1;*/
  96. #endif
  97. //printf("Memsize required = %i\n", sizex*sizex);
  98. size_t ctx_size = 0;
  99. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32);
  100. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32);
  101. ctx_size += sizex*sizez*ggml_type_sizef(GGML_TYPE_F32);
  102. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_Q4_0);
  103. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_Q4_0);
  104. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32); // BLAS
  105. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32); // BLAS
  106. ctx_size += 1024*1024*16;
  107. printf("Allocating Memory of size %li bytes, %li MB\n",ctx_size, (ctx_size/1024/1024));
  108. struct ggml_init_params params = {
  109. /*.mem_size =*/ ctx_size,
  110. /*.mem_buffer =*/ NULL,
  111. /* no_alloc =*/ 0
  112. };
  113. ctx = ggml_init(params);
  114. if (!ctx) {
  115. fprintf(stderr, "%s: ggml_init() failed\n", __func__);
  116. return 1;
  117. }
  118. printf("Creating new tensors\n");
  119. // printf("Creating new tensor m1\n");
  120. struct ggml_tensor * m11 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
  121. ggml_set_f32(m11, 1.0f);
  122. // printf("Creating new tensor m1\n");
  123. struct ggml_tensor * m12 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
  124. ggml_set_f32(m12, 1.5f);
  125. // printf("Creating new tensor m2\n");
  126. struct ggml_tensor * m2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizez);
  127. ggml_set_f32(m2, 2.0f);
  128. printf("\n------ Test 1 - Matrix Mult via F32 code ------------------------------------------------------------------------------\n");
  129. // printf("Creating new tensor m11xm2\n");
  130. struct ggml_tensor * m11xm2 = ggml_mul_mat(ctx, m11, m2);
  131. // printf("Creating compute graph\n");
  132. struct ggml_cgraph gf = ggml_build_forward(m11xm2);
  133. gf.n_threads=benchmark_params.n_threads;
  134. printf("cgraph->n_threads=%i\n",gf.n_threads);
  135. TENSOR_DUMP(m11);
  136. TENSOR_DUMP(m2);
  137. ggml_graph_compute(ctx, &gf);
  138. TENSOR_DUMP(gf.nodes[0]);
  139. printf("\n------ Test 2 - Matrix Mult via Q4_0 code ------------------------------------------------------------------------------\n");
  140. int32_t nelements = sizex*sizey;
  141. int32_t ne[2] = { sizex, sizey };
  142. std::vector<int64_t> hist_cur(1 << 4, 0);
  143. // Set up a the benchmark matrices
  144. // printf("Creating new tensor q11 & Running quantize\n");
  145. struct ggml_tensor * q11 = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, sizex, sizey);
  146. ggml_quantize_q4_0((const float *) m11->data, q11->data, nelements, ne[0], hist_cur.data());
  147. // Set up a the compute graph
  148. // printf("Creating new tensor q31\n");
  149. struct ggml_tensor * q31 = ggml_mul_mat(ctx, q11, m2);
  150. // printf("Creating compute graph\n");
  151. struct ggml_cgraph gf31 = ggml_build_forward(q31);
  152. gf31.n_threads=benchmark_params.n_threads;
  153. // Set up a second graph computation to make sure we override the CPU cache lines
  154. // printf("Creating new tensor q12 & Running quantize\n");
  155. struct ggml_tensor * q12 = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, sizex, sizey);
  156. ggml_quantize_q4_0((const float *) m12->data, q12->data, nelements, ne[0], hist_cur.data());
  157. // printf("Creating new tensor q32\n");
  158. struct ggml_tensor * q32 = ggml_mul_mat(ctx, q12, m2);
  159. //printf("Creating compute graph\n");
  160. struct ggml_cgraph gf32 = ggml_build_forward(q32);
  161. gf32.n_threads=benchmark_params.n_threads;
  162. printf("cgraph->n_threads=%i\n",gf31.n_threads);
  163. const int dimx = sizex;
  164. const int dimy = sizey;
  165. const int dimz = sizez;
  166. long long int flops_per_dot_product = dimy + dimy;
  167. long long int flops_per_matrix = flops_per_dot_product * dimx * dimz; ;
  168. 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);
  169. // Let's use the F32 result from above as a reference for the q4_0 multiplication
  170. float sum_of_F32_reference = tensor_sum_elements(gf.nodes[0]);
  171. printf("Iteration;NThreads; SizeX; SizeY; SizeZ; Required_FLOPS; Elapsed_u_Seconds; gigaFLOPS\n");
  172. printf("=====================================================================================\n");
  173. double gflops_sum = 0;
  174. for (int i=0;i<benchmark_params.n_iterations ;i++) {
  175. long long int start = ggml_time_us();
  176. //printf("Running ggml_graph_compute\n");
  177. ggml_graph_compute(ctx, &gf31);
  178. long long int stop = ggml_time_us();
  179. long long int usec = stop-start;
  180. double gflops = (double)(flops_per_matrix)/usec/1000.0;
  181. gflops_sum += gflops;
  182. printf("%9i;%8i;%6i;%6i;%6i;%15lli;%18lli;%10.2f\n",
  183. i,
  184. gf31.n_threads,
  185. sizex, sizey, sizez, flops_per_matrix,
  186. usec,gflops);
  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. printf("\n");
  208. printf("Average%78.2f\n",gflops_sum/((double)benchmark_params.n_iterations));
  209. printf("=====================================================================================\n");
  210. }