





from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
    import tensorflow as tf

    from transformers import TFBlipTextModel
    from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST





class 			A__   :




   """simple docstring"""



   def __init__( self ,  lowercase ,  lowercase=12 ,  lowercase=7 ,  lowercase=True ,  lowercase=True ,  lowercase=True ,  lowercase=99 ,  lowercase=32 ,  lowercase=32 ,  lowercase=2 ,  lowercase=4 ,  lowercase=37 ,  lowercase=0.1 ,  lowercase=0.1 ,  lowercase=512 ,  lowercase=0.02 ,  lowercase=0 ,  lowercase=None ,  )      ->						List[Any]:







          '''simple docstring'''

          a__						:     Optional[int]			=    parent
          a__						:     Any			=    batch_size
          a__						:     int			=    seq_length
          a__						:     Optional[int]			=    is_training
          a__						:     Optional[Any]			=    use_input_mask
          a__						:     Tuple			=    use_labels
          a__						:     List[str]			=    vocab_size
          a__						:     int			=    hidden_size
          a__						:     Optional[Any]			=    projection_dim
          a__						:     Optional[Any]			=    num_hidden_layers
          a__						:     List[str]			=    num_attention_heads
          a__						:     Optional[Any]			=    intermediate_size
          a__						:     Union[str, Any]			=    dropout
          a__						:     Tuple			=    attention_dropout
          a__						:     List[Any]			=    max_position_embeddings
          a__						:     int			=    initializer_range
          a__						:     Optional[Any]			=    scope
          a__						:     Optional[Any]			=    bos_token_id

   def 							__lowercase		( self)      ->						Dict:







          '''simple docstring'''

          a__						:     Tuple			=    ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size)

          a__						:     Tuple			=    None
          if self.use_input_mask:
                 a__						:     Dict			=    random_attention_mask([self.batch_size, self.seq_length])

          if input_mask is not None:
                 a__						:     Union[str, Any]			=    input_mask.numpy()
                 a__			,							a__						:     Union[str, Any]			=    input_mask.shape
                 a__						:     Optional[Any]			=    np.random.randint(1 ,  seq_length - 1 ,  size=(batch_size,))
                 for batch_idx, start_index in enumerate(lowercase):
                        a__						:     Optional[int]			=    1
                        a__						:     Tuple			=    0

          a__						:     Optional[Any]			=    self.get_config()

          return config, input_ids, tf.convert_to_tensor(lowercase)

   def 							__lowercase		( self)      ->						Dict:







          '''simple docstring'''

          return BlipTextConfig(
              vocab_size=self.vocab_size ,  hidden_size=self.hidden_size ,  projection_dim=self.projection_dim ,  num_hidden_layers=self.num_hidden_layers ,  num_attention_heads=self.num_attention_heads ,  intermediate_size=self.intermediate_size ,  dropout=self.dropout ,  attention_dropout=self.attention_dropout ,  max_position_embeddings=self.max_position_embeddings ,  initializer_range=self.initializer_range ,  bos_token_id=self.bos_token_id ,  )

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						Optional[Any]:







          '''simple docstring'''

          a__						:     List[str]			=    TFBlipTextModel(config=lowercase)
          a__						:     int			=    model(lowercase ,  attention_mask=lowercase ,  training=lowercase)
          a__						:     List[str]			=    model(lowercase ,  training=lowercase)
          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 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          a__						:     Optional[Any]			=    self.prepare_config_and_inputs()
          a__			,							a__			,							a__						:     Dict			=    config_and_inputs
          a__						:     Tuple			=    {'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict





@require_tf
class 			A__   (							__UpperCAmelCase			,  unittest.TestCase     ):




   """simple docstring"""



   __A       :    List[str]	  =  (TFBlipTextModel,) if is_tf_available() else ()
   __A       :    int	  =  False
   __A       :    Dict	  =  False
   __A       :    Tuple	  =  False

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







          '''simple docstring'''

          a__						:     str			=    BlipTextModelTester(self)
          a__						:     int			=    ConfigTester(self ,  config_class=lowercase ,  hidden_size=37)

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







          '''simple docstring'''

          self.config_tester.run_common_tests()

   def 							__lowercase		( self)      ->						str:







          '''simple docstring'''

          a__						:     List[str]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase)

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







          '''simple docstring'''

          pass

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          pass

   @unittest.skip(reason='Blip does not use inputs_embeds')
   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          pass

   @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING')
   def 							__lowercase		( self)      ->						str:







          '''simple docstring'''

          pass

   @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING')
   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          pass

   @slow
   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 a__						:     Dict			=    TFBlipTextModel.from_pretrained(lowercase)
                 self.assertIsNotNone(lowercase)

   def 							__lowercase		( self ,  lowercase=True)      ->						List[str]:







          '''simple docstring'''

          super().test_pt_tf_model_equivalence(allow_missing_keys=lowercase)









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
lowercase				:	Union[str, Any]      						=  data_utils.TransfoXLTokenizer
lowercase				:	Optional[int]      						=  data_utils.TransfoXLCorpus
lowercase				:	List[Any]      						=  data_utils
lowercase				:	Tuple      						=  data_utils



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

              a__						:     Dict			=    corpus.__dict__
              corpus_dict_no_vocab.pop('vocab'		,				A__    )
              a__						:     Optional[int]			=    pytorch_dump_folder_path + '/' + CORPUS_NAME
              print(F'Save dataset to {pytorch_dataset_dump_path}'    )
              torch.save(A__		,				A__    )

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

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

              a__						:     int			=    load_tf_weights_in_transfo_xl(A__		,				A__		,				A__    )
              # Save pytorch-model
              a__						:     Any			=    os.path.join(A__		,				A__    )
              a__						:     Dict			=    os.path.join(A__		,				A__    )
              print(F'Save PyTorch model to {os.path.abspath(A__    )}'    )
              torch.save(model.state_dict()		,				A__    )
              print(F'Save configuration file to {os.path.abspath(A__    )}'    )
              with open(A__		,				'w'		,				encoding='utf-8'    ) as f:
                     f.write(config.to_json_string()    )


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


