Skip to content

Commit f3140ea

Browse files
authored
fix: tensor loading thread count (leejet#854)
1 parent 98ba155 commit f3140ea

File tree

9 files changed

+21
-19
lines changed

9 files changed

+21
-19
lines changed

conditioner.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ struct FrozenCLIPEmbedderWithCustomWords : public Conditioner {
141141
}
142142
return true;
143143
};
144-
model_loader.load_tensors(on_load);
144+
model_loader.load_tensors(on_load, 1);
145145
readed_embeddings.push_back(embd_name);
146146
if (embd) {
147147
int64_t hidden_size = text_model->model.hidden_size;

control.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ struct ControlNet : public GGMLRunner {
445445
guided_hint_cached = true;
446446
}
447447

448-
bool load_from_file(const std::string& file_path) {
448+
bool load_from_file(const std::string& file_path, int n_threads) {
449449
LOG_INFO("loading control net from '%s'", file_path.c_str());
450450
alloc_params_buffer();
451451
std::map<std::string, ggml_tensor*> tensors;
@@ -458,7 +458,7 @@ struct ControlNet : public GGMLRunner {
458458
return false;
459459
}
460460

461-
bool success = model_loader.load_tensors(tensors, ignore_tensors);
461+
bool success = model_loader.load_tensors(tensors, ignore_tensors, n_threads);
462462

463463
if (!success) {
464464
LOG_ERROR("load control net tensors from model loader failed");

esrgan.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ struct ESRGAN : public GGMLRunner {
164164
return "esrgan";
165165
}
166166

167-
bool load_from_file(const std::string& file_path) {
167+
bool load_from_file(const std::string& file_path, int n_threads) {
168168
LOG_INFO("loading esrgan from '%s'", file_path.c_str());
169169

170170
alloc_params_buffer();
@@ -177,7 +177,7 @@ struct ESRGAN : public GGMLRunner {
177177
return false;
178178
}
179179

180-
bool success = model_loader.load_tensors(esrgan_tensors);
180+
bool success = model_loader.load_tensors(esrgan_tensors, {}, n_threads);
181181

182182
if (!success) {
183183
LOG_ERROR("load esrgan tensors from model loader failed");

lora.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct LoraModel : public GGMLRunner {
116116
return "lora";
117117
}
118118

119-
bool load_from_file(bool filter_tensor = false, int n_threads = 0) {
119+
bool load_from_file(bool filter_tensor, int n_threads) {
120120
LOG_INFO("loading LoRA from '%s'", file_path.c_str());
121121

122122
if (load_failed) {

model.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,8 @@ bool ModelLoader::load_tensors(on_new_tensor_cb_t on_new_tensor_cb, int n_thread
19571957
std::atomic<int64_t> copy_to_backend_time_ms(0);
19581958
std::atomic<int64_t> convert_time_ms(0);
19591959

1960-
int num_threads_to_use = n_threads_p > 0 ? n_threads_p : (int)std::thread::hardware_concurrency();
1960+
int num_threads_to_use = n_threads_p > 0 ? n_threads_p : get_num_physical_cores();
1961+
LOG_DEBUG("using %d threads for model loading", num_threads_to_use);
19611962

19621963
int64_t start_time = ggml_time_ms();
19631964
std::vector<TensorStorage> processed_tensor_storages;

pmid.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ struct PhotoMakerIDEmbed : public GGMLRunner {
591591
return "id_embeds";
592592
}
593593

594-
bool load_from_file(bool filter_tensor = false) {
594+
bool load_from_file(bool filter_tensor, int n_threads) {
595595
LOG_INFO("loading PhotoMaker ID Embeds from '%s'", file_path.c_str());
596596

597597
if (load_failed) {
@@ -623,11 +623,11 @@ struct PhotoMakerIDEmbed : public GGMLRunner {
623623
return true;
624624
};
625625

626-
model_loader->load_tensors(on_new_tensor_cb);
626+
model_loader->load_tensors(on_new_tensor_cb, n_threads);
627627
alloc_params_buffer();
628628

629629
dry_run = false;
630-
model_loader->load_tensors(on_new_tensor_cb);
630+
model_loader->load_tensors(on_new_tensor_cb, n_threads);
631631

632632
LOG_DEBUG("finished loading PhotoMaker ID Embeds ");
633633
return true;

stable-diffusion.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ class StableDiffusionGGML {
531531
}
532532
if (strlen(SAFE_STR(sd_ctx_params->photo_maker_path)) > 0) {
533533
pmid_lora = std::make_shared<LoraModel>(backend, sd_ctx_params->photo_maker_path, "");
534-
if (!pmid_lora->load_from_file(true)) {
534+
if (!pmid_lora->load_from_file(true, n_threads)) {
535535
LOG_WARN("load photomaker lora tensors from %s failed", sd_ctx_params->photo_maker_path);
536536
return false;
537537
}
@@ -599,14 +599,14 @@ class StableDiffusionGGML {
599599
if (!use_tiny_autoencoder) {
600600
vae_params_mem_size = first_stage_model->get_params_buffer_size();
601601
} else {
602-
if (!tae_first_stage->load_from_file(taesd_path)) {
602+
if (!tae_first_stage->load_from_file(taesd_path, n_threads)) {
603603
return false;
604604
}
605605
vae_params_mem_size = tae_first_stage->get_params_buffer_size();
606606
}
607607
size_t control_net_params_mem_size = 0;
608608
if (control_net) {
609-
if (!control_net->load_from_file(SAFE_STR(sd_ctx_params->control_net_path))) {
609+
if (!control_net->load_from_file(SAFE_STR(sd_ctx_params->control_net_path), n_threads)) {
610610
return false;
611611
}
612612
control_net_params_mem_size = control_net->get_params_buffer_size();
@@ -836,7 +836,7 @@ class StableDiffusionGGML {
836836
return;
837837
}
838838
LoraModel lora(backend, file_path, is_high_noise ? "model.high_noise_" : "");
839-
if (!lora.load_from_file()) {
839+
if (!lora.load_from_file(false, n_threads)) {
840840
LOG_WARN("load lora tensors from %s failed", file_path.c_str());
841841
return;
842842
}

tae.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct TinyAutoEncoder : public GGMLRunner {
222222
return "taesd";
223223
}
224224

225-
bool load_from_file(const std::string& file_path) {
225+
bool load_from_file(const std::string& file_path, int n_threads) {
226226
LOG_INFO("loading taesd from '%s', decode_only = %s", file_path.c_str(), decode_only ? "true" : "false");
227227
alloc_params_buffer();
228228
std::map<std::string, ggml_tensor*> taesd_tensors;
@@ -238,7 +238,7 @@ struct TinyAutoEncoder : public GGMLRunner {
238238
return false;
239239
}
240240

241-
bool success = model_loader.load_tensors(taesd_tensors, ignore_tensors);
241+
bool success = model_loader.load_tensors(taesd_tensors, ignore_tensors, n_threads);
242242

243243
if (!success) {
244244
LOG_ERROR("load tae tensors from model loader failed");

upscaler.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ struct UpscalerGGML {
1818
}
1919

2020
bool load_from_file(const std::string& esrgan_path,
21-
bool offload_params_to_cpu) {
21+
bool offload_params_to_cpu,
22+
int n_threads) {
2223
ggml_log_set(ggml_log_callback_default, nullptr);
2324
#ifdef SD_USE_CUDA
2425
LOG_DEBUG("Using CUDA backend");
@@ -54,7 +55,7 @@ struct UpscalerGGML {
5455
if (direct) {
5556
esrgan_upscaler->enable_conv2d_direct();
5657
}
57-
if (!esrgan_upscaler->load_from_file(esrgan_path)) {
58+
if (!esrgan_upscaler->load_from_file(esrgan_path, n_threads)) {
5859
return false;
5960
}
6061
return true;
@@ -124,7 +125,7 @@ upscaler_ctx_t* new_upscaler_ctx(const char* esrgan_path_c_str,
124125
return NULL;
125126
}
126127

127-
if (!upscaler_ctx->upscaler->load_from_file(esrgan_path, offload_params_to_cpu)) {
128+
if (!upscaler_ctx->upscaler->load_from_file(esrgan_path, offload_params_to_cpu, n_threads)) {
128129
delete upscaler_ctx->upscaler;
129130
upscaler_ctx->upscaler = NULL;
130131
free(upscaler_ctx);

0 commit comments

Comments
 (0)