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	:							str   ,      snake_case	:							List[Any]   ,      snake_case	:							str=1_3   ,      snake_case	:							Dict=3_2   ,      snake_case	:							Union[str, Any]=2   ,      snake_case	:							List[Any]=3   ,      snake_case	:							str=1_6   ,      snake_case	:							str=[1, 2, 1]   ,      snake_case	:							Tuple=[2, 2, 4]   ,      snake_case	:							int=2   ,      snake_case	:							str=2.0   ,      snake_case	:							List[str]=True   ,      snake_case	:							Tuple=0.0   ,      snake_case	:							Union[str, Any]=0.0   ,      snake_case	:							int=0.1   ,      snake_case	:							str="gelu"   ,      snake_case	:							Union[str, Any]=False   ,      snake_case	:							Any=True   ,      snake_case	:							str=0.02   ,      snake_case	:							List[str]=1e-5   ,      snake_case	:							Optional[int]=True   ,      snake_case	:							Optional[Any]=None   ,      snake_case	:							int=True   ,      snake_case	:							List[Any]=1_0   ,      snake_case	:							Optional[Any]=8   ,      snake_case	:							List[Any]=["stage1", "stage2", "stage3"]   ,      snake_case	:							Any=[1, 2, 3]   ,      )   ->						Union[str, Any]:

  """simple docstring"""





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

 def 						SCREAMING_SNAKE_CASE__   ( self	:							str			)   ->						Optional[Any]:

  """simple docstring"""





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

  UpperCamelCase_					:						Tuple =					None
  if self.use_labels:
   UpperCamelCase_					:						str =					ids_tensor([self.batch_size]   ,      self.type_sequence_label_size			)

  UpperCamelCase_					:						int =					self.get_config()

  return config, pixel_values, labels

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

  """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 						SCREAMING_SNAKE_CASE__   ( self	:							Dict   ,      snake_case	:							Optional[Any]   ,      snake_case	:							int   ,      snake_case	:							List[str]			)   ->						Union[str, Any]:

  """simple docstring"""





  UpperCamelCase_					:						Tuple =					MaskFormerSwinModel(config=snake_case			)
  model.to(snake_case			)
  model.eval()
  UpperCamelCase_					:						str =					model(snake_case			)

  UpperCamelCase_					:						List[Any] =					((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths			) - 1))
  UpperCamelCase_					:						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 						SCREAMING_SNAKE_CASE__   ( self	:							int   ,      snake_case	:							int   ,      snake_case	:							List[Any]   ,      snake_case	:							Any			)   ->						Dict:

  """simple docstring"""





  UpperCamelCase_					:						Dict =					MaskFormerSwinBackbone(config=snake_case			)
  model.to(snake_case			)
  model.eval()
  UpperCamelCase_					:						Optional[int] =					model(snake_case			)

  # 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(snake_case			):
   UpperCamelCase_					:						Optional[int] =					['stem']
   UpperCamelCase_					:						List[str] =					MaskFormerSwinBackbone(config=snake_case			)


 def 						SCREAMING_SNAKE_CASE__   ( self	:							str			)   ->						int:

  """simple docstring"""





  UpperCamelCase_					:						Tuple =					self.prepare_config_and_inputs()
  UpperCamelCase_, UpperCamelCase_, UpperCamelCase_					:						Any =					config_and_inputs
  UpperCamelCase_					:						int =					{'pixel_values': pixel_values}
  return config, inputs_dict





@require_torch
class 		_lowercase						(					snake_case_						,      snake_case_						,      unittest.TestCase       ):
 lowercase				     =   (
     (
         MaskFormerSwinModel,
         MaskFormerSwinBackbone,
     )
     if is_torch_available()
     else ()
 )
 lowercase				     =   {'feature-extraction': MaskFormerSwinModel} if is_torch_available() else {}
 lowercase				     =   False
 lowercase				     =   False
 lowercase				     =   False
 lowercase				     =   False
 lowercase				     =   False

 def 						SCREAMING_SNAKE_CASE__   ( self	:							List[Any]			)   ->						str:

  """simple docstring"""





  UpperCamelCase_					:						int =					MaskFormerSwinModelTester(self			)
  UpperCamelCase_					:						List[Any] =					ConfigTester(self   ,      config_class=snake_case   ,      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 						SCREAMING_SNAKE_CASE__   ( self	:							Any			)   ->						Tuple:

  """simple docstring"""





  pass

 def 						SCREAMING_SNAKE_CASE__   ( self	:							List[Any]			)   ->						Optional[int]:

  """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 						SCREAMING_SNAKE_CASE__   ( self	:							List[str]			)   ->						List[str]:

  """simple docstring"""





  return

 def 						SCREAMING_SNAKE_CASE__   ( self	:							str			)   ->						Dict:

  """simple docstring"""





  UpperCamelCase_					:						Optional[Any] =					self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*snake_case			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							List[Any]			)   ->						int:

  """simple docstring"""





  UpperCamelCase_					:						Tuple =					self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_backbone(*snake_case			)

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

  """simple docstring"""





  pass

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

  """simple docstring"""





  pass

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[Any]			)   ->						List[str]:

  """simple docstring"""





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

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

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

  """simple docstring"""





  UpperCamelCase_, UpperCamelCase_					:						Optional[Any] =					self.model_tester.prepare_config_and_inputs_for_common()

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

   UpperCamelCase_					:						Optional[int] =					['pixel_values']
   self.assertListEqual(arg_names[:1]   ,      snake_case			)

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

  """simple docstring"""





  pass

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

  """simple docstring"""





  pass

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Any   ,      snake_case	:							Dict   ,      snake_case	:							List[Any]   ,      snake_case	:							Dict   ,      snake_case	:							Dict			)   ->						Tuple:

  """simple docstring"""





  UpperCamelCase_					:						Union[str, Any] =					model_class(snake_case			)
  model.to(snake_case			)
  model.eval()

  with torch.no_grad():
   UpperCamelCase_					:						List[Any] =					model(**self._prepare_for_class(snake_case   ,      snake_case			)			)

  UpperCamelCase_					:						Any =					outputs.hidden_states

  UpperCamelCase_					:						Union[str, Any] =					getattr(
      self.model_tester   ,      'expected_num_hidden_layers'   ,      len(self.model_tester.depths			) + 1			)
  self.assertEqual(len(snake_case			)   ,      snake_case			)

  # Swin has a different seq_length
  UpperCamelCase_					:						int =					(
      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	:							Optional[Any]			)   ->						Tuple:

  """simple docstring"""





  UpperCamelCase_, UpperCamelCase_					:						Tuple =					self.model_tester.prepare_config_and_inputs_for_common()

  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)
  )

  for model_class in self.all_model_classes:
   UpperCamelCase_					:						int =					True
   self.check_hidden_states_output(snake_case   ,      snake_case   ,      snake_case   ,      snake_case			)

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

   self.check_hidden_states_output(snake_case   ,      snake_case   ,      snake_case   ,      snake_case			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Tuple			)   ->						Optional[Any]:

  """simple docstring"""





  UpperCamelCase_, UpperCamelCase_					:						List[Any] =					self.model_tester.prepare_config_and_inputs_for_common()
  UpperCamelCase_					:						Any =					3

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

  UpperCamelCase_					:						List[Any] =					image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
  UpperCamelCase_					:						Optional[int] =					image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

  for model_class in self.all_model_classes:
   UpperCamelCase_					:						Any =					True
   self.check_hidden_states_output(snake_case   ,      snake_case   ,      snake_case   ,      (padded_height, padded_width)			)

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

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

  """simple docstring"""





  pass

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

  """simple docstring"""





  pass

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

  """simple docstring"""





  pass


 def 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[Any]			)   ->						str:

  """simple docstring"""





  UpperCamelCase_, UpperCamelCase_					:						Optional[Any] =					self.model_tester.prepare_config_and_inputs_for_common()

  def set_nan_tensor_to_zero(snake_case	:							int			):
   UpperCamelCase_					:						Tuple =					0
   return t

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

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

    recursive_check(snake_case   ,      snake_case			)

  for model_class in self.all_model_classes:
   UpperCamelCase_					:						Any =					model_class(snake_case			)
   model.to(snake_case			)
   model.eval()

   UpperCamelCase_					:						int =					self._prepare_for_class(snake_case   ,      snake_case			)
   UpperCamelCase_					:						int =					self._prepare_for_class(snake_case   ,      snake_case			)
   check_equivalence(snake_case   ,      snake_case   ,      snake_case			)

   UpperCamelCase_					:						List[str] =					self._prepare_for_class(snake_case   ,      snake_case   ,      return_labels=snake_case			)
   UpperCamelCase_					:						Tuple =					self._prepare_for_class(snake_case   ,      snake_case   ,      return_labels=snake_case			)
   check_equivalence(snake_case   ,      snake_case   ,      snake_case			)

   UpperCamelCase_					:						str =					self._prepare_for_class(snake_case   ,      snake_case			)
   UpperCamelCase_					:						Dict =					self._prepare_for_class(snake_case   ,      snake_case			)
   check_equivalence(snake_case   ,      snake_case   ,      snake_case   ,      {'output_hidden_states': True}			)

   UpperCamelCase_					:						int =					self._prepare_for_class(snake_case   ,      snake_case   ,      return_labels=snake_case			)
   UpperCamelCase_					:						Union[str, Any] =					self._prepare_for_class(snake_case   ,      snake_case   ,      return_labels=snake_case			)
   check_equivalence(snake_case   ,      snake_case   ,      snake_case   ,      {'output_hidden_states': True}			)





@require_torch
class 		_lowercase						(					unittest.TestCase						,      snake_case_       ):
 lowercase				     =   (MaskFormerSwinBackbone,) if is_torch_available() else ()
 lowercase				     =   MaskFormerSwinConfig

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Dict			)   ->						Tuple:

  """simple docstring"""





  UpperCamelCase_					:						int =					MaskFormerSwinModelTester(self			)


 def 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[Any]			)   ->						Optional[int]:

  """simple docstring"""





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

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

   UpperCamelCase_					:						List[Any] =					backbone(**snake_case			)

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

            # Test output_attentions=True
   if self.has_attentions:
    UpperCamelCase_					:						Tuple =					backbone(**snake_case   ,      output_attentions=snake_case			)
    self.assertIsNotNone(outputs.attentions			)




from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments





def    __lowercase     (						):
 UpperCamelCase_					:						Optional[Any] =					HfArgumentParser(lowerCamelCase	)
 UpperCamelCase_					:						Tuple =					parser.parse_args_into_dataclasses()[0]
 UpperCamelCase_					:						Dict =					TensorFlowBenchmark(args=lowerCamelCase	)
 try:
  UpperCamelCase_					:						Any =					parser.parse_args_into_dataclasses()[0]
 except ValueError as e:
  UpperCamelCase_					:						Any =					'Arg --no_{0} is no longer used, please use --no-{0} instead.'
  UpperCamelCase_					:						Optional[int] =					' '.join(str(lowerCamelCase	).split(' '	)[:-1]	)
  UpperCamelCase_					:						Any =					''
  UpperCamelCase_					:						Any =					eval(str(lowerCamelCase	).split(' '	)[-1]	)
  UpperCamelCase_					:						List[Any] =					[]
  for arg in depreciated_args:
   # arg[2:] removes '--'
   if arg[2:] in TensorFlowBenchmark.deprecated_args:
    # arg[5:] removes '--no_'
    full_error_msg += arg_error_msg.format(arg[5:]	)
   else:
    wrong_args.append(lowerCamelCase	)
  if len(lowerCamelCase	) > 0:
   UpperCamelCase_					:						List[str] =					full_error_msg + begin_error_msg + str(lowerCamelCase	)
  raise ValueError(lowerCamelCase	)
 benchmark.run()


if __name__ == "__main__":
   main()



