Skip to content

Commit 34c460d

Browse files
committed
fix for some sdxl lora, like lcm-lora-xl
1 parent b9257f4 commit 34c460d

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

examples/cli/main.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -686,27 +686,26 @@ int main(int argc, const char* argv[]) {
686686
// Resize input image ...
687687
if (params.height % 64 != 0 || params.width % 64 != 0) {
688688
int resized_height = params.height + (64 - params.height % 64);
689-
int resized_width = params.width + (64 - params.width % 64);
689+
int resized_width = params.width + (64 - params.width % 64);
690690

691-
uint8_t *resized_image_buffer = (uint8_t *)malloc(resized_height * resized_width * 3);
691+
uint8_t* resized_image_buffer = (uint8_t*)malloc(resized_height * resized_width * 3);
692692
if (resized_image_buffer == NULL) {
693693
fprintf(stderr, "error: allocate memory for resize input image\n");
694694
free(input_image_buffer);
695695
return 1;
696696
}
697-
stbir_resize(input_image_buffer, params.width, params.height, 0,
698-
resized_image_buffer, resized_width, resized_height, 0, STBIR_TYPE_UINT8,
699-
3 /*RGB channel*/, STBIR_ALPHA_CHANNEL_NONE, 0,
700-
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
701-
STBIR_FILTER_BOX, STBIR_FILTER_BOX,
702-
STBIR_COLORSPACE_SRGB, nullptr
703-
);
697+
stbir_resize(input_image_buffer, params.width, params.height, 0,
698+
resized_image_buffer, resized_width, resized_height, 0, STBIR_TYPE_UINT8,
699+
3 /*RGB channel*/, STBIR_ALPHA_CHANNEL_NONE, 0,
700+
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
701+
STBIR_FILTER_BOX, STBIR_FILTER_BOX,
702+
STBIR_COLORSPACE_SRGB, nullptr);
704703

705704
// Save resized result
706705
free(input_image_buffer);
707706
input_image_buffer = resized_image_buffer;
708-
params.height = resized_height;
709-
params.width = resized_width;
707+
params.height = resized_height;
708+
params.width = resized_width;
710709
}
711710
}
712711

lora.hpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct LoraModel : public GGMLModule {
1111
std::string file_path;
1212
ModelLoader model_loader;
1313
bool load_failed = false;
14-
bool applied = false;
14+
bool applied = false;
1515

1616
LoraModel(ggml_backend_t backend,
1717
ggml_type wtype,
@@ -91,10 +91,15 @@ struct LoraModel : public GGMLModule {
9191
k_tensor = k_tensor.substr(0, k_pos);
9292
replace_all_chars(k_tensor, '.', '_');
9393
// LOG_DEBUG("k_tensor %s", k_tensor.c_str());
94-
// if (k_tensor == "model_diffusion_model_output_blocks_2_2_conv") { // fix for SDXL
95-
// k_tensor = "model_diffusion_model_output_blocks_2_1_conv";
96-
// }
97-
std::string lora_up_name = "lora." + k_tensor + ".lora_up.weight";
94+
std::string lora_up_name = "lora." + k_tensor + ".lora_up.weight";
95+
if (lora_tensors.find(lora_up_name) == lora_tensors.end()) {
96+
if (k_tensor == "model_diffusion_model_output_blocks_2_2_conv") {
97+
// fix for some sdxl lora, like lcm-lora-xl
98+
k_tensor = "model_diffusion_model_output_blocks_2_1_conv";
99+
lora_up_name = "lora." + k_tensor + ".lora_up.weight";
100+
}
101+
}
102+
98103
std::string lora_down_name = "lora." + k_tensor + ".lora_down.weight";
99104
std::string alpha_name = "lora." + k_tensor + ".alpha";
100105
std::string scale_name = "lora." + k_tensor + ".scale";

model.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,11 @@ std::string convert_tensor_name(const std::string& name) {
457457
}
458458
pos = new_name.rfind("lora");
459459
if (pos != std::string::npos) {
460-
std::string name_without_network_parts = new_name.substr(0, pos-1);
460+
std::string name_without_network_parts = new_name.substr(0, pos - 1);
461461
std::string network_part = new_name.substr(pos);
462462
// LOG_DEBUG("%s %s", name_without_network_parts.c_str(), network_part.c_str());
463463
std::string new_key = convert_diffusers_name_to_compvis(name_without_network_parts, '.');
464-
new_key = convert_sdxl_lora_name(new_key);
464+
new_key = convert_sdxl_lora_name(new_key);
465465
replace_all_chars(new_key, '.', '_');
466466
size_t npos = network_part.rfind("_linear_layer");
467467
if (npos != std::string::npos) {

stable-diffusion.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ sd_image_t* txt2img(sd_ctx_t* sd_ctx,
16101610
if (sd_ctx->sd->stacked_id && !sd_ctx->sd->pmid_lora->applied) {
16111611
t0 = ggml_time_ms();
16121612
sd_ctx->sd->pmid_lora->apply(sd_ctx->sd->tensors, sd_ctx->sd->n_threads);
1613-
t1 = ggml_time_ms();
1613+
t1 = ggml_time_ms();
16141614
sd_ctx->sd->pmid_lora->applied = true;
16151615
LOG_INFO("pmid_lora apply completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);
16161616
if (sd_ctx->sd->free_params_immediately) {

0 commit comments

Comments
 (0)