


'''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 				UpperCAmelCase			:


 def __init__(      self							:	List[str]			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	int=1_3			,       __lowerCamelCase							:	Union[str, Any]=7			,       __lowerCamelCase							:	Tuple=True			,       __lowerCamelCase							:	List[str]=True			,       __lowerCamelCase							:	List[str]=True			,       __lowerCamelCase							:	Dict=True			,       __lowerCamelCase							:	Dict=9_9			,       __lowerCamelCase							:	Dict=6_4			,       __lowerCamelCase							:	Union[str, Any]=3_2			,       __lowerCamelCase							:	Optional[Any]=5			,       __lowerCamelCase							:	Union[str, Any]=4			,       __lowerCamelCase							:	Any=3_7			,       __lowerCamelCase							:	List[Any]="gelu"			,       __lowerCamelCase							:	Optional[int]=0.1			,       __lowerCamelCase							:	Optional[int]=0.1			,       __lowerCamelCase							:	str=5_1_2			,       __lowerCamelCase							:	int=1_6			,       __lowerCamelCase							:	Optional[Any]=2			,       __lowerCamelCase							:	int=0.02			,       __lowerCamelCase							:	str=3			,       __lowerCamelCase							:	Any=4			,       __lowerCamelCase							:	Union[str, Any]=None			,       ):
    UpperCAmelCase__	:Dict      =      parent
    UpperCAmelCase__	:Optional[Any]      =      batch_size
    UpperCAmelCase__	:Union[str, Any]      =      seq_length
    UpperCAmelCase__	:Optional[Any]      =      is_training
    UpperCAmelCase__	:str      =      use_input_mask
    UpperCAmelCase__	:Optional[Any]      =      use_token_type_ids
    UpperCAmelCase__	:Optional[Any]      =      use_labels
    UpperCAmelCase__	:List[str]      =      vocab_size
    UpperCAmelCase__	:Optional[int]      =      hidden_size
    UpperCAmelCase__	:Optional[Any]      =      embedding_size
    UpperCAmelCase__	:Dict      =      num_hidden_layers
    UpperCAmelCase__	:str      =      num_attention_heads
    UpperCAmelCase__	:Optional[int]      =      intermediate_size
    UpperCAmelCase__	:Tuple      =      hidden_act
    UpperCAmelCase__	:Optional[Any]      =      hidden_dropout_prob
    UpperCAmelCase__	:Union[str, Any]      =      attention_probs_dropout_prob
    UpperCAmelCase__	:Optional[int]      =      max_position_embeddings
    UpperCAmelCase__	:Tuple      =      type_vocab_size
    UpperCAmelCase__	:Any      =      type_sequence_label_size
    UpperCAmelCase__	:Tuple      =      initializer_range
    UpperCAmelCase__	:Tuple      =      num_labels
    UpperCAmelCase__	:int      =      num_choices
    UpperCAmelCase__	:List[str]      =      scope


 def        __SCREAMING_SNAKE_CASE					(      self							:	Tuple ):
    UpperCAmelCase__	:int      =      ids_tensor([self.batch_size, self.seq_length]			,       self.vocab_size )

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

    UpperCAmelCase__	:Optional[Any]      =      None
    if self.use_token_type_ids:
       UpperCAmelCase__	:List[Any]      =      ids_tensor([self.batch_size, self.seq_length]			,       self.type_vocab_size )

    UpperCAmelCase__	:int      =      None
    UpperCAmelCase__	:Dict      =      None
    UpperCAmelCase__	:Tuple      =      None
    if self.use_labels:
       UpperCAmelCase__	:Dict      =      ids_tensor([self.batch_size]			,       self.type_sequence_label_size )
       UpperCAmelCase__	:str      =      ids_tensor([self.batch_size, self.seq_length]			,       self.num_labels )
       UpperCAmelCase__	:str      =      ids_tensor([self.batch_size]			,       self.num_choices )

    UpperCAmelCase__	:Tuple      =      self.get_config()

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


 def        __SCREAMING_SNAKE_CASE					(      self							:	Tuple ):
    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=lowercase_			,       initializer_range=self.initializer_range			,       )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any]			,       __lowerCamelCase							:	str			,       __lowerCamelCase							:	str			,       __lowerCamelCase							:	Optional[Any]			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	str ):
    UpperCAmelCase__	:Optional[int]      =      MobileBertModel(config=lowercase_ )
    model.to(lowercase_ )
    model.eval()
    UpperCAmelCase__	:Optional[int]      =      model(lowercase_			,       attention_mask=lowercase_			,       token_type_ids=lowercase_ )
    UpperCAmelCase__	:Tuple      =      model(lowercase_			,       token_type_ids=lowercase_ )
    UpperCAmelCase__	:Any      =      model(lowercase_ )

    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        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any]			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	str ):
    UpperCAmelCase__	:Dict      =      MobileBertForMaskedLM(config=lowercase_ )
    model.to(lowercase_ )
    model.eval()
    UpperCAmelCase__	:List[Any]      =      model(lowercase_			,       attention_mask=lowercase_			,       token_type_ids=lowercase_			,       labels=lowercase_ )
    self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.seq_length, self.vocab_size) )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Tuple			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Optional[Any]			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	int			,       __lowerCamelCase							:	str			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	List[Any] ):
    UpperCAmelCase__	:List[Any]      =      MobileBertForNextSentencePrediction(config=lowercase_ )
    model.to(lowercase_ )
    model.eval()
    UpperCAmelCase__	:str      =      model(
        lowercase_			,       attention_mask=lowercase_			,       token_type_ids=lowercase_			,       labels=lowercase_			,       )
    self.parent.assertEqual(result.logits.shape			,       (self.batch_size, 2) )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Any			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	str			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	int ):
    UpperCAmelCase__	:Dict      =      MobileBertForPreTraining(config=lowercase_ )
    model.to(lowercase_ )
    model.eval()
    UpperCAmelCase__	:Optional[Any]      =      model(
        lowercase_			,       attention_mask=lowercase_			,       token_type_ids=lowercase_			,       labels=lowercase_			,       next_sentence_label=lowercase_			,       )
    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        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any]			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	int			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	str ):
    UpperCAmelCase__	:Any      =      MobileBertForQuestionAnswering(config=lowercase_ )
    model.to(lowercase_ )
    model.eval()
    UpperCAmelCase__	:List[str]      =      model(
        lowercase_			,       attention_mask=lowercase_			,       token_type_ids=lowercase_			,       start_positions=lowercase_			,       end_positions=lowercase_			,       )
    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        __SCREAMING_SNAKE_CASE					(      self							:	List[Any]			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	List[str]			,       __lowerCamelCase							:	int ):
    UpperCAmelCase__	:List[Any]      =      self.num_labels
    UpperCAmelCase__	:Optional[Any]      =      MobileBertForSequenceClassification(lowercase_ )
    model.to(lowercase_ )
    model.eval()
    UpperCAmelCase__	:List[str]      =      model(lowercase_			,       attention_mask=lowercase_			,       token_type_ids=lowercase_			,       labels=lowercase_ )
    self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.num_labels) )


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[str]			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Union[str, Any]			,       __lowerCamelCase							:	List[Any]			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Optional[Any] ):
    UpperCAmelCase__	:Dict      =      self.num_labels
    UpperCAmelCase__	:Optional[int]      =      MobileBertForTokenClassification(config=lowercase_ )
    model.to(lowercase_ )
    model.eval()
    UpperCAmelCase__	:Optional[Any]      =      model(lowercase_			,       attention_mask=lowercase_			,       token_type_ids=lowercase_			,       labels=lowercase_ )
    self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.seq_length, self.num_labels) )


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





 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int] ):
    UpperCAmelCase__	:str      =      self.prepare_config_and_inputs()
    (
        (
        UpperCAmelCase__	
    )   ,						(
        UpperCAmelCase__	
    )   ,						(
        UpperCAmelCase__	
    )   ,						(
        UpperCAmelCase__	
    )   ,						(
        UpperCAmelCase__	
    )   ,						(
        UpperCAmelCase__	
    )   ,						(
        UpperCAmelCase__	
    )   ,							
    )	:Any      =      config_and_inputs
    UpperCAmelCase__	:List[str]      =      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict




@require_torch
class 				UpperCAmelCase			(			__a						, __a						, unittest.TestCase						):
 UpperCAmelCase				=					(
     (
         MobileBertModel,
         MobileBertForMaskedLM,
         MobileBertForMultipleChoice,
         MobileBertForNextSentencePrediction,
         MobileBertForPreTraining,
         MobileBertForQuestionAnswering,
         MobileBertForSequenceClassification,
         MobileBertForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase				=					(
     {
         "feature-extraction": MobileBertModel,
         "fill-mask": MobileBertForMaskedLM,
         "question-answering": MobileBertForQuestionAnswering,
         "text-classification": MobileBertForSequenceClassification,
         "token-classification": MobileBertForTokenClassification,
         "zero-shot": MobileBertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 UpperCAmelCase				=					True


 def        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any]			,       __lowerCamelCase							:	Optional[Any]			,       __lowerCamelCase							:	str			,       __lowerCamelCase							:	Any=False ):
    UpperCAmelCase__	:str      =      super()._prepare_for_class(lowercase_			,       lowercase_			,       return_labels=lowercase_ )

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


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[str] ):
    UpperCAmelCase__	:str      =      MobileBertModelTester(self )
    UpperCAmelCase__	:List[Any]      =      ConfigTester(self			,       config_class=lowercase_			,       hidden_size=3_7 )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Tuple ):
    self.config_tester.run_common_tests()


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):
    UpperCAmelCase__	:Union[str, Any]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_model(*lowercase_ )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any] ):
    UpperCAmelCase__	:Dict      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase_ )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    UpperCAmelCase__	:Optional[Any]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase_ )


 def        __SCREAMING_SNAKE_CASE					(      self							:	str ):
    UpperCAmelCase__	:Optional[Any]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase_ )


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):
    UpperCAmelCase__	:Union[str, Any]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase_ )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any] ):
    UpperCAmelCase__	:Union[str, Any]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase_ )


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):
    UpperCAmelCase__	:Tuple      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase_ )





 def        __SCREAMING_SNAKE_CASE					(      self							:	Any ):
    UpperCAmelCase__	:List[Any]      =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase_ )


def        a__     (					UpperCamelCase_						:      int						):
   return torch.tensor(
       SCREAMING_SNAKE_CASE_,   dtype=torch.long,   device=SCREAMING_SNAKE_CASE_,   )


__lowerCamelCase												=						1E-3




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


 @slow
 def        __SCREAMING_SNAKE_CASE					(      self							:	str ):
    UpperCAmelCase__	:List[str]      =      MobileBertModel.from_pretrained('''google/mobilebert-uncased''' ).to(lowercase_ )
    UpperCAmelCase__	:List[str]      =      _long_tensor([[1_0_1, 7_1_1_0, 1_0_0_5, 1_0_5_6, 2_0_2_3, 1_1_3_3_3, 1_7_4_1_3, 1_0_2_9, 1_0_2]] )
    with torch.no_grad():
       UpperCAmelCase__	:Tuple      =      model(lowercase_ )[0]
    UpperCAmelCase__	:Optional[Any]      =      torch.Size((1, 9, 5_1_2) )
    self.assertEqual(output.shape			,       lowercase_ )
    UpperCAmelCase__	:Union[str, Any]      =      torch.tensor(
        [
            [
                [-2.4_73_65_26e07, 8.2_69_16_56e04, 1.6_52_18_38e05],
                [-5.7_54_17_04e-01, 3.9_05_60_22e00, 4.4_01_15_07e00],
                [2.6_04_73_59e00, 1.5_67_76_52e00, -1.7_32_41_88e-01],
            ]
        ]			,       device=lowercase_			,       )

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

    self.assertTrue(lower_bound and upper_bound )




'''simple docstring'''


def   A_      (       SCREAMING_SNAKE_CASE_						)			->bool:
  if number < 0:
    raise ValueError("""number must not be negative"""						)
  return number & (number - 1) == 0


if __name__ == "__main__":
       import doctest

       doctest.testmod()

