


from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor






class      UpperCamelCase					:


   '''simple docstring'''







   def __init__(     self   ,							UpperCamelCase_   ,							UpperCamelCase_=3   ,							UpperCamelCase_=32   ,							UpperCamelCase_=3   ,							UpperCamelCase_=10   ,							UpperCamelCase_=[10, 20, 30, 40]   ,							UpperCamelCase_=[1, 1, 2, 1]   ,							UpperCamelCase_=True   ,							UpperCamelCase_=True   ,							UpperCamelCase_="relu"   ,							UpperCamelCase_=3   ,							UpperCamelCase_=None   ,							):
        lowercase_       :Any     =       parent
        lowercase_       :Union[str, Any]     =       batch_size
        lowercase_       :List[str]     =       image_size
        lowercase_       :Optional[int]     =       num_channels
        lowercase_       :Dict     =       embeddings_size
        lowercase_       :Optional[Any]     =       hidden_sizes
        lowercase_       :str     =       depths
        lowercase_       :Tuple     =       is_training
        lowercase_       :List[Any]     =       use_labels
        lowercase_       :str     =       hidden_act
        lowercase_       :int     =       num_labels
        lowercase_       :List[Any]     =       scope
        lowercase_       :Optional[int]     =       len(_A		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :str     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

        lowercase_       :Union[str, Any]     =       None
        if self.use_labels:
             lowercase_       :Optional[int]     =       ids_tensor([self.batch_size]   ,							self.num_labels		)

        lowercase_       :str     =       self.get_config()
        return config, pixel_values, labels
   def 		UpperCamelCase  (     self		):
        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   ,							)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :Optional[Any]     =       TFRegNetModel(config=_A		)
        lowercase_       :Any     =       model(_A   ,							training=_A		)
        # expected last hidden states: B, C, H // 32, W // 32
        self.parent.assertEqual(
            result.last_hidden_state.shape   ,							(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)   ,							)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :List[str]     =       self.num_labels
        lowercase_       :int     =       TFRegNetForImageClassification(_A		)
        lowercase_       :Any     =       model(_A   ,							labels=_A   ,							training=_A		)
        self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_labels)		)




   def 		UpperCamelCase  (     self		):
        lowercase_       :Dict     =       self.prepare_config_and_inputs()
        lowercase_       :Tuple     =       config_and_inputs
        lowercase_       :str     =       {'pixel_values': pixel_values}
        return config, inputs_dict






@require_tf
class      UpperCamelCase					(		a__						,      a__						,      unittest.TestCase     ):


   '''simple docstring'''







   lowercase					:	str									=(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
   lowercase					:	Union[str, Any]									=(
       {"""feature-extraction""": TFRegNetModel, """image-classification""": TFRegNetForImageClassification}
       if is_tf_available()
       else {}
   )

   lowercase					:	Union[str, Any]									=False
   lowercase					:	Tuple									=False
   lowercase					:	str									=False
   lowercase					:	Tuple									=False
   lowercase					:	List[Any]									=False
   def 		UpperCamelCase  (     self		):
        lowercase_       :List[Any]     =       TFRegNetModelTester(self		)
        lowercase_       :Optional[int]     =       ConfigTester(self   ,							config_class=_A   ,							has_text_modality=_A		)
   def 		UpperCamelCase  (     self		):
        return
   @unittest.skip(reason='''RegNet does not use inputs_embeds'''		)
   def 		UpperCamelCase  (     self		):
        pass
   @unittest.skipIf(
       not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''		)		) == 0   ,							reason='''TF does not support backprop for grouped convolutions on CPU.'''   ,							)
   @slow
   def 		UpperCamelCase  (     self		):
        super().test_keras_fit()
   @unittest.skip(reason='''RegNet does not support input and output embeddings'''		)
   def 		UpperCamelCase  (     self		):
        pass
   def 		UpperCamelCase  (     self		):
        lowercase_       :Union[str, Any]     =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_       :Union[str, Any]     =       model_class(_A		)
             lowercase_       :Tuple     =       inspect.signature(model.call		)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             lowercase_       :Optional[int]     =       [*signature.parameters.keys()]

             lowercase_       :Optional[int]     =       ['pixel_values']
             self.assertListEqual(arg_names[:1]   ,							_A		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :Any     =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_A		)
   def 		UpperCamelCase  (     self		):

        def check_hidden_states_output(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
             lowercase_       :List[Any]     =       model_class(_A		)
             lowercase_       :str     =       model(**self._prepare_for_class(_A   ,							_A		)   ,							training=_A		)

             lowercase_       :List[str]     =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

             lowercase_       :Optional[Any]     =       self.model_tester.num_stages
             self.assertEqual(len(_A		)   ,							expected_num_stages + 1		)

             # RegNet's feature maps are of shape (batch_size, num_channels, height, width)
             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]		)   ,							[self.model_tester.image_size // 2, self.model_tester.image_size // 2]   ,							)

        lowercase_       :Optional[int]     =       self.model_tester.prepare_config_and_inputs_for_common()
        lowercase_       :Optional[Any]     =       ['basic', 'bottleneck']
        for model_class in self.all_model_classes:
             for layer_type in layers_type:
                  lowercase_       :Tuple     =       layer_type
                  lowercase_       :Tuple     =       True
                  check_hidden_states_output(_A   ,							_A   ,							_A		)

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  lowercase_       :int     =       True

                  check_hidden_states_output(_A   ,							_A   ,							_A		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :Dict     =       self.model_tester.prepare_config_and_inputs_for_common()

        def check_equivalence(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_={}		):
             lowercase_       :Optional[int]     =       model(_A   ,							return_dict=_A   ,							**_A		)
             lowercase_       :List[str]     =       model(_A   ,							return_dict=_A   ,							**_A		).to_tuple()

             def recursive_check(UpperCamelCase_   ,							UpperCamelCase_		):
                  if isinstance(_A   ,							(List, Tuple)		):
                       for tuple_iterable_value, dict_iterable_value in zip(_A   ,							_A		):
                            recursive_check(_A   ,							_A		)
                  elif tuple_object is None:
                       return
                  else:
                       self.assertTrue(
                           all(tf.equal(_A   ,							_A		)		)   ,							msg=(
                               '''Tuple and dict output are not equal. Difference:'''
                               f" {tf.math.reduce_max(tf.abs(tuple_object - dict_object		)		)}"
                           )   ,							)

                  recursive_check(_A   ,							_A		)

        for model_class in self.all_model_classes:
             lowercase_       :Optional[int]     =       model_class(_A		)

             lowercase_       :List[Any]     =       self._prepare_for_class(_A   ,							_A		)
             lowercase_       :Any     =       self._prepare_for_class(_A   ,							_A		)
             check_equivalence(_A   ,							_A   ,							_A		)

             lowercase_       :int     =       self._prepare_for_class(_A   ,							_A   ,							return_labels=_A		)
             lowercase_       :Union[str, Any]     =       self._prepare_for_class(_A   ,							_A   ,							return_labels=_A		)
             check_equivalence(_A   ,							_A   ,							_A		)

             lowercase_       :Union[str, Any]     =       self._prepare_for_class(_A   ,							_A		)
             lowercase_       :Any     =       self._prepare_for_class(_A   ,							_A		)
             check_equivalence(_A   ,							_A   ,							_A   ,							{'''output_hidden_states''': True}		)

             lowercase_       :Tuple     =       self._prepare_for_class(_A   ,							_A   ,							return_labels=_A		)
             lowercase_       :Dict     =       self._prepare_for_class(_A   ,							_A   ,							return_labels=_A		)
             check_equivalence(_A   ,							_A   ,							_A   ,							{'''output_hidden_states''': True}		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :Dict     =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_A		)




   @slow
   def 		UpperCamelCase  (     self		):

        for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_       :Tuple     =       TFRegNetModel.from_pretrained(_A		)
             self.assertIsNotNone(_A		)




def     UpperCamelCase    (      )					->							Dict:
     '''simple docstring'''





     lowercase_       :List[str]     =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
     return image






@require_tf
@require_vision
class      UpperCamelCase					(		unittest.TestCase     ):


   '''simple docstring'''







   @cached_property
   def 		UpperCamelCase  (     self		):
        return (
            AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)
            if is_vision_available()
            else None
        )




   @slow
   def 		UpperCamelCase  (     self		):
        lowercase_       :Tuple     =       TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)

        lowercase_       :List[str]     =       self.default_image_processor
        lowercase_       :int     =       prepare_img()
        lowercase_       :Optional[int]     =       image_processor(images=_A   ,							return_tensors='''tf'''		)

        # forward pass
        lowercase_       :List[str]     =       model(**_A   ,							training=_A		)

        # verify the logits
        lowercase_       :int     =       tf.TensorShape((1, 1000)		)
        self.assertEqual(outputs.logits.shape   ,							_A		)

        lowercase_       :Optional[int]     =       tf.constant([-0.4180, -1.5051, -3.4836]		)

        tf.debugging.assert_near(outputs.logits[0, :3]   ,							_A   ,							atol=1E-4		)





import argparse

import torch

from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()




def     UpperCamelCase    (      _a       ,		_a       ,		_a		)					->							List[str]:
     '''simple docstring'''





     if openai_config_file == "":
          lowercase_       :str     =       OpenAIGPTConfig()
     else:
          lowercase_       :int     =       OpenAIGPTConfig.from_json_file(_a		)
     lowercase_       :int     =       OpenAIGPTModel(_a		)

     # Load weights from numpy
     load_tf_weights_in_openai_gpt(_a       ,		_a       ,		_a		)

     # Save pytorch-model
     lowercase_       :Optional[int]     =       pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME
     lowercase_       :List[str]     =       pytorch_dump_folder_path + '''/''' + CONFIG_NAME
     print(f"Save PyTorch model to {pytorch_weights_dump_path}"		)
     torch.save(model.state_dict()       ,		_a		)
     print(f"Save configuration file to {pytorch_config_dump_path}"		)
     with open(_a       ,		'''w'''       ,		encoding='''utf-8'''		) as f:
          f.write(config.to_json_string()		)


if __name__ == "__main__":
   SCREAMING_SNAKE_CASE       :		List[str]         =				argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       "--openai_checkpoint_folder_path",
       default=None,
       type=str,
       required=True,
       help="Path to the TensorFlow checkpoint path.",
   )
   parser.add_argument(
       "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
   )
   parser.add_argument(
       "--openai_config_file",
       default="",
       type=str,
       help=(
           "An optional config json file corresponding to the pre-trained OpenAI model. \n"
           "This specifies the model architecture."
       ),
   )
   SCREAMING_SNAKE_CASE       :		Optional[Any]         =				parser.parse_args()
   convert_openai_checkpoint_to_pytorch(
       args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path
   )

