


'''simple docstring'''






import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
 from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import ViTImageProcessor






class    __magic_name__				:






       def __init__(     self    ,				snake_case    ,				snake_case=1_3    ,				snake_case=3_0    ,				snake_case=2    ,				snake_case=3    ,				snake_case=True    ,				snake_case=True    ,				snake_case=3_2    ,				snake_case=5    ,				snake_case=4    ,				snake_case=3_7    ,				snake_case="gelu"    ,				snake_case=0.1    ,				snake_case=0.1    ,				snake_case=1_0    ,				snake_case=0.02    ,				snake_case=None    ,				snake_case=2    ,				)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =parent
          _UpperCAmelCase  :  str				       =batch_size
          _UpperCAmelCase  :  int				       =image_size
          _UpperCAmelCase  :  Optional[Any]				       =patch_size
          _UpperCAmelCase  :  Tuple				       =num_channels
          _UpperCAmelCase  :  Optional[Any]				       =is_training
          _UpperCAmelCase  :  Optional[int]				       =use_labels
          _UpperCAmelCase  :  List[str]				       =hidden_size
          _UpperCAmelCase  :  List[Any]				       =num_hidden_layers
          _UpperCAmelCase  :  List[Any]				       =num_attention_heads
          _UpperCAmelCase  :  Dict				       =intermediate_size
          _UpperCAmelCase  :  Any				       =hidden_act
          _UpperCAmelCase  :  Any				       =hidden_dropout_prob
          _UpperCAmelCase  :  List[str]				       =attention_probs_dropout_prob
          _UpperCAmelCase  :  Any				       =type_sequence_label_size
          _UpperCAmelCase  :  Optional[int]				       =initializer_range
          _UpperCAmelCase  :  int				       =scope
          _UpperCAmelCase  :  List[Any]				       =encoder_stride

          # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
          _UpperCAmelCase  :  int				       =(image_size // patch_size) ** 2
          _UpperCAmelCase  :  List[Any]				       =num_patches + 1






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[Any]				       =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

          _UpperCAmelCase  :  Optional[int]				       =None
          if self.use_labels:
             _UpperCAmelCase  :  Dict				       =ids_tensor([self.batch_size]    ,				self.type_sequence_label_size)

          _UpperCAmelCase  :  Any				       =self.get_config()

          return config, pixel_values, labels






       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          return ViTConfig(
              image_size=self.image_size    ,				patch_size=self.patch_size    ,				num_channels=self.num_channels    ,				hidden_size=self.hidden_size    ,				num_hidden_layers=self.num_hidden_layers    ,				num_attention_heads=self.num_attention_heads    ,				intermediate_size=self.intermediate_size    ,				hidden_act=self.hidden_act    ,				hidden_dropout_prob=self.hidden_dropout_prob    ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,				is_decoder=snake_case    ,				initializer_range=self.initializer_range    ,				encoder_stride=self.encoder_stride    ,				)






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =ViTModel(config=snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  List[Any]				       =model(snake_case)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =ViTForMaskedImageModeling(config=snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  Any				       =model(snake_case)
          self.parent.assertEqual(
              result.reconstruction.shape    ,				(self.batch_size, self.num_channels, self.image_size, self.image_size))

          # test greyscale images
          _UpperCAmelCase  :  List[str]				       =1
          _UpperCAmelCase  :  Dict				       =ViTForMaskedImageModeling(snake_case)
          model.to(snake_case)
          model.eval()

          _UpperCAmelCase  :  str				       =floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          _UpperCAmelCase  :  str				       =model(snake_case)
          self.parent.assertEqual(result.reconstruction.shape    ,				(self.batch_size, 1, self.image_size, self.image_size))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =self.type_sequence_label_size
          _UpperCAmelCase  :  Tuple				       =ViTForImageClassification(snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  Optional[int]				       =model(snake_case    ,				labels=snake_case)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.type_sequence_label_size))

          # test greyscale images
          _UpperCAmelCase  :  Union[str, Any]				       =1
          _UpperCAmelCase  :  Dict				       =ViTForImageClassification(snake_case)
          model.to(snake_case)
          model.eval()

          _UpperCAmelCase  :  Union[str, Any]				       =floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          _UpperCAmelCase  :  Any				       =model(snake_case)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.type_sequence_label_size))






       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =self.prepare_config_and_inputs()
          (
              (
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			
          )  :  List[Any]				       =config_and_inputs
          _UpperCAmelCase  :  str				       ={'pixel_values': pixel_values}
          return config, inputs_dict






@require_torch
class    __magic_name__				(						lowerCAmelCase   ,lowerCAmelCase   ,unittest.TestCase							):
       UpperCAmelCase		 =(
           (
               ViTModel,
               ViTForImageClassification,
               ViTForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       UpperCAmelCase		 =(
           {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
           if is_torch_available()
           else {}
       )
       UpperCAmelCase		 =True

       UpperCAmelCase		 =False
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False






       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =ViTModelTester(self)
          _UpperCAmelCase  :  Optional[Any]				       =ConfigTester(self    ,				config_class=snake_case    ,				has_text_modality=snake_case    ,				hidden_size=3_7)






       def      lowerCAmelCase						(     self)		->    Optional[Any]:




          '''simple docstring'''
          self.config_tester.run_common_tests()






       @unittest.skip(reason='ViT does not use inputs_embeds')
       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          pass






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase       ,			_UpperCAmelCase  :  Dict				       =self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _UpperCAmelCase  :  Union[str, Any]				       =model_class(snake_case)
             self.assertIsInstance(model.get_input_embeddings()    ,				(nn.Module))
             _UpperCAmelCase  :  Any				       =model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(snake_case    ,				nn.Linear))






       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase       ,			_UpperCAmelCase  :  Optional[Any]				       =self.model_tester.prepare_config_and_inputs_for_common()

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

             _UpperCAmelCase  :  int				       =['pixel_values']
             self.assertListEqual(arg_names[:1]    ,				snake_case)






       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case)






       def      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*snake_case)






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case)






       @slow
       def      lowerCAmelCase						(     self)		->    int:




          '''simple docstring'''
          for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase  :  Tuple				       =ViTModel.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)






def lowerCamelCase__     (		):







   '''simple docstring'''




   _UpperCAmelCase  :  Tuple				       =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
   return image






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






       @cached_property
       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') if is_vision_available() else None






       @slow
       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(snake_case)

          _UpperCAmelCase  :  Union[str, Any]				       =self.default_image_processor
          _UpperCAmelCase  :  int				       =prepare_img()
          _UpperCAmelCase  :  Union[str, Any]				       =image_processor(images=snake_case    ,				return_tensors='pt').to(snake_case)

          # forward pass
          with torch.no_grad():
             _UpperCAmelCase  :  List[str]				       =model(**snake_case)

          # verify the logits
          _UpperCAmelCase  :  List[str]				       =torch.Size((1, 1_0_0_0))
          self.assertEqual(outputs.logits.shape    ,				snake_case)

          _UpperCAmelCase  :  Optional[int]				       =torch.tensor([-0.27_44, 0.82_15, -0.08_36]).to(snake_case)

          self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,				snake_case    ,				atol=1E-4))






       @slow
       def      lowerCAmelCase						(     self)		->    Optional[Any]:




          '''simple docstring'''
          # ViT models have an `interpolate_pos_encoding` argument in their forward method,
          # allowing to interpolate the pre-trained position embeddings in order to use
          # the model on higher resolutions. The DINO model by Facebook AI leverages this
          # to visualize self-attention on higher resolution images.
          _UpperCAmelCase  :  List[Any]				       =ViTModel.from_pretrained('facebook/dino-vits8').to(snake_case)

          _UpperCAmelCase  :  Tuple				       =ViTImageProcessor.from_pretrained('facebook/dino-vits8'    ,				size=4_8_0)
          _UpperCAmelCase  :  Tuple				       =prepare_img()
          _UpperCAmelCase  :  Union[str, Any]				       =image_processor(images=snake_case    ,				return_tensors='pt')
          _UpperCAmelCase  :  str				       =inputs.pixel_values.to(snake_case)

          # forward pass
          with torch.no_grad():
             _UpperCAmelCase  :  Any				       =model(snake_case    ,				interpolate_pos_encoding=snake_case)

          # verify the logits
          _UpperCAmelCase  :  Dict				       =torch.Size((1, 3_6_0_1, 3_8_4))
          self.assertEqual(outputs.last_hidden_state.shape    ,				snake_case)

          _UpperCAmelCase  :  Optional[int]				       =torch.tensor(
              [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]).to(snake_case)

          self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]    ,				snake_case    ,				atol=1E-4))






       @slow
       @require_accelerate
       @require_torch_gpu
       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =ViTModel.from_pretrained('facebook/dino-vits8'    ,				torch_dtype=torch.floataa    ,				device_map='auto')
          _UpperCAmelCase  :  List[str]				       =self.default_image_processor

          _UpperCAmelCase  :  Optional[int]				       =prepare_img()
          _UpperCAmelCase  :  Union[str, Any]				       =image_processor(images=snake_case    ,				return_tensors='pt')
          _UpperCAmelCase  :  List[Any]				       =inputs.pixel_values.to(snake_case)

          # forward pass to make sure inference works in fp16
          with torch.no_grad():
             _UpperCAmelCase  :  Tuple				       =model(snake_case)





'''simple docstring'''






def lowerCamelCase__     (		__lowerCamelCase			:				str				):







   '''simple docstring'''




   _UpperCAmelCase  :  Dict				       =''
   for ch in key:
      if ch == " " or ch not in key_no_dups and ch.isalpha():
         key_no_dups += ch
   return key_no_dups





def lowerCamelCase__     (		__lowerCamelCase			:				str				):







   '''simple docstring'''




   _UpperCAmelCase  :  List[str]				       =[chr(i + 6_5				) for i in range(2_6				)]
   # Remove duplicate characters from key
   _UpperCAmelCase  :  Any				       =remove_duplicates(key.upper()				)
   _UpperCAmelCase  :  Optional[Any]				       =len(__lowerCamelCase				)
   # First fill cipher with key characters
   _UpperCAmelCase  :  List[Any]				       ={alphabet[i]: char for i, char in enumerate(__lowerCamelCase				)}
   # Then map remaining characters in alphabet to
   # the alphabet from the beginning
   for i in range(len(__lowerCamelCase				)			,       2_6				):
      _UpperCAmelCase  :  Union[str, Any]				       =alphabet[i - offset]
      # Ensure we are not mapping letters to letters previously mapped
      while char in key:
         offset -= 1
         _UpperCAmelCase  :  str				       =alphabet[i - offset]
      _UpperCAmelCase  :  Any				       =char
   return cipher_alphabet





def lowerCamelCase__     (		__lowerCamelCase			:				str			,       __lowerCamelCase			:				dict[str, str]				):







   '''simple docstring'''




   return "".join(cipher_map.get(__lowerCamelCase			,       __lowerCamelCase				) for ch in message.upper()				)





def lowerCamelCase__     (		__lowerCamelCase			:				str			,       __lowerCamelCase			:				dict[str, str]				):







   '''simple docstring'''




   _UpperCAmelCase  :  List[str]				       ={v: k for k, v in cipher_map.items()}
   return "".join(rev_cipher_map.get(__lowerCamelCase			,       __lowerCamelCase				) for ch in message.upper()				)





def lowerCamelCase__     (		):







   '''simple docstring'''




   _UpperCAmelCase  :  Union[str, Any]				       =input('Enter message to encode or decode: '				).strip()
   _UpperCAmelCase  :  int				       =input('Enter keyword: '				).strip()
   _UpperCAmelCase  :  int				       =input('Encipher or decipher? E/D:'				).strip()[0].lower()
   try:
      _UpperCAmelCase  :  Tuple				       ={'e': encipher, 'd': decipher}[option]
   except KeyError:
      raise KeyError('invalid input option'				)
   _UpperCAmelCase  :  List[str]				       =create_cipher_map(__lowerCamelCase				)
   print(func(__lowerCamelCase			,       __lowerCamelCase				)				)


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 main()

