




"""simple docstring"""




import unittest

from transformers import LiltConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    LiltForQuestionAnswering,
			    LiltForSequenceClassification,
			    LiltForTokenClassification,
			    LiltModel,
			)
			from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST

class   UpperCAmelCase_    :
		def __init__(						self				,						UpperCamelCase_				,						UpperCamelCase_=13				,						UpperCamelCase_=7				,						UpperCamelCase_=True				,						UpperCamelCase_=True				,						UpperCamelCase_=True				,						UpperCamelCase_=True				,						UpperCamelCase_=99				,						UpperCamelCase_=24				,						UpperCamelCase_=2				,						UpperCamelCase_=6				,						UpperCamelCase_=37				,						UpperCamelCase_="gelu"				,						UpperCamelCase_=0.1				,						UpperCamelCase_=0.1				,						UpperCamelCase_=5_12				,						UpperCamelCase_=16				,						UpperCamelCase_=2				,						UpperCamelCase_=0.0_2				,						UpperCamelCase_=3				,						UpperCamelCase_=None				,						UpperCamelCase_=10_00				,						)						->       Any:
					__lowercase			:  Optional[Any]								=       parent
					__lowercase			:  List[str]								=       batch_size
					__lowercase			:  Any								=       seq_length
					__lowercase			:  List[Any]								=       is_training
					__lowercase			:  Optional[Any]								=       use_input_mask
					__lowercase			:  Dict								=       use_token_type_ids
					__lowercase			:  int								=       use_labels
					__lowercase			:  Any								=       vocab_size
					__lowercase			:  str								=       hidden_size
					__lowercase			:  List[str]								=       num_hidden_layers
					__lowercase			:  Tuple								=       num_attention_heads
					__lowercase			:  Dict								=       intermediate_size
					__lowercase			:  List[str]								=       hidden_act
					__lowercase			:  Tuple								=       hidden_dropout_prob
					__lowercase			:  Dict								=       attention_probs_dropout_prob
					__lowercase			:  str								=       max_position_embeddings
					__lowercase			:  str								=       type_vocab_size
					__lowercase			:  Tuple								=       type_sequence_label_size
					__lowercase			:  Tuple								=       initializer_range
					__lowercase			:  Dict								=       num_labels
					__lowercase			:  List[Any]								=       scope
					__lowercase			:  Optional[Any]								=       range_bbox
		def 	_lowerCamelCase							(						self       )						->       int:
					__lowercase			:  int								=       ids_tensor([self.batch_size, self.seq_length]				,						self.vocab_size       )

					__lowercase			:  List[Any]								=       ids_tensor([self.batch_size, self.seq_length, 4]				,						self.range_bbox       )
					# Ensure that bbox is legal
					for i in range(bbox.shape[0]       ):
								for j in range(bbox.shape[1]       ):
											if bbox[i, j, 3] < bbox[i, j, 1]:
														__lowercase			:  int								=       bbox[i, j, 3]
														__lowercase			:  str								=       bbox[i, j, 1]
														__lowercase			:  List[Any]								=       t
											if bbox[i, j, 2] < bbox[i, j, 0]:
														__lowercase			:  Tuple								=       bbox[i, j, 2]
														__lowercase			:  Optional[int]								=       bbox[i, j, 0]
														__lowercase			:  List[Any]								=       t

					__lowercase			:  Union[str, Any]								=       None
					if self.use_input_mask:
								__lowercase			:  str								=       ids_tensor([self.batch_size, self.seq_length]				,						vocab_size=2       )

					__lowercase			:  Tuple								=       None
					if self.use_token_type_ids:
								__lowercase			:  Union[str, Any]								=       ids_tensor([self.batch_size, self.seq_length]				,						self.type_vocab_size       )

					__lowercase			:  Any								=       None
					__lowercase			:  Optional[int]								=       None
					if self.use_labels:
								__lowercase			:  Dict								=       ids_tensor([self.batch_size]				,						self.type_sequence_label_size       )
								__lowercase			:  int								=       ids_tensor([self.batch_size, self.seq_length]				,						self.num_labels       )

					__lowercase			:  Any								=       self.get_config()

					return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels
		def 	_lowerCamelCase							(						self       )						->       List[Any]:
					return LiltConfig(
					    vocab_size=self.vocab_size				,						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				,						max_position_embeddings=self.max_position_embeddings				,						type_vocab_size=self.type_vocab_size				,						initializer_range=self.initializer_range				,						)
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						)						->       Union[str, Any]:
					__lowercase			:  int								=       LiltModel(config=UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  Any								=       model(UpperCamelCase_				,						bbox=UpperCamelCase_				,						attention_mask=UpperCamelCase_				,						token_type_ids=UpperCamelCase_       )
					__lowercase			:  int								=       model(UpperCamelCase_				,						bbox=UpperCamelCase_				,						token_type_ids=UpperCamelCase_       )
					__lowercase			:  List[Any]								=       model(UpperCamelCase_				,						bbox=UpperCamelCase_       )

					self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)       )
					self.parent.assertEqual(result.pooler_output.shape				,						(self.batch_size, self.hidden_size)       )
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						)						->       Dict:
					__lowercase			:  Any								=       self.num_labels
					__lowercase			:  Dict								=       LiltForTokenClassification(config=UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  List[Any]								=       model(
					    UpperCamelCase_				,						bbox=UpperCamelCase_				,						attention_mask=UpperCamelCase_				,						token_type_ids=UpperCamelCase_				,						labels=UpperCamelCase_       )
					self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.num_labels)       )
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						)						->       List[Any]:
					__lowercase			:  Optional[Any]								=       LiltForQuestionAnswering(config=UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  List[str]								=       model(
					    UpperCamelCase_				,						bbox=UpperCamelCase_				,						attention_mask=UpperCamelCase_				,						token_type_ids=UpperCamelCase_				,						start_positions=UpperCamelCase_				,						end_positions=UpperCamelCase_				,						)
					self.parent.assertEqual(result.start_logits.shape				,						(self.batch_size, self.seq_length)       )
					self.parent.assertEqual(result.end_logits.shape				,						(self.batch_size, self.seq_length)       )
		def 	_lowerCamelCase							(						self       )						->       Union[str, Any]:
					__lowercase			:  List[str]								=       self.prepare_config_and_inputs()
					(
					    (
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,
					)			:  List[str]								=       config_and_inputs
					__lowercase			:  str								=       {
					    '''input_ids''': input_ids,
					    '''bbox''': bbox,
					    '''token_type_ids''': token_type_ids,
					    '''attention_mask''': input_mask,
					}
					return config, inputs_dict


@require_torch
class   UpperCAmelCase_    (			snake_case   ,					snake_case   ,					snake_case   ,					unittest.TestCase		):
		UpperCamelCase							=(
		    (
		        LiltModel,
		        LiltForSequenceClassification,
		        LiltForTokenClassification,
		        LiltForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		UpperCamelCase							=(
		    {
		        "feature-extraction": LiltModel,
		        "question-answering": LiltForQuestionAnswering,
		        "text-classification": LiltForSequenceClassification,
		        "token-classification": LiltForTokenClassification,
		        "zero-shot": LiltForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		UpperCamelCase							=False
		UpperCamelCase							=False
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_       )						->       List[Any]:
					return True
		def 	_lowerCamelCase							(						self       )						->       Any:
					__lowercase			:  Union[str, Any]								=       LiltModelTester(self       )
					__lowercase			:  Tuple								=       ConfigTester(self				,						config_class=UpperCamelCase_				,						hidden_size=37       )
		def 	_lowerCamelCase							(						self       )						->       Optional[Any]:
					self.config_tester.run_common_tests()
		def 	_lowerCamelCase							(						self       )						->       Optional[Any]:
					__lowercase			:  List[Any]								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       Optional[int]:
					__lowercase			:  Any								=       self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
								__lowercase			:  str								=       type
								self.model_tester.create_and_check_model(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       Union[str, Any]:
					__lowercase			:  List[str]								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       Union[str, Any]:
					__lowercase			:  Optional[Any]								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*UpperCamelCase_       )
		@slow
		def 	_lowerCamelCase							(						self       )						->       List[Any]:
					for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowercase			:  Dict								=       LiltModel.from_pretrained(UpperCamelCase_       )
								self.assertIsNotNone(UpperCamelCase_       )


@require_torch
@slow
class   UpperCAmelCase_    (			unittest.TestCase		):
		def 	_lowerCamelCase							(						self       )						->       Optional[Any]:
					__lowercase			:  List[Any]								=       LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''       ).to(UpperCamelCase_       )

					__lowercase			:  Union[str, Any]								=       torch.tensor([[1, 2]]				,						device=UpperCamelCase_       )
					__lowercase			:  Optional[Any]								=       torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]				,						device=UpperCamelCase_       )

					# forward pass
					with torch.no_grad():
								__lowercase			:  Tuple								=       model(input_ids=UpperCamelCase_				,						bbox=UpperCamelCase_       )

					__lowercase			:  Dict								=       torch.Size([1, 2, 7_68]       )
					__lowercase			:  str								=       torch.tensor(
					    [[-0.0_6_5_3, 0.0_9_5_0, -0.0_0_6_1], [-0.0_5_4_5, 0.0_9_2_6, -0.0_3_2_4]]				,						device=UpperCamelCase_				,						)

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










import argparse
import struct
import unittest


class        __magic_name__	:





     """simple docstring"""



     def __init__(				self							:str      ,   snake_case							:bytes	):





        '''simple docstring'''




        A_       :					Dict        =      data

        # Initialize hash values
        A_       :					Union[str, 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
        A_       :					List[Any]        =      [
            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,
        ]

        A_       :					Any        =      self.preprocessing(self.data	)
        self.final_hash()



     @staticmethod
     def     SCREAMING_SNAKE_CASE		(				snake_case							:bytes	):





        '''simple docstring'''




        A_       :					List[Any]        =      B"\x80" + (B"\x00" * (63 - (len(snake_case	) + 8) % 64))
        A_       :					str        =      struct.pack(">Q"      ,   (len(snake_case	) * 8)	)
        return data + padding + big_endian_integer



     def     SCREAMING_SNAKE_CASE		(				self							:Any	):





        '''simple docstring'''




        A_       :					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
           A_       :					Dict        =      list(struct.unpack(">16L"      ,   snake_case	)	)
           # add 48 0-ed integers
           words += [0] * 48

           A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_       :					int        =      self.hashes

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

                 A_       :					Optional[int]        =      (
                     words[index - 16] + sa + words[index - 7] + sa
                 ) % 0x1_00_00_00_00

              # Compression
              A_       :					Dict        =      self.ror(snake_case      ,   6	) ^ self.ror(snake_case      ,   11	) ^ self.ror(snake_case      ,   25	)
              A_       :					int        =      (e & f) ^ ((~e & 0xff_ff_ff_ff) & g)
              A_       :					Optional[Any]        =      (
                  h + sa + ch + self.round_constants[index] + words[index]
              ) % 0x1_00_00_00_00
              A_       :					List[str]        =      self.ror(snake_case      ,   2	) ^ self.ror(snake_case      ,   13	) ^ self.ror(snake_case      ,   22	)
              A_       :					List[Any]        =      (a & b) ^ (a & c) ^ (b & c)
              A_       :					Dict        =      (sa + maj) % 0x1_00_00_00_00

              A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_   ,    A_       :					Optional[int]        =      (
                  g,
                  f,
                  e,
                  ((d + tempa) % 0x1_00_00_00_00),
                  c,
                  b,
                  a,
                  ((tempa + tempa) % 0x1_00_00_00_00),
              )

           A_       :					List[str]        =      [a, b, c, d, e, f, g, h]

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

        A_       :					Optional[int]        =      "".join([hex(snake_case	)[2:].zfill(8	) for value in self.hashes]	)





     def     SCREAMING_SNAKE_CASE		(				self							:Optional[Any]      ,   snake_case							:int      ,   snake_case							:int	):





        '''simple docstring'''




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




class        __magic_name__	(				unittest.TestCase				):





     """simple docstring"""



     def     SCREAMING_SNAKE_CASE		(				self							:List[Any]	):





        '''simple docstring'''




        import hashlib

        A_       :					int        =      bytes("Test String"      ,   "utf-8"	)
        self.assertEqual(SHAaaa(snake_case	).hash      ,   hashlib.shaaaa(snake_case	).hexdigest()	)




def   __snake_case    (       )			->    None:
   import doctest

   doctest.testmod()

   A_       :					Optional[Any]        =      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"						)

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

   A_       :					List[Any]        =      args.input_string

   # hash input should be a bytestring
   if args.input_file:
      with open(args.input_file					,					"rb"						) as f:
         A_       :					Optional[Any]        =      f.read()
   else:
      A_       :					Any        =      bytes(_lowerCAmelCase					,					"utf-8"						)

   print(SHAaaa(_lowerCAmelCase						).hash						)


if __name__ == "__main__":
 main()
