






"""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__ (				a_							,	a_							,	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 				a__			(		self :	Optional[Any]				,    UpperCamelCase_ :	Any				,    UpperCamelCase_ :	Any				,    UpperCamelCase_ :	Optional[int]=False ):





           '''simple docstring'''

           __magic_name__      				=    super()._prepare_for_class(UpperCamelCase_				,    UpperCamelCase_				,    return_labels=UpperCamelCase_ )

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

           return inputs_dict



    class        UpperCamelCase__ (				a_):







        """simple docstring"""







        def __init__(		self :	str				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Optional[Any]=1_3				,    UpperCamelCase_ :	str=7				,    UpperCamelCase_ :	List[str]=True				,    UpperCamelCase_ :	Union[str, Any]=True				,    UpperCamelCase_ :	Optional[Any]=True				,    UpperCamelCase_ :	Tuple=True				,    UpperCamelCase_ :	Dict=9_9				,    UpperCamelCase_ :	List[Any]=3_2				,    UpperCamelCase_ :	Tuple=3_2				,    UpperCamelCase_ :	str=2				,    UpperCamelCase_ :	Tuple=4				,    UpperCamelCase_ :	Union[str, Any]=3_7				,    UpperCamelCase_ :	Optional[Any]="gelu"				,    UpperCamelCase_ :	Optional[int]=0.1				,    UpperCamelCase_ :	str=0.1				,    UpperCamelCase_ :	Any=5_1_2				,    UpperCamelCase_ :	int=1_6				,    UpperCamelCase_ :	Union[str, Any]=2				,    UpperCamelCase_ :	List[str]=0.02				,    UpperCamelCase_ :	Optional[Any]=3				,    UpperCamelCase_ :	Any=4				,    UpperCamelCase_ :	List[str]=None				,    ):





               '''simple docstring'''

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







        def 				a__			(		self :	List[str] ):





               '''simple docstring'''

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

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

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

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

               __magic_name__      				=    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 :	str				,    UpperCamelCase_ :	Optional[Any]				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	Union[str, Any]				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	Optional[Any] ):





               '''simple docstring'''

               __magic_name__      				=    TFMobileBertModel(config=UpperCamelCase_ )
               __magic_name__      				=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
               __magic_name__      				=    model(UpperCamelCase_ )

               __magic_name__      				=    [input_ids, input_mask]
               __magic_name__      				=    model(UpperCamelCase_ )

               __magic_name__      				=    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 :	Tuple				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	Union[str, Any]				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	Union[str, Any] ):





               '''simple docstring'''

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







        def 				a__			(		self :	int				,    UpperCamelCase_ :	Tuple				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	List[Any] ):





               '''simple docstring'''

               __magic_name__      				=    TFMobileBertForNextSentencePrediction(config=UpperCamelCase_ )
               __magic_name__      				=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
               __magic_name__      				=    model(UpperCamelCase_ )
               self.parent.assertEqual(result.logits.shape				,    (self.batch_size, 2) )







        def 				a__			(		self :	int				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	Optional[Any] ):





               '''simple docstring'''

               __magic_name__      				=    TFMobileBertForPreTraining(config=UpperCamelCase_ )
               __magic_name__      				=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
               __magic_name__      				=    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 :	List[str]				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	Optional[Any]				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Optional[Any]				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Tuple ):





               '''simple docstring'''

               __magic_name__      				=    self.num_labels
               __magic_name__      				=    TFMobileBertForSequenceClassification(config=UpperCamelCase_ )
               __magic_name__      				=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
               __magic_name__      				=    model(UpperCamelCase_ )
               self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels) )







        def 				a__			(		self :	Tuple				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	List[Any] ):





               '''simple docstring'''

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







        def 				a__			(		self :	Dict				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	Union[str, Any]				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	Optional[Any] ):





               '''simple docstring'''

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







        def 				a__			(		self :	List[Any]				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	Union[str, Any]				,    UpperCamelCase_ :	Union[str, Any]				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	Any				,    UpperCamelCase_ :	int ):





               '''simple docstring'''

               __magic_name__      				=    TFMobileBertForQuestionAnswering(config=UpperCamelCase_ )
               __magic_name__      				=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
               __magic_name__      				=    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 :	Optional[Any] ):





               '''simple docstring'''

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







    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           __magic_name__      				=    TFMobileBertModelTest.TFMobileBertModelTester(self )
           __magic_name__      				=    ConfigTester(self				,    config_class=UpperCamelCase_				,    hidden_size=3_7 )







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

           self.config_tester.run_common_tests()







    def 				a__			(		self :	int ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_model(*UpperCamelCase_ )







    def 				a__			(		self :	List[Any] ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCamelCase_ )







    def 				a__			(		self :	int ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCamelCase_ )







    def 				a__			(		self :	Tuple ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCamelCase_ )







    def 				a__			(		self :	Optional[Any] ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCamelCase_ )







    def 				a__			(		self :	Union[str, Any] ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCamelCase_ )







    def 				a__			(		self :	Optional[Any] ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCamelCase_ )







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCamelCase_ )







    @slow
    def 				a__			(		self :	str ):





           '''simple docstring'''

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



@require_tf
class        UpperCamelCase__ (				unittest.TestCase):







    """simple docstring"""







    @slow
    def 				a__			(		self :	Optional[Any] ):





           '''simple docstring'''

           __magic_name__      				=    TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased' )
           __magic_name__      				=    tf.constant([[0, 1, 2, 3, 4, 5]] )
           __magic_name__      				=    model(UpperCamelCase_ )[0]

           __magic_name__      				=    [1, 6, 3_0_5_2_2]
           self.assertEqual(output.shape				,    UpperCamelCase_ )

           __magic_name__      				=    tf.constant(
               [
                   [
                       [-4.5_919_547, -9.248_295, -9.645_256],
                       [-6.7_306_175, -6.440_284, -6.6_052_837],
                       [-7.2_743_506, -6.7_847_915, -6.024_673],
                   ]
               ] )
           tf.debugging.assert_near(output[:, :3, :3]				,    UpperCamelCase_				,    atol=1e-4 )







"""simple docstring"""
import os
import tempfile
import unittest

from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter
from transformers.testing_utils import slow
from transformers.utils import cached_property



@unittest.skipUnless(os.path.exists(a_)							,	"""Tatoeba directory does not exist.""")
class        UpperCamelCase__ (				unittest.TestCase):







    """simple docstring"""







    @cached_property
    def 				a__			(		self :	int ):





           '''simple docstring'''

           __magic_name__      				=    tempfile.mkdtemp()
           return TatoebaConverter(save_dir=UpperCamelCase_ )







    @slow
    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           self.resolver.convert_models(['heb-eng'] )







    @slow
    def 				a__			(		self :	Tuple ):





           '''simple docstring'''

           __magic_name__				,  __magic_name__      				=    self.resolver.write_model_card('opus-mt-he-en'				,    dry_run=UpperCamelCase_ )
           assert mmeta["long_pair"] == "heb-eng"