From 509c28f4a1054fe725d8fdfa0d15c7a2fda73928 Mon Sep 17 00:00:00 2001 From: ngxson Date: Sat, 27 Apr 2024 15:31:52 +0200 Subject: [PATCH] no duplicated tensor while reading gguf --- ggml.c | 10 +++++++++- gguf-py/gguf/gguf_reader.py | 8 +++++++- llama.cpp | 8 ++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ggml.c b/ggml.c index 3d4f931df..f2264eedc 100644 --- a/ggml.c +++ b/ggml.c @@ -20824,6 +20824,14 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p gguf_tensor_info_sanitize(info); + // make sure there is no duplicated tensor names + for (uint64_t j = 0; j < i; ++j) { + if (strcmp(info->name.data, ctx->infos[j].name.data) == 0) { + fprintf(stderr, "%s: duplicated tensor name %s\n", __func__, info->name.data); + ok = false; + } + } + if (!ok) { fprintf(stderr, "%s: failed to read tensor info\n", __func__); fclose(file); @@ -21363,7 +21371,7 @@ void gguf_add_tensor( if (gguf_find_tensor(ctx, tensor->name) != -1) { GGML_ASSERT(false && "duplicated tensor name"); } - + const int idx = ctx->header.n_tensors; ctx->infos = realloc(ctx->infos, (idx + 1)*sizeof(struct gguf_tensor_info)); diff --git a/gguf-py/gguf/gguf_reader.py b/gguf-py/gguf/gguf_reader.py index 33afac552..0ec2d27e6 100644 --- a/gguf-py/gguf/gguf_reader.py +++ b/gguf-py/gguf/gguf_reader.py @@ -234,8 +234,14 @@ class GGUFReader: def _build_tensors(self, start_offs: int, fields: list[ReaderField]) -> None: tensors = [] + tensor_names = [] # keep track of name to prevent duplicated tensors for field in fields: _name_len, name_data, _n_dims, dims, raw_dtype, offset_tensor = field.parts + # check if there's any tensor having same name already in the list + tensor_name = str(bytes(name_data), encoding = 'utf-8') + if tensor_name in tensor_names: + raise ValueError(f'Found duplicated tensor with name {tensor_name}') + tensor_names.add(tensor_name) ggml_type = GGMLQuantizationType(raw_dtype[0]) n_elems = np.prod(dims) block_size, type_size = GGML_QUANT_SIZES[ggml_type] @@ -267,7 +273,7 @@ class GGUFReader: item_count = n_bytes item_type = np.uint8 tensors.append(ReaderTensor( - name = str(bytes(name_data), encoding = 'utf-8'), + name = tensor_name, tensor_type = ggml_type, shape = dims, n_elements = n_elems, diff --git a/llama.cpp b/llama.cpp index 3a84b4916..39f7f197c 100644 --- a/llama.cpp +++ b/llama.cpp @@ -3110,9 +3110,17 @@ struct llama_model_loader { fver = (enum llama_fver) gguf_get_version(meta); + std::set tensor_names; for (auto & w : weights) { n_elements += ggml_nelements(w.tensor); n_bytes += ggml_nbytes(w.tensor); + // make sure there is no duplicated tensor names + const std::string name(w.tensor->name); + auto found = tensor_names.find(name); + if (found != tensor_names.end()) { + LLAMA_LOG_ERROR("%s: Found duplicated tensor name %s", __func__, w.tensor->name); + } + tensor_names.insert(name); } LLAMA_LOG_INFO("%s: loaded meta data with %d key-value pairs and %d tensors from %s (version %s)\n",