Spaces:
Paused
Paused
ResearcherXman commited on
Commit ·
2192aaf
1
Parent(s): ec7fc1c
fix
Browse filesFiles changed (2)
- app.py+1-2
- model_util.pyDeleted-472
17 unmodified lines181920212223242 unmodified lines2728293031323317 unmodified linesfrom style_template import stylesfrom pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstantIDPipeline, draw_kpsfrom model_util import load_models_xl, get_torch_devicefrom controlnet_util import openpose, get_depth_map, get_canny_imageimport gradio as gr2 unmodified lines# global variableMAX_SEED = np.iinfo(np.int32).maxdevice = get_torch_device()dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32STYLE_NAMES = list(styles.keys())DEFAULT_STYLE_NAME = "Watercolor"17 unmodified lines1819202122232 unmodified lines2627282930313217 unmodified linesfrom style_template import stylesfrom pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstantIDPipeline, draw_kpsfrom controlnet_util import openpose, get_depth_map, get_canny_imageimport gradio as gr2 unmodified lines# global variableMAX_SEED = np.iinfo(np.int32).maxdevice = "cuda" if torch.cuda.is_available() else "cpu"dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32STYLE_NAMES = list(styles.keys())DEFAULT_STYLE_NAME = "Watercolor"
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472from typing import Literal, Union, Optional, Tuple, List
import torchfrom transformers import CLIPTextModel, CLIPTokenizer, CLIPTextModelWithProjectionfrom diffusers import ( UNet2DConditionModel, SchedulerMixin, StableDiffusionPipeline, StableDiffusionXLPipeline, AutoencoderKL,)from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( convert_ldm_unet_checkpoint,)from safetensors.torch import load_filefrom diffusers.schedulers import ( DDIMScheduler, DDPMScheduler, LMSDiscreteScheduler, EulerDiscreteScheduler, EulerAncestralDiscreteScheduler, UniPCMultistepScheduler,)
from omegaconf import OmegaConf
# DiffUsers版StableDiffusionのモデルパラメータNUM_TRAIN_TIMESTEPS = 1000BETA_START = 0.00085BETA_END = 0.0120
UNET_PARAMS_MODEL_CHANNELS = 320UNET_PARAMS_CHANNEL_MULT = [1, 2, 4, 4]UNET_PARAMS_ATTENTION_RESOLUTIONS = [4, 2, 1]UNET_PARAMS_IMAGE_SIZE = 64 # fixed from old invalid value `32`UNET_PARAMS_IN_CHANNELS = 4UNET_PARAMS_OUT_CHANNELS = 4UNET_PARAMS_NUM_RES_BLOCKS = 2UNET_PARAMS_CONTEXT_DIM = 768UNET_PARAMS_NUM_HEADS = 8# UNET_PARAMS_USE_LINEAR_PROJECTION = False
VAE_PARAMS_Z_CHANNELS = 4VAE_PARAMS_RESOLUTION = 256VAE_PARAMS_IN_CHANNELS = 3VAE_PARAMS_OUT_CH = 3VAE_PARAMS_CH = 128VAE_PARAMS_CH_MULT = [1, 2, 4, 4]VAE_PARAMS_NUM_RES_BLOCKS = 2
# V2V2_UNET_PARAMS_ATTENTION_HEAD_DIM = [5, 10, 20, 20]V2_UNET_PARAMS_CONTEXT_DIM = 1024# V2_UNET_PARAMS_USE_LINEAR_PROJECTION = True
TOKENIZER_V1_MODEL_NAME = "CompVis/stable-diffusion-v1-4"TOKENIZER_V2_MODEL_NAME = "stabilityai/stable-diffusion-2-1"
AVAILABLE_SCHEDULERS = Literal["ddim", "ddpm", "lms", "euler_a", "euler", "uniPC"]
SDXL_TEXT_ENCODER_TYPE = Union[CLIPTextModel, CLIPTextModelWithProjection]
DIFFUSERS_CACHE_DIR = None # if you want to change the cache dir, change this
def load_checkpoint_with_text_encoder_conversion(ckpt_path: str, device="cpu"): # text encoderの格納形式が違うモデルに対応する ('text_model'がない) TEXT_ENCODER_KEY_REPLACEMENTS = [ ( "cond_stage_model.transformer.embeddings.", "cond_stage_model.transformer.text_model.embeddings.", ), ( "cond_stage_model.transformer.encoder.", "cond_stage_model.transformer.text_model.encoder.", ), ( "cond_stage_model.transformer.final_layer_norm.", "cond_stage_model.transformer.text_model.final_layer_norm.", ), ]
if ckpt_path.endswith(".safetensors"): checkpoint = None state_dict = load_file(ckpt_path) # , device) # may causes error else: checkpoint = torch.load(ckpt_path, map_location=device) if "state_dict" in checkpoint: state_dict = checkpoint["state_dict"] else: state_dict = checkpoint checkpoint = None
key_reps = [] for rep_from, rep_to in TEXT_ENCODER_KEY_REPLACEMENTS: for key in state_dict.keys(): if key.startswith(rep_from): new_key = rep_to + key[len(rep_from) :] key_reps.append((key, new_key))
for key, new_key in key_reps: state_dict[new_key] = state_dict[key] del state_dict[key]
return checkpoint, state_dict
def create_unet_diffusers_config(v2, use_linear_projection_in_v2=False): """ Creates a config for the diffusers based on the config of the LDM model. """ # unet_params = original_config.model.params.unet_config.params
block_out_channels = [ UNET_PARAMS_MODEL_CHANNELS * mult for mult in UNET_PARAMS_CHANNEL_MULT ]
down_block_types = [] resolution = 1 for i in range(len(block_out_channels)): block_type = ( "CrossAttnDownBlock2D" if resolution in UNET_PARAMS_ATTENTION_RESOLUTIONS else "DownBlock2D" ) down_block_types.append(block_type) if i != len(block_out_channels) - 1: resolution *= 2
up_block_types = [] for i in range(len(block_out_channels)): block_type = ( "CrossAttnUpBlock2D" if resolution in UNET_PARAMS_ATTENTION_RESOLUTIONS else "UpBlock2D" ) up_block_types.append(block_type) resolution //= 2
config = dict( sample_size=UNET_PARAMS_IMAGE_SIZE, in_channels=UNET_PARAMS_IN_CHANNELS, out_channels=UNET_PARAMS_OUT_CHANNELS, down_block_types=tuple(down_block_types), up_block_types=tuple(up_block_types), block_out_channels=tuple(block_out_channels), layers_per_block=UNET_PARAMS_NUM_RES_BLOCKS, cross_attention_dim=UNET_PARAMS_CONTEXT_DIM if not v2 else V2_UNET_PARAMS_CONTEXT_DIM, attention_head_dim=UNET_PARAMS_NUM_HEADS if not v2 else V2_UNET_PARAMS_ATTENTION_HEAD_DIM, # use_linear_projection=UNET_PARAMS_USE_LINEAR_PROJECTION if not v2 else V2_UNET_PARAMS_USE_LINEAR_PROJECTION, ) if v2 and use_linear_projection_in_v2: config["use_linear_projection"] = True
return config
def load_diffusers_model( pretrained_model_name_or_path: str, v2: bool = False, clip_skip: Optional[int] = None, weight_dtype: torch.dtype = torch.float32,) -> Tuple[CLIPTokenizer, CLIPTextModel, UNet2DConditionModel,]: if v2: tokenizer = CLIPTokenizer.from_pretrained( TOKENIZER_V2_MODEL_NAME, subfolder="tokenizer", torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, ) text_encoder = CLIPTextModel.from_pretrained( pretrained_model_name_or_path, subfolder="text_encoder", # default is clip skip 2 num_hidden_layers=24 - (clip_skip - 1) if clip_skip is not None else 23, torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, ) else: tokenizer = CLIPTokenizer.from_pretrained( TOKENIZER_V1_MODEL_NAME, subfolder="tokenizer", torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, ) text_encoder = CLIPTextModel.from_pretrained( pretrained_model_name_or_path, subfolder="text_encoder", num_hidden_layers=12 - (clip_skip - 1) if clip_skip is not None else 12, torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, )
unet = UNet2DConditionModel.from_pretrained( pretrained_model_name_or_path, subfolder="unet", torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, )
vae = AutoencoderKL.from_pretrained(pretrained_model_name_or_path, subfolder="vae")
return tokenizer, text_encoder, unet, vae
def load_checkpoint_model( checkpoint_path: str, v2: bool = False, clip_skip: Optional[int] = None, weight_dtype: torch.dtype = torch.float32,) -> Tuple[CLIPTokenizer, CLIPTextModel, UNet2DConditionModel,]: pipe = StableDiffusionPipeline.from_single_file( checkpoint_path, upcast_attention=True if v2 else False, torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, )
_, state_dict = load_checkpoint_with_text_encoder_conversion(checkpoint_path) unet_config = create_unet_diffusers_config(v2, use_linear_projection_in_v2=v2) unet_config["class_embed_type"] = None unet_config["addition_embed_type"] = None converted_unet_checkpoint = convert_ldm_unet_checkpoint(state_dict, unet_config) unet = UNet2DConditionModel(**unet_config) unet.load_state_dict(converted_unet_checkpoint)
tokenizer = pipe.tokenizer text_encoder = pipe.text_encoder vae = pipe.vae if clip_skip is not None: if v2: text_encoder.config.num_hidden_layers = 24 - (clip_skip - 1) else: text_encoder.config.num_hidden_layers = 12 - (clip_skip - 1)
del pipe
return tokenizer, text_encoder, unet, vae
def load_models( pretrained_model_name_or_path: str, scheduler_name: str, v2: bool = False, v_pred: bool = False, weight_dtype: torch.dtype = torch.float32,) -> Tuple[CLIPTokenizer, CLIPTextModel, UNet2DConditionModel, SchedulerMixin,]: if pretrained_model_name_or_path.endswith( ".ckpt" ) or pretrained_model_name_or_path.endswith(".safetensors"): tokenizer, text_encoder, unet, vae = load_checkpoint_model( pretrained_model_name_or_path, v2=v2, weight_dtype=weight_dtype ) else: # diffusers tokenizer, text_encoder, unet, vae = load_diffusers_model( pretrained_model_name_or_path, v2=v2, weight_dtype=weight_dtype )
if scheduler_name: scheduler = create_noise_scheduler( scheduler_name, prediction_type="v_prediction" if v_pred else "epsilon", ) else: scheduler = None
return tokenizer, text_encoder, unet, scheduler, vae
def load_diffusers_model_xl( pretrained_model_name_or_path: str, weight_dtype: torch.dtype = torch.float32,) -> Tuple[List[CLIPTokenizer], List[SDXL_TEXT_ENCODER_TYPE], UNet2DConditionModel,]: # returns tokenizer, tokenizer_2, text_encoder, text_encoder_2, unet
tokenizers = [ CLIPTokenizer.from_pretrained( pretrained_model_name_or_path, subfolder="tokenizer", torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, ), CLIPTokenizer.from_pretrained( pretrained_model_name_or_path, subfolder="tokenizer_2", torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, pad_token_id=0, # same as open clip ), ]
text_encoders = [ CLIPTextModel.from_pretrained( pretrained_model_name_or_path, subfolder="text_encoder", torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, ), CLIPTextModelWithProjection.from_pretrained( pretrained_model_name_or_path, subfolder="text_encoder_2", torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, ), ]
unet = UNet2DConditionModel.from_pretrained( pretrained_model_name_or_path, subfolder="unet", torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, ) vae = AutoencoderKL.from_pretrained(pretrained_model_name_or_path, subfolder="vae") return tokenizers, text_encoders, unet, vae
def load_checkpoint_model_xl( checkpoint_path: str, weight_dtype: torch.dtype = torch.float32,) -> Tuple[List[CLIPTokenizer], List[SDXL_TEXT_ENCODER_TYPE], UNet2DConditionModel,]: pipe = StableDiffusionXLPipeline.from_single_file( checkpoint_path, torch_dtype=weight_dtype, cache_dir=DIFFUSERS_CACHE_DIR, )
unet = pipe.unet vae = pipe.vae tokenizers = [pipe.tokenizer, pipe.tokenizer_2] text_encoders = [pipe.text_encoder, pipe.text_encoder_2] if len(text_encoders) == 2: text_encoders[1].pad_token_id = 0
del pipe
return tokenizers, text_encoders, unet, vae
def load_models_xl( pretrained_model_name_or_path: str, scheduler_name: str, weight_dtype: torch.dtype = torch.float32, noise_scheduler_kwargs=None,) -> Tuple[ List[CLIPTokenizer], List[SDXL_TEXT_ENCODER_TYPE], UNet2DConditionModel, SchedulerMixin,]: if pretrained_model_name_or_path.endswith( ".ckpt" ) or pretrained_model_name_or_path.endswith(".safetensors"): (tokenizers, text_encoders, unet, vae) = load_checkpoint_model_xl( pretrained_model_name_or_path, weight_dtype ) else: # diffusers (tokenizers, text_encoders, unet, vae) = load_diffusers_model_xl( pretrained_model_name_or_path, weight_dtype ) if scheduler_name: scheduler = create_noise_scheduler(scheduler_name, noise_scheduler_kwargs) else: scheduler = None
return tokenizers, text_encoders, unet, scheduler, vae
def create_noise_scheduler( scheduler_name: AVAILABLE_SCHEDULERS = "ddpm", noise_scheduler_kwargs=None, prediction_type: Literal["epsilon", "v_prediction"] = "epsilon",) -> SchedulerMixin: name = scheduler_name.lower().replace(" ", "_") if name.lower() == "ddim": # https://huggingface.co/docs/diffusers/v0.17.1/en/api/schedulers/ddim scheduler = DDIMScheduler(**OmegaConf.to_container(noise_scheduler_kwargs)) elif name.lower() == "ddpm": # https://huggingface.co/docs/diffusers/v0.17.1/en/api/schedulers/ddpm scheduler = DDPMScheduler(**OmegaConf.to_container(noise_scheduler_kwargs)) elif name.lower() == "lms": # https://huggingface.co/docs/diffusers/v0.17.1/en/api/schedulers/lms_discrete scheduler = LMSDiscreteScheduler( **OmegaConf.to_container(noise_scheduler_kwargs) ) elif name.lower() == "euler_a": # https://huggingface.co/docs/diffusers/v0.17.1/en/api/schedulers/euler_ancestral scheduler = EulerAncestralDiscreteScheduler( **OmegaConf.to_container(noise_scheduler_kwargs) ) elif name.lower() == "euler": # https://huggingface.co/docs/diffusers/v0.17.1/en/api/schedulers/euler_ancestral scheduler = EulerDiscreteScheduler( **OmegaConf.to_container(noise_scheduler_kwargs) ) elif name.lower() == "unipc": # https://huggingface.co/docs/diffusers/v0.17.1/en/api/schedulers/unipc scheduler = UniPCMultistepScheduler( **OmegaConf.to_container(noise_scheduler_kwargs) ) else: raise ValueError(f"Unknown scheduler name: {name}")
return scheduler
def torch_gc(): import gc
gc.collect() if torch.cuda.is_available(): with torch.cuda.device("cuda"): torch.cuda.empty_cache() torch.cuda.ipc_collect()
from enum import Enum
class CPUState(Enum): GPU = 0 CPU = 1 MPS = 2
cpu_state = CPUState.GPUxpu_available = Falsedirectml_enabled = False
def is_intel_xpu(): global cpu_state global xpu_available if cpu_state == CPUState.GPU: if xpu_available: return True return False
try: import intel_extension_for_pytorch as ipex
if torch.xpu.is_available(): xpu_available = Trueexcept: pass
try: if torch.backends.mps.is_available(): cpu_state = CPUState.MPS import torch.mpsexcept: pass
def get_torch_device(): global directml_enabled global cpu_state if directml_enabled: global directml_device return directml_device if cpu_state == CPUState.MPS: return torch.device("mps") if cpu_state == CPUState.CPU: return torch.device("cpu") else: if is_intel_xpu(): return torch.device("xpu") else: return torch.device(torch.cuda.current_device())app.py+1-2
model_util.pyDeleted-472