MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / state_seq_load_file

Method state_seq_load_file

subprojects/llama.cpp/src/llama-context.cpp:2428–2469  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2426}
2427
2428size_t llama_context::state_seq_load_file(llama_seq_id seq_id, const char * filepath, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
2429 llama_file file(filepath, "rb");
2430
2431 // version checks
2432 {
2433 const uint32_t magic = file.read_u32();
2434 const uint32_t version = file.read_u32();
2435
2436 if (magic != LLAMA_STATE_SEQ_MAGIC || version != LLAMA_STATE_SEQ_VERSION) {
2437 LLAMA_LOG_ERROR("%s: unknown (magic, version) for sequence state file: %08x, %08x\n", __func__, magic, version);
2438 return 0;
2439 }
2440 }
2441
2442 // load the prompt
2443 {
2444 const uint32_t n_token_count = file.read_u32();
2445
2446 if (n_token_count > n_token_capacity) {
2447 LLAMA_LOG_ERROR("%s: token count in sequence state file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity);
2448 return 0;
2449 }
2450
2451 file.read_raw(tokens_out, sizeof(llama_token) * n_token_count);
2452 *n_token_count_out = n_token_count;
2453 }
2454
2455 // restore the context state
2456 {
2457 const size_t state_size = file.size() - file.tell();
2458 llama_io_read_file io(&file);
2459 const size_t nread = state_seq_read_data(io, seq_id, 0);
2460 if (!nread) {
2461 LLAMA_LOG_ERROR("%s: failed to restore sequence state\n", __func__);
2462 return 0;
2463 }
2464 GGML_ASSERT(nread <= state_size);
2465 GGML_ASSERT(nread + sizeof(uint32_t) * 3 + sizeof(llama_token) * *n_token_count_out == file.tell());
2466 }
2467
2468 return file.tell();
2469}
2470
2471size_t llama_context::state_seq_save_file(llama_seq_id seq_id, const char * filepath, const llama_token * tokens, size_t n_token_count) {
2472 llama_file file(filepath, "wb");

Callers 1

Calls 4

sizeMethod · 0.65
read_u32Method · 0.45
read_rawMethod · 0.45
tellMethod · 0.45

Tested by

no test coverage detected