





import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

   if is_vision_available():
      from transformers import MaskFormerImageProcessor

if is_vision_available():
   from PIL import Image




class        _a    :

      """simple docstring"""


      def __init__(		self:			Tuple	,    __lowerCamelCase:			Optional[int]	,    __lowerCamelCase:			Tuple=2	,    __lowerCamelCase:			List[Any]=True	,    __lowerCamelCase:			Dict=False	,    __lowerCamelCase:			Any=10	,    __lowerCamelCase:			Union[str, Any]=3	,    __lowerCamelCase:			Optional[Any]=32 * 4	,    __lowerCamelCase:			List[str]=32 * 6	,    __lowerCamelCase:			int=4	,    __lowerCamelCase:			Tuple=32	,    ):
          '''simple docstring'''


          UpperCamelCase__:	Dict					=     parent
          UpperCamelCase__:	int					=     batch_size
          UpperCamelCase__:	Union[str, Any]					=     is_training
          UpperCamelCase__:	Dict					=     use_auxiliary_loss
          UpperCamelCase__:	Dict					=     num_queries
          UpperCamelCase__:	Tuple					=     num_channels
          UpperCamelCase__:	Any					=     min_size
          UpperCamelCase__:	Tuple					=     max_size
          UpperCamelCase__:	Optional[Any]					=     num_labels
          UpperCamelCase__:	Dict					=     mask_feature_size






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


          UpperCamelCase__:	Any					=     floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]					).to(
              __lowerCamelCase					)

          UpperCamelCase__:	List[str]					=     torch.ones([self.batch_size, self.min_size, self.max_size]	,    device=__lowerCamelCase					)

          UpperCamelCase__:	Union[str, Any]					=     (
              torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]	,    device=__lowerCamelCase					) > 0.5
          ).float()
          UpperCamelCase__:	str					=     (torch.rand((self.batch_size, self.num_labels)	,    device=__lowerCamelCase					) > 0.5).long()

          UpperCamelCase__:	Any					=     self.get_config()
          return config, pixel_values, pixel_mask, mask_labels, class_labels






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


          return MaskFormerConfig.from_backbone_and_decoder_configs(
              backbone_config=SwinConfig(
                  depths=[1, 1, 1, 1]	,    )	,    decoder_config=DetrConfig(
                  decoder_ffn_dim=128	,    num_queries=self.num_queries	,    decoder_attention_heads=2	,    d_model=self.mask_feature_size	,    )	,    mask_feature_size=self.mask_feature_size	,    fpn_feature_size=self.mask_feature_size	,    num_channels=self.num_channels	,    num_labels=self.num_labels	,    )






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


          UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__:	str					=     self.prepare_config_and_inputs()
          UpperCamelCase__:	Any					=     {"pixel_values": pixel_values, "pixel_mask": pixel_mask}
          return config, inputs_dict






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


          UpperCamelCase__:	int					=     output.encoder_hidden_states
          UpperCamelCase__:	Union[str, Any]					=     output.pixel_decoder_hidden_states
          UpperCamelCase__:	Any					=     output.transformer_decoder_hidden_states

          self.parent.assertTrue(len(__lowerCamelCase					)	,    len(config.backbone_config.depths					)					)
          self.parent.assertTrue(len(__lowerCamelCase					)	,    len(config.backbone_config.depths					)					)
          self.parent.assertTrue(len(__lowerCamelCase					)	,    config.decoder_config.decoder_layers					)






      def 			UpperCAmelCase_       (		self:			Tuple	,    __lowerCamelCase:			Tuple	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			Optional[Any]	,    __lowerCamelCase:			List[str]=False					):
          '''simple docstring'''


          with torch.no_grad():
              UpperCamelCase__:	Optional[int]					=     MaskFormerModel(config=__lowerCamelCase					)
              model.to(__lowerCamelCase					)
              model.eval()

              UpperCamelCase__:	str					=     model(pixel_values=__lowerCamelCase	,    pixel_mask=__lowerCamelCase					)
              UpperCamelCase__:	List[str]					=     model(__lowerCamelCase	,    output_hidden_states=__lowerCamelCase					)
          # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
          # encoder and pixel decoder
          self.parent.assertEqual(
              output.transformer_decoder_last_hidden_state.shape	,    (self.batch_size, self.num_queries, self.mask_feature_size)	,    )
          # let's ensure the other two hidden state exists
          self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None					)
          self.parent.assertTrue(output.encoder_last_hidden_state is not None					)

          if output_hidden_states:
              self.check_output_hidden_state(__lowerCamelCase	,    __lowerCamelCase					)






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


          UpperCamelCase__:	Dict					=     MaskFormerForInstanceSegmentation(config=__lowerCamelCase					)
          model.to(__lowerCamelCase					)
          model.eval()

          def comm_check_on_output(__lowerCamelCase:			str					):
              # let's still check that all the required stuff is there
              self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None					)
              self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None					)
              self.parent.assertTrue(result.encoder_last_hidden_state is not None					)
              # okay, now we need to check the logits shape
              # due to the encoder compression, masks have a //4 spatial size
              self.parent.assertEqual(
                  result.masks_queries_logits.shape	,    (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)	,    )
              # + 1 for null class
              self.parent.assertEqual(
                  result.class_queries_logits.shape	,    (self.batch_size, self.num_queries, self.num_labels + 1)					)

          with torch.no_grad():
              UpperCamelCase__:	int					=     model(pixel_values=__lowerCamelCase	,    pixel_mask=__lowerCamelCase					)
              UpperCamelCase__:	Union[str, Any]					=     model(__lowerCamelCase					)

              comm_check_on_output(__lowerCamelCase					)

              UpperCamelCase__:	Any					=     model(
                  pixel_values=__lowerCamelCase	,    pixel_mask=__lowerCamelCase	,    mask_labels=__lowerCamelCase	,    class_labels=__lowerCamelCase					)

          comm_check_on_output(__lowerCamelCase					)

          self.parent.assertTrue(result.loss is not None					)
          self.parent.assertEqual(result.loss.shape	,    torch.Size([1]					)					)




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

      """simple docstring"""


      UpperCamelCase__  		=       (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
      UpperCamelCase__  		=       (
          {"""feature-extraction""": MaskFormerModel, """image-segmentation""": MaskFormerForInstanceSegmentation}
          if is_torch_available()
          else {}
      )

      UpperCamelCase__  		=       False
      UpperCamelCase__  		=       False
      UpperCamelCase__  		=       False
      UpperCamelCase__  		=       False






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


          UpperCamelCase__:	Optional[Any]					=     MaskFormerModelTester(self					)
          UpperCamelCase__:	Any					=     ConfigTester(self	,    config_class=__lowerCamelCase	,    has_text_modality=__lowerCamelCase					)






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


          self.config_tester.run_common_tests()






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


          UpperCamelCase__				, UpperCamelCase__:	str					=     self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.create_and_check_maskformer_model(__lowerCamelCase	,    **__lowerCamelCase	,    output_hidden_states=__lowerCamelCase					)






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


          UpperCamelCase__:	Tuple					=     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__lowerCamelCase					)






      @unittest.skip(reason="MaskFormer does not use inputs_embeds"					)
      def 			UpperCAmelCase_       (		self:			str					):
          '''simple docstring'''


          pass






      @unittest.skip(reason="MaskFormer does not have a get_input_embeddings method"					)
      def 			UpperCAmelCase_       (		self:			Dict					):
          '''simple docstring'''


          pass






      @unittest.skip(reason="MaskFormer is not a generative model"					)
      def 			UpperCAmelCase_       (		self:			Union[str, Any]					):
          '''simple docstring'''


          pass






      @unittest.skip(reason="MaskFormer does not use token embeddings"					)
      def 			UpperCAmelCase_       (		self:			Any					):
          '''simple docstring'''


          pass






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


          pass






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


          pass






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


          UpperCamelCase__				, UpperCamelCase__:	Union[str, Any]					=     self.model_tester.prepare_config_and_inputs_for_common()

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

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






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


          for model_name in ["facebook/maskformer-swin-small-coco"]:
              UpperCamelCase__:	Optional[int]					=     MaskFormerModel.from_pretrained(__lowerCamelCase					)
              self.assertIsNotNone(__lowerCamelCase					)






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


          UpperCamelCase__:	Dict					=     (self.model_tester.min_size,) * 2
          UpperCamelCase__:	List[Any]					=     {
              "pixel_values": torch.randn((2, 3, *size)	,    device=__lowerCamelCase					),
              "mask_labels": torch.randn((2, 10, *size)	,    device=__lowerCamelCase					),
              "class_labels": torch.zeros(2	,    10	,    device=__lowerCamelCase					).long(),
          }

          UpperCamelCase__:	int					=     MaskFormerForInstanceSegmentation(MaskFormerConfig()					).to(__lowerCamelCase					)
          UpperCamelCase__:	Optional[int]					=     model(**__lowerCamelCase					)
          self.assertTrue(outputs.loss is not None					)






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


          UpperCamelCase__				, UpperCamelCase__:	List[Any]					=     self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.create_and_check_maskformer_model(__lowerCamelCase	,    **__lowerCamelCase	,    output_hidden_states=__lowerCamelCase					)






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


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

          for model_class in self.all_model_classes:
              UpperCamelCase__:	str					=     model_class(__lowerCamelCase					).to(__lowerCamelCase					)
              UpperCamelCase__:	Optional[int]					=     model(**__lowerCamelCase	,    output_attentions=__lowerCamelCase					)
              self.assertTrue(outputs.attentions is not None					)






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


          if not self.model_tester.is_training:
              return
          # only MaskFormerForInstanceSegmentation has the loss
          UpperCamelCase__:	Dict					=     self.all_model_classes[1]
          UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__:	Union[str, Any]					=     self.model_tester.prepare_config_and_inputs()

          UpperCamelCase__:	str					=     model_class(__lowerCamelCase					)
          model.to(__lowerCamelCase					)
          model.train()

          UpperCamelCase__:	Union[str, Any]					=     model(__lowerCamelCase	,    mask_labels=__lowerCamelCase	,    class_labels=__lowerCamelCase					).loss
          loss.backward()






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


          UpperCamelCase__:	Dict					=     self.all_model_classes[1]
          UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__				, UpperCamelCase__:	Optional[int]					=     self.model_tester.prepare_config_and_inputs()
          UpperCamelCase__:	int					=     True
          UpperCamelCase__:	int					=     True

          UpperCamelCase__:	List[Any]					=     model_class(__lowerCamelCase					)
          model.to(__lowerCamelCase					)
          model.train()

          UpperCamelCase__:	str					=     model(__lowerCamelCase	,    mask_labels=__lowerCamelCase	,    class_labels=__lowerCamelCase					)

          UpperCamelCase__:	List[str]					=     outputs.encoder_hidden_states[0]
          encoder_hidden_states.retain_grad()

          UpperCamelCase__:	Optional[int]					=     outputs.pixel_decoder_hidden_states[0]
          pixel_decoder_hidden_states.retain_grad()
          # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
          UpperCamelCase__:	List[str]					=     outputs.transformer_decoder_hidden_states[0]
          transformer_decoder_hidden_states.retain_grad()

          UpperCamelCase__:	Union[str, Any]					=     outputs.attentions[0]
          attentions.retain_grad()

          outputs.loss.backward(retain_graph=__lowerCamelCase					)

          self.assertIsNotNone(encoder_hidden_states.grad					)
          self.assertIsNotNone(pixel_decoder_hidden_states.grad					)
          self.assertIsNotNone(transformer_decoder_hidden_states.grad					)
          self.assertIsNotNone(attentions.grad					)
A__:    List[Any]  	=	1e-4



def 				lowerCAmelCase_ (							):
    UpperCamelCase__:	List[str]					=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
    return image




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

      """simple docstring"""


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


          return (
              MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco"					)
              if is_vision_available()
              else None
          )






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


          UpperCamelCase__:	List[str]					=     MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco"					).to(__lowerCamelCase					)
          UpperCamelCase__:	Optional[Any]					=     self.default_image_processor
          UpperCamelCase__:	Any					=     prepare_img()
          UpperCamelCase__:	Union[str, Any]					=     image_processor(__lowerCamelCase	,    return_tensors="pt"					).to(__lowerCamelCase					)
          UpperCamelCase__:	Any					=     inputs["pixel_values"].shape
          # check size is divisible by 32
          self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0					)
          # check size
          self.assertEqual(__lowerCamelCase	,    (1, 3, 800, 1088)					)

          with torch.no_grad():
              UpperCamelCase__:	Union[str, Any]					=     model(**__lowerCamelCase					)

          UpperCamelCase__:	Dict					=     torch.tensor(
              [[-0.0_482, 0.9_228, 0.4_951], [-0.2_547, 0.8_017, 0.8_527], [-0.0_069, 0.3_385, -0.0_089]]					).to(__lowerCamelCase					)
          self.assertTrue(
              torch.allclose(
                  outputs.encoder_last_hidden_state[0, 0, :3, :3]	,    __lowerCamelCase	,    atol=__lowerCamelCase					)					)

          UpperCamelCase__:	str					=     torch.tensor(
              [[-0.8_422, -0.8_434, -0.9_718], [-1.0_144, -0.5_565, -0.4_195], [-1.0_038, -0.4_484, -0.1_961]]					).to(__lowerCamelCase					)
          self.assertTrue(
              torch.allclose(
                  outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]	,    __lowerCamelCase	,    atol=__lowerCamelCase					)					)

          UpperCamelCase__:	Optional[Any]					=     torch.tensor(
              [[0.2_852, -0.0_159, 0.9_735], [0.6_254, 0.1_858, 0.8_529], [-0.0_680, -0.4_116, 1.8_413]]					).to(__lowerCamelCase					)
          self.assertTrue(
              torch.allclose(
                  outputs.transformer_decoder_last_hidden_state[0, :3, :3]	,    __lowerCamelCase	,    atol=__lowerCamelCase					)					)






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


          UpperCamelCase__:	Any					=     (
              MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"					)
              .to(__lowerCamelCase					)
              .eval()
          )
          UpperCamelCase__:	List[str]					=     self.default_image_processor
          UpperCamelCase__:	List[str]					=     prepare_img()
          UpperCamelCase__:	List[str]					=     image_processor(__lowerCamelCase	,    return_tensors="pt"					).to(__lowerCamelCase					)
          UpperCamelCase__:	int					=     inputs["pixel_values"].shape
          # check size is divisible by 32
          self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0					)
          # check size
          self.assertEqual(__lowerCamelCase	,    (1, 3, 800, 1088)					)

          with torch.no_grad():
              UpperCamelCase__:	Dict					=     model(**__lowerCamelCase					)
          # masks_queries_logits
          UpperCamelCase__:	Optional[int]					=     outputs.masks_queries_logits
          self.assertEqual(
              masks_queries_logits.shape	,    (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)	,    )
          UpperCamelCase__:	Union[str, Any]					=     [
              [-1.3_737_124, -1.7_724_937, -1.9_364_233],
              [-1.5_977_281, -1.9_867_939, -2.1_523_695],
              [-1.5_795_398, -1.9_269_832, -2.093_942],
          ]
          UpperCamelCase__:	Optional[Any]					=     torch.tensor(__lowerCamelCase					).to(__lowerCamelCase					)
          self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]	,    __lowerCamelCase	,    atol=__lowerCamelCase					)					)
          # class_queries_logits
          UpperCamelCase__:	Union[str, Any]					=     outputs.class_queries_logits
          self.assertEqual(
              class_queries_logits.shape	,    (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)					)
          UpperCamelCase__:	Optional[int]					=     torch.tensor(
              [
                  [1.65_12e00, -5.25_72e00, -3.35_19e00],
                  [3.61_69e-02, -5.90_25e00, -2.93_13e00],
                  [1.07_66e-04, -7.76_30e00, -5.12_63e00],
              ]					).to(__lowerCamelCase					)
          self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]	,    __lowerCamelCase	,    atol=__lowerCamelCase					)					)






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


          UpperCamelCase__:	Dict					=     (
              MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff"					)
              .to(__lowerCamelCase					)
              .eval()
          )
          UpperCamelCase__:	Dict					=     self.default_image_processor
          UpperCamelCase__:	Any					=     prepare_img()
          UpperCamelCase__:	Dict					=     image_processor(__lowerCamelCase	,    return_tensors="pt"					).to(__lowerCamelCase					)
          UpperCamelCase__:	Tuple					=     inputs["pixel_values"].shape
          # check size is divisible by 32
          self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0					)
          # check size
          self.assertEqual(__lowerCamelCase	,    (1, 3, 800, 1088)					)

          with torch.no_grad():
              UpperCamelCase__:	str					=     model(**__lowerCamelCase					)
          # masks_queries_logits
          UpperCamelCase__:	int					=     outputs.masks_queries_logits
          self.assertEqual(
              masks_queries_logits.shape	,    (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)	,    )
          UpperCamelCase__:	int					=     [[-0.9_046, -2.6_366, -4.6_062], [-3.4_179, -5.7_890, -8.8_057], [-4.9_179, -7.6_560, -10.7_711]]
          UpperCamelCase__:	List[str]					=     torch.tensor(__lowerCamelCase					).to(__lowerCamelCase					)
          self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]	,    __lowerCamelCase	,    atol=__lowerCamelCase					)					)
          # class_queries_logits
          UpperCamelCase__:	Dict					=     outputs.class_queries_logits
          self.assertEqual(
              class_queries_logits.shape	,    (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)					)
          UpperCamelCase__:	Any					=     torch.tensor(
              [[4.7_188, -3.2_585, -2.8_857], [6.6_871, -2.9_181, -1.2_487], [7.2_449, -2.2_764, -2.1_874]]					).to(__lowerCamelCase					)
          self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]	,    __lowerCamelCase	,    atol=__lowerCamelCase					)					)






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


          UpperCamelCase__:	List[str]					=     (
              MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"					)
              .to(__lowerCamelCase					)
              .eval()
          )
          UpperCamelCase__:	List[str]					=     self.default_image_processor

          UpperCamelCase__:	List[str]					=     image_processor(
              [np.zeros((3, 800, 1333)					), np.zeros((3, 800, 1333)					)]	,    segmentation_maps=[np.zeros((384, 384)					).astype(np.floataa					), np.zeros((384, 384)					).astype(np.floataa					)]	,    return_tensors="pt"	,    )

          UpperCamelCase__:	str					=     inputs["pixel_values"].to(__lowerCamelCase					)
          UpperCamelCase__:	Any					=     [el.to(__lowerCamelCase					) for el in inputs["mask_labels"]]
          UpperCamelCase__:	List[str]					=     [el.to(__lowerCamelCase					) for el in inputs["class_labels"]]

          with torch.no_grad():
              UpperCamelCase__:	List[Any]					=     model(**__lowerCamelCase					)

          self.assertTrue(outputs.loss is not None					)








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
   )

