



import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()





class 		UpperCAmelCase						(						snake_case_       ,snake_case_       ,unittest.TestCase				):
   SCREAMING_SNAKE_CASE__							=     VQModel
   SCREAMING_SNAKE_CASE__							=     '''sample'''





   @property
   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase=(32, 32)			):
         _lowerCAmelCase 			=  4
         _lowerCAmelCase 			=  3

         _lowerCAmelCase 			=  floats_tensor((batch_size, num_channels) + sizes			).to(_lowerCAmelCase			)

         return {"sample": image}





   @property
   def 	__lowerCAmelCase							(	self			):
         return (3, 32, 32)





   @property
   def 	__lowerCAmelCase							(	self			):
         return (3, 32, 32)





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  {
             '''block_out_channels''': [32, 64],
             '''in_channels''': 3,
             '''out_channels''': 3,
             '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],
             '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],
             '''latent_channels''': 3,
         }
         _lowerCAmelCase 			=  self.dummy_input
         return init_dict, inputs_dict





   def 	__lowerCAmelCase							(	self			):
         pass





   def 	__lowerCAmelCase							(	self			):
         pass





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase			,   _lowerCAmelCase 			=  VQModel.from_pretrained('''fusing/vqgan-dummy'''       ,     output_loading_info=_lowerCAmelCase			)
         self.assertIsNotNone(_lowerCAmelCase			)
         self.assertEqual(len(loading_info['''missing_keys''']			)       ,     0			)

         model.to(_lowerCAmelCase			)
         _lowerCAmelCase 			=  model(**self.dummy_input			)

         assert image is not None, "Make sure output is not None"





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  VQModel.from_pretrained('''fusing/vqgan-dummy'''			)
         model.to(_lowerCAmelCase			).eval()

         torch.manual_seed(0			)
         if torch.cuda.is_available():
               torch.cuda.manual_seed_all(0			)

         _lowerCAmelCase 			=  torch.randn(1       ,     model.config.in_channels       ,     model.config.sample_size       ,     model.config.sample_size			)
         _lowerCAmelCase 			=  image.to(_lowerCAmelCase			)
         with torch.no_grad():
               _lowerCAmelCase 			=  model(_lowerCAmelCase			).sample

         _lowerCAmelCase 			=  output[0, -1, -3:, -3:].flatten().cpu()
         # fmt: off
         _lowerCAmelCase 			=  torch.tensor([-0.0_153, -0.4_044, -0.1_880, -0.5_161, -0.2_418, -0.4_072, -0.1_612, -0.0_633, -0.0_143]			)
         # fmt: on
         self.assertTrue(torch.allclose(_lowerCAmelCase       ,     _lowerCAmelCase       ,     atol=1E-3			)			)




import argparse

import torch

from ...utils import logging
from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert


logging.set_verbosity_info()
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	Optional[Any]     ,				_SCREAMING_SNAKE_CASE   :	List[Any]     ,				_SCREAMING_SNAKE_CASE   :	List[Any]				)->Dict:
      # Initialise PyTorch model
      _lowerCAmelCase 			=  AlbertConfig.from_json_file(_SCREAMING_SNAKE_CASE				)
      print(f'''Building PyTorch model from configuration: {config}'''				)
      _lowerCAmelCase 			=  AlbertForPreTraining(_SCREAMING_SNAKE_CASE				)

      # Load weights from tf checkpoint
      load_tf_weights_in_albert(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				)

      # Save pytorch-model
      print(f'''Save PyTorch model to {pytorch_dump_path}'''				)
      torch.save(model.state_dict()     ,				_SCREAMING_SNAKE_CASE				)


if __name__ == "__main__":
    UpperCAmelCase_									=							argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
    )
    parser.add_argument(
        "--albert_config_file",
        default=None,
        type=str,
        required=True,
        help=(
            "The config json file corresponding to the pre-trained ALBERT model. \n"
            "This specifies the model architecture."
        ),
    )
    parser.add_argument(
        "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
    )
    UpperCAmelCase_									=							parser.parse_args()
    convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)