



'''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  ,)    ->		List[Any]:
      lowerCAmelCase__ :  Any							      =					parent
      lowerCAmelCase__ :  Any							      =					batch_size
      lowerCAmelCase__ :  Union[str, Any]							      =					image_size
      lowerCAmelCase__ :  int							      =					patch_size
      lowerCAmelCase__ :  Dict							      =					num_channels
      lowerCAmelCase__ :  Optional[int]							      =					embed_dim
      lowerCAmelCase__ :  List[str]							      =					depths
      lowerCAmelCase__ :  Union[str, Any]							      =					num_heads
      lowerCAmelCase__ :  str							      =					window_size
      lowerCAmelCase__ :  Union[str, Any]							      =					mlp_ratio
      lowerCAmelCase__ :  Dict							      =					qkv_bias
      lowerCAmelCase__ :  Union[str, Any]							      =					hidden_dropout_prob
      lowerCAmelCase__ :  Optional[Any]							      =					attention_probs_dropout_prob
      lowerCAmelCase__ :  Tuple							      =					drop_path_rate
      lowerCAmelCase__ :  Tuple							      =					hidden_act
      lowerCAmelCase__ :  Dict							      =					use_absolute_embeddings
      lowerCAmelCase__ :  List[str]							      =					patch_norm
      lowerCAmelCase__ :  Optional[int]							      =					layer_norm_eps
      lowerCAmelCase__ :  str							      =					initializer_range
      lowerCAmelCase__ :  str							      =					is_training
      lowerCAmelCase__ :  Tuple							      =					scope
      lowerCAmelCase__ :  Optional[Any]							      =					use_labels
      lowerCAmelCase__ :  Dict							      =					type_sequence_label_size
      lowerCAmelCase__ :  Optional[Any]							      =					encoder_stride




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

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

      lowerCAmelCase__ :  List[str]							      =					self.get_config()

      return config, pixel_values, labels




  def 							UpperCAmelCase_	(    self      )    ->		int:
      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__ :  List[str]							      =					SwinvaModel(config=__UpperCAmelCase      )
      model.to(__UpperCAmelCase      )
      model.eval()
      lowerCAmelCase__ :  int							      =					model(__UpperCAmelCase      )

      lowerCAmelCase__ :  int							      =					((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 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		List[str]:
      lowerCAmelCase__ :  int							      =					SwinvaForMaskedImageModeling(config=__UpperCAmelCase      )
      model.to(__UpperCAmelCase      )
      model.eval()
      lowerCAmelCase__ :  List[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__ :  Optional[Any]							      =					1
      lowerCAmelCase__ :  int							      =					SwinvaForMaskedImageModeling(__UpperCAmelCase      )
      model.to(__UpperCAmelCase      )
      model.eval()

      lowerCAmelCase__ :  Optional[Any]							      =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
      lowerCAmelCase__ :  List[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      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  List[str]							      =					self.type_sequence_label_size
      lowerCAmelCase__ :  Dict							      =					SwinvaForImageClassification(__UpperCAmelCase      )
      model.to(__UpperCAmelCase      )
      model.eval()
      lowerCAmelCase__ :  Tuple							      =					model(__UpperCAmelCase  ,labels=__UpperCAmelCase      )
      self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.type_sequence_label_size)      )






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







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




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

  __lowercase							:      str				=      False
  __lowercase							:      List[str]				=      False
  __lowercase							:      Any				=      False
  __lowercase							:      Dict				=      False




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




  def 							UpperCAmelCase_	(    self      )    ->		Union[str, 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      )    ->		str:
      lowerCAmelCase__ :  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      )    ->		int:
      pass




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




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

      for model_class in self.all_model_classes:
          lowerCAmelCase__ :  Optional[int]							      =					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      )    ->		str:
      lowerCAmelCase__      ,   lowerCAmelCase__ :  List[Any]							      =					self.model_tester.prepare_config_and_inputs_for_common()

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

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




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

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

          # Check attention is always last and order is fine
          lowerCAmelCase__ :  Optional[int]							      =					True
          lowerCAmelCase__ :  Union[str, Any]							      =					True
          lowerCAmelCase__ :  Tuple							      =					model_class(__UpperCAmelCase      )
          model.to(__UpperCAmelCase      )
          model.eval()
          with torch.no_grad():
              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__ :  Optional[int]							      =					2
          self.assertEqual(out_len + added_hidden_states  ,len(__UpperCAmelCase      )      )

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

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

      lowerCAmelCase__ :  Optional[int]							      =					outputs.hidden_states

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

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

      lowerCAmelCase__      ,   lowerCAmelCase__      ,   lowerCAmelCase__      ,   lowerCAmelCase__ :  int							      =					reshaped_hidden_states[0].shape
      lowerCAmelCase__ :  int							      =					(
          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      )    ->		List[str]:
      lowerCAmelCase__      ,   lowerCAmelCase__ :  Optional[int]							      =					self.model_tester.prepare_config_and_inputs_for_common()

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

      for model_class in self.all_model_classes:
          lowerCAmelCase__ :  int							      =					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__ :  Tuple							      =					True

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




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

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

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

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




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




  def 							UpperCAmelCase_	(    self      )    ->		Tuple:
      lowerCAmelCase__ :  Optional[int]							      =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase      )




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






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

      lowerCAmelCase__ :  List[str]							      =					_config_zero_init(__UpperCAmelCase      )
      for model_class in self.all_model_classes:
          lowerCAmelCase__ :  Tuple							      =					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      )    ->		Any:
      return (
          AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""      )
          if is_vision_available()
          else None
      )






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

      lowerCAmelCase__ :  int							      =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
      lowerCAmelCase__ :  Tuple							      =					image_processor(images=__UpperCAmelCase  ,return_tensors="""pt"""      ).to(__UpperCAmelCase      )

      # forward pass
      with torch.no_grad():
          lowerCAmelCase__ :  Tuple							      =					model(**__UpperCAmelCase      )

      # verify the logits
      lowerCAmelCase__ :  str							      =					torch.Size((1, 1000)      )
      self.assertEqual(outputs.logits.shape  ,__UpperCAmelCase      )
      lowerCAmelCase__ :  List[str]							      =					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'''





from maths.prime_factors import prime_factors
def    _SCREAMING_SNAKE_CASE  (       UpperCamelCase			):



    """simple docstring"""





    if not isinstance(UpperCamelCase							,    UpperCamelCase			):
        lowerCAmelCase__ :  int							      =					f"""Input value of [number={number}] must be an integer"""
        raise TypeError(UpperCamelCase			)
    if number < 1:
        raise ValueError("""Input must be a positive integer"""			)
    return -1 if len(prime_factors(UpperCamelCase			)			) % 2 else 1


if __name__ == "__main__":
 import doctest

 doctest.testmod()


