


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       __magic_name__	:


     def __init__(      self							:     Tuple							,	lowerCamelCase__							:     Union[str, Any]							,	lowerCamelCase__							:     Any=2							,	lowerCamelCase__							:     str=True							,	lowerCamelCase__							:     List[Any]=False							,	lowerCamelCase__							:     Dict=10							,	lowerCamelCase__							:     int=3							,	lowerCamelCase__							:     str=32 * 4							,	lowerCamelCase__							:     List[str]=32 * 6							,	lowerCamelCase__							:     str=4							,	lowerCamelCase__							:     Dict=32							,	)  ->      str:





      '''simple docstring'''






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


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





      '''simple docstring'''






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

      UpperCamelCase__					:    Any          =   torch.ones([self.batch_size, self.min_size, self.max_size]							,	device=lowerCamelCase__			)

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

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


     def      UpperCAmelCase__	(      self							:     List[Any]			)  ->      List[str]:





      '''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							:     Any			)  ->      int:





      '''simple docstring'''






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


     def      UpperCAmelCase__	(      self							:     int							,	lowerCamelCase__							:     int							,	lowerCamelCase__							:     Dict			)  ->      int:





      '''simple docstring'''






      UpperCamelCase__					:    List[str]          =   output.encoder_hidden_states
      UpperCamelCase__					:    str          =   output.pixel_decoder_hidden_states
      UpperCamelCase__					:    str          =   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							:     Dict							,	lowerCamelCase__							:     Optional[int]							,	lowerCamelCase__							:     Optional[Any]							,	lowerCamelCase__							:     Any							,	lowerCamelCase__							:     str=False			)  ->      Tuple:





      '''simple docstring'''






      with torch.no_grad():
       UpperCamelCase__					:    List[Any]          =   MaskFormerModel(config=lowerCamelCase__			)
       model.to(lowerCamelCase__			)
       model.eval()

       UpperCamelCase__					:    List[str]          =   model(pixel_values=lowerCamelCase__							,	pixel_mask=lowerCamelCase__			)
       UpperCamelCase__					:    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							:     Optional[Any]							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     Any							,	lowerCamelCase__							:     Optional[int]							,	lowerCamelCase__							:     Optional[Any]							,	lowerCamelCase__							:     Union[str, Any]			)  ->      Dict:





      '''simple docstring'''






      UpperCamelCase__					:    List[Any]          =   MaskFormerForInstanceSegmentation(config=lowerCamelCase__			)
      model.to(lowerCamelCase__			)
      model.eval()

      def comm_check_on_output(lowerCamelCase__							:     Union[str, Any]			):
       # 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__					:    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       __magic_name__	(     __lowerCAmelCase					,				__lowerCAmelCase					,				unittest.TestCase):
     A:		Optional[int]			=      (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
     A:		Dict			=      (
         {"feature-extraction": MaskFormerModel, "image-segmentation": MaskFormerForInstanceSegmentation}
         if is_torch_available()
         else {}
     )

     A:		Union[str, Any]			=      False
     A:		Optional[Any]			=      False
     A:		Union[str, Any]			=      False
     A:		Optional[Any]			=      False


     def      UpperCAmelCase__	(      self							:     str			)  ->      Any:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[int]          =   MaskFormerModelTester(self			)
      UpperCamelCase__					:    str          =   ConfigTester(self							,	config_class=lowerCamelCase__							,	has_text_modality=lowerCamelCase__			)


     def      UpperCAmelCase__	(      self							:     Optional[Any]			)  ->      Tuple:





      '''simple docstring'''






      self.config_tester.run_common_tests()


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





      '''simple docstring'''






      UpperCamelCase__       ,				UpperCamelCase__					:    Optional[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							:     Any			)  ->      Optional[Any]:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[int]          =   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							:     List[str]			)  ->      List[str]:





      '''simple docstring'''






      pass


     @unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method'''			)
     def      UpperCAmelCase__	(      self							:     List[str]			)  ->      Union[str, Any]:





      '''simple docstring'''






      pass


     @unittest.skip(reason='''MaskFormer is not a generative model'''			)
     def      UpperCAmelCase__	(      self							:     List[Any]			)  ->      Dict:





      '''simple docstring'''






      pass


     @unittest.skip(reason='''MaskFormer does not use token embeddings'''			)
     def      UpperCAmelCase__	(      self							:     Tuple			)  ->      Optional[int]:





      '''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]			)  ->      Any:





      '''simple docstring'''






      pass


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





      '''simple docstring'''






      pass


     def      UpperCAmelCase__	(      self							:     Optional[int]			)  ->      Union[str, Any]:





      '''simple docstring'''






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

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

       UpperCamelCase__					:    str          =   ['''pixel_values''']
       self.assertListEqual(arg_names[:1]							,	lowerCamelCase__			)


     @slow
     def      UpperCAmelCase__	(      self							:     Any			)  ->      Dict:





      '''simple docstring'''






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


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





      '''simple docstring'''






      UpperCamelCase__					:    Any          =   (self.model_tester.min_size,) * 2
      UpperCamelCase__					:    List[str]          =   {
          '''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__					:    List[Any]          =   MaskFormerForInstanceSegmentation(MaskFormerConfig()			).to(lowerCamelCase__			)
      UpperCamelCase__					:    int          =   model(**lowerCamelCase__			)
      self.assertTrue(outputs.loss is not None			)


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





      '''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]			)  ->      List[str]:





      '''simple docstring'''






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

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


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





      '''simple docstring'''






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

      UpperCamelCase__					:    Union[str, Any]          =   model_class(lowerCamelCase__			)
      model.to(lowerCamelCase__			)
      model.train()

      UpperCamelCase__					:    Optional[int]          =   model(lowerCamelCase__							,	mask_labels=lowerCamelCase__							,	class_labels=lowerCamelCase__			).loss
      loss.backward()


     def      UpperCAmelCase__	(      self							:     List[str]			)  ->      Dict:





      '''simple docstring'''






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

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

      UpperCamelCase__					:    Union[str, Any]          =   model(lowerCamelCase__							,	mask_labels=lowerCamelCase__							,	class_labels=lowerCamelCase__			)

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

      UpperCamelCase__					:    Dict          =   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__					:    str          =   outputs.transformer_decoder_hidden_states[0]
      transformer_decoder_hidden_states.retain_grad()

      UpperCamelCase__					:    Dict          =   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			)



__UpperCamelCase       :       Optional[int]						       =				1E-4

def 			_a  (				):







 """simple docstring"""



 UpperCamelCase__					:    Union[str, Any]          =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
 return image






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


     @cached_property
     def      UpperCAmelCase__	(      self							:     Union[str, Any]			)  ->      Union[str, Any]:





      '''simple docstring'''






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


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





      '''simple docstring'''






      UpperCamelCase__					:    str          =   MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco'''			).to(lowerCamelCase__			)
      UpperCamelCase__					:    List[str]          =   self.default_image_processor
      UpperCamelCase__					:    List[str]          =   prepare_img()
      UpperCamelCase__					:    Any          =   image_processor(lowerCamelCase__							,	return_tensors='''pt'''			).to(lowerCamelCase__			)
      UpperCamelCase__					:    Union[str, 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__					:    Optional[Any]          =   model(**lowerCamelCase__			)

      UpperCamelCase__					:    Dict          =   torch.tensor(
          [[-0.0482, 0.9228, 0.4951], [-0.2547, 0.8017, 0.8527], [-0.0069, 0.3385, -0.0089]]			).to(lowerCamelCase__			)
      self.assertTrue(
          torch.allclose(
              outputs.encoder_last_hidden_state[0, 0, :3, :3]							,	lowerCamelCase__							,	atol=lowerCamelCase__			)			)

      UpperCamelCase__					:    Union[str, Any]          =   torch.tensor(
          [[-0.8422, -0.8434, -0.9718], [-1.0144, -0.5565, -0.4195], [-1.0038, -0.4484, -0.1961]]			).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.2852, -0.0159, 0.9735], [0.6254, 0.1858, 0.8529], [-0.0680, -0.4116, 1.8413]]			).to(lowerCamelCase__			)
      self.assertTrue(
          torch.allclose(
              outputs.transformer_decoder_last_hidden_state[0, :3, :3]							,	lowerCamelCase__							,	atol=lowerCamelCase__			)			)


     def      UpperCAmelCase__	(      self							:     List[Any]			)  ->      Any:





      '''simple docstring'''






      UpperCamelCase__					:    Tuple          =   (
          MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''			)
          .to(lowerCamelCase__			)
          .eval()
      )
      UpperCamelCase__					:    Union[str, Any]          =   self.default_image_processor
      UpperCamelCase__					:    Optional[int]          =   prepare_img()
      UpperCamelCase__					:    Dict          =   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__					:    Tuple          =   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.373_7124, -1.772_4937, -1.936_4233],
          [-1.597_7281, -1.986_7939, -2.152_3695],
          [-1.579_5398, -1.926_9832, -2.09_3942],
      ]
      UpperCamelCase__					:    Any          =   torch.tensor(lowerCamelCase__			).to(lowerCamelCase__			)
      self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]							,	lowerCamelCase__							,	atol=lowerCamelCase__			)			)
      # class_queries_logits
      UpperCamelCase__					:    List[str]          =   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.6_5_1_2E0_0, -5.2_5_7_2E0_0, -3.3_5_1_9E0_0],
              [3.6_1_6_9E-0_2, -5.9_0_2_5E0_0, -2.9_3_1_3E0_0],
              [1.0_7_6_6E-0_4, -7.7_6_3_0E0_0, -5.1_2_6_3E0_0],
          ]			).to(lowerCamelCase__			)
      self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]							,	lowerCamelCase__							,	atol=lowerCamelCase__			)			)


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





      '''simple docstring'''






      UpperCamelCase__					:    int          =   (
          MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff'''			)
          .to(lowerCamelCase__			)
          .eval()
      )
      UpperCamelCase__					:    Tuple          =   self.default_image_processor
      UpperCamelCase__					:    Any          =   prepare_img()
      UpperCamelCase__					:    Optional[int]          =   image_processor(lowerCamelCase__							,	return_tensors='''pt'''			).to(lowerCamelCase__			)
      UpperCamelCase__					:    Optional[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__					:    List[Any]          =   model(**lowerCamelCase__			)
      # masks_queries_logits
      UpperCamelCase__					:    Dict          =   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__					:    Optional[int]          =   [[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7711]]
      UpperCamelCase__					:    Any          =   torch.tensor(lowerCamelCase__			).to(lowerCamelCase__			)
      self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]							,	lowerCamelCase__							,	atol=lowerCamelCase__			)			)
      # class_queries_logits
      UpperCamelCase__					:    str          =   outputs.class_queries_logits
      self.assertEqual(
          class_queries_logits.shape							,	(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)			)
      UpperCamelCase__					:    List[str]          =   torch.tensor(
          [[4.7188, -3.2585, -2.8857], [6.6871, -2.9181, -1.2487], [7.2449, -2.2764, -2.1874]]			).to(lowerCamelCase__			)
      self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]							,	lowerCamelCase__							,	atol=lowerCamelCase__			)			)


     def      UpperCAmelCase__	(      self							:     Tuple			)  ->      List[str]:





      '''simple docstring'''






      UpperCamelCase__					:    Any          =   (
          MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''			)
          .to(lowerCamelCase__			)
          .eval()
      )
      UpperCamelCase__					:    Dict          =   self.default_image_processor

      UpperCamelCase__					:    Optional[Any]          =   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__					:    int          =   inputs['''pixel_values'''].to(lowerCamelCase__			)
      UpperCamelCase__					:    Tuple          =   [el.to(lowerCamelCase__			) for el in inputs['''mask_labels''']]
      UpperCamelCase__					:    Optional[int]          =   [el.to(lowerCamelCase__			) for el in inputs['''class_labels''']]

      with torch.no_grad():
       UpperCamelCase__					:    Optional[int]          =   model(**lowerCamelCase__			)

      self.assertTrue(outputs.loss is not None			)






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


__UpperCamelCase       :       Dict						       =				{
    "configuration_mobilevit": ["MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MobileViTConfig", "MobileViTOnnxConfig"],
}

try:
       if not is_vision_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __UpperCamelCase       :       str						       =				["MobileViTFeatureExtractor"]
       __UpperCamelCase       :       Optional[Any]						       =				["MobileViTImageProcessor"]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __UpperCamelCase       :       Union[str, Any]						       =				[
           "MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST",
           "MobileViTForImageClassification",
           "MobileViTForSemanticSegmentation",
           "MobileViTModel",
           "MobileViTPreTrainedModel",
       ]

try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __UpperCamelCase       :       Optional[int]						       =				[
           "TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TFMobileViTForImageClassification",
           "TFMobileViTForSemanticSegmentation",
           "TFMobileViTModel",
           "TFMobileViTPreTrainedModel",
       ]

if TYPE_CHECKING:
       from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig

       try:
              if not is_vision_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .feature_extraction_mobilevit import MobileViTFeatureExtractor
              from .image_processing_mobilevit import MobileViTImageProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_mobilevit import (
                  MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  MobileViTForImageClassification,
                  MobileViTForSemanticSegmentation,
                  MobileViTModel,
                  MobileViTPreTrainedModel,
              )

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_mobilevit import (
                  TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFMobileViTForImageClassification,
                  TFMobileViTForSemanticSegmentation,
                  TFMobileViTModel,
                  TFMobileViTPreTrainedModel,
              )


else:
       import sys

       __UpperCamelCase       :       str						       =				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


