



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__  :
     """simple docstring"""



     def __init__(self				,			__a				,			__a=2				,			__a=True				,			__a=False				,			__a=10				,			__a=3				,			__a=32 * 8				,			__a=32 * 8				,			__a=4				,			__a=64				,			):






           '''simple docstring'''



           lowerCamelCase								=		parent
           lowerCamelCase								=		batch_size
           lowerCamelCase								=		is_training
           lowerCamelCase								=		use_auxiliary_loss
           lowerCamelCase								=		num_queries
           lowerCamelCase								=		num_channels
           lowerCamelCase								=		min_size
           lowerCamelCase								=		max_size
           lowerCamelCase								=		num_labels
           lowerCamelCase								=		hidden_dim
           lowerCamelCase								=		hidden_dim







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]    ).to(
               A__    )

           lowerCamelCase								=		torch.ones([self.batch_size, self.min_size, self.max_size]				,			device=A__    )

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

           lowerCamelCase								=		self.get_config()
           return config, pixel_values, pixel_mask, mask_labels, class_labels







     def 						_a      (self    ):






           '''simple docstring'''



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

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

           lowerCamelCase								=		64
           lowerCamelCase								=		1_28
           lowerCamelCase								=		self.hidden_dim
           lowerCamelCase								=		self.hidden_dim
           lowerCamelCase								=		self.hidden_dim
           return config







     def 						_a      (self    ):






           '''simple docstring'''



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







     def 						_a      (self				,			__a				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		output.encoder_hidden_states
           lowerCamelCase								=		output.pixel_decoder_hidden_states
           lowerCamelCase								=		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 						_a      (self				,			__a				,			__a				,			__a				,			__a=False    ):






           '''simple docstring'''



           with torch.no_grad():
                 lowerCamelCase								=		MaskaFormerModel(config=A__    )
                 model.to(A__    )
                 model.eval()

                 lowerCamelCase								=		model(pixel_values=A__				,			pixel_mask=A__    )
                 lowerCamelCase								=		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 						_a      (self				,			__a				,			__a				,			__a				,			__a				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		MaskaFormerForUniversalSegmentation(config=A__    )
           model.to(A__    )
           model.eval()

           def comm_check_on_output(__a    ):
                 # 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								=		model(pixel_values=A__				,			pixel_mask=A__    )
                 lowerCamelCase								=		model(A__    )

                 comm_check_on_output(A__    )

                 lowerCamelCase								=		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):
     """simple docstring"""



     _A       =			(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
     _A       =			{'feature-extraction': MaskaFormerModel} if is_torch_available() else {}

     _A       =			False
     _A       =			False
     _A       =			False
     _A       =			False







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		MaskaFormerModelTester(self    )
           lowerCamelCase								=		ConfigTester(self				,			config_class=A__				,			has_text_modality=A__    )







     def 						_a      (self    ):






           '''simple docstring'''



           self.config_tester.run_common_tests()







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.create_and_check_maskaformer_model(A__				,			**A__				,			output_hidden_states=A__    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		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 						_a      (self    ):






           '''simple docstring'''



           pass







     @unittest.skip(reason="Mask2Former does not have a get_input_embeddings method"    )
     def 						_a      (self    ):






           '''simple docstring'''



           pass







     @unittest.skip(reason="Mask2Former is not a generative model"    )
     def 						_a      (self    ):






           '''simple docstring'''



           pass







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






           '''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 						_a      (self    ):






           '''simple docstring'''



           pass







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






           '''simple docstring'''



           pass







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowerCamelCase								=		["pixel_values"]
                 self.assertListEqual(arg_names[:1]				,			A__    )







     @slow
     def 						_a      (self    ):






           '''simple docstring'''



           for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
                 lowerCamelCase								=		MaskaFormerModel.from_pretrained(A__    )
                 self.assertIsNotNone(A__    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		(self.model_tester.min_size,) * 2
           lowerCamelCase								=		{
               "pixel_values": torch.randn((2, 3, *size)				,			device=A__    ),
               "mask_labels": torch.randn((2, 10, *size)				,			device=A__    ),
               "class_labels": torch.zeros(2				,			10				,			device=A__    ).long(),
           }
           lowerCamelCase								=		self.model_tester.get_config()

           lowerCamelCase								=		MaskaFormerForUniversalSegmentation(A__    ).to(A__    )
           lowerCamelCase								=		model(**A__    )
           self.assertTrue(outputs.loss is not None    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.create_and_check_maskaformer_model(A__				,			**A__				,			output_hidden_states=A__    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 lowerCamelCase								=		model_class(A__    ).to(A__    )
                 lowerCamelCase								=		model(**A__				,			output_attentions=A__    )
                 self.assertTrue(outputs.attentions is not None    )







     def 						_a      (self    ):






           '''simple docstring'''



           if not self.model_tester.is_training:
                 return

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

           lowerCamelCase								=		model_class(A__    )
           model.to(A__    )
           model.train()

           lowerCamelCase								=		model(A__				,			mask_labels=A__				,			class_labels=A__    ).loss
           loss.backward()







     def 						_a      (self    ):






           '''simple docstring'''



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

           lowerCamelCase								=		model_class(A__    ).to(A__    )
           model.train()

           lowerCamelCase								=		model(A__				,			mask_labels=A__				,			class_labels=A__    )

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

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

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

           lowerCamelCase								=		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    )



a_						:     Optional[int]	     =		1E-4

def     __lowercase(	):


      """simple docstring"""



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






@require_vision
@slow
class    lowerCamelCase__  (						unittest.TestCase):
     """simple docstring"""



     @cached_property
     def 						_a      (self    ):






           '''simple docstring'''



           return "facebook/mask2former-swin-small-coco-instance"







     @cached_property
     def 						_a      (self    ):






           '''simple docstring'''



           return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints    ) if is_vision_available() else None







     def 						_a      (self    ):






           '''simple docstring'''



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

           with torch.no_grad():
                 lowerCamelCase								=		model(**A__    )

           lowerCamelCase								=		torch.tensor(
               [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]]    ).to(A__    )
           self.assertTrue(
               torch.allclose(
                   outputs.encoder_last_hidden_state[0, 0, :3, :3]				,			A__				,			atol=A__    )    )

           lowerCamelCase								=		torch.tensor(
               [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]]    ).to(A__    )
           self.assertTrue(
               torch.allclose(
                   outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]				,			A__				,			atol=A__    )    )

           lowerCamelCase								=		torch.tensor(
               [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]]    ).to(A__    )
           self.assertTrue(
               torch.allclose(
                   outputs.transformer_decoder_last_hidden_state[0, :3, :3]				,			A__				,			atol=A__    )    )







     def 						_a      (self    ):






           '''simple docstring'''



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

           with torch.no_grad():
                 lowerCamelCase								=		model(**A__    )
           # masks_queries_logits
           lowerCamelCase								=		outputs.masks_queries_logits
           self.assertEqual(
               masks_queries_logits.shape				,			(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)    )
           lowerCamelCase								=		[
               [-8.7839, -9.0056, -8.8121],
               [-7.4104, -7.0313, -6.5401],
               [-6.6105, -6.3427, -6.4675],
           ]
           lowerCamelCase								=		torch.tensor(A__    ).to(A__    )
           self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]				,			A__				,			atol=A__    )    )
           # class_queries_logits
           lowerCamelCase								=		outputs.class_queries_logits
           self.assertEqual(class_queries_logits.shape				,			(1, model.config.num_queries, model.config.num_labels + 1)    )
           lowerCamelCase								=		torch.tensor(
               [
                   [1.8324, -8.0835, -4.1922],
                   [0.8450, -9.0050, -3.6053],
                   [0.3045, -7.7293, -3.0275],
               ]    ).to(A__    )
           self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]				,			A__				,			atol=A__    )    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints    ).to(A__    ).eval()
           lowerCamelCase								=		self.default_image_processor

           lowerCamelCase								=		image_processor(
               [np.zeros((3, 8_00, 13_33)    ), np.zeros((3, 8_00, 13_33)    )]				,			segmentation_maps=[np.zeros((3_84, 3_84)    ).astype(np.floataa    ), np.zeros((3_84, 3_84)    ).astype(np.floataa    )]				,			return_tensors="pt"				,			)

           lowerCamelCase								=		inputs["pixel_values"].to(A__    )
           lowerCamelCase								=		[el.to(A__    ) for el in inputs["mask_labels"]]
           lowerCamelCase								=		[el.to(A__    ) for el in inputs["class_labels"]]

           with torch.no_grad():
                 lowerCamelCase								=		model(**A__    )

           self.assertTrue(outputs.loss is not None    )




from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch





@require_torch
@require_tf
class    lowerCamelCase__  (						UpperCAmelCase_):
     """simple docstring"""



     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		SMALL_MODEL_IDENTIFIER
           lowerCamelCase								=		"pt"
           lowerCamelCase								=		"tf"







     def 						_a      (self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		AutoModel.from_pretrained(self.test_model    )
           model_pt.save_pretrained(__a    )







     def 						_a      (self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		TFAutoModel.from_pretrained(self.test_model				,			from_pt=__a    )
           model_tf.save_pretrained(__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"mock_framework"

           # Framework provided - return whatever the user provides
           lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model				,			__a    )
           self.assertEqual(__a				,			__a    )

           # Local checkpoint and framework provided - return provided framework
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                 self._setup_pt_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a				,			__a    )
                 self.assertEqual(__a				,			__a    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                 self._setup_tf_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a				,			__a    )
                 self.assertEqual(__a				,			__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           with TemporaryDirectory() as local_pt_ckpt:
                 self._setup_pt_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                 self._setup_tf_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a    )
                 self.assertEqual(__a				,			self.framework_tf    )

           # Invalid local checkpoint
           with TemporaryDirectory() as local_invalid_ckpt:
                 with self.assertRaises(__a    ):
                       lowerCamelCase								=		FeaturesManager.determine_framework(__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # PyTorch not in environment -> use TensorFlow
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_torch_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_tf    )

           # Both in environment -> use PyTorch
           lowerCamelCase								=		MagicMock(return_value=__a    )
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ), patch(
               "transformers.onnx.features.is_torch_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # Both not in environment -> raise error
           lowerCamelCase								=		MagicMock(return_value=__a    )
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ), patch(
               "transformers.onnx.features.is_torch_available"				,			__a    ):
                 with self.assertRaises(__a    ):
                       lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )