Xuan Son Nguyen slaren commited on
Commit
22e446d
·
1 Parent(s): c022e9a

gguf : enforce that tensor names are unique (llama/6905)

Browse files

* not allow adding duplicated tensor name

* no duplicated tensor while reading gguf

* typo

* throw exception inside llama_model_loader

Co-authored-by: slaren <[email protected]>

---------

Co-authored-by: slaren <[email protected]>

Files changed (1) hide show
  1. ggml.c +12 -0
ggml.c CHANGED
@@ -20890,6 +20890,14 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
20890
  // TODO: return an error instead of crashing with GGML_ASSERT
20891
  gguf_tensor_info_sanitize(info);
20892
 
 
 
 
 
 
 
 
 
20893
  if (!ok) {
20894
  fprintf(stderr, "%s: failed to read tensor info\n", __func__);
20895
  fclose(file);
@@ -21426,6 +21434,10 @@ void gguf_set_kv(struct gguf_context * ctx, struct gguf_context * src) {
21426
  void gguf_add_tensor(
21427
  struct gguf_context * ctx,
21428
  const struct ggml_tensor * tensor) {
 
 
 
 
21429
  const int idx = ctx->header.n_tensors;
21430
  ctx->infos = realloc(ctx->infos, (idx + 1)*sizeof(struct gguf_tensor_info));
21431
 
 
20890
  // TODO: return an error instead of crashing with GGML_ASSERT
20891
  gguf_tensor_info_sanitize(info);
20892
 
20893
+ // make sure there is no duplicated tensor names
20894
+ for (uint64_t j = 0; j < i; ++j) {
20895
+ if (strcmp(info->name.data, ctx->infos[j].name.data) == 0) {
20896
+ fprintf(stderr, "%s: duplicated tensor name %s\n", __func__, info->name.data);
20897
+ ok = false;
20898
+ }
20899
+ }
20900
+
20901
  if (!ok) {
20902
  fprintf(stderr, "%s: failed to read tensor info\n", __func__);
20903
  fclose(file);
 
21434
  void gguf_add_tensor(
21435
  struct gguf_context * ctx,
21436
  const struct ggml_tensor * tensor) {
21437
+ if (gguf_find_tensor(ctx, tensor->name) != -1) {
21438
+ GGML_ASSERT(false && "duplicated tensor name");
21439
+ }
21440
+
21441
  const int idx = ctx->header.n_tensors;
21442
  ctx->infos = realloc(ctx->infos, (idx + 1)*sizeof(struct gguf_tensor_info));
21443