Skip to content

Commit 1302318

Browse files
committed
use an unordered_map for faster duplicate tensor name lookups
1 parent 38d3af1 commit 1302318

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

ggml/src/gguf.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cstdlib>
1111
#include <cstring>
1212
#include <map>
13+
#include <unordered_set>
1314
#include <new>
1415
#include <stdexcept>
1516
#include <string>
@@ -486,6 +487,8 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
486487
}
487488

488489
// read the tensor info
490+
std::unordered_map<std::string, int64_t> tensor_names;
491+
489492
for (int64_t i = 0; ok && i < n_tensors; ++i) {
490493
struct gguf_tensor_info info;
491494

@@ -509,12 +512,10 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
509512
ggml_set_name(&info.t, name.c_str());
510513

511514
// make sure there are no duplicate tensor names
512-
for (int64_t j = 0; ok && j < i; ++j) {
513-
if (strcmp(info.t.name, ctx->info[j].t.name) == 0) {
514-
GGML_LOG_ERROR("%s: duplicate tensor name '%s' for tensors %" PRIi64 " and %" PRIi64 "\n", __func__, info.t.name, j, i);
515-
ok = false;
516-
break;
517-
}
515+
auto [it, result] = tensor_names.emplace(info.t.name, i);
516+
if (!result) {
517+
GGML_LOG_ERROR("%s: duplicate tensor name '%s' for tensors %" PRIi64 " and %" PRIi64 "\n", __func__, info.t.name, i, it->second);
518+
ok = false;
518519
}
519520
}
520521
if (!ok) {

0 commit comments

Comments
 (0)