


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      snake_case_						( __lowercase			,       __lowercase       ):
   UpperCAmelCase_							:      str						=   checkpoint

   UpperCAmelCase_							:      Union[str, Any]						=   {}

   UpperCAmelCase_							:      Dict						=   vae_state_dict['''encoder.conv_in.weight''']
   UpperCAmelCase_							:      Optional[Any]						=   vae_state_dict['''encoder.conv_in.bias''']
   UpperCAmelCase_							:      Dict						=   vae_state_dict['''encoder.conv_out.weight''']
   UpperCAmelCase_							:      List[str]						=   vae_state_dict['''encoder.conv_out.bias''']
   UpperCAmelCase_							:      Any						=   vae_state_dict['''encoder.norm_out.weight''']
   UpperCAmelCase_							:      Optional[Any]						=   vae_state_dict['''encoder.norm_out.bias''']

   UpperCAmelCase_							:      str						=   vae_state_dict['''decoder.conv_in.weight''']
   UpperCAmelCase_							:      Tuple						=   vae_state_dict['''decoder.conv_in.bias''']
   UpperCAmelCase_							:      Union[str, Any]						=   vae_state_dict['''decoder.conv_out.weight''']
   UpperCAmelCase_							:      Tuple						=   vae_state_dict['''decoder.conv_out.bias''']
   UpperCAmelCase_							:      int						=   vae_state_dict['''decoder.norm_out.weight''']
   UpperCAmelCase_							:      List[str]						=   vae_state_dict['''decoder.norm_out.bias''']

   UpperCAmelCase_							:      Any						=   vae_state_dict['''quant_conv.weight''']
   UpperCAmelCase_							:      Optional[Any]						=   vae_state_dict['''quant_conv.bias''']
   UpperCAmelCase_							:      Dict						=   vae_state_dict['''post_quant_conv.weight''']
   UpperCAmelCase_							:      Dict						=   vae_state_dict['''post_quant_conv.bias''']

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

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

   for i in range(__lowercase       ):
      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_							:      Any						=   vae_state_dict.pop(
             F'''encoder.down.{i}.downsample.conv.weight'''       )
         UpperCAmelCase_							:      List[str]						=   vae_state_dict.pop(
             F'''encoder.down.{i}.downsample.conv.bias'''       )

      UpperCAmelCase_							:      Tuple						=   renew_vae_resnet_paths(__lowercase       )
      UpperCAmelCase_							:      Any						=   {'''old''': F'''down.{i}.block''', '''new''': F'''down_blocks.{i}.resnets'''}
      assign_to_checkpoint(__lowercase			,       __lowercase			,       __lowercase			,       additional_replacements=[meta_path]			,       config=__lowercase       )

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

      UpperCAmelCase_							:      Optional[Any]						=   renew_vae_resnet_paths(__lowercase       )
      UpperCAmelCase_							:      int						=   {'''old''': F'''mid.block_{i}''', '''new''': F'''mid_block.resnets.{i - 1}'''}
      assign_to_checkpoint(__lowercase			,       __lowercase			,       __lowercase			,       additional_replacements=[meta_path]			,       config=__lowercase       )

   UpperCAmelCase_							:      int						=   [key for key in vae_state_dict if '''encoder.mid.attn''' in key]
   UpperCAmelCase_							:      List[str]						=   renew_vae_attention_paths(__lowercase       )
   UpperCAmelCase_							:      Any						=   {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''}
   assign_to_checkpoint(__lowercase			,       __lowercase			,       __lowercase			,       additional_replacements=[meta_path]			,       config=__lowercase       )
   conv_attn_to_linear(__lowercase       )

   for i in range(__lowercase       ):
      UpperCAmelCase_							:      List[str]						=   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_							:      Dict						=   vae_state_dict[
             F'''decoder.up.{block_id}.upsample.conv.weight'''
         ]
         UpperCAmelCase_							:      Optional[Any]						=   vae_state_dict[
             F'''decoder.up.{block_id}.upsample.conv.bias'''
         ]

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

   UpperCAmelCase_							:      str						=   [key for key in vae_state_dict if '''decoder.mid.block''' in key]
   UpperCAmelCase_							:      int						=   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_							:      List[str]						=   renew_vae_resnet_paths(__lowercase       )
      UpperCAmelCase_							:      Dict						=   {'''old''': F'''mid.block_{i}''', '''new''': F'''mid_block.resnets.{i - 1}'''}
      assign_to_checkpoint(__lowercase			,       __lowercase			,       __lowercase			,       additional_replacements=[meta_path]			,       config=__lowercase       )

   UpperCAmelCase_							:      str						=   [key for key in vae_state_dict if '''decoder.mid.attn''' in key]
   UpperCAmelCase_							:      List[Any]						=   renew_vae_attention_paths(__lowercase       )
   UpperCAmelCase_							:      int						=   {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''}
   assign_to_checkpoint(__lowercase			,       __lowercase			,       __lowercase			,       additional_replacements=[meta_path]			,       config=__lowercase       )
   conv_attn_to_linear(__lowercase       )
   return new_checkpoint
def      snake_case_						( __lowercase			,       __lowercase			,       ):
   # Only support V1
   UpperCAmelCase_							:      Optional[Any]						=   requests.get(
       ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml'''       )
   UpperCAmelCase_							:      Any						=   io.BytesIO(r.content       )

   UpperCAmelCase_							:      Any						=   OmegaConf.load(__lowercase       )
   UpperCAmelCase_							:      List[Any]						=   5_1_2
   UpperCAmelCase_							:      str						=   '''cuda''' if torch.cuda.is_available() else '''cpu'''
   if checkpoint_path.endswith('''safetensors'''       ):
      from safetensors import safe_open

      UpperCAmelCase_							:      Dict						=   {}
      with safe_open(__lowercase			,       framework='''pt'''			,       device='''cpu'''       ) as f:
         for key in f.keys():
            UpperCAmelCase_							:      int						=   f.get_tensor(__lowercase       )
   else:
      UpperCAmelCase_							:      Optional[int]						=   torch.load(__lowercase			,       map_location=__lowercase       )['''state_dict''']

   # Convert the VAE model.
   UpperCAmelCase_							:      Optional[int]						=   create_vae_diffusers_config(__lowercase			,       image_size=__lowercase       )
   UpperCAmelCase_							:      Union[str, Any]						=   custom_convert_ldm_vae_checkpoint(__lowercase			,       __lowercase       )

   UpperCAmelCase_							:      Dict						=   AutoencoderKL(**__lowercase       )
   vae.load_state_dict(__lowercase       )
   vae.save_pretrained(__lowercase       )


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

       __UpperCamelCase				:  Any      					=							parser.parse_args()

       vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)



from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCamelCase				:  Any      					=							logging.get_logger(__name__)

__UpperCamelCase				:  Dict      					=							{
    'bigcode/gpt_bigcode-santacoder': 'https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json',
}





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     List[str]			= 'gpt_bigcode'
    A_  :     Optional[Any]			= ['past_key_values']
    A_  :     Optional[int]			= {
        'hidden_size': 'n_embd',
        'max_position_embeddings': 'n_positions',
        'num_attention_heads': 'n_head',
        'num_hidden_layers': 'n_layer',
    }





    def __init__(	self				:   int       ,   __snake_case				:   Dict=50_257       ,   __snake_case				:   List[str]=1_024       ,   __snake_case				:   Dict=768       ,   __snake_case				:   Optional[int]=12       ,   __snake_case				:   str=12       ,   __snake_case				:   List[str]=None       ,   __snake_case				:   List[str]="gelu_pytorch_tanh"       ,   __snake_case				:   Optional[Any]=0.1       ,   __snake_case				:   str=0.1       ,   __snake_case				:   Optional[Any]=0.1       ,   __snake_case				:   List[str]=1E-5       ,   __snake_case				:   Dict=0.02       ,   __snake_case				:   Optional[int]=True       ,   __snake_case				:   Tuple=True       ,   __snake_case				:   Optional[Any]=50_256       ,   __snake_case				:   Optional[Any]=50_256       ,   __snake_case				:   Optional[Any]=True       ,   __snake_case				:   Optional[Any]=True       ,   __snake_case				:   List[Any]=True       ,   **__snake_case				:   List[str]       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   vocab_size
       UpperCAmelCase_							:      int						=   n_positions
       UpperCAmelCase_							:      Any						=   n_embd
       UpperCAmelCase_							:      Union[str, Any]						=   n_layer
       UpperCAmelCase_							:      List[str]						=   n_head
       UpperCAmelCase_							:      List[Any]						=   n_inner
       UpperCAmelCase_							:      Optional[int]						=   activation_function
       UpperCAmelCase_							:      str						=   resid_pdrop
       UpperCAmelCase_							:      Optional[Any]						=   embd_pdrop
       UpperCAmelCase_							:      str						=   attn_pdrop
       UpperCAmelCase_							:      Any						=   layer_norm_epsilon
       UpperCAmelCase_							:      List[str]						=   initializer_range
       UpperCAmelCase_							:      Optional[int]						=   scale_attn_weights
       UpperCAmelCase_							:      Union[str, Any]						=   use_cache
       UpperCAmelCase_							:      Dict						=   attention_softmax_in_fpaa
       UpperCAmelCase_							:      Union[str, Any]						=   scale_attention_softmax_in_fpaa
       UpperCAmelCase_							:      Optional[int]						=   multi_query

       UpperCAmelCase_							:      Optional[Any]						=   bos_token_id
       UpperCAmelCase_							:      Tuple						=   eos_token_id

       super().__init__(bos_token_id=__snake_case       ,   eos_token_id=__snake_case       ,   **__snake_case	)