"""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 		UpperCamelCase      :



				def __init__(self    :				List[Any]     ,  _A    :				Dict     ,  _A    :				Any=2     ,  _A    :				str=True     ,  _A    :				Optional[int]=False     ,  _A    :				Dict=10     ,  _A    :				str=3     ,  _A    :				Union[str, Any]=32 * 8     ,  _A    :				Tuple=32 * 8     ,  _A    :				List[Any]=4     ,  _A    :				List[Any]=64     ,  )   ->     Optional[int]:
					__snake_case      :							Dict						=			parent
					__snake_case      :							str						=			batch_size
					__snake_case      :							Optional[Any]						=			is_training
					__snake_case      :							Optional[int]						=			use_auxiliary_loss
					__snake_case      :							List[str]						=			num_queries
					__snake_case      :							Optional[int]						=			num_channels
					__snake_case      :							str						=			min_size
					__snake_case      :							Dict						=			max_size
					__snake_case      :							Optional[int]						=			num_labels
					__snake_case      :							Optional[Any]						=			hidden_dim
					__snake_case      :							Optional[Any]						=			hidden_dim



				def  _lowercase					(self    :				int)   ->     Tuple:
					__snake_case      :							Dict						=			floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]).to(
					    _A)

					__snake_case      :							Optional[int]						=			torch.ones([self.batch_size, self.min_size, self.max_size]     ,  device=_A)

					__snake_case      :							Any						=			(
					    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]     ,  device=_A) > 0.5
					).float()
					__snake_case      :							Optional[Any]						=			(torch.rand((self.batch_size, self.num_labels)     ,  device=_A) > 0.5).long()

					__snake_case      :							Optional[Any]						=			self.get_config()
					return config, pixel_values, pixel_mask, mask_labels, class_labels



				def  _lowercase					(self    :				int)   ->     Any:
					__snake_case      :							Optional[Any]						=			MaskaFormerConfig(
					    hidden_size=self.hidden_dim     ,  )
					__snake_case      :							List[str]						=			self.num_queries
					__snake_case      :							Optional[Any]						=			self.num_labels

					__snake_case      :							int						=			[1, 1, 1, 1]
					__snake_case      :							Union[str, Any]						=			self.num_channels

					__snake_case      :							Optional[int]						=			64
					__snake_case      :							List[str]						=			1_28
					__snake_case      :							Optional[int]						=			self.hidden_dim
					__snake_case      :							Any						=			self.hidden_dim
					__snake_case      :							Any						=			self.hidden_dim
					return config



				def  _lowercase					(self    :				List[Any])   ->     Any:
					__snake_case		,    __snake_case		,    __snake_case		,    __snake_case		,    __snake_case      :							Dict						=			self.prepare_config_and_inputs()
					__snake_case      :							Tuple						=			{'pixel_values': pixel_values, 'pixel_mask': pixel_mask}
					return config, inputs_dict



				def  _lowercase					(self    :				Optional[Any]     ,  _A    :				Dict     ,  _A    :				Union[str, Any])   ->     Optional[int]:
					__snake_case      :							Optional[int]						=			output.encoder_hidden_states
					__snake_case      :							List[Any]						=			output.pixel_decoder_hidden_states
					__snake_case      :							Tuple						=			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  _lowercase					(self    :				str     ,  _A    :				Union[str, Any]     ,  _A    :				Dict     ,  _A    :				Optional[int]     ,  _A    :				Optional[Any]=False)   ->     List[str]:
					with torch.no_grad():
						__snake_case      :							List[str]						=			MaskaFormerModel(config=_A)
						model.to(_A)
						model.eval()

						__snake_case      :							Tuple						=			model(pixel_values=_A     ,  pixel_mask=_A)
						__snake_case      :							Optional[Any]						=			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  _lowercase					(self    :				Any     ,  _A    :				Dict     ,  _A    :				Dict     ,  _A    :				List[Any]     ,  _A    :				Optional[int]     ,  _A    :				str)   ->     List[str]:
					__snake_case      :							str						=			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():
						__snake_case      :							Any						=			model(pixel_values=_A     ,  pixel_mask=_A)
						__snake_case      :							str						=			model(_A)

						comm_check_on_output(_A)

						__snake_case      :							Union[str, Any]						=			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 		UpperCamelCase      (   lowercase				,     lowercase				,     unittest.TestCase						):
				UpperCAmelCase    :				Dict   =		(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
				UpperCAmelCase    :				Union[str, Any]   =		{"""feature-extraction""": MaskaFormerModel} if is_torch_available() else {}

				UpperCAmelCase    :				Union[str, Any]   =		False
				UpperCAmelCase    :				Dict   =		False
				UpperCAmelCase    :				Union[str, Any]   =		False
				UpperCAmelCase    :				Union[str, Any]   =		False



				def  _lowercase					(self    :				List[str])   ->     Optional[int]:
					__snake_case      :							Union[str, Any]						=			MaskaFormerModelTester(self)
					__snake_case      :							List[Any]						=			ConfigTester(self     ,  config_class=_A     ,  has_text_modality=_A)



				def  _lowercase					(self    :				Optional[Any])   ->     Any:
					self.config_tester.run_common_tests()



				def  _lowercase					(self    :				List[Any])   ->     List[Any]:
					__snake_case		,    __snake_case      :							Optional[int]						=			self.model_tester.prepare_config_and_inputs_for_common()
					self.model_tester.create_and_check_maskaformer_model(_A     ,  **_A     ,  output_hidden_states=_A)



				def  _lowercase					(self    :				Union[str, Any])   ->     Any:
					__snake_case      :							List[Any]						=			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  _lowercase					(self    :				Union[str, Any])   ->     List[Any]:
					pass



				@unittest.skip(reason='Mask2Former does not have a get_input_embeddings method')
				def  _lowercase					(self    :				Any)   ->     List[Any]:
					pass



				@unittest.skip(reason='Mask2Former is not a generative model')
				def  _lowercase					(self    :				Tuple)   ->     Optional[int]:
					pass



				@unittest.skip(reason='Mask2Former does not use token embeddings')
				def  _lowercase					(self    :				Tuple)   ->     str:
					pass



				@require_torch_multi_gpu
				@unittest.skip(
				    reason='Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`')
				def  _lowercase					(self    :				Union[str, Any])   ->     Tuple:
					pass



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



				def  _lowercase					(self    :				Optional[int])   ->     Any:
					__snake_case		,    __snake_case      :							Tuple						=			self.model_tester.prepare_config_and_inputs_for_common()

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

						__snake_case      :							int						=			['pixel_values']
						self.assertListEqual(arg_names[:1]     ,  _A)



				@slow
				def  _lowercase					(self    :				Any)   ->     Union[str, Any]:
					for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
						__snake_case      :							Optional[Any]						=			MaskaFormerModel.from_pretrained(_A)
						self.assertIsNotNone(_A)



				def  _lowercase					(self    :				Dict)   ->     Union[str, Any]:
					__snake_case      :							int						=			(self.model_tester.min_size,) * 2
					__snake_case      :							int						=			{
					    '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(),
					}
					__snake_case      :							List[Any]						=			self.model_tester.get_config()

					__snake_case      :							Optional[Any]						=			MaskaFormerForUniversalSegmentation(_A).to(_A)
					__snake_case      :							Union[str, Any]						=			model(**_A)
					self.assertTrue(outputs.loss is not None)



				def  _lowercase					(self    :				Tuple)   ->     Optional[int]:
					__snake_case		,    __snake_case      :							int						=			self.model_tester.prepare_config_and_inputs_for_common()
					self.model_tester.create_and_check_maskaformer_model(_A     ,  **_A     ,  output_hidden_states=_A)



				def  _lowercase					(self    :				int)   ->     Dict:
					__snake_case		,    __snake_case      :							Dict						=			self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						__snake_case      :							List[Any]						=			model_class(_A).to(_A)
						__snake_case      :							List[str]						=			model(**_A     ,  output_attentions=_A)
						self.assertTrue(outputs.attentions is not None)



				def  _lowercase					(self    :				Tuple)   ->     Union[str, Any]:
					if not self.model_tester.is_training:
						return

					__snake_case      :							Dict						=			self.all_model_classes[1]
					__snake_case		,    __snake_case		,    __snake_case		,    __snake_case		,    __snake_case      :							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs()

					__snake_case      :							Optional[Any]						=			model_class(_A)
					model.to(_A)
					model.train()

					__snake_case      :							List[str]						=			model(_A     ,  mask_labels=_A     ,  class_labels=_A).loss
					loss.backward()






				def  _lowercase					(self    :				Optional[Any])   ->     Tuple:
					__snake_case      :							Union[str, Any]						=			self.all_model_classes[1]
					__snake_case		,    __snake_case		,    __snake_case		,    __snake_case		,    __snake_case      :							List[str]						=			self.model_tester.prepare_config_and_inputs()
					__snake_case      :							str						=			True
					__snake_case      :							Optional[int]						=			True

					__snake_case      :							str						=			model_class(_A).to(_A)
					model.train()

					__snake_case      :							Dict						=			model(_A     ,  mask_labels=_A     ,  class_labels=_A)

					__snake_case      :							Dict						=			outputs.encoder_hidden_states[0]
					encoder_hidden_states.retain_grad()

					__snake_case      :							int						=			outputs.pixel_decoder_hidden_states[0]
					pixel_decoder_hidden_states.retain_grad()

					__snake_case      :							Any						=			outputs.transformer_decoder_hidden_states[0]
					transformer_decoder_hidden_states.retain_grad()

					__snake_case      :							int						=			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   __UpperCAmelCase    (   )							->      List[Any]:

	'''simple docstring'''





	__snake_case      :							Dict						=			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
	return image

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



				@cached_property
				def  _lowercase					(self    :				Optional[int])   ->     Any:
					return "facebook/mask2former-swin-small-coco-instance"



				@cached_property
				def  _lowercase					(self    :				Any)   ->     Any:
					return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints) if is_vision_available() else None



				def  _lowercase					(self    :				Dict)   ->     Tuple:
					__snake_case      :							Tuple						=			MaskaFormerModel.from_pretrained(self.model_checkpoints).to(_A)
					__snake_case      :							Any						=			self.default_image_processor
					__snake_case      :							List[str]						=			prepare_img()
					__snake_case      :							Dict						=			image_processor(_A     ,  return_tensors='pt').to(_A)
					__snake_case      :							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(_A     ,  (1, 3, 3_84, 3_84))

					with torch.no_grad():
						__snake_case      :							List[str]						=			model(**_A)

					__snake_case      :							Union[str, Any]						=			torch.tensor(
					    [[-0.2_790, -1.0_717, -1.1_668], [-0.5_128, -0.3_128, -0.4_987], [-0.5_832, 0.1_971, -0.0_197]]).to(_A)
					self.assertTrue(
					    torch.allclose(
					        outputs.encoder_last_hidden_state[0, 0, :3, :3]     ,  _A     ,  atol=_A))

					__snake_case      :							Dict						=			torch.tensor(
					    [[0.8_973, 1.1_847, 1.1_776], [1.1_934, 1.5_040, 1.5_128], [1.1_153, 1.4_486, 1.4_951]]).to(_A)
					self.assertTrue(
					    torch.allclose(
					        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]     ,  _A     ,  atol=_A))

					__snake_case      :							Tuple						=			torch.tensor(
					    [[2.1_152, 1.7_000, -0.8_603], [1.5_808, 1.8_004, -0.9_353], [1.6_043, 1.7_495, -0.5_999]]).to(_A)
					self.assertTrue(
					    torch.allclose(
					        outputs.transformer_decoder_last_hidden_state[0, :3, :3]     ,  _A     ,  atol=_A))



				def  _lowercase					(self    :				List[str])   ->     Optional[Any]:
					__snake_case      :							Union[str, Any]						=			MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(_A).eval()
					__snake_case      :							Optional[Any]						=			self.default_image_processor
					__snake_case      :							Union[str, Any]						=			prepare_img()
					__snake_case      :							List[str]						=			image_processor(_A     ,  return_tensors='pt').to(_A)
					__snake_case      :							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(_A     ,  (1, 3, 3_84, 3_84))

					with torch.no_grad():
						__snake_case      :							Union[str, Any]						=			model(**_A)
					# masks_queries_logits
					__snake_case      :							Optional[Any]						=			outputs.masks_queries_logits
					self.assertEqual(
					    masks_queries_logits.shape     ,  (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4))
					__snake_case      :							Any						=			[
					    [-8.7_839, -9.0_056, -8.8_121],
					    [-7.4_104, -7.0_313, -6.5_401],
					    [-6.6_105, -6.3_427, -6.4_675],
					]
					__snake_case      :							str						=			torch.tensor(_A).to(_A)
					self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]     ,  _A     ,  atol=_A))
					# class_queries_logits
					__snake_case      :							Dict						=			outputs.class_queries_logits
					self.assertEqual(class_queries_logits.shape     ,  (1, model.config.num_queries, model.config.num_labels + 1))
					__snake_case      :							Dict						=			torch.tensor(
					    [
					        [1.8_324, -8.0_835, -4.1_922],
					        [0.8_450, -9.0_050, -3.6_053],
					        [0.3_045, -7.7_293, -3.0_275],
					    ]).to(_A)
					self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]     ,  _A     ,  atol=_A))






				def  _lowercase					(self    :				Union[str, Any])   ->     int:
					__snake_case      :							List[str]						=			MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(_A).eval()
					__snake_case      :							Tuple						=			self.default_image_processor

					__snake_case      :							List[Any]						=			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'     ,  )

					__snake_case      :							Union[str, Any]						=			inputs['pixel_values'].to(_A)
					__snake_case      :							Optional[Any]						=			[el.to(_A) for el in inputs['mask_labels']]
					__snake_case      :							str						=			[el.to(_A) for el in inputs['class_labels']]

					with torch.no_grad():
						__snake_case      :							Dict						=			model(**_A)

					self.assertTrue(outputs.loss is not None)






"""simple docstring"""







def   __UpperCAmelCase    (   UpperCAmelCase_   :    list      ,							UpperCAmelCase_   :    int      ,							UpperCAmelCase_   :    int = 0      ,							UpperCAmelCase_   :    int = 0  )							->      int:

	'''simple docstring'''





	__snake_case      :							str						=			right or len(UpperCAmelCase_  ) - 1
	if left > right:
		return -1
	elif list_data[left] == key:
		return left
	elif list_data[right] == key:
		return right
	else:
		return search(UpperCAmelCase_      ,							UpperCAmelCase_      ,							left + 1      ,							right - 1  )


if __name__ == "__main__":
	import doctest

	doctest.testmod()





