from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers.models.swin.modeling_tf_swin import (
        TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFSwinForImageClassification,
        TFSwinForMaskedImageModeling,
        TFSwinModel,
    )


if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor




class 						__snake_case						:





      '''simple docstring'''






      def __init__(       self  :					Union[str, Any]     ,	_UpperCamelCase  :					List[Any]     ,	_UpperCamelCase  :					Optional[int]=13     ,	_UpperCamelCase  :					Optional[Any]=32     ,	_UpperCamelCase  :					Any=2     ,	_UpperCamelCase  :					List[Any]=3     ,	_UpperCamelCase  :					Optional[int]=16     ,	_UpperCamelCase  :					List[Any]=[1, 2, 1]     ,	_UpperCamelCase  :					Any=[2, 2, 4]     ,	_UpperCamelCase  :					str=2     ,	_UpperCamelCase  :					Optional[int]=2.0     ,	_UpperCamelCase  :					Optional[int]=True     ,	_UpperCamelCase  :					Any=0.0     ,	_UpperCamelCase  :					Tuple=0.0     ,	_UpperCamelCase  :					Optional[Any]=0.1     ,	_UpperCamelCase  :					List[Any]="gelu"     ,	_UpperCamelCase  :					List[Any]=False     ,	_UpperCamelCase  :					Optional[int]=True     ,	_UpperCamelCase  :					str=0.0_2     ,	_UpperCamelCase  :					str=1E-5     ,	_UpperCamelCase  :					int=True     ,	_UpperCamelCase  :					Tuple=None     ,	_UpperCamelCase  :					Optional[Any]=True     ,	_UpperCamelCase  :					Optional[int]=10     ,	_UpperCamelCase  :					Dict=8     ,	)					->None:


          """simple docstring"""


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



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Tuple)					->str:


          """simple docstring"""


          _lowerCamelCase  :		Optional[Any]			    =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

          _lowerCamelCase  :		Any			    =    self.get_config()

          return config, pixel_values, labels



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


          """simple docstring"""


          return SwinConfig(
              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     ,	)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Dict     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					List[Any]     ,	_UpperCamelCase  :					Any)					->Optional[Any]:


          """simple docstring"""


          _lowerCamelCase  :		Any			    =    TFSwinModel(config=_UpperCamelCase)
          _lowerCamelCase  :		str			    =    model(_UpperCamelCase)

          _lowerCamelCase  :		Union[str, Any]			    =    ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1))
          _lowerCamelCase  :		Tuple			    =    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  :					List[Any]     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					Any     ,	_UpperCamelCase  :					str)					->Optional[int]:


          """simple docstring"""


          _lowerCamelCase  :		Dict			    =    TFSwinForMaskedImageModeling(config=_UpperCamelCase)
          _lowerCamelCase  :		str			    =    model(_UpperCamelCase)
          self.parent.assertEqual(
              result.logits.shape     ,	(self.batch_size, self.num_channels, self.image_size, self.image_size))

          # test greyscale images
          _lowerCamelCase  :		List[str]			    =    1
          _lowerCamelCase  :		Union[str, Any]			    =    TFSwinForMaskedImageModeling(_UpperCamelCase)

          _lowerCamelCase  :		Dict			    =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          _lowerCamelCase  :		Optional[int]			    =    model(_UpperCamelCase)
          self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, 1, self.image_size, self.image_size))



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Optional[Any]     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					List[Any]     ,	_UpperCamelCase  :					List[str])					->List[Any]:


          """simple docstring"""


          _lowerCamelCase  :		List[Any]			    =    self.type_sequence_label_size
          _lowerCamelCase  :		List[Any]			    =    TFSwinForImageClassification(_UpperCamelCase)
          _lowerCamelCase  :		int			    =    model(_UpperCamelCase     ,	labels=_UpperCamelCase)
          self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.type_sequence_label_size))

          # test greyscale images
          _lowerCamelCase  :		str			    =    1
          _lowerCamelCase  :		Dict			    =    TFSwinForImageClassification(_UpperCamelCase)
          _lowerCamelCase  :		Union[str, Any]			    =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          _lowerCamelCase  :		Any			    =    model(_UpperCamelCase)
          self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.type_sequence_label_size))




      def 						_SCREAMING_SNAKE_CASE     (       self  :					Tuple)					->List[str]:


          """simple docstring"""


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





@require_tf
class 						__snake_case						(			__lowerCAmelCase			,	__lowerCAmelCase			,	unittest.TestCase		):





      '''simple docstring'''






      _snake_case     		=							(
          (
              TFSwinModel,
              TFSwinForImageClassification,
              TFSwinForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      _snake_case     		=							(
          {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
          if is_tf_available()
          else {}
      )

      _snake_case     		=							False
      _snake_case     		=							False
      _snake_case     		=							False
      _snake_case     		=							False



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


          """simple docstring"""


          _lowerCamelCase  :		Dict			    =    TFSwinModelTester(self)
          _lowerCamelCase  :		Any			    =    ConfigTester(self     ,	config_class=_UpperCamelCase     ,	embed_dim=37)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Tuple)					->str:


          """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  :					Any)					->str:


          """simple docstring"""


          return



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


          """simple docstring"""


          _lowerCamelCase  :		Union[str, Any]			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_UpperCamelCase)



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


          """simple docstring"""


          _lowerCamelCase  :		Any			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCamelCase)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Tuple)					->str:


          """simple docstring"""


          _lowerCamelCase  :		Dict			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase)



      @unittest.skip(reason="""Swin does not use inputs_embeds""")
      def 						_SCREAMING_SNAKE_CASE     (       self  :					Union[str, Any])					->str:


          """simple docstring"""


          pass



      def 						_SCREAMING_SNAKE_CASE     (       self  :					str)					->str:


          """simple docstring"""


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

          for model_class in self.all_model_classes:
              _lowerCamelCase  :		Union[str, Any]			    =    model_class(_UpperCamelCase)
              self.assertIsInstance(model.get_input_embeddings()     ,	tf.keras.layers.Layer)
              _lowerCamelCase  :		Dict			    =    model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(_UpperCamelCase     ,	tf.keras.layers.Dense))



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


          """simple docstring"""


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

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

              _lowerCamelCase  :		Dict			    =    ["""pixel_values"""]
              self.assertListEqual(arg_names[:1]     ,	_UpperCamelCase)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Dict)					->List[Any]:


          """simple docstring"""


          _lowerCamelCase    ,				_lowerCamelCase  :		List[Any]			    =    self.model_tester.prepare_config_and_inputs_for_common()
          _lowerCamelCase  :		Optional[Any]			    =    True

          for model_class in self.all_model_classes:
              _lowerCamelCase  :		List[str]			    =    True
              _lowerCamelCase  :		Dict			    =    False
              _lowerCamelCase  :		Union[str, Any]			    =    True
              _lowerCamelCase  :		Tuple			    =    model_class(_UpperCamelCase)
              _lowerCamelCase  :		Dict			    =    model(**self._prepare_for_class(_UpperCamelCase     ,	_UpperCamelCase))
              _lowerCamelCase  :		Dict			    =    outputs.attentions
              _lowerCamelCase  :		Optional[Any]			    =    len(self.model_tester.depths)
              self.assertEqual(len(_UpperCamelCase)     ,	_UpperCamelCase)

              # check that output_attentions also work using config
              del inputs_dict["output_attentions"]
              _lowerCamelCase  :		Dict			    =    True
              _lowerCamelCase  :		Optional[Any]			    =    config.window_size**2
              _lowerCamelCase  :		Dict			    =    model_class(_UpperCamelCase)
              _lowerCamelCase  :		Optional[Any]			    =    model(**self._prepare_for_class(_UpperCamelCase     ,	_UpperCamelCase))
              _lowerCamelCase  :		Any			    =    outputs.attentions
              self.assertEqual(len(_UpperCamelCase)     ,	_UpperCamelCase)

              self.assertListEqual(
                  list(attentions[0].shape[-3:])     ,	[self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,	)
              _lowerCamelCase  :		str			    =    len(_UpperCamelCase)

              # Check attention is always last and order is fine
              _lowerCamelCase  :		Any			    =    True
              _lowerCamelCase  :		Optional[int]			    =    True
              _lowerCamelCase  :		Any			    =    model_class(_UpperCamelCase)
              _lowerCamelCase  :		Any			    =    model(**self._prepare_for_class(_UpperCamelCase     ,	_UpperCamelCase))

              if hasattr(self.model_tester     ,	"""num_hidden_states_types"""):
                  _lowerCamelCase  :		int			    =    self.model_tester.num_hidden_states_types
              else:
                  # also another +1 for reshaped_hidden_states
                  _lowerCamelCase  :		Tuple			    =    2
              self.assertEqual(out_len + added_hidden_states     ,	len(_UpperCamelCase))

              _lowerCamelCase  :		Optional[Any]			    =    outputs.attentions

              self.assertEqual(len(_UpperCamelCase)     ,	_UpperCamelCase)

              self.assertListEqual(
                  list(self_attentions[0].shape[-3:])     ,	[self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,	)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					List[Any]     ,	_UpperCamelCase  :					List[str]     ,	_UpperCamelCase  :					Optional[Any]     ,	_UpperCamelCase  :					Optional[int]     ,	_UpperCamelCase  :					int)					->Tuple:


          """simple docstring"""


          _lowerCamelCase  :		List[Any]			    =    model_class(_UpperCamelCase)
          _lowerCamelCase  :		int			    =    model(**self._prepare_for_class(_UpperCamelCase     ,	_UpperCamelCase))
          _lowerCamelCase  :		List[str]			    =    outputs.hidden_states

          _lowerCamelCase  :		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
          _lowerCamelCase  :		Optional[Any]			    =    to_atuple(config.patch_size)

          _lowerCamelCase  :		List[str]			    =    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

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

          _lowerCamelCase  :		List[Any]			    =    outputs.reshaped_hidden_states
          self.assertEqual(len(_UpperCamelCase)     ,	_UpperCamelCase)

          _lowerCamelCase    ,				_lowerCamelCase    ,				_lowerCamelCase    ,				_lowerCamelCase  :		str			    =    reshaped_hidden_states[0].shape

          _lowerCamelCase  :		Dict			    =    tf.reshape(reshaped_hidden_states[0]     ,	(batch_size, num_channels, height * width))
          _lowerCamelCase  :		int			    =    tf.transpose(_UpperCamelCase     ,	(0, 2, 1))

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



      def 						_SCREAMING_SNAKE_CASE     (       self  :					str)					->Optional[int]:


          """simple docstring"""


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

          _lowerCamelCase  :		int			    =    to_atuple(self.model_tester.image_size)

          for model_class in self.all_model_classes:
              _lowerCamelCase  :		Dict			    =    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"]
              _lowerCamelCase  :		Optional[Any]			    =    True

              self.check_hidden_states_output(_UpperCamelCase     ,	_UpperCamelCase     ,	_UpperCamelCase     ,	_UpperCamelCase)



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


          """simple docstring"""


          _lowerCamelCase    ,				_lowerCamelCase  :		str			    =    self.model_tester.prepare_config_and_inputs_for_common()
          _lowerCamelCase  :		Tuple			    =    3

          _lowerCamelCase  :		Dict			    =    to_atuple(self.model_tester.image_size)
          _lowerCamelCase  :		List[str]			    =    to_atuple(config.patch_size)

          _lowerCamelCase  :		Tuple			    =    image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
          _lowerCamelCase  :		int			    =    image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

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




      @slow
      def 						_SCREAMING_SNAKE_CASE     (       self  :					Dict)					->Union[str, Any]:


          """simple docstring"""


          for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowerCamelCase  :		Optional[int]			    =    TFSwinModel.from_pretrained(_UpperCamelCase)
              self.assertIsNotNone(_UpperCamelCase)





@require_vision
@require_tf
class 						__snake_case						(			unittest.TestCase		):





      '''simple docstring'''






      @cached_property
      def 						_SCREAMING_SNAKE_CASE     (       self  :					Dict)					->List[Any]:


          """simple docstring"""


          return (
              AutoImageProcessor.from_pretrained("""microsoft/swin-tiny-patch4-window7-224""")
              if is_vision_available()
              else None
          )




      @slow
      def 						_SCREAMING_SNAKE_CASE     (       self  :					Any)					->int:


          """simple docstring"""


          _lowerCamelCase  :		Optional[int]			    =    TFSwinForImageClassification.from_pretrained("""microsoft/swin-tiny-patch4-window7-224""")
          _lowerCamelCase  :		List[str]			    =    self.default_image_processor

          _lowerCamelCase  :		Any			    =    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
          _lowerCamelCase  :		Optional[int]			    =    image_processor(images=_UpperCamelCase     ,	return_tensors="""tf""")

          # forward pass
          _lowerCamelCase  :		Dict			    =    model(_UpperCamelCase)

          # verify the logits
          _lowerCamelCase  :		int			    =    tf.TensorShape((1, 1000))
          self.assertEqual(outputs.logits.shape     ,	_UpperCamelCase)
          _lowerCamelCase  :		Union[str, Any]			    =    tf.constant([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1])
          self.assertTrue(np.allclose(outputs.logits[0, :3]     ,	_UpperCamelCase     ,	atol=1E-4))





import torch

from accelerate import Accelerator, DistributedType




class 						__snake_case						:





      '''simple docstring'''






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


          """simple docstring"""


          if self.enabled:
              _lowerCamelCase  :		str			    =    self.model.no_sync()
              self.model_sync_obj.__enter__()

          return self



      def __exit__(       self  :					List[str]     ,	_UpperCamelCase  :					Any     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					int)					->Optional[Any]:


          """simple docstring"""


          if self.enabled:
              # Average all models on exit
              self._sync_and_avg_model_params()
              self.model_sync_obj.__exit__(_UpperCamelCase     ,	_UpperCamelCase     ,	_UpperCamelCase)



      def __init__(       self  :					Union[str, Any]     ,	_UpperCamelCase  :					Accelerator     ,	_UpperCamelCase  :					torch.nn.Module     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					bool = True)					->int:


          """simple docstring"""


          if accelerator.distributed_type not in [
              DistributedType.NO,
              DistributedType.MULTI_CPU,
              DistributedType.MULTI_GPU,
          ]:
              raise NotImplementedError("""LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)""")
          _lowerCamelCase  :		str			    =    enabled and accelerator.distributed_type != DistributedType.NO
          _lowerCamelCase  :		Optional[int]			    =    0
          if self.enabled:
              _lowerCamelCase  :		Union[str, Any]			    =    accelerator
              _lowerCamelCase  :		int			    =    model
              _lowerCamelCase  :		int			    =    local_sgd_steps



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


          """simple docstring"""


          self.num_steps += 1
          if not self.enabled:
              return

          if self.num_steps % self.local_sgd_steps == 0:
              self._sync_and_avg_model_params()




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


          """simple docstring"""


          self.accelerator.wait_for_everyone()
          with self.accelerator.autocast():
              for param in self.model.parameters():
                  _lowerCamelCase  :		Union[str, Any]			    =    self.accelerator.reduce(param.data     ,	reduction="""mean""")




