diff --git a/invokeai/backend/model_management/convert_ckpt_to_diffusers.py b/invokeai/backend/model_management/convert_ckpt_to_diffusers.py index 1640270dbf..5fd3669911 100644 --- a/invokeai/backend/model_management/convert_ckpt_to_diffusers.py +++ b/invokeai/backend/model_management/convert_ckpt_to_diffusers.py @@ -1070,7 +1070,7 @@ def convert_controlnet_checkpoint( extract_ema, use_linear_projection=None, cross_attention_dim=None, - precision: torch.dtype = torch.float32, + precision: Optional[torch.dtype] = None, ): ctrlnet_config = create_unet_diffusers_config(original_config, image_size=image_size, controlnet=True) ctrlnet_config["upcast_attention"] = upcast_attention @@ -1111,7 +1111,6 @@ def convert_controlnet_checkpoint( return controlnet.to(precision) -# TO DO - PASS PRECISION def download_from_original_stable_diffusion_ckpt( checkpoint_path: str, model_version: BaseModelType, @@ -1121,7 +1120,7 @@ def download_from_original_stable_diffusion_ckpt( prediction_type: str = None, model_type: str = None, extract_ema: bool = False, - precision: torch.dtype = torch.float32, + precision: Optional[torch.dtype] = None, scheduler_type: str = "pndm", num_in_channels: Optional[int] = None, upcast_attention: Optional[bool] = None, @@ -1194,6 +1193,8 @@ def download_from_original_stable_diffusion_ckpt( [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer) to use. If this parameter is `None`, the function will load a new instance of [CLIPTokenizer] by itself, if needed. + precision (`torch.dtype`, *optional*, defauts to `None`): + If not provided the precision will be set to the precision of the original file. return: A StableDiffusionPipeline object representing the passed-in `.ckpt`/`.safetensors` file. """ @@ -1252,6 +1253,10 @@ def download_from_original_stable_diffusion_ckpt( logger.debug(f"model_type = {model_type}; original_config_file = {original_config_file}") + precision_probing_key = "model.diffusion_model.input_blocks.0.0.bias" + logger.debug(f"original checkpoint precision == {checkpoint[precision_probing_key].dtype}") + precision = precision or checkpoint[precision_probing_key].dtype + if original_config_file is None: key_name_v2_1 = "model.diffusion_model.input_blocks.2.1.transformer_blocks.0.attn2.to_k.weight" key_name_sd_xl_base = "conditioner.embedders.1.model.transformer.resblocks.9.mlp.c_proj.bias" @@ -1279,6 +1284,9 @@ def download_from_original_stable_diffusion_ckpt( original_config_file = BytesIO(requests.get(config_url).content) original_config = OmegaConf.load(original_config_file) + if original_config["model"]["params"].get("use_ema") is not None: + extract_ema = original_config["model"]["params"]["use_ema"] + if ( model_version == BaseModelType.StableDiffusion2 and original_config["model"]["params"].get("parameterization") == "v" @@ -1447,7 +1455,7 @@ def download_from_original_stable_diffusion_ckpt( if controlnet: pipe = pipeline_class( vae=vae.to(precision), - text_encoder=text_model, + text_encoder=text_model.to(precision), tokenizer=tokenizer, unet=unet.to(precision), scheduler=scheduler, @@ -1459,7 +1467,7 @@ def download_from_original_stable_diffusion_ckpt( else: pipe = pipeline_class( vae=vae.to(precision), - text_encoder=text_model, + text_encoder=text_model.to(precision), tokenizer=tokenizer, unet=unet.to(precision), scheduler=scheduler, @@ -1484,8 +1492,8 @@ def download_from_original_stable_diffusion_ckpt( image_noising_scheduler=image_noising_scheduler, # regular denoising components tokenizer=tokenizer, - text_encoder=text_model, - unet=unet, + text_encoder=text_model.to(precision), + unet=unet.to(precision), scheduler=scheduler, # vae vae=vae, @@ -1560,7 +1568,7 @@ def download_from_original_stable_diffusion_ckpt( if controlnet: pipe = pipeline_class( vae=vae.to(precision), - text_encoder=text_model, + text_encoder=text_model.to(precision), tokenizer=tokenizer, unet=unet.to(precision), controlnet=controlnet, @@ -1571,7 +1579,7 @@ def download_from_original_stable_diffusion_ckpt( else: pipe = pipeline_class( vae=vae.to(precision), - text_encoder=text_model, + text_encoder=text_model.to(precision), tokenizer=tokenizer, unet=unet.to(precision), scheduler=scheduler, @@ -1594,9 +1602,9 @@ def download_from_original_stable_diffusion_ckpt( pipe = StableDiffusionXLPipeline( vae=vae.to(precision), - text_encoder=text_encoder, + text_encoder=text_encoder.to(precision), tokenizer=tokenizer, - text_encoder_2=text_encoder_2, + text_encoder_2=text_encoder_2.to(precision), tokenizer_2=tokenizer_2, unet=unet.to(precision), scheduler=scheduler, @@ -1639,7 +1647,7 @@ def download_controlnet_from_original_ckpt( original_config_file: str, image_size: int = 512, extract_ema: bool = False, - precision: torch.dtype = torch.float32, + precision: Optional[torch.dtype] = None, num_in_channels: Optional[int] = None, upcast_attention: Optional[bool] = None, device: str = None, @@ -1680,6 +1688,12 @@ def download_controlnet_from_original_ckpt( while "state_dict" in checkpoint: checkpoint = checkpoint["state_dict"] + # use original precision + precision_probing_key = "input_blocks.0.0.bias" + ckpt_precision = checkpoint[precision_probing_key].dtype + logger.debug(f"original controlnet precision = {ckpt_precision}") + precision = precision or ckpt_precision + original_config = OmegaConf.load(original_config_file) if num_in_channels is not None: @@ -1699,7 +1713,7 @@ def download_controlnet_from_original_ckpt( cross_attention_dim=cross_attention_dim, ) - return controlnet + return controlnet.to(precision) def convert_ldm_vae_to_diffusers(checkpoint, vae_config: DictConfig, image_size: int) -> AutoencoderKL: diff --git a/invokeai/backend/model_management/models/controlnet.py b/invokeai/backend/model_management/models/controlnet.py index e075843a56..061be7ae49 100644 --- a/invokeai/backend/model_management/models/controlnet.py +++ b/invokeai/backend/model_management/models/controlnet.py @@ -17,6 +17,7 @@ from .base import ( ModelNotFoundException, ) from invokeai.app.services.config import InvokeAIAppConfig +import invokeai.backend.util.logging as logger class ControlNetModelFormat(str, Enum): @@ -66,7 +67,7 @@ class ControlNetModel(ModelBase): child_type: Optional[SubModelType] = None, ): if child_type is not None: - raise Exception("There is no child models in controlnet model") + raise Exception("There are no child models in controlnet model") model = None for variant in ["fp16", None]: @@ -124,9 +125,7 @@ class ControlNetModel(ModelBase): return model_path -@classmethod def _convert_controlnet_ckpt_and_cache( - cls, model_path: str, output_path: str, base_model: BaseModelType, @@ -141,6 +140,7 @@ def _convert_controlnet_ckpt_and_cache( weights = app_config.root_path / model_path output_path = Path(output_path) + logger.info(f"Converting {weights} to diffusers format") # return cached version if it exists if output_path.exists(): return output_path diff --git a/invokeai/backend/model_management/models/stable_diffusion.py b/invokeai/backend/model_management/models/stable_diffusion.py index e672067545..d81b0150e5 100644 --- a/invokeai/backend/model_management/models/stable_diffusion.py +++ b/invokeai/backend/model_management/models/stable_diffusion.py @@ -123,6 +123,7 @@ class StableDiffusion1Model(DiffusersModel): return _convert_ckpt_and_cache( version=BaseModelType.StableDiffusion1, model_config=config, + load_safety_checker=False, output_path=output_path, ) else: