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

Function llama_copy_state_data_internal

llama.cpp:10247–10360  ·  view source on GitHub ↗

copy state data into either a buffer or file depending on the passed in context * * file context: * llama_file file("/path", "wb"); * llama_data_file_context data_ctx(&file); * llama_copy_state_data(ctx, &data_ctx); * * buffer context: * std::vector buf(max_size, 0); * llama_data_buffer_context data_ctx(&buf.data()); * llama_copy_state_data(ctx, &data_ctx); * */

Source from the content-addressed store, hash-verified

10245 *
10246*/
10247static void llama_copy_state_data_internal(struct llama_context * ctx, llama_data_context * data_ctx) {
10248 // copy rng
10249 {
10250 std::stringstream rng_ss;
10251 rng_ss << ctx->rng;
10252
10253 const size_t rng_size = rng_ss.str().size();
10254 char rng_buf[LLAMA_MAX_RNG_STATE];
10255
10256 memset(&rng_buf[0], 0, LLAMA_MAX_RNG_STATE);
10257 memcpy(&rng_buf[0], rng_ss.str().data(), rng_ss.str().size());
10258
10259 data_ctx->write(&rng_size, sizeof(rng_size));
10260 data_ctx->write(&rng_buf[0], LLAMA_MAX_RNG_STATE);
10261 }
10262
10263 // copy logits
10264 {
10265 const size_t logits_cap = ctx->logits.capacity();
10266 const size_t logits_size = ctx->logits.size();
10267
10268 data_ctx->write(&logits_cap, sizeof(logits_cap));
10269 data_ctx->write(&logits_size, sizeof(logits_size));
10270
10271 if (logits_size) {
10272 data_ctx->write(ctx->logits.data(), logits_size * sizeof(float));
10273 }
10274
10275 // If there is a gap between the size and the capacity, write padding
10276 size_t padding_size = (logits_cap - logits_size) * sizeof(float);
10277 if (padding_size > 0) {
10278 std::vector<uint8_t> padding(padding_size, 0); // Create a buffer filled with zeros
10279 data_ctx->write(padding.data(), padding_size);
10280 }
10281 }
10282
10283 // copy embeddings
10284 {
10285 const size_t embedding_size = ctx->embedding.size();
10286
10287 data_ctx->write(&embedding_size, sizeof(embedding_size));
10288
10289 if (embedding_size) {
10290 data_ctx->write(ctx->embedding.data(), embedding_size * sizeof(float));
10291 }
10292 }
10293
10294 // copy kv cache
10295 {
10296 const auto & kv_self = ctx->kv_self;
10297 const auto & hparams = ctx->model.hparams;
10298 const auto & cparams = ctx->cparams;
10299
10300 const auto n_layer = hparams.n_layer;
10301 const auto n_embd = hparams.n_embd_gqa();
10302 const auto n_ctx = cparams.n_ctx;
10303
10304 const size_t kv_buf_size = kv_self.buf.size;

Callers 2

llama_copy_state_dataFunction · 0.85
llama_save_session_fileFunction · 0.85

Calls 15

ggml_element_sizeFunction · 0.70
ggml_initFunction · 0.70
ggml_tensor_overheadFunction · 0.70
ggml_graph_overheadFunction · 0.70
ggml_new_graphFunction · 0.70
ggml_new_tensor_3dFunction · 0.70
ggml_nbytesFunction · 0.70
ggml_view_3dFunction · 0.70
ggml_cpyFunction · 0.70
ggml_freeFunction · 0.70

Tested by

no test coverage detected