


"""simple docstring"""



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 _lowerCAmelCase    :







   def __init__(     self						,     UpperCamelCase__						,     UpperCamelCase__=13						,     UpperCamelCase__=7						,     UpperCamelCase__=True						,     UpperCamelCase__=True						,     UpperCamelCase__=True						,     UpperCamelCase__=True						,     UpperCamelCase__=99						,     UpperCamelCase__=64						,     UpperCamelCase__=32						,     UpperCamelCase__=5						,     UpperCamelCase__=4						,     UpperCamelCase__=37						,     UpperCamelCase__="gelu"						,     UpperCamelCase__=0.1						,     UpperCamelCase__=0.1						,     UpperCamelCase__=512						,     UpperCamelCase__=16						,     UpperCamelCase__=2						,     UpperCamelCase__=0.02						,     UpperCamelCase__=3						,     UpperCamelCase__=4						,     UpperCamelCase__=None						,     )     ->		Optional[Any]:

       '''simple docstring'''




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







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

       '''simple docstring'''




       snake_case   :				Optional[int]				 =   ids_tensor([self.batch_size, self.seq_length]						,     self.vocab_size						)

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

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

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

       snake_case   :				Dict				 =   self.get_config()

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







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''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=UpperCamelCase__						,     initializer_range=self.initializer_range						,     )







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   MobileBertModel(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				str				 =   model(UpperCamelCase__						,     attention_mask=UpperCamelCase__						,     token_type_ids=UpperCamelCase__						)
       snake_case   :				Any				 =   model(UpperCamelCase__						,     token_type_ids=UpperCamelCase__						)
       snake_case   :				str				 =   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 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Any				 =   MobileBertForMaskedLM(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				Any				 =   model(UpperCamelCase__						,     attention_mask=UpperCamelCase__						,     token_type_ids=UpperCamelCase__						,     labels=UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.seq_length, self.vocab_size)						)







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Optional[Any]:

       '''simple docstring'''




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







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		str:

       '''simple docstring'''




       snake_case   :				str				 =   MobileBertForPreTraining(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				List[Any]				 =   model(
           UpperCamelCase__						,     attention_mask=UpperCamelCase__						,     token_type_ids=UpperCamelCase__						,     labels=UpperCamelCase__						,     next_sentence_label=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 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		int:

       '''simple docstring'''




       snake_case   :				int				 =   MobileBertForQuestionAnswering(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				List[Any]				 =   model(
           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						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				str				 =   self.num_labels
       snake_case   :				List[str]				 =   MobileBertForSequenceClassification(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				Union[str, Any]				 =   model(UpperCamelCase__						,     attention_mask=UpperCamelCase__						,     token_type_ids=UpperCamelCase__						,     labels=UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.num_labels)						)







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Any:

       '''simple docstring'''




       snake_case   :				Any				 =   self.num_labels
       snake_case   :				Optional[int]				 =   MobileBertForTokenClassification(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				Dict				 =   model(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]:

       '''simple docstring'''




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







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

       '''simple docstring'''




       snake_case   :				Any				 =   self.prepare_config_and_inputs()
       (
           (
           snake_case
       ) ,(
           snake_case
       ) ,(
           snake_case
       ) ,(
           snake_case
       ) ,(
           snake_case
       ) ,(
           snake_case
       ) ,(
           snake_case
       ) ,
       )   :				Union[str, Any]				 =   config_and_inputs
       snake_case   :				Optional[int]				 =   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict







@require_torch
class _lowerCAmelCase    (				snake_case_   ,							snake_case_   ,							unittest.TestCase     ):
   __UpperCAmelCase			:	Optional[Any]							  =  (
       (
           MobileBertModel,
           MobileBertForMaskedLM,
           MobileBertForMultipleChoice,
           MobileBertForNextSentencePrediction,
           MobileBertForPreTraining,
           MobileBertForQuestionAnswering,
           MobileBertForSequenceClassification,
           MobileBertForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   __UpperCAmelCase			:	Optional[int]							  =  (
       {
           '''feature-extraction''': MobileBertModel,
           '''fill-mask''': MobileBertForMaskedLM,
           '''question-answering''': MobileBertForQuestionAnswering,
           '''text-classification''': MobileBertForSequenceClassification,
           '''token-classification''': MobileBertForTokenClassification,
           '''zero-shot''': MobileBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   __UpperCAmelCase			:	Union[str, Any]							  =  True







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__=False						)     ->		List[str]:

       '''simple docstring'''




       snake_case   :				Any				 =   super()._prepare_for_class(UpperCamelCase__						,     UpperCamelCase__						,     return_labels=UpperCamelCase__						)

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







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

       '''simple docstring'''




       snake_case   :				List[Any]				 =   MobileBertModelTester(self						)
       snake_case   :				Optional[int]				 =   ConfigTester(self						,     config_class=UpperCamelCase__						,     hidden_size=37						)







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       self.config_tester.run_common_tests()







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				Tuple				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_model(*UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       snake_case   :				List[str]				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case   :				int				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       snake_case   :				Optional[int]				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case   :				Union[str, Any]				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case   :				int				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				Optional[int]				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCamelCase__						)







def   __lowerCAmelCase						(						lowercase  :  Any			)     ->						int:


    """simple docstring"""





    return torch.tensor(
        lowercase						,							dtype=torch.long						,							device=lowercase						,							)


__snake_case             =   1E-3







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







   @slow
   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   MobileBertModel.from_pretrained("google/mobilebert-uncased"						).to(UpperCamelCase__						)
       snake_case   :				Any				 =   _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]						)
       with torch.no_grad():
           snake_case   :				Union[str, Any]				 =   model(UpperCamelCase__						)[0]
       snake_case   :				Optional[int]				 =   torch.Size((1, 9, 512)						)
       self.assertEqual(output.shape						,     UpperCamelCase__						)
       snake_case   :				int				 =   torch.tensor(
           [
               [
                   [-2.473_6526e07, 8.269_1656e04, 1.652_1838e05],
                   [-5.754_1704e-01, 3.905_6022e00, 4.401_1507e00],
                   [2.604_7359e00, 1.567_7652e00, -1.732_4188e-01],
               ]
           ]						,     device=UpperCamelCase__						,     )

       # 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   :				int				 =   torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE						)
       snake_case   :				int				 =   torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE						)

       self.assertTrue(lower_bound and upper_bound						)









"""simple docstring"""



import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class _lowerCAmelCase    (				snake_case_   ,							unittest.TestCase     ):
   __UpperCAmelCase			:	List[Any]							  =  MgpstrTokenizer
   __UpperCAmelCase			:	Optional[int]							  =  False
   __UpperCAmelCase			:	Optional[int]							  =  {}
   __UpperCAmelCase			:	List[str]							  =  False







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       super().setUp()

       # fmt: off
       snake_case   :				List[str]				 =   ["[GO]", "[s]", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
       # fmt: on
       snake_case   :				Union[str, Any]				 =   dict(zip(UpperCamelCase__						,     range(len(UpperCamelCase__						)						)						)						)

       snake_case   :				int				 =   os.path.join(self.tmpdirname						,     VOCAB_FILES_NAMES["vocab_file"]						)
       with open(self.vocab_file						,     "w"						,     encoding="utf-8"						) as fp:
           fp.write(json.dumps(UpperCamelCase__						) + "\n"						)







   def 			lowerCamelCase		(     self						,     **UpperCamelCase__						)     ->		List[str]:

       '''simple docstring'''




       return MgpstrTokenizer.from_pretrained(self.tmpdirname						,     **UpperCamelCase__						)







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				int				 =   "tester"
       snake_case   :				List[str]				 =   "tester"
       return input_text, output_text







   @unittest.skip("MGP-STR always lower cases letters."						)
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       pass







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				int				 =   self.get_tokenizers(do_lower_case=UpperCamelCase__						)
       for tokenizer in tokenizers:
           with self.subTest(F'{tokenizer.__class__.__name__}'						):
               snake_case   :				List[Any]				 =   "[SPECIAL_TOKEN]"

               tokenizer.add_special_tokens({"cls_token": special_token}						)
               snake_case   :				str				 =   tokenizer.encode([special_token]						,     add_special_tokens=UpperCamelCase__						)
               self.assertEqual(len(UpperCamelCase__						)						,     1						)

               snake_case   :				List[Any]				 =   tokenizer.decode(UpperCamelCase__						,     skip_special_tokens=UpperCamelCase__						)
               self.assertTrue(special_token not in decoded						)







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

       '''simple docstring'''




       snake_case   :				List[str]				 =   self.get_tokenizers()
       for tokenizer in tokenizers:
           with self.subTest(F'{tokenizer.__class__.__name__}'						):
               snake_case ,snake_case   :				Union[str, Any]				 =   self.get_input_output_texts(UpperCamelCase__						)

               snake_case   :				Dict				 =   tokenizer.tokenize(UpperCamelCase__						)
               snake_case   :				Tuple				 =   tokenizer.convert_tokens_to_ids(UpperCamelCase__						)
               snake_case   :				List[Any]				 =   tokenizer.encode(UpperCamelCase__						,     add_special_tokens=UpperCamelCase__						)
               self.assertListEqual(UpperCamelCase__						,     UpperCamelCase__						)

               snake_case   :				Dict				 =   tokenizer.convert_ids_to_tokens(UpperCamelCase__						)
               self.assertNotEqual(len(UpperCamelCase__						)						,     0						)
               snake_case   :				Tuple				 =   tokenizer.decode(UpperCamelCase__						)
               self.assertIsInstance(UpperCamelCase__						,     UpperCamelCase__						)

               self.assertEqual(text_a.replace(" "						,     ""						)						,     UpperCamelCase__						)







   @unittest.skip("MGP-STR tokenizer only handles one sequence."						)
   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       pass







   @unittest.skip("inputs cannot be pretokenized in MgpstrTokenizer"						)
   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       pass





