

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






      '''simple docstring'''






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



          """simple docstring"""





          _snake_case			:			Optional[Any]				       =		parent
          _snake_case			:			str				       =		batch_size
          _snake_case			:			Any				       =		seq_length
          _snake_case			:			List[str]				       =		is_training
          _snake_case			:			Dict				       =		use_input_mask
          _snake_case			:			Tuple				       =		use_token_type_ids
          _snake_case			:			Optional[Any]				       =		use_labels
          _snake_case			:			List[str]				       =		vocab_size
          _snake_case			:			List[Any]				       =		hidden_size
          _snake_case			:			str				       =		embedding_size
          _snake_case			:			str				       =		num_hidden_layers
          _snake_case			:			Tuple				       =		num_attention_heads
          _snake_case			:			Tuple				       =		intermediate_size
          _snake_case			:			Union[str, Any]				       =		hidden_act
          _snake_case			:			Tuple				       =		hidden_dropout_prob
          _snake_case			:			List[str]				       =		attention_probs_dropout_prob
          _snake_case			:			Any				       =		max_position_embeddings
          _snake_case			:			Tuple				       =		type_vocab_size
          _snake_case			:			int				       =		type_sequence_label_size
          _snake_case			:			Union[str, Any]				       =		initializer_range
          _snake_case			:			Optional[int]				       =		num_labels
          _snake_case			:			Union[str, Any]				       =		num_choices
          _snake_case			:			Tuple				       =		scope




      def 						__UpperCAmelCase      (       self			:		Optional[Any] ):



          """simple docstring"""





          _snake_case			:			Optional[Any]				       =		ids_tensor([self.batch_size, self.seq_length]  ,       self.vocab_size )

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

          _snake_case			:			str				       =		None
          if self.use_token_type_ids:
              _snake_case			:			Union[str, Any]				       =		ids_tensor([self.batch_size, self.seq_length]  ,       self.type_vocab_size )

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

          _snake_case			:			str				       =		self.get_config()

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




      def 						__UpperCAmelCase      (       self			:		Optional[Any] ):



          """simple docstring"""





          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 						__UpperCAmelCase      (       self			:		List[Any]  ,       snake_case			:		int  ,       snake_case			:		Tuple  ,       snake_case			:		str  ,       snake_case			:		Any  ,       snake_case			:		Any  ,       snake_case			:		Tuple  ,       snake_case			:		List[str] ):



          """simple docstring"""





          _snake_case			:			Tuple				       =		MobileBertModel(config=__lowerCAmelCase )
          model.to(__lowerCAmelCase )
          model.eval()
          _snake_case			:			int				       =		model(__lowerCAmelCase  ,       attention_mask=__lowerCAmelCase  ,       token_type_ids=__lowerCAmelCase )
          _snake_case			:			Any				       =		model(__lowerCAmelCase  ,       token_type_ids=__lowerCAmelCase )
          _snake_case			:			Optional[Any]				       =		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 						__UpperCAmelCase      (       self			:		str  ,       snake_case			:		Optional[int]  ,       snake_case			:		Optional[Any]  ,       snake_case			:		str  ,       snake_case			:		List[str]  ,       snake_case			:		Dict  ,       snake_case			:		List[str]  ,       snake_case			:		Any ):



          """simple docstring"""





          _snake_case			:			Dict				       =		MobileBertForMaskedLM(config=__lowerCAmelCase )
          model.to(__lowerCAmelCase )
          model.eval()
          _snake_case			:			str				       =		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 						__UpperCAmelCase      (       self			:		int  ,       snake_case			:		str  ,       snake_case			:		Any  ,       snake_case			:		List[Any]  ,       snake_case			:		str  ,       snake_case			:		Dict  ,       snake_case			:		Dict  ,       snake_case			:		Union[str, Any] ):



          """simple docstring"""





          _snake_case			:			Optional[Any]				       =		MobileBertForNextSentencePrediction(config=__lowerCAmelCase )
          model.to(__lowerCAmelCase )
          model.eval()
          _snake_case			:			int				       =		model(
              __lowerCAmelCase  ,       attention_mask=__lowerCAmelCase  ,       token_type_ids=__lowerCAmelCase  ,       labels=__lowerCAmelCase  ,       )
          self.parent.assertEqual(result.logits.shape  ,       (self.batch_size, 2) )




      def 						__UpperCAmelCase      (       self			:		Optional[Any]  ,       snake_case			:		Dict  ,       snake_case			:		List[Any]  ,       snake_case			:		int  ,       snake_case			:		Optional[Any]  ,       snake_case			:		Union[str, Any]  ,       snake_case			:		Tuple  ,       snake_case			:		Tuple ):



          """simple docstring"""





          _snake_case			:			Union[str, Any]				       =		MobileBertForPreTraining(config=__lowerCAmelCase )
          model.to(__lowerCAmelCase )
          model.eval()
          _snake_case			:			Tuple				       =		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 						__UpperCAmelCase      (       self			:		int  ,       snake_case			:		Dict  ,       snake_case			:		Dict  ,       snake_case			:		str  ,       snake_case			:		List[str]  ,       snake_case			:		Dict  ,       snake_case			:		Dict  ,       snake_case			:		List[Any] ):



          """simple docstring"""





          _snake_case			:			Optional[Any]				       =		MobileBertForQuestionAnswering(config=__lowerCAmelCase )
          model.to(__lowerCAmelCase )
          model.eval()
          _snake_case			:			Any				       =		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 						__UpperCAmelCase      (       self			:		Union[str, Any]  ,       snake_case			:		int  ,       snake_case			:		str  ,       snake_case			:		Optional[int]  ,       snake_case			:		Optional[Any]  ,       snake_case			:		Optional[int]  ,       snake_case			:		Tuple  ,       snake_case			:		str ):



          """simple docstring"""





          _snake_case			:			Tuple				       =		self.num_labels
          _snake_case			:			List[Any]				       =		MobileBertForSequenceClassification(__lowerCAmelCase )
          model.to(__lowerCAmelCase )
          model.eval()
          _snake_case			:			Union[str, Any]				       =		model(__lowerCAmelCase  ,       attention_mask=__lowerCAmelCase  ,       token_type_ids=__lowerCAmelCase  ,       labels=__lowerCAmelCase )
          self.parent.assertEqual(result.logits.shape  ,       (self.batch_size, self.num_labels) )




      def 						__UpperCAmelCase      (       self			:		Any  ,       snake_case			:		int  ,       snake_case			:		Tuple  ,       snake_case			:		List[Any]  ,       snake_case			:		int  ,       snake_case			:		str  ,       snake_case			:		Union[str, Any]  ,       snake_case			:		Optional[Any] ):



          """simple docstring"""





          _snake_case			:			Union[str, Any]				       =		self.num_labels
          _snake_case			:			Optional[Any]				       =		MobileBertForTokenClassification(config=__lowerCAmelCase )
          model.to(__lowerCAmelCase )
          model.eval()
          _snake_case			:			List[Any]				       =		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 						__UpperCAmelCase      (       self			:		Union[str, Any]  ,       snake_case			:		List[str]  ,       snake_case			:		Optional[Any]  ,       snake_case			:		Optional[Any]  ,       snake_case			:		str  ,       snake_case			:		Optional[int]  ,       snake_case			:		int  ,       snake_case			:		Union[str, Any] ):



          """simple docstring"""





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




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



          """simple docstring"""





          _snake_case			:			int				       =		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			:			Optional[int]				       =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
          return config, inputs_dict



@require_torch
class       SCREAMING_SNAKE_CASE       (				lowercase_   ,lowercase_   ,unittest.TestCase       ):






      '''simple docstring'''






      SCREAMING_SNAKE_CASE__			:					str     =					(
          (
              MobileBertModel,
              MobileBertForMaskedLM,
              MobileBertForMultipleChoice,
              MobileBertForNextSentencePrediction,
              MobileBertForPreTraining,
              MobileBertForQuestionAnswering,
              MobileBertForSequenceClassification,
              MobileBertForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      SCREAMING_SNAKE_CASE__			:					Optional[Any]     =					(
          {
              'feature-extraction': MobileBertModel,
              'fill-mask': MobileBertForMaskedLM,
              'question-answering': MobileBertForQuestionAnswering,
              'text-classification': MobileBertForSequenceClassification,
              'token-classification': MobileBertForTokenClassification,
              'zero-shot': MobileBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      SCREAMING_SNAKE_CASE__			:					List[Any]     =					True




      def 						__UpperCAmelCase      (       self			:		Optional[int]  ,       snake_case			:		str  ,       snake_case			:		Optional[Any]  ,       snake_case			:		Dict=False ):



          """simple docstring"""





          _snake_case			:			Tuple				       =		super()._prepare_for_class(__lowerCAmelCase  ,       __lowerCAmelCase  ,       return_labels=__lowerCAmelCase )

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




      def 						__UpperCAmelCase      (       self			:		Tuple ):



          """simple docstring"""





          _snake_case			:			Tuple				       =		MobileBertModelTester(self )
          _snake_case			:			str				       =		ConfigTester(self  ,       config_class=__lowerCAmelCase  ,       hidden_size=37 )




      def 						__UpperCAmelCase      (       self			:		Dict ):



          """simple docstring"""





          self.config_tester.run_common_tests()




      def 						__UpperCAmelCase      (       self			:		List[str] ):



          """simple docstring"""





          _snake_case			:			str				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_model(*__lowerCAmelCase )




      def 						__UpperCAmelCase      (       self			:		List[Any] ):



          """simple docstring"""





          _snake_case			:			Union[str, Any]				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_masked_lm(*__lowerCAmelCase )




      def 						__UpperCAmelCase      (       self			:		Dict ):



          """simple docstring"""





          _snake_case			:			int				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__lowerCAmelCase )




      def 						__UpperCAmelCase      (       self			:		str ):



          """simple docstring"""





          _snake_case			:			Any				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__lowerCAmelCase )




      def 						__UpperCAmelCase      (       self			:		List[str] ):



          """simple docstring"""





          _snake_case			:			Dict				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_pretraining(*__lowerCAmelCase )




      def 						__UpperCAmelCase      (       self			:		int ):



          """simple docstring"""





          _snake_case			:			str				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_question_answering(*__lowerCAmelCase )




      def 						__UpperCAmelCase      (       self			:		Optional[int] ):



          """simple docstring"""





          _snake_case			:			List[Any]				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__lowerCAmelCase )




      def 						__UpperCAmelCase      (       self			:		Any ):



          """simple docstring"""





          _snake_case			:			int				       =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_token_classification(*__lowerCAmelCase )



def 	lowerCamelCase__							(   a__)		->     int:



    """simple docstring"""







    return torch.tensor(
        lowerCAmelCase__      ,   dtype=torch.long      ,   device=lowerCAmelCase__      ,   )


SCREAMING_SNAKE_CASE_     = 1E-3




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






      '''simple docstring'''






      @slow
      def 						__UpperCAmelCase      (       self			:		Union[str, Any] ):



          """simple docstring"""





          _snake_case			:			int				       =		MobileBertModel.from_pretrained('google/mobilebert-uncased' ).to(__lowerCAmelCase )
          _snake_case			:			List[Any]				       =		_long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]] )
          with torch.no_grad():
              _snake_case			:			Optional[int]				       =		model(__lowerCAmelCase )[0]
          _snake_case			:			Union[str, Any]				       =		torch.Size((1, 9, 512) )
          self.assertEqual(output.shape  ,       __lowerCAmelCase )
          _snake_case			:			Dict				       =		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
          _snake_case			:			Optional[Any]				       =		torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE )
          _snake_case			:			Union[str, Any]				       =		torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE )

          self.assertTrue(lower_bound and upper_bound )




"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowerCAmelCase       :			Optional[int]   	={
    """configuration_chinese_clip""": [
        """CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """ChineseCLIPConfig""",
        """ChineseCLIPOnnxConfig""",
        """ChineseCLIPTextConfig""",
        """ChineseCLIPVisionConfig""",
    ],
    """processing_chinese_clip""": ["""ChineseCLIPProcessor"""],
}

try:
       if not is_vision_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowerCAmelCase       :			List[Any]   	=["""ChineseCLIPFeatureExtractor"""]
       __lowerCAmelCase       :			List[Any]   	=["""ChineseCLIPImageProcessor"""]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowerCAmelCase       :			Tuple   	=[
           """CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
           """ChineseCLIPModel""",
           """ChineseCLIPPreTrainedModel""",
           """ChineseCLIPTextModel""",
           """ChineseCLIPVisionModel""",
       ]

if TYPE_CHECKING:
       from .configuration_chinese_clip import (
           CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
           ChineseCLIPConfig,
           ChineseCLIPOnnxConfig,
           ChineseCLIPTextConfig,
           ChineseCLIPVisionConfig,
       )
       from .processing_chinese_clip import ChineseCLIPProcessor

       try:
              if not is_vision_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_chinese_clip import (
                  CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
                  ChineseCLIPModel,
                  ChineseCLIPPreTrainedModel,
                  ChineseCLIPTextModel,
                  ChineseCLIPVisionModel,
              )

else:
       import sys

       __lowerCAmelCase       :			Optional[Any]   	=_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



