From 567f9f14f0f61c4ca436f45bd86c47d260586448 Mon Sep 17 00:00:00 2001 From: leejet Date: Thu, 18 Sep 2025 00:00:15 +0800 Subject: [PATCH] fix: avoid multithreading issues in the model loader --- model.cpp | 2 ++ pmid.hpp | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/model.cpp b/model.cpp index 168b675..0585e98 100644 --- a/model.cpp +++ b/model.cpp @@ -2427,6 +2427,7 @@ bool ModelLoader::save_to_gguf_file(const std::string& file_path, ggml_type type auto tensor_type_rules = parse_tensor_type_rules(tensor_type_rules_str); + std::mutex tensor_mutex; auto on_new_tensor_cb = [&](const TensorStorage& tensor_storage, ggml_tensor** dst_tensor) -> bool { const std::string& name = tensor_storage.name; ggml_type tensor_type = tensor_storage.type; @@ -2444,6 +2445,7 @@ bool ModelLoader::save_to_gguf_file(const std::string& file_path, ggml_type type tensor_type = dst_type; } + std::lock_guard lock(tensor_mutex); ggml_tensor* tensor = ggml_new_tensor(ggml_ctx, tensor_type, tensor_storage.n_dims, tensor_storage.ne); if (tensor == NULL) { LOG_ERROR("ggml_new_tensor failed"); diff --git a/pmid.hpp b/pmid.hpp index d7daa41..3bd59cd 100644 --- a/pmid.hpp +++ b/pmid.hpp @@ -599,7 +599,8 @@ struct PhotoMakerIDEmbed : public GGMLRunner { return false; } - bool dry_run = true; + bool dry_run = true; + std::mutex tensor_mutex; auto on_new_tensor_cb = [&](const TensorStorage& tensor_storage, ggml_tensor** dst_tensor) -> bool { const std::string& name = tensor_storage.name; @@ -608,6 +609,7 @@ struct PhotoMakerIDEmbed : public GGMLRunner { return true; } if (dry_run) { + std::lock_guard lock(tensor_mutex); struct ggml_tensor* real = ggml_new_tensor(params_ctx, tensor_storage.type, tensor_storage.n_dims,