2024-09-09 21:36:09 +00:00
|
|
|
#include "arg.h"
|
2023-09-28 16:04:36 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "llama.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2024-09-07 18:43:51 +00:00
|
|
|
static void print_usage(int, char ** argv) {
|
2024-06-04 18:23:39 +00:00
|
|
|
LOG_TEE("\nexample usage:\n");
|
|
|
|
LOG_TEE("\n %s -m model.gguf -p \"Hello my name is\" -n 32 -np 4\n", argv[0]);
|
|
|
|
LOG_TEE("\n");
|
|
|
|
}
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
gpt_params params;
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
params.prompt = "Hello my name is";
|
|
|
|
params.n_predict = 32;
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2024-09-09 21:36:09 +00:00
|
|
|
if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON, print_usage)) {
|
2024-06-04 18:23:39 +00:00
|
|
|
return 1;
|
2023-10-22 05:37:20 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 13:48:37 +00:00
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
// number of parallel batches
|
|
|
|
int n_parallel = params.n_parallel;
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
// total length of the sequences including the prompt
|
2024-07-17 07:34:28 +00:00
|
|
|
int n_predict = params.n_predict;
|
2024-03-22 07:36:03 +00:00
|
|
|
|
2023-09-28 16:04:36 +00:00
|
|
|
// init LLM
|
|
|
|
|
2024-02-16 09:31:07 +00:00
|
|
|
llama_backend_init();
|
|
|
|
llama_numa_init(params.numa);
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2023-09-28 19:42:38 +00:00
|
|
|
// initialize the model
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
llama_model_params model_params = llama_model_params_from_gpt_params(params);
|
2023-09-28 19:42:38 +00:00
|
|
|
|
|
|
|
llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
|
2023-09-28 16:04:36 +00:00
|
|
|
|
|
|
|
if (model == NULL) {
|
|
|
|
fprintf(stderr , "%s: error: unable to load model\n" , __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-09-28 19:42:38 +00:00
|
|
|
// tokenize the prompt
|
|
|
|
|
|
|
|
std::vector<llama_token> tokens_list;
|
|
|
|
tokens_list = ::llama_tokenize(model, params.prompt, true);
|
2024-01-08 09:14:04 +00:00
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
const int n_kv_req = tokens_list.size() + (n_predict - tokens_list.size())*n_parallel;
|
2023-09-28 19:42:38 +00:00
|
|
|
|
|
|
|
// initialize the context
|
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
|
2023-09-28 19:42:38 +00:00
|
|
|
|
2024-03-22 07:36:03 +00:00
|
|
|
ctx_params.n_ctx = n_kv_req;
|
2024-06-04 18:23:39 +00:00
|
|
|
ctx_params.n_batch = std::max(n_predict, n_parallel);
|
2023-09-28 19:42:38 +00:00
|
|
|
|
2023-09-28 16:04:36 +00:00
|
|
|
llama_context * ctx = llama_new_context_with_model(model, ctx_params);
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
auto sparams = llama_sampler_chain_default_params();
|
|
|
|
|
|
|
|
llama_sampler * smpl = llama_sampler_chain_init(sparams);
|
|
|
|
|
|
|
|
llama_sampler_chain_add(smpl, llama_sampler_init_top_k(params.sparams.top_k));
|
|
|
|
llama_sampler_chain_add(smpl, llama_sampler_init_top_p(params.sparams.top_p, params.sparams.min_keep));
|
|
|
|
llama_sampler_chain_add(smpl, llama_sampler_init_temp (params.sparams.temp));
|
|
|
|
llama_sampler_chain_add(smpl, llama_sampler_init_dist (params.sparams.seed));
|
|
|
|
|
2023-09-28 16:04:36 +00:00
|
|
|
if (ctx == NULL) {
|
|
|
|
fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
const int n_ctx = llama_n_ctx(ctx);
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
LOG_TEE("\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);
|
2023-09-28 16:04:36 +00:00
|
|
|
|
|
|
|
// make sure the KV cache is big enough to hold all the prompt and generated tokens
|
|
|
|
if (n_kv_req > n_ctx) {
|
|
|
|
LOG_TEE("%s: error: n_kv_req (%d) > n_ctx, the required KV cache size is not big enough\n", __func__, n_kv_req);
|
|
|
|
LOG_TEE("%s: either reduce n_parallel or increase n_ctx\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print the prompt token-by-token
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
for (auto id : tokens_list) {
|
|
|
|
fprintf(stderr, "%s", llama_token_to_piece(ctx, id).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
2023-10-18 13:21:57 +00:00
|
|
|
// create a llama_batch
|
2023-09-28 16:04:36 +00:00
|
|
|
// we use this object to submit token data for decoding
|
2024-07-04 13:46:11 +00:00
|
|
|
llama_batch batch = llama_batch_init(std::max(tokens_list.size(), (size_t) n_parallel), 0, n_parallel);
|
|
|
|
|
|
|
|
std::vector<llama_seq_id> seq_ids(n_parallel, 0);
|
|
|
|
for (int32_t i = 0; i < n_parallel; ++i) {
|
|
|
|
seq_ids[i] = i;
|
|
|
|
}
|
2023-09-28 16:04:36 +00:00
|
|
|
|
|
|
|
// evaluate the initial prompt
|
2023-10-18 13:21:57 +00:00
|
|
|
for (size_t i = 0; i < tokens_list.size(); ++i) {
|
2024-07-04 13:46:11 +00:00
|
|
|
llama_batch_add(batch, tokens_list[i], i, seq_ids, false);
|
2023-09-28 16:04:36 +00:00
|
|
|
}
|
2023-10-18 13:21:57 +00:00
|
|
|
GGML_ASSERT(batch.n_tokens == (int) tokens_list.size());
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2024-07-04 13:46:11 +00:00
|
|
|
if (llama_model_has_encoder(model)) {
|
|
|
|
if (llama_encode(ctx, batch)) {
|
|
|
|
LOG_TEE("%s : failed to eval\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
llama_token decoder_start_token_id = llama_model_decoder_start_token(model);
|
|
|
|
if (decoder_start_token_id == -1) {
|
|
|
|
decoder_start_token_id = llama_token_bos(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
llama_batch_clear(batch);
|
|
|
|
llama_batch_add(batch, decoder_start_token_id, 0, seq_ids, false);
|
|
|
|
}
|
|
|
|
|
2023-09-28 16:04:36 +00:00
|
|
|
// llama_decode will output logits only for the last token of the prompt
|
|
|
|
batch.logits[batch.n_tokens - 1] = true;
|
|
|
|
|
2023-09-28 19:42:38 +00:00
|
|
|
if (llama_decode(ctx, batch) != 0) {
|
2023-09-28 16:04:36 +00:00
|
|
|
LOG_TEE("%s: llama_decode() failed\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-07-04 13:46:11 +00:00
|
|
|
//// assign the system KV cache to all parallel sequences
|
|
|
|
//// this way, the parallel sequences will "reuse" the prompt tokens without having to copy them
|
|
|
|
//for (int32_t i = 1; i < n_parallel; ++i) {
|
|
|
|
// llama_kv_cache_seq_cp(ctx, 0, i, -1, -1);
|
|
|
|
//}
|
2023-09-28 16:04:36 +00:00
|
|
|
|
|
|
|
if (n_parallel > 1) {
|
|
|
|
LOG_TEE("\n\n%s: generating %d sequences ...\n", __func__, n_parallel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// main loop
|
|
|
|
|
|
|
|
// we will store the parallel decoded sequences in this vector
|
|
|
|
std::vector<std::string> streams(n_parallel);
|
|
|
|
|
|
|
|
// remember the batch index of the last token for each parallel sequence
|
|
|
|
// we need this to determine which logits to sample from
|
|
|
|
std::vector<int32_t> i_batch(n_parallel, batch.n_tokens - 1);
|
|
|
|
|
|
|
|
int n_cur = batch.n_tokens;
|
|
|
|
int n_decode = 0;
|
|
|
|
|
|
|
|
const auto t_main_start = ggml_time_us();
|
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
while (n_cur <= n_predict) {
|
2023-09-28 16:04:36 +00:00
|
|
|
// prepare the next batch
|
2023-10-18 13:21:57 +00:00
|
|
|
llama_batch_clear(batch);
|
2023-09-28 16:04:36 +00:00
|
|
|
|
|
|
|
// sample the next token for each parallel sequence / stream
|
|
|
|
for (int32_t i = 0; i < n_parallel; ++i) {
|
|
|
|
if (i_batch[i] < 0) {
|
|
|
|
// the stream has already finished
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
const llama_token new_token_id = llama_sampler_sample(smpl, ctx, i_batch[i]);
|
2023-09-28 16:04:36 +00:00
|
|
|
|
2024-04-21 11:50:41 +00:00
|
|
|
// is it an end of generation? -> mark the stream as finished
|
2024-06-04 18:23:39 +00:00
|
|
|
if (llama_token_is_eog(model, new_token_id) || n_cur == n_predict) {
|
2023-09-28 16:04:36 +00:00
|
|
|
i_batch[i] = -1;
|
|
|
|
LOG_TEE("\n");
|
|
|
|
if (n_parallel > 1) {
|
|
|
|
LOG_TEE("%s: stream %d finished at n_cur = %d", __func__, i, n_cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is only one stream, we print immediately to stdout
|
|
|
|
if (n_parallel == 1) {
|
|
|
|
LOG_TEE("%s", llama_token_to_piece(ctx, new_token_id).c_str());
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
streams[i] += llama_token_to_piece(ctx, new_token_id);
|
|
|
|
|
|
|
|
i_batch[i] = batch.n_tokens;
|
|
|
|
|
2023-10-18 13:21:57 +00:00
|
|
|
// push this new token for next evaluation
|
|
|
|
llama_batch_add(batch, new_token_id, n_cur, { i }, true);
|
2023-09-28 16:04:36 +00:00
|
|
|
|
|
|
|
n_decode += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// all streams are finished
|
|
|
|
if (batch.n_tokens == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n_cur += 1;
|
|
|
|
|
|
|
|
// evaluate the current batch with the transformer model
|
2023-09-28 19:42:38 +00:00
|
|
|
if (llama_decode(ctx, batch)) {
|
2023-09-28 16:04:36 +00:00
|
|
|
fprintf(stderr, "%s : failed to eval, return code %d\n", __func__, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_TEE("\n");
|
|
|
|
|
|
|
|
if (n_parallel > 1) {
|
|
|
|
LOG_TEE("\n");
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < n_parallel; ++i) {
|
|
|
|
LOG_TEE("sequence %d:\n\n%s%s\n\n", i, params.prompt.c_str(), streams[i].c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto t_main_end = ggml_time_us();
|
|
|
|
|
|
|
|
LOG_TEE("%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",
|
|
|
|
__func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
LOG_TEE("\n");
|
2024-09-13 06:53:38 +00:00
|
|
|
llama_perf_sampler_print(smpl);
|
|
|
|
llama_perf_context_print(ctx);
|
2023-09-28 16:04:36 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
llama_batch_free(batch);
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_sampler_free(smpl);
|
2023-09-28 16:04:36 +00:00
|
|
|
llama_free(ctx);
|
|
|
|
llama_free_model(model);
|
|
|
|
|
|
|
|
llama_backend_free();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|