batched-bench.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "common.h"
  2. #include "llama.h"
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <string>
  7. #include <vector>
  8. // mutates the input string
  9. static std::vector<int> parse_list(char * p) {
  10. std::vector<int> ret;
  11. char * q = p;
  12. while (*p) {
  13. if (*p == ',') {
  14. *p = '\0';
  15. ret.push_back(std::atoi(q));
  16. q = p + 1;
  17. }
  18. ++p;
  19. }
  20. ret.push_back(std::atoi(q));
  21. return ret;
  22. }
  23. int main(int argc, char ** argv) {
  24. gpt_params params;
  25. if (argc == 1 || argv[1][0] == '-') {
  26. printf("usage: %s MODEL_PATH [N_KV_MAX] [IS_PP_SHARED] [NGL] [MMQ] <PP> <TG> <PL>\n" , argv[0]);
  27. printf(" <PP>, <TG> and PL are comma-separated lists of numbers without spaces\n\n");
  28. printf(" example: %s ggml-model-f16.gguf 2048 0 999 0 128,256,512 128,256 1,2,4,8,16,32\n\n", argv[0]);
  29. return 1 ;
  30. }
  31. int n_kv_max = 2048;
  32. int is_pp_shared = 0;
  33. int n_gpu_layers = 0;
  34. int mmq = 0;
  35. std::vector<int> n_pp = { 128, 256, 512, 1024, 2048, 3584, 7680, };
  36. std::vector<int> n_tg = { 128, 256, };
  37. std::vector<int> n_pl = { 1, 2, 4, 8, 16, 32, };
  38. //std::vector<int> n_pl = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, };
  39. if (argc >= 2) {
  40. params.model = argv[1];
  41. }
  42. if (argc >= 3) {
  43. n_kv_max = std::atoi(argv[2]);
  44. }
  45. if (argc >= 4) {
  46. is_pp_shared = std::atoi(argv[3]);
  47. }
  48. if (argc >= 5) {
  49. n_gpu_layers = std::atoi(argv[4]);
  50. }
  51. if (argc >= 6) {
  52. mmq = std::atoi(argv[5]);
  53. }
  54. if (argc >= 7) {
  55. n_pp = parse_list(argv[6]);
  56. }
  57. if (argc >= 8) {
  58. n_tg = parse_list(argv[7]);
  59. }
  60. if (argc >= 9) {
  61. n_pl = parse_list(argv[8]);
  62. }
  63. // init LLM
  64. llama_backend_init(params.numa);
  65. // initialize the model
  66. llama_model_params model_params = llama_model_default_params();
  67. model_params.n_gpu_layers = n_gpu_layers;
  68. llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
  69. if (model == NULL) {
  70. fprintf(stderr , "%s: error: unable to load model\n" , __func__);
  71. return 1;
  72. }
  73. llama_context_params ctx_params = llama_context_default_params();
  74. ctx_params.seed = 1234;
  75. ctx_params.n_ctx = n_kv_max;
  76. ctx_params.n_batch = 512;
  77. ctx_params.mul_mat_q = mmq;
  78. ctx_params.n_threads = params.n_threads;
  79. ctx_params.n_threads_batch = params.n_threads_batch == -1 ? params.n_threads : params.n_threads_batch;
  80. llama_context * ctx = llama_new_context_with_model(model, ctx_params);
  81. if (ctx == NULL) {
  82. fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
  83. return 1;
  84. }
  85. llama_batch batch = llama_batch_init(n_kv_max, 0, 1);
  86. // decode in batches of ctx_params.n_batch tokens
  87. auto decode_helper = [](llama_context * ctx, llama_batch & batch, int32_t n_batch) {
  88. for (int32_t i = 0; i < (int32_t) batch.n_tokens; i += n_batch) {
  89. const int32_t n_tokens = std::min(n_batch, (int32_t) (batch.n_tokens - i));
  90. llama_batch batch_view = {
  91. n_tokens,
  92. batch.token + i,
  93. nullptr,
  94. batch.pos + i,
  95. batch.n_seq_id + i,
  96. batch.seq_id + i,
  97. batch.logits + i,
  98. 0, 0, 0, // unused
  99. };
  100. const int ret = llama_decode(ctx, batch_view);
  101. if (ret != 0) {
  102. LOG_TEE("failed to decode the batch, n_batch = %d, ret = %d\n", n_batch, ret);
  103. return false;
  104. }
  105. }
  106. return true;
  107. };
  108. // warm up
  109. {
  110. for (int i = 0; i < 16; ++i) {
  111. llama_batch_add(batch, 0, i, { 0 }, false);
  112. }
  113. if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
  114. LOG_TEE("%s: llama_decode() failed\n", __func__);
  115. return 1;
  116. }
  117. }
  118. LOG_TEE("\n");
  119. LOG_TEE("%s: n_kv_max = %d, is_pp_shared = %d, n_gpu_layers = %d, mmq = %d, n_threads = %d, n_threads_batch = %d\n", __func__, n_kv_max, is_pp_shared, n_gpu_layers, mmq, ctx_params.n_threads, ctx_params.n_threads_batch);
  120. LOG_TEE("\n");
  121. LOG_TEE("|%6s | %6s | %4s | %6s | %8s | %8s | %8s | %8s | %8s | %8s |\n", "PP", "TG", "B", "N_KV", "T_PP s", "S_PP t/s", "T_TG s", "S_TG t/s", "T s", "S t/s");
  122. LOG_TEE("|%6s-|-%6s-|-%4s-|-%6s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|\n", "------", "------", "----", "------", "--------", "--------", "--------", "--------", "--------", "--------");
  123. for ( int i_pp = 0; i_pp < (int) n_pp.size(); ++i_pp) {
  124. for ( int i_tg = 0; i_tg < (int) n_tg.size(); ++i_tg) {
  125. for (int i_pl = 0; i_pl < (int) n_pl.size(); ++i_pl) {
  126. const int pp = n_pp[i_pp];
  127. const int tg = n_tg[i_tg];
  128. const int pl = n_pl[i_pl];
  129. const int n_ctx_req = is_pp_shared ? pp + pl*tg : pl*(pp + tg);
  130. if (n_ctx_req > n_kv_max) {
  131. continue;
  132. }
  133. llama_batch_clear(batch);
  134. const int n_tokens = is_pp_shared ? pp : pl*pp;
  135. for (int i = 0; i < n_tokens; ++i) {
  136. llama_batch_add(batch, 0, i, { 0 }, false);
  137. }
  138. batch.logits[batch.n_tokens - 1] = true;
  139. const auto t_pp_start = ggml_time_us();
  140. llama_kv_cache_clear(ctx);
  141. if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
  142. LOG_TEE("%s: llama_decode() failed\n", __func__);
  143. return 1;
  144. }
  145. if (is_pp_shared) {
  146. for (int32_t i = 1; i < pl; ++i) {
  147. llama_kv_cache_seq_cp(ctx, 0, i, 0, pp);
  148. }
  149. }
  150. const auto t_pp_end = ggml_time_us();
  151. const auto t_tg_start = ggml_time_us();
  152. for (int i = 0; i < tg; ++i) {
  153. llama_batch_clear(batch);
  154. for (int j = 0; j < pl; ++j) {
  155. llama_batch_add(batch, 0, pp + i, { j }, true);
  156. }
  157. if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
  158. LOG_TEE("%s: llama_decode() failed\n", __func__);
  159. return 1;
  160. }
  161. }
  162. const auto t_tg_end = ggml_time_us();
  163. const int32_t n_kv = n_ctx_req;
  164. const float t_pp = (t_pp_end - t_pp_start) / 1000000.0f;
  165. const float t_tg = (t_tg_end - t_tg_start) / 1000000.0f;
  166. const float t = t_pp + t_tg;
  167. const float speed_pp = is_pp_shared ? pp / t_pp : pl*pp / t_pp;
  168. const float speed_tg = pl*tg / t_tg;
  169. const float speed = n_kv / t;
  170. LOG_TEE("|%6d | %6d | %4d | %6d | %8.3f | %8.2f | %8.3f | %8.2f | %8.3f | %8.2f |\n", pp, tg, pl, n_kv, t_pp, speed_pp, t_tg, speed_tg, t, speed);
  171. }
  172. }
  173. }
  174. llama_print_timings(ctx);
  175. llama_batch_free(batch);
  176. llama_free(ctx);
  177. llama_free_model(model);
  178. llama_backend_free();
  179. fprintf(stderr, "\n\n");
  180. return 0;
  181. }