

'''simple docstring'''
import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         MODEL_FOR_PRETRAINING_MAPPING,
         AlbertForMaskedLM,
         AlbertForMultipleChoice,
         AlbertForPreTraining,
         AlbertForQuestionAnswering,
         AlbertForSequenceClassification,
         AlbertForTokenClassification,
         AlbertModel,
     )
     from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST







class 							A__   :





  def __init__(		self				:     Tuple							, _a				:     int							, _a				:     Tuple=13							, _a				:     str=7							, _a				:     Optional[int]=True							, _a				:     List[str]=True							, _a				:     Dict=True							, _a				:     Any=True							, _a				:     Union[str, Any]=99							, _a				:     Tuple=16							, _a				:     Tuple=36							, _a				:     Optional[Any]=6							, _a				:     Dict=6							, _a				:     Optional[Any]=6							, _a				:     List[Any]=37							, _a				:     Any="gelu"							, _a				:     List[str]=0.1							, _a				:     Union[str, Any]=0.1							, _a				:     Dict=512							, _a				:     int=16							, _a				:     List[Any]=2							, _a				:     int=0.02							, _a				:     int=3							, _a				:     Dict=4							, _a				:     Union[str, Any]=None							, )     ->   Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =parent
       _SCREAMING_SNAKE_CASE							   =batch_size
       _SCREAMING_SNAKE_CASE							   =seq_length
       _SCREAMING_SNAKE_CASE							   =is_training
       _SCREAMING_SNAKE_CASE							   =use_input_mask
       _SCREAMING_SNAKE_CASE							   =use_token_type_ids
       _SCREAMING_SNAKE_CASE							   =use_labels
       _SCREAMING_SNAKE_CASE							   =vocab_size
       _SCREAMING_SNAKE_CASE							   =embedding_size
       _SCREAMING_SNAKE_CASE							   =hidden_size
       _SCREAMING_SNAKE_CASE							   =num_hidden_layers
       _SCREAMING_SNAKE_CASE							   =num_hidden_groups
       _SCREAMING_SNAKE_CASE							   =num_attention_heads
       _SCREAMING_SNAKE_CASE							   =intermediate_size
       _SCREAMING_SNAKE_CASE							   =hidden_act
       _SCREAMING_SNAKE_CASE							   =hidden_dropout_prob
       _SCREAMING_SNAKE_CASE							   =attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE							   =max_position_embeddings
       _SCREAMING_SNAKE_CASE							   =type_vocab_size
       _SCREAMING_SNAKE_CASE							   =type_sequence_label_size
       _SCREAMING_SNAKE_CASE							   =initializer_range
       _SCREAMING_SNAKE_CASE							   =num_labels
       _SCREAMING_SNAKE_CASE							   =num_choices
       _SCREAMING_SNAKE_CASE							   =scope





  def 						A  (		self				:     Union[str, Any]			)     ->   Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size			)

       _SCREAMING_SNAKE_CASE							   =None
       if self.use_input_mask:
            _SCREAMING_SNAKE_CASE							   =random_attention_mask([self.batch_size, self.seq_length]			)

       _SCREAMING_SNAKE_CASE							   =None
       if self.use_token_type_ids:
            _SCREAMING_SNAKE_CASE							   =ids_tensor([self.batch_size, self.seq_length]							, self.type_vocab_size			)

       _SCREAMING_SNAKE_CASE							   =None
       _SCREAMING_SNAKE_CASE							   =None
       _SCREAMING_SNAKE_CASE							   =None
       if self.use_labels:
            _SCREAMING_SNAKE_CASE							   =ids_tensor([self.batch_size]							, self.type_sequence_label_size			)
            _SCREAMING_SNAKE_CASE							   =ids_tensor([self.batch_size, self.seq_length]							, self.num_labels			)
            _SCREAMING_SNAKE_CASE							   =ids_tensor([self.batch_size]							, self.num_choices			)

       _SCREAMING_SNAKE_CASE							   =self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





  def 						A  (		self				:     int			)     ->   List[str]:





       '''simple docstring'''






       return AlbertConfig(
           vocab_size=self.vocab_size							, hidden_size=self.hidden_size							, num_hidden_layers=self.num_hidden_layers							, num_attention_heads=self.num_attention_heads							, intermediate_size=self.intermediate_size							, hidden_act=self.hidden_act							, hidden_dropout_prob=self.hidden_dropout_prob							, attention_probs_dropout_prob=self.attention_probs_dropout_prob							, max_position_embeddings=self.max_position_embeddings							, type_vocab_size=self.type_vocab_size							, initializer_range=self.initializer_range							, num_hidden_groups=self.num_hidden_groups							, )





  def 						A  (		self				:     List[Any]							, _a				:     List[str]							, _a				:     Any							, _a				:     Dict							, _a				:     List[Any]							, _a				:     Union[str, Any]							, _a				:     Tuple							, _a				:     Dict			)     ->   int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =AlbertModel(config=_a			)
       model.to(_a			)
       model.eval()
       _SCREAMING_SNAKE_CASE							   =model(_a							, attention_mask=_a							, token_type_ids=_a			)
       _SCREAMING_SNAKE_CASE							   =model(_a							, token_type_ids=_a			)
       _SCREAMING_SNAKE_CASE							   =model(_a			)
       self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, self.seq_length, self.hidden_size)			)
       self.parent.assertEqual(result.pooler_output.shape							, (self.batch_size, self.hidden_size)			)





  def 						A  (		self				:     int							, _a				:     Any							, _a				:     Dict							, _a				:     Union[str, Any]							, _a				:     Dict							, _a				:     int							, _a				:     Union[str, Any]							, _a				:     Optional[Any]			)     ->   Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =AlbertForPreTraining(config=_a			)
       model.to(_a			)
       model.eval()
       _SCREAMING_SNAKE_CASE							   =model(
           _a							, attention_mask=_a							, token_type_ids=_a							, labels=_a							, sentence_order_label=_a							, )
       self.parent.assertEqual(result.prediction_logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)			)
       self.parent.assertEqual(result.sop_logits.shape							, (self.batch_size, config.num_labels)			)





  def 						A  (		self				:     Optional[Any]							, _a				:     List[str]							, _a				:     Union[str, Any]							, _a				:     Optional[Any]							, _a				:     Any							, _a				:     int							, _a				:     List[str]							, _a				:     str			)     ->   Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =AlbertForMaskedLM(config=_a			)
       model.to(_a			)
       model.eval()
       _SCREAMING_SNAKE_CASE							   =model(_a							, attention_mask=_a							, token_type_ids=_a							, labels=_a			)
       self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)			)





  def 						A  (		self				:     Any							, _a				:     Union[str, Any]							, _a				:     Optional[Any]							, _a				:     Tuple							, _a				:     Any							, _a				:     List[Any]							, _a				:     List[str]							, _a				:     List[str]			)     ->   Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =AlbertForQuestionAnswering(config=_a			)
       model.to(_a			)
       model.eval()
       _SCREAMING_SNAKE_CASE							   =model(
           _a							, attention_mask=_a							, token_type_ids=_a							, start_positions=_a							, end_positions=_a							, )
       self.parent.assertEqual(result.start_logits.shape							, (self.batch_size, self.seq_length)			)
       self.parent.assertEqual(result.end_logits.shape							, (self.batch_size, self.seq_length)			)





  def 						A  (		self				:     Union[str, Any]							, _a				:     Dict							, _a				:     str							, _a				:     str							, _a				:     int							, _a				:     int							, _a				:     int							, _a				:     List[Any]			)     ->   Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.num_labels
       _SCREAMING_SNAKE_CASE							   =AlbertForSequenceClassification(_a			)
       model.to(_a			)
       model.eval()
       _SCREAMING_SNAKE_CASE							   =model(_a							, attention_mask=_a							, token_type_ids=_a							, labels=_a			)
       self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)			)





  def 						A  (		self				:     Union[str, Any]							, _a				:     str							, _a				:     Union[str, Any]							, _a				:     List[str]							, _a				:     List[str]							, _a				:     str							, _a				:     Dict							, _a				:     Tuple			)     ->   Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.num_labels
       _SCREAMING_SNAKE_CASE							   =AlbertForTokenClassification(config=_a			)
       model.to(_a			)
       model.eval()
       _SCREAMING_SNAKE_CASE							   =model(_a							, attention_mask=_a							, token_type_ids=_a							, labels=_a			)
       self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.num_labels)			)





  def 						A  (		self				:     Optional[Any]							, _a				:     Any							, _a				:     Any							, _a				:     Union[str, Any]							, _a				:     Dict							, _a				:     int							, _a				:     List[str]							, _a				:     Optional[int]			)     ->   Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.num_choices
       _SCREAMING_SNAKE_CASE							   =AlbertForMultipleChoice(config=_a			)
       model.to(_a			)
       model.eval()
       _SCREAMING_SNAKE_CASE							   =input_ids.unsqueeze(1			).expand(-1							, self.num_choices							, -1			).contiguous()
       _SCREAMING_SNAKE_CASE							   =token_type_ids.unsqueeze(1			).expand(-1							, self.num_choices							, -1			).contiguous()
       _SCREAMING_SNAKE_CASE							   =input_mask.unsqueeze(1			).expand(-1							, self.num_choices							, -1			).contiguous()
       _SCREAMING_SNAKE_CASE							   =model(
           _a							, attention_mask=_a							, token_type_ids=_a							, labels=_a							, )
       self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_choices)			)





  def 						A  (		self				:     Tuple			)     ->   List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.prepare_config_and_inputs()
       (
           (
           _SCREAMING_SNAKE_CASE
       )   ,						(
           _SCREAMING_SNAKE_CASE
       )   ,						(
           _SCREAMING_SNAKE_CASE
       )   ,						(
           _SCREAMING_SNAKE_CASE
       )   ,						(
           _SCREAMING_SNAKE_CASE
       )   ,						(
           _SCREAMING_SNAKE_CASE
       )   ,						(
           _SCREAMING_SNAKE_CASE
       )   ,						
       )							   =config_and_inputs
       _SCREAMING_SNAKE_CASE							   ={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict







@require_torch
class 							A__   (	A__					,       A__					,       unittest.TestCase  ):
  A__					=    (
      (
          AlbertModel,
          AlbertForPreTraining,
          AlbertForMaskedLM,
          AlbertForMultipleChoice,
          AlbertForSequenceClassification,
          AlbertForTokenClassification,
          AlbertForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  A__					=    (
      {
          'feature-extraction': AlbertModel,
          'fill-mask': AlbertForMaskedLM,
          'question-answering': AlbertForQuestionAnswering,
          'text-classification': AlbertForSequenceClassification,
          'token-classification': AlbertForTokenClassification,
          'zero-shot': AlbertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  A__					=    True





  def 						A  (		self				:     Tuple							, _a				:     Optional[int]							, _a				:     List[Any]							, _a				:     List[str]=False			)     ->   Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =super()._prepare_for_class(_a							, _a							, return_labels=_a			)

       if return_labels:
            if model_class in get_values(_a			):
                 _SCREAMING_SNAKE_CASE							   =torch.zeros(
                     (self.model_tester.batch_size, self.model_tester.seq_length)							, dtype=torch.long							, device=_a			)
                 _SCREAMING_SNAKE_CASE							   =torch.zeros(
                     self.model_tester.batch_size							, dtype=torch.long							, device=_a			)
       return inputs_dict





  def 						A  (		self				:     Tuple			)     ->   Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =AlbertModelTester(self			)
       _SCREAMING_SNAKE_CASE							   =ConfigTester(self							, config_class=_a							, hidden_size=37			)





  def 						A  (		self				:     Optional[Any]			)     ->   Any:





       '''simple docstring'''






       self.config_tester.run_common_tests()





  def 						A  (		self				:     List[str]			)     ->   List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_a			)





  def 						A  (		self				:     Any			)     ->   Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*_a			)





  def 						A  (		self				:     str			)     ->   Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_a			)





  def 						A  (		self				:     Dict			)     ->   str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_a			)





  def 						A  (		self				:     List[Any]			)     ->   int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_a			)





  def 						A  (		self				:     int			)     ->   List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_a			)





  def 						A  (		self				:     Tuple			)     ->   Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
            _SCREAMING_SNAKE_CASE							   =type
            self.model_tester.create_and_check_model(*_a			)





  @slow
  def 						A  (		self				:     Union[str, Any]			)     ->   List[str]:





       '''simple docstring'''






       for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE							   =AlbertModel.from_pretrained(_a			)
            self.assertIsNotNone(_a			)







@require_torch
class 							A__   (	unittest.TestCase  ):





  @slow
  def 						A  (		self				:     Optional[int]			)     ->   Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =AlbertModel.from_pretrained('albert-base-v2'			)
       _SCREAMING_SNAKE_CASE							   =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]			)
       _SCREAMING_SNAKE_CASE							   =torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
       with torch.no_grad():
            _SCREAMING_SNAKE_CASE							   =model(_a							, attention_mask=_a			)[0]
       _SCREAMING_SNAKE_CASE							   =torch.Size((1, 11, 768)			)
       self.assertEqual(output.shape							, _a			)
       _SCREAMING_SNAKE_CASE							   =torch.tensor(
           [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]]			)

       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]							, _a							, atol=1e-4			)			)



'''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 	_lowerCAmelCase (						_UpperCamelCase						:					Tuple							,			_UpperCamelCase						:					List[Any]    )				->      Dict:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =checkpoint

     _SCREAMING_SNAKE_CASE							   ={}

     _SCREAMING_SNAKE_CASE							   =vae_state_dict['encoder.conv_in.weight']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['encoder.conv_in.bias']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['encoder.conv_out.weight']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['encoder.conv_out.bias']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['encoder.norm_out.weight']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['encoder.norm_out.bias']

     _SCREAMING_SNAKE_CASE							   =vae_state_dict['decoder.conv_in.weight']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['decoder.conv_in.bias']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['decoder.conv_out.weight']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['decoder.conv_out.bias']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['decoder.norm_out.weight']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['decoder.norm_out.bias']

     _SCREAMING_SNAKE_CASE							   =vae_state_dict['quant_conv.weight']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['quant_conv.bias']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['post_quant_conv.weight']
     _SCREAMING_SNAKE_CASE							   =vae_state_dict['post_quant_conv.bias']

     # Retrieves the keys for the encoder down blocks only
     _SCREAMING_SNAKE_CASE							   =len({'.'.join(layer.split('.'    )[:3]    ) for layer in vae_state_dict if 'encoder.down' in layer}    )
     _SCREAMING_SNAKE_CASE							   ={
         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
     _SCREAMING_SNAKE_CASE							   =len({'.'.join(layer.split('.'    )[:3]    ) for layer in vae_state_dict if 'decoder.up' in layer}    )
     _SCREAMING_SNAKE_CASE							   ={
         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    ):
          _SCREAMING_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:
               _SCREAMING_SNAKE_CASE							   =vae_state_dict.pop(
                   f"encoder.down.{i}.downsample.conv.weight"    )
               _SCREAMING_SNAKE_CASE							   =vae_state_dict.pop(
                   f"encoder.down.{i}.downsample.conv.bias"    )

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

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

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

     _SCREAMING_SNAKE_CASE							   =[key for key in vae_state_dict if 'encoder.mid.attn' in key]
     _SCREAMING_SNAKE_CASE							   =renew_vae_attention_paths(_UpperCamelCase    )
     _SCREAMING_SNAKE_CASE							   ={'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    ):
          _SCREAMING_SNAKE_CASE							   =num_up_blocks - 1 - i
          _SCREAMING_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:
               _SCREAMING_SNAKE_CASE							   =vae_state_dict[
                   f"decoder.up.{block_id}.upsample.conv.weight"
               ]
               _SCREAMING_SNAKE_CASE							   =vae_state_dict[
                   f"decoder.up.{block_id}.upsample.conv.bias"
               ]

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

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

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

     _SCREAMING_SNAKE_CASE							   =[key for key in vae_state_dict if 'decoder.mid.attn' in key]
     _SCREAMING_SNAKE_CASE							   =renew_vae_attention_paths(_UpperCamelCase    )
     _SCREAMING_SNAKE_CASE							   ={'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							,			)				->      List[Any]:
     """simple docstring"""




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

     _SCREAMING_SNAKE_CASE							   =OmegaConf.load(_UpperCamelCase    )
     _SCREAMING_SNAKE_CASE							   =5_12
     _SCREAMING_SNAKE_CASE							   ='cuda' if torch.cuda.is_available() else 'cpu'
     if checkpoint_path.endswith('safetensors'    ):
          from safetensors import safe_open

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

     # Convert the VAE model.
     _SCREAMING_SNAKE_CASE							   =create_vae_diffusers_config(_UpperCamelCase							,			image_size=_UpperCamelCase    )
     _SCREAMING_SNAKE_CASE							   =custom_convert_ldm_vae_checkpoint(_UpperCamelCase							,			_UpperCamelCase    )

     _SCREAMING_SNAKE_CASE							   =AutoencoderKL(**_UpperCamelCase    )
     vae.load_state_dict(_UpperCamelCase    )
     vae.save_pretrained(_UpperCamelCase    )


if __name__ == "__main__":
     lowerCamelCase			:				Union[str, Any] 			=					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.")

     lowerCamelCase			:				List[str] 			=					parser.parse_args()

     vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
