





import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import UperNetForSemanticSegmentation
   from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor




class        _a    :

      """simple docstring"""


      def __init__(		self:			Optional[int]	,    __lowerCamelCase:			Tuple	,    __lowerCamelCase:			List[str]=13	,    __lowerCamelCase:			Optional[int]=32	,    __lowerCamelCase:			Optional[Any]=3	,    __lowerCamelCase:			List[Any]=4	,    __lowerCamelCase:			Any=[10, 20, 30, 40]	,    __lowerCamelCase:			Union[str, Any]=[2, 2, 3, 2]	,    __lowerCamelCase:			List[Any]=True	,    __lowerCamelCase:			List[Any]=True	,    __lowerCamelCase:			Dict=37	,    __lowerCamelCase:			Optional[Any]="gelu"	,    __lowerCamelCase:			Optional[int]=10	,    __lowerCamelCase:			List[str]=0.02	,    __lowerCamelCase:			Any=["stage2", "stage3", "stage4"]	,    __lowerCamelCase:			Any=3	,    __lowerCamelCase:			List[str]=None	,    ):
          '''simple docstring'''


          UpperCamelCase__:	Union[str, Any]					=     parent
          UpperCamelCase__:	Dict					=     batch_size
          UpperCamelCase__:	Optional[Any]					=     image_size
          UpperCamelCase__:	Optional[int]					=     num_channels
          UpperCamelCase__:	Optional[int]					=     num_stages
          UpperCamelCase__:	str					=     hidden_sizes
          UpperCamelCase__:	List[str]					=     depths
          UpperCamelCase__:	Dict					=     is_training
          UpperCamelCase__:	List[Any]					=     use_labels
          UpperCamelCase__:	Optional[int]					=     intermediate_size
          UpperCamelCase__:	Any					=     hidden_act
          UpperCamelCase__:	Any					=     type_sequence_label_size
          UpperCamelCase__:	Dict					=     initializer_range
          UpperCamelCase__:	str					=     out_features
          UpperCamelCase__:	Union[str, Any]					=     num_labels
          UpperCamelCase__:	Union[str, Any]					=     scope
          UpperCamelCase__:	Any					=     num_stages






      def 			UpperCAmelCase_       (		self:			Union[str, Any]					):
          '''simple docstring'''


          UpperCamelCase__:	List[Any]					=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

          UpperCamelCase__:	Any					=     None
          if self.use_labels:
              UpperCamelCase__:	Optional[Any]					=     ids_tensor([self.batch_size]	,    self.type_sequence_label_size					)

          UpperCamelCase__:	int					=     self.get_config()

          return config, pixel_values, labels






      def 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          return ConvNextConfig(
              num_channels=self.num_channels	,    num_stages=self.num_stages	,    hidden_sizes=self.hidden_sizes	,    depths=self.depths	,    is_training=self.is_training	,    intermediate_size=self.intermediate_size	,    hidden_act=self.hidden_act	,    out_features=self.out_features	,    )






      def 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          return UperNetConfig(
              backbone_config=self.get_backbone_config()	,    hidden_size=512	,    pool_scales=[1, 2, 3, 6]	,    use_auxiliary_head=__lowerCamelCase	,    auxiliary_loss_weight=0.4	,    auxiliary_in_channels=40	,    auxiliary_channels=256	,    auxiliary_num_convs=1	,    auxiliary_concat_input=__lowerCamelCase	,    loss_ignore_index=255	,    num_labels=self.num_labels	,    )






      def 			UpperCAmelCase_       (		self:			List[Any]	,    __lowerCamelCase:			Dict	,    __lowerCamelCase:			Any	,    __lowerCamelCase:			int					):
          '''simple docstring'''


          UpperCamelCase__:	List[Any]					=     UperNetForSemanticSegmentation(config=__lowerCamelCase					)
          model.to(__lowerCamelCase					)
          model.eval()
          UpperCamelCase__:	List[str]					=     model(__lowerCamelCase					)
          self.parent.assertEqual(
              result.logits.shape	,    (self.batch_size, self.num_labels, self.image_size, self.image_size)					)






      def 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[Any]					=     self.prepare_config_and_inputs()
          (
              (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, (
              UpperCamelCase__
          )				, 
          ):	Optional[int]					=     config_and_inputs
          UpperCamelCase__:	Union[str, Any]					=     {"pixel_values": pixel_values}
          return config, inputs_dict




@require_torch
class        _a    (    UpperCamelCase__						,			UpperCamelCase__						,			unittest.TestCase):

      """simple docstring"""


      UpperCamelCase__  		=       (UperNetForSemanticSegmentation,) if is_torch_available() else ()
      UpperCamelCase__  		=       {"""image-segmentation""": UperNetForSemanticSegmentation} if is_torch_available() else {}
      UpperCamelCase__  		=       False
      UpperCamelCase__  		=       False
      UpperCamelCase__  		=       False
      UpperCamelCase__  		=       False
      UpperCamelCase__  		=       False
      UpperCamelCase__  		=       False






      def 			UpperCAmelCase_       (		self:			Optional[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	List[str]					=     UperNetModelTester(self					)
          UpperCamelCase__:	Union[str, Any]					=     ConfigTester(self	,    config_class=__lowerCamelCase	,    has_text_modality=__lowerCamelCase	,    hidden_size=37					)






      def 			UpperCAmelCase_       (		self:			Optional[int]					):
          '''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 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          return






      def 			UpperCAmelCase_       (		self:			Union[str, Any]					):
          '''simple docstring'''


          UpperCamelCase__				, UpperCamelCase__:	str					=     self.model_tester.prepare_config_and_inputs_for_common()

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

              UpperCamelCase__:	str					=     ["pixel_values"]
              self.assertListEqual(arg_names[:1]	,    __lowerCamelCase					)






      def 			UpperCAmelCase_       (		self:			Optional[int]					):
          '''simple docstring'''


          UpperCamelCase__:	int					=     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCamelCase					)






      @unittest.skip(reason="UperNet does not use inputs_embeds"					)
      def 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          pass






      @unittest.skip(reason="UperNet does not support input and output embeddings"					)
      def 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          pass






      @unittest.skip(reason="UperNet does not have a base model"					)
      def 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          pass






      @unittest.skip(reason="UperNet does not have a base model"					)
      def 			UpperCAmelCase_       (		self:			Optional[Any]					):
          '''simple docstring'''


          pass






      @require_torch_multi_gpu
      @unittest.skip(reason="UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`"					)
      def 			UpperCAmelCase_       (		self:			Optional[int]					):
          '''simple docstring'''


          pass






      @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."					)
      def 			UpperCAmelCase_       (		self:			Union[str, Any]					):
          '''simple docstring'''


          pass






      def 			UpperCAmelCase_       (		self:			Any					):
          '''simple docstring'''


          def check_hidden_states_output(__lowerCamelCase:			List[str]	,    __lowerCamelCase:			int	,    __lowerCamelCase:			Optional[Any]					):
              UpperCamelCase__:	Union[str, Any]					=     model_class(__lowerCamelCase					)
              model.to(__lowerCamelCase					)
              model.eval()

              with torch.no_grad():
                  UpperCamelCase__:	List[str]					=     model(**self._prepare_for_class(__lowerCamelCase	,    __lowerCamelCase					)					)

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

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

              # ConvNext'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 // 4, self.model_tester.image_size // 4]	,    )

          UpperCamelCase__				, UpperCamelCase__:	str					=     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCamelCase__:	Any					=     True
              check_hidden_states_output(__lowerCamelCase	,    __lowerCamelCase	,    __lowerCamelCase					)

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

              check_hidden_states_output(__lowerCamelCase	,    __lowerCamelCase	,    __lowerCamelCase					)






      def 			UpperCAmelCase_       (		self:			Union[str, Any]					):
          '''simple docstring'''


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

          UpperCamelCase__:	Tuple					=     _config_zero_init(__lowerCamelCase					)
          UpperCamelCase__:	str					=     _config_zero_init(configs_no_init.backbone_config					)
          for model_class in self.all_model_classes:
              UpperCamelCase__:	Union[str, Any]					=     model_class(config=__lowerCamelCase					)
              for name, param in model.named_parameters():
                  if param.requires_grad:
                      self.assertIn(
                          ((param.data.mean() * 1e9).round() / 1e9).item()	,    [0.0, 1.0]	,    msg=F"Parameter {name} of model {model_class} seems not properly initialized"	,    )






      @unittest.skip(reason="UperNet does not have tied weights"					)
      def 			UpperCAmelCase_       (		self:			Dict					):
          '''simple docstring'''


          pass






      @slow
      def 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCamelCase__:	Union[str, Any]					=     UperNetForSemanticSegmentation.from_pretrained(__lowerCamelCase					)
              self.assertIsNotNone(__lowerCamelCase					)



def 				lowerCAmelCase_ (							):
    UpperCamelCase__:	str					=     hf_hub_download(
        repo_id="hf-internal-testing/fixtures_ade20k"    ,repo_type="dataset"    ,filename="ADE_val_00000001.jpg")
    UpperCamelCase__:	Optional[Any]					=     Image.open(A_).convert("RGB")
    return image




@require_torch
@require_vision
@slow
class        _a    (    unittest.TestCase):

      """simple docstring"""


      def 			UpperCAmelCase_       (		self:			List[Any]					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[Any]					=     AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"					)
          UpperCamelCase__:	Dict					=     UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"					).to(__lowerCamelCase					)

          UpperCamelCase__:	str					=     prepare_img()
          UpperCamelCase__:	Dict					=     processor(images=__lowerCamelCase	,    return_tensors="pt"					).to(__lowerCamelCase					)

          with torch.no_grad():
              UpperCamelCase__:	int					=     model(**__lowerCamelCase					)

          UpperCamelCase__:	Any					=     torch.Size((1, model.config.num_labels, 512, 512)					)
          self.assertEqual(outputs.logits.shape	,    __lowerCamelCase					)

          UpperCamelCase__:	str					=     torch.tensor(
              [[-7.5_958, -7.5_958, -7.4_302], [-7.5_958, -7.5_958, -7.4_302], [-7.4_797, -7.4_797, -7.3_068]]					).to(__lowerCamelCase					)
          self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]	,    __lowerCamelCase	,    atol=1e-4					)					)






      def 			UpperCAmelCase_       (		self:			int					):
          '''simple docstring'''


          UpperCamelCase__:	Optional[Any]					=     AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"					)
          UpperCamelCase__:	str					=     UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"					).to(__lowerCamelCase					)

          UpperCamelCase__:	List[Any]					=     prepare_img()
          UpperCamelCase__:	int					=     processor(images=__lowerCamelCase	,    return_tensors="pt"					).to(__lowerCamelCase					)

          with torch.no_grad():
              UpperCamelCase__:	int					=     model(**__lowerCamelCase					)

          UpperCamelCase__:	Union[str, Any]					=     torch.Size((1, model.config.num_labels, 512, 512)					)
          self.assertEqual(outputs.logits.shape	,    __lowerCamelCase					)

          UpperCamelCase__:	int					=     torch.tensor(
              [[-8.8_110, -8.8_110, -8.6_521], [-8.8_110, -8.8_110, -8.6_521], [-8.7_746, -8.7_746, -8.6_130]]					).to(__lowerCamelCase					)
          self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]	,    __lowerCamelCase	,    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 				lowerCAmelCase_ (							A_    ,A_    ,A_):
    # Construct model
    if openai_config_file == "":
        UpperCamelCase__:	Optional[Any]					=     OpenAIGPTConfig()
    else:
        UpperCamelCase__:	List[Any]					=     OpenAIGPTConfig.from_json_file(A_)
    UpperCamelCase__:	Optional[Any]					=     OpenAIGPTModel(A_)

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

    # Save pytorch-model
    UpperCamelCase__:	Tuple					=     pytorch_dump_folder_path + "/" + WEIGHTS_NAME
    UpperCamelCase__:	List[Any]					=     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__":
   A__:    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.'''
       ),
   )
   A__:    Union[str, Any]  	=	parser.parse_args()
   convert_openai_checkpoint_to_pytorch(
       args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path
   )

