





"""simple docstring"""





from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

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 (
      TF_MODEL_FOR_PRETRAINING_MAPPING,
      TFMobileBertForMaskedLM,
      TFMobileBertForMultipleChoice,
      TFMobileBertForNextSentencePrediction,
      TFMobileBertForPreTraining,
      TFMobileBertForQuestionAnswering,
      TFMobileBertForSequenceClassification,
      TFMobileBertForTokenClassification,
      TFMobileBertModel,
  )






@require_tf
class 					_lowerCAmelCase   (     a  ,   a  ,   unittest.TestCase       ):
       """simple docstring"""




       __magic_name__     :int		  =					(
           (
               TFMobileBertModel,
               TFMobileBertForMaskedLM,
               TFMobileBertForNextSentencePrediction,
               TFMobileBertForPreTraining,
               TFMobileBertForQuestionAnswering,
               TFMobileBertForSequenceClassification,
               TFMobileBertForTokenClassification,
               TFMobileBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       __magic_name__     :Tuple		  =					(
           {
               """feature-extraction""": TFMobileBertModel,
               """fill-mask""": TFMobileBertForMaskedLM,
               """question-answering""": TFMobileBertForQuestionAnswering,
               """text-classification""": TFMobileBertForSequenceClassification,
               """token-classification""": TFMobileBertForTokenClassification,
               """zero-shot""": TFMobileBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __magic_name__     :str		  =					False
       __magic_name__     :Optional[int]		  =					False






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase=False      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							super()._prepare_for_class(__UpperCAmelCase    , __UpperCAmelCase    , return_labels=__UpperCAmelCase      )

         if return_labels:
           if model_class in get_values(__UpperCAmelCase      ):
             lowerCAmelCase__			:int											=							tf.zeros(self.model_tester.batch_size    , dtype=tf.intaa      )

         return inputs_dict






       class 					_lowerCAmelCase   (     a       ):
              """simple docstring"""




              def __init__(    self    , __UpperCAmelCase    , __UpperCAmelCase=1_3    , __UpperCAmelCase=7    , __UpperCAmelCase=True    , __UpperCAmelCase=True    , __UpperCAmelCase=True    , __UpperCAmelCase=True    , __UpperCAmelCase=9_9    , __UpperCAmelCase=3_2    , __UpperCAmelCase=3_2    , __UpperCAmelCase=2    , __UpperCAmelCase=4    , __UpperCAmelCase=3_7    , __UpperCAmelCase="gelu"    , __UpperCAmelCase=0.1    , __UpperCAmelCase=0.1    , __UpperCAmelCase=5_1_2    , __UpperCAmelCase=1_6    , __UpperCAmelCase=2    , __UpperCAmelCase=0.02    , __UpperCAmelCase=3    , __UpperCAmelCase=4    , __UpperCAmelCase=None    , ):



                '''simple docstring'''

                lowerCAmelCase__			:List[Any]											=							parent
                lowerCAmelCase__			:str											=							batch_size
                lowerCAmelCase__			:str											=							seq_length
                lowerCAmelCase__			:str											=							is_training
                lowerCAmelCase__			:List[str]											=							use_input_mask
                lowerCAmelCase__			:str											=							use_token_type_ids
                lowerCAmelCase__			:Dict											=							use_labels
                lowerCAmelCase__			:Optional[Any]											=							vocab_size
                lowerCAmelCase__			:str											=							hidden_size
                lowerCAmelCase__			:List[str]											=							num_hidden_layers
                lowerCAmelCase__			:int											=							num_attention_heads
                lowerCAmelCase__			:str											=							intermediate_size
                lowerCAmelCase__			:Optional[Any]											=							hidden_act
                lowerCAmelCase__			:List[str]											=							hidden_dropout_prob
                lowerCAmelCase__			:Tuple											=							attention_probs_dropout_prob
                lowerCAmelCase__			:List[Any]											=							max_position_embeddings
                lowerCAmelCase__			:Tuple											=							type_vocab_size
                lowerCAmelCase__			:Optional[int]											=							type_sequence_label_size
                lowerCAmelCase__			:Dict											=							initializer_range
                lowerCAmelCase__			:Optional[int]											=							num_labels
                lowerCAmelCase__			:Union[str, Any]											=							num_choices
                lowerCAmelCase__			:Union[str, Any]											=							scope
                lowerCAmelCase__			:Union[str, Any]											=							embedding_size






              def      snake_case						(    self      ):



                '''simple docstring'''

                lowerCAmelCase__			:str											=							ids_tensor([self.batch_size, self.seq_length]    , self.vocab_size      )

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

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

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

                lowerCAmelCase__			:Tuple											=							MobileBertConfig(
                    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    , embedding_size=self.embedding_size    , )

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






              def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



                '''simple docstring'''

                lowerCAmelCase__			:Tuple											=							TFMobileBertModel(config=__UpperCAmelCase      )
                lowerCAmelCase__			:List[str]											=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                lowerCAmelCase__			:List[Any]											=							model(__UpperCAmelCase      )

                lowerCAmelCase__			:str											=							[input_ids, input_mask]
                lowerCAmelCase__			:Optional[Any]											=							model(__UpperCAmelCase      )

                lowerCAmelCase__			:Dict											=							model(__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      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



                '''simple docstring'''

                lowerCAmelCase__			:List[str]											=							TFMobileBertForMaskedLM(config=__UpperCAmelCase      )
                lowerCAmelCase__			:List[str]											=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                lowerCAmelCase__			:Tuple											=							model(__UpperCAmelCase      )
                self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.seq_length, self.vocab_size)      )






              def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



                '''simple docstring'''

                lowerCAmelCase__			:str											=							TFMobileBertForNextSentencePrediction(config=__UpperCAmelCase      )
                lowerCAmelCase__			:List[Any]											=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                lowerCAmelCase__			:str											=							model(__UpperCAmelCase      )
                self.parent.assertEqual(result.logits.shape    , (self.batch_size, 2)      )






              def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



                '''simple docstring'''

                lowerCAmelCase__			:Optional[Any]											=							TFMobileBertForPreTraining(config=__UpperCAmelCase      )
                lowerCAmelCase__			:Optional[int]											=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                lowerCAmelCase__			:List[str]											=							model(__UpperCAmelCase      )
                self.parent.assertEqual(
                    result.prediction_logits.shape    , (self.batch_size, self.seq_length, self.vocab_size)      )
                self.parent.assertEqual(result.seq_relationship_logits.shape    , (self.batch_size, 2)      )






              def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



                '''simple docstring'''

                lowerCAmelCase__			:Dict											=							self.num_labels
                lowerCAmelCase__			:int											=							TFMobileBertForSequenceClassification(config=__UpperCAmelCase      )
                lowerCAmelCase__			:str											=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                lowerCAmelCase__			:str											=							model(__UpperCAmelCase      )
                self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.num_labels)      )






              def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



                '''simple docstring'''

                lowerCAmelCase__			:Tuple											=							self.num_choices
                lowerCAmelCase__			:Optional[Any]											=							TFMobileBertForMultipleChoice(config=__UpperCAmelCase      )
                lowerCAmelCase__			:Optional[int]											=							tf.tile(tf.expand_dims(__UpperCAmelCase    , 1      )    , (1, self.num_choices, 1)      )
                lowerCAmelCase__			:int											=							tf.tile(tf.expand_dims(__UpperCAmelCase    , 1      )    , (1, self.num_choices, 1)      )
                lowerCAmelCase__			:str											=							tf.tile(tf.expand_dims(__UpperCAmelCase    , 1      )    , (1, self.num_choices, 1)      )
                lowerCAmelCase__			:Tuple											=							{
                    'input_ids': multiple_choice_inputs_ids,
                    'attention_mask': multiple_choice_input_mask,
                    'token_type_ids': multiple_choice_token_type_ids,
                }
                lowerCAmelCase__			:Union[str, Any]											=							model(__UpperCAmelCase      )
                self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.num_choices)      )






              def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



                '''simple docstring'''

                lowerCAmelCase__			:List[str]											=							self.num_labels
                lowerCAmelCase__			:List[Any]											=							TFMobileBertForTokenClassification(config=__UpperCAmelCase      )
                lowerCAmelCase__			:str											=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                lowerCAmelCase__			:Union[str, Any]											=							model(__UpperCAmelCase      )
                self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.seq_length, self.num_labels)      )






              def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



                '''simple docstring'''

                lowerCAmelCase__			:List[str]											=							TFMobileBertForQuestionAnswering(config=__UpperCAmelCase      )
                lowerCAmelCase__			:List[Any]											=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                lowerCAmelCase__			:str											=							model(__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      snake_case						(    self      ):



                '''simple docstring'''

                lowerCAmelCase__			:Dict											=							self.prepare_config_and_inputs()
                (
                    lowerCAmelCase__				
                )			:Dict											=							config_and_inputs
                lowerCAmelCase__			:Tuple											=							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
                return config, inputs_dict






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Dict											=							TFMobileBertModelTest.TFMobileBertModelTester(self      )
         lowerCAmelCase__			:List[Any]											=							ConfigTester(self    , config_class=__UpperCAmelCase    , hidden_size=3_7      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         self.config_tester.run_common_tests()






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[Any]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_model(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[Any]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_masked_lm(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:int											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:str											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[int]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_pretraining(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[Any]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_question_answering(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:str											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[int]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_token_classification(*__UpperCAmelCase      )






       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         for model_name in ["google/mobilebert-uncased"]:
           lowerCAmelCase__			:Any											=							TFMobileBertModel.from_pretrained(__UpperCAmelCase      )
           self.assertIsNotNone(__UpperCAmelCase      )






@require_tf
class 					_lowerCAmelCase   (     unittest.TestCase       ):
       """simple docstring"""




       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased'      )
         lowerCAmelCase__			:Union[str, Any]											=							tf.constant([[0, 1, 2, 3, 4, 5]]      )
         lowerCAmelCase__			:Any											=							model(__UpperCAmelCase      )[0]

         lowerCAmelCase__			:Optional[Any]											=							[1, 6, 3_0_5_2_2]
         self.assertEqual(output.shape    , __UpperCAmelCase      )

         lowerCAmelCase__			:Any											=							tf.constant(
             [
                 [
                     [-4.5_91_95_47, -9.24_82_95, -9.64_52_56],
                     [-6.7_30_61_75, -6.44_02_84, -6.6_05_28_37],
                     [-7.2_74_35_06, -6.7_84_79_15, -6.02_46_73],
                 ]
             ]      )
         tf.debugging.assert_near(output[:, :3, :3]    , __UpperCAmelCase    , atol=1E-4      )


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







class      A__							:






				def __init__(						self					:      Dict    ,     a					:      str   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[int]         =		data
							lowerCAmelCase__      :      List[Any]         =		[0X67_452_301, 0Xef_cda_b89, 0X98_bad_cfe, 0X10_325_476, 0Xc3_d2e_1f0]






				@staticmethod
				def 							_lowerCamelCase		(						a					:      int    ,     a					:      List[str]   ):






							'''simple docstring'''







							return ((n << b) | (n >> (32 - b))) & 0Xff_fff_fff






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Any         =		b'\x80' + b'\x00' * (63 - (len(self.data   ) + 8) % 64)
							lowerCAmelCase__      :      List[str]         =		self.data + padding + struct.pack('>Q'    ,     8 * len(self.data   )   )
							return padded_data






				def 							_lowerCamelCase		(						self					:      Tuple   ):






							'''simple docstring'''







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






				def 							_lowerCamelCase		(						self					:      Tuple    ,     a					:      List[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		list(struct.unpack('>16L'    ,     a   )   ) + [0] * 64
							for i in range(16    ,     80   ):
										lowerCAmelCase__      :      int         =		self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16])    ,     1   )
							return w






				def 							_lowerCamelCase		(						self					:      Dict   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[Any]         =		self.padding()
							lowerCAmelCase__      :      List[Any]         =		self.split_blocks()
							for block in self.blocks:
										lowerCAmelCase__      :      str         =		self.expand_block(a   )
										lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      str         =		self.h
										for i in range(0    ,     80   ):
													if 0 <= i < 20:
																lowerCAmelCase__      :      Tuple         =		(b & c) | ((~b) & d)
																lowerCAmelCase__      :      int         =		0X5a_827_999
													elif 20 <= i < 40:
																lowerCAmelCase__      :      List[str]         =		b ^ c ^ d
																lowerCAmelCase__      :      Any         =		0X6e_d9e_ba1
													elif 40 <= i < 60:
																lowerCAmelCase__      :      Tuple         =		(b & c) | (b & d) | (c & d)
																lowerCAmelCase__      :      Tuple         =		0X8f_1bb_cdc
													elif 60 <= i < 80:
																lowerCAmelCase__      :      List[Any]         =		b ^ c ^ d
																lowerCAmelCase__      :      int         =		0Xca_62c_1d6
													lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      Dict         =		(
													    self.rotate(a    ,     5   ) + f + e + k + expanded_block[i] & 0Xff_fff_fff,
													    a,
													    self.rotate(a    ,     30   ),
													    c,
													    d,
													)
										lowerCAmelCase__      :      Optional[Any]         =		(
										    self.h[0] + a & 0Xff_fff_fff,
										    self.h[1] + b & 0Xff_fff_fff,
										    self.h[2] + c & 0Xff_fff_fff,
										    self.h[3] + d & 0Xff_fff_fff,
										    self.h[4] + e & 0Xff_fff_fff,
										)
							return ("{:08x}" * 5).format(*self.h   )






def  lowerCAmelCase__			(  )				->		Tuple:
			lowerCAmelCase__      :      Optional[int]         =		b'Test String'
			assert SHAaHash(SCREAMING_SNAKE_CASE_     ).final_hash() == hashlib.shaa(SCREAMING_SNAKE_CASE_     ).hexdigest()  # noqa: S324
def  lowerCAmelCase__			(  )				->		Any:
			lowerCAmelCase__      :      str         =		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'     )
			lowerCAmelCase__      :      Dict         =		parser.parse_args()
			lowerCAmelCase__      :      Tuple         =		args.input_string
			# In any case hash input should be a bytestring
			if args.input_file:
						with open(args.input_file       ,							'rb'     ) as f:
									lowerCAmelCase__      :      List[Any]         =		f.read()
			else:
						lowerCAmelCase__      :      Tuple         =		bytes(SCREAMING_SNAKE_CASE_       ,							'utf-8'     )
			print(SHAaHash(SCREAMING_SNAKE_CASE_     ).final_hash()     )


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

					doctest.testmod()