
"""simple docstring"""

import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

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


if is_flax_available():
      import jax

      from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel

class __lowerCAmelCase	(   unittest.TestCase    ):





     def __init__(			self			,  __UpperCAmelCase			,  __UpperCAmelCase=13			,  __UpperCAmelCase=30			,  __UpperCAmelCase=2			,  __UpperCAmelCase=3			,  __UpperCAmelCase=True			,  __UpperCAmelCase=True			,  __UpperCAmelCase=32			,  __UpperCAmelCase=5			,  __UpperCAmelCase=4			,  __UpperCAmelCase=37			,  __UpperCAmelCase="gelu"			,  __UpperCAmelCase=0.1			,  __UpperCAmelCase=0.1			,  __UpperCAmelCase=10			,  __UpperCAmelCase=0.0_2			,  ):

      '''simple docstring'''




      __UpperCamelCase       						= parent
      __UpperCamelCase       						= batch_size
      __UpperCamelCase       						= image_size
      __UpperCamelCase       						= patch_size
      __UpperCamelCase       						= num_channels
      __UpperCamelCase       						= is_training
      __UpperCamelCase       						= use_labels
      __UpperCamelCase       						= hidden_size
      __UpperCamelCase       						= num_hidden_layers
      __UpperCamelCase       						= num_attention_heads
      __UpperCamelCase       						= intermediate_size
      __UpperCamelCase       						= hidden_act
      __UpperCamelCase       						= hidden_dropout_prob
      __UpperCamelCase       						= attention_probs_dropout_prob
      __UpperCamelCase       						= type_sequence_label_size
      __UpperCamelCase       						= initializer_range

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





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




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

      __UpperCamelCase       						= 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=__UpperCAmelCase			,  initializer_range=self.initializer_range			,  )

      return config, pixel_values





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= FlaxViTModel(config=__UpperCAmelCase   )
      __UpperCamelCase       						= model(__UpperCAmelCase   )
      # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
      __UpperCamelCase       						= (self.image_size, self.image_size)
      __UpperCamelCase       						= (self.patch_size, self.patch_size)
      __UpperCamelCase       						= (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
      self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, num_patches + 1, self.hidden_size)   )





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.type_sequence_label_size
      __UpperCamelCase       						= FlaxViTForImageClassification(config=__UpperCAmelCase   )
      __UpperCamelCase       						= model(__UpperCAmelCase   )
      self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.type_sequence_label_size)   )

      # test greyscale images
      __UpperCamelCase       						= 1
      __UpperCamelCase       						= FlaxViTForImageClassification(__UpperCAmelCase   )

      __UpperCamelCase       						= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
      __UpperCamelCase       						= model(__UpperCAmelCase   )







     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.prepare_config_and_inputs()
      (
          (
          __UpperCamelCase     
      )						,			(
          __UpperCamelCase     
      )						,			     
      )       						= config_and_inputs
      __UpperCamelCase       						= {'pixel_values': pixel_values}
      return config, inputs_dict


@require_flax
class __lowerCAmelCase	(   _snake_case    ,						unittest.TestCase    ):
     lowercase		     =		(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= FlaxViTModelTester(self   )
      __UpperCamelCase       						= ConfigTester(self			,  config_class=__UpperCAmelCase			,  has_text_modality=__UpperCAmelCase			,  hidden_size=37   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      self.config_tester.run_common_tests()





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__UpperCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase						,			__UpperCamelCase       						= self.model_tester.prepare_config_and_inputs_for_common()

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

       __UpperCamelCase       						= ['pixel_values']
       self.assertListEqual(arg_names[:1]			,  __UpperCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase						,			__UpperCamelCase       						= self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       with self.subTest(model_class.__name__   ):
        __UpperCamelCase       						= self._prepare_for_class(__UpperCAmelCase			,  __UpperCAmelCase   )
        __UpperCamelCase       						= model_class(__UpperCAmelCase   )

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

        with self.subTest('JIT Enabled'   ):
         __UpperCamelCase       						= model_jitted(**__UpperCAmelCase   ).to_tuple()

        with self.subTest('JIT Disabled'   ):
         with jax.disable_jit():
          __UpperCamelCase       						= model_jitted(**__UpperCAmelCase   ).to_tuple()

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







     @slow
     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      for model_class_name in self.all_model_classes:
       __UpperCamelCase       						= model_class_name.from_pretrained('google/vit-base-patch16-224'   )
       __UpperCamelCase       						= model(np.ones((1, 3, 224, 224)   )   )
       self.assertIsNotNone(__UpperCAmelCase   )





"""simple docstring"""

import argparse
import struct
import unittest

class __lowerCAmelCase	:





     def __init__(			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= data

      # Initialize hash values
      __UpperCamelCase       						= [
          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       						= [
          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       						= self.preprocessing(self.data   )
      self.final_hash()





     @staticmethod
     def 		UpperCAmelCase      (			__UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= b'\x80' + (b'\x00' * (63 - (len(__UpperCAmelCase   ) + 8) % 64))
      __UpperCamelCase       						= struct.pack('>Q'			,  (len(__UpperCAmelCase   ) * 8)   )
      return data + padding + big_endian_integer





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= [
          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       						= list(struct.unpack('>16L'			,  __UpperCAmelCase   )   )
       # add 48 0-ed integers
       words += [0] * 48

       __UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase       						= self.hashes

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

         __UpperCamelCase       						= (
             words[index - 16] + sa + words[index - 7] + sa
         ) % 0x1_00_00_00_00

        # Compression
        __UpperCamelCase       						= self.ror(__UpperCAmelCase			,  6   ) ^ self.ror(__UpperCAmelCase			,  11   ) ^ self.ror(__UpperCAmelCase			,  25   )
        __UpperCamelCase       						= (e & f) ^ ((~e & 0xff_ff_ff_ff) & g)
        __UpperCamelCase       						= (
            h + sa + ch + self.round_constants[index] + words[index]
        ) % 0x1_00_00_00_00
        __UpperCamelCase       						= self.ror(__UpperCAmelCase			,  2   ) ^ self.ror(__UpperCAmelCase			,  13   ) ^ self.ror(__UpperCAmelCase			,  22   )
        __UpperCamelCase       						= (a & b) ^ (a & c) ^ (b & c)
        __UpperCamelCase       						= (sa + maj) % 0x1_00_00_00_00

        __UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase       						= (
            g,
            f,
            e,
            ((d + tempa) % 0x1_00_00_00_00),
            c,
            b,
            a,
            ((tempa + tempa) % 0x1_00_00_00_00),
        )

       __UpperCamelCase       						= [a, b, c, d, e, f, g, h]

       # Modify final values
       __UpperCamelCase       						= [
           ((element + mutated_hash_values[index]) % 0x1_00_00_00_00)
           for index, element in enumerate(self.hashes   )
       ]

      __UpperCamelCase       						= ''.join([hex(__UpperCAmelCase   )[2:].zfill(8   ) for value in self.hashes]   )







     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

      '''simple docstring'''




      return 0xff_ff_ff_ff & (value << (32 - rotations)) | (value >> rotations)


class __lowerCAmelCase	(   unittest.TestCase    ):





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      import hashlib

      __UpperCamelCase       						= bytes('Test String'			,  'utf-8'   )
      self.assertEqual(SHAaaa(__UpperCAmelCase   ).hash			,  hashlib.shaaaa(__UpperCAmelCase   ).hexdigest()   )






def 							A		(       )   ->			None:
 import doctest

 doctest.testmod()

 __UpperCamelCase       						= 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       						= parser.parse_args()

 __UpperCamelCase       						= args.input_string

 # hash input should be a bytestring
 if args.input_file:
  with open(args.input_file       ,      'rb'	) as f:
   __UpperCamelCase       						= f.read()
 else:
  __UpperCamelCase       						= bytes(snake_case       ,      'utf-8'	)

 print(SHAaaa(snake_case	).hash	)


if __name__ == "__main__":
      main()



