





import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MODEL_FOR_PRETRAINING_MAPPING,
							    MobileBertForMaskedLM,
							    MobileBertForMultipleChoice,
							    MobileBertForNextSentencePrediction,
							    MobileBertForPreTraining,
							    MobileBertForQuestionAnswering,
							    MobileBertForSequenceClassification,
							    MobileBertForTokenClassification,
							    MobileBertModel,
							)






class        _a:







		def __init__(    self ,						__snake_case ,						__snake_case=1_3 ,						__snake_case=7 ,						__snake_case=True ,						__snake_case=True ,						__snake_case=True ,						__snake_case=True ,						__snake_case=9_9 ,						__snake_case=6_4 ,						__snake_case=3_2 ,						__snake_case=5 ,						__snake_case=4 ,						__snake_case=3_7 ,						__snake_case="gelu" ,						__snake_case=0.1 ,						__snake_case=0.1 ,						__snake_case=5_1_2 ,						__snake_case=1_6 ,						__snake_case=2 ,						__snake_case=0.02 ,						__snake_case=3 ,						__snake_case=4 ,						__snake_case=None ,						)      ->      Union[str, Any]:


							'''simple docstring'''

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







		def        lowercase       (    self				)      ->      Dict:


							'''simple docstring'''

							_snake_case       :					Dict				       =   ids_tensor([self.batch_size, self.seq_length] ,						self.vocab_size				)

							_snake_case       :					int				       =   None
							if self.use_input_mask:
												_snake_case       :					Any				       =   random_attention_mask([self.batch_size, self.seq_length]				)

							_snake_case       :					int				       =   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[Any]				       =   None
							_snake_case       :					Optional[int]				       =   None
							_snake_case       :					Tuple				       =   None
							if self.use_labels:
												_snake_case       :					Dict				       =   ids_tensor([self.batch_size] ,						self.type_sequence_label_size				)
												_snake_case       :					int				       =   ids_tensor([self.batch_size, self.seq_length] ,						self.num_labels				)
												_snake_case       :					Dict				       =   ids_tensor([self.batch_size] ,						self.num_choices				)

							_snake_case       :					Union[str, Any]				       =   self.get_config()

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







		def        lowercase       (    self				)      ->      Optional[int]:


							'''simple docstring'''

							return 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 ,						embedding_size=self.embedding_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 ,						is_decoder=__snake_case ,						initializer_range=self.initializer_range ,						)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      List[str]:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   MobileBertModel(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Optional[int]				       =   model(__snake_case ,						attention_mask=__snake_case ,						token_type_ids=__snake_case				)
							_snake_case       :					Dict				       =   model(__snake_case ,						token_type_ids=__snake_case				)
							_snake_case       :					Any				       =   model(__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        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      Optional[Any]:


							'''simple docstring'''

							_snake_case       :					int				       =   MobileBertForMaskedLM(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					str				       =   model(__snake_case ,						attention_mask=__snake_case ,						token_type_ids=__snake_case ,						labels=__snake_case				)
							self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.vocab_size)				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      Optional[Any]:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   MobileBertForNextSentencePrediction(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					List[Any]				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						token_type_ids=__snake_case ,						labels=__snake_case ,						)
							self.parent.assertEqual(result.logits.shape ,						(self.batch_size, 2)				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      Optional[int]:


							'''simple docstring'''

							_snake_case       :					Optional[int]				       =   MobileBertForPreTraining(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					int				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						token_type_ids=__snake_case ,						labels=__snake_case ,						next_sentence_label=__snake_case ,						)
							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        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      Optional[int]:


							'''simple docstring'''

							_snake_case       :					Union[str, Any]				       =   MobileBertForQuestionAnswering(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					List[str]				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						token_type_ids=__snake_case ,						start_positions=__snake_case ,						end_positions=__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        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      Any:


							'''simple docstring'''

							_snake_case       :					str				       =   self.num_labels
							_snake_case       :					int				       =   MobileBertForSequenceClassification(__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Optional[Any]				       =   model(__snake_case ,						attention_mask=__snake_case ,						token_type_ids=__snake_case ,						labels=__snake_case				)
							self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.num_labels)				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      Optional[int]:


							'''simple docstring'''

							_snake_case       :					int				       =   self.num_labels
							_snake_case       :					Dict				       =   MobileBertForTokenClassification(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Tuple				       =   model(__snake_case ,						attention_mask=__snake_case ,						token_type_ids=__snake_case ,						labels=__snake_case				)
							self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.num_labels)				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      Union[str, Any]:


							'''simple docstring'''

							_snake_case       :					Tuple				       =   self.num_choices
							_snake_case       :					Optional[Any]				       =   MobileBertForMultipleChoice(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					List[str]				       =   input_ids.unsqueeze(1				).expand(-1 ,						self.num_choices ,						-1				).contiguous()
							_snake_case       :					Optional[Any]				       =   token_type_ids.unsqueeze(1				).expand(-1 ,						self.num_choices ,						-1				).contiguous()
							_snake_case       :					Tuple				       =   input_mask.unsqueeze(1				).expand(-1 ,						self.num_choices ,						-1				).contiguous()
							_snake_case       :					List[str]				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						token_type_ids=__snake_case ,						labels=__snake_case ,						)
							self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.num_choices)				)







		def        lowercase       (    self				)      ->      List[Any]:


							'''simple docstring'''

							_snake_case       :					List[Any]				       =   self.prepare_config_and_inputs()
							(
							    (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , 
							)       :					Optional[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_torch
class        _a(    __A	,   __A	,   unittest.TestCase    ):
		lowerCamelCase__     :Tuple   				=		(
		    (
		        MobileBertModel,
		        MobileBertForMaskedLM,
		        MobileBertForMultipleChoice,
		        MobileBertForNextSentencePrediction,
		        MobileBertForPreTraining,
		        MobileBertForQuestionAnswering,
		        MobileBertForSequenceClassification,
		        MobileBertForTokenClassification,
		    )
		    if is_torch_available()
		    else ()
		)
		lowerCamelCase__     :Optional[Any]   				=		(
		    {
		        'feature-extraction': MobileBertModel,
		        'fill-mask': MobileBertForMaskedLM,
		        'question-answering': MobileBertForQuestionAnswering,
		        'text-classification': MobileBertForSequenceClassification,
		        'token-classification': MobileBertForTokenClassification,
		        'zero-shot': MobileBertForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		lowerCamelCase__     :List[Any]   				=		True







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case=False				)      ->      Optional[Any]:


							'''simple docstring'''

							_snake_case       :					Optional[int]				       =   super()._prepare_for_class(__snake_case ,						__snake_case ,						return_labels=__snake_case				)

							if return_labels:
												if model_class in get_values(__snake_case				):
																	_snake_case       :					Union[str, Any]				       =   torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.seq_length) ,						dtype=torch.long ,						device=__snake_case				)
																	_snake_case       :					Union[str, Any]				       =   torch.zeros(
																	    self.model_tester.batch_size ,						dtype=torch.long ,						device=__snake_case				)
							return inputs_dict







		def        lowercase       (    self				)      ->      Tuple:


							'''simple docstring'''

							_snake_case       :					Optional[Any]				       =   MobileBertModelTester(self				)
							_snake_case       :					Optional[Any]				       =   ConfigTester(self ,						config_class=__snake_case ,						hidden_size=3_7				)







		def        lowercase       (    self				)      ->      Tuple:


							'''simple docstring'''

							self.config_tester.run_common_tests()







		def        lowercase       (    self				)      ->      str:


							'''simple docstring'''

							_snake_case       :					Dict				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*__snake_case				)







		def        lowercase       (    self				)      ->      int:


							'''simple docstring'''

							_snake_case       :					List[Any]				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*__snake_case				)







		def        lowercase       (    self				)      ->      List[str]:


							'''simple docstring'''

							_snake_case       :					Union[str, Any]				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__snake_case				)







		def        lowercase       (    self				)      ->      str:


							'''simple docstring'''

							_snake_case       :					int				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__snake_case				)







		def        lowercase       (    self				)      ->      List[str]:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*__snake_case				)







		def        lowercase       (    self				)      ->      Optional[Any]:


							'''simple docstring'''

							_snake_case       :					Tuple				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*__snake_case				)







		def        lowercase       (    self				)      ->      Union[str, Any]:


							'''simple docstring'''

							_snake_case       :					Optional[int]				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__snake_case				)







		def        lowercase       (    self				)      ->      str:


							'''simple docstring'''

							_snake_case       :					Any				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*__snake_case				)







def    A				(							UpperCAmelCase				):
					return torch.tensor(
					    UpperCAmelCase  ,  dtype=torch.long  ,  device=UpperCAmelCase  ,  )


__lowerCAmelCase :Dict     =    1E-3






@require_torch
@require_sentencepiece
@require_tokenizers
class        _a(    unittest.TestCase    ):







		@slow
		def        lowercase       (    self				)      ->      Union[str, Any]:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   MobileBertModel.from_pretrained("google/mobilebert-uncased"				).to(__snake_case				)
							_snake_case       :					Any				       =   _long_tensor([[1_0_1, 7_1_1_0, 1_0_0_5, 1_0_5_6, 2_0_2_3, 1_1_3_3_3, 1_7_4_1_3, 1_0_2_9, 1_0_2]]				)
							with torch.no_grad():
												_snake_case       :					str				       =   model(__snake_case				)[0]
							_snake_case       :					Any				       =   torch.Size((1, 9, 5_1_2)				)
							self.assertEqual(output.shape ,						__snake_case				)
							_snake_case       :					List[str]				       =   torch.tensor(
							    [
							        [
							            [-2.4_736_526E07, 8.2_691_656E04, 1.6_521_838E05],
							            [-5.7_541_704E-01, 3.9_056_022E00, 4.4_011_507E00],
							            [2.6_047_359E00, 1.5_677_652E00, -1.7_324_188E-01],
							        ]
							    ] ,						device=__snake_case ,						)

							# MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
							# ~1 difference, it's therefore not a good idea to measure using addition.
							# Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
							# result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
							_snake_case       :					Tuple				       =   torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE				)
							_snake_case       :					Tuple				       =   torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE				)

							self.assertTrue(lower_bound and upper_bound				)






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from .config import config_command_parser
from .config_args import default_config_file, load_config_from_file  # noqa: F401
from .default import default_command_parser
from .update import update_command_parser







def    A				(							UpperCAmelCase=None				):
					_snake_case       :					Union[str, Any]				       =   argparse.ArgumentParser(add_help=UpperCAmelCase  ,  allow_abbrev=UpperCAmelCase				)
					# The main config parser
					_snake_case       :					Tuple				       =   config_command_parser(UpperCAmelCase				)
					# The subparser to add commands to
					_snake_case       :					Any				       =   config_parser.add_subparsers(title="subcommands"  ,  dest="subcommand"				)

					# Then add other parsers with the parent parser
					default_command_parser(UpperCAmelCase  ,  parents=[parent_parser]				)
					update_command_parser(UpperCAmelCase  ,  parents=[parent_parser]				)

					return config_parser







def    A				(							):
					_snake_case       :					str				       =   get_config_parser()
					_snake_case       :					Union[str, Any]				       =   config_parser.parse_args()

					if not hasattr(UpperCAmelCase  ,  "func"				):
										config_parser.print_help()
										exit(1				)

					# Run
					args.func(UpperCAmelCase				)


if __name__ == "__main__":
							main()