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