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

Function llama_build_graph

llama.cpp:6408–6753  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6406static llm_offload_trie k_offload_func_trie(k_offload_map);
6407
6408static 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

Callers 2

llama_decode_internalFunction · 0.85

Calls 15

ggml_allocr_is_measureFunction · 0.85
ggml_allocr_allocFunction · 0.85
findMethod · 0.80
build_llama_variantsMethod · 0.80
build_baichuanMethod · 0.80
build_falconMethod · 0.80
build_starcoderMethod · 0.80
build_persimmonMethod · 0.80
build_refactMethod · 0.80
build_bloomMethod · 0.80
build_mptMethod · 0.80
build_stablelmMethod · 0.80

Tested by

no test coverage detected