Upload extensions_built_in/advanced_generator/PureLoraGenerator.py with huggingface_hub
Browse files
extensions_built_in/advanced_generator/PureLoraGenerator.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
|
| 4 |
+
from toolkit.config_modules import ModelConfig, GenerateImageConfig, SampleConfig, LoRMConfig
|
| 5 |
+
from toolkit.lorm import ExtractMode, convert_diffusers_unet_to_lorm
|
| 6 |
+
from toolkit.sd_device_states_presets import get_train_sd_device_state_preset
|
| 7 |
+
from toolkit.stable_diffusion_model import StableDiffusion
|
| 8 |
+
import gc
|
| 9 |
+
import torch
|
| 10 |
+
from jobs.process import BaseExtensionProcess
|
| 11 |
+
from toolkit.train_tools import get_torch_dtype
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def flush():
|
| 15 |
+
torch.cuda.empty_cache()
|
| 16 |
+
gc.collect()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class PureLoraGenerator(BaseExtensionProcess):
|
| 20 |
+
|
| 21 |
+
def __init__(self, process_id: int, job, config: OrderedDict):
|
| 22 |
+
super().__init__(process_id, job, config)
|
| 23 |
+
self.output_folder = self.get_conf('output_folder', required=True)
|
| 24 |
+
self.device = self.get_conf('device', 'cuda')
|
| 25 |
+
self.device_torch = torch.device(self.device)
|
| 26 |
+
self.model_config = ModelConfig(**self.get_conf('model', required=True))
|
| 27 |
+
self.generate_config = SampleConfig(**self.get_conf('sample', required=True))
|
| 28 |
+
self.dtype = self.get_conf('dtype', 'float16')
|
| 29 |
+
self.torch_dtype = get_torch_dtype(self.dtype)
|
| 30 |
+
lorm_config = self.get_conf('lorm', None)
|
| 31 |
+
self.lorm_config = LoRMConfig(**lorm_config) if lorm_config is not None else None
|
| 32 |
+
|
| 33 |
+
self.device_state_preset = get_train_sd_device_state_preset(
|
| 34 |
+
device=torch.device(self.device),
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
self.progress_bar = None
|
| 38 |
+
self.sd = StableDiffusion(
|
| 39 |
+
device=self.device,
|
| 40 |
+
model_config=self.model_config,
|
| 41 |
+
dtype=self.dtype,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
def run(self):
|
| 45 |
+
super().run()
|
| 46 |
+
print("Loading model...")
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
self.sd.load_model()
|
| 49 |
+
self.sd.unet.eval()
|
| 50 |
+
self.sd.unet.to(self.device_torch)
|
| 51 |
+
if isinstance(self.sd.text_encoder, list):
|
| 52 |
+
for te in self.sd.text_encoder:
|
| 53 |
+
te.eval()
|
| 54 |
+
te.to(self.device_torch)
|
| 55 |
+
else:
|
| 56 |
+
self.sd.text_encoder.eval()
|
| 57 |
+
self.sd.to(self.device_torch)
|
| 58 |
+
|
| 59 |
+
print(f"Converting to LoRM UNet")
|
| 60 |
+
# replace the unet with LoRMUnet
|
| 61 |
+
convert_diffusers_unet_to_lorm(
|
| 62 |
+
self.sd.unet,
|
| 63 |
+
config=self.lorm_config,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
sample_folder = os.path.join(self.output_folder)
|
| 67 |
+
gen_img_config_list = []
|
| 68 |
+
|
| 69 |
+
sample_config = self.generate_config
|
| 70 |
+
start_seed = sample_config.seed
|
| 71 |
+
current_seed = start_seed
|
| 72 |
+
for i in range(len(sample_config.prompts)):
|
| 73 |
+
if sample_config.walk_seed:
|
| 74 |
+
current_seed = start_seed + i
|
| 75 |
+
|
| 76 |
+
filename = f"[time]_[count].{self.generate_config.ext}"
|
| 77 |
+
output_path = os.path.join(sample_folder, filename)
|
| 78 |
+
prompt = sample_config.prompts[i]
|
| 79 |
+
extra_args = {}
|
| 80 |
+
gen_img_config_list.append(GenerateImageConfig(
|
| 81 |
+
prompt=prompt, # it will autoparse the prompt
|
| 82 |
+
width=sample_config.width,
|
| 83 |
+
height=sample_config.height,
|
| 84 |
+
negative_prompt=sample_config.neg,
|
| 85 |
+
seed=current_seed,
|
| 86 |
+
guidance_scale=sample_config.guidance_scale,
|
| 87 |
+
guidance_rescale=sample_config.guidance_rescale,
|
| 88 |
+
num_inference_steps=sample_config.sample_steps,
|
| 89 |
+
network_multiplier=sample_config.network_multiplier,
|
| 90 |
+
output_path=output_path,
|
| 91 |
+
output_ext=sample_config.ext,
|
| 92 |
+
adapter_conditioning_scale=sample_config.adapter_conditioning_scale,
|
| 93 |
+
**extra_args
|
| 94 |
+
))
|
| 95 |
+
|
| 96 |
+
# send to be generated
|
| 97 |
+
self.sd.generate_images(gen_img_config_list, sampler=sample_config.sampler)
|
| 98 |
+
print("Done generating images")
|
| 99 |
+
# cleanup
|
| 100 |
+
del self.sd
|
| 101 |
+
gc.collect()
|
| 102 |
+
torch.cuda.empty_cache()
|