Skip to content

Commit dd88594

Browse files
Save prompt after initial prompt eval (fixes #1257)
1 parent f0d70f1 commit dd88594

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

examples/main/main.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ int main(int argc, char ** argv) {
385385

386386
embd.clear();
387387

388+
// optionally save the session after prompt eval (for faster prompt loading next time)
389+
if (!path_session.empty() && need_to_save_session) {
390+
need_to_save_session = false;
391+
llama_save_session_file(ctx, path_session.c_str(), session_tokens.data(), session_tokens.size());
392+
}
393+
388394
if ((int) embd_inp.size() <= n_consumed && !is_interacting) {
389395
// out of user input, sample next token
390396
const float temp = params.temp;
@@ -401,12 +407,6 @@ int main(int argc, char ** argv) {
401407
const float mirostat_eta = params.mirostat_eta;
402408
const bool penalize_nl = params.penalize_nl;
403409

404-
// optionally save the session on first sample (for faster prompt loading next time)
405-
if (!path_session.empty() && need_to_save_session) {
406-
need_to_save_session = false;
407-
llama_save_session_file(ctx, path_session.c_str(), session_tokens.data(), session_tokens.size());
408-
}
409-
410410
llama_token id = 0;
411411

412412
{

llama.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -2724,10 +2724,11 @@ size_t llama_load_session_file(struct llama_context * ctx, const char * path_ses
27242724
const size_t n_orig_state_size = llama_get_state_size(ctx);
27252725
if (n_state_size != n_orig_state_size) {
27262726
fprintf(stderr, "%s : failed to validate state size\n", __func__);
2727+
return 0;
27272728
}
2728-
std::unique_ptr<uint8_t[]> state_data(new uint8_t[n_state_size]);
2729-
file.read_raw(state_data.get(), n_state_size);
2730-
return llama_set_state_data(ctx, state_data.get());
2729+
std::vector<uint8_t> state_data(n_state_size);
2730+
file.read_raw(state_data.data(), n_state_size);
2731+
return llama_set_state_data(ctx, state_data.data());
27312732
}
27322733

27332734
size_t llama_save_session_file(struct llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count) {

0 commit comments

Comments
 (0)