




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       _UpperCamelCase   :
      def __init__(      self :Optional[int]			,   lowerCamelCase :Tuple			,   lowerCamelCase :Any=13			,   lowerCamelCase :Optional[Any]=32			,   lowerCamelCase :int=2			,   lowerCamelCase :int=3			,   lowerCamelCase :str=16			,   lowerCamelCase :List[str]=[1, 2, 1]			,   lowerCamelCase :Optional[int]=[2, 2, 4]			,   lowerCamelCase :Optional[Any]=2			,   lowerCamelCase :Optional[int]=2.0			,   lowerCamelCase :Optional[Any]=True			,   lowerCamelCase :Tuple=0.0			,   lowerCamelCase :Optional[int]=0.0			,   lowerCamelCase :str=0.1			,   lowerCamelCase :Optional[int]="gelu"			,   lowerCamelCase :int=False			,   lowerCamelCase :Dict=True			,   lowerCamelCase :Tuple=0.02			,   lowerCamelCase :str=1e-5			,   lowerCamelCase :Union[str, Any]=True			,   lowerCamelCase :Any=None			,   lowerCamelCase :Tuple=True			,   lowerCamelCase :Union[str, Any]=10			,   lowerCamelCase :Optional[int]=8			,   )  ->      None:
       UpperCAmelCase__							=    parent
       UpperCAmelCase__							=    batch_size
       UpperCAmelCase__							=    image_size
       UpperCAmelCase__							=    patch_size
       UpperCAmelCase__							=    num_channels
       UpperCAmelCase__							=    embed_dim
       UpperCAmelCase__							=    depths
       UpperCAmelCase__							=    num_heads
       UpperCAmelCase__							=    window_size
       UpperCAmelCase__							=    mlp_ratio
       UpperCAmelCase__							=    qkv_bias
       UpperCAmelCase__							=    hidden_dropout_prob
       UpperCAmelCase__							=    attention_probs_dropout_prob
       UpperCAmelCase__							=    drop_path_rate
       UpperCAmelCase__							=    hidden_act
       UpperCAmelCase__							=    use_absolute_embeddings
       UpperCAmelCase__							=    patch_norm
       UpperCAmelCase__							=    layer_norm_eps
       UpperCAmelCase__							=    initializer_range
       UpperCAmelCase__							=    is_training
       UpperCAmelCase__							=    scope
       UpperCAmelCase__							=    use_labels
       UpperCAmelCase__							=    type_sequence_label_size
       UpperCAmelCase__							=    encoder_stride
      def 						UpperCAmelCase_    (      self :Any   )  ->      Tuple:
       UpperCAmelCase__							=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

       UpperCAmelCase__							=    self.get_config()

       return config, pixel_values, labels
      def 						UpperCAmelCase_    (      self :Optional[int]   )  ->      str:
       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 						UpperCAmelCase_    (      self :str			,   lowerCamelCase :List[str]			,   lowerCamelCase :Optional[int]			,   lowerCamelCase :List[str]   )  ->      Any:
       UpperCAmelCase__							=    TFSwinModel(config=__lowerCamelCase   )
       UpperCAmelCase__							=    model(__lowerCamelCase   )

       UpperCAmelCase__							=    ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths   ) - 1))
       UpperCAmelCase__							=    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 :Union[str, Any]			,   lowerCamelCase :List[str]			,   lowerCamelCase :Dict			,   lowerCamelCase :str   )  ->      str:
       UpperCAmelCase__							=    TFSwinForMaskedImageModeling(config=__lowerCamelCase   )
       UpperCAmelCase__							=    model(__lowerCamelCase   )
       self.parent.assertEqual(
           result.logits.shape			,   (self.batch_size, self.num_channels, self.image_size, self.image_size)   )

       # test greyscale images
       UpperCAmelCase__							=    1
       UpperCAmelCase__							=    TFSwinForMaskedImageModeling(__lowerCamelCase   )

       UpperCAmelCase__							=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
       UpperCAmelCase__							=    model(__lowerCamelCase   )
       self.parent.assertEqual(result.logits.shape			,   (self.batch_size, 1, self.image_size, self.image_size)   )
      def 						UpperCAmelCase_    (      self :List[Any]			,   lowerCamelCase :int			,   lowerCamelCase :int			,   lowerCamelCase :List[str]   )  ->      Optional[Any]:
       UpperCAmelCase__							=    self.type_sequence_label_size
       UpperCAmelCase__							=    TFSwinForImageClassification(__lowerCamelCase   )
       UpperCAmelCase__							=    model(__lowerCamelCase			,   labels=__lowerCamelCase   )
       self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.type_sequence_label_size)   )

       # test greyscale images
       UpperCAmelCase__							=    1
       UpperCAmelCase__							=    TFSwinForImageClassification(__lowerCamelCase   )
       UpperCAmelCase__							=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
       UpperCAmelCase__							=    model(__lowerCamelCase   )
       self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.type_sequence_label_size)   )







      def 						UpperCAmelCase_    (      self :List[str]   )  ->      int:
       UpperCAmelCase__							=    self.prepare_config_and_inputs()
       UpperCAmelCase__							=    config_and_inputs
       UpperCAmelCase__							=    {"""pixel_values""": pixel_values}
       return config, inputs_dict



@require_tf
class       _UpperCamelCase   (					__snake_case						,							__snake_case						,							unittest.TestCase		):
      UpperCAmelCase_          =		(
          (
              TFSwinModel,
              TFSwinForImageClassification,
              TFSwinForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      UpperCAmelCase_          =		(
          {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
          if is_tf_available()
          else {}
      )

      UpperCAmelCase_          =		False
      UpperCAmelCase_          =		False
      UpperCAmelCase_          =		False
      UpperCAmelCase_          =		False
      def 						UpperCAmelCase_    (      self :Union[str, Any]   )  ->      List[Any]:
       UpperCAmelCase__							=    TFSwinModelTester(self   )
       UpperCAmelCase__							=    ConfigTester(self			,   config_class=__lowerCamelCase			,   embed_dim=37   )
      def 						UpperCAmelCase_    (      self :int   )  ->      int:
       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 :Optional[Any]   )  ->      Optional[int]:
       return
      def 						UpperCAmelCase_    (      self :int   )  ->      Dict:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :Any   )  ->      Dict:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :int   )  ->      Optional[int]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase   )
      @unittest.skip(reason="Swin does not use inputs_embeds"   )
      def 						UpperCAmelCase_    (      self :Any   )  ->      Optional[int]:
       pass
      def 						UpperCAmelCase_    (      self :int   )  ->      Optional[Any]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCAmelCase__							=    model_class(__lowerCamelCase   )
        self.assertIsInstance(model.get_input_embeddings()			,   tf.keras.layers.Layer   )
        UpperCAmelCase__							=    model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(__lowerCamelCase			,   tf.keras.layers.Dense   )   )
      def 						UpperCAmelCase_    (      self :Dict   )  ->      Dict:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCAmelCase__							=    ["""pixel_values"""]
        self.assertListEqual(arg_names[:1]			,   __lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :Tuple   )  ->      Optional[Any]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase__							=    True

       for model_class in self.all_model_classes:
        UpperCAmelCase__							=    True
        UpperCAmelCase__							=    False
        UpperCAmelCase__							=    True
        UpperCAmelCase__							=    model_class(__lowerCamelCase   )
        UpperCAmelCase__							=    model(**self._prepare_for_class(__lowerCamelCase			,   __lowerCamelCase   )   )
        UpperCAmelCase__							=    outputs.attentions
        UpperCAmelCase__							=    len(self.model_tester.depths   )
        self.assertEqual(len(__lowerCamelCase   )			,   __lowerCamelCase   )

        # check that output_attentions also work using config
        del inputs_dict["output_attentions"]
        UpperCAmelCase__							=    True
        UpperCAmelCase__							=    config.window_size**2
        UpperCAmelCase__							=    model_class(__lowerCamelCase   )
        UpperCAmelCase__							=    model(**self._prepare_for_class(__lowerCamelCase			,   __lowerCamelCase   )   )
        UpperCAmelCase__							=    outputs.attentions
        self.assertEqual(len(__lowerCamelCase   )			,   __lowerCamelCase   )

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

        # Check attention is always last and order is fine
        UpperCAmelCase__							=    True
        UpperCAmelCase__							=    True
        UpperCAmelCase__							=    model_class(__lowerCamelCase   )
        UpperCAmelCase__							=    model(**self._prepare_for_class(__lowerCamelCase			,   __lowerCamelCase   )   )

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

        UpperCAmelCase__							=    outputs.attentions

        self.assertEqual(len(__lowerCamelCase   )			,   __lowerCamelCase   )

        self.assertListEqual(
            list(self_attentions[0].shape[-3:]   )			,   [self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,   )
      def 						UpperCAmelCase_    (      self :List[Any]			,   lowerCamelCase :List[Any]			,   lowerCamelCase :Tuple			,   lowerCamelCase :int			,   lowerCamelCase :Optional[int]   )  ->      List[str]:
       UpperCAmelCase__							=    model_class(__lowerCamelCase   )
       UpperCAmelCase__							=    model(**self._prepare_for_class(__lowerCamelCase			,   __lowerCamelCase   )   )
       UpperCAmelCase__							=    outputs.hidden_states

       UpperCAmelCase__							=    getattr(
           self.model_tester			,   "expected_num_hidden_layers"			,   len(self.model_tester.depths   ) + 1   )
       self.assertEqual(len(__lowerCamelCase   )			,   __lowerCamelCase   )

       # Swin has a different seq_length
       UpperCAmelCase__							=    to_atuple(config.patch_size   )

       UpperCAmelCase__							=    (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]			,   )

       UpperCAmelCase__							=    outputs.reshaped_hidden_states
       self.assertEqual(len(__lowerCamelCase   )			,   __lowerCamelCase   )

       UpperCAmelCase__							=    reshaped_hidden_states[0].shape

       UpperCAmelCase__							=    tf.reshape(reshaped_hidden_states[0]			,   (batch_size, num_channels, height * width)   )
       UpperCAmelCase__							=    tf.transpose(__lowerCamelCase			,   (0, 2, 1)   )

       self.assertListEqual(
           list(reshaped_hidden_states.shape[-2:]   )			,   [num_patches, self.model_tester.embed_dim]			,   )
      def 						UpperCAmelCase_    (      self :List[str]   )  ->      Optional[int]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs_for_common()

       UpperCAmelCase__							=    to_atuple(self.model_tester.image_size   )

       for model_class in self.all_model_classes:
        UpperCAmelCase__							=    True
        self.check_hidden_states_output(__lowerCamelCase			,   __lowerCamelCase			,   __lowerCamelCase			,   __lowerCamelCase   )

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        UpperCAmelCase__							=    True

        self.check_hidden_states_output(__lowerCamelCase			,   __lowerCamelCase			,   __lowerCamelCase			,   __lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :Optional[int]   )  ->      Any:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase__							=    3

       UpperCAmelCase__							=    to_atuple(self.model_tester.image_size   )
       UpperCAmelCase__							=    to_atuple(config.patch_size   )

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

       for model_class in self.all_model_classes:
        UpperCAmelCase__							=    True
        self.check_hidden_states_output(__lowerCamelCase			,   __lowerCamelCase			,   __lowerCamelCase			,   (padded_height, padded_width)   )

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        UpperCAmelCase__							=    True
        self.check_hidden_states_output(__lowerCamelCase			,   __lowerCamelCase			,   __lowerCamelCase			,   (padded_height, padded_width)   )







      @slow
      def 						UpperCAmelCase_    (      self :int   )  ->      Optional[int]:







       for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase__							=    TFSwinModel.from_pretrained(__lowerCamelCase   )
        self.assertIsNotNone(__lowerCamelCase   )



@require_vision
@require_tf
class       _UpperCamelCase   (					unittest.TestCase		):
      @cached_property
      def 						UpperCAmelCase_    (      self :Tuple   )  ->      List[Any]:
       return (
           AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224"   )
           if is_vision_available()
           else None
       )







      @slow
      def 						UpperCAmelCase_    (      self :int   )  ->      Optional[int]:
       UpperCAmelCase__							=    TFSwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224"   )
       UpperCAmelCase__							=    self.default_image_processor

       UpperCAmelCase__							=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
       UpperCAmelCase__							=    image_processor(images=__lowerCamelCase			,   return_tensors="tf"   )

       # forward pass
       UpperCAmelCase__							=    model(__lowerCamelCase   )

       # verify the logits
       UpperCAmelCase__							=    tf.TensorShape((1, 1000)   )
       self.assertEqual(outputs.logits.shape			,   __lowerCamelCase   )
       UpperCAmelCase__							=    tf.constant([-0.09_48, -0.64_54, -0.09_21]   )
       self.assertTrue(np.allclose(outputs.logits[0, :3]			,   __lowerCamelCase			,   atol=1e-4   )   )






def   a_					(_lowerCAmelCase  :   str       )->				int:
    snake_case:						List[str]         =   0
    for letter in data:
        snake_case:						int         =   (hash_ << 4) + ord(_lowerCAmelCase       )
        snake_case:						Optional[int]         =   hash_ & 0xf_0_0_0_0_0_0_0
        if x != 0:
            hash_ ^= x >> 24
        hash_ &= ~x
    return hash_


if __name__ == "__main__":
    import doctest

    doctest.testmod()


