



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







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







					def  _lowercase			(       self      :     str):
											A__	:							Any						    = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

											A__	:							Optional[int]						    = None
											if self.use_labels:
																	A__	:							int						    = ids_tensor([self.batch_size]			,		self.type_sequence_label_size)

											A__	:							List[str]						    = self.get_config()

											return config, pixel_values, labels







					def  _lowercase			(       self      :     Any):
											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  _lowercase			(       self      :     Tuple			,		_A      :     Optional[int]			,		_A      :     int			,		_A      :     Optional[int]):
											A__	:							Tuple						    = MaskFormerSwinModel(config=_A)
											model.to(_A)
											model.eval()
											A__	:							Optional[Any]						    = model(_A)

											A__	:							List[str]						    = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1))
											A__	:							Dict						    = 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  _lowercase			(       self      :     Union[str, Any]			,		_A      :     Optional[Any]			,		_A      :     Dict			,		_A      :     Any):
											A__	:							Any						    = MaskFormerSwinBackbone(config=_A)
											model.to(_A)
											model.eval()
											A__	:							str						    = model(_A)

											# 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(_A):
																	A__	:							Dict						    = ["stem"]
																	A__	:							Tuple						    = MaskFormerSwinBackbone(config=_A)







					def  _lowercase			(       self      :     Optional[Any]):
											A__	:							int						    = self.prepare_config_and_inputs()
											A__    ,       A__    ,       A__	:							Optional[Any]						    = config_and_inputs
											A__	:							int						    = {"pixel_values": pixel_values}
											return config, inputs_dict




@require_torch
class 							lowerCAmelCase__  (	UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase						):
					__A     :    Union[str, Any]           =						(
					    (
					        MaskFormerSwinModel,
					        MaskFormerSwinBackbone,
					    )
					    if is_torch_available()
					    else ()
					)
					__A     :    Optional[Any]           =						{'feature-extraction': MaskFormerSwinModel} if is_torch_available() else {}
					__A     :    int           =						False
					__A     :    int           =						False
					__A     :    Any           =						False
					__A     :    Optional[Any]           =						False
					__A     :    Dict           =						False







					def  _lowercase			(       self      :     List[Any]):
											A__	:							List[str]						    = MaskFormerSwinModelTester(self)
											A__	:							Optional[Any]						    = ConfigTester(self			,		config_class=_A			,		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  _lowercase			(       self      :     Tuple):
											pass







					def  _lowercase			(       self      :     Optional[int]):
											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  _lowercase			(       self      :     Union[str, Any]):
											return







					def  _lowercase			(       self      :     int):
											A__	:							Union[str, Any]						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A)







					def  _lowercase			(       self      :     str):
											A__	:							Optional[int]						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_backbone(*_A)







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







					@unittest.skip("Swin does not support feedforward chunking")
					def  _lowercase			(       self      :     List[str]):
											pass







					def  _lowercase			(       self      :     Any):
											A__    ,       A__	:							Dict						    = self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	A__	:							Any						    = model_class(_A)
																	self.assertIsInstance(model.get_input_embeddings()			,		(nn.Module))
																	A__	:							List[str]						    = model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(_A			,		nn.Linear))







					def  _lowercase			(       self      :     Any):
											A__    ,       A__	:							List[Any]						    = self.model_tester.prepare_config_and_inputs_for_common()

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

																	A__	:							Any						    = ["pixel_values"]
																	self.assertListEqual(arg_names[:1]			,		_A)







					@unittest.skip(reason="MaskFormerSwin is only used as backbone and doesn't support output_attentions")
					def  _lowercase			(       self      :     Union[str, Any]):
											pass







					@unittest.skip(reason="MaskFormerSwin is only used as an internal backbone")
					def  _lowercase			(       self      :     Union[str, Any]):
											pass







					def  _lowercase			(       self      :     Tuple			,		_A      :     int			,		_A      :     Union[str, Any]			,		_A      :     Optional[int]			,		_A      :     List[Any]):
											A__	:							Tuple						    = model_class(_A)
											model.to(_A)
											model.eval()

											with torch.no_grad():
																	A__	:							int						    = model(**self._prepare_for_class(_A			,		_A))

											A__	:							Union[str, Any]						    = outputs.hidden_states

											A__	:							Union[str, Any]						    = getattr(
											    self.model_tester			,		"expected_num_hidden_layers"			,		len(self.model_tester.depths) + 1)
											self.assertEqual(len(_A)			,		_A)

											# Swin has a different seq_length
											A__	:							List[str]						    = (
											    config.patch_size
											    if isinstance(config.patch_size			,		collections.abc.Iterable)
											    else (config.patch_size, config.patch_size)
											)

											A__	:							List[Any]						    = (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  _lowercase			(       self      :     List[str]):
											A__    ,       A__	:							List[Any]						    = self.model_tester.prepare_config_and_inputs_for_common()

											A__	:							Union[str, 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)
											)

											for model_class in self.all_model_classes:
																	A__	:							Dict						    = True
																	self.check_hidden_states_output(_A			,		_A			,		_A			,		_A)

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

																	self.check_hidden_states_output(_A			,		_A			,		_A			,		_A)







					def  _lowercase			(       self      :     Optional[Any]):
											A__    ,       A__	:							Union[str, Any]						    = self.model_tester.prepare_config_and_inputs_for_common()
											A__	:							Union[str, Any]						    = 3

											A__	:							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)
											)
											A__	:							List[Any]						    = (
											    config.patch_size
											    if isinstance(config.patch_size			,		collections.abc.Iterable)
											    else (config.patch_size, config.patch_size)
											)

											A__	:							Optional[Any]						    = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
											A__	:							Tuple						    = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

											for model_class in self.all_model_classes:
																	A__	:							List[Any]						    = True
																	self.check_hidden_states_output(_A			,		_A			,		_A			,		(padded_height, padded_width))

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







					@unittest.skip(reason="MaskFormerSwin doesn't have pretrained checkpoints")
					def  _lowercase			(       self      :     Union[str, Any]):
											pass







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







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







					def  _lowercase			(       self      :     Dict):
											A__    ,       A__	:							Optional[Any]						    = self.model_tester.prepare_config_and_inputs_for_common()

											def set_nan_tensor_to_zero(_A      :     List[str]):
																	A__	:							List[str]						    = 0
																	return t

											def check_equivalence(_A      :     List[str]			,		_A      :     str			,		_A      :     Any			,		_A      :     Dict={}):
																	with torch.no_grad():
																							A__	:							int						    = model(**_A			,		return_dict=_A			,		**_A)
																							A__	:							Optional[int]						    = model(**_A			,		return_dict=_A			,		**_A).to_tuple()

																							def recursive_check(_A      :     str			,		_A      :     Tuple):
																													if isinstance(_A			,		(List, Tuple)):
																																			for tuple_iterable_value, dict_iterable_value in zip(_A			,		_A):
																																									recursive_check(_A			,		_A)
																													elif isinstance(_A			,		_A):
																																			for tuple_iterable_value, dict_iterable_value in zip(
																																			    tuple_object.values()			,		dict_object.values()):
																																									recursive_check(_A			,		_A)
																													elif tuple_object is None:
																																			return
																													else:
																																			self.assertTrue(
																																			    torch.allclose(
																																			        set_nan_tensor_to_zero(_A)			,		set_nan_tensor_to_zero(_A)			,		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(_A).any()} and `inf`: {torch.isinf(_A)}. Dict has'
																																			        F' `nan`: {torch.isnan(_A).any()} and `inf`: {torch.isinf(_A)}.'
																																			    )			,		)

																							recursive_check(_A			,		_A)

											for model_class in self.all_model_classes:
																	A__	:							Optional[int]						    = model_class(_A)
																	model.to(_A)
																	model.eval()

																	A__	:							Tuple						    = self._prepare_for_class(_A			,		_A)
																	A__	:							List[Any]						    = self._prepare_for_class(_A			,		_A)
																	check_equivalence(_A			,		_A			,		_A)

																	A__	:							Any						    = self._prepare_for_class(_A			,		_A			,		return_labels=_A)
																	A__	:							Dict						    = self._prepare_for_class(_A			,		_A			,		return_labels=_A)
																	check_equivalence(_A			,		_A			,		_A)

																	A__	:							str						    = self._prepare_for_class(_A			,		_A)
																	A__	:							List[Any]						    = self._prepare_for_class(_A			,		_A)
																	check_equivalence(_A			,		_A			,		_A			,		{"output_hidden_states": True})

																	A__	:							List[Any]						    = self._prepare_for_class(_A			,		_A			,		return_labels=_A)
																	A__	:							Tuple						    = self._prepare_for_class(_A			,		_A			,		return_labels=_A)
																	check_equivalence(_A			,		_A			,		_A			,		{"output_hidden_states": True})




@require_torch
class 							lowerCAmelCase__  (	unittest.TestCase					,							UpperCamelCase						):
					__A     :    int           =						(MaskFormerSwinBackbone,) if is_torch_available() else ()
					__A     :    Any           =						MaskFormerSwinConfig







					def  _lowercase			(       self      :     Optional[Any]):
											A__	:							Optional[int]						    = MaskFormerSwinModelTester(self)







					def  _lowercase			(       self      :     Optional[int]):
											A__    ,       A__	:							Optional[int]						    = self.model_tester.prepare_config_and_inputs_for_common()
											A__	:							int						    = inputs_dict["pixel_values"].shape[0]

											for backbone_class in self.all_model_classes:
																	A__	:							List[Any]						    = backbone_class(_A)
																	backbone.to(_A)
																	backbone.eval()

																	A__	:							Tuple						    = backbone(**_A)

																	# Test default outputs and verify feature maps
																	self.assertIsInstance(outputs.feature_maps			,		_A)
																	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
																	A__	:							Union[str, Any]						    = backbone(**_A			,		output_hidden_states=_A)
																	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)
																													A__    ,       A__    ,       A__	:							Optional[Any]						    = hidden_state.shape
																													self.assertTrue((h_batch_size, h_n_channels)			,		(batch_size, n_channels))

            # Test output_attentions=True
																	if self.has_attentions:
																							A__	:							List[str]						    = backbone(**_A			,		output_attentions=_A)
																							self.assertIsNotNone(outputs.attentions)




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


snake_case    :						Optional[int]  =						logging.get_logger(__name__)

snake_case    :						Union[str, Any]  =						{
    'facebook/data2vec-text-base': 'https://huggingface.co/data2vec/resolve/main/config.json',
}




class 							lowerCAmelCase__  (	UpperCamelCase						):
					__A     :    Optional[Any]           =						'data2vec-text'







					def __init__(       self      :     Optional[Any]			,		_A      :     List[str]=3_0522			,		_A      :     Optional[int]=768			,		_A      :     Union[str, Any]=12			,		_A      :     str=12			,		_A      :     Optional[int]=3072			,		_A      :     Dict="gelu"			,		_A      :     List[str]=0.1			,		_A      :     Any=0.1			,		_A      :     str=512			,		_A      :     Any=2			,		_A      :     str=0.02			,		_A      :     List[str]=1e-12			,		_A      :     Optional[int]=1			,		_A      :     Any=0			,		_A      :     Any=2			,		_A      :     List[Any]="absolute"			,		_A      :     Tuple=True			,		_A      :     str=None			,		**_A      :     List[Any]			,		):
											super().__init__(pad_token_id=_A			,		bos_token_id=_A			,		eos_token_id=_A			,		**_A)

											A__	:							Optional[int]						    = vocab_size
											A__	:							Tuple						    = hidden_size
											A__	:							str						    = num_hidden_layers
											A__	:							Union[str, Any]						    = num_attention_heads
											A__	:							Dict						    = hidden_act
											A__	:							List[Any]						    = intermediate_size
											A__	:							int						    = hidden_dropout_prob
											A__	:							Dict						    = attention_probs_dropout_prob
											A__	:							Union[str, Any]						    = max_position_embeddings
											A__	:							Tuple						    = type_vocab_size
											A__	:							Union[str, Any]						    = initializer_range
											A__	:							str						    = layer_norm_eps
											A__	:							int						    = position_embedding_type
											A__	:							str						    = use_cache
											A__	:							int						    = classifier_dropout




class 							lowerCAmelCase__  (	UpperCamelCase						):







					@property
					def  _lowercase			(       self      :     Tuple):
											if self.task == "multiple-choice":
																	A__	:							List[str]						    = {0: "batch", 1: "choice", 2: "sequence"}
											else:
																	A__	:							List[str]						    = {0: "batch", 1: "sequence"}
											return OrderedDict(
											    [
											        ("input_ids", dynamic_axis),
											        ("attention_mask", dynamic_axis),
											    ])