





'''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 lowerCamelCase						:







  def __init__(		self     ,     lowercase__     ,     lowercase__=13     ,     lowercase__=32     ,     lowercase__=2     ,     lowercase__=3     ,     lowercase__=16     ,     lowercase__=[1, 2, 1]     ,     lowercase__=[2, 2, 4]     ,     lowercase__=2     ,     lowercase__=2.0     ,     lowercase__=True     ,     lowercase__=0.0     ,     lowercase__=0.0     ,     lowercase__=0.1     ,     lowercase__="gelu"     ,     lowercase__=False     ,     lowercase__=True     ,     lowercase__=0.02     ,     lowercase__=1E-5     ,     lowercase__=True     ,     lowercase__=None     ,     lowercase__=True     ,     lowercase__=10     ,     lowercase__=8     ,     lowercase__=["stage1", "stage2", "stage3"]     ,     lowercase__=[1, 2, 3]     ,     )		->						Dict:






        """simple docstring"""





        _snake_case		:	List[str]					     =     parent
        _snake_case		:	Dict					     =     batch_size
        _snake_case		:	Optional[int]					     =     image_size
        _snake_case		:	Dict					     =     patch_size
        _snake_case		:	Union[str, Any]					     =     num_channels
        _snake_case		:	str					     =     embed_dim
        _snake_case		:	Any					     =     depths
        _snake_case		:	Optional[int]					     =     num_heads
        _snake_case		:	Any					     =     window_size
        _snake_case		:	Any					     =     mlp_ratio
        _snake_case		:	List[str]					     =     qkv_bias
        _snake_case		:	Tuple					     =     hidden_dropout_prob
        _snake_case		:	Optional[Any]					     =     attention_probs_dropout_prob
        _snake_case		:	Optional[Any]					     =     drop_path_rate
        _snake_case		:	Optional[int]					     =     hidden_act
        _snake_case		:	str					     =     use_absolute_embeddings
        _snake_case		:	List[str]					     =     patch_norm
        _snake_case		:	Any					     =     layer_norm_eps
        _snake_case		:	Tuple					     =     initializer_range
        _snake_case		:	Dict					     =     is_training
        _snake_case		:	Dict					     =     scope
        _snake_case		:	int					     =     use_labels
        _snake_case		:	Union[str, Any]					     =     type_sequence_label_size
        _snake_case		:	Optional[Any]					     =     encoder_stride
        _snake_case		:	Any					     =     out_features
        _snake_case		:	Dict					     =     out_indices







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






        """simple docstring"""





        _snake_case		:	List[str]					     =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

        _snake_case		:	Dict					     =     self.get_config()

        return config, pixel_values, labels







  def 				UpperCAmelCase_				(		self		)		->						Dict:






        """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 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__		)		->						Dict:






        """simple docstring"""





        _snake_case		:	Optional[Any]					     =     MaskFormerSwinModel(config=UpperCAmelCase_		)
        model.to(UpperCAmelCase_		)
        model.eval()
        _snake_case		:	Optional[Any]					     =     model(UpperCAmelCase_		)

        _snake_case		:	str					     =     ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths		) - 1))
        _snake_case		:	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 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__		)		->						Tuple:






        """simple docstring"""





        _snake_case		:	int					     =     MaskFormerSwinBackbone(config=UpperCAmelCase_		)
        model.to(UpperCAmelCase_		)
        model.eval()
        _snake_case		:	Tuple					     =     model(UpperCAmelCase_		)

        # 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(UpperCAmelCase_		):
              _snake_case		:	Optional[Any]					     =     ['''stem''']
              _snake_case		:	str					     =     MaskFormerSwinBackbone(config=UpperCAmelCase_		)







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






        """simple docstring"""





        _snake_case		:	str					     =     self.prepare_config_and_inputs()
        _snake_case     ,							_snake_case     ,							_snake_case		:	Union[str, Any]					     =     config_and_inputs
        _snake_case		:	Optional[int]					     =     {'''pixel_values''': pixel_values}
        return config, inputs_dict





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







  def 				UpperCAmelCase_				(		self		)		->						str:






        """simple docstring"""





        _snake_case		:	List[Any]					     =     MaskFormerSwinModelTester(self		)
        _snake_case		:	Dict					     =     ConfigTester(self     ,     config_class=UpperCAmelCase_     ,     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		)		->						Any:






        """simple docstring"""





        pass







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






        """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 				UpperCAmelCase_				(		self		)		->						Tuple:






        """simple docstring"""





        return







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






        """simple docstring"""





        _snake_case		:	int					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*UpperCAmelCase_		)







  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        _snake_case		:	Optional[Any]					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*UpperCAmelCase_		)







  @unittest.skip('''Swin does not use inputs_embeds'''		)
  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        pass







  @unittest.skip('''Swin does not support feedforward chunking'''		)
  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        pass







  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        _snake_case     ,							_snake_case		:	int					     =     self.model_tester.prepare_config_and_inputs_for_common()

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







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






        """simple docstring"""





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

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

              _snake_case		:	int					     =     ['''pixel_values''']
              self.assertListEqual(arg_names[:1]     ,     UpperCAmelCase_		)







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






        """simple docstring"""





        pass







  @unittest.skip(reason='''MaskFormerSwin is only used as an internal backbone'''		)
  def 				UpperCAmelCase_				(		self		)		->						Tuple:






        """simple docstring"""





        pass







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__		)		->						Optional[int]:






        """simple docstring"""





        _snake_case		:	int					     =     model_class(UpperCAmelCase_		)
        model.to(UpperCAmelCase_		)
        model.eval()

        with torch.no_grad():
              _snake_case		:	List[str]					     =     model(**self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_		)		)

        _snake_case		:	Any					     =     outputs.hidden_states

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

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

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

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







  def 				UpperCAmelCase_				(		self		)		->						Dict:






        """simple docstring"""





        _snake_case     ,							_snake_case		:	Dict					     =     self.model_tester.prepare_config_and_inputs_for_common()

        _snake_case		:	List[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:
              _snake_case		:	Tuple					     =     True
              self.check_hidden_states_output(UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_		)

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

              self.check_hidden_states_output(UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_		)







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






        """simple docstring"""





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

        _snake_case		:	Tuple					     =     (
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size     ,     collections.abc.Iterable		)
            else (self.model_tester.image_size, self.model_tester.image_size)
        )
        _snake_case		:	Any					     =     (
            config.patch_size
            if isinstance(config.patch_size     ,     collections.abc.Iterable		)
            else (config.patch_size, config.patch_size)
        )

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

        for model_class in self.all_model_classes:
              _snake_case		:	Union[str, Any]					     =     True
              self.check_hidden_states_output(UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_     ,     (padded_height, padded_width)		)

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







  @unittest.skip(reason='''MaskFormerSwin doesn\'t have pretrained checkpoints'''		)
  def 				UpperCAmelCase_				(		self		)		->						Optional[int]:






        """simple docstring"""





        pass







  @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''		)
  def 				UpperCAmelCase_				(		self		)		->						List[str]:






        """simple docstring"""





        pass







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






        """simple docstring"""





        pass







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






        """simple docstring"""





        _snake_case     ,							_snake_case		:	int					     =     self.model_tester.prepare_config_and_inputs_for_common()

        def set_nan_tensor_to_zero(lowercase__		):
              _snake_case		:	Dict					     =     0
              return t

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

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

                    recursive_check(UpperCAmelCase_     ,     UpperCAmelCase_		)

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

              _snake_case		:	Any					     =     self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_		)
              _snake_case		:	Optional[int]					     =     self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_		)
              check_equivalence(UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_		)

              _snake_case		:	Optional[Any]					     =     self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_     ,     return_labels=UpperCAmelCase_		)
              _snake_case		:	str					     =     self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_     ,     return_labels=UpperCAmelCase_		)
              check_equivalence(UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_		)

              _snake_case		:	int					     =     self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_		)
              _snake_case		:	Optional[Any]					     =     self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_		)
              check_equivalence(UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_     ,     {'''output_hidden_states''': True}		)

              _snake_case		:	Union[str, Any]					     =     self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_     ,     return_labels=UpperCAmelCase_		)
              _snake_case		:	List[Any]					     =     self._prepare_for_class(UpperCAmelCase_     ,     UpperCAmelCase_     ,     return_labels=UpperCAmelCase_		)
              check_equivalence(UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_     ,     {'''output_hidden_states''': True}		)





@require_torch
class lowerCamelCase						(unittest.TestCase						,      snake_case__					):
  _lowercase  :	Union[str, Any]       					=				(MaskFormerSwinBackbone,) if is_torch_available() else ()
  _lowercase  :	Tuple       					=				MaskFormerSwinConfig







  def 				UpperCAmelCase_				(		self		)		->						Any:






        """simple docstring"""





        _snake_case		:	List[Any]					     =     MaskFormerSwinModelTester(self		)







  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        _snake_case     ,							_snake_case		:	Optional[Any]					     =     self.model_tester.prepare_config_and_inputs_for_common()
        _snake_case		:	Tuple					     =     inputs_dict['''pixel_values'''].shape[0]

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

              _snake_case		:	Tuple					     =     backbone(**UpperCAmelCase_		)

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

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

            # Test output_attentions=True
              if self.has_attentions:
                    _snake_case		:	Dict					     =     backbone(**UpperCAmelCase_     ,     output_attentions=UpperCAmelCase_		)
                    self.assertIsNotNone(outputs.attentions		)









'''simple docstring'''

from ...utils import is_torch_available, is_transformers_available


if is_transformers_available() and is_torch_available():
   from .pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings, VQDiffusionPipeline


