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

Method get_key_or_arr

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

Source from the content-addressed store, hash-verified

393 // get array of n <= N_MAX elements, or a single element repeated n times
394 template<typename T, size_t N_MAX>
395 bool llama_model_loader::get_key_or_arr(const std::string & key, std::array<T, N_MAX> & result, uint32_t n, bool required) {
396 const int kid = gguf_find_key(meta.get(), key.c_str());
397
398 if (kid < 0) {
399 if (required) {
400 throw std::runtime_error(format("key not found in model: %s", key.c_str()));
401 }
402 return false;
403 }
404
405 if (n > N_MAX) {
406 throw std::runtime_error(format("n > N_MAX: %u > %u for key %s", (uint32_t) n, (uint32_t) N_MAX, key.c_str()));
407 }
408
409 if (gguf_get_kv_type(meta.get(), kid) == GGUF_TYPE_ARRAY) {
410 struct GGUFMeta::ArrayInfo arr_info =
411 GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(meta.get(), kid);
412
413 if (n != arr_info.length) {
414 throw std::runtime_error(format("key %s has wrong array length; expected %u, got %u", key.c_str(), n, (uint32_t) arr_info.length));
415 }
416
417 return get_arr(key, result, required);
418 }
419
420 T value;
421
422 bool ok = get_key(key, value, required);
423 if (!ok) {
424 return false;
425 }
426
427 for (uint32_t i = 0; i < n; i++) {
428 result[i] = value;
429 }
430
431 return true;
432 }
433
434 template<typename T>
435 bool llama_model_loader::get_key_or_arr(enum llm_kv kid, T & result, uint32_t n, bool required) {

Callers 1

load_hparamsMethod · 0.80

Calls 6

formatFunction · 0.70
llm_kvEnum · 0.70
gguf_find_keyFunction · 0.50
gguf_get_kv_typeFunction · 0.50
getMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected