benchmark-matmult.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #if defined(_MSC_VER)
  18. #pragma warning(disable: 4244 4267) // possible loss of data
  19. #endif
  20. float tensor_sum_elements(const ggml_tensor * tensor) {
  21. float sum = 0;
  22. if (tensor->type==GGML_TYPE_F32) {
  23. for (int j = 0; j < tensor->ne[1]; j++) {
  24. for (int k = 0; k < tensor->ne[0]; k++) {
  25. sum += ((float *) tensor->data)[j*tensor->ne[0]+k];
  26. }
  27. }
  28. }
  29. return sum;
  30. }
  31. void tensor_dump(const ggml_tensor * tensor, const char * name) {
  32. printf("%15s: type = %i (%5s) ne = %5" PRIi64 " x %5" PRIi64 " x %5" PRIi64 ", nb = (%5zi, %5zi, %5zi) - ", name,
  33. tensor->type, ggml_type_name(tensor->type),
  34. tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->nb[0], tensor->nb[1], tensor->nb[2]);
  35. float sum = tensor_sum_elements(tensor);
  36. printf("Sum of tensor %s is %6.2f\n", name, sum);
  37. }
  38. #define TENSOR_DUMP(tensor) tensor_dump(tensor, #tensor)
  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. }
  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. fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
  81. printf("Starting Test\n");
  82. // create the ggml context
  83. struct ggml_context * ctx;
  84. //const int sizex = 4096;
  85. //const int sizey = 11008;
  86. #undef VERBOSE_DEBUGGING
  87. #ifndef VERBOSE_DEBUGGING
  88. const int sizey = 4096;
  89. const int sizex = 11008;
  90. const int sizez = 128;
  91. #else
  92. /* Working - let's increase size */
  93. const int sizey = 1;
  94. const int sizex = (8*32);
  95. const int sizez = 1;
  96. /*const int sizey = 1;
  97. const int sizex = 3*(8*32);
  98. const int sizez = 1;*/
  99. #endif
  100. //printf("Memsize required = %i\n", sizex*sizex);
  101. size_t ctx_size = 0;
  102. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32);
  103. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32);
  104. ctx_size += sizex*sizez*ggml_type_sizef(GGML_TYPE_F32);
  105. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_Q4_0);
  106. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_Q4_0);
  107. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32); // BLAS
  108. ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32); // BLAS
  109. ctx_size += 1024*1024*16;
  110. printf("Allocating Memory of size %zi bytes, %zi MB\n",ctx_size, (ctx_size/1024/1024));
  111. struct ggml_init_params params = {
  112. /*.mem_size =*/ ctx_size,
  113. /*.mem_buffer =*/ NULL,
  114. /* no_alloc =*/ 0
  115. };
  116. ctx = ggml_init(params);
  117. if (!ctx) {
  118. fprintf(stderr, "%s: ggml_init() failed\n", __func__);
  119. return 1;
  120. }
  121. printf("Creating new tensors\n");
  122. // printf("Creating new tensor m1\n");
  123. struct ggml_tensor * m11 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
  124. ggml_set_f32(m11, 1.0f);
  125. // printf("Creating new tensor m1\n");
  126. struct ggml_tensor * m12 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
  127. ggml_set_f32(m12, 1.5f);
  128. // printf("Creating new tensor m2\n");
  129. struct ggml_tensor * m2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizez);
  130. ggml_set_f32(m2, 2.0f);
  131. printf("\n------ Test 1 - Matrix Mult via F32 code ------------------------------------------------------------------------------\n");
  132. // printf("Creating new tensor m11xm2\n");
  133. struct ggml_tensor * m11xm2 = ggml_mul_mat(ctx, m11, m2);
  134. // printf("Creating compute graph\n");
  135. struct ggml_cgraph gf = ggml_build_forward(m11xm2);
  136. gf.n_threads=benchmark_params.n_threads;
  137. printf("cgraph->n_threads=%i\n",gf.n_threads);
  138. TENSOR_DUMP(m11);
  139. TENSOR_DUMP(m2);
  140. ggml_graph_compute(ctx, &gf);
  141. TENSOR_DUMP(gf.nodes[0]);
  142. printf("\n------ Test 2 - Matrix Mult via Q4_0 code ------------------------------------------------------------------------------\n");
  143. int32_t nelements = sizex*sizey;
  144. int32_t ne[2] = { sizex, sizey };
  145. std::vector<int64_t> hist_cur(1 << 4, 0);
  146. // Set up a the benchmark matrices
  147. // printf("Creating new tensor q11 & Running quantize\n");
  148. struct ggml_tensor * q11 = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, sizex, sizey);
  149. ggml_quantize_q4_0((const float *) m11->data, q11->data, nelements, ne[0], hist_cur.data());
  150. // Set up a the compute graph
  151. // printf("Creating new tensor q31\n");
  152. struct ggml_tensor * q31 = ggml_mul_mat(ctx, q11, m2);
  153. // printf("Creating compute graph\n");
  154. struct ggml_cgraph gf31 = ggml_build_forward(q31);
  155. gf31.n_threads=benchmark_params.n_threads;
  156. // Set up a second graph computation to make sure we override the CPU cache lines
  157. // printf("Creating new tensor q12 & Running quantize\n");
  158. struct ggml_tensor * q12 = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, sizex, sizey);
  159. ggml_quantize_q4_0((const float *) m12->data, q12->data, nelements, ne[0], hist_cur.data());
  160. // printf("Creating new tensor q32\n");
  161. struct ggml_tensor * q32 = ggml_mul_mat(ctx, q12, m2);
  162. //printf("Creating compute graph\n");
  163. struct ggml_cgraph gf32 = ggml_build_forward(q32);
  164. gf32.n_threads=benchmark_params.n_threads;
  165. printf("cgraph->n_threads=%i\n",gf31.n_threads);
  166. const int dimx = sizex;
  167. const int dimy = sizey;
  168. const int dimz = sizez;
  169. long long int flops_per_dot_product = dimy + dimy;
  170. long long int flops_per_matrix = flops_per_dot_product * dimx * dimz; ;
  171. 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);
  172. // Let's use the F32 result from above as a reference for the q4_0 multiplication
  173. float sum_of_F32_reference = tensor_sum_elements(gf.nodes[0]);
  174. printf("Iteration;NThreads; SizeX; SizeY; SizeZ; Required_FLOPS; Elapsed_u_Seconds; gigaFLOPS\n");
  175. printf("=====================================================================================\n");
  176. double gflops_sum = 0;
  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. double gflops = (double)(flops_per_matrix)/usec/1000.0;
  184. gflops_sum += gflops;
  185. printf("%9i;%8i;%6i;%6i;%6i;%15lli;%18lli;%10.2f\n",
  186. i,
  187. gf31.n_threads,
  188. sizex, sizey, sizez, flops_per_matrix,
  189. usec,gflops);
  190. #ifdef VERBOSE_DEBUGGING
  191. TENSOR_DUMP("res",gf31.nodes[0])
  192. #endif
  193. // Check that the matrix multiplication result is in the right ballpark
  194. // We cannot use the exact value from the F32 multiplication because the quantizuation will be slightly different
  195. float sum_of_Q4_result = tensor_sum_elements(gf31.nodes[0]);
  196. float delta = abs(sum_of_Q4_result - sum_of_F32_reference);
  197. float allowed_delta = (sum_of_F32_reference) / 1000 / 1000; // Let's accept an epsilon of 10^-6
  198. if (delta > allowed_delta) {
  199. printf("\nABORT - ERROR in Matrix Multiplication result - expected %6.2f, got %6.2f (delta %6.2f > allowed_delta %6.2f)\n",
  200. sum_of_F32_reference,
  201. sum_of_Q4_result,
  202. delta,
  203. allowed_delta
  204. );
  205. exit(0);
  206. }
  207. // Running a different graph computation to make sure we override the CPU cache lines
  208. ggml_graph_compute(ctx, &gf32);
  209. }
  210. printf("\n");
  211. printf("Average%78.2f\n",gflops_sum/((double)benchmark_params.n_iterations));
  212. printf("=====================================================================================\n");
  213. }