Team Ai
Datasetpublic

echodict/llama.cpp

version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes479downloads
batched-bench.cpp260 linesDownload Raw Back to batched-bench
1#include "arg.h"2#include "common.h"3#include "log.h"4#include "llama.h"5 6#include <algorithm>7#include <clocale>8#include <cstdio>9#include <string>10#include <vector>11 12static void print_usage(int, char ** argv) {13    LOG("\nexample usage:\n");14    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]);15    LOG("\n");16}17 18int main(int argc, char ** argv) {19    std::setlocale(LC_NUMERIC, "C");20 21    common_params params;22 23    common_init();24 25    if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_BENCH, print_usage)) {26        return 1;27    }28 29    int is_pp_shared   = params.is_pp_shared;30    int is_tg_separate = params.is_tg_separate;31 32    std::vector<int> n_pp = params.n_pp;33    std::vector<int> n_tg = params.n_tg;34    std::vector<int> n_pl = params.n_pl;35 36    // init LLM37 38    llama_backend_init();39    llama_numa_init(params.numa);40 41    // initialize the model42 43    llama_model_params model_params = common_model_params_to_llama(params);44 45    llama_model * model = llama_model_load_from_file(params.model.path.c_str(), model_params);46 47    if (model == NULL) {48        fprintf(stderr , "%s: error: unable to load model\n" , __func__);49        return 1;50    }51 52    llama_context_params ctx_params = common_context_params_to_llama(params);53 54    // ensure enough sequences are available55    ctx_params.n_seq_max = n_pl.empty() ? 1 : *std::max_element(n_pl.begin(), n_pl.end());56 57    llama_context * ctx = llama_init_from_model(model, ctx_params);58 59    if (ctx == NULL) {60        fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);61        llama_model_free(model);62        return 1;63    }64 65    const llama_vocab * vocab   = llama_model_get_vocab(model);66    const int32_t       n_vocab = llama_vocab_n_tokens(vocab);67 68    const auto get_token_rand = [n_vocab]() -> llama_token {69        return std::rand() % n_vocab;70    };71 72    auto * mem = llama_get_memory(ctx);73 74    const int32_t n_kv_max = llama_n_ctx(ctx);75 76    llama_batch batch = llama_batch_init(n_kv_max, 0, 1);77 78    // decode in batches of ctx_params.n_batch tokens79    auto decode_helper = [](llama_context * ctx, llama_batch & batch, int32_t n_batch, bool synchronize) {80        for (int32_t i = 0; i < batch.n_tokens; i += n_batch) {81            const int32_t n_tokens = std::min(n_batch, batch.n_tokens - i);82 83            llama_batch batch_view = {84                n_tokens,85                batch.token    + i,86                nullptr,87                batch.pos      + i,88                batch.n_seq_id + i,89                batch.seq_id   + i,90                batch.logits   + i,91            };92 93            const int ret = llama_decode(ctx, batch_view);94            if (ret != 0) {95                LOG_ERR("failed to decode the batch, n_batch = %d, ret = %d\n", n_batch, ret);96                return false;97            }98 99            if (synchronize) {100                llama_synchronize(ctx);101            }102        }103 104        return true;105    };106 107    // warm up108    {109        for (int i = 0; i < 16; ++i) {110            common_batch_add(batch, get_token_rand(), i, { 0 }, false);111        }112 113        if (!decode_helper(ctx, batch, ctx_params.n_batch, true)) {114            LOG_ERR("%s: llama_decode() failed\n", __func__);115            llama_free(ctx);116            llama_model_free(model);117            return 1;118        }119    }120 121    if (!params.batched_bench_output_jsonl) {122        LOG("\n");123        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);124        LOG("\n");125        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");126        LOG("|%6s-|-%6s-|-%4s-|-%6s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|\n", "------", "------", "----", "------", "--------", "--------", "--------", "--------", "--------", "--------");127    }128 129    for (        int i_pp = 0; i_pp < (int) n_pp.size(); ++i_pp) {130        for (    int i_tg = 0; i_tg < (int) n_tg.size(); ++i_tg) {131            for (int i_pl = 0; i_pl < (int) n_pl.size(); ++i_pl) {132                const int pp = n_pp[i_pp];133                const int tg = n_tg[i_tg];134                const int pl = n_pl[i_pl];135 136                const int n_ctx_req = is_pp_shared ? (params.kv_unified ? pp : pl*pp) + pl*tg : pl*(pp + tg);137 138                if (n_ctx_req > n_kv_max) {139                    continue;140                }141 142                common_batch_clear(batch);143 144                for (int j = 0; j < (is_pp_shared ? 1 : pl); ++j) {145                    for (int i = 0; i < pp; ++i) {146                        common_batch_add(batch, get_token_rand(), i, { j }, i == pp - 1);147                    }148                }149 150                llama_memory_clear(mem, false);151 152                const auto t_pp_start = ggml_time_us();153 154                if (!decode_helper(ctx, batch, ctx_params.n_batch, false)) {155                    LOG_ERR("%s: llama_decode() failed\n", __func__);156                    llama_free(ctx);157                    llama_model_free(model);158                    return 1;159                }160 161                llama_synchronize(ctx);162 163                const auto t_pp_end = ggml_time_us();164 165                if (is_pp_shared) {166                    for (int32_t i = 1; i < pl; ++i) {167                        llama_memory_seq_cp(mem, 0, i, -1, -1);168                    }169 170                    if (!params.kv_unified) {171                        // run one dummy token to apply the memory copy172                        common_batch_clear(batch);173                        common_batch_add(batch, get_token_rand(), pp + 0, { 0 }, true);174                        if (!decode_helper(ctx, batch, ctx_params.n_batch, true)) {175                            LOG_ERR("%s: llama_decode() failed\n", __func__);176                            llama_free(ctx);177                            llama_model_free(model);178                            return 1;179                        }180                        llama_memory_seq_rm(mem, 0, pp, -1);181                    }182                }183 184                const auto t_tg_start = ggml_time_us();185 186                if (is_tg_separate) {187                    // decode pattern:188                    // 0 0 0 ... 1 1 1 ... 2 2 2 ... 3 3 3 ...189                    for (int j = 0; j < pl; ++j) {190                        for (int i = 0; i < tg; ++i) {191                            common_batch_clear(batch);192 193                            common_batch_add(batch, get_token_rand(), pp + i, { j }, true);194 195                            if (!decode_helper(ctx, batch, ctx_params.n_batch, true)) {196                                LOG_ERR("%s: llama_decode() failed\n", __func__);197                                llama_free(ctx);198                                llama_model_free(model);199                                return 1;200                            }201                        }202                    }203                } else {204                    // decode pattern:205                    // 0123 0123 0123 ...206                    for (int i = 0; i < tg; ++i) {207                        common_batch_clear(batch);208 209                        for (int j = 0; j < pl; ++j) {210                            common_batch_add(batch, get_token_rand(), pp + i, { j }, true);211                        }212 213                        if (!decode_helper(ctx, batch, ctx_params.n_batch, true)) {214                            LOG_ERR("%s: llama_decode() failed\n", __func__);215                            llama_free(ctx);216                            llama_model_free(model);217                            return 1;218                        }219                    }220                }221 222                const auto t_tg_end = ggml_time_us();223 224                const int32_t n_kv = n_ctx_req;225 226                const float t_pp = (t_pp_end - t_pp_start) / 1000000.0f;227                const float t_tg = (t_tg_end - t_tg_start) / 1000000.0f;228                const float t    = t_pp + t_tg;229 230                const float speed_pp = is_pp_shared ? pp / t_pp : pl*pp / t_pp;231                const float speed_tg = pl*tg / t_tg;232                const float speed    = ((is_pp_shared ? pp : pl*pp) + pl*tg) / t;233 234                if(params.batched_bench_output_jsonl) {235                    LOG(236                        "{\"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, "237                        "\"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",238                        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,239                        pp, tg, pl, n_kv, t_pp, speed_pp, t_tg, speed_tg, t, speed240                    );241                } else {242                    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);243                }244            }245        }246    }247 248    LOG("\n");249    llama_perf_context_print(ctx);250 251    llama_batch_free(batch);252 253    llama_free(ctx);254    llama_model_free(model);255 256    llama_backend_free();257 258    return 0;259}260