


'''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       a				(      __a			, __a       )  ->     List[Any]:



					'''simple docstring'''






					UpperCamelCase__       :List[str]							 =			checkpoint

					UpperCamelCase__       :Any							 =			{}

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

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

					UpperCamelCase__       :Tuple							 =			vae_state_dict['''quant_conv.weight''']
					UpperCamelCase__       :List[str]							 =			vae_state_dict['''quant_conv.bias''']
					UpperCamelCase__       :List[Any]							 =			vae_state_dict['''post_quant_conv.weight''']
					UpperCamelCase__       :Tuple							 =			vae_state_dict['''post_quant_conv.bias''']

					# Retrieves the keys for the encoder down blocks only
					UpperCamelCase__       :Tuple							 =			len({'''.'''.join(layer.split('''.'''       )[:3]       ) for layer in vae_state_dict if '''encoder.down''' in layer}       )
					UpperCamelCase__       :Tuple							 =			{
					    layer_id: [key for key in vae_state_dict if f'''down.{layer_id}''' in key] for layer_id in range(__a       )
					}

					# Retrieves the keys for the decoder up blocks only
					UpperCamelCase__       :List[Any]							 =			len({'''.'''.join(layer.split('''.'''       )[:3]       ) for layer in vae_state_dict if '''decoder.up''' in layer}       )
					UpperCamelCase__       :Tuple							 =			{
					    layer_id: [key for key in vae_state_dict if f'''up.{layer_id}''' in key] for layer_id in range(__a       )
					}

					for i in range(__a       ):
										UpperCamelCase__       :Union[str, Any]							 =			[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__       :Tuple							 =			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__       :Union[str, Any]							 =			renew_vae_resnet_paths(__a       )
										UpperCamelCase__       :Union[str, Any]							 =			{'''old''': f'''down.{i}.block''', '''new''': f'''down_blocks.{i}.resnets'''}
										assign_to_checkpoint(__a			, __a			, __a			, additional_replacements=[meta_path]			, config=__a       )

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

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

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

					for i in range(__a       ):
										UpperCamelCase__       :Union[str, Any]							 =			num_up_blocks - 1 - i
										UpperCamelCase__       :int							 =			[
										    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__       :str							 =			vae_state_dict[
															    f'''decoder.up.{block_id}.upsample.conv.weight'''
															]
															UpperCamelCase__       :Dict							 =			vae_state_dict[
															    f'''decoder.up.{block_id}.upsample.conv.bias'''
															]

										UpperCamelCase__       :Union[str, Any]							 =			renew_vae_resnet_paths(__a       )
										UpperCamelCase__       :Dict							 =			{'''old''': f'''up.{block_id}.block''', '''new''': f'''up_blocks.{i}.resnets'''}
										assign_to_checkpoint(__a			, __a			, __a			, additional_replacements=[meta_path]			, config=__a       )

					UpperCamelCase__       :Tuple							 =			[key for key in vae_state_dict if '''decoder.mid.block''' in key]
					UpperCamelCase__       :Tuple							 =			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__       :str							 =			renew_vae_resnet_paths(__a       )
										UpperCamelCase__       :str							 =			{'''old''': f'''mid.block_{i}''', '''new''': f'''mid_block.resnets.{i - 1}'''}
										assign_to_checkpoint(__a			, __a			, __a			, additional_replacements=[meta_path]			, config=__a       )

					UpperCamelCase__       :Optional[Any]							 =			[key for key in vae_state_dict if '''decoder.mid.attn''' in key]
					UpperCamelCase__       :Optional[int]							 =			renew_vae_attention_paths(__a       )
					UpperCamelCase__       :Union[str, Any]							 =			{'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''}
					assign_to_checkpoint(__a			, __a			, __a			, additional_replacements=[meta_path]			, config=__a       )
					conv_attn_to_linear(__a       )
					return new_checkpoint


def       a				(      __a			, __a			, )  ->     str:



					'''simple docstring'''






					UpperCamelCase__       :Optional[Any]							 =			requests.get(
					    ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml'''       )
					UpperCamelCase__       :Optional[Any]							 =			io.BytesIO(r.content       )

					UpperCamelCase__       :Dict							 =			OmegaConf.load(__a       )
					UpperCamelCase__       :List[Any]							 =			512
					UpperCamelCase__       :List[str]							 =			'''cuda''' if torch.cuda.is_available() else '''cpu'''
					if checkpoint_path.endswith('''safetensors'''       ):
										from safetensors import safe_open

										UpperCamelCase__       :Union[str, Any]							 =			{}
										with safe_open(__a			, framework='''pt'''			, device='''cpu'''       ) as f:
															for key in f.keys():
																				UpperCamelCase__       :List[Any]							 =			f.get_tensor(__a       )
					else:
										UpperCamelCase__       :Any							 =			torch.load(__a			, map_location=__a       )['''state_dict''']

					# Convert the VAE model.
					UpperCamelCase__       :Optional[Any]							 =			create_vae_diffusers_config(__a			, image_size=__a       )
					UpperCamelCase__       :List[str]							 =			custom_convert_ldm_vae_checkpoint(__a			, __a       )

					UpperCamelCase__       :Union[str, Any]							 =			AutoencoderKL(**__a       )
					vae.load_state_dict(__a       )
					vae.save_pretrained(__a       )


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

		__snake_case											=					parser.parse_args()

		vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)



'''simple docstring'''
import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_distilbert import DistilBertTokenizer


__snake_case											=					logging.get_logger(__name__)

__snake_case											=					{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

__snake_case											=					{
    '''vocab_file''': {
        '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt''',
        '''distilbert-base-uncased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt'''
        ),
        '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt''',
        '''distilbert-base-cased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt'''
        ),
        '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt''',
        '''distilbert-base-multilingual-cased''': (
            '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json''',
        '''distilbert-base-uncased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json''',
        '''distilbert-base-cased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-german-cased''': (
            '''https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-multilingual-cased''': (
            '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json'''
        ),
    },
}

__snake_case											=					{
    '''distilbert-base-uncased''': 512,
    '''distilbert-base-uncased-distilled-squad''': 512,
    '''distilbert-base-cased''': 512,
    '''distilbert-base-cased-distilled-squad''': 512,
    '''distilbert-base-german-cased''': 512,
    '''distilbert-base-multilingual-cased''': 512,
}


__snake_case											=					{
    '''distilbert-base-uncased''': {'''do_lower_case''': True},
    '''distilbert-base-uncased-distilled-squad''': {'''do_lower_case''': True},
    '''distilbert-base-cased''': {'''do_lower_case''': False},
    '''distilbert-base-cased-distilled-squad''': {'''do_lower_case''': False},
    '''distilbert-base-german-cased''': {'''do_lower_case''': False},
    '''distilbert-base-multilingual-cased''': {'''do_lower_case''': False},
}
class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    VOCAB_FILES_NAMES
		_a   			=    PRETRAINED_VOCAB_FILES_MAP
		_a   			=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		_a   			=    PRETRAINED_INIT_CONFIGURATION
		_a   			=    ['input_ids', 'attention_mask']
		_a   			=    DistilBertTokenizer




		def __init__(		self   ,       UpperCamelCase_=None   ,       UpperCamelCase_=None   ,       UpperCamelCase_=True   ,       UpperCamelCase_="[UNK]"   ,       UpperCamelCase_="[SEP]"   ,       UpperCamelCase_="[PAD]"   ,       UpperCamelCase_="[CLS]"   ,       UpperCamelCase_="[MASK]"   ,       UpperCamelCase_=True   ,       UpperCamelCase_=None   ,       **UpperCamelCase_   ,       ):

							'''simple docstring'''


							super().__init__(
							    UpperCamelCase_   ,       tokenizer_file=UpperCamelCase_   ,       do_lower_case=UpperCamelCase_   ,       unk_token=UpperCamelCase_   ,       sep_token=UpperCamelCase_   ,       pad_token=UpperCamelCase_   ,       cls_token=UpperCamelCase_   ,       mask_token=UpperCamelCase_   ,       tokenize_chinese_chars=UpperCamelCase_   ,       strip_accents=UpperCamelCase_   ,       **UpperCamelCase_   ,       )

							UpperCamelCase__       :int							 =			json.loads(self.backend_tokenizer.normalizer.__getstate__()							)
							if (
							    normalizer_state.get('''lowercase'''   ,       UpperCamelCase_							) != do_lower_case
							    or normalizer_state.get('''strip_accents'''   ,       UpperCamelCase_							) != strip_accents
							    or normalizer_state.get('''handle_chinese_chars'''   ,       UpperCamelCase_							) != tokenize_chinese_chars
							):
												UpperCamelCase__       :int							 =			getattr(UpperCamelCase_   ,       normalizer_state.pop('''type'''							)							)
												UpperCamelCase__       :Optional[Any]							 =			do_lower_case
												UpperCamelCase__       :Optional[Any]							 =			strip_accents
												UpperCamelCase__       :List[Any]							 =			tokenize_chinese_chars
												UpperCamelCase__       :Any							 =			normalizer_class(**UpperCamelCase_							)

							UpperCamelCase__       :int							 =			do_lower_case




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_=None							):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			[self.cls_token_id] + token_ids_a + [self.sep_token_id]

							if token_ids_a:
												output += token_ids_a + [self.sep_token_id]

							return output




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = None							):

							'''simple docstring'''


							UpperCamelCase__       :List[str]							 =			[self.sep_token_id]
							UpperCamelCase__       :List[str]							 =			[self.cls_token_id]
							if token_ids_a is None:
												return len(cls + token_ids_a + sep							) * [0]
							return len(cls + token_ids_a + sep							) * [0] + len(token_ids_a + sep							) * [1]




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = None							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			self._tokenizer.model.save(UpperCamelCase_   ,       name=UpperCamelCase_							)
							return tuple(UpperCamelCase_							)