2023-04-24 18:23:31 +02:00
|
|
|
#include "common.h"
|
|
|
|
#include "llama.h"
|
|
|
|
|
2023-04-29 12:48:11 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <chrono>
|
2023-04-24 18:23:31 +02:00
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
gpt_params params;
|
2023-10-17 18:12:46 +02:00
|
|
|
|
2023-04-24 18:23:31 +02:00
|
|
|
params.prompt = "The quick brown fox";
|
|
|
|
|
2023-09-07 19:22:29 +02:00
|
|
|
if (!gpt_params_parse(argc, argv, params)) {
|
2023-04-24 18:23:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-09-15 22:59:49 +02:00
|
|
|
print_build_info();
|
2023-05-01 18:23:47 +02:00
|
|
|
|
2023-04-29 12:48:11 +02:00
|
|
|
if (params.n_predict < 0) {
|
|
|
|
params.n_predict = 16;
|
|
|
|
}
|
|
|
|
|
2023-04-24 18:23:31 +02:00
|
|
|
auto n_past = 0;
|
2023-10-17 18:12:46 +02:00
|
|
|
|
|
|
|
std::string result0;
|
|
|
|
std::string result1;
|
2023-04-24 18:23:31 +02:00
|
|
|
|
|
|
|
// init
|
2023-09-28 21:42:38 +02:00
|
|
|
llama_model * model;
|
|
|
|
llama_context * ctx;
|
|
|
|
|
2023-10-17 18:12:46 +02:00
|
|
|
std::tie(model, ctx) = llama_init_from_gpt_params(params);
|
|
|
|
if (model == nullptr || ctx == nullptr) {
|
|
|
|
fprintf(stderr, "%s : failed to init\n", __func__);
|
2023-06-24 10:47:58 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2023-10-17 18:12:46 +02:00
|
|
|
|
|
|
|
// tokenize prompt
|
2023-09-07 19:22:29 +02:00
|
|
|
auto tokens = llama_tokenize(ctx, params.prompt, true);
|
2023-04-24 18:23:31 +02:00
|
|
|
|
|
|
|
// evaluate prompt
|
2023-10-17 18:12:46 +02:00
|
|
|
llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size(), n_past, 0));
|
|
|
|
n_past += tokens.size();
|
2023-04-24 18:23:31 +02:00
|
|
|
|
2023-10-17 18:12:46 +02:00
|
|
|
// save state (rng, logits, embedding and kv_cache) to file
|
2023-04-29 12:48:11 +02:00
|
|
|
{
|
2023-10-17 18:12:46 +02:00
|
|
|
std::vector<uint8_t> state_mem(llama_get_state_size(ctx));
|
|
|
|
|
|
|
|
{
|
|
|
|
FILE *fp_write = fopen("dump_state.bin", "wb");
|
|
|
|
llama_copy_state_data(ctx, state_mem.data()); // could also copy directly to memory mapped file
|
|
|
|
fwrite(state_mem.data(), 1, state_mem.size(), fp_write);
|
|
|
|
fclose(fp_write);
|
|
|
|
}
|
2023-04-29 12:48:11 +02:00
|
|
|
}
|
2023-04-24 18:23:31 +02:00
|
|
|
|
|
|
|
// save state (last tokens)
|
2023-04-29 12:48:11 +02:00
|
|
|
const auto n_past_saved = n_past;
|
2023-04-24 18:23:31 +02:00
|
|
|
|
|
|
|
// first run
|
2023-10-17 18:12:46 +02:00
|
|
|
printf("\nfirst run: %s", params.prompt.c_str());
|
2023-04-29 12:53:12 +02:00
|
|
|
|
2023-04-24 18:23:31 +02:00
|
|
|
for (auto i = 0; i < params.n_predict; i++) {
|
2023-09-28 18:04:36 +02:00
|
|
|
auto * logits = llama_get_logits(ctx);
|
2023-09-28 21:42:38 +02:00
|
|
|
auto n_vocab = llama_n_vocab(model);
|
2023-10-17 18:12:46 +02:00
|
|
|
|
llama : new sampling algorithms (#1126)
* Sample interface, new samplers.
New samplers:
- locally typical sampling
- tail free sampling
- frequency and presence penalty
- mirostat
Ignore EOS fix: -inf should be used.
* mirostat
* Added --logit-bias and --no-penalize-nl, removed std::span
* Use C++11, clarify llama API documentation, rename Mirostat parameters to --mirostat_lr and --mirostat_ent, add temperature sampling for Mirostat, simplify Mirostat sampling API parameters (removed N and *k)
Use C++11, clarify llama API documentation, rename Mirostat parameters to --mirostat_lr and --mirostat_ent, add temperature sampling for Mirostat, simplify Mirostat sampling API parameters (removed N and *k)
* Save and load example adjust
* Tests
* Windows build fix
* Windows test fix
2023-04-29 07:34:41 +02:00
|
|
|
std::vector<llama_token_data> candidates;
|
|
|
|
candidates.reserve(n_vocab);
|
|
|
|
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});
|
|
|
|
}
|
|
|
|
llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
|
|
|
|
auto next_token = llama_sample_token(ctx, &candidates_p);
|
2023-08-27 13:19:19 +02:00
|
|
|
auto next_token_str = llama_token_to_piece(ctx, next_token);
|
2023-04-29 12:48:11 +02:00
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
printf("%s", next_token_str.c_str());
|
2023-10-17 18:12:46 +02:00
|
|
|
result0 += next_token_str;
|
|
|
|
|
2023-09-28 21:42:38 +02:00
|
|
|
if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0))) {
|
2023-04-24 18:23:31 +02:00
|
|
|
fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
|
2023-06-24 10:47:58 +02:00
|
|
|
llama_free(ctx);
|
|
|
|
llama_free_model(model);
|
2023-04-24 18:23:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
n_past += 1;
|
|
|
|
}
|
2023-04-29 12:53:12 +02:00
|
|
|
|
2023-04-24 18:23:31 +02:00
|
|
|
printf("\n\n");
|
|
|
|
|
2023-06-24 10:47:58 +02:00
|
|
|
// free old context
|
2023-04-24 18:23:31 +02:00
|
|
|
llama_free(ctx);
|
|
|
|
|
2023-06-24 10:47:58 +02:00
|
|
|
// make new context
|
2023-09-28 21:42:38 +02:00
|
|
|
auto * ctx2 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
|
2023-04-24 18:23:31 +02:00
|
|
|
|
2023-10-17 18:12:46 +02:00
|
|
|
printf("\nsecond run: %s", params.prompt.c_str());
|
|
|
|
|
|
|
|
// load state (rng, logits, embedding and kv_cache) from file
|
2023-04-29 12:48:11 +02:00
|
|
|
{
|
2023-10-17 18:12:46 +02:00
|
|
|
std::vector<uint8_t> state_mem(llama_get_state_size(ctx2));
|
2023-04-29 12:53:12 +02:00
|
|
|
|
2023-10-17 18:12:46 +02:00
|
|
|
FILE * fp_read = fopen("dump_state.bin", "rb");
|
|
|
|
|
|
|
|
const size_t ret = fread(state_mem.data(), 1, state_mem.size(), fp_read);
|
|
|
|
if (ret != state_mem.size()) {
|
2023-04-29 12:53:12 +02:00
|
|
|
fprintf(stderr, "\n%s : failed to read state\n", __func__);
|
2023-06-24 10:47:58 +02:00
|
|
|
llama_free(ctx2);
|
|
|
|
llama_free_model(model);
|
2023-04-29 12:53:12 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-17 18:12:46 +02:00
|
|
|
llama_set_state_data(ctx2, state_mem.data());
|
|
|
|
|
2023-04-29 12:48:11 +02:00
|
|
|
fclose(fp_read);
|
2023-04-24 18:23:31 +02:00
|
|
|
}
|
2023-04-29 12:48:11 +02:00
|
|
|
|
2023-04-24 18:23:31 +02:00
|
|
|
// restore state (last tokens)
|
|
|
|
n_past = n_past_saved;
|
|
|
|
|
|
|
|
// second run
|
|
|
|
for (auto i = 0; i < params.n_predict; i++) {
|
2023-09-28 18:04:36 +02:00
|
|
|
auto * logits = llama_get_logits(ctx2);
|
2023-09-28 21:42:38 +02:00
|
|
|
auto n_vocab = llama_n_vocab(model);
|
llama : new sampling algorithms (#1126)
* Sample interface, new samplers.
New samplers:
- locally typical sampling
- tail free sampling
- frequency and presence penalty
- mirostat
Ignore EOS fix: -inf should be used.
* mirostat
* Added --logit-bias and --no-penalize-nl, removed std::span
* Use C++11, clarify llama API documentation, rename Mirostat parameters to --mirostat_lr and --mirostat_ent, add temperature sampling for Mirostat, simplify Mirostat sampling API parameters (removed N and *k)
Use C++11, clarify llama API documentation, rename Mirostat parameters to --mirostat_lr and --mirostat_ent, add temperature sampling for Mirostat, simplify Mirostat sampling API parameters (removed N and *k)
* Save and load example adjust
* Tests
* Windows build fix
* Windows test fix
2023-04-29 07:34:41 +02:00
|
|
|
std::vector<llama_token_data> candidates;
|
|
|
|
candidates.reserve(n_vocab);
|
|
|
|
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});
|
|
|
|
}
|
|
|
|
llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
|
|
|
|
auto next_token = llama_sample_token(ctx2, &candidates_p);
|
2023-08-27 13:19:19 +02:00
|
|
|
auto next_token_str = llama_token_to_piece(ctx2, next_token);
|
2023-04-29 12:48:11 +02:00
|
|
|
|
2023-08-21 22:07:43 +02:00
|
|
|
printf("%s", next_token_str.c_str());
|
2023-10-17 18:12:46 +02:00
|
|
|
result1 += next_token_str;
|
|
|
|
|
|
|
|
if (llama_decode(ctx2, llama_batch_get_one(&next_token, 1, n_past, 0))) {
|
2023-04-24 18:23:31 +02:00
|
|
|
fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
|
2023-06-24 10:47:58 +02:00
|
|
|
llama_free(ctx2);
|
|
|
|
llama_free_model(model);
|
2023-04-24 18:23:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
n_past += 1;
|
|
|
|
}
|
2023-04-29 12:48:11 +02:00
|
|
|
|
2023-10-17 18:12:46 +02:00
|
|
|
printf("\n");
|
2023-04-29 12:48:11 +02:00
|
|
|
|
2023-06-24 10:47:58 +02:00
|
|
|
llama_free(ctx2);
|
|
|
|
llama_free_model(model);
|
|
|
|
|
2023-10-17 18:12:46 +02:00
|
|
|
if (result0 != result1) {
|
|
|
|
fprintf(stderr, "\n%s : error : the 2 generations are different\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\n%s : success\n", __func__);
|
|
|
|
|
2023-04-24 18:23:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|