batched-bench.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. static void print_usage(int argc, char ** argv, const gpt_params & params) {
  24. gpt_params_print_usage(argc, argv, params);
  25. LOG_TEE("\nexample usage:\n");
  26. LOG_TEE("\n %s -m model.gguf -c 2048 -b 2048 -ub 512 -npp 128,256,512 -ntg 128,256 -npl 1,2,4,8,16,32 [-pps]\n", argv[0]);
  27. LOG_TEE("\n");
  28. }
  29. int main(int argc, char ** argv) {
  30. gpt_params params;
  31. if (!gpt_params_parse(argc, argv, params)) {
  32. print_usage(argc, argv, params);
  33. return 1;
  34. }
  35. int is_pp_shared = params.is_pp_shared;
  36. std::vector<int> n_pp = params.n_pp;
  37. std::vector<int> n_tg = params.n_tg;
  38. std::vector<int> n_pl = params.n_pl;
  39. // init LLM
  40. llama_backend_init();
  41. llama_numa_init(params.numa);
  42. // initialize the model
  43. llama_model_params model_params = llama_model_params_from_gpt_params(params);
  44. llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
  45. if (model == NULL) {
  46. fprintf(stderr , "%s: error: unable to load model\n" , __func__);
  47. return 1;
  48. }
  49. llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
  50. // ensure enough sequences are available
  51. ctx_params.n_seq_max = *std::max_element(n_pl.begin(), n_pl.end());
  52. llama_context * ctx = llama_new_context_with_model(model, ctx_params);
  53. if (ctx == NULL) {
  54. fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
  55. return 1;
  56. }
  57. const int32_t n_kv_max = llama_n_ctx(ctx);
  58. llama_batch batch = llama_batch_init(n_kv_max, 0, 1);
  59. // decode in batches of ctx_params.n_batch tokens
  60. auto decode_helper = [](llama_context * ctx, llama_batch & batch, int32_t n_batch) {
  61. for (int32_t i = 0; i < (int32_t) batch.n_tokens; i += n_batch) {
  62. const int32_t n_tokens = std::min(n_batch, (int32_t) (batch.n_tokens - i));
  63. llama_batch batch_view = {
  64. n_tokens,
  65. batch.token + i,
  66. nullptr,
  67. batch.pos + i,
  68. batch.n_seq_id + i,
  69. batch.seq_id + i,
  70. batch.logits + i,
  71. 0, 0, 0, // unused
  72. };
  73. const int ret = llama_decode(ctx, batch_view);
  74. if (ret != 0) {
  75. LOG_TEE("failed to decode the batch, n_batch = %d, ret = %d\n", n_batch, ret);
  76. return false;
  77. }
  78. llama_synchronize(ctx);
  79. }
  80. return true;
  81. };
  82. // warm up
  83. {
  84. for (int i = 0; i < 16; ++i) {
  85. llama_batch_add(batch, 0, i, { 0 }, false);
  86. }
  87. if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
  88. LOG_TEE("%s: llama_decode() failed\n", __func__);
  89. return 1;
  90. }
  91. }
  92. LOG_TEE("\n");
  93. LOG_TEE("%s: n_kv_max = %d, n_batch = %d, n_ubatch = %d, flash_attn = %d, is_pp_shared = %d, n_gpu_layers = %d, n_threads = %u, n_threads_batch = %u\n", __func__, n_kv_max, params.n_batch, params.n_ubatch, params.flash_attn, params.is_pp_shared, params.n_gpu_layers, ctx_params.n_threads, ctx_params.n_threads_batch);
  94. LOG_TEE("\n");
  95. 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");
  96. LOG_TEE("|%6s-|-%6s-|-%4s-|-%6s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|\n", "------", "------", "----", "------", "--------", "--------", "--------", "--------", "--------", "--------");
  97. for ( int i_pp = 0; i_pp < (int) n_pp.size(); ++i_pp) {
  98. for ( int i_tg = 0; i_tg < (int) n_tg.size(); ++i_tg) {
  99. for (int i_pl = 0; i_pl < (int) n_pl.size(); ++i_pl) {
  100. const int pp = n_pp[i_pp];
  101. const int tg = n_tg[i_tg];
  102. const int pl = n_pl[i_pl];
  103. const int n_ctx_req = is_pp_shared ? pp + pl*tg : pl*(pp + tg);
  104. if (n_ctx_req > n_kv_max) {
  105. continue;
  106. }
  107. llama_batch_clear(batch);
  108. for (int i = 0; i < pp; ++i) {
  109. for (int j = 0; j < (is_pp_shared ? 1 : pl); ++j) {
  110. llama_batch_add(batch, 0, i, { j }, false);
  111. }
  112. }
  113. batch.logits[batch.n_tokens - 1] = true;
  114. const auto t_pp_start = ggml_time_us();
  115. llama_kv_cache_clear(ctx);
  116. if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
  117. LOG_TEE("%s: llama_decode() failed\n", __func__);
  118. return 1;
  119. }
  120. if (is_pp_shared) {
  121. for (int32_t i = 1; i < pl; ++i) {
  122. llama_kv_cache_seq_cp(ctx, 0, i, -1, -1);
  123. }
  124. }
  125. const auto t_pp_end = ggml_time_us();
  126. const auto t_tg_start = ggml_time_us();
  127. for (int i = 0; i < tg; ++i) {
  128. llama_batch_clear(batch);
  129. for (int j = 0; j < pl; ++j) {
  130. llama_batch_add(batch, 0, pp + i, { j }, true);
  131. }
  132. if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
  133. LOG_TEE("%s: llama_decode() failed\n", __func__);
  134. return 1;
  135. }
  136. }
  137. const auto t_tg_end = ggml_time_us();
  138. const int32_t n_kv = n_ctx_req;
  139. const float t_pp = (t_pp_end - t_pp_start) / 1000000.0f;
  140. const float t_tg = (t_tg_end - t_tg_start) / 1000000.0f;
  141. const float t = t_pp + t_tg;
  142. const float speed_pp = is_pp_shared ? pp / t_pp : pl*pp / t_pp;
  143. const float speed_tg = pl*tg / t_tg;
  144. const float speed = n_kv / t;
  145. 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);
  146. }
  147. }
  148. }
  149. llama_print_timings(ctx);
  150. llama_batch_free(batch);
  151. llama_free(ctx);
  152. llama_free_model(model);
  153. llama_backend_free();
  154. fprintf(stderr, "\n\n");
  155. return 0;
  156. }