MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / state_seq_load_file

Method state_seq_load_file

smallthinker/src/llama-context.cpp:1691–1732  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1689}
1690
1691size_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) {
1692 llama_file file(filepath, "rb");
1693
1694 // version checks
1695 {
1696 const uint32_t magic = file.read_u32();
1697 const uint32_t version = file.read_u32();
1698
1699 if (magic != LLAMA_STATE_SEQ_MAGIC || version != LLAMA_STATE_SEQ_VERSION) {
1700 LLAMA_LOG_ERROR("%s: unknown (magic, version) for sequence state file: %08x, %08x\n", __func__, magic, version);
1701 return 0;
1702 }
1703 }
1704
1705 // load the prompt
1706 {
1707 const uint32_t n_token_count = file.read_u32();
1708
1709 if (n_token_count > n_token_capacity) {
1710 LLAMA_LOG_ERROR("%s: token count in sequence state file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity);
1711 return 0;
1712 }
1713
1714 file.read_raw(tokens_out, sizeof(llama_token) * n_token_count);
1715 *n_token_count_out = n_token_count;
1716 }
1717
1718 // restore the context state
1719 {
1720 const size_t state_size = file.size() - file.tell();
1721 llama_io_read_file io(&file);
1722 const size_t nread = state_seq_read_data(io, seq_id);
1723 if (!nread) {
1724 LLAMA_LOG_ERROR("%s: failed to restore sequence state\n", __func__);
1725 return 0;
1726 }
1727 GGML_ASSERT(nread <= state_size);
1728 GGML_ASSERT(nread + sizeof(uint32_t) * 3 + sizeof(llama_token) * *n_token_count_out == file.tell());
1729 }
1730
1731 return file.tell();
1732}
1733
1734size_t llama_context::state_seq_save_file(llama_seq_id seq_id, const char * filepath, const llama_token * tokens, size_t n_token_count) {
1735 llama_file file(filepath, "wb");

Callers 1

Calls 4

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

Tested by

no test coverage detected