




import argparse
import struct
import unittest

class   __a:






    """simple docstring"""





    def __init__(				self       ,_SCREAMING_SNAKE_CASE  )			->		None:
         UpperCAmelCase_      :							Any       =      data

         # Initialize hash values
         UpperCAmelCase_      :							List[Any]       =      [
             0X6a_09_e6_67,
             0Xbb_67_ae_85,
             0X3c_6e_f3_72,
             0Xa5_4f_f5_3a,
             0X51_0e_52_7f,
             0X9b_05_68_8c,
             0X1f_83_d9_ab,
             0X5b_e0_cd_19,
         ]

         # Initialize round constants
         UpperCAmelCase_      :							List[str]       =      [
             0X42_8a_2f_98,
             0X71_37_44_91,
             0Xb5_c0_fb_cf,
             0Xe9_b5_db_a5,
             0X39_56_c2_5b,
             0X59_f1_11_f1,
             0X92_3f_82_a4,
             0Xab_1c_5e_d5,
             0Xd8_07_aa_98,
             0X12_83_5b_01,
             0X24_31_85_be,
             0X55_0c_7d_c3,
             0X72_be_5d_74,
             0X80_de_b1_fe,
             0X9b_dc_06_a7,
             0Xc1_9b_f1_74,
             0Xe4_9b_69_c1,
             0Xef_be_47_86,
             0X0f_c1_9d_c6,
             0X24_0c_a1_cc,
             0X2d_e9_2c_6f,
             0X4a_74_84_aa,
             0X5c_b0_a9_dc,
             0X76_f9_88_da,
             0X98_3e_51_52,
             0Xa8_31_c6_6d,
             0Xb0_03_27_c8,
             0Xbf_59_7f_c7,
             0Xc6_e0_0b_f3,
             0Xd5_a7_91_47,
             0X06_ca_63_51,
             0X14_29_29_67,
             0X27_b7_0a_85,
             0X2e_1b_21_38,
             0X4d_2c_6d_fc,
             0X53_38_0d_13,
             0X65_0a_73_54,
             0X76_6a_0a_bb,
             0X81_c2_c9_2e,
             0X92_72_2c_85,
             0Xa2_bf_e8_a1,
             0Xa8_1a_66_4b,
             0Xc2_4b_8b_70,
             0Xc7_6c_51_a3,
             0Xd1_92_e8_19,
             0Xd6_99_06_24,
             0Xf4_0e_35_85,
             0X10_6a_a0_70,
             0X19_a4_c1_16,
             0X1e_37_6c_08,
             0X27_48_77_4c,
             0X34_b0_bc_b5,
             0X39_1c_0c_b3,
             0X4e_d8_aa_4a,
             0X5b_9c_ca_4f,
             0X68_2e_6f_f3,
             0X74_8f_82_ee,
             0X78_a5_63_6f,
             0X84_c8_78_14,
             0X8c_c7_02_08,
             0X90_be_ff_fa,
             0Xa4_50_6c_eb,
             0Xbe_f9_a3_f7,
             0Xc6_71_78_f2,
         ]

         UpperCAmelCase_      :							int       =      self.preprocessing(self.data  )
         self.final_hash()





    @staticmethod
    def 							a__				(				_SCREAMING_SNAKE_CASE  )			->		bytes:
         UpperCAmelCase_      :							int       =      b'''\x80''' + (b'''\x00''' * (63 - (len(_SCREAMING_SNAKE_CASE  ) + 8) % 64))
         UpperCAmelCase_      :							Optional[int]       =      struct.pack('''>Q'''       ,(len(_SCREAMING_SNAKE_CASE  ) * 8)  )
         return data + padding + big_endian_integer





    def 							a__				(				self  )			->		None:
         # Convert into blocks of 64 bytes
         UpperCAmelCase_      :							Tuple       =      [
             self.preprocessed_data[x : x + 64]
             for x in range(0       ,len(self.preprocessed_data  )       ,64  )
         ]

         for block in self.blocks:
              # Convert the given block into a list of 4 byte integers
              UpperCAmelCase_      :							Tuple       =      list(struct.unpack('''>16L'''       ,_SCREAMING_SNAKE_CASE  )  )
              # add 48 0-ed integers
              words += [0] * 48

              UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_      :							int       =      self.hashes

              for index in range(0       ,64  ):
                   if index > 15:
                        # modify the zero-ed indexes at the end of the array
                        UpperCAmelCase_      :							Tuple       =      (
                            self.ror(words[index - 15]       ,7  )
                            ^ self.ror(words[index - 15]       ,18  )
                            ^ (words[index - 15] >> 3)
                        )
                        UpperCAmelCase_      :							List[Any]       =      (
                            self.ror(words[index - 2]       ,17  )
                            ^ self.ror(words[index - 2]       ,19  )
                            ^ (words[index - 2] >> 10)
                        )

                        UpperCAmelCase_      :							List[str]       =      (
                            words[index - 16] + sa + words[index - 7] + sa
                        ) % 0X1_00_00_00_00

                   # Compression
                   UpperCAmelCase_      :							int       =      self.ror(_SCREAMING_SNAKE_CASE       ,6  ) ^ self.ror(_SCREAMING_SNAKE_CASE       ,11  ) ^ self.ror(_SCREAMING_SNAKE_CASE       ,25  )
                   UpperCAmelCase_      :							List[str]       =      (e & f) ^ ((~e & 0Xff_ff_ff_ff) & g)
                   UpperCAmelCase_      :							Optional[Any]       =      (
                       h + sa + ch + self.round_constants[index] + words[index]
                   ) % 0X1_00_00_00_00
                   UpperCAmelCase_      :							Union[str, Any]       =      self.ror(_SCREAMING_SNAKE_CASE       ,2  ) ^ self.ror(_SCREAMING_SNAKE_CASE       ,13  ) ^ self.ror(_SCREAMING_SNAKE_CASE       ,22  )
                   UpperCAmelCase_      :							str       =      (a & b) ^ (a & c) ^ (b & c)
                   UpperCAmelCase_      :							Tuple       =      (sa + maj) % 0X1_00_00_00_00

                   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_      :							int       =      (
                       g,
                       f,
                       e,
                       ((d + tempa) % 0X1_00_00_00_00),
                       c,
                       b,
                       a,
                       ((tempa + tempa) % 0X1_00_00_00_00),
                   )

              UpperCAmelCase_      :							int       =      [a, b, c, d, e, f, g, h]

              # Modify final values
              UpperCAmelCase_      :							Any       =      [
                  ((element + mutated_hash_values[index]) % 0X1_00_00_00_00)
                  for index, element in enumerate(self.hashes  )
              ]

         UpperCAmelCase_      :							Dict       =      ''''''.join([hex(_SCREAMING_SNAKE_CASE  )[2:].zfill(8  ) for value in self.hashes]  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		int:
         return 0Xff_ff_ff_ff & (value << (32 - rotations)) | (value >> rotations)


class   __a(					unittest.TestCase       ):






    """simple docstring"""





    def 							a__				(				self  )			->		None:
         import hashlib

         UpperCAmelCase_      :							int       =      bytes('''Test String'''       ,'''utf-8'''  )
         self.assertEqual(SHAaaa(_SCREAMING_SNAKE_CASE  ).hash       ,hashlib.shaaaa(_SCREAMING_SNAKE_CASE  ).hexdigest()  )







def lowerCamelCase__       (      ):

     '''simple docstring'''


     import doctest

     doctest.testmod()

     UpperCAmelCase_      :							List[str]       =      argparse.ArgumentParser()
     parser.add_argument(
         '''-s'''			,						'''--string'''			,						dest='''input_string'''			,						default='''Hello World!! Welcome to Cryptography'''			,						help='''Hash the string'''			,						)
     parser.add_argument(
         '''-f'''			,						'''--file'''			,						dest='''input_file'''			,						help='''Hash contents of a file'''				)

     UpperCAmelCase_      :							List[Any]       =      parser.parse_args()

     UpperCAmelCase_      :							Optional[int]       =      args.input_string

     # hash input should be a bytestring
     if args.input_file:
          with open(args.input_file			,						'''rb'''				) as f:
               UpperCAmelCase_      :							List[str]       =      f.read()
     else:
          UpperCAmelCase_      :							List[str]       =      bytes(_lowercase			,						'''utf-8'''				)

     print(SHAaaa(_lowercase				).hash				)


if __name__ == "__main__":
     main()





import inspect
import unittest

import numpy as np

from transformers import BeitConfig
from transformers.testing_utils import require_flax, require_vision, slow
from transformers.utils import cached_property, is_flax_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor


if is_flax_available():
     import jax

     from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
     from PIL import Image

     from transformers import BeitImageProcessor

class   __a(					unittest.TestCase       ):






    """simple docstring"""





    def __init__(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE=100       ,_SCREAMING_SNAKE_CASE=13       ,_SCREAMING_SNAKE_CASE=30       ,_SCREAMING_SNAKE_CASE=2       ,_SCREAMING_SNAKE_CASE=3       ,_SCREAMING_SNAKE_CASE=True       ,_SCREAMING_SNAKE_CASE=True       ,_SCREAMING_SNAKE_CASE=32       ,_SCREAMING_SNAKE_CASE=5       ,_SCREAMING_SNAKE_CASE=4       ,_SCREAMING_SNAKE_CASE=37       ,_SCREAMING_SNAKE_CASE="gelu"       ,_SCREAMING_SNAKE_CASE=0.1       ,_SCREAMING_SNAKE_CASE=0.1       ,_SCREAMING_SNAKE_CASE=10       ,_SCREAMING_SNAKE_CASE=0.02       ,_SCREAMING_SNAKE_CASE=3       ,)			->		Dict:
         UpperCAmelCase_      :							Union[str, Any]       =      parent
         UpperCAmelCase_      :							Tuple       =      vocab_size
         UpperCAmelCase_      :							List[str]       =      batch_size
         UpperCAmelCase_      :							Any       =      image_size
         UpperCAmelCase_      :							Union[str, Any]       =      patch_size
         UpperCAmelCase_      :							Union[str, Any]       =      num_channels
         UpperCAmelCase_      :							Any       =      is_training
         UpperCAmelCase_      :							Union[str, Any]       =      use_labels
         UpperCAmelCase_      :							Union[str, Any]       =      hidden_size
         UpperCAmelCase_      :							Union[str, Any]       =      num_hidden_layers
         UpperCAmelCase_      :							int       =      num_attention_heads
         UpperCAmelCase_      :							Union[str, Any]       =      intermediate_size
         UpperCAmelCase_      :							Any       =      hidden_act
         UpperCAmelCase_      :							Optional[int]       =      hidden_dropout_prob
         UpperCAmelCase_      :							List[Any]       =      attention_probs_dropout_prob
         UpperCAmelCase_      :							List[str]       =      type_sequence_label_size
         UpperCAmelCase_      :							Union[str, Any]       =      initializer_range

         # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         UpperCAmelCase_      :							Dict       =      (image_size // patch_size) ** 2
         UpperCAmelCase_      :							List[str]       =      num_patches + 1





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

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

         UpperCAmelCase_      :							Optional[int]       =      BeitConfig(
             vocab_size=self.vocab_size       ,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=_SCREAMING_SNAKE_CASE       ,initializer_range=self.initializer_range       ,)

         return config, pixel_values, labels





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		Optional[Any]:
         UpperCAmelCase_      :							Union[str, Any]       =      FlaxBeitModel(config=_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							List[str]       =      model(_SCREAMING_SNAKE_CASE  )
         self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.hidden_size)  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		Tuple:
         UpperCAmelCase_      :							Union[str, Any]       =      FlaxBeitForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							Any       =      model(_SCREAMING_SNAKE_CASE  )
         self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length - 1, self.vocab_size)  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		str:
         UpperCAmelCase_      :							Dict       =      self.type_sequence_label_size
         UpperCAmelCase_      :							int       =      FlaxBeitForImageClassification(config=_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							Union[str, Any]       =      model(_SCREAMING_SNAKE_CASE  )
         self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.type_sequence_label_size)  )

         # test greyscale images
         UpperCAmelCase_      :							Any       =      1
         UpperCAmelCase_      :							List[Any]       =      FlaxBeitForImageClassification(_SCREAMING_SNAKE_CASE  )

         UpperCAmelCase_      :							str       =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
         UpperCAmelCase_      :							Optional[Any]       =      model(_SCREAMING_SNAKE_CASE  )





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


@require_flax
class   __a(					_a    ,						unittest.TestCase       ):






    """simple docstring"""





    lowerCAmelCase        =				(
        (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
    )





    def 							a__				(				self  )			->		None:
         UpperCAmelCase_      :							List[Any]       =      FlaxBeitModelTester(self  )
         UpperCAmelCase_      :							List[str]       =      ConfigTester(self       ,config_class=_SCREAMING_SNAKE_CASE       ,has_text_modality=_SCREAMING_SNAKE_CASE       ,hidden_size=37  )





    def 							a__				(				self  )			->		Optional[int]:
         self.config_tester.run_common_tests()





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

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

              UpperCAmelCase_      :							Optional[int]       =      ['''pixel_values''']
              self.assertListEqual(arg_names[:1]       ,_SCREAMING_SNAKE_CASE  )





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

         for model_class in self.all_model_classes:
              with self.subTest(model_class.__name__  ):
                   UpperCAmelCase_      :							List[Any]       =      self._prepare_for_class(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )
                   UpperCAmelCase_      :							Union[str, Any]       =      model_class(_SCREAMING_SNAKE_CASE  )

                   @jax.jit
                   def model_jitted(_SCREAMING_SNAKE_CASE       ,**_SCREAMING_SNAKE_CASE  ):
                        return model(pixel_values=_SCREAMING_SNAKE_CASE       ,**_SCREAMING_SNAKE_CASE  )

                   with self.subTest('''JIT Enabled'''  ):
                        UpperCAmelCase_      :							Dict       =      model_jitted(**_SCREAMING_SNAKE_CASE  ).to_tuple()

                   with self.subTest('''JIT Disabled'''  ):
                        with jax.disable_jit():
                             UpperCAmelCase_      :							List[str]       =      model_jitted(**_SCREAMING_SNAKE_CASE  ).to_tuple()

                   self.assertEqual(len(_SCREAMING_SNAKE_CASE  )       ,len(_SCREAMING_SNAKE_CASE  )  )
                   for jitted_output, output in zip(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  ):
                        self.assertEqual(jitted_output.shape       ,output.shape  )





    def 							a__				(				self  )			->		List[str]:
         UpperCAmelCase_      :							Dict       =      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE  )





    def 							a__				(				self  )			->		Union[str, Any]:
         UpperCAmelCase_      :							Optional[Any]       =      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE  )





    def 							a__				(				self  )			->		List[str]:
         UpperCAmelCase_      :							int       =      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE  )





    @slow
    def 							a__				(				self  )			->		List[Any]:
         for model_class_name in self.all_model_classes:
              UpperCAmelCase_      :							List[Any]       =      model_class_name.from_pretrained('''microsoft/beit-base-patch16-224'''  )
              UpperCAmelCase_      :							Optional[int]       =      model(np.ones((1, 3, 224, 224)  )  )
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE  )







def lowerCamelCase__       (      ):

     '''simple docstring'''


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



@require_vision
@require_flax
class   __a(					unittest.TestCase       ):






    """simple docstring"""





    @cached_property
    def 							a__				(				self  )			->		Dict:
         return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224'''  ) if is_vision_available() else None





    @slow
    def 							a__				(				self  )			->		Optional[int]:
         UpperCAmelCase_      :							Tuple       =      FlaxBeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''  )

         UpperCAmelCase_      :							List[Any]       =      self.default_image_processor
         UpperCAmelCase_      :							Optional[Any]       =      prepare_img()
         UpperCAmelCase_      :							Optional[Any]       =      image_processor(images=_SCREAMING_SNAKE_CASE       ,return_tensors='''np'''  ).pixel_values

         # prepare bool_masked_pos
         UpperCAmelCase_      :							Union[str, Any]       =      np.ones((1, 196)       ,dtype=_SCREAMING_SNAKE_CASE  )

         # forward pass
         UpperCAmelCase_      :							Optional[int]       =      model(pixel_values=_SCREAMING_SNAKE_CASE       ,bool_masked_pos=_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							List[str]       =      outputs.logits

         # verify the logits
         UpperCAmelCase_      :							List[str]       =      (1, 196, 8_192)
         self.assertEqual(logits.shape       ,_SCREAMING_SNAKE_CASE  )

         UpperCAmelCase_      :							List[str]       =      np.array(
             [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]  )

         self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3]       ,_SCREAMING_SNAKE_CASE       ,atol=1e-2  )  )





    @slow
    def 							a__				(				self  )			->		Union[str, Any]:
         UpperCAmelCase_      :							Any       =      FlaxBeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''  )

         UpperCAmelCase_      :							Any       =      self.default_image_processor
         UpperCAmelCase_      :							Any       =      prepare_img()
         UpperCAmelCase_      :							Union[str, Any]       =      image_processor(images=_SCREAMING_SNAKE_CASE       ,return_tensors='''np'''  )

         # forward pass
         UpperCAmelCase_      :							Optional[Any]       =      model(**_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							Optional[Any]       =      outputs.logits

         # verify the logits
         UpperCAmelCase_      :							Dict       =      (1, 1_000)
         self.assertEqual(logits.shape       ,_SCREAMING_SNAKE_CASE  )

         UpperCAmelCase_      :							Dict       =      np.array([-1.23_85, -1.09_87, -1.01_08]  )

         self.assertTrue(np.allclose(logits[0, :3]       ,_SCREAMING_SNAKE_CASE       ,atol=1e-4  )  )

         UpperCAmelCase_      :							Dict       =      281
         self.assertEqual(logits.argmax(-1  ).item()       ,_SCREAMING_SNAKE_CASE  )





    @slow
    def 							a__				(				self  )			->		Union[str, Any]:
         UpperCAmelCase_      :							str       =      FlaxBeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''  )

         UpperCAmelCase_      :							Tuple       =      self.default_image_processor
         UpperCAmelCase_      :							Any       =      prepare_img()
         UpperCAmelCase_      :							Dict       =      image_processor(images=_SCREAMING_SNAKE_CASE       ,return_tensors='''np'''  )

         # forward pass
         UpperCAmelCase_      :							Dict       =      model(**_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							Optional[Any]       =      outputs.logits

         # verify the logits
         UpperCAmelCase_      :							Union[str, Any]       =      (1, 21_841)
         self.assertEqual(logits.shape       ,_SCREAMING_SNAKE_CASE  )

         UpperCAmelCase_      :							Dict       =      np.array([1.68_81, -0.27_87, 0.59_01]  )

         self.assertTrue(np.allclose(logits[0, :3]       ,_SCREAMING_SNAKE_CASE       ,atol=1e-4  )  )

         UpperCAmelCase_      :							Dict       =      2_396
         self.assertEqual(logits.argmax(-1  ).item()       ,_SCREAMING_SNAKE_CASE  )