0019-fix-string-arr-kv-loading.patch 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: jmorganca <jmorganca@gmail.com>
  3. Date: Wed, 5 Mar 2025 17:41:07 -0800
  4. Subject: [PATCH] fix string arr kv loading
  5. ---
  6. ggml/include/gguf.h | 1 +
  7. ggml/src/gguf.cpp | 7 +++++--
  8. src/llama-vocab.cpp | 2 +-
  9. 3 files changed, 7 insertions(+), 3 deletions(-)
  10. diff --git a/ggml/include/gguf.h b/ggml/include/gguf.h
  11. index 79ee2020..3efb22f0 100644
  12. --- a/ggml/include/gguf.h
  13. +++ b/ggml/include/gguf.h
  14. @@ -114,6 +114,7 @@ extern "C" {
  15. // get raw pointer to the first element of the array with the given key_id
  16. // for bool arrays, note that they are always stored as int8 on all platforms (usually this makes no difference)
  17. GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int64_t key_id);
  18. + GGML_API size_t gguf_get_arr_data_n(const struct gguf_context * ctx, int64_t key_id);
  19. // get ith C string from array with given key_id
  20. GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int64_t key_id, size_t i);
  21. diff --git a/ggml/src/gguf.cpp b/ggml/src/gguf.cpp
  22. index ab13669c..f75b923f 100644
  23. --- a/ggml/src/gguf.cpp
  24. +++ b/ggml/src/gguf.cpp
  25. @@ -777,10 +777,14 @@ enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int64_t key_id
  26. const void * gguf_get_arr_data(const struct gguf_context * ctx, int64_t key_id) {
  27. GGML_ASSERT(key_id >= 0 && key_id < gguf_get_n_kv(ctx));
  28. - GGML_ASSERT(ctx->kv[key_id].get_type() != GGUF_TYPE_STRING);
  29. return ctx->kv[key_id].data.data();
  30. }
  31. +size_t gguf_get_arr_data_n(const struct gguf_context * ctx, int64_t key_id) {
  32. + GGML_ASSERT(key_id >= 0 && key_id < gguf_get_n_kv(ctx));
  33. + return ctx->kv[key_id].data.size();
  34. +}
  35. +
  36. const char * gguf_get_arr_str(const struct gguf_context * ctx, int64_t key_id, size_t i) {
  37. GGML_ASSERT(key_id >= 0 && key_id < gguf_get_n_kv(ctx));
  38. GGML_ASSERT(ctx->kv[key_id].get_type() == GGUF_TYPE_STRING);
  39. @@ -874,7 +878,6 @@ const char * gguf_get_val_str(const struct gguf_context * ctx, int64_t key_id) {
  40. const void * gguf_get_val_data(const struct gguf_context * ctx, int64_t key_id) {
  41. GGML_ASSERT(key_id >= 0 && key_id < gguf_get_n_kv(ctx));
  42. GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
  43. - GGML_ASSERT(ctx->kv[key_id].get_type() != GGUF_TYPE_STRING);
  44. return ctx->kv[key_id].data.data();
  45. }
  46. diff --git a/src/llama-vocab.cpp b/src/llama-vocab.cpp
  47. index c7ff28be..7a185443 100644
  48. --- a/src/llama-vocab.cpp
  49. +++ b/src/llama-vocab.cpp
  50. @@ -1443,7 +1443,7 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
  51. const int precompiled_charsmap_keyidx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_PRECOMPILED_CHARSMAP).c_str());
  52. if (precompiled_charsmap_keyidx != -1) {
  53. - size_t n_precompiled_charsmap = gguf_get_arr_n(ctx, precompiled_charsmap_keyidx);
  54. + size_t n_precompiled_charsmap = gguf_get_arr_data_n(ctx, precompiled_charsmap_keyidx);
  55. const char * pc = (const char *) gguf_get_arr_data(ctx, precompiled_charsmap_keyidx);
  56. precompiled_charsmap.assign(pc, pc + n_precompiled_charsmap);
  57. #ifdef IS_BIG_ENDIAN