"""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  snake_case__      :


 def __init__( self    ,		lowerCamelCase    ,		lowerCamelCase=13    ,		lowerCamelCase=7    ,		lowerCamelCase=True    ,		lowerCamelCase=True    ,		lowerCamelCase=True    ,		lowerCamelCase=True    ,		lowerCamelCase=99    ,		lowerCamelCase=64    ,		lowerCamelCase=32    ,		lowerCamelCase=5    ,		lowerCamelCase=4    ,		lowerCamelCase=37    ,		lowerCamelCase="gelu"    ,		lowerCamelCase=0.1    ,		lowerCamelCase=0.1    ,		lowerCamelCase=512    ,		lowerCamelCase=16    ,		lowerCamelCase=2    ,		lowerCamelCase=0.02    ,		lowerCamelCase=3    ,		lowerCamelCase=4    ,		lowerCamelCase=None    ,		):
        __a       =    parent
        __a       =    batch_size
        __a       =    seq_length
        __a       =    is_training
        __a       =    use_input_mask
        __a       =    use_token_type_ids
        __a       =    use_labels
        __a       =    vocab_size
        __a       =    hidden_size
        __a       =    embedding_size
        __a       =    num_hidden_layers
        __a       =    num_attention_heads
        __a       =    intermediate_size
        __a       =    hidden_act
        __a       =    hidden_dropout_prob
        __a       =    attention_probs_dropout_prob
        __a       =    max_position_embeddings
        __a       =    type_vocab_size
        __a       =    type_sequence_label_size
        __a       =    initializer_range
        __a       =    num_labels
        __a       =    num_choices
        __a       =    scope


 def 						a__   ( self						):
        __a       =    ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size						)

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

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

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

        __a       =    self.get_config()

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


 def 						a__   ( self						):
        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=lowerCamelCase    ,		initializer_range=self.initializer_range    ,		)


 def 						a__   ( self    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    MobileBertModel(config=lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    model(lowerCamelCase    ,		attention_mask=lowerCamelCase    ,		token_type_ids=lowerCamelCase						)
        __a       =    model(lowerCamelCase    ,		token_type_ids=lowerCamelCase						)
        __a       =    model(lowerCamelCase						)

        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    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    MobileBertForMaskedLM(config=lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    model(lowerCamelCase    ,		attention_mask=lowerCamelCase    ,		token_type_ids=lowerCamelCase    ,		labels=lowerCamelCase						)
        self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)						)


 def 						a__   ( self    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    MobileBertForNextSentencePrediction(config=lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    model(
            lowerCamelCase    ,		attention_mask=lowerCamelCase    ,		token_type_ids=lowerCamelCase    ,		labels=lowerCamelCase    ,		)
        self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, 2)						)


 def 						a__   ( self    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    MobileBertForPreTraining(config=lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    model(
            lowerCamelCase    ,		attention_mask=lowerCamelCase    ,		token_type_ids=lowerCamelCase    ,		labels=lowerCamelCase    ,		next_sentence_label=lowerCamelCase    ,		)
        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    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    MobileBertForQuestionAnswering(config=lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    model(
            lowerCamelCase    ,		attention_mask=lowerCamelCase    ,		token_type_ids=lowerCamelCase    ,		start_positions=lowerCamelCase    ,		end_positions=lowerCamelCase    ,		)
        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    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    self.num_labels
        __a       =    MobileBertForSequenceClassification(lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    model(lowerCamelCase    ,		attention_mask=lowerCamelCase    ,		token_type_ids=lowerCamelCase    ,		labels=lowerCamelCase						)
        self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)						)


 def 						a__   ( self    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    self.num_labels
        __a       =    MobileBertForTokenClassification(config=lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    model(lowerCamelCase    ,		attention_mask=lowerCamelCase    ,		token_type_ids=lowerCamelCase    ,		labels=lowerCamelCase						)
        self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.num_labels)						)


 def 						a__   ( self    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    self.num_choices
        __a       =    MobileBertForMultipleChoice(config=lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    input_ids.unsqueeze(1						).expand(-1    ,		self.num_choices    ,		-1						).contiguous()
        __a       =    token_type_ids.unsqueeze(1						).expand(-1    ,		self.num_choices    ,		-1						).contiguous()
        __a       =    input_mask.unsqueeze(1						).expand(-1    ,		self.num_choices    ,		-1						).contiguous()
        __a       =    model(
            lowerCamelCase    ,		attention_mask=lowerCamelCase    ,		token_type_ids=lowerCamelCase    ,		labels=lowerCamelCase    ,		)
        self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_choices)						)






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






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


 def 						a__   ( self    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase=False						):
        __a       =    super()._prepare_for_class(lowerCamelCase    ,		lowerCamelCase    ,		return_labels=lowerCamelCase						)

        if return_labels:
               if model_class in get_values(lowerCamelCase						):
                      __a       =    torch.zeros(
                          (self.model_tester.batch_size, self.model_tester.seq_length)    ,		dtype=torch.long    ,		device=lowerCamelCase						)
                      __a       =    torch.zeros(
                          self.model_tester.batch_size    ,		dtype=torch.long    ,		device=lowerCamelCase						)
        return inputs_dict


 def 						a__   ( self						):
        __a       =    MobileBertModelTester(self						)
        __a       =    ConfigTester(self    ,		config_class=lowerCamelCase    ,		hidden_size=37						)


 def 						a__   ( self						):
        self.config_tester.run_common_tests()


 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_model(*lowerCamelCase						)


 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCamelCase						)


 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCamelCase						)


 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCamelCase						)


 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCamelCase						)


 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCamelCase						)


 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCamelCase						)






 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCamelCase						)



def 	_lowerCamelCase(	a				):
       return torch.tensor(
           a   ,    dtype=torch.long   ,    device=a   ,    )


SCREAMING_SNAKE_CASE__:Optional[int]									=			1e-3






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


 @slow
 def 						a__   ( self						):
        __a       =    MobileBertModel.from_pretrained("google/mobilebert-uncased"						).to(lowerCamelCase						)
        __a       =    _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]						)
        with torch.no_grad():
               __a       =    model(lowerCamelCase						)[0]
        __a       =    torch.Size((1, 9, 512)						)
        self.assertEqual(output.shape    ,		lowerCamelCase						)
        __a       =    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=lowerCamelCase    ,		)

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

        self.assertTrue(lower_bound and upper_bound						)

"""simple docstring"""



def 	_lowerCamelCase(	a = 1   ,    a = 1_0_0_0				):
       __a       =    1
       __a       =    0

       for divide_by_number in range(a   ,    digit + 1				):
              __a       =    []
              __a       =    numerator
              for _ in range(1   ,    digit + 1				):
                     if now_divide in has_been_divided:
                            if longest_list_length < len(a				):
                                   __a       =    len(a				)
                                   __a       =    divide_by_number
                     else:
                            has_been_divided.append(a				)
                            __a       =    now_divide * 1_0 % divide_by_number

       return the_digit


# Tests
if __name__ == "__main__":
   import doctest

   doctest.testmod()
