try to fix loras

This commit is contained in:
Concedo 2024-05-15 16:05:51 +08:00
parent ebbb969526
commit e1e6833a7a
2 changed files with 23 additions and 2 deletions

View file

@ -251,8 +251,8 @@ bool sdtype_load_model(const sd_load_model_inputs inputs) {
if(lorafilename!="" && inputs.lora_multiplier>0)
{
printf("\nApplying LoRA now...\n");
sd_ctx->sd->apply_lora_from_file(lorafilename,inputs.lora_multiplier);
printf("\nSet pending LoRA...\n");
sd_ctx->sd->set_pending_lora(lorafilename,inputs.lora_multiplier);
}
return true;

View file

@ -57,6 +57,9 @@ void calculate_alphas_cumprod(float* alphas_cumprod,
/*=============================================== StableDiffusionGGML ================================================*/
static std::string pending_apply_lora_fname = "";
static float pending_apply_lora_power = 1.0f;
class StableDiffusionGGML {
public:
ggml_backend_t backend = NULL; // general backend
@ -87,6 +90,7 @@ public:
// lora_name => multiplier
std::unordered_map<std::string, float> curr_lora_state;
std::shared_ptr<Denoiser> denoiser = std::make_shared<CompVisDenoiser>();
StableDiffusionGGML() = default;
@ -402,6 +406,11 @@ public:
return result < -1;
}
void set_pending_lora(const std::string& lora_path, float multiplier) {
pending_apply_lora_fname = lora_path;
pending_apply_lora_power = multiplier;
}
void apply_lora_from_file(const std::string& lora_path, float multiplier) {
int64_t t0 = ggml_time_ms();
std::string st_file_path = lora_path;
@ -1394,6 +1403,12 @@ sd_image_t* txt2img(sd_ctx_t* sd_ctx,
int64_t t0 = ggml_time_ms();
sd_ctx->sd->apply_loras(lora_f2m);
if(pending_apply_lora_fname!="" && pending_apply_lora_power>0)
{
printf("\nApplying LoRA now...\n");
sd_ctx->sd->apply_lora_from_file(pending_apply_lora_fname,pending_apply_lora_power);
pending_apply_lora_fname = "";
}
int64_t t1 = ggml_time_ms();
LOG_INFO("apply_loras completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);
struct ggml_init_params params;
@ -1587,6 +1602,12 @@ sd_image_t* img2img(sd_ctx_t* sd_ctx,
// load lora from file
int64_t t0 = ggml_time_ms();
sd_ctx->sd->apply_loras(lora_f2m);
if(pending_apply_lora_fname!="" && pending_apply_lora_power>0)
{
printf("\nApplying LoRA now...\n");
sd_ctx->sd->apply_lora_from_file(pending_apply_lora_fname,pending_apply_lora_power);
pending_apply_lora_fname = "";
}
int64_t t1 = ggml_time_ms();
LOG_INFO("apply_loras completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);