






"""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     ,     a_     ,     a_=1_3     ,     a_=3_2     ,     a_=2     ,     a_=3     ,     a_=1_6     ,     a_=[1, 2, 1]     ,     a_=[2, 2, 4]     ,     a_=2     ,     a_=2.0     ,     a_=True     ,     a_=0.0     ,     a_=0.0     ,     a_=0.1     ,     a_="gelu"     ,     a_=False     ,     a_=True     ,     a_=0.02     ,     a_=1E-5     ,     a_=True     ,     a_=None     ,     a_=True     ,     a_=1_0     ,     a_=8     ,     a_=["stage1", "stage2", "stage3"]     ,     a_=[1, 2, 3]     ,     )	->Optional[Any]:







        '''simple docstring'''


        _a							=	parent
        _a							=	batch_size
        _a							=	image_size
        _a							=	patch_size
        _a							=	num_channels
        _a							=	embed_dim
        _a							=	depths
        _a							=	num_heads
        _a							=	window_size
        _a							=	mlp_ratio
        _a							=	qkv_bias
        _a							=	hidden_dropout_prob
        _a							=	attention_probs_dropout_prob
        _a							=	drop_path_rate
        _a							=	hidden_act
        _a							=	use_absolute_embeddings
        _a							=	patch_norm
        _a							=	layer_norm_eps
        _a							=	initializer_range
        _a							=	is_training
        _a							=	scope
        _a							=	use_labels
        _a							=	type_sequence_label_size
        _a							=	encoder_stride
        _a							=	out_features
        _a							=	out_indices






      def 			lowerCamelCase__    (    self	)	->Optional[Any]:







        '''simple docstring'''


        _a							=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

        _a							=	None
        if self.use_labels:
          _a							=	ids_tensor([self.batch_size]     ,     self.type_sequence_label_size	)

        _a							=	self.get_config()

        return config, pixel_values, labels






      def 			lowerCamelCase__    (    self	)	->int:







        '''simple docstring'''


        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 			lowerCamelCase__    (    self     ,     a_     ,     a_     ,     a_	)	->Union[str, Any]:







        '''simple docstring'''


        _a							=	MaskFormerSwinModel(config=A_	)
        model.to(A_	)
        model.eval()
        _a							=	model(A_	)

        _a							=	((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths	) - 1))
        _a							=	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 			lowerCamelCase__    (    self     ,     a_     ,     a_     ,     a_	)	->int:







        '''simple docstring'''


        _a							=	MaskFormerSwinBackbone(config=A_	)
        model.to(A_	)
        model.eval()
        _a							=	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	)     ,     [1_3, 1_6, 1_6, 1_6]	)

        # verify channels
        self.parent.assertEqual(len(model.channels	)     ,     len(config.out_features	)	)
        self.parent.assertListEqual(model.channels     ,     [1_6, 3_2, 6_4]	)

        # verify ValueError
        with self.parent.assertRaises(A_	):
          _a							=	["stem"]
          _a							=	MaskFormerSwinBackbone(config=A_	)






      def 			lowerCamelCase__    (    self	)	->Optional[Any]:







        '''simple docstring'''


        _a							=	self.prepare_config_and_inputs()
        _a			,	_a			,	_a							=	config_and_inputs
        _a							=	{"pixel_values": pixel_values}
        return config, inputs_dict







@require_torch
class    lowercase_    (_UpperCAmelCase,    _UpperCAmelCase,    unittest.TestCase     ):
      A__	:		str       			=(
          (
              MaskFormerSwinModel,
              MaskFormerSwinBackbone,
          )
          if is_torch_available()
          else ()
      )
      A__	:		Union[str, Any]       			={'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {}
      A__	:		Optional[Any]       			=False
      A__	:		Union[str, Any]       			=False
      A__	:		Optional[Any]       			=False
      A__	:		Dict       			=False
      A__	:		List[Any]       			=False






      def 			lowerCamelCase__    (    self	)	->List[str]:







        '''simple docstring'''


        _a							=	MaskFormerSwinModelTester(self	)
        _a							=	ConfigTester(self     ,     config_class=A_     ,     embed_dim=3_7	)






      @require_torch_multi_gpu
      @unittest.skip(
          reason=(
              "`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with"
              " `nn.DataParallel`"
          )	)
      def 			lowerCamelCase__    (    self	)	->Any:







        '''simple docstring'''


        pass






      def 			lowerCamelCase__    (    self	)	->Tuple:







        '''simple docstring'''


        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 			lowerCamelCase__    (    self	)	->Dict:







        '''simple docstring'''


        return






      def 			lowerCamelCase__    (    self	)	->Any:







        '''simple docstring'''


        _a							=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A_	)






      def 			lowerCamelCase__    (    self	)	->Optional[int]:







        '''simple docstring'''


        _a							=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*A_	)






      @unittest.skip("Swin does not use inputs_embeds"	)
      def 			lowerCamelCase__    (    self	)	->List[str]:







        '''simple docstring'''


        pass






      @unittest.skip("Swin does not support feedforward chunking"	)
      def 			lowerCamelCase__    (    self	)	->Any:







        '''simple docstring'''


        pass






      def 			lowerCamelCase__    (    self	)	->int:







        '''simple docstring'''


        _a			,	_a							=	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          _a							=	model_class(A_	)
          self.assertIsInstance(model.get_input_embeddings()     ,     (nn.Module)	)
          _a							=	model.get_output_embeddings()
          self.assertTrue(x is None or isinstance(A_     ,     nn.Linear	)	)






      def 			lowerCamelCase__    (    self	)	->Union[str, Any]:







        '''simple docstring'''


        _a			,	_a							=	self.model_tester.prepare_config_and_inputs_for_common()

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

          _a							=	["pixel_values"]
          self.assertListEqual(arg_names[:1]     ,     A_	)






      @unittest.skip(reason="MaskFormerSwin is only used as backbone and doesn\'t support output_attentions"	)
      def 			lowerCamelCase__    (    self	)	->Any:







        '''simple docstring'''


        pass






      @unittest.skip(reason="MaskFormerSwin is only used as an internal backbone"	)
      def 			lowerCamelCase__    (    self	)	->List[Any]:







        '''simple docstring'''


        pass






      def 			lowerCamelCase__    (    self     ,     a_     ,     a_     ,     a_     ,     a_	)	->int:







        '''simple docstring'''


        _a							=	model_class(A_	)
        model.to(A_	)
        model.eval()

        with torch.no_grad():
          _a							=	model(**self._prepare_for_class(A_     ,     A_	)	)

        _a							=	outputs.hidden_states

        _a							=	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							=	(
            config.patch_size
            if isinstance(config.patch_size     ,     collections.abc.Iterable	)
            else (config.patch_size, config.patch_size)
        )

        _a							=	(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 			lowerCamelCase__    (    self	)	->Union[str, Any]:







        '''simple docstring'''


        _a			,	_a							=	self.model_tester.prepare_config_and_inputs_for_common()

        _a							=	(
            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							=	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							=	True

          self.check_hidden_states_output(A_     ,     A_     ,     A_     ,     A_	)






      def 			lowerCamelCase__    (    self	)	->Dict:







        '''simple docstring'''


        _a			,	_a							=	self.model_tester.prepare_config_and_inputs_for_common()
        _a							=	3

        _a							=	(
            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							=	(
            config.patch_size
            if isinstance(config.patch_size     ,     collections.abc.Iterable	)
            else (config.patch_size, config.patch_size)
        )

        _a							=	image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
        _a							=	image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

        for model_class in self.all_model_classes:
          _a							=	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							=	True
          self.check_hidden_states_output(A_     ,     A_     ,     A_     ,     (padded_height, padded_width)	)






      @unittest.skip(reason="MaskFormerSwin doesn\'t have pretrained checkpoints"	)
      def 			lowerCamelCase__    (    self	)	->List[str]:







        '''simple docstring'''


        pass






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







        '''simple docstring'''


        pass






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







        '''simple docstring'''


        pass






      def 			lowerCamelCase__    (    self	)	->int:







        '''simple docstring'''


        _a			,	_a							=	self.model_tester.prepare_config_and_inputs_for_common()

        def set_nan_tensor_to_zero(a_	):
          _a							=	0
          return t

        def check_equivalence(a_     ,     a_     ,     a_     ,     a_={}	):
          with torch.no_grad():
            _a							=	model(**A_     ,     return_dict=A_     ,     **A_	)
            _a							=	model(**A_     ,     return_dict=A_     ,     **A_	).to_tuple()

            def recursive_check(a_     ,     a_	):
              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							=	model_class(A_	)
          model.to(A_	)
          model.eval()

          _a							=	self._prepare_for_class(A_     ,     A_	)
          _a							=	self._prepare_for_class(A_     ,     A_	)
          check_equivalence(A_     ,     A_     ,     A_	)

          _a							=	self._prepare_for_class(A_     ,     A_     ,     return_labels=A_	)
          _a							=	self._prepare_for_class(A_     ,     A_     ,     return_labels=A_	)
          check_equivalence(A_     ,     A_     ,     A_	)

          _a							=	self._prepare_for_class(A_     ,     A_	)
          _a							=	self._prepare_for_class(A_     ,     A_	)
          check_equivalence(A_     ,     A_     ,     A_     ,     {"output_hidden_states": True}	)

          _a							=	self._prepare_for_class(A_     ,     A_     ,     return_labels=A_	)
          _a							=	self._prepare_for_class(A_     ,     A_     ,     return_labels=A_	)
          check_equivalence(A_     ,     A_     ,     A_     ,     {"output_hidden_states": True}	)







@require_torch
class    lowercase_    (unittest.TestCase,    _UpperCAmelCase     ):
      A__	:		Tuple       			=(MaskFormerSwinBackbone,) if is_torch_available() else ()
      A__	:		int       			=MaskFormerSwinConfig






      def 			lowerCamelCase__    (    self	)	->List[str]:







        '''simple docstring'''


        _a							=	MaskFormerSwinModelTester(self	)






      def 			lowerCamelCase__    (    self	)	->List[Any]:







        '''simple docstring'''


        _a			,	_a							=	self.model_tester.prepare_config_and_inputs_for_common()
        _a							=	inputs_dict["pixel_values"].shape[0]

        for backbone_class in self.all_model_classes:
          _a							=	backbone_class(A_	)
          backbone.to(A_	)
          backbone.eval()

          _a							=	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							=	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							=	hidden_state.shape
              self.assertTrue((h_batch_size, h_n_channels)     ,     (batch_size, n_channels)	)

            # Test output_attentions=True
          if self.has_attentions:
            _a							=	backbone(**A_     ,     output_attentions=A_	)
            self.assertIsNotNone(outputs.attentions	)










"""simple docstring"""




from typing import TYPE_CHECKING

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


UpperCamelCase							       =    {
    """configuration_transfo_xl""": ["""TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TransfoXLConfig"""],
    """tokenization_transfo_xl""": ["""TransfoXLCorpus""", """TransfoXLTokenizer"""],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase							       =    [
         """TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """AdaptiveEmbedding""",
         """TransfoXLForSequenceClassification""",
         """TransfoXLLMHeadModel""",
         """TransfoXLModel""",
         """TransfoXLPreTrainedModel""",
         """load_tf_weights_in_transfo_xl""",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase							       =    [
         """TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """TFAdaptiveEmbedding""",
         """TFTransfoXLForSequenceClassification""",
         """TFTransfoXLLMHeadModel""",
         """TFTransfoXLMainLayer""",
         """TFTransfoXLModel""",
         """TFTransfoXLPreTrainedModel""",
     ]


if TYPE_CHECKING:
     from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig
     from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_transfo_xl import (
              TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
              AdaptiveEmbedding,
              TransfoXLForSequenceClassification,
              TransfoXLLMHeadModel,
              TransfoXLModel,
              TransfoXLPreTrainedModel,
              load_tf_weights_in_transfo_xl,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_transfo_xl import (
              TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFAdaptiveEmbedding,
              TFTransfoXLForSequenceClassification,
              TFTransfoXLLMHeadModel,
              TFTransfoXLMainLayer,
              TFTransfoXLModel,
              TFTransfoXLPreTrainedModel,
          )

else:
     import sys

     UpperCamelCase							       =    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


