
import argparse
import io

import requests
import torch
from omegaconf import OmegaConf

from diffusers import AutoencoderKL
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import (
    assign_to_checkpoint,
    conv_attn_to_linear,
    create_vae_diffusers_config,
    renew_vae_attention_paths,
    renew_vae_resnet_paths,
)



def      __lowerCAmelCase						(							UpperCAmelCase__				:      Optional[Any]			,				UpperCAmelCase__				:      Any       )  ->    int:
							lowerCamelCase_							  =					checkpoint

							lowerCamelCase_							  =					{}

							lowerCamelCase_							  =					vae_state_dict["""encoder.conv_in.weight"""]
							lowerCamelCase_							  =					vae_state_dict["""encoder.conv_in.bias"""]
							lowerCamelCase_							  =					vae_state_dict["""encoder.conv_out.weight"""]
							lowerCamelCase_							  =					vae_state_dict["""encoder.conv_out.bias"""]
							lowerCamelCase_							  =					vae_state_dict["""encoder.norm_out.weight"""]
							lowerCamelCase_							  =					vae_state_dict["""encoder.norm_out.bias"""]

							lowerCamelCase_							  =					vae_state_dict["""decoder.conv_in.weight"""]
							lowerCamelCase_							  =					vae_state_dict["""decoder.conv_in.bias"""]
							lowerCamelCase_							  =					vae_state_dict["""decoder.conv_out.weight"""]
							lowerCamelCase_							  =					vae_state_dict["""decoder.conv_out.bias"""]
							lowerCamelCase_							  =					vae_state_dict["""decoder.norm_out.weight"""]
							lowerCamelCase_							  =					vae_state_dict["""decoder.norm_out.bias"""]

							lowerCamelCase_							  =					vae_state_dict["""quant_conv.weight"""]
							lowerCamelCase_							  =					vae_state_dict["""quant_conv.bias"""]
							lowerCamelCase_							  =					vae_state_dict["""post_quant_conv.weight"""]
							lowerCamelCase_							  =					vae_state_dict["""post_quant_conv.bias"""]

							# Retrieves the keys for the encoder down blocks only
							lowerCamelCase_							  =					len({""".""".join(layer.split("""."""       )[:3]       ) for layer in vae_state_dict if """encoder.down""" in layer}       )
							lowerCamelCase_							  =					{
							    layer_id: [key for key in vae_state_dict if F'''down.{layer_id}''' in key] for layer_id in range(UpperCAmelCase__       )
							}

							# Retrieves the keys for the decoder up blocks only
							lowerCamelCase_							  =					len({""".""".join(layer.split("""."""       )[:3]       ) for layer in vae_state_dict if """decoder.up""" in layer}       )
							lowerCamelCase_							  =					{
							    layer_id: [key for key in vae_state_dict if F'''up.{layer_id}''' in key] for layer_id in range(UpperCAmelCase__       )
							}

							for i in range(UpperCAmelCase__       ):
														lowerCamelCase_							  =					[key for key in down_blocks[i] if F'''down.{i}''' in key and F'''down.{i}.downsample''' not in key]

														if F'''encoder.down.{i}.downsample.conv.weight''' in vae_state_dict:
																					lowerCamelCase_							  =					vae_state_dict.pop(
																					    F'''encoder.down.{i}.downsample.conv.weight'''       )
																					lowerCamelCase_							  =					vae_state_dict.pop(
																					    F'''encoder.down.{i}.downsample.conv.bias'''       )

														lowerCamelCase_							  =					renew_vae_resnet_paths(UpperCAmelCase__       )
														lowerCamelCase_							  =					{"""old""": F'''down.{i}.block''', """new""": F'''down_blocks.{i}.resnets'''}
														assign_to_checkpoint(UpperCAmelCase__			,				UpperCAmelCase__			,				UpperCAmelCase__			,				additional_replacements=[meta_path]			,				config=UpperCAmelCase__       )

							lowerCamelCase_							  =					[key for key in vae_state_dict if """encoder.mid.block""" in key]
							lowerCamelCase_							  =					2
							for i in range(1			,				num_mid_res_blocks + 1       ):
														lowerCamelCase_							  =					[key for key in mid_resnets if F'''encoder.mid.block_{i}''' in key]

														lowerCamelCase_							  =					renew_vae_resnet_paths(UpperCAmelCase__       )
														lowerCamelCase_							  =					{"""old""": F'''mid.block_{i}''', """new""": F'''mid_block.resnets.{i - 1}'''}
														assign_to_checkpoint(UpperCAmelCase__			,				UpperCAmelCase__			,				UpperCAmelCase__			,				additional_replacements=[meta_path]			,				config=UpperCAmelCase__       )

							lowerCamelCase_							  =					[key for key in vae_state_dict if """encoder.mid.attn""" in key]
							lowerCamelCase_							  =					renew_vae_attention_paths(UpperCAmelCase__       )
							lowerCamelCase_							  =					{"""old""": """mid.attn_1""", """new""": """mid_block.attentions.0"""}
							assign_to_checkpoint(UpperCAmelCase__			,				UpperCAmelCase__			,				UpperCAmelCase__			,				additional_replacements=[meta_path]			,				config=UpperCAmelCase__       )
							conv_attn_to_linear(UpperCAmelCase__       )

							for i in range(UpperCAmelCase__       ):
														lowerCamelCase_							  =					num_up_blocks - 1 - i
														lowerCamelCase_							  =					[
														    key for key in up_blocks[block_id] if F'''up.{block_id}''' in key and F'''up.{block_id}.upsample''' not in key
														]

														if F'''decoder.up.{block_id}.upsample.conv.weight''' in vae_state_dict:
																					lowerCamelCase_							  =					vae_state_dict[
																					    F'''decoder.up.{block_id}.upsample.conv.weight'''
																					]
																					lowerCamelCase_							  =					vae_state_dict[
																					    F'''decoder.up.{block_id}.upsample.conv.bias'''
																					]

														lowerCamelCase_							  =					renew_vae_resnet_paths(UpperCAmelCase__       )
														lowerCamelCase_							  =					{"""old""": F'''up.{block_id}.block''', """new""": F'''up_blocks.{i}.resnets'''}
														assign_to_checkpoint(UpperCAmelCase__			,				UpperCAmelCase__			,				UpperCAmelCase__			,				additional_replacements=[meta_path]			,				config=UpperCAmelCase__       )

							lowerCamelCase_							  =					[key for key in vae_state_dict if """decoder.mid.block""" in key]
							lowerCamelCase_							  =					2
							for i in range(1			,				num_mid_res_blocks + 1       ):
														lowerCamelCase_							  =					[key for key in mid_resnets if F'''decoder.mid.block_{i}''' in key]

														lowerCamelCase_							  =					renew_vae_resnet_paths(UpperCAmelCase__       )
														lowerCamelCase_							  =					{"""old""": F'''mid.block_{i}''', """new""": F'''mid_block.resnets.{i - 1}'''}
														assign_to_checkpoint(UpperCAmelCase__			,				UpperCAmelCase__			,				UpperCAmelCase__			,				additional_replacements=[meta_path]			,				config=UpperCAmelCase__       )

							lowerCamelCase_							  =					[key for key in vae_state_dict if """decoder.mid.attn""" in key]
							lowerCamelCase_							  =					renew_vae_attention_paths(UpperCAmelCase__       )
							lowerCamelCase_							  =					{"""old""": """mid.attn_1""", """new""": """mid_block.attentions.0"""}
							assign_to_checkpoint(UpperCAmelCase__			,				UpperCAmelCase__			,				UpperCAmelCase__			,				additional_replacements=[meta_path]			,				config=UpperCAmelCase__       )
							conv_attn_to_linear(UpperCAmelCase__       )
							return new_checkpoint



def      __lowerCAmelCase						(							UpperCAmelCase__				:      str			,				UpperCAmelCase__				:      str			,				)  ->    Tuple:
							# Only support V1
							lowerCamelCase_							  =					requests.get(
							    """ https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"""       )
							lowerCamelCase_							  =					io.BytesIO(r.content       )

							lowerCamelCase_							  =					OmegaConf.load(UpperCAmelCase__       )
							lowerCamelCase_							  =					5_1_2
							lowerCamelCase_							  =					"""cuda""" if torch.cuda.is_available() else """cpu"""
							if checkpoint_path.endswith("""safetensors"""       ):
														from safetensors import safe_open

														lowerCamelCase_							  =					{}
														with safe_open(UpperCAmelCase__			,				framework="""pt"""			,				device="""cpu"""       ) as f:
																					for key in f.keys():
																												lowerCamelCase_							  =					f.get_tensor(UpperCAmelCase__       )
							else:
														lowerCamelCase_							  =					torch.load(UpperCAmelCase__			,				map_location=UpperCAmelCase__       )["""state_dict"""]

							# Convert the VAE model.
							lowerCamelCase_							  =					create_vae_diffusers_config(UpperCAmelCase__			,				image_size=UpperCAmelCase__       )
							lowerCamelCase_							  =					custom_convert_ldm_vae_checkpoint(UpperCAmelCase__			,				UpperCAmelCase__       )

							lowerCamelCase_							  =					AutoencoderKL(**UpperCAmelCase__       )
							vae.load_state_dict(UpperCAmelCase__       )
							vae.save_pretrained(UpperCAmelCase__       )


if __name__ == "__main__":
		lowercase        =  argparse.ArgumentParser()

		parser.add_argument('''--vae_pt_path''', default=None, type=str, required=True, help='''Path to the VAE.pt to convert.''')
		parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the VAE.pt to convert.''')

		lowercase        =  parser.parse_args()

		vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)







import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
		from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
		import torch_xla.core.xla_model as xm



def      __lowerCAmelCase						(							UpperCAmelCase__				:      Any       )  ->    List[str]:
							if is_torch_version("""<"""			,				"""2.0.0"""       ) or not hasattr(UpperCAmelCase__			,				"""_dynamo"""       ):
														return False
							return isinstance(UpperCAmelCase__			,				torch._dynamo.eval_frame.OptimizedModule       )



def      __lowerCAmelCase						(							UpperCAmelCase__				:      str			,				UpperCAmelCase__				:      bool = True       )  ->    str:
							lowerCamelCase_							  =					(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

							lowerCamelCase_							  =					is_compiled_module(UpperCAmelCase__       )
							if is_compiled:
														lowerCamelCase_							  =					model
														lowerCamelCase_							  =					model._orig_mod

							if is_deepspeed_available():
														options += (DeepSpeedEngine,)

							while isinstance(UpperCAmelCase__			,				UpperCAmelCase__       ):
														lowerCamelCase_							  =					model.module

							if not keep_fpaa_wrapper:
														lowerCamelCase_							  =					getattr(UpperCAmelCase__			,				"""forward"""       )
														lowerCamelCase_							  =					model.__dict__.pop("""_original_forward"""			,				UpperCAmelCase__       )
														if original_forward is not None:
																					while hasattr(UpperCAmelCase__			,				"""__wrapped__"""       ):
																												lowerCamelCase_							  =					forward.__wrapped__
																												if forward == original_forward:
																																			break
																					lowerCamelCase_							  =					forward
														if getattr(UpperCAmelCase__			,				"""_converted_to_transformer_engine"""			,				UpperCAmelCase__       ):
																					convert_model(UpperCAmelCase__			,				to_transformer_engine=UpperCAmelCase__       )

							if is_compiled:
														lowerCamelCase_							  =					model
														lowerCamelCase_							  =					compiled_model

							return model



def      __lowerCAmelCase						(							)  ->    Any:
							PartialState().wait_for_everyone()



def      __lowerCAmelCase						(							UpperCAmelCase__				:      Union[str, Any]			,				UpperCAmelCase__				:      Tuple       )  ->    Dict:
							if PartialState().distributed_type == DistributedType.TPU:
														xm.save(UpperCAmelCase__			,				UpperCAmelCase__       )
							elif PartialState().local_process_index == 0:
														torch.save(UpperCAmelCase__			,				UpperCAmelCase__       )



@contextmanager
def      __lowerCAmelCase						(							**UpperCAmelCase__				:      Any       )  ->    Dict:
							for key, value in kwargs.items():
														lowerCamelCase_							  =					str(UpperCAmelCase__       )

							yield

							for key in kwargs:
														if key.upper() in os.environ:
																					del os.environ[key.upper()]



def      __lowerCAmelCase						(							UpperCAmelCase__				:      Union[str, Any]       )  ->    str:
							if not hasattr(UpperCAmelCase__			,				"""__qualname__"""       ) and not hasattr(UpperCAmelCase__			,				"""__name__"""       ):
														lowerCamelCase_							  =					getattr(UpperCAmelCase__			,				"""__class__"""			,				UpperCAmelCase__       )
							if hasattr(UpperCAmelCase__			,				"""__qualname__"""       ):
														return obj.__qualname__
							if hasattr(UpperCAmelCase__			,				"""__name__"""       ):
														return obj.__name__
							return str(UpperCAmelCase__       )



def      __lowerCAmelCase						(							UpperCAmelCase__				:      str			,				UpperCAmelCase__				:      Union[str, Any]       )  ->    Dict:
							for key, value in source.items():
														if isinstance(UpperCAmelCase__			,				UpperCAmelCase__       ):
																					lowerCamelCase_							  =					destination.setdefault(UpperCAmelCase__			,				{}       )
																					merge_dicts(UpperCAmelCase__			,				UpperCAmelCase__       )
														else:
																					lowerCamelCase_							  =					value

							return destination



def      __lowerCAmelCase						(							UpperCAmelCase__				:      int = None       )  ->    bool:
							if port is None:
														lowerCamelCase_							  =					2_9_5_0_0
							with socket.socket(socket.AF_INET			,				socket.SOCK_STREAM       ) as s:
														return s.connect_ex(("""localhost""", port)       ) == 0





