




'''simple docstring'''


import collections
import inspect
import unittest

from transformers import SwinvaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
 from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor







class      SCREAMING_SNAKE_CASE    :






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


           '''simple docstring'''

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






      def 							lowercase_						( self   :  Optional[int]       ):


           '''simple docstring'''

           a_     :							int     						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

           a_     :							Union[str, Any]     						=  self.get_config()

           return config, pixel_values, labels






      def 							lowercase_						( self   :  List[Any]       ):


           '''simple docstring'''

           return SwinvaConfig(
               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 							lowercase_						( self   :  List[str]						,						lowercase__   :  Optional[Any]						,						lowercase__   :  str						,						lowercase__   :  Dict       ):


           '''simple docstring'''

           a_     :							Dict     						=  SwinvaModel(config=lowercase__       )
           model.to(lowercase__       )
           model.eval()
           a_     :							int     						=  model(lowercase__       )

           a_     :							Union[str, Any]     						=  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
           a_     :							Optional[Any]     						=  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 							lowercase_						( self   :  List[Any]						,						lowercase__   :  List[str]						,						lowercase__   :  Union[str, Any]						,						lowercase__   :  Union[str, Any]       ):


           '''simple docstring'''

           a_     :							Dict     						=  SwinvaForMaskedImageModeling(config=lowercase__       )
           model.to(lowercase__       )
           model.eval()
           a_     :							List[Any]     						=  model(lowercase__       )
           self.parent.assertEqual(
               result.logits.shape						,						(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

           # test greyscale images
           a_     :							Optional[Any]     						=  1
           a_     :							Dict     						=  SwinvaForMaskedImageModeling(lowercase__       )
           model.to(lowercase__       )
           model.eval()

           a_     :							Optional[Any]     						=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
           a_     :							Tuple     						=  model(lowercase__       )
           self.parent.assertEqual(result.logits.shape						,						(self.batch_size, 1, self.image_size, self.image_size)       )






      def 							lowercase_						( self   :  Optional[Any]						,						lowercase__   :  Any						,						lowercase__   :  List[Any]						,						lowercase__   :  Optional[Any]       ):


           '''simple docstring'''

           a_     :							Optional[Any]     						=  self.type_sequence_label_size
           a_     :							Tuple     						=  SwinvaForImageClassification(lowercase__       )
           model.to(lowercase__       )
           model.eval()
           a_     :							Union[str, Any]     						=  model(lowercase__						,						labels=lowercase__       )
           self.parent.assertEqual(result.logits.shape						,						(self.batch_size, self.type_sequence_label_size)       )






      def 							lowercase_						( self   :  Any       ):


           '''simple docstring'''

           a_     :							Dict     						=  self.prepare_config_and_inputs()
           a_						,     a_						,     a_     :							int     						=  config_and_inputs
           a_     :							Dict     						=  {"""pixel_values""": pixel_values}
           return config, inputs_dict







@require_torch
class      SCREAMING_SNAKE_CASE    (						snake_case_							,      snake_case_							,      unittest.TestCase				):
      __magic_name__  :       str   							=					(
          (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
      )
      __magic_name__  :       int   							=					(
          {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
          if is_torch_available()
          else {}
      )

      __magic_name__  :       Optional[Any]   							=					False
      __magic_name__  :       List[Any]   							=					False
      __magic_name__  :       Optional[int]   							=					False
      __magic_name__  :       Dict   							=					False






      def 							lowercase_						( self   :  str       ):


           '''simple docstring'''

           a_     :							List[str]     						=  SwinvaModelTester(self       )
           a_     :							Union[str, Any]     						=  ConfigTester(self						,						config_class=lowercase__						,						embed_dim=37       )






      def 							lowercase_						( self   :  Union[str, Any]       ):


           '''simple docstring'''

           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 							lowercase_						( self   :  Union[str, Any]       ):


           '''simple docstring'''

           a_     :							Tuple     						=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase__       )






      @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""       )
      def 							lowercase_						( self   :  Any       ):


           '''simple docstring'''

           pass






      @unittest.skip(reason="""Swinv2 does not use inputs_embeds"""       )
      def 							lowercase_						( self   :  Any       ):


           '''simple docstring'''

           pass






      def 							lowercase_						( self   :  Optional[Any]       ):


           '''simple docstring'''

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

           for model_class in self.all_model_classes:
                a_     :							Optional[Any]     						=  model_class(lowercase__       )
                self.assertIsInstance(model.get_input_embeddings()						,						(nn.Module)       )
                a_     :							Optional[Any]     						=  model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(lowercase__						,						nn.Linear       )       )






      def 							lowercase_						( self   :  int       ):


           '''simple docstring'''

           a_						,     a_     :							Any     						=  self.model_tester.prepare_config_and_inputs_for_common()

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

                a_     :							List[str]     						=  ["""pixel_values"""]
                self.assertListEqual(arg_names[:1]						,						lowercase__       )






      def 							lowercase_						( self   :  str       ):


           '''simple docstring'''

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

           for model_class in self.all_model_classes:
                a_     :							Tuple     						=  True
                a_     :							Tuple     						=  False
                a_     :							Optional[Any]     						=  True
                a_     :							str     						=  model_class(lowercase__       )
                model.to(lowercase__       )
                model.eval()
                with torch.no_grad():
                     a_     :							Optional[Any]     						=  model(**self._prepare_for_class(lowercase__						,						lowercase__       )       )
                a_     :							Optional[Any]     						=  outputs.attentions
                a_     :							Dict     						=  len(self.model_tester.depths       )
                self.assertEqual(len(lowercase__       )						,						lowercase__       )

                # check that output_attentions also work using config
                del inputs_dict["output_attentions"]
                a_     :							List[Any]     						=  True
                a_     :							Union[str, Any]     						=  config.window_size**2
                a_     :							Optional[int]     						=  model_class(lowercase__       )
                model.to(lowercase__       )
                model.eval()
                with torch.no_grad():
                     a_     :							Tuple     						=  model(**self._prepare_for_class(lowercase__						,						lowercase__       )       )
                a_     :							Optional[Any]     						=  outputs.attentions
                self.assertEqual(len(lowercase__       )						,						lowercase__       )

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

                # Check attention is always last and order is fine
                a_     :							Union[str, Any]     						=  True
                a_     :							List[Any]     						=  True
                a_     :							Any     						=  model_class(lowercase__       )
                model.to(lowercase__       )
                model.eval()
                with torch.no_grad():
                     a_     :							Tuple     						=  model(**self._prepare_for_class(lowercase__						,						lowercase__       )       )

                if hasattr(self.model_tester						,						"""num_hidden_states_types"""       ):
                     a_     :							str     						=  self.model_tester.num_hidden_states_types
                else:
                     # also another +1 for reshaped_hidden_states
                     a_     :							Optional[Any]     						=  2
                self.assertEqual(out_len + added_hidden_states						,						len(lowercase__       )       )

                a_     :							List[Any]     						=  outputs.attentions

                self.assertEqual(len(lowercase__       )						,						lowercase__       )

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






      def 							lowercase_						( self   :  str						,						lowercase__   :  List[Any]						,						lowercase__   :  str						,						lowercase__   :  Optional[Any]						,						lowercase__   :  int       ):


           '''simple docstring'''

           a_     :							Union[str, Any]     						=  model_class(lowercase__       )
           model.to(lowercase__       )
           model.eval()

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

           a_     :							Union[str, Any]     						=  outputs.hidden_states

           a_     :							Tuple     						=  getattr(
               self.model_tester						,						"""expected_num_hidden_layers"""						,						len(self.model_tester.depths       ) + 1       )
           self.assertEqual(len(lowercase__       )						,						lowercase__       )

           # Swinv2 has a different seq_length
           a_     :							int     						=  (
               config.patch_size
               if isinstance(config.patch_size						,						collections.abc.Iterable       )
               else (config.patch_size, config.patch_size)
           )

           a_     :							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]						,						)

           a_     :							Any     						=  outputs.reshaped_hidden_states
           self.assertEqual(len(lowercase__       )						,						lowercase__       )

           a_						,     a_						,     a_						,     a_     :							Tuple     						=  reshaped_hidden_states[0].shape
           a_     :							int     						=  (
               reshaped_hidden_states[0].view(lowercase__						,						lowercase__						,						height * width       ).permute(0						,						2						,						1       )
           )
           self.assertListEqual(
               list(reshaped_hidden_states.shape[-2:]       )						,						[num_patches, self.model_tester.embed_dim]						,						)






      def 							lowercase_						( self   :  Union[str, Any]       ):


           '''simple docstring'''

           a_						,     a_     :							Any     						=  self.model_tester.prepare_config_and_inputs_for_common()

           a_     :							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:
                a_     :							int     						=  True
                self.check_hidden_states_output(lowercase__						,						lowercase__						,						lowercase__						,						lowercase__       )

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

                self.check_hidden_states_output(lowercase__						,						lowercase__						,						lowercase__						,						lowercase__       )






      def 							lowercase_						( self   :  Tuple       ):


           '''simple docstring'''

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

           a_     :							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)
           )
           a_     :							Tuple     						=  (
               config.patch_size
               if isinstance(config.patch_size						,						collections.abc.Iterable       )
               else (config.patch_size, config.patch_size)
           )

           a_     :							Union[str, Any]     						=  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
           a_     :							List[Any]     						=  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

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

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






      def 							lowercase_						( self   :  str       ):


           '''simple docstring'''

           a_     :							Optional[int]     						=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*lowercase__       )






      def 							lowercase_						( self   :  Any       ):


           '''simple docstring'''

           a_     :							str     						=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowercase__       )






      @slow
      def 							lowercase_						( self   :  Any       ):


           '''simple docstring'''

           for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                a_     :							List[str]     						=  SwinvaModel.from_pretrained(lowercase__       )
                self.assertIsNotNone(lowercase__       )






      def 							lowercase_						( self   :  Any       ):


           '''simple docstring'''

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

           a_     :							Union[str, Any]     						=  _config_zero_init(lowercase__       )
           for model_class in self.all_model_classes:
                a_     :							Any     						=  model_class(config=lowercase__       )
                for name, param in model.named_parameters():
                     if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
                          self.assertIn(
                              ((param.data.mean() * 1e9).round() / 1e9).item()						,						[0.0, 1.0]						,						msg=F"Parameter {name} of model {model_class} seems not properly initialized"						,						)







@require_vision
@require_torch
class      SCREAMING_SNAKE_CASE    (						unittest.TestCase				):






      @cached_property
      def 							lowercase_						( self   :  str       ):


           '''simple docstring'''

           return (
               AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""       )
               if is_vision_available()
               else None
           )






      @slow
      def 							lowercase_						( self   :  Tuple       ):


           '''simple docstring'''

           a_     :							Optional[Any]     						=  SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""       ).to(
               lowercase__       )
           a_     :							str     						=  self.default_image_processor

           a_     :							List[Any]     						=  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
           a_     :							int     						=  image_processor(images=lowercase__						,						return_tensors="""pt"""       ).to(lowercase__       )

           # forward pass
           with torch.no_grad():
                a_     :							List[Any]     						=  model(**lowercase__       )

           # verify the logits
           a_     :							Union[str, Any]     						=  torch.Size((1, 1000)       )
           self.assertEqual(outputs.logits.shape						,						lowercase__       )
           a_     :							Union[str, Any]     						=  torch.tensor([-0.3947, -0.4306, 0.0026]       ).to(lowercase__       )
           self.assertTrue(torch.allclose(outputs.logits[0, :3]						,						lowercase__						,						atol=1e-4       )       )






'''simple docstring'''


from ..utils import DummyObject, requires_backends







class      SCREAMING_SNAKE_CASE    (						metaclass=snake_case_				):
      __magic_name__  :       Dict   							=					['''transformers''', '''torch''', '''note_seq''']






      def __init__( self   :  List[str]						,						*lowercase__   :  Dict						,						**lowercase__   :  int       ):


           '''simple docstring'''

           requires_backends(self						,						["""transformers""", """torch""", """note_seq"""]       )






      @classmethod
      def 							lowercase_						( cls   :  Dict						,						*lowercase__   :  List[str]						,						**lowercase__   :  str       ):


           '''simple docstring'''

           requires_backends(cls						,						["""transformers""", """torch""", """note_seq"""]       )






      @classmethod
      def 							lowercase_						( cls   :  str						,						*lowercase__   :  Optional[int]						,						**lowercase__   :  Any       ):


           '''simple docstring'''

           requires_backends(cls						,						["""transformers""", """torch""", """note_seq"""]       )
