


"""simple docstring"""

import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, 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 MaskaFormerForUniversalSegmentation, MaskaFormerModel

       if is_vision_available():
              from transformers import MaskaFormerImageProcessor

if is_vision_available():
       from PIL import Image





class  lowerCamelCase__    :
      def __init__(		self						:						int   ,							A_						:						Optional[Any]   ,							A_						:						str=2   ,							A_						:						List[Any]=True   ,							A_						:						Union[str, Any]=False   ,							A_						:						Any=1_0   ,							A_						:						List[str]=3   ,							A_						:						List[Any]=3_2 * 8   ,							A_						:						List[str]=3_2 * 8   ,							A_						:						List[str]=4   ,							A_						:						Any=6_4   ,							):






       '''simple docstring'''


       __lowercase						=						parent
       __lowercase						=						batch_size
       __lowercase						=						is_training
       __lowercase						=						use_auxiliary_loss
       __lowercase						=						num_queries
       __lowercase						=						num_channels
       __lowercase						=						min_size
       __lowercase						=						max_size
       __lowercase						=						num_labels
       __lowercase						=						hidden_dim
       __lowercase						=						hidden_dim
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]				).to(
           A_				)

       __lowercase						=						torch.ones([self.batch_size, self.min_size, self.max_size]   ,							device=A_				)

       __lowercase						=						(
           torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]   ,							device=A_				) > 0.5
       ).float()
       __lowercase						=						(torch.rand((self.batch_size, self.num_labels)   ,							device=A_				) > 0.5).long()

       __lowercase						=						self.get_config()
       return config, pixel_values, pixel_mask, mask_labels, class_labels
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						MaskaFormerConfig(
           hidden_size=self.hidden_dim   ,							)
       __lowercase						=						self.num_queries
       __lowercase						=						self.num_labels

       __lowercase						=						[1, 1, 1, 1]
       __lowercase						=						self.num_channels

       __lowercase						=						6_4
       __lowercase						=						1_2_8
       __lowercase						=						self.hidden_dim
       __lowercase						=						self.hidden_dim
       __lowercase						=						self.hidden_dim
       return config
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       __lowercase				,		__lowercase				,		__lowercase				,		__lowercase				,		__lowercase						=						self.prepare_config_and_inputs()
       __lowercase						=						{"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
       return config, inputs_dict
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]   ,							A_						:						int   ,							A_						:						Dict				):






       '''simple docstring'''


       __lowercase						=						output.encoder_hidden_states
       __lowercase						=						output.pixel_decoder_hidden_states
       __lowercase						=						output.transformer_decoder_hidden_states

       self.parent.assertTrue(len(A_				)   ,							len(config.backbone_config.depths				)				)
       self.parent.assertTrue(len(A_				)   ,							len(config.backbone_config.depths				)				)
       self.parent.assertTrue(len(A_				)   ,							config.decoder_layers				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Any   ,							A_						:						Optional[Any]   ,							A_						:						Optional[Any]   ,							A_						:						Union[str, Any]   ,							A_						:						List[Any]=False				):






       '''simple docstring'''


       with torch.no_grad():
        __lowercase						=						MaskaFormerModel(config=A_				)
        model.to(A_				)
        model.eval()

        __lowercase						=						model(pixel_values=A_   ,							pixel_mask=A_				)
        __lowercase						=						model(A_   ,							output_hidden_states=A_				)

       self.parent.assertEqual(
           output.transformer_decoder_last_hidden_state.shape   ,							(self.batch_size, self.num_queries, self.hidden_dim)   ,							)
       # 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(A_   ,							A_				)






      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]   ,							A_						:						List[Any]   ,							A_						:						Optional[Any]   ,							A_						:						List[str]   ,							A_						:						Optional[Any]   ,							A_						:						Optional[Any]				):






       '''simple docstring'''


       __lowercase						=						MaskaFormerForUniversalSegmentation(config=A_				)
       model.to(A_				)
       model.eval()

       def comm_check_on_output(A_						:						Tuple				):
        # 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():
        __lowercase						=						model(pixel_values=A_   ,							pixel_mask=A_				)
        __lowercase						=						model(A_				)

        comm_check_on_output(A_				)

        __lowercase						=						model(
            pixel_values=A_   ,							pixel_mask=A_   ,							mask_labels=A_   ,							class_labels=A_				)

       comm_check_on_output(A_				)

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






@require_torch
class  lowerCamelCase__    (		_a      , _a      , unittest.TestCase     ):
      a			:  Optional[Any]           =		(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
      a			:  Optional[int]           =		{"""feature-extraction""": MaskaFormerModel} if is_torch_available() else {}

      a			:  List[Any]           =		False
      a			:  Any           =		False
      a			:  str           =		False
      a			:  Union[str, Any]           =		False
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase						=						MaskaFormerModelTester(self				)
       __lowercase						=						ConfigTester(self   ,							config_class=A_   ,							has_text_modality=A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       self.config_tester.run_common_tests()
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.create_and_check_maskaformer_model(A_   ,							**A_   ,							output_hidden_states=A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase						=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*A_				)
      @unittest.skip(reason="""Mask2Former does not use inputs_embeds"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       pass
      @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]				):






       '''simple docstring'''


       pass
      @unittest.skip(reason="""Mask2Former is not a generative model"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       pass
      @unittest.skip(reason="""Mask2Former does not use token embeddings"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       pass
      @require_torch_multi_gpu
      @unittest.skip(
          reason="""Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       pass
      @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						str				):






       '''simple docstring'''


       pass
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()

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

        __lowercase						=						["""pixel_values"""]
        self.assertListEqual(arg_names[:1]   ,							A_				)
      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
        __lowercase						=						MaskaFormerModel.from_pretrained(A_				)
        self.assertIsNotNone(A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       __lowercase						=						(self.model_tester.min_size,) * 2
       __lowercase						=						{
           """pixel_values""": torch.randn((2, 3, *size)   ,							device=A_				),
           """mask_labels""": torch.randn((2, 1_0, *size)   ,							device=A_				),
           """class_labels""": torch.zeros(2   ,							1_0   ,							device=A_				).long(),
       }
       __lowercase						=						self.model_tester.get_config()

       __lowercase						=						MaskaFormerForUniversalSegmentation(A_				).to(A_				)
       __lowercase						=						model(**A_				)
       self.assertTrue(outputs.loss is not None				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.create_and_check_maskaformer_model(A_   ,							**A_   ,							output_hidden_states=A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        __lowercase						=						model_class(A_				).to(A_				)
        __lowercase						=						model(**A_   ,							output_attentions=A_				)
        self.assertTrue(outputs.attentions is not None				)
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       if not self.model_tester.is_training:
        return

       __lowercase						=						self.all_model_classes[1]
       __lowercase				,		__lowercase				,		__lowercase				,		__lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs()

       __lowercase						=						model_class(A_				)
       model.to(A_				)
       model.train()

       __lowercase						=						model(A_   ,							mask_labels=A_   ,							class_labels=A_				).loss
       loss.backward()






      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase						=						self.all_model_classes[1]
       __lowercase				,		__lowercase				,		__lowercase				,		__lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs()
       __lowercase						=						True
       __lowercase						=						True

       __lowercase						=						model_class(A_				).to(A_				)
       model.train()

       __lowercase						=						model(A_   ,							mask_labels=A_   ,							class_labels=A_				)

       __lowercase						=						outputs.encoder_hidden_states[0]
       encoder_hidden_states.retain_grad()

       __lowercase						=						outputs.pixel_decoder_hidden_states[0]
       pixel_decoder_hidden_states.retain_grad()

       __lowercase						=						outputs.transformer_decoder_hidden_states[0]
       transformer_decoder_hidden_states.retain_grad()

       __lowercase						=						outputs.attentions[0]
       attentions.retain_grad()

       outputs.loss.backward(retain_graph=A_				)

       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__					=1e-4


def lowerCAmelCase_   (					):






 """simple docstring"""







 __lowercase						=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
 return image





@require_vision
@slow
class  lowerCamelCase__    (		unittest.TestCase     ):
      @cached_property
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       return "facebook/mask2former-swin-small-coco-instance"
      @cached_property
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints				) if is_vision_available() else None
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						MaskaFormerModel.from_pretrained(self.model_checkpoints				).to(A_				)
       __lowercase						=						self.default_image_processor
       __lowercase						=						prepare_img()
       __lowercase						=						image_processor(A_   ,							return_tensors="""pt"""				).to(A_				)
       __lowercase						=						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(A_   ,							(1, 3, 3_8_4, 3_8_4)				)

       with torch.no_grad():
        __lowercase						=						model(**A_				)

       __lowercase						=						torch.tensor(
           [[-0.27_90, -1.07_17, -1.16_68], [-0.51_28, -0.31_28, -0.49_87], [-0.58_32, 0.19_71, -0.01_97]]				).to(A_				)
       self.assertTrue(
           torch.allclose(
               outputs.encoder_last_hidden_state[0, 0, :3, :3]   ,							A_   ,							atol=A_				)				)

       __lowercase						=						torch.tensor(
           [[0.89_73, 1.18_47, 1.17_76], [1.19_34, 1.50_40, 1.51_28], [1.11_53, 1.44_86, 1.49_51]]				).to(A_				)
       self.assertTrue(
           torch.allclose(
               outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]   ,							A_   ,							atol=A_				)				)

       __lowercase						=						torch.tensor(
           [[2.11_52, 1.70_00, -0.86_03], [1.58_08, 1.80_04, -0.93_53], [1.60_43, 1.74_95, -0.59_99]]				).to(A_				)
       self.assertTrue(
           torch.allclose(
               outputs.transformer_decoder_last_hidden_state[0, :3, :3]   ,							A_   ,							atol=A_				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       __lowercase						=						MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints				).to(A_				).eval()
       __lowercase						=						self.default_image_processor
       __lowercase						=						prepare_img()
       __lowercase						=						image_processor(A_   ,							return_tensors="""pt"""				).to(A_				)
       __lowercase						=						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(A_   ,							(1, 3, 3_8_4, 3_8_4)				)

       with torch.no_grad():
        __lowercase						=						model(**A_				)
       # masks_queries_logits
       __lowercase						=						outputs.masks_queries_logits
       self.assertEqual(
           masks_queries_logits.shape   ,							(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)				)
       __lowercase						=						[
           [-8.78_39, -9.00_56, -8.81_21],
           [-7.41_04, -7.03_13, -6.54_01],
           [-6.61_05, -6.34_27, -6.46_75],
       ]
       __lowercase						=						torch.tensor(A_				).to(A_				)
       self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,							A_   ,							atol=A_				)				)
       # class_queries_logits
       __lowercase						=						outputs.class_queries_logits
       self.assertEqual(class_queries_logits.shape   ,							(1, model.config.num_queries, model.config.num_labels + 1)				)
       __lowercase						=						torch.tensor(
           [
               [1.83_24, -8.08_35, -4.19_22],
               [0.84_50, -9.00_50, -3.60_53],
               [0.30_45, -7.72_93, -3.02_75],
           ]				).to(A_				)
       self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,							A_   ,							atol=A_				)				)






      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints				).to(A_				).eval()
       __lowercase						=						self.default_image_processor

       __lowercase						=						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"""   ,							)

       __lowercase						=						inputs["""pixel_values"""].to(A_				)
       __lowercase						=						[el.to(A_				) for el in inputs["""mask_labels"""]]
       __lowercase						=						[el.to(A_				) for el in inputs["""class_labels"""]]

       with torch.no_grad():
        __lowercase						=						model(**A_				)

       self.assertTrue(outputs.loss is not None				)




"""simple docstring"""

def lowerCAmelCase_   (					UpperCamelCase__						:			Union[str, Any]					, UpperCamelCase__						:			Union[str, Any]					, UpperCamelCase__						:			int			):






 """simple docstring"""







 if n == 0:
  return 1

 elif n % 2 == 1:
  return (binary_exponentiation(UpperCamelCase__					, n - 1					, UpperCamelCase__			) * a) % mod

 else:
  __lowercase						=						binary_exponentiation(UpperCamelCase__					, n / 2					, UpperCamelCase__			)
  return (b * b) % mod


# a prime number
UpperCAmelCase__					=701

UpperCAmelCase__					=10_0000_0000
UpperCAmelCase__					=10

# using binary exponentiation function, O(log(p)):
print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p)

print((a / b) % p == (a * b ** (p - 2)) % p)
