
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__		:
      def __init__(      self    ,  __magic_name__    ,  __magic_name__=2    ,  __magic_name__=True    ,  __magic_name__=False    ,  __magic_name__=1_0    ,  __magic_name__=3    ,  __magic_name__=3_2 * 4    ,  __magic_name__=3_2 * 6    ,  __magic_name__=4    ,  __magic_name__=3_2    ,  ):
       lowerCamelCase		:	List[str]							=      parent
       lowerCamelCase		:	str							=      batch_size
       lowerCamelCase		:	int							=      is_training
       lowerCamelCase		:	Dict							=      use_auxiliary_loss
       lowerCamelCase		:	Union[str, Any]							=      num_queries
       lowerCamelCase		:	Tuple							=      num_channels
       lowerCamelCase		:	Any							=      min_size
       lowerCamelCase		:	Optional[int]							=      max_size
       lowerCamelCase		:	Optional[Any]							=      num_labels
       lowerCamelCase		:	Tuple							=      mask_feature_size
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Optional[int]							=      floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]      ).to(
           __magic_name__      )

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

       lowerCamelCase		:	str							=      (
           torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]    ,  device=__magic_name__      ) > 0.5
       ).float()
       lowerCamelCase		:	List[Any]							=      (torch.rand((self.batch_size, self.num_labels)    ,  device=__magic_name__      ) > 0.5).long()

       lowerCamelCase		:	Tuple							=      self.get_config()
       return config, pixel_values, pixel_mask, mask_labels, class_labels
      def 						UpperCamelCase__							(      self      ):
       return MaskFormerConfig.from_backbone_and_decoder_configs(
           backbone_config=SwinConfig(
               depths=[1, 1, 1, 1]    ,  )    ,  decoder_config=DetrConfig(
               decoder_ffn_dim=1_2_8    ,  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      ):
       lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase		:	Tuple							=      self.prepare_config_and_inputs()
       lowerCamelCase		:	List[str]							=      {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
       return config, inputs_dict
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__      ):
       lowerCamelCase		:	Optional[int]							=      output.encoder_hidden_states
       lowerCamelCase		:	List[str]							=      output.pixel_decoder_hidden_states
       lowerCamelCase		:	Dict							=      output.transformer_decoder_hidden_states

       self.parent.assertTrue(len(__magic_name__      )    ,  len(config.backbone_config.depths      )      )
       self.parent.assertTrue(len(__magic_name__      )    ,  len(config.backbone_config.depths      )      )
       self.parent.assertTrue(len(__magic_name__      )    ,  config.decoder_config.decoder_layers      )
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__=False      ):
       with torch.no_grad():
        lowerCamelCase		:	Optional[int]							=      MaskFormerModel(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()

        lowerCamelCase		:	Tuple							=      model(pixel_values=__magic_name__    ,  pixel_mask=__magic_name__      )
        lowerCamelCase		:	Dict							=      model(__magic_name__    ,  output_hidden_states=__magic_name__      )
       # 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(__magic_name__    ,  __magic_name__      )






      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__      ):
       lowerCamelCase		:	str							=      MaskFormerForInstanceSegmentation(config=__magic_name__      )
       model.to(__magic_name__      )
       model.eval()

       def comm_check_on_output(__magic_name__      ):
        # 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():
        lowerCamelCase		:	str							=      model(pixel_values=__magic_name__    ,  pixel_mask=__magic_name__      )
        lowerCamelCase		:	Union[str, Any]							=      model(__magic_name__      )

        comm_check_on_output(__magic_name__      )

        lowerCamelCase		:	Optional[Any]							=      model(
            pixel_values=__magic_name__    ,  pixel_mask=__magic_name__    ,  mask_labels=__magic_name__    ,  class_labels=__magic_name__      )

       comm_check_on_output(__magic_name__      )

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





@require_torch
class 							A__		(	__SCREAMING_SNAKE_CASE  ,   __SCREAMING_SNAKE_CASE  ,   unittest.TestCase):
      _UpperCAmelCase				:    str           =			(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
      _UpperCAmelCase				:    Optional[Any]           =			(
          {"""feature-extraction""": MaskFormerModel, """image-segmentation""": MaskFormerForInstanceSegmentation}
          if is_torch_available()
          else {}
      )

      _UpperCAmelCase				:    Tuple           =			False
      _UpperCAmelCase				:    Dict           =			False
      _UpperCAmelCase				:    List[Any]           =			False
      _UpperCAmelCase				:    Any           =			False
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Union[str, Any]							=      MaskFormerModelTester(self      )
       lowerCamelCase		:	Tuple							=      ConfigTester(self    ,  config_class=__magic_name__    ,  has_text_modality=__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       self.config_tester.run_common_tests()
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase			,    lowerCamelCase		:	Tuple							=      self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.create_and_check_maskformer_model(__magic_name__    ,  **__magic_name__    ,  output_hidden_states=__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Optional[int]							=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__magic_name__      )
      @unittest.skip(reason="""MaskFormer does not use inputs_embeds"""      )
      def 						UpperCamelCase__							(      self      ):
       pass
      @unittest.skip(reason="""MaskFormer does not have a get_input_embeddings method"""      )
      def 						UpperCamelCase__							(      self      ):
       pass
      @unittest.skip(reason="""MaskFormer is not a generative model"""      )
      def 						UpperCamelCase__							(      self      ):
       pass
      @unittest.skip(reason="""MaskFormer does not use token embeddings"""      )
      def 						UpperCamelCase__							(      self      ):
       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      ):
       pass
      @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""      )
      def 						UpperCamelCase__							(      self      ):
       pass
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase			,    lowerCamelCase		:	Optional[int]							=      self.model_tester.prepare_config_and_inputs_for_common()

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

        lowerCamelCase		:	Optional[Any]							=      ["""pixel_values"""]
        self.assertListEqual(arg_names[:1]    ,  __magic_name__      )
      @slow
      def 						UpperCamelCase__							(      self      ):
       for model_name in ["facebook/maskformer-swin-small-coco"]:
        lowerCamelCase		:	Tuple							=      MaskFormerModel.from_pretrained(__magic_name__      )
        self.assertIsNotNone(__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Union[str, Any]							=      (self.model_tester.min_size,) * 2
       lowerCamelCase		:	Dict							=      {
           """pixel_values""": torch.randn((2, 3, *size)    ,  device=__magic_name__      ),
           """mask_labels""": torch.randn((2, 1_0, *size)    ,  device=__magic_name__      ),
           """class_labels""": torch.zeros(2    ,  1_0    ,  device=__magic_name__      ).long(),
       }

       lowerCamelCase		:	Optional[int]							=      MaskFormerForInstanceSegmentation(MaskFormerConfig()      ).to(__magic_name__      )
       lowerCamelCase		:	List[Any]							=      model(**__magic_name__      )
       self.assertTrue(outputs.loss is not None      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase			,    lowerCamelCase		:	str							=      self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.create_and_check_maskformer_model(__magic_name__    ,  **__magic_name__    ,  output_hidden_states=__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase			,    lowerCamelCase		:	Tuple							=      self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        lowerCamelCase		:	Union[str, Any]							=      model_class(__magic_name__      ).to(__magic_name__      )
        lowerCamelCase		:	int							=      model(**__magic_name__    ,  output_attentions=__magic_name__      )
        self.assertTrue(outputs.attentions is not None      )
      def 						UpperCamelCase__							(      self      ):
       if not self.model_tester.is_training:
        return
       # only MaskFormerForInstanceSegmentation has the loss
       lowerCamelCase		:	str							=      self.all_model_classes[1]
       lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase		:	Union[str, Any]							=      self.model_tester.prepare_config_and_inputs()

       lowerCamelCase		:	Optional[int]							=      model_class(__magic_name__      )
       model.to(__magic_name__      )
       model.train()

       lowerCamelCase		:	Optional[Any]							=      model(__magic_name__    ,  mask_labels=__magic_name__    ,  class_labels=__magic_name__      ).loss
       loss.backward()






      def 						UpperCamelCase__							(      self      ):
       # only MaskFormerForInstanceSegmentation has the loss
       lowerCamelCase		:	Optional[Any]							=      self.all_model_classes[1]
       lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase			,    lowerCamelCase		:	List[Any]							=      self.model_tester.prepare_config_and_inputs()
       lowerCamelCase		:	Union[str, Any]							=      True
       lowerCamelCase		:	List[Any]							=      True

       lowerCamelCase		:	Optional[Any]							=      model_class(__magic_name__      )
       model.to(__magic_name__      )
       model.train()

       lowerCamelCase		:	int							=      model(__magic_name__    ,  mask_labels=__magic_name__    ,  class_labels=__magic_name__      )

       lowerCamelCase		:	List[Any]							=      outputs.encoder_hidden_states[0]
       encoder_hidden_states.retain_grad()

       lowerCamelCase		:	Tuple							=      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
       lowerCamelCase		:	Any							=      outputs.transformer_decoder_hidden_states[0]
       transformer_decoder_hidden_states.retain_grad()

       lowerCamelCase		:	int							=      outputs.attentions[0]
       attentions.retain_grad()

       outputs.loss.backward(retain_graph=__magic_name__      )

       self.assertIsNotNone(encoder_hidden_states.grad      )
       self.assertIsNotNone(pixel_decoder_hidden_states.grad      )
       self.assertIsNotNone(transformer_decoder_hidden_states.grad      )
       self.assertIsNotNone(attentions.grad      )


_lowerCamelCase					   =1E-4





def     _a					(						):
 lowerCamelCase		:	Optional[Any]							=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							)
 return image





@require_vision
@slow
class 							A__		(	unittest.TestCase):
      @cached_property
      def 						UpperCamelCase__							(      self      ):
       return (
           MaskFormerImageProcessor.from_pretrained("""facebook/maskformer-swin-small-coco"""      )
           if is_vision_available()
           else None
       )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	str							=      MaskFormerModel.from_pretrained("""facebook/maskformer-swin-small-coco"""      ).to(__magic_name__      )
       lowerCamelCase		:	List[str]							=      self.default_image_processor
       lowerCamelCase		:	Tuple							=      prepare_img()
       lowerCamelCase		:	Optional[Any]							=      image_processor(__magic_name__    ,  return_tensors="""pt"""      ).to(__magic_name__      )
       lowerCamelCase		:	Union[str, Any]							=      inputs["""pixel_values"""].shape
       # check size is divisible by 32
       self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0      )
       # check size
       self.assertEqual(__magic_name__    ,  (1, 3, 8_0_0, 1_0_8_8)      )

       with torch.no_grad():
        lowerCamelCase		:	int							=      model(**__magic_name__      )

       lowerCamelCase		:	str							=      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(__magic_name__      )
       self.assertTrue(
           torch.allclose(
               outputs.encoder_last_hidden_state[0, 0, :3, :3]    ,  __magic_name__    ,  atol=__magic_name__      )      )

       lowerCamelCase		:	List[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(__magic_name__      )
       self.assertTrue(
           torch.allclose(
               outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]    ,  __magic_name__    ,  atol=__magic_name__      )      )

       lowerCamelCase		:	List[str]							=      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(__magic_name__      )
       self.assertTrue(
           torch.allclose(
               outputs.transformer_decoder_last_hidden_state[0, :3, :3]    ,  __magic_name__    ,  atol=__magic_name__      )      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Tuple							=      (
           MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""      )
           .to(__magic_name__      )
           .eval()
       )
       lowerCamelCase		:	Tuple							=      self.default_image_processor
       lowerCamelCase		:	Union[str, Any]							=      prepare_img()
       lowerCamelCase		:	Optional[Any]							=      image_processor(__magic_name__    ,  return_tensors="""pt"""      ).to(__magic_name__      )
       lowerCamelCase		:	Union[str, Any]							=      inputs["""pixel_values"""].shape
       # check size is divisible by 32
       self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0      )
       # check size
       self.assertEqual(__magic_name__    ,  (1, 3, 8_0_0, 1_0_8_8)      )

       with torch.no_grad():
        lowerCamelCase		:	Optional[Any]							=      model(**__magic_name__      )
       # masks_queries_logits
       lowerCamelCase		:	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)    ,  )
       lowerCamelCase		:	List[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],
       ]
       lowerCamelCase		:	List[Any]							=      torch.tensor(__magic_name__      ).to(__magic_name__      )
       self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]    ,  __magic_name__    ,  atol=__magic_name__      )      )
       # class_queries_logits
       lowerCamelCase		:	Dict							=      outputs.class_queries_logits
       self.assertEqual(
           class_queries_logits.shape    ,  (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)      )
       lowerCamelCase		:	Union[str, Any]							=      torch.tensor(
           [
               [1.6512e00, -5.2572e00, -3.3519e00],
               [3.6169e-02, -5.9025e00, -2.9313e00],
               [1.0766e-04, -7.7630e00, -5.1263e00],
           ]      ).to(__magic_name__      )
       self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]    ,  __magic_name__    ,  atol=__magic_name__      )      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Union[str, Any]							=      (
           MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-resnet101-coco-stuff"""      )
           .to(__magic_name__      )
           .eval()
       )
       lowerCamelCase		:	Tuple							=      self.default_image_processor
       lowerCamelCase		:	Any							=      prepare_img()
       lowerCamelCase		:	Tuple							=      image_processor(__magic_name__    ,  return_tensors="""pt"""      ).to(__magic_name__      )
       lowerCamelCase		:	int							=      inputs["""pixel_values"""].shape
       # check size is divisible by 32
       self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0      )
       # check size
       self.assertEqual(__magic_name__    ,  (1, 3, 8_0_0, 1_0_8_8)      )

       with torch.no_grad():
        lowerCamelCase		:	int							=      model(**__magic_name__      )
       # masks_queries_logits
       lowerCamelCase		:	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)    ,  )
       lowerCamelCase		:	Tuple							=      [[-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]]
       lowerCamelCase		:	Tuple							=      torch.tensor(__magic_name__      ).to(__magic_name__      )
       self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]    ,  __magic_name__    ,  atol=__magic_name__      )      )
       # class_queries_logits
       lowerCamelCase		:	Optional[Any]							=      outputs.class_queries_logits
       self.assertEqual(
           class_queries_logits.shape    ,  (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)      )
       lowerCamelCase		:	Union[str, 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(__magic_name__      )
       self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]    ,  __magic_name__    ,  atol=__magic_name__      )      )






      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Dict							=      (
           MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""      )
           .to(__magic_name__      )
           .eval()
       )
       lowerCamelCase		:	Optional[Any]							=      self.default_image_processor

       lowerCamelCase		:	List[Any]							=      image_processor(
           [np.zeros((3, 8_0_0, 1_3_3_3)      ), np.zeros((3, 8_0_0, 1_3_3_3)      )]    ,  segmentation_maps=[np.zeros((3_8_4, 3_8_4)      ).astype(np.floataa      ), np.zeros((3_8_4, 3_8_4)      ).astype(np.floataa      )]    ,  return_tensors="""pt"""    ,  )

       lowerCamelCase		:	Tuple							=      inputs["""pixel_values"""].to(__magic_name__      )
       lowerCamelCase		:	Union[str, Any]							=      [el.to(__magic_name__      ) for el in inputs["""mask_labels"""]]
       lowerCamelCase		:	Any							=      [el.to(__magic_name__      ) for el in inputs["""class_labels"""]]

       with torch.no_grad():
        lowerCamelCase		:	Tuple							=      model(**__magic_name__      )

       self.assertTrue(outputs.loss is not None      )






def     _a					(						lowerCamelCase							):

 if p < 2:
  raise ValueError("""p should not be less than 2!"""							)
 elif p == 2:
  return True

 lowerCamelCase		:	Any							=      4
 lowerCamelCase		:	List[str]							=      (1 << p) - 1
 for _ in range(p - 2							):
  lowerCamelCase		:	List[Any]							=      ((s * s) - 2) % m
 return s == 0


if __name__ == "__main__":
      print(lucas_lehmer_test(7))
      print(lucas_lehmer_test(1_1))




