


'''simple docstring'''

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

				"""simple docstring"""




				def __init__(	self       ,    lowerCamelCase       ,    lowerCamelCase=2       ,    lowerCamelCase=True       ,    lowerCamelCase=False       ,    lowerCamelCase=10       ,    lowerCamelCase=3       ,    lowerCamelCase=32 * 4       ,    lowerCamelCase=32 * 6       ,    lowerCamelCase=4       ,    lowerCamelCase=32       ,    )    ->      Tuple:







					"""simple docstring"""






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





				def    lowercase__							(	self )    ->      Dict:







					"""simple docstring"""






					snake_case__			:			Optional[int]             =		floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to(
					    _a )

					snake_case__			:			List[Any]             =		torch.ones([self.batch_size, self.min_size, self.max_size]       ,    device=_a )

					snake_case__			:			List[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__			:			List[str]             =		self.get_config()
					return config, pixel_values, pixel_mask, mask_labels, class_labels





				def    lowercase__							(	self )    ->      Tuple:







					"""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    lowercase__							(	self )    ->      Union[str, Any]:







					"""simple docstring"""






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





				def    lowercase__							(	self       ,    lowerCamelCase       ,    lowerCamelCase )    ->      List[Any]:







					"""simple docstring"""






					snake_case__			:			int             =		output.encoder_hidden_states
					snake_case__			:			str             =		output.pixel_decoder_hidden_states
					snake_case__			:			List[Any]             =		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_config.decoder_layers )





				def    lowercase__							(	self       ,    lowerCamelCase       ,    lowerCamelCase       ,    lowerCamelCase       ,    lowerCamelCase=False )    ->      int:







					"""simple docstring"""






					with torch.no_grad():
						snake_case__			:			List[str]             =		MaskFormerModel(config=_a )
						model.to(_a )
						model.eval()

						snake_case__			:			Union[str, Any]             =		model(pixel_values=_a       ,    pixel_mask=_a )
						snake_case__			:			Union[str, Any]             =		model(_a       ,    output_hidden_states=_a )
					# 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(_a       ,    _a )





				def    lowercase__							(	self       ,    lowerCamelCase       ,    lowerCamelCase       ,    lowerCamelCase       ,    lowerCamelCase       ,    lowerCamelCase )    ->      Any:







					"""simple docstring"""






					snake_case__			:			Optional[int]             =		MaskFormerForInstanceSegmentation(config=_a )
					model.to(_a )
					model.eval()

					def comm_check_on_output(lowerCamelCase ):
						# 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__			:			Tuple             =		model(pixel_values=_a       ,    pixel_mask=_a )
						snake_case__			:			Optional[Any]             =		model(_a )

						comm_check_on_output(_a )

						snake_case__			:			Optional[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 		snake_case				(    UpperCAmelCase__				,    UpperCAmelCase__				,    unittest.TestCase						):

				"""simple docstring"""




				_lowerCAmelCase           =    (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
				_lowerCAmelCase           =    (
				    {'feature-extraction': MaskFormerModel, 'image-segmentation': MaskFormerForInstanceSegmentation}
				    if is_torch_available()
				    else {}
				)

				_lowerCAmelCase           =    False
				_lowerCAmelCase           =    False
				_lowerCAmelCase           =    False
				_lowerCAmelCase           =    False





				def    lowercase__							(	self )    ->      Optional[Any]:







					"""simple docstring"""






					snake_case__			:			str             =		MaskFormerModelTester(self )
					snake_case__			:			Any             =		ConfigTester(self       ,    config_class=_a       ,    has_text_modality=_a )





				def    lowercase__							(	self )    ->      Optional[int]:







					"""simple docstring"""






					self.config_tester.run_common_tests()





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







					"""simple docstring"""






					snake_case__      ,snake_case__			:			Optional[int]             =		self.model_tester.prepare_config_and_inputs_for_common()
					self.model_tester.create_and_check_maskformer_model(_a       ,    **_a       ,    output_hidden_states=_a )





				def    lowercase__							(	self )    ->      Dict:







					"""simple docstring"""






					snake_case__			:			Dict             =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*_a )





				@unittest.skip(reason='''MaskFormer does not use inputs_embeds''' )
				def    lowercase__							(	self )    ->      Dict:







					"""simple docstring"""






					pass





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







					"""simple docstring"""






					pass





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







					"""simple docstring"""






					pass





				@unittest.skip(reason='''MaskFormer does not use token embeddings''' )
				def    lowercase__							(	self )    ->      Union[str, Any]:







					"""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    lowercase__							(	self )    ->      Tuple:







					"""simple docstring"""






					pass





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







					"""simple docstring"""






					pass





				def    lowercase__							(	self )    ->      Optional[int]:







					"""simple docstring"""






					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__			:			List[str]             =		inspect.signature(model.forward )
						# signature.parameters is an OrderedDict => so arg_names order is deterministic
						snake_case__			:			Any             =		[*signature.parameters.keys()]

						snake_case__			:			Dict             =		['''pixel_values''']
						self.assertListEqual(arg_names[:1]       ,    _a )





				@slow
				def    lowercase__							(	self )    ->      Dict:







					"""simple docstring"""






					for model_name in ["facebook/maskformer-swin-small-coco"]:
						snake_case__			:			Dict             =		MaskFormerModel.from_pretrained(_a )
						self.assertIsNotNone(_a )





				def    lowercase__							(	self )    ->      Any:







					"""simple docstring"""






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





				def    lowercase__							(	self )    ->      Optional[int]:







					"""simple docstring"""






					snake_case__      ,snake_case__			:			Union[str, Any]             =		self.model_tester.prepare_config_and_inputs_for_common()
					self.model_tester.create_and_check_maskformer_model(_a       ,    **_a       ,    output_hidden_states=_a )





				def    lowercase__							(	self )    ->      int:







					"""simple docstring"""






					snake_case__      ,snake_case__			:			List[Any]             =		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						snake_case__			:			Dict             =		model_class(_a ).to(_a )
						snake_case__			:			Tuple             =		model(**_a       ,    output_attentions=_a )
						self.assertTrue(outputs.attentions is not None )





				def    lowercase__							(	self )    ->      Optional[int]:







					"""simple docstring"""






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

					snake_case__			:			int             =		model_class(_a )
					model.to(_a )
					model.train()

					snake_case__			:			Any             =		model(_a       ,    mask_labels=_a       ,    class_labels=_a ).loss
					loss.backward()





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







					"""simple docstring"""






					snake_case__			:			List[str]             =		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__			:			int             =		True
					snake_case__			:			int             =		True

					snake_case__			:			Any             =		model_class(_a )
					model.to(_a )
					model.train()

					snake_case__			:			Tuple             =		model(_a       ,    mask_labels=_a       ,    class_labels=_a )

					snake_case__			:			Any             =		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()
					# we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
					snake_case__			:			Union[str, Any]             =		outputs.transformer_decoder_hidden_states[0]
					transformer_decoder_hidden_states.retain_grad()

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



_lowerCAmelCase     :							Optional[int]    =				1E-4






def 					_A			(			):
	snake_case__			:			Tuple             =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
	return image





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

				"""simple docstring"""




				@cached_property
				def    lowercase__							(	self )    ->      int:







					"""simple docstring"""






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





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







					"""simple docstring"""






					snake_case__			:			Any             =		MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco''' ).to(_a )
					snake_case__			:			Tuple             =		self.default_image_processor
					snake_case__			:			List[Any]             =		prepare_img()
					snake_case__			:			List[str]             =		image_processor(_a       ,    return_tensors='''pt''' ).to(_a )
					snake_case__			:			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, 800, 1088) )

					with torch.no_grad():
						snake_case__			:			int             =		model(**_a )

					snake_case__			:			List[Any]             =		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(_a )
					self.assertTrue(
					    torch.allclose(
					        outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,    _a       ,    atol=_a ) )

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

					snake_case__			:			Tuple             =		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(_a )
					self.assertTrue(
					    torch.allclose(
					        outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,    _a       ,    atol=_a ) )





				def    lowercase__							(	self )    ->      Tuple:







					"""simple docstring"""






					snake_case__			:			List[Any]             =		(
					    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco''' )
					    .to(_a )
					    .eval()
					)
					snake_case__			:			int             =		self.default_image_processor
					snake_case__			:			List[str]             =		prepare_img()
					snake_case__			:			Tuple             =		image_processor(_a       ,    return_tensors='''pt''' ).to(_a )
					snake_case__			:			List[str]             =		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, 800, 1088) )

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





				def    lowercase__							(	self )    ->      Optional[Any]:







					"""simple docstring"""






					snake_case__			:			int             =		(
					    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff''' )
					    .to(_a )
					    .eval()
					)
					snake_case__			:			Tuple             =		self.default_image_processor
					snake_case__			:			Optional[int]             =		prepare_img()
					snake_case__			:			List[Any]             =		image_processor(_a       ,    return_tensors='''pt''' ).to(_a )
					snake_case__			:			Dict             =		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, 800, 1088) )

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





				def    lowercase__							(	self )    ->      str:







					"""simple docstring"""






					snake_case__			:			int             =		(
					    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco''' )
					    .to(_a )
					    .eval()
					)
					snake_case__			:			Dict             =		self.default_image_processor

					snake_case__			:			Dict             =		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'''       ,    )

					snake_case__			:			str             =		inputs['''pixel_values'''].to(_a )
					snake_case__			:			Optional[Any]             =		[el.to(_a ) for el in inputs['''mask_labels''']]
					snake_case__			:			int             =		[el.to(_a ) for el in inputs['''class_labels''']]

					with torch.no_grad():
						snake_case__			:			Tuple             =		model(**_a )

					self.assertTrue(outputs.loss is not None )











"""simple docstring"""




import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin






class        __magic_name__				(			unittest.TestCase		,   UpperCAmelCase__							):






				'''simple docstring'''





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   load_tool("""text-classification"""					)
							self.tool.setup()
							lowerCamelCase      =   load_tool("""text-classification"""						,					remote=_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.tool("""That's quite cool"""						,					["""positive""", """negative"""]					)
							self.assertEqual(_a						,					"""positive"""					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.remote_tool("""That's quite cool"""						,					["""positive""", """negative"""]					)
							self.assertEqual(_a						,					"""positive"""					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.tool(text="""That's quite cool"""						,					labels=["""positive""", """negative"""]					)
							self.assertEqual(_a						,					"""positive"""					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.remote_tool(text="""That's quite cool"""						,					labels=["""positive""", """negative"""]					)
							self.assertEqual(_a						,					"""positive"""					)



