import inspect import os import time from typing import Any, Callable, Dict, List, Optional, Union, Tuple import gc import torch import numpy as np from glob import glob from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel from diffusers.loaders import TextualInversionLoaderMixin from diffusers.image_processor import VaeImageProcessor from diffusers.models import AutoencoderKL from diffusers.schedulers import (DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, KarrasDiffusionSchedulers) from diffusers.models.embeddings import TimestepEmbedding, Timesteps from diffusers.utils.torch_utils import randn_tensor from diffusers.utils import logging from PIL import Image from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, CLIPTextModelWithProjection from .lyrasd_vae_model import LyraSdVaeModel from .module.lyrasd_ip_adapter import LyraIPAdapter from .lora_util import add_text_lora_layer, add_xltext_lora_layer, add_lora_to_opt_model, load_state_dict from safetensors.torch import load_file class LyraSDXLPipelineBase(TextualInversionLoaderMixin): def __init__(self, device=torch.device("cuda"), dtype=torch.float16, num_channels_unet=4, num_channels_latents=4, vae_scale_factor=8, vae_scaling_factor=0.13025) -> None: self.device = device self.dtype = dtype self.num_channels_unet = num_channels_unet self.num_channels_latents = num_channels_latents self.vae_scale_factor = vae_scale_factor self.vae_scaling_factor = vae_scaling_factor self.unet_cache = {} self.unet_in_channels = 4 self.controlnet_cache = {} self.controlnet_add_embedding = {} self.loaded_lora = {} self.loaded_lora_strength = {} self.scheduler = None self.init_pipe() def init_pipe(self): self.vae = LyraSdVaeModel( scale_factor=self.vae_scale_factor, scaling_factor=self.vae_scaling_factor, is_upcast=True) self.unet = torch.classes.lyrasd.XLUnet2dConditionalModelOp( "fp16", self.num_channels_unet, self.num_channels_latents) self.default_sample_size = 128 self.addition_time_embed_dim = 256 flip_sin_to_cos, freq_shift = True, 0 self.projection_class_embeddings_input_dim, self.time_embed_dim = 2816, 1280 self.add_time_proj = Timesteps( self.addition_time_embed_dim, flip_sin_to_cos, freq_shift).to(self.dtype).to(self.device) self.add_embedding = TimestepEmbedding( self.projection_class_embeddings_input_dim, self.time_embed_dim).to(self.dtype).to(self.device) self.image_processor = VaeImageProcessor( vae_scale_factor=self.vae_scale_factor) self.mask_processor = VaeImageProcessor( vae_scale_factor=self.vae_scale_factor, do_normalize=False, do_binarize=True, do_convert_grayscale=True ) self.control_image_processor = VaeImageProcessor( vae_scale_factor=self.vae_scale_factor, do_convert_rgb=True, do_normalize=False ) self.feature_extractor = CLIPImageProcessor() def reload_pipe(self, model_path): self.tokenizer = CLIPTokenizer.from_pretrained( model_path, subfolder="tokenizer") self.text_encoder = CLIPTextModel.from_pretrained( model_path, subfolder="text_encoder").to(self.dtype).to(self.device) self.tokenizer_2 = CLIPTokenizer.from_pretrained( model_path, subfolder="tokenizer_2") self.text_encoder_2 = CLIPTextModelWithProjection.from_pretrained( model_path, subfolder="text_encoder_2").to(self.dtype).to(self.device) self.reload_unet_model_v2(model_path) self.reload_vae_model_v2(model_path) if not self.scheduler: self.scheduler = EulerAncestralDiscreteScheduler.from_pretrained( model_path, subfolder="scheduler") def load_embedding_weight(self, model, weight_path, unet_file_format="fp16"): bin_list = glob(weight_path) sate_dicts = model.state_dict() dtype = np.float32 if unet_file_format == "fp32" else np.float16 for bin_file in bin_list: weight = torch.from_numpy(np.fromfile(bin_file, dtype=dtype)).to( self.dtype).to(self.device) key = '.'.join(os.path.basename(bin_file).split('.')[1:-1]) weight = weight.reshape(sate_dicts[key].shape) sate_dicts.update({key: weight}) model.load_state_dict(sate_dicts) @property def _execution_device(self): if not hasattr(self.unet, "_hf_hook"): return self.device for module in self.unet.modules(): if ( hasattr(module, "_hf_hook") and hasattr(module._hf_hook, "execution_device") and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device) return self.device def reload_unet_model(self, unet_path, unet_file_format='fp32'): if len(unet_path) > 0 and unet_path[-1] != "/": unet_path = unet_path + "/" self.unet.reload_unet_model(unet_path, unet_file_format) self.load_embedding_weight( self.add_embedding, f"{unet_path}add_embedding*", unet_file_format=unet_file_format) def reload_vae_model(self, vae_path, vae_file_format='fp32'): if len(vae_path) > 0 and vae_path[-1] != "/": vae_path = vae_path + "/" return self.vae.reload_vae_model(vae_path, vae_file_format) def load_lora(self, lora_model_path, lora_name, lora_strength, lora_file_format='fp32'): if len(lora_model_path) > 0 and lora_model_path[-1] != "/": lora_model_path = lora_model_path + "/" lora = add_xltext_lora_layer( self.text_encoder, self.text_encoder_2, lora_model_path, lora_strength, lora_file_format) self.loaded_lora[lora_name] = lora self.unet.load_lora(lora_model_path, lora_name, lora_strength, lora_file_format) def unload_lora(self, lora_name, clean_cache=False): for layer_data in self.loaded_lora[lora_name]: layer = layer_data['layer'] added_weight = layer_data['added_weight'] layer.weight.data -= added_weight self.unet.unload_lora(lora_name, clean_cache) del self.loaded_lora[lora_name] gc.collect() torch.cuda.empty_cache() def load_lora_v2(self, lora_model_path, lora_name, lora_strength): if lora_name in self.loaded_lora: state_dict = self.loaded_lora[lora_name] else: state_dict = load_state_dict(lora_model_path) self.loaded_lora[lora_name] = state_dict self.loaded_lora_strength[lora_name] = lora_strength add_lora_to_opt_model(state_dict, self.unet, self.text_encoder, self.text_encoder_2, lora_strength) def unload_lora_v2(self, lora_name, clean_cache=False): state_dict = self.loaded_lora[lora_name] lora_strength = self.loaded_lora_strength[lora_name] add_lora_to_opt_model(state_dict, self.unet, self.text_encoder, self.text_encoder_2, -1.0 * lora_strength) del self.loaded_lora_strength[lora_name] if clean_cache: del self.loaded_lora[lora_name] gc.collect() torch.cuda.empty_cache() def clean_lora_cache(self): self.unet.clean_lora_cache() def get_loaded_lora(self): return self.unet.get_loaded_lora() def _get_aug_emb(self, time_ids, text_embeds, dtype): time_embeds = self.add_time_proj(time_ids.flatten()) time_embeds = time_embeds.reshape((text_embeds.shape[0], -1)) add_embeds = torch.concat([text_embeds, time_embeds], dim=-1) add_embeds = add_embeds.to(dtype) aug_emb = self.add_embedding(add_embeds) return aug_emb def load_ip_adapter(self, dir_ip_adapter, ip_plus, image_encoder_path, num_ip_tokens, ip_projection_dim, dir_face_in=None, num_fp_tokens=1, fp_projection_dim=None, sdxl=True): self.ip_adapter_helper = LyraIPAdapter(self, sdxl, "cuda", dir_ip_adapter, ip_plus, image_encoder_path, num_ip_tokens, ip_projection_dim, dir_face_in, num_fp_tokens, fp_projection_dim) def reload_unet_model_v2(self, model_path): checkpoint_file = os.path.join( model_path, "unet/diffusion_pytorch_model.bin") if not os.path.exists(checkpoint_file): checkpoint_file = os.path.join( model_path, "unet/diffusion_pytorch_model.safetensors") if checkpoint_file in self.unet_cache: state_dict = self.unet_cache[checkpoint_file] else: if "safetensors" in checkpoint_file: state_dict = load_file(checkpoint_file) else: state_dict = torch.load(checkpoint_file, map_location="cpu") for key in state_dict: if len(state_dict[key].shape) == 4: # converted_unet_checkpoint[key] = converted_unet_checkpoint[key].to(torch.float16).to("cuda").permute(0,2,3,1).contiguous().cpu() state_dict[key] = state_dict[key].to( torch.float16).permute(0, 2, 3, 1).contiguous() state_dict[key] = state_dict[key].to(torch.float16) self.unet_cache[checkpoint_file] = state_dict self.unet.reload_unet_model_from_cache(state_dict, "cpu") self.load_embedding_weight_v2(self.add_embedding, state_dict) def load_embedding_weight_v2(self, model, state_dict): sub_state_dict = {} for k in state_dict: if k.startswith("add_embedding"): v = state_dict[k] sub_k = ".".join(k.split(".")[1:]) sub_state_dict[sub_k] = v model.load_state_dict(sub_state_dict) def reload_vae_model_v2(self, model_path): self.vae.reload_vae_model_v2(model_path) def load_controlnet_model_v2(self, model_name, controlnet_path): checkpoint_file = os.path.join( controlnet_path, "diffusion_pytorch_model.bin") if not os.path.exists(checkpoint_file): checkpoint_file = os.path.join( controlnet_path, "diffusion_pytorch_model.safetensors") if checkpoint_file in self.controlnet_cache: state_dict = self.controlnet_cache[checkpoint_file] else: if "safetensors" in checkpoint_file: state_dict = load_file(checkpoint_file) else: state_dict = torch.load(checkpoint_file, map_location="cpu") for key in state_dict: if len(state_dict[key].shape) == 4: # converted_unet_checkpoint[key] = converted_unet_checkpoint[key].to(torch.float16).to("cuda").permute(0,2,3,1).contiguous().cpu() state_dict[key] = state_dict[key].to( torch.float16).permute(0, 2, 3, 1).contiguous() state_dict[key] = state_dict[key].to(torch.float16) self.controlnet_cache[checkpoint_file] = state_dict self.unet.load_controlnet_model_from_state_dict( model_name, state_dict, "cpu") add_embedding = TimestepEmbedding( self.projection_class_embeddings_input_dim, self.time_embed_dim).to(self.dtype).to(self.device) self.load_embedding_weight_v2(add_embedding, state_dict) self.controlnet_add_embedding[model_name] = add_embedding def unload_controlnet_model(self, model_name): self.unet.unload_controlnet_model(model_name, True) del self.controlnet_add_embedding[model_name] def get_loaded_controlnet(self): return self.unet.get_loaded_controlnet()