



"""simple docstring"""

from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import (
			    TFFunnelBaseModel,
			    TFFunnelForMaskedLM,
			    TFFunnelForMultipleChoice,
			    TFFunnelForPreTraining,
			    TFFunnelForQuestionAnswering,
			    TFFunnelForSequenceClassification,
			    TFFunnelForTokenClassification,
			    TFFunnelModel,
			)







class    __lowercase     :







						"""simple docstring"""







						def __init__(self   ,		lowercase__   ,		lowercase__=13   ,		lowercase__=7   ,		lowercase__=True   ,		lowercase__=True   ,		lowercase__=True   ,		lowercase__=True   ,		lowercase__=99   ,		lowercase__=[1, 1, 2]   ,		lowercase__=1   ,		lowercase__=32   ,		lowercase__=4   ,		lowercase__=8   ,		lowercase__=37   ,		lowercase__="gelu_new"   ,		lowercase__=0.1   ,		lowercase__=0.1   ,		lowercase__=0.0   ,		lowercase__=5_12   ,		lowercase__=3   ,		lowercase__=0.02   ,		lowercase__=3   ,		lowercase__=4   ,		lowercase__=None   ,		lowercase__=False   ,		):
											snake_case_					:		Union[str, Any] 					=					parent
											snake_case_					:		List[str] 					=					batch_size
											snake_case_					:		Union[str, Any] 					=					seq_length
											snake_case_					:		int 					=					is_training
											snake_case_					:		List[str] 					=					use_input_mask
											snake_case_					:		Optional[int] 					=					use_token_type_ids
											snake_case_					:		int 					=					use_labels
											snake_case_					:		List[str] 					=					vocab_size
											snake_case_					:		List[Any] 					=					block_sizes
											snake_case_					:		Any 					=					num_decoder_layers
											snake_case_					:		Optional[Any] 					=					d_model
											snake_case_					:		List[Any] 					=					n_head
											snake_case_					:		Any 					=					d_head
											snake_case_					:		Optional[int] 					=					d_inner
											snake_case_					:		Tuple 					=					hidden_act
											snake_case_					:		List[str] 					=					hidden_dropout
											snake_case_					:		Optional[Any] 					=					attention_dropout
											snake_case_					:		Union[str, Any] 					=					activation_dropout
											snake_case_					:		List[str] 					=					max_position_embeddings
											snake_case_					:		Tuple 					=					type_vocab_size
											snake_case_					:		Dict 					=					2
											snake_case_					:		Dict 					=					num_labels
											snake_case_					:		List[Any] 					=					num_choices
											snake_case_					:		str 					=					scope
											snake_case_					:		int 					=					initializer_std

											# Used in the tests to check the size of the first attention layer
											snake_case_					:		List[Any] 					=					n_head
											# Used in the tests to check the size of the first hidden state
											snake_case_					:		Optional[Any] 					=					self.d_model
											# Used in the tests to check the number of output hidden states/attentions
											snake_case_					:		Optional[Any] 					=					sum(self.block_sizes       ) + (0 if base else self.num_decoder_layers)
											# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
											# the last hidden state of the first block (which is the first hidden state of the decoder).
											if not base:
																snake_case_					:		Optional[int] 					=					self.num_hidden_layers + 2







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		List[str] 					=					ids_tensor([self.batch_size, self.seq_length]   ,		self.vocab_size       )

											snake_case_					:		List[str] 					=					None
											if self.use_input_mask:
																snake_case_					:		List[Any] 					=					random_attention_mask([self.batch_size, self.seq_length]       )

											snake_case_					:		Any 					=					None
											if self.use_token_type_ids:
																snake_case_					:		List[str] 					=					ids_tensor([self.batch_size, self.seq_length]   ,		self.type_vocab_size       )

											snake_case_					:		List[str] 					=					None
											snake_case_					:		List[str] 					=					None
											snake_case_					:		Optional[Any] 					=					None
											if self.use_labels:
																snake_case_					:		str 					=					ids_tensor([self.batch_size]   ,		self.type_sequence_label_size       )
																snake_case_					:		str 					=					ids_tensor([self.batch_size, self.seq_length]   ,		self.num_labels       )
																snake_case_					:		Any 					=					ids_tensor([self.batch_size]   ,		self.num_choices       )

											snake_case_					:		str 					=					FunnelConfig(
											    vocab_size=self.vocab_size   ,		block_sizes=self.block_sizes   ,		num_decoder_layers=self.num_decoder_layers   ,		d_model=self.d_model   ,		n_head=self.n_head   ,		d_head=self.d_head   ,		d_inner=self.d_inner   ,		hidden_act=self.hidden_act   ,		hidden_dropout=self.hidden_dropout   ,		attention_dropout=self.attention_dropout   ,		activation_dropout=self.activation_dropout   ,		max_position_embeddings=self.max_position_embeddings   ,		type_vocab_size=self.type_vocab_size   ,		initializer_std=self.initializer_std   ,		)

											return (
											    config,
											    input_ids,
											    token_type_ids,
											    input_mask,
											    sequence_labels,
											    token_labels,
											    choice_labels,
											)







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		Optional[int] 					=					TFFunnelModel(config=lowercase__       )
											snake_case_					:		Any 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											snake_case_					:		Optional[Any] 					=					model(lowercase__       )

											snake_case_					:		Tuple 					=					[input_ids, input_mask]
											snake_case_					:		Any 					=					model(lowercase__       )

											snake_case_					:		Tuple 					=					model(lowercase__       )
											self.parent.assertEqual(result.last_hidden_state.shape   ,		(self.batch_size, self.seq_length, self.d_model)       )

											snake_case_					:		Optional[int] 					=					False
											snake_case_					:		int 					=					TFFunnelModel(config=lowercase__       )
											snake_case_					:		int 					=					model(lowercase__       )
											self.parent.assertEqual(result.last_hidden_state.shape   ,		(self.batch_size, self.seq_length, self.d_model)       )

											snake_case_					:		Dict 					=					False
											snake_case_					:		Any 					=					TFFunnelModel(config=lowercase__       )
											snake_case_					:		Any 					=					model(lowercase__       )
											self.parent.assertEqual(result.last_hidden_state.shape   ,		(self.batch_size, self.seq_length, self.d_model)       )







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		int 					=					TFFunnelBaseModel(config=lowercase__       )
											snake_case_					:		List[str] 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											snake_case_					:		Optional[int] 					=					model(lowercase__       )

											snake_case_					:		Optional[Any] 					=					[input_ids, input_mask]
											snake_case_					:		Optional[Any] 					=					model(lowercase__       )

											snake_case_					:		str 					=					model(lowercase__       )
											self.parent.assertEqual(result.last_hidden_state.shape   ,		(self.batch_size, 2, self.d_model)       )

											snake_case_					:		Optional[int] 					=					False
											snake_case_					:		Optional[Any] 					=					TFFunnelBaseModel(config=lowercase__       )
											snake_case_					:		Optional[Any] 					=					model(lowercase__       )
											self.parent.assertEqual(result.last_hidden_state.shape   ,		(self.batch_size, 3, self.d_model)       )

											snake_case_					:		Dict 					=					False
											snake_case_					:		int 					=					TFFunnelBaseModel(config=lowercase__       )
											snake_case_					:		Tuple 					=					model(lowercase__       )
											self.parent.assertEqual(result.last_hidden_state.shape   ,		(self.batch_size, 2, self.d_model)       )







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		str 					=					TFFunnelForPreTraining(config=lowercase__       )
											snake_case_					:		List[Any] 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											snake_case_					:		List[Any] 					=					model(lowercase__       )
											self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length)       )







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		Union[str, Any] 					=					TFFunnelForMaskedLM(config=lowercase__       )
											snake_case_					:		Dict 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											snake_case_					:		Any 					=					model(lowercase__       )
											self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length, self.vocab_size)       )







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		Union[str, Any] 					=					self.num_labels
											snake_case_					:		Any 					=					TFFunnelForSequenceClassification(config=lowercase__       )
											snake_case_					:		List[str] 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											snake_case_					:		List[Any] 					=					model(lowercase__       )
											self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.num_labels)       )







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		Union[str, Any] 					=					self.num_choices
											snake_case_					:		List[str] 					=					TFFunnelForMultipleChoice(config=lowercase__       )
											snake_case_					:		str 					=					tf.tile(tf.expand_dims(lowercase__   ,		1       )   ,		(1, self.num_choices, 1)       )
											snake_case_					:		Optional[int] 					=					tf.tile(tf.expand_dims(lowercase__   ,		1       )   ,		(1, self.num_choices, 1)       )
											snake_case_					:		Any 					=					tf.tile(tf.expand_dims(lowercase__   ,		1       )   ,		(1, self.num_choices, 1)       )
											snake_case_					:		List[str] 					=					{
											    """input_ids""": multiple_choice_inputs_ids,
											    """attention_mask""": multiple_choice_input_mask,
											    """token_type_ids""": multiple_choice_token_type_ids,
											}
											snake_case_					:		Union[str, Any] 					=					model(lowercase__       )
											self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.num_choices)       )







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		int 					=					self.num_labels
											snake_case_					:		Tuple 					=					TFFunnelForTokenClassification(config=lowercase__       )
											snake_case_					:		str 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											snake_case_					:		Optional[Any] 					=					model(lowercase__       )
											self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length, self.num_labels)       )







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		Tuple 					=					TFFunnelForQuestionAnswering(config=lowercase__       )
											snake_case_					:		str 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											snake_case_					:		str 					=					model(lowercase__       )
											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 						__UpperCamelCase							(self       ):
											snake_case_					:		List[str] 					=					self.prepare_config_and_inputs()
											(
											    (
											    snake_case_
											)					,			(
											    snake_case_
											)					,			(
											    snake_case_
											)					,			(
											    snake_case_
											)					,			(
											    snake_case_
											)					,			(
											    snake_case_
											)					,			(
											    snake_case_
											)					,			
											)					:		List[Any] 					=					config_and_inputs
											snake_case_					:		Tuple 					=					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
											return config, inputs_dict







@require_tf
class    __lowercase     (					_UpperCAmelCase					,    _UpperCAmelCase					,    unittest.TestCase):







						"""simple docstring"""







						_A		:							List[Any]							       =		(
						    (
						        TFFunnelModel,
						        TFFunnelForMaskedLM,
						        TFFunnelForPreTraining,
						        TFFunnelForQuestionAnswering,
						        TFFunnelForTokenClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						_A		:							Union[str, Any]							       =		(
						    {
						        """feature-extraction""": (TFFunnelBaseModel, TFFunnelModel),
						        """fill-mask""": TFFunnelForMaskedLM,
						        """question-answering""": TFFunnelForQuestionAnswering,
						        """text-classification""": TFFunnelForSequenceClassification,
						        """token-classification""": TFFunnelForTokenClassification,
						        """zero-shot""": TFFunnelForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						_A		:							Any							       =		False
						_A		:							List[str]							       =		False







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Union[str, Any] 					=					TFFunnelModelTester(self       )
											snake_case_					:		Any 					=					ConfigTester(self   ,		config_class=lowercase__       )







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







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		int 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*lowercase__       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Optional[int] 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_pretraining(*lowercase__       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Tuple 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*lowercase__       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Any 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*lowercase__       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		int 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*lowercase__       )







@require_tf
class    __lowercase     (					_UpperCAmelCase					,    unittest.TestCase):







						"""simple docstring"""







						_A		:							int							       =		(
						    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
						)
						_A		:							str							       =		False
						_A		:							str							       =		False







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Optional[int] 					=					TFFunnelModelTester(self   ,		base=lowercase__       )
											snake_case_					:		List[str] 					=					ConfigTester(self   ,		config_class=lowercase__       )







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







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Tuple 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_base_model(*lowercase__       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Union[str, Any] 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*lowercase__       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		List[str] 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*lowercase__       )









"""simple docstring"""

import argparse
import hashlib  # hashlib is only used inside the Test class
import struct







class    __lowercase     :







						"""simple docstring"""







						def __init__(self   ,		lowercase__       ):
											snake_case_					:		Union[str, Any] 					=					data
											snake_case_					:		List[str] 					=					[0X6_7_4_5_2_3_0_1, 0Xe_f_c_d_a_b_8_9, 0X9_8_b_a_d_c_f_e, 0X1_0_3_2_5_4_7_6, 0Xc_3_d_2_e_1_f_0]







						@staticmethod
						def 						__UpperCamelCase							(lowercase__   ,		lowercase__       ):
											return ((n << b) | (n >> (32 - b))) & 0Xf_f_f_f_f_f_f_f







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Any 					=					B"""\x80""" + B"""\x00""" * (63 - (len(self.data       ) + 8) % 64)
											snake_case_					:		Tuple 					=					self.data + padding + struct.pack(""">Q"""   ,		8 * len(self.data       )       )
											return padded_data







						def 						__UpperCamelCase							(self       ):
											return [
											    self.padded_data[i : i + 64] for i in range(0   ,		len(self.padded_data       )   ,		64       )
											]







						def 						__UpperCamelCase							(self   ,		lowercase__       ):
											snake_case_					:		int 					=					list(struct.unpack(""">16L"""   ,		lowercase__       )       ) + [0] * 64
											for i in range(16   ,		80       ):
																snake_case_					:		Dict 					=					self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16])   ,		1       )
											return w







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		List[Any] 					=					self.padding()
											snake_case_					:		Any 					=					self.split_blocks()
											for block in self.blocks:
																snake_case_					:		Any 					=					self.expand_block(lowercase__       )
																snake_case_					,			snake_case_					,			snake_case_					,			snake_case_					,			snake_case_					:		List[Any] 					=					self.h
																for i in range(0   ,		80       ):
																					if 0 <= i < 20:
																										snake_case_					:		Optional[Any] 					=					(b & c) | ((~b) & d)
																										snake_case_					:		List[str] 					=					0X5_a_8_2_7_9_9_9
																					elif 20 <= i < 40:
																										snake_case_					:		Union[str, Any] 					=					b ^ c ^ d
																										snake_case_					:		Tuple 					=					0X6_e_d_9_e_b_a_1
																					elif 40 <= i < 60:
																										snake_case_					:		str 					=					(b & c) | (b & d) | (c & d)
																										snake_case_					:		List[str] 					=					0X8_f_1_b_b_c_d_c
																					elif 60 <= i < 80:
																										snake_case_					:		Tuple 					=					b ^ c ^ d
																										snake_case_					:		str 					=					0Xc_a_6_2_c_1_d_6
																					snake_case_					,			snake_case_					,			snake_case_					,			snake_case_					,			snake_case_					:		Optional[Any] 					=					(
																					    self.rotate(lowercase__   ,		5       ) + f + e + k + expanded_block[i] & 0Xf_f_f_f_f_f_f_f,
																					    a,
																					    self.rotate(lowercase__   ,		30       ),
																					    c,
																					    d,
																					)
																snake_case_					:		Any 					=					(
																    self.h[0] + a & 0Xf_f_f_f_f_f_f_f,
																    self.h[1] + b & 0Xf_f_f_f_f_f_f_f,
																    self.h[2] + c & 0Xf_f_f_f_f_f_f_f,
																    self.h[3] + d & 0Xf_f_f_f_f_f_f_f,
																    self.h[4] + e & 0Xf_f_f_f_f_f_f_f,
																)
											return ("{:08x}" * 5).format(*self.h       )







def 							SCREAMING_SNAKE_CASE__      (		):



					"""simple docstring"""





					snake_case_					:		Union[str, Any] 					=					b"""Test String"""
					assert SHAaHash(SCREAMING_SNAKE_CASE__			).final_hash() == hashlib.shaa(SCREAMING_SNAKE_CASE__			).hexdigest()  # noqa: S324







def 							SCREAMING_SNAKE_CASE__      (		):



					"""simple docstring"""





					snake_case_					:		int 					=					argparse.ArgumentParser(description="""Process some strings or files"""			)
					parser.add_argument(
					    """--string"""				,    dest="""input_string"""				,    default="""Hello World!! Welcome to Cryptography"""				,    help="""Hash the string"""				,    )
					parser.add_argument("""--file"""				,    dest="""input_file"""				,    help="""Hash contents of a file"""			)
					snake_case_					:		Optional[int] 					=					parser.parse_args()
					snake_case_					:		Optional[int] 					=					args.input_string
					# In any case hash input should be a bytestring
					if args.input_file:
										with open(args.input_file				,    """rb"""			) as f:
															snake_case_					:		List[str] 					=					f.read()
					else:
										snake_case_					:		Dict 					=					bytes(SCREAMING_SNAKE_CASE__				,    """utf-8"""			)
					print(SHAaHash(SCREAMING_SNAKE_CASE__			).final_hash()			)


if __name__ == "__main__":
			main()
			import doctest

			doctest.testmod()




