


"""simple docstring"""





def    _snake_case  (  _snake_case :	int	):

      # bit count represents no. of bits in the gray code
      if bit_count < 0:
            raise ValueError('''The given input must be positive'''	)

      # get the generated string sequence
      lowerCAmelCase    :		Optional[int]				=      gray_code_sequence_string(_snake_case	)
      #
      # convert them to integers
      for i in range(len(_snake_case	)	):
            lowerCAmelCase    :		Any				=      int(sequence[i]       ,      2	)

      return sequence


def    _snake_case  (  _snake_case :	int	):

      # The approach is a recursive one
      # Base case achieved when either n = 0 or n=1
      if bit_count == 0:
            return ["0"]

      if bit_count == 1:
            return ["0", "1"]

      lowerCAmelCase    :		int				=      1 << bit_count  # defines the length of the sequence
      # 1<< n is equivalent to 2^n

      # recursive answer will generate answer for n-1 bits
      lowerCAmelCase    :		List[Any]				=      gray_code_sequence_string(bit_count - 1	)

      lowerCAmelCase    :		int				=      []

      # append 0 to first half of the smaller sequence generated
      for i in range(seq_len // 2	):
            lowerCAmelCase    :		str				=      '''0''' + smaller_sequence[i]
            sequence.append(_snake_case	)

      # append 1 to second half ... start from the end of the list
      for i in reversed(range(seq_len // 2	)	):
            lowerCAmelCase    :		int				=      '''1''' + smaller_sequence[i]
            sequence.append(_snake_case	)

      return sequence


if __name__ == "__main__":
 import doctest

 doctest.testmod()






"""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 						snake_case_:






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






     def    lowerCamelCase__   (		self     :    Any ):
           lowerCAmelCase    :		str				=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

           lowerCAmelCase    :		Union[str, Any]				=      None
           if self.use_labels:
                 lowerCAmelCase    :		Union[str, Any]				=      ids_tensor([self.batch_size]    , self.type_sequence_label_size )

           lowerCAmelCase    :		Tuple				=      self.get_config()

           return config, pixel_values, labels






     def    lowerCamelCase__   (		self     :    List[Any] ):
           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    lowerCamelCase__   (		self     :    Union[str, Any]    , UpperCamelCase_     :    Any    , UpperCamelCase_     :    str    , UpperCamelCase_     :    Dict ):
           lowerCAmelCase    :		List[str]				=      SwinvaModel(config=UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		List[str]				=      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    lowerCamelCase__   (		self     :    Tuple    , UpperCamelCase_     :    int    , UpperCamelCase_     :    str    , UpperCamelCase_     :    Optional[int] ):
           lowerCAmelCase    :		Tuple				=      SwinvaForMaskedImageModeling(config=UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		Dict				=      model(UpperCamelCase_ )
           self.parent.assertEqual(
               result.logits.shape    , (self.batch_size, self.num_channels, self.image_size, self.image_size) )

           # test greyscale images
           lowerCAmelCase    :		List[Any]				=      1
           lowerCAmelCase    :		List[str]				=      SwinvaForMaskedImageModeling(UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()

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






     def    lowerCamelCase__   (		self     :    Union[str, Any]    , UpperCamelCase_     :    Tuple    , UpperCamelCase_     :    List[str]    , UpperCamelCase_     :    int ):
           lowerCAmelCase    :		List[str]				=      self.type_sequence_label_size
           lowerCAmelCase    :		Optional[Any]				=      SwinvaForImageClassification(UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		Optional[int]				=      model(UpperCamelCase_    , labels=UpperCamelCase_ )
           self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.type_sequence_label_size) )






     def    lowerCamelCase__   (		self     :    str ):
           lowerCAmelCase    :		Optional[int]				=      self.prepare_config_and_inputs()
           lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase    :		str				=      config_and_inputs
           lowerCAmelCase    :		Dict				=      {'''pixel_values''': pixel_values}
           return config, inputs_dict



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

     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False






     def    lowerCamelCase__   (		self     :    int ):
           lowerCAmelCase    :		Dict				=      SwinvaModelTester(self )
           lowerCAmelCase    :		List[str]				=      ConfigTester(self    , config_class=UpperCamelCase_    , embed_dim=3_7 )






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           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    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		Tuple				=      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    lowerCamelCase__   (		self     :    Dict ):
           pass






     @unittest.skip(reason='''Swinv2 does not use inputs_embeds''' )
     def    lowerCamelCase__   (		self     :    int ):
           pass






     def    lowerCamelCase__   (		self     :    List[Any] ):
           lowerCAmelCase,					lowerCAmelCase    :		Dict				=      self.model_tester.prepare_config_and_inputs_for_common()

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






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase,					lowerCAmelCase    :		Tuple				=      self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowerCAmelCase    :		int				=      ['''pixel_values''']
                 self.assertListEqual(arg_names[:1]    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Tuple ):
           lowerCAmelCase,					lowerCAmelCase    :		Dict				=      self.model_tester.prepare_config_and_inputs_for_common()
           lowerCAmelCase    :		Optional[Any]				=      True

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

                 # check that output_attentions also work using config
                 del inputs_dict["output_attentions"]
                 lowerCAmelCase    :		Any				=      True
                 lowerCAmelCase    :		Union[str, Any]				=      config.window_size**2
                 lowerCAmelCase    :		int				=      model_class(UpperCamelCase_ )
                 model.to(UpperCamelCase_ )
                 model.eval()
                 with torch.no_grad():
                       lowerCAmelCase    :		Optional[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    :		str				=      len(UpperCamelCase_ )

                 # Check attention is always last and order is fine
                 lowerCAmelCase    :		Optional[int]				=      True
                 lowerCAmelCase    :		int				=      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_ ) )

                 if hasattr(self.model_tester    , '''num_hidden_states_types''' ):
                       lowerCAmelCase    :		List[Any]				=      self.model_tester.num_hidden_states_types
                 else:
                       # also another +1 for reshaped_hidden_states
                       lowerCAmelCase    :		Union[str, Any]				=      2
                 self.assertEqual(out_len + added_hidden_states    , len(UpperCamelCase_ ) )

                 lowerCAmelCase    :		List[str]				=      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    lowerCamelCase__   (		self     :    int    , UpperCamelCase_     :    Tuple    , UpperCamelCase_     :    Dict    , UpperCamelCase_     :    List[Any]    , UpperCamelCase_     :    Optional[Any] ):
           lowerCAmelCase    :		int				=      model_class(UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()

           with torch.no_grad():
                 lowerCAmelCase    :		Union[str, Any]				=      model(**self._prepare_for_class(UpperCamelCase_    , UpperCamelCase_ ) )

           lowerCAmelCase    :		str				=      outputs.hidden_states

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

           lowerCAmelCase    :		str				=      (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    :		List[str]				=      outputs.reshaped_hidden_states
           self.assertEqual(len(UpperCamelCase_ )    , UpperCamelCase_ )

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

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

           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    :		Tuple				=      True

                 self.check_hidden_states_output(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase,					lowerCAmelCase    :		Union[str, Any]				=      self.model_tester.prepare_config_and_inputs_for_common()
           lowerCAmelCase    :		Dict				=      3

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

           lowerCAmelCase    :		List[str]				=      image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
           lowerCAmelCase    :		Tuple				=      image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

           for model_class in self.all_model_classes:
                 lowerCAmelCase    :		str				=      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    :		Optional[int]				=      True
                 self.check_hidden_states_output(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_    , (padded_height, padded_width) )






     def    lowerCamelCase__   (		self     :    int ):
           lowerCAmelCase    :		str				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    str ):
           lowerCAmelCase    :		Dict				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_ )






     @slow
     def    lowerCamelCase__   (		self     :    int ):
           for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCAmelCase    :		int				=      SwinvaModel.from_pretrained(UpperCamelCase_ )
                 self.assertIsNotNone(UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase,					lowerCAmelCase    :		int				=      self.model_tester.prepare_config_and_inputs_for_common()

           lowerCAmelCase    :		Union[str, Any]				=      _config_zero_init(UpperCamelCase_ )
           for model_class in self.all_model_classes:
                 lowerCAmelCase    :		Union[str, 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 						snake_case_(				unittest.TestCase	):






     @cached_property
     def    lowerCamelCase__   (		self     :    Dict ):
           return (
               AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' )
               if is_vision_available()
               else None
           )






     @slow
     def    lowerCamelCase__   (		self     :    Dict ):
           lowerCAmelCase    :		str				=      SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ).to(
               UpperCamelCase_ )
           lowerCAmelCase    :		List[Any]				=      self.default_image_processor

           lowerCAmelCase    :		int				=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
           lowerCAmelCase    :		Union[str, Any]				=      image_processor(images=UpperCamelCase_    , return_tensors='''pt''' ).to(UpperCamelCase_ )

           # forward pass
           with torch.no_grad():
                 lowerCAmelCase    :		Dict				=      model(**UpperCamelCase_ )

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


