benchmark-matmult.cpp 9.5 KB

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