
'''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 	lowerCAmelCase_:
  '''simple docstring'''




  def __init__(   self  ,__UpperCAmelCase  ,__UpperCAmelCase=13  ,__UpperCAmelCase=32  ,__UpperCAmelCase=2  ,__UpperCAmelCase=3  ,__UpperCAmelCase=16  ,__UpperCAmelCase=[1, 2, 1]  ,__UpperCAmelCase=[2, 2, 4]  ,__UpperCAmelCase=2  ,__UpperCAmelCase=2.0  ,__UpperCAmelCase=True  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=False  ,__UpperCAmelCase=True  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=1E-5  ,__UpperCAmelCase=True  ,__UpperCAmelCase=None  ,__UpperCAmelCase=True  ,__UpperCAmelCase=10  ,__UpperCAmelCase=8  ,)   ->		Optional[int]:
      lowerCAmelCase__			:  List[str]											= parent
      lowerCAmelCase__			:  Union[str, Any]											= batch_size
      lowerCAmelCase__			:  Any											= image_size
      lowerCAmelCase__			:  Dict											= patch_size
      lowerCAmelCase__			:  Dict											= num_channels
      lowerCAmelCase__			:  List[Any]											= embed_dim
      lowerCAmelCase__			:  str											= depths
      lowerCAmelCase__			:  Dict											= num_heads
      lowerCAmelCase__			:  str											= window_size
      lowerCAmelCase__			:  int											= mlp_ratio
      lowerCAmelCase__			:  Union[str, Any]											= qkv_bias
      lowerCAmelCase__			:  Dict											= hidden_dropout_prob
      lowerCAmelCase__			:  str											= attention_probs_dropout_prob
      lowerCAmelCase__			:  Optional[int]											= drop_path_rate
      lowerCAmelCase__			:  List[str]											= hidden_act
      lowerCAmelCase__			:  Optional[int]											= use_absolute_embeddings
      lowerCAmelCase__			:  Any											= patch_norm
      lowerCAmelCase__			:  Union[str, Any]											= layer_norm_eps
      lowerCAmelCase__			:  Optional[int]											= initializer_range
      lowerCAmelCase__			:  Tuple											= is_training
      lowerCAmelCase__			:  Any											= scope
      lowerCAmelCase__			:  Optional[Any]											= use_labels
      lowerCAmelCase__			:  Optional[int]											= type_sequence_label_size
      lowerCAmelCase__			:  int											= encoder_stride




  def 							UpperCAmelCase_     (   self				)   ->		List[str]:
      lowerCAmelCase__			:  Tuple											= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

      lowerCAmelCase__			:  Optional[int]											= self.get_config()

      return config, pixel_values, labels




  def 							UpperCAmelCase_     (   self				)   ->		Dict:
      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 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		List[Any]:
      lowerCAmelCase__			:  Tuple											= SwinvaModel(config=__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()
      lowerCAmelCase__			:  Union[str, Any]											= model(__UpperCAmelCase				)

      lowerCAmelCase__			:  Tuple											= ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths				) - 1))
      lowerCAmelCase__			:  List[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 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		Tuple:
      lowerCAmelCase__			:  Any											= SwinvaForMaskedImageModeling(config=__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()
      lowerCAmelCase__			:  List[Any]											= model(__UpperCAmelCase				)
      self.parent.assertEqual(
          result.logits.shape  ,(self.batch_size, self.num_channels, self.image_size, self.image_size)				)

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

      lowerCAmelCase__			:  int											= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
      lowerCAmelCase__			:  str											= model(__UpperCAmelCase				)
      self.parent.assertEqual(result.logits.shape  ,(self.batch_size, 1, self.image_size, self.image_size)				)




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		Dict:
      lowerCAmelCase__			:  str											= self.type_sequence_label_size
      lowerCAmelCase__			:  str											= SwinvaForImageClassification(__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()
      lowerCAmelCase__			:  Any											= model(__UpperCAmelCase  ,labels=__UpperCAmelCase				)
      self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.type_sequence_label_size)				)






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







@require_torch
class 	lowerCAmelCase_(    __SCREAMING_SNAKE_CASE						,			__SCREAMING_SNAKE_CASE						,			unittest.TestCase					):
  '''simple docstring'''




  __lowercase							:				List[str]           =				(
      (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
  )
  __lowercase							:				List[str]           =				(
      {"feature-extraction": SwinvaModel, "image-classification": SwinvaForImageClassification}
      if is_torch_available()
      else {}
  )

  __lowercase							:				Dict           =				False
  __lowercase							:				Optional[Any]           =				False
  __lowercase							:				Union[str, Any]           =				False
  __lowercase							:				Optional[Any]           =				False




  def 							UpperCAmelCase_     (   self				)   ->		Tuple:
      lowerCAmelCase__			:  List[str]											= SwinvaModelTester(self				)
      lowerCAmelCase__			:  Any											= ConfigTester(self  ,config_class=__UpperCAmelCase  ,embed_dim=37				)




  def 							UpperCAmelCase_     (   self				)   ->		Any:
      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				)   ->		List[str]:
      lowerCAmelCase__			:  Optional[int]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__UpperCAmelCase				)




  @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""				)
  def 							UpperCAmelCase_     (   self				)   ->		Optional[Any]:
      pass




  @unittest.skip(reason="""Swinv2 does not use inputs_embeds"""				)
  def 							UpperCAmelCase_     (   self				)   ->		Dict:
      pass




  def 							UpperCAmelCase_     (   self				)   ->		Optional[Any]:
      lowerCAmelCase__			:  int											= 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()  ,(nn.Module)				)
          lowerCAmelCase__			:  List[str]											= model.get_output_embeddings()
          self.assertTrue(x is None or isinstance(__UpperCAmelCase  ,nn.Linear				)				)




  def 							UpperCAmelCase_     (   self				)   ->		Any:
      lowerCAmelCase__			:  List[Any]											= self.model_tester.prepare_config_and_inputs_for_common()

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

          lowerCAmelCase__			:  Tuple											= ["""pixel_values"""]
          self.assertListEqual(arg_names[:1]  ,__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		int:
      lowerCAmelCase__			:  Any											= self.model_tester.prepare_config_and_inputs_for_common()
      lowerCAmelCase__			:  Optional[Any]											= True

      for model_class in self.all_model_classes:
          lowerCAmelCase__			:  Union[str, Any]											= True
          lowerCAmelCase__			:  Optional[Any]											= False
          lowerCAmelCase__			:  Optional[int]											= True
          lowerCAmelCase__			:  int											= model_class(__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          with torch.no_grad():
              lowerCAmelCase__			:  List[Any]											= model(**self._prepare_for_class(__UpperCAmelCase  ,__UpperCAmelCase				)				)
          lowerCAmelCase__			:  str											= outputs.attentions
          lowerCAmelCase__			:  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__			:  int											= config.window_size**2
          lowerCAmelCase__			:  Any											= model_class(__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          with torch.no_grad():
              lowerCAmelCase__			:  int											= model(**self._prepare_for_class(__UpperCAmelCase  ,__UpperCAmelCase				)				)
          lowerCAmelCase__			:  Dict											= 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__			:  Dict											= len(__UpperCAmelCase				)

          # Check attention is always last and order is fine
          lowerCAmelCase__			:  Any											= True
          lowerCAmelCase__			:  Any											= True
          lowerCAmelCase__			:  Optional[int]											= model_class(__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          with torch.no_grad():
              lowerCAmelCase__			:  List[str]											= model(**self._prepare_for_class(__UpperCAmelCase  ,__UpperCAmelCase				)				)

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

          lowerCAmelCase__			:  Tuple											= 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 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		List[Any]:
      lowerCAmelCase__			:  Optional[int]											= model_class(__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()

      with torch.no_grad():
          lowerCAmelCase__			:  Optional[Any]											= model(**self._prepare_for_class(__UpperCAmelCase  ,__UpperCAmelCase				)				)

      lowerCAmelCase__			:  List[Any]											= outputs.hidden_states

      lowerCAmelCase__			:  List[Any]											= getattr(
          self.model_tester  ,"""expected_num_hidden_layers"""  ,len(self.model_tester.depths				) + 1				)
      self.assertEqual(len(__UpperCAmelCase				)  ,__UpperCAmelCase				)

      # Swinv2 has a different seq_length
      lowerCAmelCase__			:  List[str]											= (
          config.patch_size
          if isinstance(config.patch_size  ,collections.abc.Iterable				)
          else (config.patch_size, config.patch_size)
      )

      lowerCAmelCase__			:  Union[str, Any]											= (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__			:  int											= outputs.reshaped_hidden_states
      self.assertEqual(len(__UpperCAmelCase				)  ,__UpperCAmelCase				)

      lowerCAmelCase__			:  str											= reshaped_hidden_states[0].shape
      lowerCAmelCase__			:  Any											= (
          reshaped_hidden_states[0].view(__UpperCAmelCase  ,__UpperCAmelCase  ,height * width				).permute(0  ,2  ,1				)
      )
      self.assertListEqual(
          list(reshaped_hidden_states.shape[-2:]				)  ,[num_patches, self.model_tester.embed_dim]  ,)




  def 							UpperCAmelCase_     (   self				)   ->		str:
      lowerCAmelCase__			:  Union[str, Any]											= self.model_tester.prepare_config_and_inputs_for_common()

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

      for model_class in self.all_model_classes:
          lowerCAmelCase__			:  Union[str, Any]											= 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__			:  Union[str, Any]											= True

          self.check_hidden_states_output(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		List[str]:
      lowerCAmelCase__			:  Any											= self.model_tester.prepare_config_and_inputs_for_common()
      lowerCAmelCase__			:  Tuple											= 3

      lowerCAmelCase__			:  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)
      )
      lowerCAmelCase__			:  List[str]											= (
          config.patch_size
          if isinstance(config.patch_size  ,collections.abc.Iterable				)
          else (config.patch_size, config.patch_size)
      )

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

      for model_class in self.all_model_classes:
          lowerCAmelCase__			:  int											= 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__			:  Tuple											= True
          self.check_hidden_states_output(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,(padded_height, padded_width)				)




  def 							UpperCAmelCase_     (   self				)   ->		List[str]:
      lowerCAmelCase__			:  Optional[Any]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		str:
      lowerCAmelCase__			:  str											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase				)




  @slow
  def 							UpperCAmelCase_     (   self				)   ->		Optional[Any]:
      for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase__			:  Dict											= SwinvaModel.from_pretrained(__UpperCAmelCase				)
          self.assertIsNotNone(__UpperCAmelCase				)






  def 							UpperCAmelCase_     (   self				)   ->		Any:
      lowerCAmelCase__			:  Dict											= self.model_tester.prepare_config_and_inputs_for_common()

      lowerCAmelCase__			:  Tuple											= _config_zero_init(__UpperCAmelCase				)
      for model_class in self.all_model_classes:
          lowerCAmelCase__			:  List[Any]											= model_class(config=__UpperCAmelCase				)
          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 	lowerCAmelCase_(    unittest.TestCase					):
  '''simple docstring'''




  @cached_property
  def 							UpperCAmelCase_     (   self				)   ->		int:
      return (
          AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""				)
          if is_vision_available()
          else None
      )






  @slow
  def 							UpperCAmelCase_     (   self				)   ->		Tuple:
      lowerCAmelCase__			:  Tuple											= SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""				).to(
          __UpperCAmelCase				)
      lowerCAmelCase__			:  Tuple											= self.default_image_processor

      lowerCAmelCase__			:  Union[str, Any]											= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""				)
      lowerCAmelCase__			:  Any											= image_processor(images=__UpperCAmelCase  ,return_tensors="""pt"""				).to(__UpperCAmelCase				)

      # forward pass
      with torch.no_grad():
          lowerCAmelCase__			:  Optional[int]											= model(**__UpperCAmelCase				)

      # verify the logits
      lowerCAmelCase__			:  int											= torch.Size((1, 1000)				)
      self.assertEqual(outputs.logits.shape  ,__UpperCAmelCase				)
      lowerCAmelCase__			:  Union[str, Any]											= torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6]				).to(__UpperCAmelCase				)
      self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,__UpperCAmelCase  ,atol=1E-4				)				)







'''simple docstring'''





import copy
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Optional, Union






@dataclass
class 	lowerCAmelCase_:
  '''simple docstring'''




  __lowercase							:				Optional[Union[str, Path]]           =				None
  __lowercase							:				bool           =				False
  __lowercase							:				bool           =				False
  __lowercase							:				bool           =				False
  __lowercase							:				Optional[Dict]           =				None
  __lowercase							:				Optional[str]           =				None
  __lowercase							:				bool           =				False
  __lowercase							:				bool           =				False
  __lowercase							:				bool           =				False
  __lowercase							:				bool           =				True
  __lowercase							:				Optional[int]           =				None
  __lowercase							:				int           =				1
  __lowercase							:				Optional[Union[str, bool]]           =				None
  __lowercase							:				bool           =				False
  __lowercase							:				Optional[Dict]           =				None
  __lowercase							:				Optional[str]           =				None




  def 							UpperCAmelCase_     (   self				)   ->		"DownloadConfig":
      return self.__class__(**{k: copy.deepcopy(__UpperCAmelCase				) for k, v in self.__dict__.items()}				)


