1
1
# Copyright (C) 2023 Intel Corporation
2
2
# SPDX-License-Identifier: AGPL-3.0
3
3
4
- import math
5
4
import cv2
6
5
import os
7
6
import torch
18
17
from modules import images , devices , extra_networks , masking , shared
19
18
from modules .processing import (
20
19
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 ,
22
21
process_images
23
22
)
24
23
from modules .sd_models import CheckpointInfo
25
- from modules .shared import Shared , opts , state
24
+ from modules .shared import opts , state
26
25
27
26
from PIL import Image , ImageOps
28
27
from pathlib import Path
29
28
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
32
31
from openvino .runtime import Core
33
32
34
33
from diffusers import (
@@ -82,7 +81,7 @@ def from_single_file(self, pretrained_model_link_or_path, **kwargs):
82
81
text_encoder = kwargs .pop ("text_encoder" , None )
83
82
tokenizer = kwargs .pop ("tokenizer" , None )
84
83
local_config_file = kwargs .pop ("local_config_file" , None )
85
-
84
+
86
85
torch_dtype = kwargs .pop ("torch_dtype" , None )
87
86
88
87
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):
135
134
if file_path .startswith ("main/" ):
136
135
file_path = file_path [len ("main/" ) :]
137
136
137
+ from huggingface_hub import hf_hub_download
138
138
pretrained_model_link_or_path = hf_hub_download (
139
139
repo_id ,
140
140
filename = file_path ,
@@ -323,7 +323,6 @@ def init_new(self, all_prompts, all_seeds, all_subseeds):
323
323
imgs .append (image )
324
324
325
325
if len (imgs ) == 1 :
326
- batch_images = np .expand_dims (imgs [0 ], axis = 0 ).repeat (self .batch_size , axis = 0 )
327
326
if self .overlay_images is not None :
328
327
self .overlay_images = self .overlay_images * self .batch_size
329
328
@@ -332,7 +331,6 @@ def init_new(self, all_prompts, all_seeds, all_subseeds):
332
331
333
332
elif len (imgs ) <= self .batch_size :
334
333
self .batch_size = len (imgs )
335
- batch_images = np .array (imgs )
336
334
else :
337
335
raise RuntimeError (f"bad number of images passed: { len (imgs )} ; expecting { self .batch_size } or less" )
338
336
@@ -354,6 +352,7 @@ def process_images_openvino(p: StableDiffusionProcessing, sampler_name, enable_c
354
352
subseed = get_fixed_seed (p .subseed )
355
353
356
354
comments = {}
355
+ custom_inputs = {}
357
356
358
357
p .setup_prompts ()
359
358
@@ -423,11 +422,13 @@ def infotext(iteration=0, position_in_batch=0):
423
422
with devices .autocast ():
424
423
extra_networks .activate (p , p .extra_network_data )
425
424
426
- # TODO: support multiplier
427
425
if ('lora' in modules .extra_networks .extra_network_registry ):
428
426
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 ]
430
430
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 })
431
432
432
433
if p .scripts is not None :
433
434
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):
438
439
# strength, which is saved as "Model Strength: 1.0" in the infotext
439
440
if n == 0 :
440
441
with open (os .path .join (paths .data_path , "params.txt" ), "w" , encoding = "utf8" ) as file :
441
- processed = Processed (p , [], p .seed , "" )
442
442
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 ))
443
443
444
444
if p .n_iter > 1 :
@@ -449,50 +449,38 @@ def infotext(iteration=0, position_in_batch=0):
449
449
time_stamps = []
450
450
451
451
def callback (iter , t , latents ):
452
- time_stamps .append (time .time ())
452
+ time_stamps .append (time .time ()) # noqa: B023
453
453
454
454
time_stamps .append (time .time ())
455
+
455
456
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
+ })
468
461
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
+ })
481
466
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 (
483
473
prompt = p .prompts ,
484
474
negative_prompt = p .negative_prompts ,
485
475
num_inference_steps = p .steps ,
486
476
guidance_scale = p .cfg_scale ,
487
- mask_image = p .mask ,
488
- image = p .init_images ,
489
- strength = p .denoising_strength ,
490
477
generator = generator ,
491
478
output_type = "np" ,
492
479
callback = callback ,
493
480
callback_steps = 1 ,
494
- )
495
-
481
+ ** custom_inputs
482
+ )
483
+
496
484
model_state .recompile = 0
497
485
498
486
warmup_duration = time_stamps [1 ] - time_stamps [0 ]
@@ -616,21 +604,21 @@ def show(self, is_img2img):
616
604
617
605
def ui (self , is_img2img ):
618
606
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 )
620
608
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 )
621
609
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" )
622
610
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" ))
623
611
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
+ """ )
634
622
635
623
def device_change (choice ):
636
624
if (model_state .device == choice ):
@@ -663,7 +651,7 @@ def run(self, p, openvino_device, override_sampler, sampler_name, enable_caching
663
651
processed = process_images_openvino (p , p .sampler_name , enable_caching , openvino_device , mode )
664
652
else :
665
653
if p .image_mask is None :
666
- mode = 1
654
+ mode = 1
667
655
else :
668
656
mode = 2
669
657
p .init = functools .partial (init_new , p )
0 commit comments