





"""simple docstring"""




import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
    import jax
    import jax.numpy as jnp

    from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor





class 			A__   (							unittest.TestCase     ):




   """simple docstring"""



   def __init__( self ,  lowercase ,  lowercase=3 ,  lowercase=32 ,  lowercase=3 ,  lowercase=10 ,  lowercase=[10, 20, 30, 40] ,  lowercase=[1, 1, 2, 1] ,  lowercase=True ,  lowercase=True ,  lowercase="relu" ,  lowercase=3 ,  lowercase=None ,  )      ->						Optional[Any]:







          '''simple docstring'''

          a__						:     List[Any]			=    parent
          a__						:     List[str]			=    batch_size
          a__						:     Dict			=    image_size
          a__						:     Any			=    num_channels
          a__						:     Union[str, Any]			=    embeddings_size
          a__						:     Dict			=    hidden_sizes
          a__						:     List[str]			=    depths
          a__						:     int			=    is_training
          a__						:     Tuple			=    use_labels
          a__						:     List[Any]			=    hidden_act
          a__						:     Tuple			=    num_labels
          a__						:     Optional[int]			=    scope
          a__						:     str			=    len(lowercase)

   def 							__lowercase		( self)      ->						int:







          '''simple docstring'''

          a__						:     str			=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

          a__						:     List[str]			=    self.get_config()

          return config, pixel_values

   def 							__lowercase		( self)      ->						int:







          '''simple docstring'''

          return RegNetConfig(
              num_channels=self.num_channels ,  embeddings_size=self.embeddings_size ,  hidden_sizes=self.hidden_sizes ,  depths=self.depths ,  hidden_act=self.hidden_act ,  num_labels=self.num_labels ,  image_size=self.image_size ,  )

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







          '''simple docstring'''

          a__						:     int			=    FlaxRegNetModel(config=lowercase)
          a__						:     Tuple			=    model(lowercase)

          # Output shape (b, c, h, w)
          self.parent.assertEqual(
              result.last_hidden_state.shape ,  (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) ,  )

   def 							__lowercase		( self ,  lowercase ,  lowercase)      ->						str:







          '''simple docstring'''

          a__						:     Any			=    self.num_labels
          a__						:     int			=    FlaxRegNetForImageClassification(config=lowercase)
          a__						:     str			=    model(lowercase)
          self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.num_labels))

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







          '''simple docstring'''

          a__						:     List[str]			=    self.prepare_config_and_inputs()
          a__						:     Any			=    config_and_inputs
          a__						:     Optional[int]			=    {'pixel_values': pixel_values}
          return config, inputs_dict





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




   """simple docstring"""



   __A       :    Tuple	  =  (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

   __A       :    Dict	  =  False
   __A       :    str	  =  False
   __A       :    Optional[int]	  =  False

   def 							__lowercase		( self)      ->						None:







          '''simple docstring'''

          a__						:     Dict			=    FlaxRegNetModelTester(self)
          a__						:     Optional[int]			=    ConfigTester(self ,  config_class=lowercase ,  has_text_modality=lowercase)

   def 							__lowercase		( self)      ->						str:







          '''simple docstring'''

          self.create_and_test_config_common_properties()
          self.config_tester.create_and_test_config_to_json_string()
          self.config_tester.create_and_test_config_to_json_file()
          self.config_tester.create_and_test_config_from_and_save_pretrained()
          self.config_tester.create_and_test_config_with_num_labels()
          self.config_tester.check_config_can_be_init_without_params()
          self.config_tester.check_config_arguments_init()

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







          '''simple docstring'''

          return

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







          '''simple docstring'''

          a__						:     Tuple			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase)

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







          '''simple docstring'''

          a__						:     Optional[int]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*lowercase)

   @unittest.skip(reason='RegNet does not use inputs_embeds')
   def 							__lowercase		( self)      ->						Optional[int]:







          '''simple docstring'''

          pass

   @unittest.skip(reason='RegNet does not support input and output embeddings')
   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          pass

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







          '''simple docstring'''

          a__						:     int			=    self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 a__						:     Dict			=    model_class(lowercase)
                 a__						:     List[str]			=    inspect.signature(model.__call__)
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 a__						:     List[str]			=    [*signature.parameters.keys()]

                 a__						:     int			=    ['pixel_values']
                 self.assertListEqual(arg_names[:1] ,  lowercase)

   def 							__lowercase		( self)      ->						str:







          '''simple docstring'''

          def check_hidden_states_output(lowercase ,  lowercase ,  lowercase):
                 a__						:     str			=    model_class(lowercase)

                 a__						:     str			=    model(**self._prepare_for_class(lowercase ,  lowercase))

                 a__						:     Any			=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                 a__						:     List[Any]			=    self.model_tester.num_stages
                 self.assertEqual(len(lowercase) ,  expected_num_stages + 1)

          a__						:     Optional[int]			=    self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 a__						:     List[str]			=    True
                 check_hidden_states_output(lowercase ,  lowercase ,  lowercase)

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 a__						:     Optional[Any]			=    True

                 check_hidden_states_output(lowercase ,  lowercase ,  lowercase)

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







          '''simple docstring'''

          a__						:     Optional[Any]			=    self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 with self.subTest(model_class.__name__):
                        a__						:     List[str]			=    self._prepare_for_class(lowercase ,  lowercase)
                        a__						:     str			=    model_class(lowercase)

                        @jax.jit
                        def model_jitted(lowercase ,  **lowercase):
                               return model(pixel_values=lowercase ,  **lowercase)

                        with self.subTest('JIT Enabled'):
                               a__						:     int			=    model_jitted(**lowercase).to_tuple()

                        with self.subTest('JIT Disabled'):
                               with jax.disable_jit():
                                      a__						:     int			=    model_jitted(**lowercase).to_tuple()

                        self.assertEqual(len(lowercase) ,  len(lowercase))
                        for jitted_output, output in zip(lowercase ,  lowercase):
                               self.assertEqual(jitted_output.shape ,  output.shape)



def A_     (		)  ->		Dict:
       a__						:     Optional[int]			=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
       return image





@require_flax
class 			A__   (							unittest.TestCase     ):




   """simple docstring"""



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







          '''simple docstring'''

          return AutoImageProcessor.from_pretrained('facebook/regnet-y-040') if is_vision_available() else None

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







          '''simple docstring'''

          a__						:     Union[str, Any]			=    FlaxRegNetForImageClassification.from_pretrained('facebook/regnet-y-040')

          a__						:     Union[str, Any]			=    self.default_image_processor
          a__						:     Optional[int]			=    prepare_img()
          a__						:     List[str]			=    image_processor(images=lowercase ,  return_tensors='np')

          a__						:     Dict			=    model(**lowercase)

          # verify the logits
          a__						:     Optional[Any]			=    (1, 1000)
          self.assertEqual(outputs.logits.shape ,  lowercase)

          a__						:     Tuple			=    jnp.array([-0.41_80, -1.50_51, -3.48_36])

          self.assertTrue(jnp.allclose(outputs.logits[0, :3] ,  lowercase ,  atol=1e-4))









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,
    )


