





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    A__  :





      def __init__(					self			,				A_			,				A_=13			,				A_=7			,				A_=True			,				A_=True			,				A_=True			,				A_=True			,				A_=99			,				A_=64			,				A_=32			,				A_=5			,				A_=4			,				A_=37			,				A_="gelu"			,				A_=0.1			,				A_=0.1			,				A_=512			,				A_=16			,				A_=2			,				A_=0.02			,				A_=3			,				A_=4			,				A_=None			,				):
       '''simple docstring'''




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





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




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

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

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

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

       UpperCamelCase      :   Optional[Any]			      =  self.get_config()

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





      def 			__UpperCamelCase(					self  ):
       '''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=A_			,				initializer_range=self.initializer_range			,				)





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   List[str]			      =  MobileBertModel(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Any			      =  model(A_			,				attention_mask=A_			,				token_type_ids=A_  )
       UpperCamelCase      :   Dict			      =  model(A_			,				token_type_ids=A_  )
       UpperCamelCase      :   Optional[Any]			      =  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			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[int]			      =  MobileBertForMaskedLM(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   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			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[int]			      =  MobileBertForNextSentencePrediction(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   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			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Tuple			      =  MobileBertForPreTraining(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Optional[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			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[int]			      =  MobileBertForQuestionAnswering(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   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			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   List[str]			      =  self.num_labels
       UpperCamelCase      :   Dict			      =  MobileBertForSequenceClassification(A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Optional[int]			      =  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			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Tuple			      =  self.num_labels
       UpperCamelCase      :   str			      =  MobileBertForTokenClassification(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   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			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Union[str, Any]			      =  self.num_choices
       UpperCamelCase      :   int			      =  MobileBertForMultipleChoice(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   str			      =  input_ids.unsqueeze(1  ).expand(-1			,				self.num_choices			,				-1  ).contiguous()
       UpperCamelCase      :   List[Any]			      =  token_type_ids.unsqueeze(1  ).expand(-1			,				self.num_choices			,				-1  ).contiguous()
       UpperCamelCase      :   int			      =  input_mask.unsqueeze(1  ).expand(-1			,				self.num_choices			,				-1  ).contiguous()
       UpperCamelCase      :   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  ):
       '''simple docstring'''




       UpperCamelCase      :   Any			      =  self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       
       )      :   Dict			      =  config_and_inputs
       UpperCamelCase      :   Optional[int]			      =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict



@require_torch
class    A__  (			__snake_case			,  __snake_case			,  unittest.TestCase     ):
      _UpperCAmelCase      :int		       =  (
          (
              MobileBertModel,
              MobileBertForMaskedLM,
              MobileBertForMultipleChoice,
              MobileBertForNextSentencePrediction,
              MobileBertForPreTraining,
              MobileBertForQuestionAnswering,
              MobileBertForSequenceClassification,
              MobileBertForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      _UpperCAmelCase      :Optional[int]		       =  (
          {
              'feature-extraction': MobileBertModel,
              'fill-mask': MobileBertForMaskedLM,
              'question-answering': MobileBertForQuestionAnswering,
              'text-classification': MobileBertForSequenceClassification,
              'token-classification': MobileBertForTokenClassification,
              'zero-shot': MobileBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _UpperCAmelCase      :Optional[Any]		       =  True





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_=False  ):
       '''simple docstring'''




       UpperCamelCase      :   Tuple			      =  super()._prepare_for_class(A_			,				A_			,				return_labels=A_  )

       if return_labels:
        if model_class in get_values(A_  ):
         UpperCamelCase      :   Optional[int]			      =  torch.zeros(
             (self.model_tester.batch_size, self.model_tester.seq_length)			,				dtype=torch.long			,				device=A_  )
         UpperCamelCase      :   List[Any]			      =  torch.zeros(
             self.model_tester.batch_size			,				dtype=torch.long			,				device=A_  )
       return inputs_dict





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Tuple			      =  MobileBertModelTester(self  )
       UpperCamelCase      :   Tuple			      =  ConfigTester(self			,				config_class=A_			,				hidden_size=37  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Tuple			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_model(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




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





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   List[Any]			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Dict			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   List[str]			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_pretraining(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[Any]			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_question_answering(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   int			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   int			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_token_classification(*A_  )



def 						A_							(	_lowerCAmelCase	)		->					List[str]:
 return torch.tensor(
     _lowerCAmelCase				,  dtype=torch.long				,  device=_lowerCAmelCase				,  )


__lowerCamelCase			:	List[Any]					    =	1E-3



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





      @slow
      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Dict			      =  MobileBertModel.from_pretrained("google/mobilebert-uncased"  ).to(A_  )
       UpperCamelCase      :   List[Any]			      =  _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]  )
       with torch.no_grad():
        UpperCamelCase      :   Union[str, Any]			      =  model(A_  )[0]
       UpperCamelCase      :   List[str]			      =  torch.Size((1, 9, 512)  )
       self.assertEqual(output.shape			,				A_  )
       UpperCamelCase      :   Any			      =  torch.tensor(
           [
               [
                   [-2.4736526e07, 8.2691656e04, 1.6521838e05],
                   [-5.7541704e-01, 3.9056022e00, 4.4011507e00],
                   [2.6047359e00, 1.5677652e00, -1.7324188e-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
       UpperCamelCase      :   int			      =  torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE  )
       UpperCamelCase      :   List[str]			      =  torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE  )

       self.assertTrue(lower_bound and upper_bound  )







from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


__lowerCamelCase			:	List[str]					    =	logging.get_logger(__name__)



class    A__  (			__snake_case     ):
      _UpperCAmelCase      :List[str]		       =  ['input_features', 'attention_mask']





      def __init__(					self			,				A_=80			,				A_=1_6000			,				A_=80			,				A_=0.0			,				A_=True			,				A_=True			,				A_=True			,				**A_			,				):
       '''simple docstring'''




       super().__init__(feature_size=A_			,				sampling_rate=A_			,				padding_value=A_			,				**A_  )
       UpperCamelCase      :   List[Any]			      =  num_mel_bins
       UpperCamelCase      :   Any			      =  do_ceptral_normalize
       UpperCamelCase      :   int			      =  normalize_means
       UpperCamelCase      :   Tuple			      =  normalize_vars
       UpperCamelCase      :   List[Any]			      =  True





      def 			__UpperCamelCase(					self			,				A_			,				):
       '''simple docstring'''




       UpperCamelCase      :   Optional[int]			      =  waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
       UpperCamelCase      :   Optional[Any]			      =  torch.from_numpy(A_  ).unsqueeze(0  )
       UpperCamelCase      :   Dict			      =  ta_kaldi.fbank(A_			,				num_mel_bins=self.num_mel_bins			,				sample_frequency=self.sampling_rate  )
       return features.numpy()





      @staticmethod
      def 			__UpperCamelCase(					A_			,				A_			,				A_ = True			,				A_ = True			,				A_ = 0.0			,				):
       '''simple docstring'''




       if normalize_means:
        UpperCamelCase      :   str			      =  x[:input_length].mean(axis=0  )
        UpperCamelCase      :   Any			      =  np.subtract(A_			,				A_  )
       if normalize_vars:
        UpperCamelCase      :   List[str]			      =  x[:input_length].std(axis=0  )
        UpperCamelCase      :   Optional[int]			      =  np.divide(A_			,				A_  )

       if input_length < x.shape[0]:
        UpperCamelCase      :   Dict			      =  padding_value

       # make sure array is in float32
       UpperCamelCase      :   Union[str, Any]			      =  x.astype(np.floataa  )

       return x





      def 			__UpperCamelCase(					self			,				A_			,				A_ = None  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[Any]			      =  attention_mask.sum(-1  ) if attention_mask is not None else [x.shape[0] for x in input_features]
       return [
           self.utterance_cmvn(A_			,				A_			,				self.normalize_means			,				self.normalize_vars			,				self.padding_value  )
           for x, n in zip(A_			,				A_  )
       ]





      def __call__(					self			,				A_			,				A_ = False			,				A_ = None			,				A_ = False			,				A_ = None			,				A_ = None			,				A_ = None			,				A_ = None			,				**A_			,				):
       '''simple docstring'''




       if sampling_rate is not None:
        if sampling_rate != self.sampling_rate:
         raise ValueError(
             F"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of"""
             F""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with"""
             F""" {self.sampling_rate} and not {sampling_rate}."""  )
       else:
        logger.warning(
            "It is strongly recommended to pass the `sampling_rate` argument to this function. "
            "Failing to do so can result in silent errors that might be hard to debug."  )

       UpperCamelCase      :   str			      =  isinstance(A_			,				np.ndarray  ) and len(raw_speech.shape  ) > 1
       if is_batched_numpy and len(raw_speech.shape  ) > 2:
        raise ValueError(F"""Only mono-channel audio is supported for input to {self}"""  )
       UpperCamelCase      :   List[Any]			      =  is_batched_numpy or (
           isinstance(A_			,				(list, tuple)  ) and (isinstance(raw_speech[0]			,				(np.ndarray, tuple, list)  ))
       )

       if is_batched:
        UpperCamelCase      :   List[str]			      =  [np.asarray(A_			,				dtype=np.floataa  ) for speech in raw_speech]
       elif not is_batched and not isinstance(A_			,				np.ndarray  ):
        UpperCamelCase      :   int			      =  np.asarray(A_			,				dtype=np.floataa  )
       elif isinstance(A_			,				np.ndarray  ) and raw_speech.dtype is np.dtype(np.floataa  ):
        UpperCamelCase      :   Union[str, Any]			      =  raw_speech.astype(np.floataa  )

       # always return batch
       if not is_batched:
        UpperCamelCase      :   Optional[int]			      =  [raw_speech]

       # extract fbank features
       UpperCamelCase      :   Optional[int]			      =  [self._extract_fbank_features(A_  ) for waveform in raw_speech]

       # convert into correct format for padding
       UpperCamelCase      :   List[Any]			      =  BatchFeature({"input_features": features}  )

       UpperCamelCase      :   Any			      =  self.pad(
           A_			,				padding=A_			,				max_length=A_			,				truncation=A_			,				pad_to_multiple_of=A_			,				return_attention_mask=A_			,				**A_			,				)

       # make sure list is in array format
       UpperCamelCase      :   Optional[int]			      =  padded_inputs.get("input_features"  )
       if isinstance(input_features[0]			,				A_  ):
        UpperCamelCase      :   str			      =  [np.asarray(A_			,				dtype=np.floataa  ) for feature in input_features]

       UpperCamelCase      :   Optional[int]			      =  padded_inputs.get("attention_mask"  )
       if attention_mask is not None:
        UpperCamelCase      :   Optional[int]			      =  [np.asarray(A_			,				dtype=np.intaa  ) for array in attention_mask]

       # Utterance-level cepstral mean and variance normalization
       if self.do_ceptral_normalize:
        UpperCamelCase      :   List[Any]			      =  (
            np.array(A_			,				dtype=np.intaa  )
            if self._get_padding_strategies(A_			,				max_length=A_  ) is not PaddingStrategy.DO_NOT_PAD
            else None
        )
        UpperCamelCase      :   int			      =  self.normalize(
            padded_inputs["input_features"]			,				attention_mask=A_  )

       if return_tensors is not None:
        UpperCamelCase      :   List[str]			      =  padded_inputs.convert_to_tensors(A_  )

       return padded_inputs
