





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




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




				def        __lowercase	(  self	:List[Any]      ):
											__lowerCamelCase   :   Optional[Any]       =floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]      ).to(
											    __lowercase      )

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

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

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




				def        __lowercase	(  self	:List[str]      ):
											__lowerCamelCase   :   Any       =MaskaFormerConfig(
											    hidden_size=self.hidden_dim    ,     )
											__lowerCamelCase   :   Dict       =self.num_queries
											__lowerCamelCase   :   str       =self.num_labels

											__lowerCamelCase   :   Optional[int]       =[1, 1, 1, 1]
											__lowerCamelCase   :   int       =self.num_channels

											__lowerCamelCase   :   str       =64
											__lowerCamelCase   :   Union[str, Any]       =128
											__lowerCamelCase   :   Dict       =self.hidden_dim
											__lowerCamelCase   :   Tuple       =self.hidden_dim
											__lowerCamelCase   :   Optional[int]       =self.hidden_dim
											return config




				def        __lowercase	(  self	:List[Any]      ):
											__lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase   :   List[Any]       =self.prepare_config_and_inputs()
											__lowerCamelCase   :   Union[str, Any]       ={'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
											return config, inputs_dict




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

																		__lowerCamelCase   :   Union[str, Any]       =model(pixel_values=__lowercase    ,     pixel_mask=__lowercase      )
																		__lowerCamelCase   :   Optional[int]       =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        __lowercase	(  self	:Any    ,     __lowercase	:Tuple    ,     __lowercase	:List[str]    ,     __lowercase	:List[Any]    ,     __lowercase	:Tuple    ,     __lowercase	:str      ):
											__lowerCamelCase   :   Dict       =MaskaFormerForUniversalSegmentation(config=__lowercase      )
											model.to(__lowercase      )
											model.eval()

											def comm_check_on_output(__lowercase	:Union[str, 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():
																		__lowerCamelCase   :   Optional[int]       =model(pixel_values=__lowercase    ,     pixel_mask=__lowercase      )
																		__lowerCamelCase   :   str       =model(__lowercase      )

																		comm_check_on_output(__lowercase      )

																		__lowerCamelCase   :   List[str]       =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 		SCREAMING_SNAKE_CASE_				(     snake_case__							,	snake_case__							,	unittest.TestCase ):
				"""simple docstring"""
				__snake_case :							Optional[Any]									=		(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
				__snake_case :							Tuple									=		{"""feature-extraction""": MaskaFormerModel} if is_torch_available() else {}

				__snake_case :							Tuple									=		False
				__snake_case :							Optional[int]									=		False
				__snake_case :							Optional[int]									=		False
				__snake_case :							str									=		False




				def        __lowercase	(  self	:str      ):
											__lowerCamelCase   :   Dict       =MaskaFormerModelTester(self      )
											__lowerCamelCase   :   List[Any]       =ConfigTester(self    ,     config_class=__lowercase    ,     has_text_modality=__lowercase      )




				def        __lowercase	(  self	:Tuple      ):
											self.config_tester.run_common_tests()




				def        __lowercase	(  self	:List[str]      ):
											__lowerCamelCase     ,    __lowerCamelCase   :   List[str]       =self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.create_and_check_maskaformer_model(__lowercase    ,     **__lowercase    ,     output_hidden_states=__lowercase      )




				def        __lowercase	(  self	:Any      ):
											__lowerCamelCase   :   Dict       =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        __lowercase	(  self	:int      ):
											pass




				@unittest.skip(reason='''Mask2Former does not have a get_input_embeddings method'''      )
				def        __lowercase	(  self	:Union[str, Any]      ):
											pass




				@unittest.skip(reason='''Mask2Former is not a generative model'''      )
				def        __lowercase	(  self	:List[str]      ):
											pass




				@unittest.skip(reason='''Mask2Former does not use token embeddings'''      )
				def        __lowercase	(  self	:List[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        __lowercase	(  self	:Optional[Any]      ):
											pass




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




				def        __lowercase	(  self	:Dict      ):
											__lowerCamelCase     ,    __lowerCamelCase   :   int       =self.model_tester.prepare_config_and_inputs_for_common()

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

																		__lowerCamelCase   :   Any       =['''pixel_values''']
																		self.assertListEqual(arg_names[:1]    ,     __lowercase      )




				@slow
				def        __lowercase	(  self	:int      ):
											for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
																		__lowerCamelCase   :   List[Any]       =MaskaFormerModel.from_pretrained(__lowercase      )
																		self.assertIsNotNone(__lowercase      )




				def        __lowercase	(  self	:Any      ):
											__lowerCamelCase   :   str       =(self.model_tester.min_size,) * 2
											__lowerCamelCase   :   str       ={
											    '''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(),
											}
											__lowerCamelCase   :   Any       =self.model_tester.get_config()

											__lowerCamelCase   :   Optional[Any]       =MaskaFormerForUniversalSegmentation(__lowercase      ).to(__lowercase      )
											__lowerCamelCase   :   Any       =model(**__lowercase      )
											self.assertTrue(outputs.loss is not None      )




				def        __lowercase	(  self	:int      ):
											__lowerCamelCase     ,    __lowerCamelCase   :   List[str]       =self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.create_and_check_maskaformer_model(__lowercase    ,     **__lowercase    ,     output_hidden_states=__lowercase      )




				def        __lowercase	(  self	:Union[str, Any]      ):
											__lowerCamelCase     ,    __lowerCamelCase   :   Dict       =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase   :   Optional[int]       =model_class(__lowercase      ).to(__lowercase      )
																		__lowerCamelCase   :   Tuple       =model(**__lowercase    ,     output_attentions=__lowercase      )
																		self.assertTrue(outputs.attentions is not None      )




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

											__lowerCamelCase   :   str       =self.all_model_classes[1]
											__lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase   :   str       =self.model_tester.prepare_config_and_inputs()

											__lowerCamelCase   :   int       =model_class(__lowercase      )
											model.to(__lowercase      )
											model.train()

											__lowerCamelCase   :   Union[str, Any]       =model(__lowercase    ,     mask_labels=__lowercase    ,     class_labels=__lowercase      ).loss
											loss.backward()




				def        __lowercase	(  self	:List[str]      ):
											__lowerCamelCase   :   List[Any]       =self.all_model_classes[1]
											__lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase   :   Dict       =self.model_tester.prepare_config_and_inputs()
											__lowerCamelCase   :   List[Any]       =True
											__lowerCamelCase   :   Union[str, Any]       =True

											__lowerCamelCase   :   List[Any]       =model_class(__lowercase      ).to(__lowercase      )
											model.train()

											__lowerCamelCase   :   Dict       =model(__lowercase    ,     mask_labels=__lowercase    ,     class_labels=__lowercase      )

											__lowerCamelCase   :   str       =outputs.encoder_hidden_states[0]
											encoder_hidden_states.retain_grad()

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

											__lowerCamelCase   :   List[str]       =outputs.transformer_decoder_hidden_states[0]
											transformer_decoder_hidden_states.retain_grad()

											__lowerCamelCase   :   Tuple       =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      )

_UpperCamelCase					      =   1e-4



def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							__lowerCamelCase   :   Any       =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
							return image
@require_vision
@slow
class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




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




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




				def        __lowercase	(  self	:Optional[int]      ):
											__lowerCamelCase   :   Dict       =MaskaFormerModel.from_pretrained(self.model_checkpoints      ).to(__lowercase      )
											__lowerCamelCase   :   int       =self.default_image_processor
											__lowerCamelCase   :   List[Any]       =prepare_img()
											__lowerCamelCase   :   List[str]       =image_processor(__lowercase    ,     return_tensors='''pt'''      ).to(__lowercase      )
											__lowerCamelCase   :   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(__lowercase    ,     (1, 3, 384, 384)      )

											with torch.no_grad():
																		__lowerCamelCase   :   Optional[Any]       =model(**__lowercase      )

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

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

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




				def        __lowercase	(  self	:int      ):
											__lowerCamelCase   :   Tuple       =MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints      ).to(__lowercase      ).eval()
											__lowerCamelCase   :   Union[str, Any]       =self.default_image_processor
											__lowerCamelCase   :   Union[str, Any]       =prepare_img()
											__lowerCamelCase   :   Any       =image_processor(__lowercase    ,     return_tensors='''pt'''      ).to(__lowercase      )
											__lowerCamelCase   :   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(__lowercase    ,     (1, 3, 384, 384)      )

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




				def        __lowercase	(  self	:str      ):
											__lowerCamelCase   :   Optional[Any]       =MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints      ).to(__lowercase      ).eval()
											__lowerCamelCase   :   List[str]       =self.default_image_processor

											__lowerCamelCase   :   str       =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'''    ,     )

											__lowerCamelCase   :   Optional[int]       =inputs['''pixel_values'''].to(__lowercase      )
											__lowerCamelCase   :   Tuple       =[el.to(__lowercase      ) for el in inputs['''mask_labels''']]
											__lowerCamelCase   :   Tuple       =[el.to(__lowercase      ) for el in inputs['''class_labels''']]

											with torch.no_grad():
																		__lowerCamelCase   :   str       =model(**__lowercase      )

											self.assertTrue(outputs.loss is not None      )











"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_UpperCamelCase					      =   {
    'configuration_pix2struct': [
        'PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'Pix2StructConfig',
        'Pix2StructTextConfig',
        'Pix2StructVisionConfig',
    ],
    'processing_pix2struct': ['Pix2StructProcessor'],
}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							_UpperCamelCase					      =   ['Pix2StructImageProcessor']


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							_UpperCamelCase					      =   [
							    'PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST',
							    'Pix2StructPreTrainedModel',
							    'Pix2StructForConditionalGeneration',
							    'Pix2StructVisionModel',
							    'Pix2StructTextModel',
							]

if TYPE_CHECKING:
							from .configuration_pixastruct import (
							    PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    PixaStructConfig,
							    PixaStructTextConfig,
							    PixaStructVisionConfig,
							)
							from .processing_pixastruct import PixaStructProcessor

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .image_processing_pixastruct import PixaStructImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_pixastruct import (
														    PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    PixaStructForConditionalGeneration,
														    PixaStructPreTrainedModel,
														    PixaStructTextModel,
														    PixaStructVisionModel,
														)

else:
							import sys

							_UpperCamelCase					      =   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




