
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__=512			,	UpperCAmelCase__=16			,	UpperCAmelCase__=2			,	UpperCAmelCase__=0.02			,	UpperCAmelCase__=3			,	UpperCAmelCase__=None			,	UpperCAmelCase__=1_000			,	):
							A__							=       parent
							A__							=       batch_size
							A__							=       seq_length
							A__							=       is_training
							A__							=       use_input_mask
							A__							=       use_token_type_ids
							A__							=       use_labels
							A__							=       vocab_size
							A__							=       hidden_size
							A__							=       num_hidden_layers
							A__							=       num_attention_heads
							A__							=       intermediate_size
							A__							=       hidden_act
							A__							=       hidden_dropout_prob
							A__							=       attention_probs_dropout_prob
							A__							=       max_position_embeddings
							A__							=       type_vocab_size
							A__							=       type_sequence_label_size
							A__							=       initializer_range
							A__							=       num_labels
							A__							=       scope
							A__							=       range_bbox



	def 			__A							(       self		):
							A__							=       ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size		)

							A__							=       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]:
																									A__							=       bbox[i, j, 3]
																									A__							=       bbox[i, j, 1]
																									A__							=       t
																			if bbox[i, j, 2] < bbox[i, j, 0]:
																									A__							=       bbox[i, j, 2]
																									A__							=       bbox[i, j, 0]
																									A__							=       t

							A__							=       None
							if self.use_input_mask:
													A__							=       ids_tensor([self.batch_size, self.seq_length]			,	vocab_size=2		)

							A__							=       None
							if self.use_token_type_ids:
													A__							=       ids_tensor([self.batch_size, self.seq_length]			,	self.type_vocab_size		)

							A__							=       None
							A__							=       None
							if self.use_labels:
													A__							=       ids_tensor([self.batch_size]			,	self.type_sequence_label_size		)
													A__							=       ids_tensor([self.batch_size, self.seq_length]			,	self.num_labels		)

							A__							=       self.get_config()

							return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels



	def 			__A							(       self		):
							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 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	):
							A__							=       LiltModel(config=_SCREAMING_SNAKE_CASE		)
							model.to(_SCREAMING_SNAKE_CASE		)
							model.eval()
							A__							=       model(_SCREAMING_SNAKE_CASE			,	bbox=_SCREAMING_SNAKE_CASE			,	attention_mask=_SCREAMING_SNAKE_CASE			,	token_type_ids=_SCREAMING_SNAKE_CASE		)
							A__							=       model(_SCREAMING_SNAKE_CASE			,	bbox=_SCREAMING_SNAKE_CASE			,	token_type_ids=_SCREAMING_SNAKE_CASE		)
							A__							=       model(_SCREAMING_SNAKE_CASE			,	bbox=_SCREAMING_SNAKE_CASE		)

							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 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	):
							A__							=       self.num_labels
							A__							=       LiltForTokenClassification(config=_SCREAMING_SNAKE_CASE		)
							model.to(_SCREAMING_SNAKE_CASE		)
							model.eval()
							A__							=       model(
							    _SCREAMING_SNAKE_CASE			,	bbox=_SCREAMING_SNAKE_CASE			,	attention_mask=_SCREAMING_SNAKE_CASE			,	token_type_ids=_SCREAMING_SNAKE_CASE			,	labels=_SCREAMING_SNAKE_CASE		)
							self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.seq_length, self.num_labels)		)



	def 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	):
							A__							=       LiltForQuestionAnswering(config=_SCREAMING_SNAKE_CASE		)
							model.to(_SCREAMING_SNAKE_CASE		)
							model.eval()
							A__							=       model(
							    _SCREAMING_SNAKE_CASE			,	bbox=_SCREAMING_SNAKE_CASE			,	attention_mask=_SCREAMING_SNAKE_CASE			,	token_type_ids=_SCREAMING_SNAKE_CASE			,	start_positions=_SCREAMING_SNAKE_CASE			,	end_positions=_SCREAMING_SNAKE_CASE			,	)
							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 			__A							(       self		):
							A__							=       self.prepare_config_and_inputs()
							(
							    (
							    A__ 
							)						,       (
							    A__ 
							)						,       (
							    A__ 
							)						,       (
							    A__ 
							)						,       (
							    A__ 
							)						,       (
							    A__ 
							)						,       (
							    A__ 
							)						,        
							)							=       config_and_inputs
							A__							=       {
							    "input_ids": input_ids,
							    "bbox": bbox,
							    "token_type_ids": token_type_ids,
							    "attention_mask": input_mask,
							}
							return config, inputs_dict






@require_torch
class      UpperCamelCase    (						lowerCAmelCase__       ,    lowerCAmelCase__       ,    lowerCAmelCase__       ,    unittest.TestCase     ):
	lowerCAmelCase	:   Dict				=	(
	    (
	        LiltModel,
	        LiltForSequenceClassification,
	        LiltForTokenClassification,
	        LiltForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	lowerCAmelCase	:   str				=	(
	    {
	        "feature-extraction": LiltModel,
	        "question-answering": LiltForQuestionAnswering,
	        "text-classification": LiltForSequenceClassification,
	        "token-classification": LiltForTokenClassification,
	        "zero-shot": LiltForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	lowerCAmelCase	:   str				=	False
	lowerCAmelCase	:   Tuple				=	False



	def 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__		):
							return True



	def 			__A							(       self		):
							A__							=       LiltModelTester(self		)
							A__							=       ConfigTester(self			,	config_class=_SCREAMING_SNAKE_CASE			,	hidden_size=37		)



	def 			__A							(       self		):
							self.config_tester.run_common_tests()



	def 			__A							(       self		):
							A__							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)



	def 			__A							(       self		):
							A__							=       self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
													A__							=       type
													self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)



	def 			__A							(       self		):
							A__							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE		)



	def 			__A							(       self		):
							A__							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE		)



	@slow
	def 			__A							(       self		):
							for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													A__							=       LiltModel.from_pretrained(_SCREAMING_SNAKE_CASE		)
													self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)






@require_torch
@slow
class      UpperCamelCase    (						unittest.TestCase     ):



	def 			__A							(       self		):
							A__							=       LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"		).to(_SCREAMING_SNAKE_CASE		)

							A__							=       torch.tensor([[1, 2]]			,	device=_SCREAMING_SNAKE_CASE		)
							A__							=       torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]			,	device=_SCREAMING_SNAKE_CASE		)

							# forward pass
							with torch.no_grad():
													A__							=       model(input_ids=_SCREAMING_SNAKE_CASE			,	bbox=_SCREAMING_SNAKE_CASE		)

							A__							=       torch.Size([1, 2, 768]		)
							A__							=       torch.tensor(
							    [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]]			,	device=_SCREAMING_SNAKE_CASE			,	)

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










import argparse
import struct
import unittest






class      UpperCamelCase    :



	def __init__(       self			,	UpperCAmelCase__		):
							A__							=       data

							# Initialize hash values
							A__							=       [
							    0x6A_09E_667,
							    0xBB_67A_E85,
							    0x3C_6EF_372,
							    0xA5_4FF_53A,
							    0x51_0E5_27F,
							    0x9B_056_88C,
							    0x1F_83D_9AB,
							    0x5B_E0C_D19,
							]

							# Initialize round constants
							A__							=       [
							    0x42_8A2_F98,
							    0x71_374_491,
							    0xB5_C0F_BCF,
							    0xE9_B5D_BA5,
							    0x39_56C_25B,
							    0x59_F11_1F1,
							    0x92_3F8_2A4,
							    0xAB_1C5_ED5,
							    0xD8_07A_A98,
							    0x12_835_B01,
							    0x24_318_5BE,
							    0x55_0C7_DC3,
							    0x72_BE5_D74,
							    0x80_DEB_1FE,
							    0x9B_DC0_6A7,
							    0xC1_9BF_174,
							    0xE4_9B6_9C1,
							    0xEF_BE4_786,
							    0x0F_C19_DC6,
							    0x24_0CA_1CC,
							    0x2D_E92_C6F,
							    0x4A_748_4AA,
							    0x5C_B0A_9DC,
							    0x76_F98_8DA,
							    0x98_3E5_152,
							    0xA8_31C_66D,
							    0xB0_032_7C8,
							    0xBF_597_FC7,
							    0xC6_E00_BF3,
							    0xD5_A79_147,
							    0x06_CA6_351,
							    0x14_292_967,
							    0x27_B70_A85,
							    0x2E_1B2_138,
							    0x4D_2C6_DFC,
							    0x53_380_D13,
							    0x65_0A7_354,
							    0x76_6A0_ABB,
							    0x81_C2C_92E,
							    0x92_722_C85,
							    0xA2_BFE_8A1,
							    0xA8_1A6_64B,
							    0xC2_4B8_B70,
							    0xC7_6C5_1A3,
							    0xD1_92E_819,
							    0xD6_990_624,
							    0xF4_0E3_585,
							    0x10_6AA_070,
							    0x19_A4C_116,
							    0x1E_376_C08,
							    0x27_487_74C,
							    0x34_B0B_CB5,
							    0x39_1C0_CB3,
							    0x4E_D8A_A4A,
							    0x5B_9CC_A4F,
							    0x68_2E6_FF3,
							    0x74_8F8_2EE,
							    0x78_A56_36F,
							    0x84_C87_814,
							    0x8C_C70_208,
							    0x90_BEF_FFA,
							    0xA4_506_CEB,
							    0xBE_F9A_3F7,
							    0xC6_717_8F2,
							]

							A__							=       self.preprocessing(self.data		)
							self.final_hash()



	@staticmethod
	def 			__A							(       UpperCAmelCase__		):
							A__							=       b"\x80" + (b"\x00" * (63 - (len(UpperCAmelCase__		) + 8) % 64))
							A__							=       struct.pack(">Q"			,	(len(UpperCAmelCase__		) * 8)		)
							return data + padding + big_endian_integer



	def 			__A							(       self		):
							# Convert into blocks of 64 bytes
							A__							=       [
							    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__							=       list(struct.unpack(">16L"			,	UpperCAmelCase__		)		)
													# add 48 0-ed integers
													words += [0] * 48

													A__						,       A__						,       A__						,       A__						,       A__						,       A__						,       A__						,       A__							=       self.hashes

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

																									A__							=       (
																									    words[index - 16] + sa + words[index - 7] + sa
																									) % 0x100_000_000

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

																			A__						,       A__						,       A__						,       A__						,       A__						,       A__						,       A__						,       A__							=       (
																			    g,
																			    f,
																			    e,
																			    ((d + tempa) % 0x100_000_000),
																			    c,
																			    b,
																			    a,
																			    ((tempa + tempa) % 0x100_000_000),
																			)

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

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

							A__							=       "".join([hex(UpperCAmelCase__		)[2:].zfill(8		) for value in self.hashes]		)



	def 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__		):
							return 0xFF_FFF_FFF & (value << (32 - rotations)) | (value >> rotations)






class      UpperCamelCase    (						unittest.TestCase     ):



	def 			__A							(       self		):
							import hashlib

							A__							=       bytes("Test String"			,	"utf-8"		)
							self.assertEqual(SHAaaa(UpperCAmelCase__		).hash			,	hashlib.shaaaa(UpperCAmelCase__		).hexdigest()		)





def  UpperCamelCase						(       )->       None:





						"""simple docstring"""





						import doctest

						doctest.testmod()

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

						A__							=       args.input_string

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

						print(SHAaaa(_A    ).hash    )


if __name__ == "__main__":
						main()





