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

Method llama_model_loader

smallthinker/src/llama-model-loader.cpp:443–691  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

441 template bool llama_model_loader::get_key_or_arr<std::array<uint32_t, 512>>(enum llm_kv kid, std::array<uint32_t, 512> & result, uint32_t n, bool required);
442
443llama_model_loader::llama_model_loader(
444 const std::string & fname,
445 std::vector<std::string> & splits,
446 bool use_mmap,
447 bool check_tensors,
448 const llama_model_kv_override * param_overrides_p,
449 const llama_model_tensor_buft_override * param_tensor_buft_overrides_p) {
450 int trace = 0;
451 if (getenv("LLAMA_TRACE")) {
452 trace = atoi(getenv("LLAMA_TRACE"));
453 }
454
455 if (param_overrides_p != nullptr) {
456 for (const struct llama_model_kv_override * p = param_overrides_p; p->key[0] != 0; p++) {
457 kv_overrides.insert({std::string(p->key), *p});
458 }
459 }
460
461 tensor_buft_overrides = param_tensor_buft_overrides_p;
462
463 // Load the main GGUF
464 struct ggml_context * ctx = NULL;
465 struct gguf_init_params params = {
466 /*.no_alloc = */ true,
467 /*.ctx = */ &ctx,
468 };
469
470 meta.reset(gguf_init_from_file(fname.c_str(), params));
471 if (!meta) {
472 throw std::runtime_error(format("%s: failed to load model from %s", __func__, fname.c_str()));
473 }
474
475 get_key(llm_kv(LLM_KV_GENERAL_ARCHITECTURE), arch_name, false);
476 llm_kv = LLM_KV(llm_arch_from_string(arch_name));
477
478 files.emplace_back(new llama_file(fname.c_str(), "rb"));
479 contexts.emplace_back(ctx);
480
481 // Save tensors data offset of the main file.
482 // For subsidiary files, `meta` tensor data offset must not be used,
483 // so we build a unified tensors index for weights.
484 for (ggml_tensor * cur = ggml_get_first_tensor(ctx); cur; cur = ggml_get_next_tensor(ctx, cur)) {
485 std::string tensor_name = std::string(cur->name);
486 // make sure there is no duplicated tensor names
487 if (weights_map.find(tensor_name) != weights_map.end()) {
488 throw std::runtime_error(format("invalid model: tensor '%s' is duplicated", ggml_get_name(cur)));
489 }
490 n_elements += ggml_nelements(cur);
491 n_bytes += ggml_nbytes(cur);
492 weights_map.emplace(tensor_name, llama_tensor_weight(files.back().get(), 0, meta.get(), cur));
493 }
494 uint16_t n_split = 0;
495 get_key(llm_kv(LLM_KV_SPLIT_COUNT), n_split, false);
496
497 // Load additional GGML contexts
498 if (n_split > 1) {
499 // make sure the main file is loaded first
500 uint16_t idx = 0;

Callers

nothing calls this directly

Calls 15

llama_tensor_weightClass · 0.85
llama_get_list_splitsFunction · 0.85
findMethod · 0.80
substrMethod · 0.80
formatFunction · 0.70
llm_kvEnum · 0.70
LLM_KVClass · 0.70
llm_arch_from_stringFunction · 0.70
llama_file_version_nameFunction · 0.70
gguf_kv_to_strFunction · 0.70
replace_allFunction · 0.70

Tested by

no test coverage detected