





"""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   __SCREAMING_SNAKE_CASE     :







  '''simple docstring'''


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

  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					List[str]:
        _UpperCamelCase   :						Union[str, Any]    =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        _UpperCamelCase   :						List[Any]    =							None
        if self.use_labels:
              _UpperCamelCase   :						Union[str, Any]    =							ids_tensor([self.batch_size]       , self.type_sequence_label_size						)

        _UpperCamelCase   :						Dict    =							self.get_config()

        return config, pixel_values, labels

  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]						)		->					List[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  __SCREAMING_SNAKE_CASE					(		self :							Any       , __a :							Optional[Any]       , __a :							Tuple       , __a :							int						)		->					Dict:
        _UpperCamelCase   :						Union[str, Any]    =							MaskFormerSwinModel(config=__a						)
        model.to(__a						)
        model.eval()
        _UpperCamelCase   :						List[Any]    =							model(__a						)

        _UpperCamelCase   :						List[Any]    =							((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
        _UpperCamelCase   :						Optional[int]    =							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  __SCREAMING_SNAKE_CASE					(		self :							Tuple       , __a :							Tuple       , __a :							Any       , __a :							Any						)		->					List[str]:
        _UpperCamelCase   :						Optional[Any]    =							MaskFormerSwinBackbone(config=__a						)
        model.to(__a						)
        model.eval()
        _UpperCamelCase   :						List[Any]    =							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						):
              _UpperCamelCase   :						List[Any]    =							["stem"]
              _UpperCamelCase   :						List[Any]    =							MaskFormerSwinBackbone(config=__a						)


  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					Dict:
        _UpperCamelCase   :						Optional[Any]    =							self.prepare_config_and_inputs()
        _UpperCamelCase,	_UpperCamelCase,	_UpperCamelCase   :						List[str]    =							config_and_inputs
        _UpperCamelCase   :						Optional[int]    =							{"pixel_values": pixel_values}
        return config, inputs_dict





@require_torch
class   __SCREAMING_SNAKE_CASE     (    _UpperCamelCase		,	_UpperCamelCase		,	unittest.TestCase ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__				:Optional[int]						  =       (
      (
          MaskFormerSwinModel,
          MaskFormerSwinBackbone,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE__				:Dict						  =       {"feature-extraction": MaskFormerSwinModel} if is_torch_available() else {}
  SCREAMING_SNAKE_CASE__				:Optional[Any]						  =       False
  SCREAMING_SNAKE_CASE__				:int						  =       False
  SCREAMING_SNAKE_CASE__				:int						  =       False
  SCREAMING_SNAKE_CASE__				:Optional[int]						  =       False
  SCREAMING_SNAKE_CASE__				:str						  =       False

  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					List[str]:
        _UpperCamelCase   :						Optional[int]    =							MaskFormerSwinModelTester(self						)
        _UpperCamelCase   :						List[str]    =							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  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					str:
        pass

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]						)		->					List[Any]:
        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  __SCREAMING_SNAKE_CASE					(		self :							Tuple						)		->					int:
        return

  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]						)		->					Tuple:
        _UpperCamelCase   :						Any    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Tuple:
        _UpperCamelCase   :						List[Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*__a						)

  @unittest.skip("Swin does not use inputs_embeds"						)
  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					Any:
        pass

  @unittest.skip("Swin does not support feedforward chunking"						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]						)		->					int:
        pass

  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					Optional[Any]:
        _UpperCamelCase,	_UpperCamelCase   :						Optional[Any]    =							self.model_tester.prepare_config_and_inputs_for_common()

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

  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Dict:
        _UpperCamelCase,	_UpperCamelCase   :						int    =							self.model_tester.prepare_config_and_inputs_for_common()

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

              _UpperCamelCase   :						str    =							["pixel_values"]
              self.assertListEqual(arg_names[:1]       , __a						)

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

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

  def  __SCREAMING_SNAKE_CASE					(		self :							Any       , __a :							Union[str, Any]       , __a :							List[Any]       , __a :							List[Any]       , __a :							List[str]						)		->					str:
        _UpperCamelCase   :						int    =							model_class(__a						)
        model.to(__a						)
        model.eval()

        with torch.no_grad():
              _UpperCamelCase   :						int    =							model(**self._prepare_for_class(__a       , __a						)						)

        _UpperCamelCase   :						List[Any]    =							outputs.hidden_states

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

        _UpperCamelCase   :						Dict    =							(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  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					str:
        _UpperCamelCase,	_UpperCamelCase   :						Union[str, Any]    =							self.model_tester.prepare_config_and_inputs_for_common()

        _UpperCamelCase   :						List[str]    =							(
            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:
              _UpperCamelCase   :						Optional[int]    =							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"]
              _UpperCamelCase   :						Dict    =							True

              self.check_hidden_states_output(__a       , __a       , __a       , __a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Any						)		->					List[str]:
        _UpperCamelCase,	_UpperCamelCase   :						List[str]    =							self.model_tester.prepare_config_and_inputs_for_common()
        _UpperCamelCase   :						List[str]    =							3

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

        _UpperCamelCase   :						Tuple    =							image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
        _UpperCamelCase   :						List[str]    =							image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

        for model_class in self.all_model_classes:
              _UpperCamelCase   :						Optional[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"]
              _UpperCamelCase   :						List[Any]    =							True
              self.check_hidden_states_output(__a       , __a       , __a       , (padded_height, padded_width)						)

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

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

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


  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple						)		->					Tuple:
        _UpperCamelCase,	_UpperCamelCase   :						Union[str, Any]    =							self.model_tester.prepare_config_and_inputs_for_common()

        def set_nan_tensor_to_zero(__a :							Union[str, Any]						):
              _UpperCamelCase   :						Optional[int]    =							0
              return t

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

                    def recursive_check(__a :							Union[str, Any]       , __a :							Any						):
                          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:
              _UpperCamelCase   :						List[str]    =							model_class(__a						)
              model.to(__a						)
              model.eval()

              _UpperCamelCase   :						List[Any]    =							self._prepare_for_class(__a       , __a						)
              _UpperCamelCase   :						Any    =							self._prepare_for_class(__a       , __a						)
              check_equivalence(__a       , __a       , __a						)

              _UpperCamelCase   :						Any    =							self._prepare_for_class(__a       , __a       , return_labels=__a						)
              _UpperCamelCase   :						List[Any]    =							self._prepare_for_class(__a       , __a       , return_labels=__a						)
              check_equivalence(__a       , __a       , __a						)

              _UpperCamelCase   :						Tuple    =							self._prepare_for_class(__a       , __a						)
              _UpperCamelCase   :						Dict    =							self._prepare_for_class(__a       , __a						)
              check_equivalence(__a       , __a       , __a       , {"output_hidden_states": True}						)

              _UpperCamelCase   :						Tuple    =							self._prepare_for_class(__a       , __a       , return_labels=__a						)
              _UpperCamelCase   :						Optional[Any]    =							self._prepare_for_class(__a       , __a       , return_labels=__a						)
              check_equivalence(__a       , __a       , __a       , {"output_hidden_states": True}						)





@require_torch
class   __SCREAMING_SNAKE_CASE     (    unittest.TestCase		,	_UpperCamelCase ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__				:Any						  =       (MaskFormerSwinBackbone,) if is_torch_available() else ()
  SCREAMING_SNAKE_CASE__				:Tuple						  =       MaskFormerSwinConfig

  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					str:
        _UpperCamelCase   :						Optional[int]    =							MaskFormerSwinModelTester(self						)


  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple						)		->					Tuple:
        _UpperCamelCase,	_UpperCamelCase   :						Optional[int]    =							self.model_tester.prepare_config_and_inputs_for_common()
        _UpperCamelCase   :						Dict    =							inputs_dict["pixel_values"].shape[0]

        for backbone_class in self.all_model_classes:
              _UpperCamelCase   :						Union[str, Any]    =							backbone_class(__a						)
              backbone.to(__a						)
              backbone.eval()

              _UpperCamelCase   :						Optional[Any]    =							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
              _UpperCamelCase   :						List[str]    =							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)
                          _UpperCamelCase,	_UpperCamelCase,	_UpperCamelCase   :						Dict    =							hidden_state.shape
                          self.assertTrue((h_batch_size, h_n_channels)       , (batch_size, n_channels)						)

            # Test output_attentions=True
              if self.has_attentions:
                    _UpperCamelCase   :						Optional[Any]    =							backbone(**__a       , output_attentions=__a						)
                    self.assertIsNotNone(outputs.attentions						)







"""simple docstring"""


from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding



class   __SCREAMING_SNAKE_CASE     (    _UpperCamelCase ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__				:Optional[Any]						  =       ["image_processor", "tokenizer"]
  SCREAMING_SNAKE_CASE__				:Dict						  =       "AutoImageProcessor"
  SCREAMING_SNAKE_CASE__				:Optional[int]						  =       "AutoTokenizer"

  def __init__(		self :							Dict       , __a :							Tuple       , __a :							str						)		->					Optional[Any]:
        super().__init__(__a       , __a						)
        _UpperCamelCase   :						Tuple    =							self.image_processor

  def __call__(		self :							Tuple       , __a :							Tuple=None       , __a :							str=None       , __a :							Any=None       , **__a :							Optional[Any]						)		->					Union[str, Any]:

        if text is None and images is None:
              raise ValueError("You have to specify either text or images. Both cannot be none."						)

        if text is not None:
              _UpperCamelCase   :						List[str]    =							self.tokenizer(__a       , return_tensors=__a       , **__a						)

        if images is not None:
              _UpperCamelCase   :						str    =							self.image_processor(__a       , return_tensors=__a       , **__a						)

        if text is not None and images is not None:
              _UpperCamelCase   :						Optional[int]    =							image_features.pixel_values
              return encoding
        elif text is not None:
              return encoding
        else:
              return BatchEncoding(data=dict(**__a						)       , tensor_type=__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]       , *__a :							Optional[int]       , **__a :							Optional[int]						)		->					List[Any]:
        return self.tokenizer.batch_decode(*__a       , **__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							int       , *__a :							Any       , **__a :							List[Any]						)		->					Tuple:
        return self.tokenizer.decode(*__a       , **__a						)


  @property
  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Any:
        return ["input_ids", "attention_mask", "pixel_values"]
