





from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import (
           TF_MODEL_FOR_PRETRAINING_MAPPING,
           TFMobileBertForMaskedLM,
           TFMobileBertForMultipleChoice,
           TFMobileBertForNextSentencePrediction,
           TFMobileBertForPreTraining,
           TFMobileBertForQuestionAnswering,
           TFMobileBertForSequenceClassification,
           TFMobileBertForTokenClassification,
           TFMobileBertModel,
       )







@require_tf
class       __A							(   lowerCamelCase__			,lowerCamelCase__			,unittest.TestCase	):




      """simple docstring"""







      UpperCAmelCase__							=   (
          (
              TFMobileBertModel,
              TFMobileBertForMaskedLM,
              TFMobileBertForNextSentencePrediction,
              TFMobileBertForPreTraining,
              TFMobileBertForQuestionAnswering,
              TFMobileBertForSequenceClassification,
              TFMobileBertForTokenClassification,
              TFMobileBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      UpperCAmelCase__							=   (
          {
              """feature-extraction""": TFMobileBertModel,
              """fill-mask""": TFMobileBertForMaskedLM,
              """question-answering""": TFMobileBertForQuestionAnswering,
              """text-classification""": TFMobileBertForSequenceClassification,
              """token-classification""": TFMobileBertForTokenClassification,
              """zero-shot""": TFMobileBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      UpperCAmelCase__							=   False
      UpperCAmelCase__							=   False


      def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__=False):



             """simple docstring"""





             _lowerCamelCase				:  Any						=		super()._prepare_for_class(a__   ,				a__   ,				return_labels=a__)

             if return_labels:
                    if model_class in get_values(a__):
                           _lowerCamelCase				:  Union[str, Any]						=		tf.zeros(self.model_tester.batch_size   ,				dtype=tf.intaa)

             return inputs_dict







      class       __A							(   lowerCamelCase__	):




            """simple docstring"""







            def __init__(		self   ,				a__   ,				a__=13   ,				a__=7   ,				a__=True   ,				a__=True   ,				a__=True   ,				a__=True   ,				a__=99   ,				a__=32   ,				a__=32   ,				a__=2   ,				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"""





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


            def 					__snake_case      (		self):



                   """simple docstring"""





                   _lowerCamelCase				:  int						=		ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size)

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

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

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

                   _lowerCamelCase				:  Optional[Any]						=		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   ,				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   ,				initializer_range=self.initializer_range   ,				embedding_size=self.embedding_size   ,				)

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


            def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



                   """simple docstring"""





                   _lowerCamelCase				:  Union[str, Any]						=		TFMobileBertModel(config=a__)
                   _lowerCamelCase				:  int						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                   _lowerCamelCase				:  Any						=		model(a__)

                   _lowerCamelCase				:  Dict						=		[input_ids, input_mask]
                   _lowerCamelCase				:  List[Any]						=		model(a__)

                   _lowerCamelCase				:  Union[str, 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 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



                   """simple docstring"""





                   _lowerCamelCase				:  Any						=		TFMobileBertForMaskedLM(config=a__)
                   _lowerCamelCase				:  str						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                   _lowerCamelCase				:  Optional[int]						=		model(a__)
                   self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size))


            def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



                   """simple docstring"""





                   _lowerCamelCase				:  Tuple						=		TFMobileBertForNextSentencePrediction(config=a__)
                   _lowerCamelCase				:  Tuple						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                   _lowerCamelCase				:  str						=		model(a__)
                   self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, 2))


            def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



                   """simple docstring"""





                   _lowerCamelCase				:  Tuple						=		TFMobileBertForPreTraining(config=a__)
                   _lowerCamelCase				:  int						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                   _lowerCamelCase				:  List[str]						=		model(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 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



                   """simple docstring"""





                   _lowerCamelCase				:  List[str]						=		self.num_labels
                   _lowerCamelCase				:  Any						=		TFMobileBertForSequenceClassification(config=a__)
                   _lowerCamelCase				:  Tuple						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                   _lowerCamelCase				:  Optional[Any]						=		model(a__)
                   self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels))


            def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



                   """simple docstring"""





                   _lowerCamelCase				:  Any						=		self.num_choices
                   _lowerCamelCase				:  Dict						=		TFMobileBertForMultipleChoice(config=a__)
                   _lowerCamelCase				:  List[str]						=		tf.tile(tf.expand_dims(a__   ,				1)   ,				(1, self.num_choices, 1))
                   _lowerCamelCase				:  Union[str, Any]						=		tf.tile(tf.expand_dims(a__   ,				1)   ,				(1, self.num_choices, 1))
                   _lowerCamelCase				:  List[Any]						=		tf.tile(tf.expand_dims(a__   ,				1)   ,				(1, self.num_choices, 1))
                   _lowerCamelCase				:  Any						=		{
                       '''input_ids''': multiple_choice_inputs_ids,
                       '''attention_mask''': multiple_choice_input_mask,
                       '''token_type_ids''': multiple_choice_token_type_ids,
                   }
                   _lowerCamelCase				:  Any						=		model(a__)
                   self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_choices))


            def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



                   """simple docstring"""





                   _lowerCamelCase				:  str						=		self.num_labels
                   _lowerCamelCase				:  List[str]						=		TFMobileBertForTokenClassification(config=a__)
                   _lowerCamelCase				:  Any						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                   _lowerCamelCase				:  List[Any]						=		model(a__)
                   self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.num_labels))


            def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



                   """simple docstring"""





                   _lowerCamelCase				:  Optional[Any]						=		TFMobileBertForQuestionAnswering(config=a__)
                   _lowerCamelCase				:  Optional[Any]						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                   _lowerCamelCase				:  str						=		model(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 					__snake_case      (		self):



                   """simple docstring"""





                   _lowerCamelCase				:  Tuple						=		self.prepare_config_and_inputs()
                   (
                       (
                       _lowerCamelCase
                   ),	(
                       _lowerCamelCase
                   ),	(
                       _lowerCamelCase
                   ),	(
                       _lowerCamelCase
                   ),	(
                       _lowerCamelCase
                   ),	(
                       _lowerCamelCase
                   ),	(
                       _lowerCamelCase
                   ),	
                   )				:  List[Any]						=		config_and_inputs
                   _lowerCamelCase				:  Union[str, Any]						=		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
                   return config, inputs_dict


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  int						=		TFMobileBertModelTest.TFMobileBertModelTester(self)
             _lowerCamelCase				:  Tuple						=		ConfigTester(self   ,				config_class=a__   ,				hidden_size=37)


      def 					__snake_case      (		self):



             """simple docstring"""





             self.config_tester.run_common_tests()


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[Any]						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_model(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Optional[Any]						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_masked_lm(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[str]						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_multiple_choice(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[Any]						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_pretraining(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Union[str, Any]						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_question_answering(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[Any]						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_sequence_classification(*a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  int						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_token_classification(*a__)



      @slow
      def 					__snake_case      (		self):



             """simple docstring"""





             for model_name in ["google/mobilebert-uncased"]:
                    _lowerCamelCase				:  int						=		TFMobileBertModel.from_pretrained(a__)
                    self.assertIsNotNone(a__)







@require_tf
class       __A							(   unittest.TestCase	):




      """simple docstring"""







      @slow
      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Tuple						=		TFMobileBertForPreTraining.from_pretrained('''google/mobilebert-uncased''')
             _lowerCamelCase				:  Optional[Any]						=		tf.constant([[0, 1, 2, 3, 4, 5]])
             _lowerCamelCase				:  int						=		model(a__)[0]

             _lowerCamelCase				:  Tuple						=		[1, 6, 3_0522]
             self.assertEqual(output.shape   ,				a__)

             _lowerCamelCase				:  Optional[Any]						=		tf.constant(
                 [
                     [
                         [-4.591_9547, -9.24_8295, -9.64_5256],
                         [-6.730_6175, -6.44_0284, -6.605_2837],
                         [-7.274_3506, -6.784_7915, -6.02_4673],
                     ]
                 ])
             tf.debugging.assert_near(output[:, :3, :3]   ,				a__   ,				atol=1e-4)








import doctest
from collections import deque

import numpy as np







class       __A							:




      """simple docstring"""







      def __init__(		self):



             """simple docstring"""





             _lowerCamelCase				:  Any						=		[2, 1, 2, -1]
             _lowerCamelCase				:  Optional[int]						=		[1, 2, 3, 4]



      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Optional[int]						=		len(self.first_signal)
             _lowerCamelCase				:  str						=		len(self.second_signal)

             _lowerCamelCase				:  str						=		max(a__   ,				a__)

             # create a zero matrix of max_length x max_length
             _lowerCamelCase				:  Optional[Any]						=		[[0] * max_length for i in range(a__)]

             # fills the smaller signal with zeros to make both signals of same length
             if length_first_signal < length_second_signal:
                    self.first_signal += [0] * (max_length - length_first_signal)
             elif length_first_signal > length_second_signal:
                    self.second_signal += [0] * (max_length - length_second_signal)
             for i in range(a__):
                    _lowerCamelCase				:  Optional[Any]						=		deque(self.second_signal)
                    rotated_signal.rotate(a__)
                    for j, item in enumerate(a__):
                           matrix[i][j] += item

        # multiply the matrix with the first signal
             _lowerCamelCase				:  List[Any]						=		np.matmul(np.transpose(a__)   ,				np.transpose(self.first_signal))

             # rounding-off to two decimal places
             return [round(a__   ,				2) for i in final_signal]


if __name__ == "__main__":
       doctest.testmod()

