KBaba7/llama.cpp
0
1#include "arg.h"2#include "common.h"3#include "log.h"4#include "llama.h"5 6#include <algorithm>7#include <cstdio>8#include <string>9#include <vector>10 11static void print_usage(int, char ** argv) {12 LOG("\nexample usage:\n");13 LOG("\n %s -m model.gguf -p \"Hello my name is\" -n 32 -np 4\n", argv[0]);14 LOG("\n");15}16 17int main(int argc, char ** argv) {18 common_params params;19 20 params.prompt = "Hello my name is";21 params.n_predict = 32;22 23 if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON, print_usage)) {24 return 1;25 }26 27 common_init();28 29 // number of parallel batches30 int n_parallel = params.n_parallel;31 32 // total length of the sequences including the prompt33 int n_predict = params.n_predict;34 35 // init LLM36 37 llama_backend_init();38 llama_numa_init(params.numa);39 40 // initialize the model41 42 llama_model_params model_params = common_model_params_to_llama(params);43 44 llama_model * model = llama_model_load_from_file(params.model.c_str(), model_params);45 46 if (model == NULL) {47 LOG_ERR("%s: error: unable to load model\n" , __func__);48 return 1;49 }50 51 const llama_vocab * vocab = llama_model_get_vocab(model);52 53 // tokenize the prompt54 55 std::vector<llama_token> tokens_list;56 tokens_list = common_tokenize(vocab, params.prompt, true);57 58 const int n_kv_req = tokens_list.size() + (n_predict - tokens_list.size())*n_parallel;59 60 // initialize the context61 62 llama_context_params ctx_params = common_context_params_to_llama(params);63 64 ctx_params.n_ctx = n_kv_req;65 ctx_params.n_batch = std::max(n_predict, n_parallel);66 67 llama_context * ctx = llama_init_from_model(model, ctx_params);68 69 auto sparams = llama_sampler_chain_default_params();70 sparams.no_perf = false;71 72 llama_sampler * smpl = llama_sampler_chain_init(sparams);73 74 llama_sampler_chain_add(smpl, llama_sampler_init_top_k(params.sampling.top_k));75 llama_sampler_chain_add(smpl, llama_sampler_init_top_p(params.sampling.top_p, params.sampling.min_keep));76 llama_sampler_chain_add(smpl, llama_sampler_init_temp (params.sampling.temp));77 llama_sampler_chain_add(smpl, llama_sampler_init_dist (params.sampling.seed));78 79 if (ctx == NULL) {80 LOG_ERR("%s: error: failed to create the llama_context\n" , __func__);81 return 1;82 }83 84 const int n_ctx = llama_n_ctx(ctx);85 86 LOG_INF("\n%s: n_predict = %d, n_ctx = %d, n_batch = %u, n_parallel = %d, n_kv_req = %d\n", __func__, n_predict, n_ctx, ctx_params.n_batch, n_parallel, n_kv_req);87 88 // make sure the KV cache is big enough to hold all the prompt and generated tokens89 if (n_kv_req > n_ctx) {90 LOG_ERR("%s: error: n_kv_req (%d) > n_ctx, the required KV cache size is not big enough\n", __func__, n_kv_req);91 LOG_ERR("%s: either reduce n_parallel or increase n_ctx\n", __func__);92 return 1;93 }94 95 // print the prompt token-by-token96 97 LOG("\n");98 99 for (auto id : tokens_list) {100 LOG("%s", common_token_to_piece(ctx, id).c_str());101 }102 103 // create a llama_batch104 // we use this object to submit token data for decoding105 llama_batch batch = llama_batch_init(std::max(tokens_list.size(), (size_t) n_parallel), 0, n_parallel);106 107 std::vector<llama_seq_id> seq_ids(n_parallel, 0);108 for (int32_t i = 0; i < n_parallel; ++i) {109 seq_ids[i] = i;110 }111 112 // evaluate the initial prompt113 for (size_t i = 0; i < tokens_list.size(); ++i) {114 common_batch_add(batch, tokens_list[i], i, seq_ids, false);115 }116 GGML_ASSERT(batch.n_tokens == (int) tokens_list.size());117 118 if (llama_model_has_encoder(model)) {119 if (llama_encode(ctx, batch)) {120 LOG_ERR("%s : failed to eval\n", __func__);121 return 1;122 }123 124 llama_token decoder_start_token_id = llama_model_decoder_start_token(model);125 if (decoder_start_token_id == LLAMA_TOKEN_NULL) {126 decoder_start_token_id = llama_vocab_bos(vocab);127 }128 129 common_batch_clear(batch);130 common_batch_add(batch, decoder_start_token_id, 0, seq_ids, false);131 }132 133 // llama_decode will output logits only for the last token of the prompt134 batch.logits[batch.n_tokens - 1] = true;135 136 if (llama_decode(ctx, batch) != 0) {137 LOG_ERR("%s: llama_decode() failed\n", __func__);138 return 1;139 }140 141 //// assign the system KV cache to all parallel sequences142 //// this way, the parallel sequences will "reuse" the prompt tokens without having to copy them143 //for (int32_t i = 1; i < n_parallel; ++i) {144 // llama_kv_cache_seq_cp(ctx, 0, i, -1, -1);145 //}146 147 if (n_parallel > 1) {148 LOG("\n\n%s: generating %d sequences ...\n", __func__, n_parallel);149 }150 151 // main loop152 153 // we will store the parallel decoded sequences in this vector154 std::vector<std::string> streams(n_parallel);155 156 // remember the batch index of the last token for each parallel sequence157 // we need this to determine which logits to sample from158 std::vector<int32_t> i_batch(n_parallel, batch.n_tokens - 1);159 160 int n_cur = batch.n_tokens;161 int n_decode = 0;162 163 const auto t_main_start = ggml_time_us();164 165 while (n_cur <= n_predict) {166 // prepare the next batch167 common_batch_clear(batch);168 169 // sample the next token for each parallel sequence / stream170 for (int32_t i = 0; i < n_parallel; ++i) {171 if (i_batch[i] < 0) {172 // the stream has already finished173 continue;174 }175 176 const llama_token new_token_id = llama_sampler_sample(smpl, ctx, i_batch[i]);177 178 // is it an end of generation? -> mark the stream as finished179 if (llama_vocab_is_eog(vocab, new_token_id) || n_cur == n_predict) {180 i_batch[i] = -1;181 LOG("\n");182 if (n_parallel > 1) {183 LOG_INF("%s: stream %d finished at n_cur = %d", __func__, i, n_cur);184 }185 186 continue;187 }188 189 // if there is only one stream, we print immediately to stdout190 if (n_parallel == 1) {191 LOG("%s", common_token_to_piece(ctx, new_token_id).c_str());192 }193 194 streams[i] += common_token_to_piece(ctx, new_token_id);195 196 i_batch[i] = batch.n_tokens;197 198 // push this new token for next evaluation199 common_batch_add(batch, new_token_id, n_cur, { i }, true);200 201 n_decode += 1;202 }203 204 // all streams are finished205 if (batch.n_tokens == 0) {206 break;207 }208 209 n_cur += 1;210 211 // evaluate the current batch with the transformer model212 if (llama_decode(ctx, batch)) {213 LOG_ERR("%s : failed to eval, return code %d\n", __func__, 1);214 return 1;215 }216 }217 218 if (n_parallel > 1) {219 LOG("\n");220 221 for (int32_t i = 0; i < n_parallel; ++i) {222 LOG("sequence %d:\n\n%s%s\n\n", i, params.prompt.c_str(), streams[i].c_str());223 }224 }225 226 const auto t_main_end = ggml_time_us();227 228 LOG_INF("%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",229 __func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));230 231 LOG("\n");232 llama_perf_sampler_print(smpl);233 llama_perf_context_print(ctx);234 235 fprintf(stderr, "\n");236 237 llama_batch_free(batch);238 239 llama_sampler_free(smpl);240 llama_free(ctx);241 llama_model_free(model);242 243 llama_backend_free();244 245 return 0;246}247 