|
| 1 | +#include "arg.h" |
| 2 | +#include "common.h" |
| 3 | +#include "log.h" |
| 4 | +#include "llama.h" |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include <cstdlib> |
| 8 | +#include <cstdio> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +static void print_usage(int, char ** argv) { |
| 13 | + LOG("\nexample usage:\n"); |
| 14 | + LOG("\n %s -m model.gguf -c 8192 -b 2048 -ub 512\n", argv[0]); |
| 15 | + LOG("\n"); |
| 16 | +} |
| 17 | + |
| 18 | +int main(int argc, char ** argv) { |
| 19 | + common_params params; |
| 20 | + |
| 21 | + if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_BENCH, print_usage)) { |
| 22 | + return 1; |
| 23 | + } |
| 24 | + |
| 25 | + common_init(); |
| 26 | + |
| 27 | + // init LLM |
| 28 | + |
| 29 | + llama_backend_init(); |
| 30 | + llama_numa_init(params.numa); |
| 31 | + |
| 32 | + // initialize the model |
| 33 | + |
| 34 | + llama_model_params model_params = common_model_params_to_llama(params); |
| 35 | + |
| 36 | + llama_model * model = llama_model_load_from_file(params.model.c_str(), model_params); |
| 37 | + |
| 38 | + if (model == NULL) { |
| 39 | + fprintf(stderr , "%s: error: unable to load model\n" , __func__); |
| 40 | + return 1; |
| 41 | + } |
| 42 | + |
| 43 | + llama_context_params ctx_params = common_context_params_to_llama(params); |
| 44 | + |
| 45 | + llama_context * ctx = llama_init_from_model(model, ctx_params); |
| 46 | + |
| 47 | + if (ctx == NULL) { |
| 48 | + fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__); |
| 49 | + return 1; |
| 50 | + } |
| 51 | + |
| 52 | + const unsigned int n_kv_max = llama_n_ctx(ctx); |
| 53 | + const llama_vocab * vocab = llama_model_get_vocab(model); |
| 54 | + const unsigned int n_vocab = llama_vocab_n_tokens(vocab); |
| 55 | + const llama_token bos = llama_vocab_bos(vocab); |
| 56 | + const llama_token eos = llama_vocab_eos(vocab); |
| 57 | + |
| 58 | + // decode in batches of ctx_params.n_batch tokens |
| 59 | + auto decode_helper = [](llama_context * ctx, llama_batch & batch, int32_t n_batch) { |
| 60 | + for (int32_t i = 0; i < (int32_t) batch.n_tokens; i += n_batch) { |
| 61 | + const int32_t n_tokens = std::min(n_batch, (int32_t) (batch.n_tokens - i)); |
| 62 | + |
| 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 | + }; |
| 72 | + |
| 73 | + const int ret = llama_decode(ctx, batch_view); |
| 74 | + if (ret != 0) { |
| 75 | + LOG_ERR("failed to decode the batch, n_batch = %d, ret = %d\n", n_batch, ret); |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + llama_synchronize(ctx); |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | + }; |
| 84 | + |
| 85 | + const unsigned int pp = params.n_ubatch; |
| 86 | + const unsigned int tg = params.n_ubatch / 4; |
| 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, 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.n_gpu_layers, ctx_params.n_threads, ctx_params.n_threads_batch); |
| 91 | + LOG("\n"); |
| 92 | + LOG("|%6s | %6s | %6s | %8s | %8s | %8s | %8s |\n", "PP", "TG", "N_KV", "T_PP s", "S_PP t/s", "T_TG s", "S_TG t/s"); |
| 93 | + LOG("|%6s-|-%6s-|-%6s-|-%8s-|-%8s-|-%8s-|-%8s-|\n", "------", "------", "------", "--------", "--------", "--------", "--------"); |
| 94 | + } |
| 95 | + |
| 96 | + llama_batch batch = llama_batch_init(n_kv_max, 0, 1); |
| 97 | + |
| 98 | + // warm up |
| 99 | + { |
| 100 | + common_batch_add(batch, bos, 0, { 0 }, false); |
| 101 | + common_batch_add(batch, eos, 1, { 0 }, false); |
| 102 | + |
| 103 | + if (!decode_helper(ctx, batch, ctx_params.n_batch)) { |
| 104 | + LOG_ERR("%s: llama_decode() failed\n", __func__); |
| 105 | + return 1; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + common_batch_clear(batch); |
| 110 | + llama_kv_cache_clear(ctx); |
| 111 | + |
| 112 | + for (unsigned int n_kv = 0; n_kv < n_kv_max; n_kv += params.n_ubatch) { |
| 113 | + // clean up KV cache before generation |
| 114 | + llama_kv_cache_seq_rm(ctx, 0, n_kv, -1); |
| 115 | + |
| 116 | + // first measure token generation performance at this context size |
| 117 | + const auto t_tg_start = ggml_time_us(); |
| 118 | + |
| 119 | + for (unsigned int i = 0; i < tg; ++i) { |
| 120 | + common_batch_clear(batch); |
| 121 | + common_batch_add(batch, std::rand() % n_vocab, n_kv + i, { 0 }, true); |
| 122 | + |
| 123 | + if (!decode_helper(ctx, batch, ctx_params.n_batch)) { |
| 124 | + LOG_ERR("%s: llama_decode() failed\n", __func__); |
| 125 | + return 1; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + const auto t_tg_end = ggml_time_us(); |
| 130 | + |
| 131 | + // clean up KV cache after generation |
| 132 | + llama_kv_cache_seq_rm(ctx, 0, n_kv, -1); |
| 133 | + |
| 134 | + // prepare batch of pp size for prompt processing performance measurement |
| 135 | + common_batch_clear(batch); |
| 136 | + |
| 137 | + for (unsigned int i = 0; i < pp; ++i) { |
| 138 | + common_batch_add(batch, std::rand() % n_vocab, n_kv + i, { 0 }, false); |
| 139 | + } |
| 140 | + batch.logits[batch.n_tokens - 1] = true; |
| 141 | + |
| 142 | + // measure prompt processing performance |
| 143 | + const auto t_pp_start = ggml_time_us(); |
| 144 | + |
| 145 | + if (!decode_helper(ctx, batch, ctx_params.n_batch)) { |
| 146 | + LOG_ERR("%s: llama_decode() failed\n", __func__); |
| 147 | + return 1; |
| 148 | + } |
| 149 | + |
| 150 | + const auto t_pp_end = ggml_time_us(); |
| 151 | + |
| 152 | + // calculate and print metrics |
| 153 | + const float t_pp = (t_pp_end - t_pp_start) / 1000000.0f; |
| 154 | + const float t_tg = (t_tg_end - t_tg_start) / 1000000.0f; |
| 155 | + |
| 156 | + const float speed_pp = pp / t_pp; |
| 157 | + const float speed_tg = tg / t_tg; |
| 158 | + |
| 159 | + if(params.batched_bench_output_jsonl) { |
| 160 | + LOG( |
| 161 | + "{\"n_kv_max\": %d, \"n_batch\": %d, \"n_ubatch\": %d, \"flash_attn\": %d, \"n_gpu_layers\": %d, \"n_threads\": %u, \"n_threads_batch\": %u, " |
| 162 | + "\"pp\": %d, \"tg\": %d, \"n_kv\": %d, \"t_pp\": %f, \"speed_pp\": %f, \"t_tg\": %f, \"speed_tg\": %f }\n", |
| 163 | + n_kv_max, params.n_batch, params.n_ubatch, params.flash_attn, params.n_gpu_layers, ctx_params.n_threads, ctx_params.n_threads_batch, |
| 164 | + pp, tg, n_kv, t_pp, speed_pp, t_tg, speed_tg |
| 165 | + ); |
| 166 | + } else { |
| 167 | + LOG("|%6d | %6d | %6d | %8.3f | %8.2f | %8.3f | %8.2f |\n", pp, tg, n_kv, t_pp, speed_pp, t_tg, speed_tg); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + llama_perf_context_print(ctx); |
| 172 | + |
| 173 | + llama_batch_free(batch); |
| 174 | + |
| 175 | + llama_free(ctx); |
| 176 | + llama_model_free(model); |
| 177 | + |
| 178 | + llama_backend_free(); |
| 179 | + |
| 180 | + return 0; |
| 181 | +} |
0 commit comments