


'''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 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_     ,       SCREAMING_SNAKE_CASE_     ,       unittest.TestCase						):



     """simple docstring"""






     __magic_name__           =     (
         (
             TFMobileBertModel,
             TFMobileBertForMaskedLM,
             TFMobileBertForNextSentencePrediction,
             TFMobileBertForPreTraining,
             TFMobileBertForQuestionAnswering,
             TFMobileBertForSequenceClassification,
             TFMobileBertForTokenClassification,
             TFMobileBertForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     __magic_name__           =     (
         {
             "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__           =     False
     __magic_name__           =     False







     def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__=False ):

            '''simple docstring'''

            _lowerCAmelCase     :				Any       =							super()._prepare_for_class(UpperCAmelCase_   ,						UpperCAmelCase_   ,						return_labels=UpperCAmelCase_ )

            if return_labels:
                   if model_class in get_values(UpperCAmelCase_ ):
                          _lowerCAmelCase     :				List[Any]       =							tf.zeros(self.model_tester.batch_size   ,						dtype=tf.intaa )

            return inputs_dict
     class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_						):



          """simple docstring"""






          def __init__(			self   ,						snake_case__   ,						snake_case__=13   ,						snake_case__=7   ,						snake_case__=True   ,						snake_case__=True   ,						snake_case__=True   ,						snake_case__=True   ,						snake_case__=99   ,						snake_case__=32   ,						snake_case__=32   ,						snake_case__=2   ,						snake_case__=4   ,						snake_case__=37   ,						snake_case__="gelu"   ,						snake_case__=0.1   ,						snake_case__=0.1   ,						snake_case__=512   ,						snake_case__=16   ,						snake_case__=2   ,						snake_case__=0.02   ,						snake_case__=3   ,						snake_case__=4   ,						snake_case__=None   ,						):

                 '''simple docstring'''

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







          def a						(			self ):

                 '''simple docstring'''

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

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

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

                 _lowerCAmelCase     :				Tuple       =							None
                 _lowerCAmelCase     :				Tuple       =							None
                 _lowerCAmelCase     :				Optional[Any]       =							None
                 if self.use_labels:
                        _lowerCAmelCase     :				Any       =							ids_tensor([self.batch_size]   ,						self.type_sequence_label_size )
                        _lowerCAmelCase     :				Union[str, Any]       =							ids_tensor([self.batch_size, self.seq_length]   ,						self.num_labels )
                        _lowerCAmelCase     :				Union[str, 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 a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

                 '''simple docstring'''

                 _lowerCAmelCase     :				Any       =							TFMobileBertModel(config=UpperCAmelCase_ )
                 _lowerCAmelCase     :				int       =							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                 _lowerCAmelCase     :				Any       =							model(UpperCAmelCase_ )

                 _lowerCAmelCase     :				Tuple       =							[input_ids, input_mask]
                 _lowerCAmelCase     :				Dict       =							model(UpperCAmelCase_ )

                 _lowerCAmelCase     :				List[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 a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

                 '''simple docstring'''

                 _lowerCAmelCase     :				Dict       =							TFMobileBertForMaskedLM(config=UpperCAmelCase_ )
                 _lowerCAmelCase     :				Tuple       =							{'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 a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

                 '''simple docstring'''

                 _lowerCAmelCase     :				Optional[Any]       =							TFMobileBertForNextSentencePrediction(config=UpperCAmelCase_ )
                 _lowerCAmelCase     :				Any       =							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                 _lowerCAmelCase     :				Dict       =							model(UpperCAmelCase_ )
                 self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, 2) )







          def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

                 '''simple docstring'''

                 _lowerCAmelCase     :				List[str]       =							TFMobileBertForPreTraining(config=UpperCAmelCase_ )
                 _lowerCAmelCase     :				List[str]       =							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                 _lowerCAmelCase     :				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 a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

                 '''simple docstring'''

                 _lowerCAmelCase     :				Any       =							self.num_labels
                 _lowerCAmelCase     :				Tuple       =							TFMobileBertForSequenceClassification(config=UpperCAmelCase_ )
                 _lowerCAmelCase     :				Any       =							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                 _lowerCAmelCase     :				int       =							model(UpperCAmelCase_ )
                 self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_labels) )







          def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

                 '''simple docstring'''

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







          def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

                 '''simple docstring'''

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







          def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

                 '''simple docstring'''

                 _lowerCAmelCase     :				Union[str, Any]       =							TFMobileBertForQuestionAnswering(config=UpperCAmelCase_ )
                 _lowerCAmelCase     :				Union[str, Any]       =							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                 _lowerCAmelCase     :				Tuple       =							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 a						(			self ):

                 '''simple docstring'''

                 _lowerCAmelCase     :				Union[str, Any]       =							self.prepare_config_and_inputs()
                 (
                     (
                     _lowerCAmelCase							
                 )      ,  (
                     _lowerCAmelCase							
                 )      ,  (
                     _lowerCAmelCase							
                 )      ,  (
                     _lowerCAmelCase							
                 )      ,  (
                     _lowerCAmelCase							
                 )      ,  (
                     _lowerCAmelCase							
                 )      ,  (
                     _lowerCAmelCase							
                 )      ,  							
                 )     :				Any       =							config_and_inputs
                 _lowerCAmelCase     :				str       =							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
                 return config, inputs_dict







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Dict       =							TFMobileBertModelTest.TFMobileBertModelTester(self )
            _lowerCAmelCase     :				str       =							ConfigTester(self   ,						config_class=UpperCAmelCase_   ,						hidden_size=37 )







     def a						(			self ):

            '''simple docstring'''

            self.config_tester.run_common_tests()







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[Any]       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_model(*UpperCAmelCase_ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCAmelCase_ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCAmelCase_ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCAmelCase_ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCAmelCase_ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[Any]       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCAmelCase_ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[str]       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCAmelCase_ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Any       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCAmelCase_ )







     @slow
     def a						(			self ):

            '''simple docstring'''

            for model_name in ["google/mobilebert-uncased"]:
                   _lowerCAmelCase     :				int       =							TFMobileBertModel.from_pretrained(UpperCAmelCase_ )
                   self.assertIsNotNone(UpperCAmelCase_ )
@require_tf
class 						UpperCamelCase__       (			unittest.TestCase						):



     """simple docstring"""






     @slow
     def a						(			self ):

            '''simple docstring'''

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

            _lowerCAmelCase     :				Any       =							[1, 6, 3_0522]
            self.assertEqual(output.shape   ,						UpperCAmelCase_ )

            _lowerCAmelCase     :				Optional[Any]       =							tf.constant(
                [
                    [
                        [-4.591_9547, -9.24_8295, -9.64_5256],
                        [-6.730_6175, -6.44_0284, -6.605_2837],
                        [-7.274_3506, -6.784_7915, -6.02_4673],
                    ]
                ] )
            tf.debugging.assert_near(output[:, :3, :3]   ,						UpperCAmelCase_   ,						atol=1E-4 )






'''simple docstring'''





import json
from typing import Iterator, List, Union

from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers
from tokenizers.implementations.base_tokenizer import BaseTokenizer
from tokenizers.models import Unigram
from tokenizers.processors import TemplateProcessing


class 					snake_case			(UpperCamelCase ):



       def __init__(					self  ,UpperCAmelCase_ = "▁"  ,UpperCAmelCase_ = True  ,UpperCAmelCase_ = "<unk>"  ,UpperCAmelCase_ = "</s>"  ,UpperCAmelCase_ = "<pad>"  ,)	->				Optional[int]:
           lowercase__									=    {
               "pad": {"id": 0, "token": pad_token},
               "eos": {"id": 1, "token": eos_token},
               "unk": {"id": 2, "token": unk_token},
           }

           lowercase__									=    [None] * len(self.special_tokens   )
           for token_dict in self.special_tokens.values():
               lowercase__									=    token_dict["token"]

           lowercase__									=    Tokenizer(Unigram()   )

           lowercase__									=    normalizers.Sequence(
               [
                   normalizers.Nmt(),
                   normalizers.NFKC(),
                   normalizers.Replace(Regex(" {2,}"   )  ," "   ),
                   normalizers.Lowercase(),
               ]   )
           lowercase__									=    pre_tokenizers.Sequence(
               [
                   pre_tokenizers.Metaspace(replacement=UpperCAmelCase_  ,add_prefix_space=UpperCAmelCase_   ),
                   pre_tokenizers.Digits(individual_digits=UpperCAmelCase_   ),
                   pre_tokenizers.Punctuation(),
               ]   )
           lowercase__									=    decoders.Metaspace(replacement=UpperCAmelCase_  ,add_prefix_space=UpperCAmelCase_   )

           lowercase__									=    TemplateProcessing(
               single=F'''$A {self.special_tokens['eos']['token']}'''  ,special_tokens=[(self.special_tokens["eos"]["token"], self.special_tokens["eos"]["id"])]  ,)

           lowercase__									=    {
               "model": "SentencePieceUnigram",
               "replacement": replacement,
               "add_prefix_space": add_prefix_space,
           }

           super().__init__(UpperCAmelCase_  ,UpperCAmelCase_   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_ = 8_000  ,UpperCAmelCase_ = True  ,)	->				List[str]:
           lowercase__									=    trainers.UnigramTrainer(
               vocab_size=UpperCAmelCase_  ,special_tokens=self.special_tokens_list  ,show_progress=UpperCAmelCase_  ,)

           if isinstance(UpperCAmelCase_  ,UpperCAmelCase_   ):
               lowercase__									=    [files]
           self._tokenizer.train(UpperCAmelCase_  ,trainer=UpperCAmelCase_   )

           self.add_unk_id()



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_ = 8_000  ,UpperCAmelCase_ = True  ,)	->				Union[str, Any]:
           lowercase__									=    trainers.UnigramTrainer(
               vocab_size=UpperCAmelCase_  ,special_tokens=self.special_tokens_list  ,show_progress=UpperCAmelCase_  ,)

           self._tokenizer.train_from_iterator(UpperCAmelCase_  ,trainer=UpperCAmelCase_   )

           self.add_unk_id()







       def 				_a   (					self   )	->				str:
           lowercase__									=    json.loads(self._tokenizer.to_str()   )

           lowercase__									=    self.special_tokens["unk"]["id"]

           lowercase__									=    Tokenizer.from_str(json.dumps(UpperCAmelCase_   )   )




