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				:     int	,						lowercase_				:     List[str]	,						lowercase_				:     Any=13	,						lowercase_				:     List[str]=7	,						lowercase_				:     Union[str, Any]=True	,						lowercase_				:     Tuple=True	,						lowercase_				:     Any=True	,						lowercase_				:     Optional[int]=True	,						lowercase_				:     Any=99	,						lowercase_				:     int=64	,						lowercase_				:     int=32	,						lowercase_				:     Tuple=5	,						lowercase_				:     Optional[Any]=4	,						lowercase_				:     Optional[Any]=37	,						lowercase_				:     List[Any]="gelu"	,						lowercase_				:     Union[str, Any]=0.1	,						lowercase_				:     List[Any]=0.1	,						lowercase_				:     Any=5_12	,						lowercase_				:     Optional[int]=16	,						lowercase_				:     List[str]=2	,						lowercase_				:     Union[str, Any]=0.02	,						lowercase_				:     int=3	,						lowercase_				:     Union[str, Any]=4	,						lowercase_				:     List[Any]=None	,						)					->						str:
       lowercase__ :   List[Any]    					=				parent
       lowercase__ :   Optional[Any]    					=				batch_size
       lowercase__ :   List[Any]    					=				seq_length
       lowercase__ :   Optional[int]    					=				is_training
       lowercase__ :   Union[str, Any]    					=				use_input_mask
       lowercase__ :   Optional[int]    					=				use_token_type_ids
       lowercase__ :   Optional[Any]    					=				use_labels
       lowercase__ :   Tuple    					=				vocab_size
       lowercase__ :   List[Any]    					=				hidden_size
       lowercase__ :   Any    					=				embedding_size
       lowercase__ :   Tuple    					=				num_hidden_layers
       lowercase__ :   str    					=				num_attention_heads
       lowercase__ :   Optional[int]    					=				intermediate_size
       lowercase__ :   Tuple    					=				hidden_act
       lowercase__ :   str    					=				hidden_dropout_prob
       lowercase__ :   Dict    					=				attention_probs_dropout_prob
       lowercase__ :   int    					=				max_position_embeddings
       lowercase__ :   str    					=				type_vocab_size
       lowercase__ :   Union[str, Any]    					=				type_sequence_label_size
       lowercase__ :   str    					=				initializer_range
       lowercase__ :   int    					=				num_labels
       lowercase__ :   str    					=				num_choices
       lowercase__ :   List[str]    					=				scope






   def       __UpperCamelCase     (      self				:     Optional[int]  )					->						Tuple:
       lowercase__ :   Optional[int]    					=				ids_tensor([self.batch_size, self.seq_length]	,						self.vocab_size  )

       lowercase__ :   Dict    					=				None
       if self.use_input_mask:
           lowercase__ :   Optional[int]    					=				random_attention_mask([self.batch_size, self.seq_length]  )

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

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

       lowercase__ :   Union[str, Any]    					=				self.get_config()

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






   def       __UpperCamelCase     (      self				:     Optional[int]  )					->						Optional[int]:
       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       __UpperCamelCase     (      self				:     List[str]	,						lowercase_				:     str	,						lowercase_				:     Optional[Any]	,						lowercase_				:     List[str]	,						lowercase_				:     int	,						lowercase_				:     Optional[int]	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     Optional[int]  )					->						List[Any]:
       lowercase__ :   List[str]    					=				MobileBertModel(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   List[Any]    					=				model(lowercase_	,						attention_mask=lowercase_	,						token_type_ids=lowercase_  )
       lowercase__ :   Union[str, Any]    					=				model(lowercase_	,						token_type_ids=lowercase_  )
       lowercase__ :   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       __UpperCamelCase     (      self				:     Optional[Any]	,						lowercase_				:     Dict	,						lowercase_				:     Dict	,						lowercase_				:     int	,						lowercase_				:     Optional[int]	,						lowercase_				:     Dict	,						lowercase_				:     str	,						lowercase_				:     List[Any]  )					->						str:
       lowercase__ :   Tuple    					=				MobileBertForMaskedLM(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   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       __UpperCamelCase     (      self				:     Union[str, Any]	,						lowercase_				:     Any	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     List[Any]	,						lowercase_				:     str	,						lowercase_				:     List[Any]	,						lowercase_				:     Optional[Any]	,						lowercase_				:     int  )					->						int:
       lowercase__ :   Union[str, Any]    					=				MobileBertForNextSentencePrediction(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   List[str]    					=				model(
           lowercase_	,						attention_mask=lowercase_	,						token_type_ids=lowercase_	,						labels=lowercase_	,						)
       self.parent.assertEqual(result.logits.shape	,						(self.batch_size, 2)  )






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






   def       __UpperCamelCase     (      self				:     List[str]	,						lowercase_				:     Any	,						lowercase_				:     Any	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     Optional[int]	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     List[Any]	,						lowercase_				:     Tuple  )					->						Dict:
       lowercase__ :   int    					=				self.num_labels
       lowercase__ :   Dict    					=				MobileBertForTokenClassification(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   Dict    					=				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       __UpperCamelCase     (      self				:     Tuple	,						lowercase_				:     Any	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     List[Any]	,						lowercase_				:     Optional[Any]	,						lowercase_				:     Union[str, Any]  )					->						Union[str, Any]:
       lowercase__ :   Any    					=				self.num_choices
       lowercase__ :   Any    					=				MobileBertForMultipleChoice(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   Optional[Any]    					=				input_ids.unsqueeze(1  ).expand(-1	,						self.num_choices	,						-1  ).contiguous()
       lowercase__ :   int    					=				token_type_ids.unsqueeze(1  ).expand(-1	,						self.num_choices	,						-1  ).contiguous()
       lowercase__ :   Optional[int]    					=				input_mask.unsqueeze(1  ).expand(-1	,						self.num_choices	,						-1  ).contiguous()
       lowercase__ :   Optional[int]    					=				model(
           lowercase_	,						attention_mask=lowercase_	,						token_type_ids=lowercase_	,						labels=lowercase_	,						)
       self.parent.assertEqual(result.logits.shape	,						(self.batch_size, self.num_choices)  )







   def       __UpperCamelCase     (      self				:     Union[str, Any]  )					->						Dict:
       lowercase__ :   Dict    					=				self.prepare_config_and_inputs()
       (
           (
           lowercase__
       )					,   (
           lowercase__
       )					,   (
           lowercase__
       )					,   (
           lowercase__
       )					,   (
           lowercase__
       )					,   (
           lowercase__
       )					,   (
           lowercase__
       )					,   
       ) :   Any    					=				config_and_inputs
       lowercase__ :   str    					=				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict



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






   def       __UpperCamelCase     (      self				:     Dict	,						lowercase_				:     List[str]	,						lowercase_				:     Any	,						lowercase_				:     Dict=False  )					->						Any:
       lowercase__ :   str    					=				super()._prepare_for_class(lowercase_	,						lowercase_	,						return_labels=lowercase_  )

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






   def       __UpperCamelCase     (      self				:     Any  )					->						Dict:
       lowercase__ :   Optional[int]    					=				MobileBertModelTester(self  )
       lowercase__ :   Union[str, Any]    					=				ConfigTester(self	,						config_class=lowercase_	,						hidden_size=37  )






   def       __UpperCamelCase     (      self				:     Optional[Any]  )					->						Tuple:
       self.config_tester.run_common_tests()






   def       __UpperCamelCase     (      self				:     int  )					->						Tuple:
       lowercase__ :   List[Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_model(*lowercase_  )






   def       __UpperCamelCase     (      self				:     str  )					->						Optional[Any]:
       lowercase__ :   List[str]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Dict  )					->						Dict:
       lowercase__ :   Optional[int]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Any  )					->						Any:
       lowercase__ :   int    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Dict  )					->						Tuple:
       lowercase__ :   Dict    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Optional[Any]  )					->						int:
       lowercase__ :   Optional[Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Optional[int]  )					->						Optional[int]:
       lowercase__ :   List[Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase_  )







   def       __UpperCamelCase     (      self				:     Dict  )					->						int:
       lowercase__ :   List[Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase_  )



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


UpperCamelCase  							=						1E-3



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






   @slow
   def       __UpperCamelCase     (      self				:     List[Any]  )					->						Any:
       lowercase__ :   Dict    					=				MobileBertModel.from_pretrained("google/mobilebert-uncased"  ).to(lowercase_  )
       lowercase__ :   Dict    					=				_long_tensor([[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]]  )
       with torch.no_grad():
           lowercase__ :   Optional[int]    					=				model(lowercase_  )[0]
       lowercase__ :   str    					=				torch.Size((1, 9, 5_12)  )
       self.assertEqual(output.shape	,						lowercase_  )
       lowercase__ :   Optional[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
       lowercase__ :   Tuple    					=				torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE  )
       lowercase__ :   Optional[int]    					=				torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE  )

       self.assertTrue(lower_bound and upper_bound  )







"""simple docstring"""







import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY


@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class       SCREAMING_SNAKE_CASE__				(		unittest.TestCase				):
       """simple docstring"""


       a  :				int   =MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING

       def        lowercase__							(						self		,     snake_case__		,     snake_case__		,     snake_case__						):






         """simple docstring"""





         lowerCAmelCase     :     Tuple								=							hf_hub_download(
             repo_id="nateraw/video-demo"		,     filename="archery.mp4"		,     repo_type="dataset"						)
         lowerCAmelCase     :     Dict								=							VideoClassificationPipeline(model=snake_case__		,     image_processor=snake_case__		,     top_k=2						)
         lowerCAmelCase     :     Any								=							[
             example_video_filepath,
             "https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4",
         ]
         return video_classifier, examples

       def        lowercase__							(						self		,     snake_case__		,     snake_case__						):






         """simple docstring"""





         for example in examples:
           lowerCAmelCase     :     str								=							video_classifier(snake_case__						)

           self.assertEqual(
               snake_case__		,     [
                   {"score": ANY(snake_case__						), "label": ANY(snake_case__						)},
                   {"score": ANY(snake_case__						), "label": ANY(snake_case__						)},
               ]		,     )

       @require_torch
       def        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase     :     Union[str, Any]								=							"hf-internal-testing/tiny-random-VideoMAEForVideoClassification"
         lowerCAmelCase     :     str								=							VideoMAEFeatureExtractor(
             size={"shortest_edge": 10}		,     crop_size={"height": 10, "width": 10}						)
         lowerCAmelCase     :     int								=							pipeline(
             "video-classification"		,     model=snake_case__		,     feature_extractor=snake_case__		,     frame_sampling_rate=4						)

         lowerCAmelCase     :     Optional[int]								=							hf_hub_download(repo_id="nateraw/video-demo"		,     filename="archery.mp4"		,     repo_type="dataset"						)
         lowerCAmelCase     :     Union[str, Any]								=							video_classifier(snake_case__		,     top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,     decimals=4						)		,     [{"score": 0.5199, "label": "LABEL_0"}, {"score": 0.4801, "label": "LABEL_1"}]		,     )

         lowerCAmelCase     :     Tuple								=							video_classifier(
             [
                 video_file_path,
                 video_file_path,
             ]		,     top_k=2		,     )
         self.assertEqual(
             nested_simplify(snake_case__		,     decimals=4						)		,     [
                 [{"score": 0.5199, "label": "LABEL_0"}, {"score": 0.4801, "label": "LABEL_1"}],
                 [{"score": 0.5199, "label": "LABEL_0"}, {"score": 0.4801, "label": "LABEL_1"}],
             ]		,     )







       @require_tf
       def        lowercase__							(						self						):






         """simple docstring"""





         pass





