




"""simple docstring"""





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 					_UpperCamelCase    (			UpperCamelCase				,       UpperCamelCase							)			->			Union[str, Any]:


			"""simple docstring"""
			__UpperCAmelCase	: int		=					checkpoint

			__UpperCAmelCase	: List[str]		=					{}

			__UpperCAmelCase	: Dict		=					vae_state_dict["encoder.conv_in.weight"]
			__UpperCAmelCase	: Dict		=					vae_state_dict["encoder.conv_in.bias"]
			__UpperCAmelCase	: int		=					vae_state_dict["encoder.conv_out.weight"]
			__UpperCAmelCase	: int		=					vae_state_dict["encoder.conv_out.bias"]
			__UpperCAmelCase	: Optional[Any]		=					vae_state_dict["encoder.norm_out.weight"]
			__UpperCAmelCase	: Optional[int]		=					vae_state_dict["encoder.norm_out.bias"]

			__UpperCAmelCase	: Tuple		=					vae_state_dict["decoder.conv_in.weight"]
			__UpperCAmelCase	: Optional[int]		=					vae_state_dict["decoder.conv_in.bias"]
			__UpperCAmelCase	: str		=					vae_state_dict["decoder.conv_out.weight"]
			__UpperCAmelCase	: Any		=					vae_state_dict["decoder.conv_out.bias"]
			__UpperCAmelCase	: Union[str, Any]		=					vae_state_dict["decoder.norm_out.weight"]
			__UpperCAmelCase	: List[Any]		=					vae_state_dict["decoder.norm_out.bias"]

			__UpperCAmelCase	: List[Any]		=					vae_state_dict["quant_conv.weight"]
			__UpperCAmelCase	: Dict		=					vae_state_dict["quant_conv.bias"]
			__UpperCAmelCase	: str		=					vae_state_dict["post_quant_conv.weight"]
			__UpperCAmelCase	: Optional[Any]		=					vae_state_dict["post_quant_conv.bias"]

			# Retrieves the keys for the encoder down blocks only
			__UpperCAmelCase	: Optional[int]		=					len({".".join(layer.split("."							)[:3]							) for layer in vae_state_dict if "encoder.down" in layer}							)
			__UpperCAmelCase	: int		=					{
			    layer_id: [key for key in vae_state_dict if f"down.{layer_id}" in key] for layer_id in range(__snake_case							)
			}

			# Retrieves the keys for the decoder up blocks only
			__UpperCAmelCase	: Optional[Any]		=					len({".".join(layer.split("."							)[:3]							) for layer in vae_state_dict if "decoder.up" in layer}							)
			__UpperCAmelCase	: int		=					{
			    layer_id: [key for key in vae_state_dict if f"up.{layer_id}" in key] for layer_id in range(__snake_case							)
			}

			for i in range(__snake_case							):
						__UpperCAmelCase	: List[str]		=					[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:
									__UpperCAmelCase	: Dict		=					vae_state_dict.pop(
									    f"encoder.down.{i}.downsample.conv.weight"							)
									__UpperCAmelCase	: Tuple		=					vae_state_dict.pop(
									    f"encoder.down.{i}.downsample.conv.bias"							)

						__UpperCAmelCase	: List[Any]		=					renew_vae_resnet_paths(__snake_case							)
						__UpperCAmelCase	: Union[str, Any]		=					{"old": f"down.{i}.block", "new": f"down_blocks.{i}.resnets"}
						assign_to_checkpoint(__snake_case				,       __snake_case				,       __snake_case				,       additional_replacements=[meta_path]				,       config=__snake_case							)

			__UpperCAmelCase	: List[str]		=					[key for key in vae_state_dict if "encoder.mid.block" in key]
			__UpperCAmelCase	: Tuple		=					2
			for i in range(1				,       num_mid_res_blocks + 1							):
						__UpperCAmelCase	: Any		=					[key for key in mid_resnets if f"encoder.mid.block_{i}" in key]

						__UpperCAmelCase	: Optional[int]		=					renew_vae_resnet_paths(__snake_case							)
						__UpperCAmelCase	: Dict		=					{"old": f"mid.block_{i}", "new": f"mid_block.resnets.{i - 1}"}
						assign_to_checkpoint(__snake_case				,       __snake_case				,       __snake_case				,       additional_replacements=[meta_path]				,       config=__snake_case							)

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

			for i in range(__snake_case							):
						__UpperCAmelCase	: List[Any]		=					num_up_blocks - 1 - i
						__UpperCAmelCase	: str		=					[
						    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:
									__UpperCAmelCase	: Optional[int]		=					vae_state_dict[
									    f"decoder.up.{block_id}.upsample.conv.weight"
									]
									__UpperCAmelCase	: str		=					vae_state_dict[
									    f"decoder.up.{block_id}.upsample.conv.bias"
									]

						__UpperCAmelCase	: Any		=					renew_vae_resnet_paths(__snake_case							)
						__UpperCAmelCase	: int		=					{"old": f"up.{block_id}.block", "new": f"up_blocks.{i}.resnets"}
						assign_to_checkpoint(__snake_case				,       __snake_case				,       __snake_case				,       additional_replacements=[meta_path]				,       config=__snake_case							)

			__UpperCAmelCase	: Optional[int]		=					[key for key in vae_state_dict if "decoder.mid.block" in key]
			__UpperCAmelCase	: str		=					2
			for i in range(1				,       num_mid_res_blocks + 1							):
						__UpperCAmelCase	: List[str]		=					[key for key in mid_resnets if f"decoder.mid.block_{i}" in key]

						__UpperCAmelCase	: Optional[int]		=					renew_vae_resnet_paths(__snake_case							)
						__UpperCAmelCase	: List[str]		=					{"old": f"mid.block_{i}", "new": f"mid_block.resnets.{i - 1}"}
						assign_to_checkpoint(__snake_case				,       __snake_case				,       __snake_case				,       additional_replacements=[meta_path]				,       config=__snake_case							)

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

def 					_UpperCamelCase    (			UpperCamelCase				,       UpperCamelCase				,       )			->			Tuple:


			"""simple docstring"""
			# Only support V1
			__UpperCAmelCase	: List[str]		=					requests.get(
			    " https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"							)
			__UpperCAmelCase	: int		=					io.BytesIO(r.content							)

			__UpperCAmelCase	: List[Any]		=					OmegaConf.load(__snake_case							)
			__UpperCAmelCase	: Optional[int]		=					512
			__UpperCAmelCase	: Tuple		=					"cuda" if torch.cuda.is_available() else "cpu"
			if checkpoint_path.endswith("safetensors"							):
						from safetensors import safe_open

						__UpperCAmelCase	: Optional[Any]		=					{}
						with safe_open(__snake_case				,       framework="pt"				,       device="cpu"							) as f:
									for key in f.keys():
												__UpperCAmelCase	: str		=					f.get_tensor(__snake_case							)
			else:
						__UpperCAmelCase	: int		=					torch.load(__snake_case				,       map_location=__snake_case							)["state_dict"]

			# Convert the VAE model.
			__UpperCAmelCase	: Tuple		=					create_vae_diffusers_config(__snake_case				,       image_size=__snake_case							)
			__UpperCAmelCase	: Optional[Any]		=					custom_convert_ldm_vae_checkpoint(__snake_case				,       __snake_case							)

			__UpperCAmelCase	: List[Any]		=					AutoencoderKL(**__snake_case							)
			vae.load_state_dict(__snake_case							)
			vae.save_pretrained(__snake_case							)


if __name__ == "__main__":
					A   =       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.""")

					A   =       parser.parse_args()

					vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)








"""simple docstring"""





import json
import logging
import os
import socket

import git
import numpy as np
import torch


logging.basicConfig(
    format="""%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d -  %(message)s""",
    datefmt="""%m/%d/%Y %H:%M:%S""",
    level=logging.INFO,
)
A   =       logging.getLogger(__name__)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			Any:


			"""simple docstring"""
			__UpperCAmelCase	: Optional[Any]		=					git.Repo(search_parent_directories=UpperCamelCase							)
			__UpperCAmelCase	: Any		=					{
			    "repo_id": str(UpperCamelCase							),
			    "repo_sha": str(repo.head.object.hexsha							),
			    "repo_branch": str(repo.active_branch							),
			}

			with open(os.path.join(UpperCamelCase				,       "git_log.json"							)				,       "w"							) as f:
						json.dump(UpperCamelCase				,       UpperCamelCase				,       indent=4							)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			str:


			"""simple docstring"""
			if params.n_gpu <= 0:
						__UpperCAmelCase	: str		=					0
						__UpperCAmelCase	: Dict		=					-1
						__UpperCAmelCase	: Tuple		=					True
						__UpperCAmelCase	: List[str]		=					False
						return

			assert torch.cuda.is_available()

			logger.info("Initializing GPUs"							)
			if params.n_gpu > 1:
						assert params.local_rank != -1

						__UpperCAmelCase	: Optional[int]		=					int(os.environ["WORLD_SIZE"]							)
						__UpperCAmelCase	: Union[str, Any]		=					int(os.environ["N_GPU_NODE"]							)
						__UpperCAmelCase	: Optional[int]		=					int(os.environ["RANK"]							)

						# number of nodes / node ID
						__UpperCAmelCase	: int		=					params.world_size // params.n_gpu_per_node
						__UpperCAmelCase	: Optional[int]		=					params.global_rank // params.n_gpu_per_node
						__UpperCAmelCase	: Tuple		=					True

						assert params.n_nodes == int(os.environ["N_NODES"]							)
						assert params.node_id == int(os.environ["NODE_RANK"]							)

			# local job (single GPU)
			else:
						assert params.local_rank == -1

						__UpperCAmelCase	: Union[str, Any]		=					1
						__UpperCAmelCase	: Dict		=					0
						__UpperCAmelCase	: List[Any]		=					0
						__UpperCAmelCase	: Tuple		=					0
						__UpperCAmelCase	: Any		=					1
						__UpperCAmelCase	: str		=					1
						__UpperCAmelCase	: Dict		=					False

			# sanity checks
			assert params.n_nodes >= 1
			assert 0 <= params.node_id < params.n_nodes
			assert 0 <= params.local_rank <= params.global_rank < params.world_size
			assert params.world_size == params.n_nodes * params.n_gpu_per_node

			# define whether this is the master process / if we are in multi-node distributed mode
			__UpperCAmelCase	: Union[str, Any]		=					params.node_id == 0 and params.local_rank == 0
			__UpperCAmelCase	: Dict		=					params.n_nodes > 1

			# summary
			__UpperCAmelCase	: Any		=					f"--- Global rank: {params.global_rank} - "
			logger.info(PREFIX + "Number of nodes: %i" % params.n_nodes							)
			logger.info(PREFIX + "Node ID        : %i" % params.node_id							)
			logger.info(PREFIX + "Local rank     : %i" % params.local_rank							)
			logger.info(PREFIX + "World size     : %i" % params.world_size							)
			logger.info(PREFIX + "GPUs per node  : %i" % params.n_gpu_per_node							)
			logger.info(PREFIX + "Master         : %s" % str(params.is_master							)							)
			logger.info(PREFIX + "Multi-node     : %s" % str(params.multi_node							)							)
			logger.info(PREFIX + "Multi-GPU      : %s" % str(params.multi_gpu							)							)
			logger.info(PREFIX + "Hostname       : %s" % socket.gethostname()							)

			# set GPU device
			torch.cuda.set_device(params.local_rank							)

			# initialize multi-GPU
			if params.multi_gpu:
						logger.info("Initializing PyTorch distributed"							)
						torch.distributed.init_process_group(
						    init_method="env://"				,       backend="nccl"				,       )





def 					_UpperCamelCase    (			UpperCamelCase							)			->			Tuple:


			"""simple docstring"""
			np.random.seed(args.seed							)
			torch.manual_seed(args.seed							)
			if args.n_gpu > 0:
						torch.cuda.manual_seed_all(args.seed							)


