| 6406 | static llm_offload_trie k_offload_func_trie(k_offload_map); |
| 6407 | |
| 6408 | static struct ggml_cgraph * llama_build_graph( |
| 6409 | llama_context & lctx, |
| 6410 | const llama_batch & batch) { |
| 6411 | const auto & model = lctx.model; |
| 6412 | |
| 6413 | // check if we should build the worst-case graph (for memory measurement) |
| 6414 | const bool worst_case = ggml_allocr_is_measure(lctx.alloc); |
| 6415 | |
| 6416 | // keep track of the input that has already been allocated |
| 6417 | bool alloc_inp_tokens = false; |
| 6418 | bool alloc_inp_embd = false; |
| 6419 | bool alloc_inp_pos = false; |
| 6420 | bool alloc_inp_KQ_scale = false; |
| 6421 | bool alloc_inp_KQ_mask = false; |
| 6422 | bool alloc_inp_K_shift = false; |
| 6423 | |
| 6424 | #ifdef GGML_USE_CUBLAS |
| 6425 | const bool do_offload = true; |
| 6426 | #else |
| 6427 | const bool do_offload = true; // TODO: set to false after finishing refactoring |
| 6428 | #endif |
| 6429 | |
| 6430 | int n_non_view = 0; // number of non-view tensors that have been processed by the callback |
| 6431 | |
| 6432 | // this callback allows us to apply custom logic to each tensor (e.g. ggml-alloc, offloading, etc.) |
| 6433 | // TODO: will be removed with backend v2 |
| 6434 | |
| 6435 | // For sparse deriv, we offload layers from the starting layer to the end. |
| 6436 | // For dense deriv, we offload layers from the end to the starting layer. |
| 6437 | bool offload_starting_layers = lctx.model.sparse_deriv; |
| 6438 | |
| 6439 | llm_build_cb cb = [&](struct ggml_tensor * cur, const char * name, int il) { |
| 6440 | if (il >= 0) { |
| 6441 | ggml_format_name(cur, "%s-%d", name, il); |
| 6442 | } else { |
| 6443 | ggml_set_name(cur, name); |
| 6444 | } |
| 6445 | |
| 6446 | // |
| 6447 | // allocate input tensors and set input data |
| 6448 | // |
| 6449 | // TODO: will be removed with backend v2 |
| 6450 | |
| 6451 | if (!alloc_inp_tokens && strcmp(name, "inp_tokens") == 0) { |
| 6452 | ggml_allocr_alloc(lctx.alloc, cur); |
| 6453 | |
| 6454 | if (!ggml_allocr_is_measure(lctx.alloc) && batch.token) { |
| 6455 | const int64_t n_tokens = cur->ne[0]; |
| 6456 | |
| 6457 | memcpy(cur->data, batch.token, n_tokens*ggml_element_size(cur)); |
| 6458 | } |
| 6459 | |
| 6460 | alloc_inp_tokens = true; |
| 6461 | } |
| 6462 | |
| 6463 | if (!alloc_inp_embd && strcmp(name, "inp_embd") == 0) { |
| 6464 | ggml_allocr_alloc(lctx.alloc, cur); |
| 6465 |
no test coverage detected