Skip to content

Commit 70269ca

Browse files
authored
llama : fix session load / save (ggml-org#1263)
1 parent b925f1f commit 70269ca

File tree

3 files changed

+96
-69
lines changed

3 files changed

+96
-69
lines changed

examples/main/main.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,22 @@ int main(int argc, char ** argv) {
161161
std::vector<llama_token> session_tokens;
162162

163163
if (!path_session.empty()) {
164-
fprintf(stderr, "%s: attempting to load saved session from %s..\n", __func__, path_session.c_str());
164+
fprintf(stderr, "%s: attempting to load saved session from '%s'\n", __func__, path_session.c_str());
165165

166-
// REVIEW - fopen to check for existing session
166+
// fopen to check for existing session
167167
FILE * fp = std::fopen(path_session.c_str(), "rb");
168168
if (fp != NULL) {
169169
std::fclose(fp);
170170

171171
session_tokens.resize(params.n_ctx);
172172
size_t n_token_count_out = 0;
173-
const size_t n_session_bytes = llama_load_session_file(ctx, path_session.c_str(), session_tokens.data(), session_tokens.capacity(), &n_token_count_out);
173+
if (!llama_load_session_file(ctx, path_session.c_str(), session_tokens.data(), session_tokens.capacity(), &n_token_count_out)) {
174+
fprintf(stderr, "%s: error: failed to load session file '%s'\n", __func__, path_session.c_str());
175+
return 1;
176+
}
174177
session_tokens.resize(n_token_count_out);
175178

176-
if (n_session_bytes > 0) {
177-
fprintf(stderr, "%s: loaded %zu bytes of session data!\n", __func__, n_session_bytes);
178-
} else {
179-
fprintf(stderr, "%s: could not load session file, will recreate\n", __func__);
180-
}
179+
fprintf(stderr, "%s: loaded a session with prompt size of %d tokens\n", __func__, (int) session_tokens.size());
181180
} else {
182181
fprintf(stderr, "%s: session file does not exist, will create\n", __func__);
183182
}
@@ -214,7 +213,7 @@ int main(int argc, char ** argv) {
214213
}
215214

216215
// number of tokens to keep when resetting context
217-
if (params.n_keep < 0 || params.n_keep > (int)embd_inp.size() || params.instruct) {
216+
if (params.n_keep < 0 || params.n_keep > (int) embd_inp.size() || params.instruct) {
218217
params.n_keep = (int)embd_inp.size();
219218
}
220219

@@ -329,7 +328,7 @@ int main(int argc, char ** argv) {
329328
// insert n_left/2 tokens at the start of embd from last_n_tokens
330329
embd.insert(embd.begin(), last_n_tokens.begin() + n_ctx - n_left/2 - embd.size(), last_n_tokens.end() - embd.size());
331330

332-
// REVIEW - stop saving session if we run out of context
331+
// stop saving session if we run out of context
333332
path_session = "";
334333

335334
//printf("\n---\n");
@@ -355,6 +354,7 @@ int main(int argc, char ** argv) {
355354
n_session_consumed++;
356355

357356
if (n_session_consumed >= (int) session_tokens.size()) {
357+
++i;
358358
break;
359359
}
360360
}

llama.cpp

Lines changed: 79 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,6 +2566,85 @@ size_t llama_set_state_data(struct llama_context * ctx, const uint8_t * src) {
25662566
return nread;
25672567
}
25682568

2569+
bool llama_load_session_file(struct llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
2570+
llama_file file(path_session, "rb");
2571+
2572+
// sanity checks
2573+
{
2574+
const uint32_t magic = file.read_u32();
2575+
const uint32_t version = file.read_u32();
2576+
2577+
if (!(magic == LLAMA_SESSION_MAGIC && version == LLAMA_SESSION_VERSION)) {
2578+
fprintf(stderr, "%s : unknown (magic, version) for session file: %08x, %08x\n", __func__, magic, version);
2579+
return false;
2580+
}
2581+
2582+
llama_hparams session_hparams;
2583+
file.read_raw(&session_hparams, sizeof(llama_hparams));
2584+
2585+
if (session_hparams != ctx->model.hparams) {
2586+
fprintf(stderr, "%s : model hparams didn't match from session file!\n", __func__);
2587+
return false;
2588+
}
2589+
}
2590+
2591+
// load the prompt
2592+
{
2593+
const uint32_t n_token_count = file.read_u32();
2594+
2595+
if (n_token_count > n_token_capacity) {
2596+
fprintf(stderr, "%s : token count in session file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity);
2597+
return false;
2598+
}
2599+
2600+
file.read_raw(tokens_out, sizeof(llama_token) * n_token_count);
2601+
*n_token_count_out = n_token_count;
2602+
}
2603+
2604+
// restore the context state
2605+
{
2606+
const size_t n_state_size_cur = file.size - file.tell();
2607+
const size_t n_state_size_exp = llama_get_state_size(ctx);
2608+
2609+
if (n_state_size_cur != n_state_size_exp) {
2610+
fprintf(stderr, "%s : the state size in session file didn't match! expected %zu, got %zu\n", __func__, n_state_size_exp, n_state_size_cur);
2611+
return false;
2612+
}
2613+
2614+
std::vector<uint8_t> state_data(n_state_size_cur);
2615+
file.read_raw(state_data.data(), n_state_size_cur);
2616+
2617+
llama_set_state_data(ctx, state_data.data());
2618+
}
2619+
2620+
return true;
2621+
}
2622+
2623+
bool llama_save_session_file(struct llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count) {
2624+
llama_file file(path_session, "wb");
2625+
2626+
file.write_u32(LLAMA_SESSION_MAGIC);
2627+
file.write_u32(LLAMA_SESSION_VERSION);
2628+
2629+
file.write_raw(&ctx->model.hparams, sizeof(llama_hparams));
2630+
2631+
// save the prompt
2632+
file.write_u32((uint32_t) n_token_count);
2633+
file.write_raw(tokens, sizeof(llama_token) * n_token_count);
2634+
2635+
// save the context state
2636+
{
2637+
const size_t n_state_size = llama_get_state_size(ctx);
2638+
2639+
std::vector<uint8_t> state_data(n_state_size);
2640+
llama_copy_state_data(ctx, state_data.data());
2641+
2642+
file.write_raw(state_data.data(), n_state_size);
2643+
}
2644+
2645+
return true;
2646+
}
2647+
25692648
int llama_eval(
25702649
struct llama_context * ctx,
25712650
const llama_token * tokens,
@@ -2693,57 +2772,3 @@ const char * llama_print_system_info(void) {
26932772
std::vector<std::pair<std::string, struct ggml_tensor *>>& llama_internal_get_tensor_map(struct llama_context * ctx) {
26942773
return ctx->model.tensors_by_name;
26952774
}
2696-
2697-
size_t llama_load_session_file(struct llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
2698-
// TODO leverage mmap
2699-
llama_file file(path_session, "rb");
2700-
const uint32_t magic = file.read_u32();
2701-
const uint32_t version = file.read_u32();
2702-
2703-
if (!(magic == 'ggsn' && version == 0)) {
2704-
fprintf(stderr, "%s : unknown (magic, version) for session file: %08x, %08x\n", __func__, magic, version);
2705-
return 0;
2706-
}
2707-
2708-
llama_hparams session_hparams;
2709-
file.read_raw(&session_hparams, sizeof(llama_hparams));
2710-
2711-
// REVIEW
2712-
if (session_hparams != ctx->model.hparams) {
2713-
fprintf(stderr, "%s : model hparams didn't match from session file!\n", __func__);
2714-
return 0;
2715-
}
2716-
2717-
const uint32_t n_token_count = file.read_u32();
2718-
LLAMA_ASSERT(n_token_capacity >= n_token_count);
2719-
file.read_raw(tokens_out, sizeof(llama_token) * n_token_count);
2720-
*n_token_count_out = n_token_count;
2721-
2722-
const size_t n_state_size = file.size - file.tell();
2723-
const size_t n_orig_state_size = llama_get_state_size(ctx);
2724-
if (n_state_size != n_orig_state_size) {
2725-
fprintf(stderr, "%s : failed to validate state size\n", __func__);
2726-
}
2727-
std::unique_ptr<uint8_t[]> state_data(new uint8_t[n_state_size]);
2728-
file.read_raw(state_data.get(), n_state_size);
2729-
return llama_set_state_data(ctx, state_data.get());
2730-
}
2731-
2732-
size_t llama_save_session_file(struct llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count) {
2733-
// TODO save temp & swap
2734-
llama_file file(path_session, "wb");
2735-
2736-
const size_t n_state_size = llama_get_state_size(ctx);
2737-
std::unique_ptr<uint8_t[]> state_data(new uint8_t[n_state_size]);
2738-
llama_copy_state_data(ctx, state_data.get());
2739-
2740-
file.write_u32('ggsn'); // magic
2741-
file.write_u32(0); // version
2742-
file.write_raw(&ctx->model.hparams, sizeof(llama_hparams));
2743-
2744-
file.write_u32((uint32_t) n_token_count); // REVIEW
2745-
file.write_raw(tokens, sizeof(llama_token) * n_token_count);
2746-
2747-
file.write_raw(state_data.get(), n_state_size);
2748-
return n_state_size; // REVIEW
2749-
}

llama.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
# define LLAMA_API
2020
#endif
2121

22-
#define LLAMA_FILE_VERSION 1
23-
#define LLAMA_FILE_MAGIC 0x67676a74 // 'ggjt' in hex
24-
#define LLAMA_FILE_MAGIC_UNVERSIONED 0x67676d6c // pre-versioned files
22+
#define LLAMA_FILE_VERSION 1
23+
#define LLAMA_FILE_MAGIC 'ggjt'
24+
#define LLAMA_FILE_MAGIC_UNVERSIONED 'ggml'
25+
#define LLAMA_SESSION_MAGIC 'ggsn'
26+
#define LLAMA_SESSION_VERSION 0
2527

2628
#ifdef __cplusplus
2729
extern "C" {
@@ -138,8 +140,8 @@ extern "C" {
138140
LLAMA_API size_t llama_set_state_data(struct llama_context * ctx, const uint8_t * src);
139141

140142
// Save/load session file
141-
LLAMA_API size_t llama_load_session_file(struct llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out);
142-
LLAMA_API size_t llama_save_session_file(struct llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count);
143+
LLAMA_API bool llama_load_session_file(struct llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out);
144+
LLAMA_API bool llama_save_session_file(struct llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count);
143145

144146
// Run the llama inference to obtain the logits and probabilities for the next token.
145147
// tokens + n_tokens is the provided batch of new tokens to process

0 commit comments

Comments
 (0)