





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       :





       '''simple docstring'''







       def __init__(  self :    Any							,				A :    Optional[Any]							,				A :    List[Any]=13							,				A :    str=7							,				A :    Dict=True							,				A :    Dict=True							,				A :    str=True							,				A :    Any=True							,				A :    Optional[int]=99							,				A :    Dict=64							,				A :    Optional[Any]=32							,				A :    Union[str, Any]=5							,				A :    Any=4							,				A :    List[str]=37							,				A :    List[Any]="gelu"							,				A :    Tuple=0.1							,				A :    str=0.1							,				A :    Optional[Any]=512							,				A :    Union[str, Any]=16							,				A :    List[str]=2							,				A :    Optional[Any]=0.02							,				A :    Union[str, Any]=3							,				A :    Any=4							,				A :    str=None							,				):
         __snake_case:						List[Any]   =						parent
         __snake_case:						Any   =						batch_size
         __snake_case:						List[Any]   =						seq_length
         __snake_case:						Optional[Any]   =						is_training
         __snake_case:						List[Any]   =						use_input_mask
         __snake_case:						Optional[int]   =						use_token_type_ids
         __snake_case:						List[str]   =						use_labels
         __snake_case:						Dict   =						vocab_size
         __snake_case:						Optional[Any]   =						hidden_size
         __snake_case:						Optional[int]   =						embedding_size
         __snake_case:						Optional[Any]   =						num_hidden_layers
         __snake_case:						List[str]   =						num_attention_heads
         __snake_case:						Any   =						intermediate_size
         __snake_case:						Any   =						hidden_act
         __snake_case:						Tuple   =						hidden_dropout_prob
         __snake_case:						Dict   =						attention_probs_dropout_prob
         __snake_case:						Optional[int]   =						max_position_embeddings
         __snake_case:						Optional[Any]   =						type_vocab_size
         __snake_case:						Union[str, Any]   =						type_sequence_label_size
         __snake_case:						List[Any]   =						initializer_range
         __snake_case:						Any   =						num_labels
         __snake_case:						List[Any]   =						num_choices
         __snake_case:						Optional[int]   =						scope




       def 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						List[str]   =						ids_tensor([self.batch_size, self.seq_length]							,				self.vocab_size		)

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

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

         __snake_case:						Tuple   =						None
         __snake_case:						Union[str, Any]   =						None
         __snake_case:						Optional[Any]   =						None
         if self.use_labels:
           __snake_case:						List[Any]   =						ids_tensor([self.batch_size]							,				self.type_sequence_label_size		)
           __snake_case:						Optional[Any]   =						ids_tensor([self.batch_size, self.seq_length]							,				self.num_labels		)
           __snake_case:						int   =						ids_tensor([self.batch_size]							,				self.num_choices		)

         __snake_case:						int   =						self.get_config()

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




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         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=A							,				initializer_range=self.initializer_range							,				)




       def 							UpperCAmelCase__ (  self :    List[str]							,				A :    int							,				A :    List[str]							,				A :    Optional[Any]							,				A :    Union[str, Any]							,				A :    Any							,				A :    Any							,				A :    List[Any]		):
         __snake_case:						Optional[Any]   =						MobileBertModel(config=A		)
         model.to(A		)
         model.eval()
         __snake_case:						Dict   =						model(A							,				attention_mask=A							,				token_type_ids=A		)
         __snake_case:						Dict   =						model(A							,				token_type_ids=A		)
         __snake_case:						List[str]   =						model(A		)

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




       def 							UpperCAmelCase__ (  self :    int							,				A :    Dict							,				A :    Tuple							,				A :    Tuple							,				A :    List[str]							,				A :    Any							,				A :    str							,				A :    int		):
         __snake_case:						str   =						MobileBertForNextSentencePrediction(config=A		)
         model.to(A		)
         model.eval()
         __snake_case:						Union[str, Any]   =						model(
             A							,				attention_mask=A							,				token_type_ids=A							,				labels=A							,				)
         self.parent.assertEqual(result.logits.shape							,				(self.batch_size, 2)		)




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




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




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






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







@require_torch
class     __snake_case       (							__lowerCamelCase   ,			__lowerCamelCase   ,			unittest.TestCase			):





       '''simple docstring'''







       lowerCAmelCase__      		=     (
           (
               MobileBertModel,
               MobileBertForMaskedLM,
               MobileBertForMultipleChoice,
               MobileBertForNextSentencePrediction,
               MobileBertForPreTraining,
               MobileBertForQuestionAnswering,
               MobileBertForSequenceClassification,
               MobileBertForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       lowerCAmelCase__      		=     (
           {
               """feature-extraction""": MobileBertModel,
               """fill-mask""": MobileBertForMaskedLM,
               """question-answering""": MobileBertForQuestionAnswering,
               """text-classification""": MobileBertForSequenceClassification,
               """token-classification""": MobileBertForTokenClassification,
               """zero-shot""": MobileBertForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCAmelCase__      		=     True




       def 							UpperCAmelCase__ (  self :    List[Any]							,				A :    str							,				A :    Tuple							,				A :    Union[str, Any]=False		):
         __snake_case:						Union[str, Any]   =						super()._prepare_for_class(A							,				A							,				return_labels=A		)

         if return_labels:
           if model_class in get_values(A		):
             __snake_case:						List[Any]   =						torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.seq_length)							,				dtype=torch.long							,				device=A		)
             __snake_case:						List[str]   =						torch.zeros(
                 self.model_tester.batch_size							,				dtype=torch.long							,				device=A		)
         return inputs_dict




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						int   =						MobileBertModelTester(self		)
         __snake_case:						Tuple   =						ConfigTester(self							,				config_class=A							,				hidden_size=37		)




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         self.config_tester.run_common_tests()




       def 							UpperCAmelCase__ (  self :    Tuple		):
         __snake_case:						Union[str, Any]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_model(*A		)




       def 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						Optional[int]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_masked_lm(*A		)




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						Optional[int]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A		)




       def 							UpperCAmelCase__ (  self :    Dict		):
         __snake_case:						str   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A		)




       def 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						Optional[Any]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_pretraining(*A		)




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




       def 							UpperCAmelCase__ (  self :    Dict		):
         __snake_case:						Tuple   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A		)






       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						Optional[int]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_token_classification(*A		)







def 			A__							(    SCREAMING_SNAKE_CASE__)   ->    str:
  return torch.tensor(
      SCREAMING_SNAKE_CASE__					,					dtype=torch.long					,					device=SCREAMING_SNAKE_CASE__					,					)


__UpperCAmelCase       :   Any				       =							1e-3







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





       '''simple docstring'''







       @slow
       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						Union[str, Any]   =						MobileBertModel.from_pretrained("""google/mobilebert-uncased"""		).to(A		)
         __snake_case:						List[Any]   =						_long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]		)
         with torch.no_grad():
           __snake_case:						Dict   =						model(A		)[0]
         __snake_case:						str   =						torch.Size((1, 9, 512)		)
         self.assertEqual(output.shape							,				A		)
         __snake_case:						str   =						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=A							,				)

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

         self.assertTrue(lower_bound and upper_bound		)













def 			A__							(    SCREAMING_SNAKE_CASE__ = 200)   ->    int:
  __snake_case:						Optional[int]   =						[1, 2, 5, 10, 20, 50, 100, 200]
  __snake_case:						List[Any]   =						[0] * (pence + 1)
  __snake_case:						int   =						1  # base case: 1 way to make 0 pence

  for coin in coins:
    for i in range(SCREAMING_SNAKE_CASE__					,					pence + 1					,					1):
      number_of_ways[i] += number_of_ways[i - coin]
  return number_of_ways[pence]


if __name__ == "__main__":
      assert solution(200) == 73_682






