batched-bench.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "arg.h"
  2. #include "common.h"
  3. #include "log.h"
  4. #include "llama.h"
  5. #include <algorithm>
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. static void print_usage(int, char ** argv) {
  10. LOG("\nexample usage:\n");
  11. LOG("\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]);
  12. LOG("\n");
  13. }
  14. int main(int argc, char ** argv) {
  15. common_params params;
  16. if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_BENCH, print_usage)) {
  17. return 1;
  18. }
  19. common_init();
  20. int is_pp_shared = params.is_pp_shared;
  21. int is_tg_separate = params.is_tg_separate;
  22. std::vector<int> n_pp = params.n_pp;
  23. std::vector<int> n_tg = params.n_tg;
  24. std::vector<int> n_pl = params.n_pl;
  25. // init LLM
  26. llama_backend_init();
  27. llama_numa_init(params.numa);
  28. // initialize the model
  29. llama_model_params model_params = common_model_params_to_llama(params);
  30. llama_model * model = llama_model_load_from_file(params.model.path.c_str(), model_params);
  31. if (model == NULL) {
  32. fprintf(stderr , "%s: error: unable to load model\n" , __func__);
  33. return 1;
  34. }
  35. llama_context_params ctx_params = common_context_params_to_llama(params);
  36. // ensure enough sequences are available
  37. ctx_params.n_seq_max = n_pl.empty() ? 1 : *std::max_element(n_pl.begin(), n_pl.end());
  38. llama_context * ctx = llama_init_from_model(model, ctx_params);
  39. if (ctx == NULL) {
  40. fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
  41. llama_model_free(model);
  42. return 1;
  43. }
  44. const llama_vocab * vocab = llama_model_get_vocab(model);
  45. const int32_t n_vocab = llama_vocab_n_tokens(vocab);
  46. const auto get_token_rand = [n_vocab]() -> llama_token {
  47. return std::rand() % n_vocab;
  48. };
  49. auto * mem = llama_get_memory(ctx);
  50. const int32_t n_kv_max = llama_n_ctx(ctx);
  51. llama_batch batch = llama_batch_init(n_kv_max, 0, 1);
  52. // decode in batches of ctx_params.n_batch tokens
  53. auto decode_helper = [](llama_context * ctx, llama_batch & batch, int32_t n_batch, bool synchronize) {
  54. for (int32_t i = 0; i < batch.n_tokens; i += n_batch) {
  55. const int32_t n_tokens = std::min(n_batch, batch.n_tokens - i);
  56. llama_batch batch_view = {
  57. n_tokens,
  58. batch.token + i,
  59. nullptr,
  60. batch.pos + i,
  61. batch.n_seq_id + i,
  62. batch.seq_id + i,
  63. batch.logits + i,
  64. };
  65. const int ret = llama_decode(ctx, batch_view);
  66. if (ret != 0) {
  67. LOG_ERR("failed to decode the batch, n_batch = %d, ret = %d\n", n_batch, ret);
  68. return false;
  69. }
  70. if (synchronize) {
  71. llama_synchronize(ctx);
  72. }
  73. }
  74. return true;
  75. };
  76. // warm up
  77. {
  78. for (int i = 0; i < 16; ++i) {
  79. common_batch_add(batch, get_token_rand(), i, { 0 }, false);
  80. }
  81. if (!decode_helper(ctx, batch, ctx_params.n_batch, true)) {
  82. LOG_ERR("%s: llama_decode() failed\n", __func__);
  83. llama_free(ctx);
  84. llama_model_free(model);
  85. return 1;
  86. }
  87. }
  88. if (!params.batched_bench_output_jsonl) {
  89. LOG("\n");
  90. LOG("%s: n_kv_max = %d, n_batch = %d, n_ubatch = %d, flash_attn = %d, is_pp_shared = %d, is_tg_separate = %d, n_gpu_layers = %d, n_threads = %u, n_threads_batch = %u\n", __func__, n_kv_max, params.n_batch, params.n_ubatch, int(params.flash_attn_type), is_pp_shared, is_tg_separate, params.n_gpu_layers, ctx_params.n_threads, ctx_params.n_threads_batch);
  91. LOG("\n");
  92. LOG("|%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");
  93. LOG("|%6s-|-%6s-|-%4s-|-%6s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|\n", "------", "------", "----", "------", "--------", "--------", "--------", "--------", "--------", "--------");
  94. }
  95. for ( int i_pp = 0; i_pp < (int) n_pp.size(); ++i_pp) {
  96. for ( int i_tg = 0; i_tg < (int) n_tg.size(); ++i_tg) {
  97. for (int i_pl = 0; i_pl < (int) n_pl.size(); ++i_pl) {
  98. const int pp = n_pp[i_pp];
  99. const int tg = n_tg[i_tg];
  100. const int pl = n_pl[i_pl];
  101. const int n_ctx_req = is_pp_shared ? (params.kv_unified ? pp : pl*pp) + pl*tg : pl*(pp + tg);
  102. if (n_ctx_req > n_kv_max) {
  103. continue;
  104. }
  105. common_batch_clear(batch);
  106. for (int j = 0; j < (is_pp_shared ? 1 : pl); ++j) {
  107. for (int i = 0; i < pp; ++i) {
  108. common_batch_add(batch, get_token_rand(), i, { j }, i == pp - 1);
  109. }
  110. }
  111. llama_memory_clear(mem, false);
  112. const auto t_pp_start = ggml_time_us();
  113. if (!decode_helper(ctx, batch, ctx_params.n_batch, false)) {
  114. LOG_ERR("%s: llama_decode() failed\n", __func__);
  115. llama_free(ctx);
  116. llama_model_free(model);
  117. return 1;
  118. }
  119. llama_synchronize(ctx);
  120. const auto t_pp_end = ggml_time_us();
  121. if (is_pp_shared) {
  122. for (int32_t i = 1; i < pl; ++i) {
  123. llama_memory_seq_cp(mem, 0, i, -1, -1);
  124. }
  125. if (!params.kv_unified) {
  126. // run one dummy token to apply the memory copy
  127. common_batch_clear(batch);
  128. common_batch_add(batch, get_token_rand(), pp + 0, { 0 }, true);
  129. if (!decode_helper(ctx, batch, ctx_params.n_batch, true)) {
  130. LOG_ERR("%s: llama_decode() failed\n", __func__);
  131. llama_free(ctx);
  132. llama_model_free(model);
  133. return 1;
  134. }
  135. llama_memory_seq_rm(mem, 0, pp, -1);
  136. }
  137. }
  138. const auto t_tg_start = ggml_time_us();
  139. if (is_tg_separate) {
  140. // decode pattern:
  141. // 0 0 0 ... 1 1 1 ... 2 2 2 ... 3 3 3 ...
  142. for (int j = 0; j < pl; ++j) {
  143. for (int i = 0; i < tg; ++i) {
  144. common_batch_clear(batch);
  145. common_batch_add(batch, get_token_rand(), pp + i, { j }, true);
  146. if (!decode_helper(ctx, batch, ctx_params.n_batch, true)) {
  147. LOG_ERR("%s: llama_decode() failed\n", __func__);
  148. llama_free(ctx);
  149. llama_model_free(model);
  150. return 1;
  151. }
  152. }
  153. }
  154. } else {
  155. // decode pattern:
  156. // 0123 0123 0123 ...
  157. for (int i = 0; i < tg; ++i) {
  158. common_batch_clear(batch);
  159. for (int j = 0; j < pl; ++j) {
  160. common_batch_add(batch, get_token_rand(), pp + i, { j }, true);
  161. }
  162. if (!decode_helper(ctx, batch, ctx_params.n_batch, true)) {
  163. LOG_ERR("%s: llama_decode() failed\n", __func__);
  164. llama_free(ctx);
  165. llama_model_free(model);
  166. return 1;
  167. }
  168. }
  169. }
  170. const auto t_tg_end = ggml_time_us();
  171. const int32_t n_kv = n_ctx_req;
  172. const float t_pp = (t_pp_end - t_pp_start) / 1000000.0f;
  173. const float t_tg = (t_tg_end - t_tg_start) / 1000000.0f;
  174. const float t = t_pp + t_tg;
  175. const float speed_pp = is_pp_shared ? pp / t_pp : pl*pp / t_pp;
  176. const float speed_tg = pl*tg / t_tg;
  177. const float speed = ((is_pp_shared ? pp : pl*pp) + pl*tg) / t;
  178. if(params.batched_bench_output_jsonl) {
  179. LOG(
  180. "{\"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, "
  181. "\"pp\": %d, \"tg\": %d, \"pl\": %d, \"n_kv\": %d, \"t_pp\": %f, \"speed_pp\": %f, \"t_tg\": %f, \"speed_tg\": %f, \"t\": %f, \"speed\": %f}\n",
  182. n_kv_max, params.n_batch, params.n_ubatch, int(params.flash_attn_type), params.is_pp_shared, params.n_gpu_layers, ctx_params.n_threads, ctx_params.n_threads_batch,
  183. pp, tg, pl, n_kv, t_pp, speed_pp, t_tg, speed_tg, t, speed
  184. );
  185. } else {
  186. LOG("|%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);
  187. }
  188. }
  189. }
  190. }
  191. LOG("\n");
  192. llama_perf_context_print(ctx);
  193. llama_batch_free(batch);
  194. llama_free(ctx);
  195. llama_model_free(model);
  196. llama_backend_free();
  197. return 0;
  198. }