Skip to content

Commit 567f9f1

Browse files
committed
fix: avoid multithreading issues in the model loader
1 parent 1e5f207 commit 567f9f1

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

model.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,7 @@ bool ModelLoader::save_to_gguf_file(const std::string& file_path, ggml_type type
24272427

24282428
auto tensor_type_rules = parse_tensor_type_rules(tensor_type_rules_str);
24292429

2430+
std::mutex tensor_mutex;
24302431
auto on_new_tensor_cb = [&](const TensorStorage& tensor_storage, ggml_tensor** dst_tensor) -> bool {
24312432
const std::string& name = tensor_storage.name;
24322433
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
24442445
tensor_type = dst_type;
24452446
}
24462447

2448+
std::lock_guard<std::mutex> lock(tensor_mutex);
24472449
ggml_tensor* tensor = ggml_new_tensor(ggml_ctx, tensor_type, tensor_storage.n_dims, tensor_storage.ne);
24482450
if (tensor == NULL) {
24492451
LOG_ERROR("ggml_new_tensor failed");

pmid.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ struct PhotoMakerIDEmbed : public GGMLRunner {
599599
return false;
600600
}
601601

602-
bool dry_run = true;
602+
bool dry_run = true;
603+
std::mutex tensor_mutex;
603604
auto on_new_tensor_cb = [&](const TensorStorage& tensor_storage, ggml_tensor** dst_tensor) -> bool {
604605
const std::string& name = tensor_storage.name;
605606

@@ -608,6 +609,7 @@ struct PhotoMakerIDEmbed : public GGMLRunner {
608609
return true;
609610
}
610611
if (dry_run) {
612+
std::lock_guard<std::mutex> lock(tensor_mutex);
611613
struct ggml_tensor* real = ggml_new_tensor(params_ctx,
612614
tensor_storage.type,
613615
tensor_storage.n_dims,

0 commit comments

Comments
 (0)