Skip to content

Commit f4b5326

Browse files
wbrunaleejet
andauthored
feat: support for merging LoRA weights on model conversion (#2079)
Co-authored-by: leejet <leejet714@gmail.com>
1 parent bbdcad4 commit f4b5326

7 files changed

Lines changed: 279 additions & 23 deletions

File tree

‎examples/cli/main.cpp‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,15 @@ int main(int argc, const char* argv[]) {
716716
}
717717

718718
if (cli_params.mode == CONVERT) {
719+
std::vector<sd_lora_t> loras;
720+
loras.reserve(gen_params.lora_map.size() + gen_params.high_noise_lora_map.size());
721+
for (const auto& [path, multiplier] : gen_params.lora_map) {
722+
loras.push_back({false, multiplier, path.c_str()});
723+
}
724+
for (const auto& [path, multiplier] : gen_params.high_noise_lora_map) {
725+
loras.push_back({true, multiplier, path.c_str()});
726+
}
727+
719728
bool success = convert_with_components(ctx_params.model_path.c_str(),
720729
ctx_params.clip_l_path.c_str(),
721730
ctx_params.clip_g_path.c_str(),
@@ -726,7 +735,9 @@ int main(int argc, const char* argv[]) {
726735
ctx_params.wtype,
727736
ctx_params.tensor_type_rules.c_str(),
728737
cli_params.convert_name,
729-
ctx_params.n_threads);
738+
ctx_params.n_threads,
739+
loras.empty() ? nullptr : loras.data(),
740+
(int)loras.size());
730741
if (!success) {
731742
LOG_ERROR("convert '%s'/'%s' to '%s' failed",
732743
ctx_params.model_path.c_str(),

‎examples/common/common.cpp‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,14 +2271,15 @@ bool SDGenerationParams::from_json_str(
22712271
return true;
22722272
}
22732273

2274-
void SDGenerationParams::extract_and_remove_lora(const std::string& lora_model_dir) {
2274+
bool SDGenerationParams::extract_and_remove_lora(const std::string& lora_model_dir) {
22752275
if (lora_model_dir.empty()) {
2276-
return;
2276+
return true;
22772277
}
22782278
static const std::regex re(R"(<lora:([^:>]+):([^>]+)>)");
22792279
static const std::vector<std::string> valid_ext = {".gguf", ".safetensors", ".pt", ".ckpt"};
22802280
std::smatch m;
22812281

2282+
bool missing = false;
22822283
std::string tmp = prompt;
22832284

22842285
while (std::regex_search(tmp, m, re)) {
@@ -2320,8 +2321,9 @@ void SDGenerationParams::extract_and_remove_lora(const std::string& lora_model_d
23202321
}
23212322
if (!found) {
23222323
LOG_WARN("can not found lora %s", final_path.lexically_normal().string().c_str());
2323-
tmp = m.suffix().str();
2324-
prompt = std::regex_replace(prompt, re, "", std::regex_constants::format_first_only);
2324+
tmp = m.suffix().str();
2325+
prompt = std::regex_replace(prompt, re, "", std::regex_constants::format_first_only);
2326+
missing = true;
23252327
continue;
23262328
}
23272329
}
@@ -2337,6 +2339,8 @@ void SDGenerationParams::extract_and_remove_lora(const std::string& lora_model_d
23372339

23382340
tmp = m.suffix().str();
23392341
}
2342+
2343+
return !missing;
23402344
}
23412345

23422346
bool SDGenerationParams::width_and_height_are_set() const {
@@ -2458,7 +2462,7 @@ bool SDGenerationParams::initialize_cache_params() {
24582462
return true;
24592463
}
24602464

2461-
bool SDGenerationParams::resolve(const std::string& lora_model_dir, const std::string& hires_upscalers_dir, bool strict) {
2465+
bool SDGenerationParams::resolve(const std::string& lora_model_dir, const std::string& hires_upscalers_dir, bool strict, bool validate_missing_loras) {
24622466
vae_tiling_params.extra_tiling_args = extra_tiling_args.empty() ? nullptr : extra_tiling_args.c_str();
24632467

24642468
if (high_noise_sample_params.sample_steps <= 0) {
@@ -2502,7 +2506,9 @@ bool SDGenerationParams::resolve(const std::string& lora_model_dir, const std::s
25022506

25032507
prompt_with_lora = prompt;
25042508
if (!lora_model_dir.empty()) {
2505-
extract_and_remove_lora(lora_model_dir);
2509+
if (!extract_and_remove_lora(lora_model_dir) && validate_missing_loras) {
2510+
return false;
2511+
}
25062512
}
25072513
return true;
25082514
}
@@ -2630,7 +2636,7 @@ bool SDGenerationParams::resolve_and_validate(SDMode mode,
26302636
const std::string& lora_model_dir,
26312637
const std::string& hires_upscalers_dir,
26322638
bool strict) {
2633-
if (!resolve(lora_model_dir, hires_upscalers_dir, strict)) {
2639+
if (!resolve(lora_model_dir, hires_upscalers_dir, strict, mode == CONVERT)) {
26342640
return false;
26352641
}
26362642
if (!validate(mode)) {

‎examples/common/common.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,12 @@ struct SDGenerationParams {
313313
const std::function<std::string(const std::string&)>& lora_path_resolver = {});
314314
bool parse_image_preprocess_json(const std::string& json_str);
315315
bool initialize_cache_params();
316-
void extract_and_remove_lora(const std::string& lora_model_dir);
316+
bool extract_and_remove_lora(const std::string& lora_model_dir);
317317
bool width_and_height_are_set() const;
318318
void set_width_and_height_if_unset(int w, int h);
319319
int get_resolved_width() const;
320320
int get_resolved_height() const;
321-
bool resolve(const std::string& lora_model_dir, const std::string& hires_upscalers_dir, bool strict = false);
321+
bool resolve(const std::string& lora_model_dir, const std::string& hires_upscalers_dir, bool strict = false, bool validate_missing_loras = false);
322322
bool validate(SDMode mode);
323323
bool resolve_and_validate(SDMode mode,
324324
const std::string& lora_model_dir,

‎include/stable-diffusion.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,9 @@ SD_API bool convert_with_components(const char* model_path,
601601
enum sd_type_t output_type,
602602
const char* tensor_type_rules,
603603
bool convert_name,
604-
int n_threads);
604+
int n_threads,
605+
const sd_lora_t* loras,
606+
int lora_count);
605607

606608
SD_API bool preprocess_canny(sd_image_t image,
607609
float high_threshold,

0 commit comments

Comments
 (0)