import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import MaskFormerSwinBackbone
				from transformers.models.maskformer import MaskFormerSwinModel



class        lowercase_   :



							def __init__(						self				,    lowercase_				,    lowercase_=13				,    lowercase_=32				,    lowercase_=2				,    lowercase_=3				,    lowercase_=16				,    lowercase_=[1, 2, 1]				,    lowercase_=[2, 2, 4]				,    lowercase_=2				,    lowercase_=2.0				,    lowercase_=True				,    lowercase_=0.0				,    lowercase_=0.0				,    lowercase_=0.1				,    lowercase_="gelu"				,    lowercase_=False				,    lowercase_=True				,    lowercase_=0.02				,    lowercase_=1e-5				,    lowercase_=True				,    lowercase_=None				,    lowercase_=True				,    lowercase_=10				,    lowercase_=8				,    lowercase_=["stage1", "stage2", "stage3"]				,    lowercase_=[1, 2, 3]				,    ):
													_snake_case						:					str    =   parent
													_snake_case						:					Any    =   batch_size
													_snake_case						:					Optional[Any]    =   image_size
													_snake_case						:					Tuple    =   patch_size
													_snake_case						:					Optional[Any]    =   num_channels
													_snake_case						:					Any    =   embed_dim
													_snake_case						:					str    =   depths
													_snake_case						:					str    =   num_heads
													_snake_case						:					Optional[Any]    =   window_size
													_snake_case						:					Optional[Any]    =   mlp_ratio
													_snake_case						:					str    =   qkv_bias
													_snake_case						:					str    =   hidden_dropout_prob
													_snake_case						:					List[Any]    =   attention_probs_dropout_prob
													_snake_case						:					Optional[Any]    =   drop_path_rate
													_snake_case						:					int    =   hidden_act
													_snake_case						:					Any    =   use_absolute_embeddings
													_snake_case						:					Dict    =   patch_norm
													_snake_case						:					Tuple    =   layer_norm_eps
													_snake_case						:					Union[str, Any]    =   initializer_range
													_snake_case						:					List[str]    =   is_training
													_snake_case						:					Tuple    =   scope
													_snake_case						:					Optional[int]    =   use_labels
													_snake_case						:					Tuple    =   type_sequence_label_size
													_snake_case						:					int    =   encoder_stride
													_snake_case						:					str    =   out_features
													_snake_case						:					str    =   out_indices



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Tuple    =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

													_snake_case						:					Tuple    =   None
													if self.use_labels:
																			_snake_case						:					List[str]    =   ids_tensor([self.batch_size]				,    self.type_sequence_label_size    )

													_snake_case						:					Tuple    =   self.get_config()

													return config, pixel_values, labels



							def 	UpperCamelCase	(						self    ):
													return MaskFormerSwinConfig(
													    image_size=self.image_size				,    patch_size=self.patch_size				,    num_channels=self.num_channels				,    embed_dim=self.embed_dim				,    depths=self.depths				,    num_heads=self.num_heads				,    window_size=self.window_size				,    mlp_ratio=self.mlp_ratio				,    qkv_bias=self.qkv_bias				,    hidden_dropout_prob=self.hidden_dropout_prob				,    attention_probs_dropout_prob=self.attention_probs_dropout_prob				,    drop_path_rate=self.drop_path_rate				,    hidden_act=self.hidden_act				,    use_absolute_embeddings=self.use_absolute_embeddings				,    path_norm=self.patch_norm				,    layer_norm_eps=self.layer_norm_eps				,    initializer_range=self.initializer_range				,    encoder_stride=self.encoder_stride				,    out_features=self.out_features				,    out_indices=self.out_indices				,    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					int    =   MaskFormerSwinModel(config=lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					Optional[Any]    =   model(lowercase_    )

													_snake_case						:					List[Any]    =   ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
													_snake_case						:					List[str]    =   int(config.embed_dim * 2 ** (len(config.depths    ) - 1)    )

													self.parent.assertEqual(result.last_hidden_state.shape				,    (self.batch_size, expected_seq_len, expected_dim)    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Optional[Any]    =   MaskFormerSwinBackbone(config=lowercase_    )
													model.to(lowercase_    )
													model.eval()
													_snake_case						:					Optional[int]    =   model(lowercase_    )

													# verify feature maps
													self.parent.assertEqual(len(result.feature_maps    )				,    len(config.out_features    )    )
													self.parent.assertListEqual(list(result.feature_maps[0].shape    )				,    [13, 16, 16, 16]    )

													# verify channels
													self.parent.assertEqual(len(model.channels    )				,    len(config.out_features    )    )
													self.parent.assertListEqual(model.channels				,    [16, 32, 64]    )

													# verify ValueError
													with self.parent.assertRaises(lowercase_    ):
																			_snake_case						:					List[Any]    =   ["stem"]
																			_snake_case						:					Tuple    =   MaskFormerSwinBackbone(config=lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Union[str, Any]    =   self.prepare_config_and_inputs()
													_snake_case    ,_snake_case    ,_snake_case						:					List[str]    =   config_and_inputs
													_snake_case						:					int    =   {"pixel_values": pixel_values}
													return config, inputs_dict




@require_torch
class        lowercase_   (					__snake_case						,			__snake_case						,			unittest.TestCase		):
							_lowerCamelCase						=		(
							    (
							        MaskFormerSwinModel,
							        MaskFormerSwinBackbone,
							    )
							    if is_torch_available()
							    else ()
							)
							_lowerCamelCase						=		{'feature-extraction': MaskFormerSwinModel} if is_torch_available() else {}
							_lowerCamelCase						=		False
							_lowerCamelCase						=		False
							_lowerCamelCase						=		False
							_lowerCamelCase						=		False
							_lowerCamelCase						=		False



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[Any]    =   MaskFormerSwinModelTester(self    )
													_snake_case						:					Optional[int]    =   ConfigTester(self				,    config_class=lowercase_				,    embed_dim=37    )



							@require_torch_multi_gpu
							@unittest.skip(
							    reason=(
							        "`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with"
							        " `nn.DataParallel`"
							    )    )
							def 	UpperCamelCase	(						self    ):
													pass



							def 	UpperCamelCase	(						self    ):
													self.create_and_test_config_common_properties()
													self.config_tester.create_and_test_config_to_json_string()
													self.config_tester.create_and_test_config_to_json_file()
													self.config_tester.create_and_test_config_from_and_save_pretrained()
													self.config_tester.create_and_test_config_with_num_labels()
													self.config_tester.check_config_can_be_init_without_params()
													self.config_tester.check_config_arguments_init()



							def 	UpperCamelCase	(						self    ):
													return



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Tuple    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_backbone(*lowercase_    )



							@unittest.skip("Swin does not use inputs_embeds"    )
							def 	UpperCamelCase	(						self    ):
													pass



							@unittest.skip("Swin does not support feedforward chunking"    )
							def 	UpperCamelCase	(						self    ):
													pass



							def 	UpperCamelCase	(						self    ):
													_snake_case    ,_snake_case						:					Union[str, Any]    =   self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			_snake_case						:					Optional[Any]    =   model_class(lowercase_    )
																			self.assertIsInstance(model.get_input_embeddings()				,    (nn.Module)    )
																			_snake_case						:					int    =   model.get_output_embeddings()
																			self.assertTrue(x is None or isinstance(lowercase_				,    nn.Linear    )    )



							def 	UpperCamelCase	(						self    ):
													_snake_case    ,_snake_case						:					str    =   self.model_tester.prepare_config_and_inputs_for_common()

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

																			_snake_case						:					int    =   ["pixel_values"]
																			self.assertListEqual(arg_names[:1]				,    lowercase_    )



							@unittest.skip(reason="MaskFormerSwin is only used as backbone and doesn't support output_attentions"    )
							def 	UpperCamelCase	(						self    ):
													pass



							@unittest.skip(reason="MaskFormerSwin is only used as an internal backbone"    )
							def 	UpperCamelCase	(						self    ):
													pass



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					str    =   model_class(lowercase_    )
													model.to(lowercase_    )
													model.eval()

													with torch.no_grad():
																			_snake_case						:					Tuple    =   model(**self._prepare_for_class(lowercase_				,    lowercase_    )    )

													_snake_case						:					int    =   outputs.hidden_states

													_snake_case						:					Tuple    =   getattr(
													    self.model_tester				,    "expected_num_hidden_layers"				,    len(self.model_tester.depths    ) + 1    )
													self.assertEqual(len(lowercase_    )				,    lowercase_    )

													# Swin has a different seq_length
													_snake_case						:					Any    =   (
													    config.patch_size
													    if isinstance(config.patch_size				,    collections.abc.Iterable    )
													    else (config.patch_size, config.patch_size)
													)

													_snake_case						:					int    =   (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

													self.assertListEqual(
													    list(hidden_states[0].shape[-2:]    )				,    [num_patches, self.model_tester.embed_dim]				,    )



							def 	UpperCamelCase	(						self    ):
													_snake_case    ,_snake_case						:					Optional[Any]    =   self.model_tester.prepare_config_and_inputs_for_common()

													_snake_case						:					Dict    =   (
													    self.model_tester.image_size
													    if isinstance(self.model_tester.image_size				,    collections.abc.Iterable    )
													    else (self.model_tester.image_size, self.model_tester.image_size)
													)

													for model_class in self.all_model_classes:
																			_snake_case						:					List[Any]    =   True
																			self.check_hidden_states_output(lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    )

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			_snake_case						:					List[Any]    =   True

																			self.check_hidden_states_output(lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case    ,_snake_case						:					Any    =   self.model_tester.prepare_config_and_inputs_for_common()
													_snake_case						:					Union[str, Any]    =   3

													_snake_case						:					Any    =   (
													    self.model_tester.image_size
													    if isinstance(self.model_tester.image_size				,    collections.abc.Iterable    )
													    else (self.model_tester.image_size, self.model_tester.image_size)
													)
													_snake_case						:					Optional[int]    =   (
													    config.patch_size
													    if isinstance(config.patch_size				,    collections.abc.Iterable    )
													    else (config.patch_size, config.patch_size)
													)

													_snake_case						:					Optional[int]    =   image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
													_snake_case						:					Dict    =   image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

													for model_class in self.all_model_classes:
																			_snake_case						:					int    =   True
																			self.check_hidden_states_output(lowercase_				,    lowercase_				,    lowercase_				,    (padded_height, padded_width)    )

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			_snake_case						:					Optional[Any]    =   True
																			self.check_hidden_states_output(lowercase_				,    lowercase_				,    lowercase_				,    (padded_height, padded_width)    )



							@unittest.skip(reason="MaskFormerSwin doesn't have pretrained checkpoints"    )
							def 	UpperCamelCase	(						self    ):
													pass



							@unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin"    )
							def 	UpperCamelCase	(						self    ):
													pass



							@unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin"    )
							def 	UpperCamelCase	(						self    ):
													pass



							def 	UpperCamelCase	(						self    ):
													_snake_case    ,_snake_case						:					Tuple    =   self.model_tester.prepare_config_and_inputs_for_common()

													def set_nan_tensor_to_zero(lowercase_    ):
																			_snake_case						:					Tuple    =   0
																			return t

													def check_equivalence(lowercase_				,    lowercase_				,    lowercase_				,    lowercase_={}    ):
																			with torch.no_grad():
																									_snake_case						:					List[Any]    =   model(**lowercase_				,    return_dict=lowercase_				,    **lowercase_    )
																									_snake_case						:					Optional[Any]    =   model(**lowercase_				,    return_dict=lowercase_				,    **lowercase_    ).to_tuple()

																									def recursive_check(lowercase_				,    lowercase_    ):
																															if isinstance(lowercase_				,    (List, Tuple)    ):
																																					for tuple_iterable_value, dict_iterable_value in zip(lowercase_				,    lowercase_    ):
																																											recursive_check(lowercase_				,    lowercase_    )
																															elif isinstance(lowercase_				,    lowercase_    ):
																																					for tuple_iterable_value, dict_iterable_value in zip(
																																					    tuple_object.values()				,    dict_object.values()    ):
																																											recursive_check(lowercase_				,    lowercase_    )
																															elif tuple_object is None:
																																					return
																															else:
																																					self.assertTrue(
																																					    torch.allclose(
																																					        set_nan_tensor_to_zero(lowercase_    )				,    set_nan_tensor_to_zero(lowercase_    )				,    atol=1e-5    )				,    msg=(
																																					        "Tuple and dict output are not equal. Difference:"
																																					        f""" {torch.max(torch.abs(tuple_object - dict_object    )    )}. Tuple has `nan`:"""
																																					        f""" {torch.isnan(lowercase_    ).any()} and `inf`: {torch.isinf(lowercase_    )}. Dict has"""
																																					        f""" `nan`: {torch.isnan(lowercase_    ).any()} and `inf`: {torch.isinf(lowercase_    )}."""
																																					    )				,    )

																									recursive_check(lowercase_				,    lowercase_    )

													for model_class in self.all_model_classes:
																			_snake_case						:					str    =   model_class(lowercase_    )
																			model.to(lowercase_    )
																			model.eval()

																			_snake_case						:					Dict    =   self._prepare_for_class(lowercase_				,    lowercase_    )
																			_snake_case						:					str    =   self._prepare_for_class(lowercase_				,    lowercase_    )
																			check_equivalence(lowercase_				,    lowercase_				,    lowercase_    )

																			_snake_case						:					Dict    =   self._prepare_for_class(lowercase_				,    lowercase_				,    return_labels=lowercase_    )
																			_snake_case						:					List[str]    =   self._prepare_for_class(lowercase_				,    lowercase_				,    return_labels=lowercase_    )
																			check_equivalence(lowercase_				,    lowercase_				,    lowercase_    )

																			_snake_case						:					Optional[Any]    =   self._prepare_for_class(lowercase_				,    lowercase_    )
																			_snake_case						:					List[Any]    =   self._prepare_for_class(lowercase_				,    lowercase_    )
																			check_equivalence(lowercase_				,    lowercase_				,    lowercase_				,    {"output_hidden_states": True}    )

																			_snake_case						:					Optional[Any]    =   self._prepare_for_class(lowercase_				,    lowercase_				,    return_labels=lowercase_    )
																			_snake_case						:					Optional[int]    =   self._prepare_for_class(lowercase_				,    lowercase_				,    return_labels=lowercase_    )
																			check_equivalence(lowercase_				,    lowercase_				,    lowercase_				,    {"output_hidden_states": True}    )




@require_torch
class        lowercase_   (					unittest.TestCase						,			__snake_case		):
							_lowerCamelCase						=		(MaskFormerSwinBackbone,) if is_torch_available() else ()
							_lowerCamelCase						=		MaskFormerSwinConfig



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Dict    =   MaskFormerSwinModelTester(self    )



							def 	UpperCamelCase	(						self    ):
													_snake_case    ,_snake_case						:					List[Any]    =   self.model_tester.prepare_config_and_inputs_for_common()
													_snake_case						:					Dict    =   inputs_dict["pixel_values"].shape[0]

													for backbone_class in self.all_model_classes:
																			_snake_case						:					List[str]    =   backbone_class(lowercase_    )
																			backbone.to(lowercase_    )
																			backbone.eval()

																			_snake_case						:					Optional[int]    =   backbone(**lowercase_    )

																			# Test default outputs and verify feature maps
																			self.assertIsInstance(outputs.feature_maps				,    lowercase_    )
																			self.assertTrue(len(outputs.feature_maps    ) == len(backbone.channels    )    )
																			for feature_map, n_channels in zip(outputs.feature_maps				,    backbone.channels    ):
																									self.assertTrue(feature_map.shape[:2]				,    (batch_size, n_channels)    )
																			self.assertIsNone(outputs.hidden_states    )
																			self.assertIsNone(outputs.attentions    )

																			# Test output_hidden_states=True
																			_snake_case						:					Any    =   backbone(**lowercase_				,    output_hidden_states=lowercase_    )
																			self.assertIsNotNone(outputs.hidden_states    )
																			self.assertTrue(len(outputs.hidden_states    )				,    len(backbone.stage_names    )    )
																			# We skip the stem layer
																			for hidden_states, n_channels in zip(outputs.hidden_states[1:]				,    backbone.channels    ):
																									for hidden_state in hidden_states:
																															# Hidden states are in the format (batch_size, (height * width), n_channels)
																															_snake_case    ,_snake_case    ,_snake_case						:					str    =   hidden_state.shape
																															self.assertTrue((h_batch_size, h_n_channels)				,    (batch_size, n_channels)    )

            # Test output_attentions=True
																			if self.has_attentions:
																									_snake_case						:					List[Any]    =   backbone(**lowercase_				,    output_attentions=lowercase_    )
																									self.assertIsNotNone(outputs.attentions    )
import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel



class        lowercase_   (					__snake_case		):
							_lowerCamelCase						=		'M-CLIP'



							def __init__(						self				,    lowercase_=1_024				,    lowercase_=768				,    **lowercase_    ):
													_snake_case						:					str    =   transformerDimSize
													_snake_case						:					Union[str, Any]    =   imageDimSize
													super().__init__(**lowercase_    )




class        lowercase_   (					__snake_case		):
							_lowerCamelCase						=		MCLIPConfig



							def __init__(						self				,    lowercase_				,    *lowercase_				,    **lowercase_    ):
													super().__init__(lowercase_				,    *lowercase_				,    **lowercase_    )
													_snake_case						:					List[Any]    =   XLMRobertaModel(lowercase_    )
													_snake_case						:					int    =   torch.nn.Linear(
													    in_features=config.transformerDimensions				,    out_features=config.numDims    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Tuple    =   self.transformer(input_ids=lowercase_				,    attention_mask=lowercase_    )[0]
													_snake_case						:					Tuple    =   (embs * attention_mask.unsqueeze(2    )).sum(dim=1    ) / attention_mask.sum(dim=1    )[:, None]
													return self.LinearTransformation(lowercase_    ), embs