From e22b2d124635d7f9403b8ee4644e472a29a9b332 Mon Sep 17 00:00:00 2001 From: Kawrakow Date: Sat, 7 Feb 2026 19:20:15 +0200 Subject: [PATCH] Be able to read uint32_t and bool arrays from GGUFs (#1252) --- src/llama-model-loader.cpp | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/llama-model-loader.cpp b/src/llama-model-loader.cpp index 20416f79..4437c1ec 100644 --- a/src/llama-model-loader.cpp +++ b/src/llama-model-loader.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN @@ -551,15 +552,21 @@ bool llama_model_loader::get_arr(const std::string & key, std::vector & resul switch (arr_info.gt) { case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same::value)); break; - case GGUF_TYPE_INT32: GGML_ASSERT( - (std::is_same::value) || - (std::is_same::value)); break; + case GGUF_TYPE_UINT32: + case GGUF_TYPE_BOOL: + case GGUF_TYPE_INT32: GGML_ASSERT((std::is_same_v) || (std::is_same_v)); break; default: - throw std::runtime_error(format("%s is not a float32, int32 array", key.c_str())); + throw std::runtime_error(format("%s is not a float32, int32, uint32 or bool array", key.c_str())); } result.resize(arr_info.length); - result.assign((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length); + if (arr_info.gt == GGUF_TYPE_BOOL) { + std::transform((const bool *)arr_info.data, (const bool *)arr_info.data + arr_info.length, result.begin(), + [] (bool x) { return static_cast(x); }); + + } else { + result.assign((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length); + } return true; } @@ -579,19 +586,24 @@ bool llama_model_loader::get_arr(const std::string & key, std::array & GGUFMeta::GKV::get_kv(meta, kid); switch (arr_info.gt) { - case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same::value)); break; - case GGUF_TYPE_INT32: GGML_ASSERT( - (std::is_same::value) || - (std::is_same::value)); break; + case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same_v)); break; + case GGUF_TYPE_UINT32: + case GGUF_TYPE_BOOL: + case GGUF_TYPE_INT32: GGML_ASSERT((std::is_same_v) || (std::is_same_v)); break; default: - throw std::runtime_error(format("%s is not a float32, int32 array", key.c_str())); + throw std::runtime_error(format("%s is not a float32, int32 array", key.c_str())); } if (arr_info.length > N_MAX) { throw std::runtime_error(format("array length %u for key %s exceeds max %u", (uint32_t) arr_info.length, key.c_str(), (uint32_t) N_MAX)); } - std::copy((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length, result.begin()); + if (arr_info.gt == GGUF_TYPE_BOOL) { + std::transform((const bool *)arr_info.data, (const bool *)arr_info.data + arr_info.length, result.begin(), + [] (bool x) { return static_cast(x); }); + } else { + std::copy((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length, result.begin()); + } return true; }