




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



					'''simple docstring'''





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



					def     __snake_case						(	self     :						List[str])    ->							Any:
								A_									= floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]).to(
								    _lowercase)

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

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

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



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

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

								A_									= 64
								A_									= 128
								A_									= self.hidden_dim
								A_									= self.hidden_dim
								A_									= self.hidden_dim
								return config



					def     __snake_case						(	self     :						Tuple)    ->							Optional[int]:
								A_		, A_		, A_		, A_		, A_									= self.prepare_config_and_inputs()
								A_									= {'pixel_values': pixel_values, 'pixel_mask': pixel_mask}
								return config, inputs_dict



					def     __snake_case						(	self     :						Dict      ,       _lowercase     :						Tuple      ,       _lowercase     :						Any)    ->							Tuple:
								A_									= output.encoder_hidden_states
								A_									= output.pixel_decoder_hidden_states
								A_									= output.transformer_decoder_hidden_states

								self.parent.assertTrue(len(_lowercase)      ,       len(config.backbone_config.depths))
								self.parent.assertTrue(len(_lowercase)      ,       len(config.backbone_config.depths))
								self.parent.assertTrue(len(_lowercase)      ,       config.decoder_layers)



					def     __snake_case						(	self     :						Optional[Any]      ,       _lowercase     :						Optional[int]      ,       _lowercase     :						List[str]      ,       _lowercase     :						Dict      ,       _lowercase     :						Tuple=False)    ->							Tuple:
								with torch.no_grad():
											A_									= MaskaFormerModel(config=_lowercase)
											model.to(_lowercase)
											model.eval()

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

								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(_lowercase      ,       _lowercase)



					def     __snake_case						(	self     :						List[Any]      ,       _lowercase     :						int      ,       _lowercase     :						Any      ,       _lowercase     :						Tuple      ,       _lowercase     :						str      ,       _lowercase     :						Any)    ->							Tuple:
								A_									= MaskaFormerForUniversalSegmentation(config=_lowercase)
								model.to(_lowercase)
								model.eval()

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

											comm_check_on_output(_lowercase)

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

								comm_check_on_output(_lowercase)

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





@require_torch
class   __UpperCAmelCase				(     lowerCAmelCase			,lowerCAmelCase			,unittest.TestCase				):



					'''simple docstring'''





					_UpperCamelCase							=		(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
					_UpperCamelCase							=		{"""feature-extraction""": MaskaFormerModel} if is_torch_available() else {}

					_UpperCamelCase							=		False
					_UpperCamelCase							=		False
					_UpperCamelCase							=		False
					_UpperCamelCase							=		False



					def     __snake_case						(	self     :						int)    ->							str:
								A_									= MaskaFormerModelTester(self)
								A_									= ConfigTester(self      ,       config_class=_lowercase      ,       has_text_modality=_lowercase)



					def     __snake_case						(	self     :						str)    ->							Dict:
								self.config_tester.run_common_tests()



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



					def     __snake_case						(	self     :						Dict)    ->							Tuple:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*_lowercase)



					@unittest.skip(reason='Mask2Former does not use inputs_embeds')
					def     __snake_case						(	self     :						List[Any])    ->							Any:
								pass



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



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



					@unittest.skip(reason='Mask2Former does not use token embeddings')
					def     __snake_case						(	self     :						List[Any])    ->							Optional[Any]:
								pass



					@require_torch_multi_gpu
					@unittest.skip(
					    reason='Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`')
					def     __snake_case						(	self     :						Optional[int])    ->							List[str]:
								pass



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



					def     __snake_case						(	self     :						int)    ->							List[Any]:
								A_		, A_									= self.model_tester.prepare_config_and_inputs_for_common()

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

											A_									= ['pixel_values']
											self.assertListEqual(arg_names[:1]      ,       _lowercase)



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



					def     __snake_case						(	self     :						Optional[Any])    ->							Optional[int]:
								A_									= (self.model_tester.min_size,) * 2
								A_									= {
								    'pixel_values': torch.randn((2, 3, *size)      ,       device=_lowercase),
								    'mask_labels': torch.randn((2, 10, *size)      ,       device=_lowercase),
								    'class_labels': torch.zeros(2      ,       10      ,       device=_lowercase).long(),
								}
								A_									= self.model_tester.get_config()

								A_									= MaskaFormerForUniversalSegmentation(_lowercase).to(_lowercase)
								A_									= model(**_lowercase)
								self.assertTrue(outputs.loss is not None)



					def     __snake_case						(	self     :						List[Any])    ->							Union[str, Any]:
								A_		, A_									= self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.create_and_check_maskaformer_model(_lowercase      ,       **_lowercase      ,       output_hidden_states=_lowercase)



					def     __snake_case						(	self     :						str)    ->							str:
								A_		, A_									= self.model_tester.prepare_config_and_inputs_for_common()

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



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

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

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

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



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

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

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

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

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

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

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

								outputs.loss.backward(retain_graph=_lowercase)

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





_SCREAMING_SNAKE_CASE			=						1e-4





def 			lowerCamelCase(							)		->		Any:
			A_									= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
			return image






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



					'''simple docstring'''





					@cached_property
					def     __snake_case						(	self     :						Dict)    ->							Tuple:
								return "facebook/mask2former-swin-small-coco-instance"



					@cached_property
					def     __snake_case						(	self     :						List[Any])    ->							Optional[int]:
								return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints) if is_vision_available() else None



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

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

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

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

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



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

								with torch.no_grad():
											A_									= model(**_lowercase)
								# masks_queries_logits
								A_									= outputs.masks_queries_logits
								self.assertEqual(
								    masks_queries_logits.shape      ,       (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4))
								A_									= [
								    [-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],
								]
								A_									= torch.tensor(_lowercase).to(_lowercase)
								self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]      ,       _lowercase      ,       atol=_lowercase))
								# class_queries_logits
								A_									= outputs.class_queries_logits
								self.assertEqual(class_queries_logits.shape      ,       (1, model.config.num_queries, model.config.num_labels + 1))
								A_									= 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(_lowercase)
								self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]      ,       _lowercase      ,       atol=_lowercase))



					def     __snake_case						(	self     :						Any)    ->							Tuple:
								A_									= MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(_lowercase).eval()
								A_									= self.default_image_processor

								A_									= image_processor(
								    [np.zeros((3, 800, 1_333)), np.zeros((3, 800, 1_333))]      ,       segmentation_maps=[np.zeros((384, 384)).astype(np.floataa), np.zeros((384, 384)).astype(np.floataa)]      ,       return_tensors='pt'      ,       )

								A_									= inputs['pixel_values'].to(_lowercase)
								A_									= [el.to(_lowercase) for el in inputs['mask_labels']]
								A_									= [el.to(_lowercase) for el in inputs['class_labels']]

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

								self.assertTrue(outputs.loss is not None)










'''simple docstring'''
import os
import sys

_SCREAMING_SNAKE_CASE			=						os.path.join(os.path.dirname(__file__), "src")
sys.path.append(SRC_DIR)


from transformers import (
    AutoConfig,
    AutoModel,
    AutoModelForCausalLM,
    AutoModelForMaskedLM,
    AutoModelForQuestionAnswering,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    add_start_docstrings,
)


_SCREAMING_SNAKE_CASE			=						[
    "torch",
    "numpy",
    "tokenizers",
    "filelock",
    "requests",
    "tqdm",
    "regex",
    "sentencepiece",
    "sacremoses",
    "importlib_metadata",
    "huggingface_hub",
]





@add_start_docstrings(AutoConfig.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		int:
			return AutoConfig.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoTokenizer.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		List[str]:
			return AutoTokenizer.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoModel.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		Union[str, Any]:
			return AutoModel.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoModelForCausalLM.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		Optional[int]:
			return AutoModelForCausalLM.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoModelForMaskedLM.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		Dict:
			return AutoModelForMaskedLM.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoModelForSequenceClassification.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		Union[str, Any]:
			return AutoModelForSequenceClassification.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )






@add_start_docstrings(AutoModelForQuestionAnswering.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		str:
			return AutoModelForQuestionAnswering.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )




