2023-04-24 16:23:31 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "llama.h"
|
|
|
|
|
2023-04-29 10:48:11 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <chrono>
|
2023-04-24 16:23:31 +00:00
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
gpt_params params;
|
2023-10-17 16:12:46 +00:00
|
|
|
|
2023-04-24 16:23:31 +00:00
|
|
|
params.prompt = "The quick brown fox";
|
|
|
|
|
2023-09-07 17:22:29 +00:00
|
|
|
if (!gpt_params_parse(argc, argv, params)) {
|
2023-04-24 16:23:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-09-15 20:59:49 +00:00
|
|
|
print_build_info();
|
2023-05-01 16:23:47 +00:00
|
|
|
|
2023-04-29 10:48:11 +00:00
|
|
|
if (params.n_predict < 0) {
|
|
|
|
params.n_predict = 16;
|
|
|
|
}
|
|
|
|
|
2023-04-24 16:23:31 +00:00
|
|
|
auto n_past = 0;
|
2023-10-17 16:12:46 +00:00
|
|
|
|
|
|
|
std::string result0;
|
|
|
|
std::string result1;
|
2023-04-24 16:23:31 +00:00
|
|
|
|
|
|
|
// init
|
2023-09-28 19:42:38 +00:00
|
|
|
llama_model * model;
|
|
|
|
llama_context * ctx;
|
|
|
|
|
2023-10-17 16:12:46 +00: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 08:47:58 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2023-10-17 16:12:46 +00:00
|
|
|
|
|
|
|
// tokenize prompt
|
2023-09-07 17:22:29 +00:00
|
|
|
auto tokens = llama_tokenize(ctx, params.prompt, true);
|
2023-04-24 16:23:31 +00:00
|
|
|
|
|
|
|
// evaluate prompt
|
2023-10-17 16:12:46 +00:00
|
|
|
llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size(), n_past, 0));
|
|
|
|
n_past += tokens.size();
|
2023-04-24 16:23:31 +00:00
|
|
|
|
2023-10-17 16:12:46 +00:00
|
|
|
// save state (rng, logits, embedding and kv_cache) to file
|
2023-04-29 10:48:11 +00:00
|
|
|
{
|
2023-10-17 16:12:46 +00:00
|
|
|
std::vector<uint8_t> state_mem(llama_get_state_size(ctx));
|
2024-01-13 16:29:43 +00:00
|
|
|
const size_t written = llama_copy_state_data(ctx, state_mem.data());
|
2023-10-17 16:12:46 +00:00
|
|
|
|
2024-01-13 16:29:43 +00:00
|
|
|
FILE *fp_write = fopen("dump_state.bin", "wb");
|
|
|
|
fwrite(state_mem.data(), 1, written, fp_write);
|
|
|
|
fclose(fp_write);
|
|
|
|
|
|
|
|
fprintf(stderr, "%s : serialized state into %zd out of a maximum of %zd bytes\n", __func__, written, state_mem.size());
|
2023-04-29 10:48:11 +00:00
|
|
|
}
|
2023-04-24 16:23:31 +00:00
|
|
|
|
|
|
|
// save state (last tokens)
|
2023-04-29 10:48:11 +00:00
|
|
|
const auto n_past_saved = n_past;
|
2023-04-24 16:23:31 +00:00
|
|
|
|
|
|
|
// first run
|
2023-10-17 16:12:46 +00:00
|
|
|
printf("\nfirst run: %s", params.prompt.c_str());
|
2023-04-29 10:53:12 +00:00
|
|
|
|
2023-04-24 16:23:31 +00:00
|
|
|
for (auto i = 0; i < params.n_predict; i++) {
|
2023-09-28 16:04:36 +00:00
|
|
|
auto * logits = llama_get_logits(ctx);
|
2023-09-28 19:42:38 +00:00
|
|
|
auto n_vocab = llama_n_vocab(model);
|
2023-10-17 16:12:46 +00: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 05:34:41 +00: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 11:19:19 +00:00
|
|
|
auto next_token_str = llama_token_to_piece(ctx, next_token);
|
2023-04-29 10:48:11 +00:00
|
|
|
|
2023-08-21 20:07:43 +00:00
|
|
|
printf("%s", next_token_str.c_str());
|
2023-10-17 16:12:46 +00:00
|
|
|
result0 += next_token_str;
|
|
|
|
|
2023-09-28 19:42:38 +00:00
|
|
|
if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0))) {
|
2023-04-24 16:23:31 +00:00
|
|
|
fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
|
2023-06-24 08:47:58 +00:00
|
|
|
llama_free(ctx);
|
|
|
|
llama_free_model(model);
|
2023-04-24 16:23:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
n_past += 1;
|
|
|
|
}
|
2023-04-29 10:53:12 +00:00
|
|
|
|
2023-04-24 16:23:31 +00:00
|
|
|
printf("\n\n");
|
|
|
|
|
2023-06-24 08:47:58 +00:00
|
|
|
// free old context
|
2023-04-24 16:23:31 +00:00
|
|
|
llama_free(ctx);
|
|
|
|
|
2023-06-24 08:47:58 +00:00
|
|
|
// make new context
|
2023-09-28 19:42:38 +00:00
|
|
|
auto * ctx2 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
|
2023-04-24 16:23:31 +00:00
|
|
|
|
2023-10-17 16:12:46 +00:00
|
|
|
printf("\nsecond run: %s", params.prompt.c_str());
|
|
|
|
|
|
|
|
// load state (rng, logits, embedding and kv_cache) from file
|
2023-04-29 10:48:11 +00:00
|
|
|
{
|
2023-10-17 16:12:46 +00:00
|
|
|
std::vector<uint8_t> state_mem(llama_get_state_size(ctx2));
|
2023-04-29 10:53:12 +00:00
|
|
|
|
2023-10-17 16:12:46 +00:00
|
|
|
FILE * fp_read = fopen("dump_state.bin", "rb");
|
2024-01-13 16:29:43 +00:00
|
|
|
const size_t read = fread(state_mem.data(), 1, state_mem.size(), fp_read);
|
|
|
|
fclose(fp_read);
|
2023-10-17 16:12:46 +00:00
|
|
|
|
2024-01-13 16:29:43 +00:00
|
|
|
if (read != llama_set_state_data(ctx2, state_mem.data())) {
|
2023-04-29 10:53:12 +00:00
|
|
|
fprintf(stderr, "\n%s : failed to read state\n", __func__);
|
2023-06-24 08:47:58 +00:00
|
|
|
llama_free(ctx2);
|
|
|
|
llama_free_model(model);
|
2023-04-29 10:53:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-01-13 16:29:43 +00:00
|
|
|
fprintf(stderr, "%s : deserialized state from %zd out of a maximum of %zd bytes\n", __func__, read, state_mem.size());
|
2023-04-24 16:23:31 +00:00
|
|
|
}
|
2023-04-29 10:48:11 +00:00
|
|
|
|
2023-04-24 16:23:31 +00:00
|
|
|
// restore state (last tokens)
|
|
|
|
n_past = n_past_saved;
|
|
|
|
|
|
|
|
// second run
|
|
|
|
for (auto i = 0; i < params.n_predict; i++) {
|
2023-09-28 16:04:36 +00:00
|
|
|
auto * logits = llama_get_logits(ctx2);
|
2023-09-28 19:42:38 +00: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 05:34:41 +00: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 11:19:19 +00:00
|
|
|
auto next_token_str = llama_token_to_piece(ctx2, next_token);
|
2023-04-29 10:48:11 +00:00
|
|
|
|
2023-08-21 20:07:43 +00:00
|
|
|
printf("%s", next_token_str.c_str());
|
2023-10-17 16:12:46 +00:00
|
|
|
result1 += next_token_str;
|
|
|
|
|
|
|
|
if (llama_decode(ctx2, llama_batch_get_one(&next_token, 1, n_past, 0))) {
|
2023-04-24 16:23:31 +00:00
|
|
|
fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
|
2023-06-24 08:47:58 +00:00
|
|
|
llama_free(ctx2);
|
|
|
|
llama_free_model(model);
|
2023-04-24 16:23:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
n_past += 1;
|
|
|
|
}
|
2023-04-29 10:48:11 +00:00
|
|
|
|
2023-10-17 16:12:46 +00:00
|
|
|
printf("\n");
|
2023-04-29 10:48:11 +00:00
|
|
|
|
2023-06-24 08:47:58 +00:00
|
|
|
llama_free(ctx2);
|
|
|
|
llama_free_model(model);
|
|
|
|
|
2023-10-17 16:12:46 +00: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 16:23:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|