2023-06-16 20:58:09 +02:00
|
|
|
#include "common.h"
|
|
|
|
#include "llama.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
static void print_usage(int argc, char ** argv, const gpt_params & params) {
|
|
|
|
gpt_params_print_usage(argc, argv, params);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
LOG_TEE("\nexample usage:\n");
|
|
|
|
LOG_TEE("\n %s -m model.gguf -p \"Hello my name is\" -n 32\n", argv[0]);
|
|
|
|
LOG_TEE("\n");
|
|
|
|
}
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
gpt_params params;
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
params.prompt = "Hello my name is";
|
|
|
|
params.n_predict = 32;
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
if (!gpt_params_parse(argc, argv, params)) {
|
|
|
|
print_usage(argc, argv, params);
|
|
|
|
return 1;
|
2023-06-16 20:58:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// total length of the sequence including the prompt
|
2024-06-04 20:23:39 +02:00
|
|
|
const int n_predict = params.n_predict;
|
2023-09-28 18:04:36 +02:00
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
// init LLM
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2024-02-16 10:31:07 +01:00
|
|
|
llama_backend_init();
|
|
|
|
llama_numa_init(params.numa);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 21:42:38 +02:00
|
|
|
// initialize the model
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
llama_model_params model_params = llama_model_params_from_gpt_params(params);
|
2023-09-28 21:42:38 +02:00
|
|
|
|
|
|
|
llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
if (model == NULL) {
|
|
|
|
fprintf(stderr , "%s: error: unable to load model\n" , __func__);
|
2023-06-16 20:58:09 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-09-28 21:42:38 +02:00
|
|
|
// initialize the context
|
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
|
2023-09-28 21:42:38 +02:00
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
llama_context * ctx = llama_new_context_with_model(model, ctx_params);
|
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
if (ctx == NULL) {
|
|
|
|
fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
// tokenize the prompt
|
2023-06-16 20:58:09 +02:00
|
|
|
|
|
|
|
std::vector<llama_token> tokens_list;
|
2023-08-21 22:07:43 +02:00
|
|
|
tokens_list = ::llama_tokenize(ctx, params.prompt, true);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
const int n_ctx = llama_n_ctx(ctx);
|
2024-06-04 20:23:39 +02:00
|
|
|
const int n_kv_req = tokens_list.size() + (n_predict - tokens_list.size());
|
2023-09-28 18:04:36 +02:00
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
LOG_TEE("\n%s: n_predict = %d, n_ctx = %d, n_kv_req = %d\n", __func__, n_predict, n_ctx, n_kv_req);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02: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 > n_ctx, the required KV cache size is not big enough\n", __func__);
|
2024-06-04 20:23:39 +02:00
|
|
|
LOG_TEE("%s: either reduce n_predict or increase n_ctx\n", __func__);
|
2023-06-16 20:58:09 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// print the prompt token-by-token
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
for (auto id : tokens_list) {
|
2023-08-27 13:19:19 +02:00
|
|
|
fprintf(stderr, "%s", llama_token_to_piece(ctx, id).c_str());
|
2023-06-16 20:58:09 +02:00
|
|
|
}
|
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
fflush(stderr);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// create a llama_batch with size 512
|
|
|
|
// we use this object to submit token data for decoding
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-10-18 15:21:57 +02:00
|
|
|
llama_batch batch = llama_batch_init(512, 0, 1);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// evaluate the initial prompt
|
2023-10-27 16:37:41 +02:00
|
|
|
for (size_t i = 0; i < tokens_list.size(); i++) {
|
|
|
|
llama_batch_add(batch, tokens_list[i], i, { 0 }, false);
|
2023-09-28 18:04:36 +02:00
|
|
|
}
|
2023-08-21 22:07:43 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// llama_decode will output logits only for the last token of the prompt
|
|
|
|
batch.logits[batch.n_tokens - 1] = true;
|
|
|
|
|
2023-09-28 21:42:38 +02:00
|
|
|
if (llama_decode(ctx, batch) != 0) {
|
2023-09-28 18:04:36 +02:00
|
|
|
LOG_TEE("%s: llama_decode() failed\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// main loop
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
int n_cur = batch.n_tokens;
|
|
|
|
int n_decode = 0;
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
const auto t_main_start = ggml_time_us();
|
|
|
|
|
2024-06-04 20:23:39 +02:00
|
|
|
while (n_cur <= n_predict) {
|
2023-08-21 22:07:43 +02:00
|
|
|
// sample the next token
|
2023-09-28 18:04:36 +02:00
|
|
|
{
|
2023-09-28 21:42:38 +02:00
|
|
|
auto n_vocab = llama_n_vocab(model);
|
2023-09-28 18:04:36 +02:00
|
|
|
auto * logits = llama_get_logits_ith(ctx, batch.n_tokens - 1);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
std::vector<llama_token_data> candidates;
|
|
|
|
candidates.reserve(n_vocab);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
|
|
|
|
candidates.emplace_back(llama_token_data{ token_id, logits[token_id], 0.0f });
|
|
|
|
}
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// sample the most likely token
|
|
|
|
const llama_token new_token_id = llama_sample_token_greedy(ctx, &candidates_p);
|
|
|
|
|
2024-04-21 13:50:41 +02:00
|
|
|
// is it an end of generation?
|
2024-06-04 20:23:39 +02:00
|
|
|
if (llama_token_is_eog(model, new_token_id) || n_cur == n_predict) {
|
2023-09-28 18:04:36 +02:00
|
|
|
LOG_TEE("\n");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
LOG_TEE("%s", llama_token_to_piece(ctx, new_token_id).c_str());
|
|
|
|
fflush(stdout);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// prepare the next batch
|
2023-10-27 16:37:41 +02:00
|
|
|
llama_batch_clear(batch);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// push this new token for next evaluation
|
2023-10-27 16:37:41 +02:00
|
|
|
llama_batch_add(batch, new_token_id, n_cur, { 0 }, true);
|
2023-09-28 18:04:36 +02:00
|
|
|
|
|
|
|
n_decode += 1;
|
2023-06-16 20:58:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
n_cur += 1;
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
// evaluate the current batch with the transformer model
|
2023-09-28 21:42:38 +02:00
|
|
|
if (llama_decode(ctx, batch)) {
|
2023-09-28 18:04:36 +02:00
|
|
|
fprintf(stderr, "%s : failed to eval, return code %d\n", __func__, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-08-21 22:07:43 +02:00
|
|
|
}
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-09-28 18:04:36 +02:00
|
|
|
LOG_TEE("\n");
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
llama_print_timings(ctx);
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
llama_batch_free(batch);
|
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
llama_free(ctx);
|
|
|
|
llama_free_model(model);
|
2023-06-16 20:58:09 +02:00
|
|
|
|
2023-07-10 17:49:56 +02:00
|
|
|
llama_backend_free();
|
|
|
|
|
2023-06-16 20:58:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|