
"""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   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     Optional[Any]:







							"""simple docstring"""


							__snake_case     			=       checkpoint

							__snake_case     			=       {}

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

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

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

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

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

							for i in range(SCREAMING_SNAKE_CASE ):
														__snake_case     			=       [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:
																					__snake_case     			=       vae_state_dict.pop(
																					    F'''encoder.down.{i}.downsample.conv.weight''' )
																					__snake_case     			=       vae_state_dict.pop(
																					    F'''encoder.down.{i}.downsample.conv.bias''' )

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

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

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

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

							for i in range(SCREAMING_SNAKE_CASE ):
														__snake_case     			=       num_up_blocks - 1 - i
														__snake_case     			=       [
														    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:
																					__snake_case     			=       vae_state_dict[
																					    F'''decoder.up.{block_id}.upsample.conv.weight'''
																					]
																					__snake_case     			=       vae_state_dict[
																					    F'''decoder.up.{block_id}.upsample.conv.bias'''
																					]

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

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

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

							__snake_case     			=       [key for key in vae_state_dict if "decoder.mid.attn" in key]
							__snake_case     			=       renew_vae_attention_paths(SCREAMING_SNAKE_CASE )
							__snake_case     			=       {"old": "mid.attn_1", "new": "mid_block.attentions.0"}
							assign_to_checkpoint(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    additional_replacements=[meta_path]							,    config=SCREAMING_SNAKE_CASE )
							conv_attn_to_linear(SCREAMING_SNAKE_CASE )
							return new_checkpoint
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    )  ->     Optional[Any]:







							"""simple docstring"""


							__snake_case     			=       requests.get(
							    " https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml" )
							__snake_case     			=       io.BytesIO(r.content )

							__snake_case     			=       OmegaConf.load(SCREAMING_SNAKE_CASE )
							__snake_case     			=       5_12
							__snake_case     			=       "cuda" if torch.cuda.is_available() else "cpu"
							if checkpoint_path.endswith("safetensors" ):
														from safetensors import safe_open

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

							# Convert the VAE model.
							__snake_case     			=       create_vae_diffusers_config(SCREAMING_SNAKE_CASE							,    image_size=SCREAMING_SNAKE_CASE )
							__snake_case     			=       custom_convert_ldm_vae_checkpoint(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )

							__snake_case     			=       AutoencoderKL(**SCREAMING_SNAKE_CASE )
							vae.load_state_dict(SCREAMING_SNAKE_CASE )
							vae.save_pretrained(SCREAMING_SNAKE_CASE )


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

		_SCREAMING_SNAKE_CASE	       =      parser.parse_args()

		vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)


"""simple docstring"""







def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE )  ->     list:







							"""simple docstring"""


							__snake_case     			=       len(SCREAMING_SNAKE_CASE )
							for i in range(1							,    SCREAMING_SNAKE_CASE ):
														__snake_case     			=       collection[i]
														__snake_case     			=       0
														__snake_case     			=       i - 1

														while low <= high:
																					__snake_case     			=       (low + high) // 2
																					if val < collection[mid]:
																												__snake_case     			=       mid - 1
																					else:
																												__snake_case     			=       mid + 1
														for j in range(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    -1 ):
																					__snake_case     			=       collection[j - 1]
														__snake_case     			=       val
							return collection


if __name__ == "__main__":
		_SCREAMING_SNAKE_CASE	       =      input("""Enter numbers separated by a comma:\n""").strip()
		_SCREAMING_SNAKE_CASE	       =      [int(item) for item in user_input.split(""",""")]
		print(binary_insertion_sort(unsorted))
