Skip to content

Commit befa012

Browse files
authored
Merge pull request AUTOMATIC1111#4 from likholat/lora_multiplier
Multiplier for Lora
2 parents ac9c9e1 + d7e45a8 commit befa012

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

scripts/openvino_accelerate.py

+42-54
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Copyright (C) 2023 Intel Corporation
22
# SPDX-License-Identifier: AGPL-3.0
33

4-
import math
54
import cv2
65
import os
76
import torch
@@ -18,17 +17,17 @@
1817
from modules import images, devices, extra_networks, masking, shared
1918
from modules.processing import (
2019
StableDiffusionProcessing, Processed, apply_overlay, apply_color_correction,
21-
get_fixed_seed, create_random_tensors, create_infotext, setup_color_correction,
20+
get_fixed_seed, create_infotext, setup_color_correction,
2221
process_images
2322
)
2423
from modules.sd_models import CheckpointInfo
25-
from modules.shared import Shared, opts, state
24+
from modules.shared import opts, state
2625

2726
from PIL import Image, ImageOps
2827
from pathlib import Path
2928

30-
import openvino.frontend.pytorch.torchdynamo.backend
31-
from openvino.frontend.pytorch.torchdynamo.execute import partitioned_modules, compiled_cache
29+
import openvino.frontend.pytorch.torchdynamo.backend # noqa: F401
30+
from openvino.frontend.pytorch.torchdynamo.execute import partitioned_modules, compiled_cache # noqa: F401
3231
from openvino.runtime import Core
3332

3433
from diffusers import (
@@ -82,7 +81,7 @@ def from_single_file(self, pretrained_model_link_or_path, **kwargs):
8281
text_encoder = kwargs.pop("text_encoder", None)
8382
tokenizer = kwargs.pop("tokenizer", None)
8483
local_config_file = kwargs.pop("local_config_file", None)
85-
84+
8685
torch_dtype = kwargs.pop("torch_dtype", None)
8786

8887
use_safetensors = kwargs.pop("use_safetensors", None if is_safetensors_available() else False)
@@ -135,6 +134,7 @@ def from_single_file(self, pretrained_model_link_or_path, **kwargs):
135134
if file_path.startswith("main/"):
136135
file_path = file_path[len("main/") :]
137136

137+
from huggingface_hub import hf_hub_download
138138
pretrained_model_link_or_path = hf_hub_download(
139139
repo_id,
140140
filename=file_path,
@@ -323,7 +323,6 @@ def init_new(self, all_prompts, all_seeds, all_subseeds):
323323
imgs.append(image)
324324

325325
if len(imgs) == 1:
326-
batch_images = np.expand_dims(imgs[0], axis=0).repeat(self.batch_size, axis=0)
327326
if self.overlay_images is not None:
328327
self.overlay_images = self.overlay_images * self.batch_size
329328

@@ -332,7 +331,6 @@ def init_new(self, all_prompts, all_seeds, all_subseeds):
332331

333332
elif len(imgs) <= self.batch_size:
334333
self.batch_size = len(imgs)
335-
batch_images = np.array(imgs)
336334
else:
337335
raise RuntimeError(f"bad number of images passed: {len(imgs)}; expecting {self.batch_size} or less")
338336

@@ -354,6 +352,7 @@ def process_images_openvino(p: StableDiffusionProcessing, sampler_name, enable_c
354352
subseed = get_fixed_seed(p.subseed)
355353

356354
comments = {}
355+
custom_inputs = {}
357356

358357
p.setup_prompts()
359358

@@ -423,11 +422,13 @@ def infotext(iteration=0, position_in_batch=0):
423422
with devices.autocast():
424423
extra_networks.activate(p, p.extra_network_data)
425424

426-
# TODO: support multiplier
427425
if ('lora' in modules.extra_networks.extra_network_registry):
428426
import lora
429-
for lora_model in lora.loaded_loras:
427+
# TODO: multiple Loras aren't supported for Diffusers now, needs to add warning
428+
if lora.loaded_loras:
429+
lora_model = lora.loaded_loras[0]
430430
shared.sd_diffusers_model.load_lora_weights(os.path.join(os.getcwd(), "models", "Lora"), weight_name=lora_model.name + ".safetensors")
431+
custom_inputs.update(cross_attention_kwargs={"scale" : lora_model.te_multiplier})
431432

432433
if p.scripts is not None:
433434
p.scripts.process_batch(p, batch_number=n, prompts=p.prompts, seeds=p.seeds, subseeds=p.subseeds)
@@ -438,7 +439,6 @@ def infotext(iteration=0, position_in_batch=0):
438439
# strength, which is saved as "Model Strength: 1.0" in the infotext
439440
if n == 0:
440441
with open(os.path.join(paths.data_path, "params.txt"), "w", encoding="utf8") as file:
441-
processed = Processed(p, [], p.seed, "")
442442
file.write(create_infotext(p, p.all_prompts, p.all_seeds, p.all_subseeds, comments=[], position_in_batch=0 % p.batch_size, iteration=0 // p.batch_size))
443443

444444
if p.n_iter > 1:
@@ -449,50 +449,38 @@ def infotext(iteration=0, position_in_batch=0):
449449
time_stamps = []
450450

451451
def callback(iter, t, latents):
452-
time_stamps.append(time.time())
452+
time_stamps.append(time.time()) # noqa: B023
453453

454454
time_stamps.append(time.time())
455+
455456
if (mode == 0):
456-
output = shared.sd_diffusers_model(
457-
prompt=p.prompts,
458-
negative_prompt=p.negative_prompts,
459-
num_inference_steps=p.steps,
460-
guidance_scale=p.cfg_scale,
461-
width = p.width,
462-
height = p.height,
463-
generator=generator,
464-
output_type="np",
465-
callback = callback,
466-
callback_steps = 1,
467-
)
457+
custom_inputs.update({
458+
'width': p.width,
459+
'height': p.height,
460+
})
468461
elif (mode == 1):
469-
output = shared.sd_diffusers_model(
470-
prompt=p.prompts,
471-
negative_prompt=p.negative_prompts,
472-
num_inference_steps=p.steps,
473-
guidance_scale=p.cfg_scale,
474-
image = p.init_images,
475-
strength = p.denoising_strength,
476-
generator=generator,
477-
output_type="np",
478-
callback = callback,
479-
callback_steps = 1,
480-
)
462+
custom_inputs.update({
463+
'image': p.init_images,
464+
'strength':p.denoising_strength,
465+
})
481466
else:
482-
output = shared.sd_diffusers_model(
467+
custom_inputs.update({
468+
'image': p.init_images,
469+
'strength':p.denoising_strength,
470+
'mask_image': p.mask,
471+
})
472+
output = shared.sd_diffusers_model(
483473
prompt=p.prompts,
484474
negative_prompt=p.negative_prompts,
485475
num_inference_steps=p.steps,
486476
guidance_scale=p.cfg_scale,
487-
mask_image = p.mask,
488-
image = p.init_images,
489-
strength = p.denoising_strength,
490477
generator=generator,
491478
output_type="np",
492479
callback = callback,
493480
callback_steps = 1,
494-
)
495-
481+
**custom_inputs
482+
)
483+
496484
model_state.recompile = 0
497485

498486
warmup_duration = time_stamps[1] - time_stamps[0]
@@ -616,21 +604,21 @@ def show(self, is_img2img):
616604

617605
def ui(self, is_img2img):
618606
core = Core()
619-
openvino_device = gr.Dropdown(label="Select a device", choices=[device for device in core.available_devices], value=model_state.device)
607+
openvino_device = gr.Dropdown(label="Select a device", choices=list(core.available_devices), value=model_state.device)
620608
override_sampler = gr.Checkbox(label="Override the sampling selection from the main UI (Recommended as only below sampling methods have been validated for OpenVINO)", value=True)
621609
sampler_name = gr.Radio(label="Select a sampling method", choices=["Euler a", "Euler", "LMS", "Heun", "DPM++ 2M", "LMS Karras", "DPM++ 2M Karras", "DDIM", "PLMS"], value="Euler a")
622610
enable_caching = gr.Checkbox(label="Cache the compiled models on disk for faster model load in subsequent launches (Recommended)", value=True, elem_id=self.elem_id("enable_caching"))
623611
warmup_status = gr.Textbox(label="Device", interactive=False, visible=False)
624-
warmup_note = gr.Markdown(
625-
"""
626-
###
627-
### Note:
628-
First inference involves compilation of the model for best performance.
629-
Excluding the first inference (or warm up inference) is recommended for
630-
performance measurements. When resolution, batchsize, or device is changed,
631-
or samplers like DPM++ or Karras are selected, model is recompiled. Subsequent
632-
iterations use the cached compiled model for faster inference.
633-
""")
612+
gr.Markdown(
613+
"""
614+
###
615+
### Note:
616+
First inference involves compilation of the model for best performance.
617+
Excluding the first inference (or warm up inference) is recommended for
618+
performance measurements. When resolution, batchsize, or device is changed,
619+
or samplers like DPM++ or Karras are selected, model is recompiled. Subsequent
620+
iterations use the cached compiled model for faster inference.
621+
""")
634622

635623
def device_change(choice):
636624
if (model_state.device == choice):
@@ -663,7 +651,7 @@ def run(self, p, openvino_device, override_sampler, sampler_name, enable_caching
663651
processed = process_images_openvino(p, p.sampler_name, enable_caching, openvino_device, mode)
664652
else:
665653
if p.image_mask is None:
666-
mode = 1
654+
mode = 1
667655
else:
668656
mode = 2
669657
p.init = functools.partial(init_new, p)

0 commit comments

Comments
 (0)