


"""simple docstring"""



import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class _lowerCAmelCase    (				_snake_case   ,							unittest.TestCase     ):
   __UpperCAmelCase			:	Tuple							  =  KandinskyVaaPriorPipeline
   __UpperCAmelCase			:	Optional[int]							  =  ['''prompt''']
   __UpperCAmelCase			:	List[Any]							  =  ['''prompt''', '''negative_prompt''']
   __UpperCAmelCase			:	List[str]							  =  [
       '''num_images_per_prompt''',
       '''generator''',
       '''num_inference_steps''',
       '''latents''',
       '''negative_prompt''',
       '''guidance_scale''',
       '''output_type''',
       '''return_dict''',
   ]
   __UpperCAmelCase			:	Union[str, Any]							  =  False







   @property
   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       return 32







   @property
   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       return 32







   @property
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       return self.time_input_dim







   @property
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       return self.time_input_dim * 4







   @property
   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       return 100







   @property
   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				List[str]				 =   CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"						)
       return tokenizer







   @property
   def 			lowerCamelCase		(     self						)     ->		List[str]:

       '''simple docstring'''




       torch.manual_seed(0						)
       snake_case   :				Optional[Any]				 =   CLIPTextConfig(
           bos_token_id=0						,     eos_token_id=2						,     hidden_size=self.text_embedder_hidden_size						,     projection_dim=self.text_embedder_hidden_size						,     intermediate_size=37						,     layer_norm_eps=1e-05						,     num_attention_heads=4						,     num_hidden_layers=5						,     pad_token_id=1						,     vocab_size=1000						,     )
       return CLIPTextModelWithProjection(UpperCamelCase__						)







   @property
   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       torch.manual_seed(0						)

       snake_case   :				Tuple				 =   {
           "num_attention_heads": 2,
           "attention_head_dim": 12,
           "embedding_dim": self.text_embedder_hidden_size,
           "num_layers": 1,
       }

       snake_case   :				int				 =   PriorTransformer(**UpperCamelCase__						)
       # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
       snake_case   :				List[Any]				 =   nn.Parameter(torch.ones(model.clip_std.shape						)						)
       return model







   @property
   def 			lowerCamelCase		(     self						)     ->		Union[str, Any]:

       '''simple docstring'''




       torch.manual_seed(0						)
       snake_case   :				Optional[Any]				 =   CLIPVisionConfig(
           hidden_size=self.text_embedder_hidden_size						,     image_size=224						,     projection_dim=self.text_embedder_hidden_size						,     intermediate_size=37						,     num_attention_heads=4						,     num_channels=3						,     num_hidden_layers=5						,     patch_size=14						,     )

       snake_case   :				str				 =   CLIPVisionModelWithProjection(UpperCamelCase__						)
       return model







   @property
   def 			lowerCamelCase		(     self						)     ->		Optional[int]:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   CLIPImageProcessor(
           crop_size=224						,     do_center_crop=UpperCamelCase__						,     do_normalize=UpperCamelCase__						,     do_resize=UpperCamelCase__						,     image_mean=[0.48145466, 0.4578275, 0.40821073]						,     image_std=[0.26862954, 0.26130258, 0.27577711]						,     resample=3						,     size=224						,     )

       return image_processor







   def 			lowerCamelCase		(     self						)     ->		Optional[int]:

       '''simple docstring'''




       snake_case   :				int				 =   self.dummy_prior
       snake_case   :				Dict				 =   self.dummy_image_encoder
       snake_case   :				Dict				 =   self.dummy_text_encoder
       snake_case   :				Optional[int]				 =   self.dummy_tokenizer
       snake_case   :				Optional[Any]				 =   self.dummy_image_processor

       snake_case   :				int				 =   UnCLIPScheduler(
           variance_type="fixed_small_log"						,     prediction_type="sample"						,     num_train_timesteps=1000						,     clip_sample=UpperCamelCase__						,     clip_sample_range=10.0						,     )

       snake_case   :				str				 =   {
           "prior": prior,
           "image_encoder": image_encoder,
           "text_encoder": text_encoder,
           "tokenizer": tokenizer,
           "scheduler": scheduler,
           "image_processor": image_processor,
       }

       return components







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__=0						)     ->		Any:

       '''simple docstring'''




       if str(UpperCamelCase__						).startswith("mps"						):
           snake_case   :				Optional[int]				 =   torch.manual_seed(UpperCamelCase__						)
       else:
           snake_case   :				Tuple				 =   torch.Generator(device=UpperCamelCase__						).manual_seed(UpperCamelCase__						)
       snake_case   :				Tuple				 =   {
           "prompt": "horse",
           "generator": generator,
           "guidance_scale": 4.0,
           "num_inference_steps": 2,
           "output_type": "np",
       }
       return inputs







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Union[str, Any]				 =   "cpu"

       snake_case   :				List[Any]				 =   self.get_dummy_components()

       snake_case   :				List[str]				 =   self.pipeline_class(**UpperCamelCase__						)
       snake_case   :				int				 =   pipe.to(UpperCamelCase__						)

       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Any				 =   pipe(**self.get_dummy_inputs(UpperCamelCase__						)						)
       snake_case   :				List[str]				 =   output.image_embeds

       snake_case   :				Optional[Any]				 =   pipe(
           **self.get_dummy_inputs(UpperCamelCase__						)						,     return_dict=UpperCamelCase__						,     )[0]

       snake_case   :				Tuple				 =   image[0, -10:]
       snake_case   :				Dict				 =   image_from_tuple[0, -10:]

       assert image.shape == (1, 32)

       snake_case   :				Optional[int]				 =   np.array(
           [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2
       assert np.abs(image_from_tuple_slice.flatten() - expected_slice						).max() < 1e-2







   @skip_mps
   def 			lowerCamelCase		(     self						)     ->		List[str]:

       '''simple docstring'''




       snake_case   :				Dict				 =   torch_device == "cpu"
       snake_case   :				List[str]				 =   True
       snake_case   :				Optional[int]				 =   False

       self._test_inference_batch_single_identical(
           test_max_difference=UpperCamelCase__						,     relax_max_difference=UpperCamelCase__						,     test_mean_pixel_difference=UpperCamelCase__						,     )







   @skip_mps
   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Tuple				 =   torch_device == "cpu"
       snake_case   :				str				 =   False

       self._test_attention_slicing_forward_pass(
           test_max_difference=UpperCamelCase__						,     test_mean_pixel_difference=UpperCamelCase__						,     )









"""simple docstring"""



import argparse
import os
import pickle
import sys

import torch

from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl
from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils
from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

# We do this to be able to load python 2 datasets pickles
# See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918
__snake_case             =   data_utils.TransfoXLTokenizer
__snake_case             =   data_utils.TransfoXLCorpus
__snake_case             =   data_utils
__snake_case             =   data_utils







def   __lowerCAmelCase						(						lowercase  :  Optional[int]						,							lowercase  :  int						,							lowercase  :  List[Any]						,							lowercase  :  Union[str, Any]			)     ->						List[Any]:


    """simple docstring"""





    if transfo_xl_dataset_file:
        # Convert a pre-processed corpus (see original TensorFlow repo)
        with open(lowercase						,							"rb"			) as fp:
            snake_case   :				int				 =   pickle.load(lowercase						,							encoding="latin1"			)
        # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term)
        snake_case   :				int				 =   pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["pretrained_vocab_file"]
        print(F'Save vocabulary to {pytorch_vocab_dump_path}'			)
        snake_case   :				str				 =   corpus.vocab.__dict__
        torch.save(lowercase						,							lowercase			)

        snake_case   :				str				 =   corpus.__dict__
        corpus_dict_no_vocab.pop("vocab"						,							lowercase			)
        snake_case   :				Dict				 =   pytorch_dump_folder_path + "/" + CORPUS_NAME
        print(F'Save dataset to {pytorch_dataset_dump_path}'			)
        torch.save(lowercase						,							lowercase			)

    if tf_checkpoint_path:
        # Convert a pre-trained TensorFlow model
        snake_case   :				Union[str, Any]				 =   os.path.abspath(lowercase			)
        snake_case   :				str				 =   os.path.abspath(lowercase			)

        print(F'Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.'			)
        # Initialise PyTorch model
        if transfo_xl_config_file == "":
            snake_case   :				int				 =   TransfoXLConfig()
        else:
            snake_case   :				Optional[int]				 =   TransfoXLConfig.from_json_file(lowercase			)
        print(F'Building PyTorch model from configuration: {config}'			)
        snake_case   :				str				 =   TransfoXLLMHeadModel(lowercase			)

        snake_case   :				str				 =   load_tf_weights_in_transfo_xl(lowercase						,							lowercase						,							lowercase			)
        # Save pytorch-model
        snake_case   :				Union[str, Any]				 =   os.path.join(lowercase						,							lowercase			)
        snake_case   :				Optional[Any]				 =   os.path.join(lowercase						,							lowercase			)
        print(F'Save PyTorch model to {os.path.abspath(lowercase			)}'			)
        torch.save(model.state_dict()						,							lowercase			)
        print(F'Save configuration file to {os.path.abspath(lowercase			)}'			)
        with open(lowercase						,							"w"						,							encoding="utf-8"			) as f:
            f.write(config.to_json_string()			)


if __name__ == "__main__":
  __snake_case             =   argparse.ArgumentParser()
  parser.add_argument(
      """--pytorch_dump_folder_path""",
      default=None,
      type=str,
      required=True,
      help="""Path to the folder to store the PyTorch model or dataset/vocab.""",
  )
  parser.add_argument(
      """--tf_checkpoint_path""",
      default="""""",
      type=str,
      help="""An optional path to a TensorFlow checkpoint path to be converted.""",
  )
  parser.add_argument(
      """--transfo_xl_config_file""",
      default="""""",
      type=str,
      help=(
          """An optional config json file corresponding to the pre-trained BERT model. \n"""
          """This specifies the model architecture."""
      ),
  )
  parser.add_argument(
      """--transfo_xl_dataset_file""",
      default="""""",
      type=str,
      help="""An optional dataset file to be converted in a vocabulary.""",
  )
  __snake_case             =   parser.parse_args()
  convert_transfo_xl_checkpoint_to_pytorch(
      args.tf_checkpoint_path,
      args.transfo_xl_config_file,
      args.pytorch_dump_folder_path,
      args.transfo_xl_dataset_file,
  )





