

'''simple docstring'''







import argparse

import torch

from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__,							UpperCAmelCase__					)      ->      Any:
       # Construct model
       if gpta_config_file == "":
              A_						       =   GPTaConfig()
       else:
              A_						       =   GPTaConfig.from_json_file(UpperCAmelCase__					)
       A_						       =   GPTaModel(UpperCAmelCase__					)

       # Load weights from numpy
       load_tf_weights_in_gpta(UpperCAmelCase__,							UpperCAmelCase__,							UpperCAmelCase__					)

       # Save pytorch-model
       A_						       =   pytorch_dump_folder_path + """/""" + WEIGHTS_NAME
       A_						       =   pytorch_dump_folder_path + """/""" + CONFIG_NAME
       print(F'''Save PyTorch model to {pytorch_weights_dump_path}'''					)
       torch.save(model.state_dict(),							UpperCAmelCase__					)
       print(F'''Save configuration file to {pytorch_config_dump_path}'''					)
       with open(UpperCAmelCase__,							"""w""",							encoding="""utf-8"""					) as f:
              f.write(config.to_json_string()					)


if __name__ == "__main__":
      __lowerCamelCase									=     argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          '''--gpt2_checkpoint_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(
          '''--gpt2_config_file''',
          default='''''',
          type=str,
          help=(
              '''An optional config json file corresponding to the pre-trained OpenAI model. \n'''
              '''This specifies the model architecture.'''
          ),
      )
      __lowerCamelCase									=     parser.parse_args()
      convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)



'''simple docstring'''







import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
      import torch

      from transformers import (
          MODEL_MAPPING,
          SegformerForImageClassification,
          SegformerForSemanticSegmentation,
          SegformerModel,
      )
      from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import SegformerImageProcessor


class        A__   (   _snake_case      ):



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_						       =   self.config_class(**self.inputs_dict							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""hidden_sizes"""							)							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""num_attention_heads"""							)							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""num_encoder_blocks"""							)							)


class        A__   :



    def __init__(       self		,					UpperCamelCase__		,					UpperCamelCase__=13		,					UpperCamelCase__=64		,					UpperCamelCase__=3		,					UpperCamelCase__=4		,					UpperCamelCase__=[2, 2, 2, 2]		,					UpperCamelCase__=[8, 4, 2, 1]		,					UpperCamelCase__=[16, 32, 64, 128]		,					UpperCamelCase__=[1, 4, 8, 16]		,					UpperCamelCase__=[1, 2, 4, 8]		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__="gelu"		,					UpperCamelCase__=0.1		,					UpperCamelCase__=0.1		,					UpperCamelCase__=0.02		,					UpperCamelCase__=3		,					UpperCamelCase__=None		,					)    ->   Tuple:





           '''simple docstring'''
           A_						       =   parent
           A_						       =   batch_size
           A_						       =   image_size
           A_						       =   num_channels
           A_						       =   num_encoder_blocks
           A_						       =   sr_ratios
           A_						       =   depths
           A_						       =   hidden_sizes
           A_						       =   downsampling_rates
           A_						       =   num_attention_heads
           A_						       =   is_training
           A_						       =   use_labels
           A_						       =   hidden_act
           A_						       =   hidden_dropout_prob
           A_						       =   attention_probs_dropout_prob
           A_						       =   initializer_range
           A_						       =   num_labels
           A_						       =   scope



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

           A_						       =   None
           if self.use_labels:
                  A_						       =   ids_tensor([self.batch_size, self.image_size, self.image_size]		,					self.num_labels							)

           A_						       =   self.get_config()
           return config, pixel_values, labels



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





           '''simple docstring'''
           return SegformerConfig(
               image_size=self.image_size		,					num_channels=self.num_channels		,					num_encoder_blocks=self.num_encoder_blocks		,					depths=self.depths		,					hidden_sizes=self.hidden_sizes		,					num_attention_heads=self.num_attention_heads		,					hidden_act=self.hidden_act		,					hidden_dropout_prob=self.hidden_dropout_prob		,					attention_probs_dropout_prob=self.attention_probs_dropout_prob		,					initializer_range=self.initializer_range		,					)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   SegformerModel(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)
           A_						       =   A_						       =   self.image_size // (self.downsampling_rates[-1] * 2)
           self.parent.assertEqual(
               result.last_hidden_state.shape		,					(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Any:





           '''simple docstring'''
           A_						       =   self.num_labels
           A_						       =   SegformerForSemanticSegmentation(UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(
               result.logits.shape		,					(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)							)
           A_						       =   model(UpperCamelCase__		,					labels=UpperCamelCase__							)
           self.parent.assertEqual(
               result.logits.shape		,					(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)							)
           self.parent.assertGreater(result.loss		,					0.0							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Dict:





           '''simple docstring'''
           A_						       =   1
           A_						       =   SegformerForSemanticSegmentation(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   torch.randint(0		,					1		,					(self.batch_size, self.image_size, self.image_size)							).to(UpperCamelCase__							)
           A_						       =   model(UpperCamelCase__		,					labels=UpperCamelCase__							)
           self.parent.assertGreater(result.loss		,					0.0							)







    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.prepare_config_and_inputs()
           A_       ,      A_       ,      A_						       =   config_and_inputs
           A_						       =   {"""pixel_values""": pixel_values}
           return config, inputs_dict


@require_torch
class        A__   (   _snake_case    ,    _snake_case    ,    unittest.TestCase      ):
    lowercase						       =		(
        (
            SegformerModel,
            SegformerForSemanticSegmentation,
            SegformerForImageClassification,
        )
        if is_torch_available()
        else ()
    )
    lowercase						       =		(
        {
            "feature-extraction": SegformerModel,
            "image-classification": SegformerForImageClassification,
            "image-segmentation": SegformerForSemanticSegmentation,
        }
        if is_torch_available()
        else {}
    )

    lowercase						       =		True
    lowercase						       =		False
    lowercase						       =		False
    lowercase						       =		False



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





           '''simple docstring'''
           A_						       =   SegformerModelTester(self							)
           A_						       =   SegformerConfigTester(self		,					config_class=UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           self.config_tester.run_common_tests()



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





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_binary_image_segmentation(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_segmentation(*UpperCamelCase__							)



    @unittest.skip("""SegFormer does not use inputs_embeds"""							)
    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           pass



    @unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""							)
    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           pass



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()

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

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



    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()
           A_						       =   True

           for model_class in self.all_model_classes:
                  A_						       =   True
                  A_						       =   False
                  A_						       =   True
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()
                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)
                  A_						       =   outputs.attentions

                  A_						       =   sum(self.model_tester.depths							)
                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

                  # check that output_attentions also work using config
                  del inputs_dict["output_attentions"]
                  A_						       =   True
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()
                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)
                  A_						       =   outputs.attentions

                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

                  # verify the first attentions (first block, first layer)
                  A_						       =   (self.model_tester.image_size // 4) ** 2
                  A_						       =   (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                  self.assertListEqual(
                      list(attentions[0].shape[-3:]							)		,					[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]		,					)

                  # verify the last attentions (last block, last layer)
                  A_						       =   (self.model_tester.image_size // 32) ** 2
                  A_						       =   (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
                  self.assertListEqual(
                      list(attentions[-1].shape[-3:]							)		,					[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]		,					)
                  A_						       =   len(UpperCamelCase__							)

                  # Check attention is always last and order is fine
                  A_						       =   True
                  A_						       =   True
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()
                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)

                  self.assertEqual(out_len + 1		,					len(UpperCamelCase__							)							)

                  A_						       =   outputs.attentions

                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)
                  # verify the first attentions (first block, first layer)
                  A_						       =   (self.model_tester.image_size // 4) ** 2
                  A_						       =   (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                  self.assertListEqual(
                      list(self_attentions[0].shape[-3:]							)		,					[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]		,					)



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           def check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							):
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()

                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)

                  A_						       =   outputs.hidden_states

                  A_						       =   self.model_tester.num_encoder_blocks
                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

                  # verify the first hidden states (first block)
                  self.assertListEqual(
                      list(hidden_states[0].shape[-3:]							)		,					[
                          self.model_tester.hidden_sizes[0],
                          self.model_tester.image_size // 4,
                          self.model_tester.image_size // 4,
                      ]		,					)

           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()

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

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  A_						       =   True

                  check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)



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





           '''simple docstring'''
           if not self.model_tester.is_training:
                  return

           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()
           A_						       =   True

           for model_class in self.all_model_classes:
                  if model_class in get_values(UpperCamelCase__							):
                         continue

                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.train()
                  A_						       =   self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__		,					return_labels=UpperCamelCase__							)
                  A_						       =   model(**UpperCamelCase__							).loss
                  loss.backward()



    @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""							)
    def        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           pass







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





           '''simple docstring'''
           for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A_						       =   SegformerModel.from_pretrained(UpperCamelCase__							)
                  self.assertIsNotNone(UpperCamelCase__							)

def    UpperCAmelCase__     (   )      ->      Any:
       A_						       =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
       return image







@require_torch
class        A__   (   unittest.TestCase      ):



    @slow
    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""							).to(
               UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

           A_						       =   torch.Size((1, model.config.num_labels, 128, 128)							)
           self.assertEqual(outputs.logits.shape		,					UpperCamelCase__							)

           A_						       =   torch.tensor(
               [
                   [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
                   [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
                   [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
               ]							).to(UpperCamelCase__							)
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]		,					UpperCamelCase__		,					atol=1e-4							)							)



    @slow
    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained(
               """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""							).to(UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

           A_						       =   torch.Size((1, model.config.num_labels, 128, 128)							)
           self.assertEqual(outputs.logits.shape		,					UpperCamelCase__							)

           A_						       =   torch.tensor(
               [
                   [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
                   [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
                   [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
               ]							).to(UpperCamelCase__							)
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]		,					UpperCamelCase__		,					atol=1e-1							)							)







    @slow
    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""							).to(
               UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

           A_						       =   outputs.logits.detach().cpu()

           A_						       =   image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__		,					target_sizes=[(500, 300)]							)
           A_						       =   torch.Size((500, 300)							)
           self.assertEqual(segmentation[0].shape		,					UpperCamelCase__							)

           A_						       =   image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__							)
           A_						       =   torch.Size((128, 128)							)
           self.assertEqual(segmentation[0].shape		,					UpperCamelCase__							)
