





'''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 __a			:





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

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							parent
      __SCREAMING_SNAKE_CASE   				=							batch_size
      __SCREAMING_SNAKE_CASE   				=							image_size
      __SCREAMING_SNAKE_CASE   				=							patch_size
      __SCREAMING_SNAKE_CASE   				=							num_channels
      __SCREAMING_SNAKE_CASE   				=							embed_dim
      __SCREAMING_SNAKE_CASE   				=							depths
      __SCREAMING_SNAKE_CASE   				=							num_heads
      __SCREAMING_SNAKE_CASE   				=							window_size
      __SCREAMING_SNAKE_CASE   				=							mlp_ratio
      __SCREAMING_SNAKE_CASE   				=							qkv_bias
      __SCREAMING_SNAKE_CASE   				=							hidden_dropout_prob
      __SCREAMING_SNAKE_CASE   				=							attention_probs_dropout_prob
      __SCREAMING_SNAKE_CASE   				=							drop_path_rate
      __SCREAMING_SNAKE_CASE   				=							hidden_act
      __SCREAMING_SNAKE_CASE   				=							use_absolute_embeddings
      __SCREAMING_SNAKE_CASE   				=							patch_norm
      __SCREAMING_SNAKE_CASE   				=							layer_norm_eps
      __SCREAMING_SNAKE_CASE   				=							initializer_range
      __SCREAMING_SNAKE_CASE   				=							is_training
      __SCREAMING_SNAKE_CASE   				=							scope
      __SCREAMING_SNAKE_CASE   				=							use_labels
      __SCREAMING_SNAKE_CASE   				=							type_sequence_label_size
      __SCREAMING_SNAKE_CASE   				=							encoder_stride





   def   UpperCAmelCase__  (       self					:      Tuple     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

      __SCREAMING_SNAKE_CASE   				=							self.get_config()

      return config, pixel_values, labels





   def   UpperCAmelCase__  (       self					:      str     ):

      '''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   UpperCAmelCase__  (       self					:      Any					,lowerCamelCase					:      Any					,lowerCamelCase					:      Dict					,lowerCamelCase					:      Any     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							SwinvaModel(config=lowerCamelCase     )
      model.to(lowerCamelCase     )
      model.eval()
      __SCREAMING_SNAKE_CASE   				=							model(lowerCamelCase     )

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

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							SwinvaForMaskedImageModeling(config=lowerCamelCase     )
      model.to(lowerCamelCase     )
      model.eval()
      __SCREAMING_SNAKE_CASE   				=							model(lowerCamelCase     )
      self.parent.assertEqual(
          result.logits.shape					,(self.batch_size, self.num_channels, self.image_size, self.image_size)     )

      # test greyscale images
      __SCREAMING_SNAKE_CASE   				=							1
      __SCREAMING_SNAKE_CASE   				=							SwinvaForMaskedImageModeling(lowerCamelCase     )
      model.to(lowerCamelCase     )
      model.eval()

      __SCREAMING_SNAKE_CASE   				=							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
      __SCREAMING_SNAKE_CASE   				=							model(lowerCamelCase     )
      self.parent.assertEqual(result.logits.shape					,(self.batch_size, 1, self.image_size, self.image_size)     )





   def   UpperCAmelCase__  (       self					:      str					,lowerCamelCase					:      Optional[int]					,lowerCamelCase					:      Union[str, Any]					,lowerCamelCase					:      str     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.type_sequence_label_size
      __SCREAMING_SNAKE_CASE   				=							SwinvaForImageClassification(lowerCamelCase     )
      model.to(lowerCamelCase     )
      model.eval()
      __SCREAMING_SNAKE_CASE   				=							model(lowerCamelCase					,labels=lowerCamelCase     )
      self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.type_sequence_label_size)     )





   def   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

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







@require_torch
class __a			( _snake_case, _snake_case, unittest.TestCase      ):
   __UpperCamelCase :			str						   =						(
       (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
   )
   __UpperCamelCase :			Any						   =						(
       {'feature-extraction': SwinvaModel, 'image-classification': SwinvaForImageClassification}
       if is_torch_available()
       else {}
   )

   __UpperCamelCase :			Dict						   =						False
   __UpperCamelCase :			Tuple						   =						False
   __UpperCamelCase :			str						   =						False
   __UpperCamelCase :			Any						   =						False





   def   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							SwinvaModelTester(self     )
      __SCREAMING_SNAKE_CASE   				=							ConfigTester(self					,config_class=lowerCamelCase					,embed_dim=37     )





   def   UpperCAmelCase__  (       self					:      Optional[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   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase     )





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

      '''simple docstring'''

      pass





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

      '''simple docstring'''

      pass





   def   UpperCAmelCase__  (       self					:      Optional[Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         __SCREAMING_SNAKE_CASE   				=							model_class(lowerCamelCase     )
         self.assertIsInstance(model.get_input_embeddings()					,(nn.Module)     )
         __SCREAMING_SNAKE_CASE   				=							model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(lowerCamelCase					,nn.Linear     )     )





   def   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs_for_common()

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

         __SCREAMING_SNAKE_CASE   				=							["""pixel_values"""]
         self.assertListEqual(arg_names[:1]					,lowerCamelCase     )





   def   UpperCAmelCase__  (       self					:      Any     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs_for_common()
      __SCREAMING_SNAKE_CASE   				=							True

      for model_class in self.all_model_classes:
         __SCREAMING_SNAKE_CASE   				=							True
         __SCREAMING_SNAKE_CASE   				=							False
         __SCREAMING_SNAKE_CASE   				=							True
         __SCREAMING_SNAKE_CASE   				=							model_class(lowerCamelCase     )
         model.to(lowerCamelCase     )
         model.eval()
         with torch.no_grad():
            __SCREAMING_SNAKE_CASE   				=							model(**self._prepare_for_class(lowerCamelCase					,lowerCamelCase     )     )
         __SCREAMING_SNAKE_CASE   				=							outputs.attentions
         __SCREAMING_SNAKE_CASE   				=							len(self.model_tester.depths     )
         self.assertEqual(len(lowerCamelCase     )					,lowerCamelCase     )

         # check that output_attentions also work using config
         del inputs_dict["output_attentions"]
         __SCREAMING_SNAKE_CASE   				=							True
         __SCREAMING_SNAKE_CASE   				=							config.window_size**2
         __SCREAMING_SNAKE_CASE   				=							model_class(lowerCamelCase     )
         model.to(lowerCamelCase     )
         model.eval()
         with torch.no_grad():
            __SCREAMING_SNAKE_CASE   				=							model(**self._prepare_for_class(lowerCamelCase					,lowerCamelCase     )     )
         __SCREAMING_SNAKE_CASE   				=							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]					,)
         __SCREAMING_SNAKE_CASE   				=							len(lowerCamelCase     )

         # Check attention is always last and order is fine
         __SCREAMING_SNAKE_CASE   				=							True
         __SCREAMING_SNAKE_CASE   				=							True
         __SCREAMING_SNAKE_CASE   				=							model_class(lowerCamelCase     )
         model.to(lowerCamelCase     )
         model.eval()
         with torch.no_grad():
            __SCREAMING_SNAKE_CASE   				=							model(**self._prepare_for_class(lowerCamelCase					,lowerCamelCase     )     )

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

         __SCREAMING_SNAKE_CASE   				=							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					:      Dict					,lowerCamelCase					:      Any					,lowerCamelCase					:      int					,lowerCamelCase					:      Optional[Any]					,lowerCamelCase					:      int     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							model_class(lowerCamelCase     )
      model.to(lowerCamelCase     )
      model.eval()

      with torch.no_grad():
         __SCREAMING_SNAKE_CASE   				=							model(**self._prepare_for_class(lowerCamelCase					,lowerCamelCase     )     )

      __SCREAMING_SNAKE_CASE   				=							outputs.hidden_states

      __SCREAMING_SNAKE_CASE   				=							getattr(
          self.model_tester					,"""expected_num_hidden_layers"""					,len(self.model_tester.depths     ) + 1     )
      self.assertEqual(len(lowerCamelCase     )					,lowerCamelCase     )

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

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

      __SCREAMING_SNAKE_CASE   				=							outputs.reshaped_hidden_states
      self.assertEqual(len(lowerCamelCase     )					,lowerCamelCase     )

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





   def   UpperCAmelCase__  (       self					:      Optional[int]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs_for_common()

      __SCREAMING_SNAKE_CASE   				=							(
          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:
         __SCREAMING_SNAKE_CASE   				=							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"]
         __SCREAMING_SNAKE_CASE   				=							True

         self.check_hidden_states_output(lowerCamelCase					,lowerCamelCase					,lowerCamelCase					,lowerCamelCase     )





   def   UpperCAmelCase__  (       self					:      Optional[int]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs_for_common()
      __SCREAMING_SNAKE_CASE   				=							3

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

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

      for model_class in self.all_model_classes:
         __SCREAMING_SNAKE_CASE   				=							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"]
         __SCREAMING_SNAKE_CASE   				=							True
         self.check_hidden_states_output(lowerCamelCase					,lowerCamelCase					,lowerCamelCase					,(padded_height, padded_width)     )





   def   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase     )





   def   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCamelCase     )





   @slow
   def   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __SCREAMING_SNAKE_CASE   				=							SwinvaModel.from_pretrained(lowerCamelCase     )
         self.assertIsNotNone(lowerCamelCase     )





   def   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs_for_common()

      __SCREAMING_SNAKE_CASE   				=							_config_zero_init(lowerCamelCase     )
      for model_class in self.all_model_classes:
         __SCREAMING_SNAKE_CASE   				=							model_class(config=lowerCamelCase     )
         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 __a			( unittest.TestCase      ):





   @cached_property
   def   UpperCAmelCase__  (       self					:      Union[str, Any]     ):

      '''simple docstring'''

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





   @slow
   def   UpperCAmelCase__  (       self					:      str     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""     ).to(
          lowerCamelCase     )
      __SCREAMING_SNAKE_CASE   				=							self.default_image_processor

      __SCREAMING_SNAKE_CASE   				=							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
      __SCREAMING_SNAKE_CASE   				=							image_processor(images=lowerCamelCase					,return_tensors="""pt"""     ).to(lowerCamelCase     )

      # forward pass
      with torch.no_grad():
         __SCREAMING_SNAKE_CASE   				=							model(**lowerCamelCase     )

      # verify the logits
      __SCREAMING_SNAKE_CASE   				=							torch.Size((1, 1000)     )
      self.assertEqual(outputs.logits.shape					,lowerCamelCase     )
      __SCREAMING_SNAKE_CASE   				=							torch.tensor([-0.3_947, -0.4_306, 0.0_026]     ).to(lowerCamelCase     )
      self.assertTrue(torch.allclose(outputs.logits[0, :3]					,lowerCamelCase					,atol=1E-4     )     )












'''simple docstring'''

import os
from math import logaa

def     __magic_name__							(				__UpperCAmelCase = "base_exp.txt"   )    ->       int:

   '''simple docstring'''







   __SCREAMING_SNAKE_CASE   				=							0
   __SCREAMING_SNAKE_CASE   				=							0
   for i, line in enumerate(open(os.path.join(os.path.dirname(__UpperCAmelCase   )				,						__UpperCAmelCase   )   )   ):
      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE   				=							list(map(__UpperCAmelCase				,						line.split(""","""   )   )   )
      if x * logaa(__UpperCAmelCase   ) > largest:
         __SCREAMING_SNAKE_CASE   				=							x * logaa(__UpperCAmelCase   )
         __SCREAMING_SNAKE_CASE   				=							i + 1
   return result


if __name__ == "__main__":
    print(solution())





