






"""simple docstring"""



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     :  Union[str, Any]   ,       snake_case__     :  int   ,       snake_case__     :  Union[str, Any]=13   ,       snake_case__     :  Dict=32   ,       snake_case__     :  str=2   ,       snake_case__     :  List[Any]=3   ,       snake_case__     :  Optional[Any]=16   ,       snake_case__     :  Optional[Any]=[1, 2, 1]   ,       snake_case__     :  Union[str, Any]=[2, 2, 4]   ,       snake_case__     :  List[Any]=2   ,       snake_case__     :  Optional[Any]=2.0   ,       snake_case__     :  List[Any]=True   ,       snake_case__     :  List[Any]=0.0   ,       snake_case__     :  List[Any]=0.0   ,       snake_case__     :  List[str]=0.1   ,       snake_case__     :  Optional[int]="gelu"   ,       snake_case__     :  Dict=False   ,       snake_case__     :  int=True   ,       snake_case__     :  Any=0.02   ,       snake_case__     :  str=1E-5   ,       snake_case__     :  List[Any]=True   ,       snake_case__     :  Tuple=None   ,       snake_case__     :  Union[str, Any]=True   ,       snake_case__     :  Optional[Any]=10   ,       snake_case__     :  List[str]=8   ,       snake_case__     :  Tuple=["stage1", "stage2", "stage3"]   ,       snake_case__     :  List[str]=[1, 2, 3]   ,       ):
										lowerCamelCase_       :							int									=parent
										lowerCamelCase_       :							Optional[Any]									=batch_size
										lowerCamelCase_       :							str									=image_size
										lowerCamelCase_       :							Tuple									=patch_size
										lowerCamelCase_       :							Tuple									=num_channels
										lowerCamelCase_       :							Optional[int]									=embed_dim
										lowerCamelCase_       :							Dict									=depths
										lowerCamelCase_       :							Any									=num_heads
										lowerCamelCase_       :							Any									=window_size
										lowerCamelCase_       :							int									=mlp_ratio
										lowerCamelCase_       :							Any									=qkv_bias
										lowerCamelCase_       :							Union[str, Any]									=hidden_dropout_prob
										lowerCamelCase_       :							Optional[Any]									=attention_probs_dropout_prob
										lowerCamelCase_       :							Dict									=drop_path_rate
										lowerCamelCase_       :							List[Any]									=hidden_act
										lowerCamelCase_       :							Any									=use_absolute_embeddings
										lowerCamelCase_       :							Optional[int]									=patch_norm
										lowerCamelCase_       :							Tuple									=layer_norm_eps
										lowerCamelCase_       :							List[str]									=initializer_range
										lowerCamelCase_       :							Optional[int]									=is_training
										lowerCamelCase_       :							Optional[Any]									=scope
										lowerCamelCase_       :							Optional[int]									=use_labels
										lowerCamelCase_       :							Dict									=type_sequence_label_size
										lowerCamelCase_       :							str									=encoder_stride
										lowerCamelCase_       :							Optional[int]									=out_features
										lowerCamelCase_       :							Optional[int]									=out_indices


			def 	UpperCAmelCase__						(      self     :  Tuple					):
										lowerCamelCase_       :							List[str]									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

										lowerCamelCase_       :							Optional[int]									=self.get_config()

										return config, pixel_values, labels


			def 	UpperCAmelCase__						(      self     :  Dict					):
										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     :  Optional[Any]   ,       snake_case__     :  str   ,       snake_case__     :  Union[str, Any]   ,       snake_case__     :  Union[str, Any]					):
										lowerCamelCase_       :							Dict									=MaskFormerSwinModel(config=__lowerCamelCase					)
										model.to(__lowerCamelCase					)
										model.eval()
										lowerCamelCase_       :							int									=model(__lowerCamelCase					)

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

										# 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(__lowerCamelCase					):
																	lowerCamelCase_       :							Union[str, Any]									=["stem"]
																	lowerCamelCase_       :							Union[str, Any]									=MaskFormerSwinBackbone(config=__lowerCamelCase					)


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







@require_torch
class    lowercase__			(							snake_case__,   snake_case__,   unittest.TestCase   ):
			_UpperCAmelCase		:Tuple   			= (
			    (
			        MaskFormerSwinModel,
			        MaskFormerSwinBackbone,
			    )
			    if is_torch_available()
			    else ()
			)
			_UpperCAmelCase		:Dict   			= {"feature-extraction": MaskFormerSwinModel} if is_torch_available() else {}
			_UpperCAmelCase		:Union[str, Any]   			= False
			_UpperCAmelCase		:Tuple   			= False
			_UpperCAmelCase		:Optional[int]   			= False
			_UpperCAmelCase		:Any   			= False
			_UpperCAmelCase		:Dict   			= False


			def 	UpperCAmelCase__						(      self     :  Union[str, Any]					):
										lowerCamelCase_       :							Union[str, Any]									=MaskFormerSwinModelTester(self					)
										lowerCamelCase_       :							Optional[int]									=ConfigTester(self   ,       config_class=__lowerCamelCase   ,       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     :  str					):
										pass


			def 	UpperCAmelCase__						(      self     :  Tuple					):
										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     :  Tuple					):
										return


			def 	UpperCAmelCase__						(      self     :  Dict					):
										lowerCamelCase_       :							Optional[int]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__lowerCamelCase					)


			def 	UpperCAmelCase__						(      self     :  List[str]					):
										lowerCamelCase_       :							Any									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_backbone(*__lowerCamelCase					)


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


			@unittest.skip("Swin does not support feedforward chunking"					)
			def 	UpperCAmelCase__						(      self     :  Union[str, Any]					):
										pass


			def 	UpperCAmelCase__						(      self     :  Any					):
										lowerCamelCase_       :							Union[str, Any]									=self.model_tester.prepare_config_and_inputs_for_common()

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


			def 	UpperCAmelCase__						(      self     :  Optional[Any]					):
										lowerCamelCase_       :							List[Any]									=self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowerCamelCase_       :							Optional[int]									=["pixel_values"]
																	self.assertListEqual(arg_names[:1]   ,       __lowerCamelCase					)


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


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


			def 	UpperCAmelCase__						(      self     :  List[Any]   ,       snake_case__     :  int   ,       snake_case__     :  Dict   ,       snake_case__     :  Optional[Any]   ,       snake_case__     :  Any					):
										lowerCamelCase_       :							Any									=model_class(__lowerCamelCase					)
										model.to(__lowerCamelCase					)
										model.eval()

										with torch.no_grad():
																	lowerCamelCase_       :							str									=model(**self._prepare_for_class(__lowerCamelCase   ,       __lowerCamelCase					)					)

										lowerCamelCase_       :							Tuple									=outputs.hidden_states

										lowerCamelCase_       :							Any									=getattr(
										    self.model_tester   ,       "expected_num_hidden_layers"   ,       len(self.model_tester.depths					) + 1					)
										self.assertEqual(len(__lowerCamelCase					)   ,       __lowerCamelCase					)

										# Swin has a different seq_length
										lowerCamelCase_       :							Optional[int]									=(
										    config.patch_size
										    if isinstance(config.patch_size   ,       collections.abc.Iterable					)
										    else (config.patch_size, config.patch_size)
										)

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

										lowerCamelCase_       :							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:
																	lowerCamelCase_       :							List[Any]									=True
																	self.check_hidden_states_output(__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase					)

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	lowerCamelCase_       :							Optional[int]									=True

																	self.check_hidden_states_output(__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase					)


			def 	UpperCAmelCase__						(      self     :  List[str]					):
										lowerCamelCase_       :							List[str]									=self.model_tester.prepare_config_and_inputs_for_common()
										lowerCamelCase_       :							Optional[int]									=3

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

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

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

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


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


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


			@unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin"					)
			def 	UpperCAmelCase__						(      self     :  Union[str, Any]					):
										pass


			def 	UpperCAmelCase__						(      self     :  Optional[int]					):
										lowerCamelCase_       :							Any									=self.model_tester.prepare_config_and_inputs_for_common()

										def set_nan_tensor_to_zero(snake_case__     :  str					):
																	lowerCamelCase_       :							Optional[int]									=0
																	return t

										def check_equivalence(snake_case__     :  int   ,       snake_case__     :  Tuple   ,       snake_case__     :  Optional[int]   ,       snake_case__     :  List[Any]={}					):
																	with torch.no_grad():
																								lowerCamelCase_       :							Any									=model(**__lowerCamelCase   ,       return_dict=__lowerCamelCase   ,       **__lowerCamelCase					)
																								lowerCamelCase_       :							int									=model(**__lowerCamelCase   ,       return_dict=__lowerCamelCase   ,       **__lowerCamelCase					).to_tuple()

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

																								recursive_check(__lowerCamelCase   ,       __lowerCamelCase					)

										for model_class in self.all_model_classes:
																	lowerCamelCase_       :							List[Any]									=model_class(__lowerCamelCase					)
																	model.to(__lowerCamelCase					)
																	model.eval()

																	lowerCamelCase_       :							str									=self._prepare_for_class(__lowerCamelCase   ,       __lowerCamelCase					)
																	lowerCamelCase_       :							Tuple									=self._prepare_for_class(__lowerCamelCase   ,       __lowerCamelCase					)
																	check_equivalence(__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase					)

																	lowerCamelCase_       :							Any									=self._prepare_for_class(__lowerCamelCase   ,       __lowerCamelCase   ,       return_labels=__lowerCamelCase					)
																	lowerCamelCase_       :							List[Any]									=self._prepare_for_class(__lowerCamelCase   ,       __lowerCamelCase   ,       return_labels=__lowerCamelCase					)
																	check_equivalence(__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase					)

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

																	lowerCamelCase_       :							Union[str, Any]									=self._prepare_for_class(__lowerCamelCase   ,       __lowerCamelCase   ,       return_labels=__lowerCamelCase					)
																	lowerCamelCase_       :							Optional[Any]									=self._prepare_for_class(__lowerCamelCase   ,       __lowerCamelCase   ,       return_labels=__lowerCamelCase					)
																	check_equivalence(__lowerCamelCase   ,       __lowerCamelCase   ,       __lowerCamelCase   ,       {"output_hidden_states": True}					)







@require_torch
class    lowercase__			(							unittest.TestCase,   snake_case__   ):
			_UpperCAmelCase		:Optional[Any]   			= (MaskFormerSwinBackbone,) if is_torch_available() else ()
			_UpperCAmelCase		:List[str]   			= MaskFormerSwinConfig


			def 	UpperCAmelCase__						(      self     :  Dict					):
										lowerCamelCase_       :							Tuple									=MaskFormerSwinModelTester(self					)


			def 	UpperCAmelCase__						(      self     :  List[str]					):
										lowerCamelCase_       :							List[Any]									=self.model_tester.prepare_config_and_inputs_for_common()
										lowerCamelCase_       :							Union[str, Any]									=inputs_dict["pixel_values"].shape[0]

										for backbone_class in self.all_model_classes:
																	lowerCamelCase_       :							Optional[Any]									=backbone_class(__lowerCamelCase					)
																	backbone.to(__lowerCamelCase					)
																	backbone.eval()

																	lowerCamelCase_       :							List[Any]									=backbone(**__lowerCamelCase					)

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

            # Test output_attentions=True
																	if self.has_attentions:
																								lowerCamelCase_       :							int									=backbone(**__lowerCamelCase   ,       output_attentions=__lowerCamelCase					)
																								self.assertIsNotNone(outputs.attentions					)







from typing import TYPE_CHECKING

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


lowerCAmelCase__      		= {'configuration_speech_encoder_decoder': ['SpeechEncoderDecoderConfig']}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCAmelCase__      		= ['SpeechEncoderDecoderModel']

try:
    if not is_flax_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCAmelCase__      		= ['FlaxSpeechEncoderDecoderModel']

if TYPE_CHECKING:
    from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel

    try:
        if not is_flax_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel

else:
    import sys

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