



"""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 				lowercase__       (   SCREAMING_SNAKE_CASE    , SCREAMING_SNAKE_CASE    , unittest.TestCase				):







       '''simple docstring'''


       UpperCamelCase						=					(
           (
               TFMobileBertModel,
               TFMobileBertForMaskedLM,
               TFMobileBertForNextSentencePrediction,
               TFMobileBertForPreTraining,
               TFMobileBertForQuestionAnswering,
               TFMobileBertForSequenceClassification,
               TFMobileBertForTokenClassification,
               TFMobileBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       UpperCamelCase						=					(
           {
               '''feature-extraction''': TFMobileBertModel,
               '''fill-mask''': TFMobileBertForMaskedLM,
               '''question-answering''': TFMobileBertForQuestionAnswering,
               '''text-classification''': TFMobileBertForSequenceClassification,
               '''token-classification''': TFMobileBertForTokenClassification,
               '''zero-shot''': TFMobileBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       UpperCamelCase						=					False
       UpperCamelCase						=					False



       def 					lowercase__    (							self      :  int		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Optional[int]=False   )			->						Union[str, Any]:


            '''simple docstring'''




            UpperCAmelCase_      			=							super()._prepare_for_class(_UpperCAmelCase		,    _UpperCAmelCase		,    return_labels=_UpperCAmelCase   )

            if return_labels:
                 if model_class in get_values(_UpperCAmelCase   ):
                      UpperCAmelCase_      			=							tf.zeros(self.model_tester.batch_size		,    dtype=tf.intaa   )

            return inputs_dict
       class 				lowercase__       (   SCREAMING_SNAKE_CASE				):







              '''simple docstring'''


              def __init__(							self      :  Optional[Any]		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Dict=13		,    _UpperCAmelCase      :  str=7		,    _UpperCAmelCase      :  Union[str, Any]=True		,    _UpperCAmelCase      :  Any=True		,    _UpperCAmelCase      :  int=True		,    _UpperCAmelCase      :  Optional[Any]=True		,    _UpperCAmelCase      :  Dict=99		,    _UpperCAmelCase      :  Tuple=32		,    _UpperCAmelCase      :  Dict=32		,    _UpperCAmelCase      :  List[Any]=2		,    _UpperCAmelCase      :  Any=4		,    _UpperCAmelCase      :  Union[str, Any]=37		,    _UpperCAmelCase      :  Optional[Any]="gelu"		,    _UpperCAmelCase      :  Optional[Any]=0.1		,    _UpperCAmelCase      :  Any=0.1		,    _UpperCAmelCase      :  str=512		,    _UpperCAmelCase      :  str=16		,    _UpperCAmelCase      :  Optional[int]=2		,    _UpperCAmelCase      :  Union[str, Any]=0.02		,    _UpperCAmelCase      :  List[Any]=3		,    _UpperCAmelCase      :  Optional[int]=4		,    _UpperCAmelCase      :  Union[str, Any]=None		,    )			->						Optional[int]:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							parent
                   UpperCAmelCase_      			=							batch_size
                   UpperCAmelCase_      			=							seq_length
                   UpperCAmelCase_      			=							is_training
                   UpperCAmelCase_      			=							use_input_mask
                   UpperCAmelCase_      			=							use_token_type_ids
                   UpperCAmelCase_      			=							use_labels
                   UpperCAmelCase_      			=							vocab_size
                   UpperCAmelCase_      			=							hidden_size
                   UpperCAmelCase_      			=							num_hidden_layers
                   UpperCAmelCase_      			=							num_attention_heads
                   UpperCAmelCase_      			=							intermediate_size
                   UpperCAmelCase_      			=							hidden_act
                   UpperCAmelCase_      			=							hidden_dropout_prob
                   UpperCAmelCase_      			=							attention_probs_dropout_prob
                   UpperCAmelCase_      			=							max_position_embeddings
                   UpperCAmelCase_      			=							type_vocab_size
                   UpperCAmelCase_      			=							type_sequence_label_size
                   UpperCAmelCase_      			=							initializer_range
                   UpperCAmelCase_      			=							num_labels
                   UpperCAmelCase_      			=							num_choices
                   UpperCAmelCase_      			=							scope
                   UpperCAmelCase_      			=							embedding_size



              def 					lowercase__    (							self      :  List[str]   )			->						Optional[int]:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							ids_tensor([self.batch_size, self.seq_length]		,    self.vocab_size   )

                   UpperCAmelCase_      			=							None
                   if self.use_input_mask:
                        UpperCAmelCase_      			=							random_attention_mask([self.batch_size, self.seq_length]   )

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

                   UpperCAmelCase_      			=							None
                   UpperCAmelCase_      			=							None
                   UpperCAmelCase_      			=							None
                   if self.use_labels:
                        UpperCAmelCase_      			=							ids_tensor([self.batch_size]		,    self.type_sequence_label_size   )
                        UpperCAmelCase_      			=							ids_tensor([self.batch_size, self.seq_length]		,    self.num_labels   )
                        UpperCAmelCase_      			=							ids_tensor([self.batch_size]		,    self.num_choices   )

                   UpperCAmelCase_      			=							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 					lowercase__    (							self      :  str		,    _UpperCAmelCase      :  Union[str, Any]		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Dict		,    _UpperCAmelCase      :  Dict		,    _UpperCAmelCase      :  List[str]   )			->						str:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							TFMobileBertModel(config=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )

                   UpperCAmelCase_      			=							[input_ids, input_mask]
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )

                   UpperCAmelCase_      			=							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 					lowercase__    (							self      :  Any		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  List[str]		,    _UpperCAmelCase      :  str		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Union[str, Any]		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  Tuple   )			->						Optional[Any]:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							TFMobileBertForMaskedLM(config=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )
                   self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.seq_length, self.vocab_size)   )



              def 					lowercase__    (							self      :  Tuple		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  Optional[Any]   )			->						str:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							TFMobileBertForNextSentencePrediction(config=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )
                   self.parent.assertEqual(result.logits.shape		,    (self.batch_size, 2)   )



              def 					lowercase__    (							self      :  Union[str, Any]		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  int   )			->						Optional[int]:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							TFMobileBertForPreTraining(config=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                   UpperCAmelCase_      			=							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 					lowercase__    (							self      :  Tuple		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Optional[Any]		,    _UpperCAmelCase      :  List[str]		,    _UpperCAmelCase      :  Optional[Any]		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  List[str]		,    _UpperCAmelCase      :  Optional[Any]   )			->						Optional[Any]:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							self.num_labels
                   UpperCAmelCase_      			=							TFMobileBertForSequenceClassification(config=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )
                   self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.num_labels)   )



              def 					lowercase__    (							self      :  Any		,    _UpperCAmelCase      :  str		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  Dict		,    _UpperCAmelCase      :  Optional[Any]		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Tuple   )			->						Tuple:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							self.num_choices
                   UpperCAmelCase_      			=							TFMobileBertForMultipleChoice(config=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							tf.tile(tf.expand_dims(_UpperCAmelCase		,    1   )		,    (1, self.num_choices, 1)   )
                   UpperCAmelCase_      			=							tf.tile(tf.expand_dims(_UpperCAmelCase		,    1   )		,    (1, self.num_choices, 1)   )
                   UpperCAmelCase_      			=							tf.tile(tf.expand_dims(_UpperCAmelCase		,    1   )		,    (1, self.num_choices, 1)   )
                   UpperCAmelCase_      			=							{
                       "input_ids": multiple_choice_inputs_ids,
                       "attention_mask": multiple_choice_input_mask,
                       "token_type_ids": multiple_choice_token_type_ids,
                   }
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )
                   self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.num_choices)   )



              def 					lowercase__    (							self      :  Optional[int]		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Dict		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  int   )			->						int:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							self.num_labels
                   UpperCAmelCase_      			=							TFMobileBertForTokenClassification(config=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )
                   self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.seq_length, self.num_labels)   )



              def 					lowercase__    (							self      :  List[str]		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  List[str]		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  Dict		,    _UpperCAmelCase      :  Union[str, Any]   )			->						List[str]:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							TFMobileBertForQuestionAnswering(config=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                   UpperCAmelCase_      			=							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 					lowercase__    (							self      :  List[str]   )			->						Any:


                   '''simple docstring'''




                   UpperCAmelCase_      			=							self.prepare_config_and_inputs()
                   (
                       (
                       UpperCAmelCase_   
                   )			,   (
                       UpperCAmelCase_   
                   )			,   (
                       UpperCAmelCase_   
                   )			,   (
                       UpperCAmelCase_   
                   )			,   (
                       UpperCAmelCase_   
                   )			,   (
                       UpperCAmelCase_   
                   )			,   (
                       UpperCAmelCase_   
                   )			,      
                   )      			=							config_and_inputs
                   UpperCAmelCase_      			=							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
                   return config, inputs_dict



       def 					lowercase__    (							self      :  List[Any]   )			->						Optional[Any]:


            '''simple docstring'''




            UpperCAmelCase_      			=							TFMobileBertModelTest.TFMobileBertModelTester(self   )
            UpperCAmelCase_      			=							ConfigTester(self		,    config_class=_UpperCAmelCase		,    hidden_size=37   )



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


            '''simple docstring'''




            self.config_tester.run_common_tests()



       def 					lowercase__    (							self      :  int   )			->						List[Any]:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_model(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  Optional[int]   )			->						Tuple:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_masked_lm(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  str   )			->						Dict:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_UpperCAmelCase   )



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


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  int   )			->						List[str]:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_pretraining(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  Optional[Any]   )			->						Optional[int]:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_question_answering(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  str   )			->						int:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  Tuple   )			->						Optional[int]:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_token_classification(*_UpperCAmelCase   )







       @slow
       def 					lowercase__    (							self      :  List[str]   )			->						str:


            '''simple docstring'''




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



@require_tf
class 				lowercase__       (   unittest.TestCase				):







       '''simple docstring'''


       @slow
       def 					lowercase__    (							self      :  Optional[int]   )			->						Optional[Any]:


            '''simple docstring'''




            UpperCAmelCase_      			=							TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased"   )
            UpperCAmelCase_      			=							tf.constant([[0, 1, 2, 3, 4, 5]]   )
            UpperCAmelCase_      			=							model(_UpperCAmelCase   )[0]

            UpperCAmelCase_      			=							[1, 6, 30522]
            self.assertEqual(output.shape		,    _UpperCAmelCase   )

            UpperCAmelCase_      			=							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 unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
  import torch

  from transformers import XLMRobertaModel
@require_sentencepiece
@require_tokenizers
@require_torch
class 				lowercase__       (   unittest.TestCase				):







       '''simple docstring'''


       @slow
       def 					lowercase__    (							self      :  Tuple   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							XLMRobertaModel.from_pretrained("xlm-roberta-base"   )
             UpperCAmelCase_      			=							torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]   )
             # The dog is cute and lives in the garden house

             UpperCAmelCase_      			=							torch.Size((1, 12, 768)   )  # batch_size, sequence_length, embedding_vector_dim
             UpperCAmelCase_      			=							torch.tensor(
                 [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]]   )
             #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
             #  xlmr.eval()
             #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
             with torch.no_grad():
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )["last_hidden_state"].detach()
             self.assertEqual(output.shape		,    _UpperCAmelCase   )
             # compare the actual values for a slice of last dim
             self.assertTrue(torch.allclose(output[:, :, -1]		,    _UpperCAmelCase		,    atol=1e-3   )   )







       @slow
       def 					lowercase__    (							self      :  List[Any]   )			->						Optional[int]:


             '''simple docstring'''




             UpperCAmelCase_      			=							XLMRobertaModel.from_pretrained("xlm-roberta-large"   )
             UpperCAmelCase_      			=							torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]   )
             # The dog is cute and lives in the garden house

             UpperCAmelCase_      			=							torch.Size((1, 12, 1024)   )  # batch_size, sequence_length, embedding_vector_dim
             UpperCAmelCase_      			=							torch.tensor(
                 [[-0.0699, -0.0318, 0.0705, -0.1241, 0.0999, -0.0520, 0.1004, -0.1838, -0.4704, 0.1437, 0.0821, 0.0126]]   )
             #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
             #  xlmr.eval()
             #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
             with torch.no_grad():
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )["last_hidden_state"].detach()
             self.assertEqual(output.shape		,    _UpperCAmelCase   )
             # compare the actual values for a slice of last dim
             self.assertTrue(torch.allclose(output[:, :, -1]		,    _UpperCAmelCase		,    atol=1e-3   )   )




