




from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
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.models.distilbert.modeling_tf_distilbert import (
       TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFDistilBertForMaskedLM,
       TFDistilBertForMultipleChoice,
       TFDistilBertForQuestionAnswering,
       TFDistilBertForSequenceClassification,
       TFDistilBertForTokenClassification,
       TFDistilBertModel,
   )





class 			lowerCamelCase_				:


 '''simple docstring'''







 def __init__(				self    :						Union[str, Any]							,							_lowerCAmelCase    :						int							,							):
       SCREAMING_SNAKE_CASE_      		=     parent
       SCREAMING_SNAKE_CASE_      		=     13
       SCREAMING_SNAKE_CASE_      		=     7
       SCREAMING_SNAKE_CASE_      		=     True
       SCREAMING_SNAKE_CASE_      		=     True
       SCREAMING_SNAKE_CASE_      		=     False
       SCREAMING_SNAKE_CASE_      		=     True
       SCREAMING_SNAKE_CASE_      		=     99
       SCREAMING_SNAKE_CASE_      		=     32
       SCREAMING_SNAKE_CASE_      		=     2
       SCREAMING_SNAKE_CASE_      		=     4
       SCREAMING_SNAKE_CASE_      		=     37
       SCREAMING_SNAKE_CASE_      		=     'gelu'
       SCREAMING_SNAKE_CASE_      		=     0.1
       SCREAMING_SNAKE_CASE_      		=     0.1
       SCREAMING_SNAKE_CASE_      		=     512
       SCREAMING_SNAKE_CASE_      		=     16
       SCREAMING_SNAKE_CASE_      		=     2
       SCREAMING_SNAKE_CASE_      		=     0.02
       SCREAMING_SNAKE_CASE_      		=     3
       SCREAMING_SNAKE_CASE_      		=     4
       SCREAMING_SNAKE_CASE_      		=     None



 def  lowerCAmelCase_    (				self    :						str		):
       SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size		)

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

       SCREAMING_SNAKE_CASE_      		=     None
       SCREAMING_SNAKE_CASE_      		=     None
       SCREAMING_SNAKE_CASE_      		=     None
       if self.use_labels:
             SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size]							,							self.type_sequence_label_size		)
             SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.seq_length]							,							self.num_labels		)
             SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size]							,							self.num_choices		)

       SCREAMING_SNAKE_CASE_      		=     DistilBertConfig(
           vocab_size=self.vocab_size							,							dim=self.hidden_size							,							n_layers=self.num_hidden_layers							,							n_heads=self.num_attention_heads							,							hidden_dim=self.intermediate_size							,							hidden_act=self.hidden_act							,							dropout=self.hidden_dropout_prob							,							attention_dropout=self.attention_probs_dropout_prob							,							max_position_embeddings=self.max_position_embeddings							,							initializer_range=self.initializer_range							,							)

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



 def  lowerCAmelCase_    (				self    :						List[Any]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Dict							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						int		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertModel(config=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}

       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     [input_ids, input_mask]

       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)

       self.parent.assertEqual(result.last_hidden_state.shape							,							(self.batch_size, self.seq_length, self.hidden_size)		)



 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						Tuple							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Dict		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForMaskedLM(config=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.vocab_size)		)



 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						int		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForQuestionAnswering(config=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {
           'input_ids': input_ids,
           'attention_mask': input_mask,
       }
       SCREAMING_SNAKE_CASE_      		=     model(_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  lowerCAmelCase_    (				self    :						Tuple							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						List[Any]							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						Optional[int]		):
       SCREAMING_SNAKE_CASE_      		=     self.num_labels
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForSequenceClassification(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels)		)



 def  lowerCAmelCase_    (				self    :						List[str]							,							_lowerCAmelCase    :						Tuple							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						int		):
       SCREAMING_SNAKE_CASE_      		=     self.num_choices
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForMultipleChoice(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     tf.tile(tf.expand_dims(_lowerCAmelCase							,							1		)							,							(1, self.num_choices, 1)		)
       SCREAMING_SNAKE_CASE_      		=     tf.tile(tf.expand_dims(_lowerCAmelCase							,							1		)							,							(1, self.num_choices, 1)		)
       SCREAMING_SNAKE_CASE_      		=     {
           'input_ids': multiple_choice_inputs_ids,
           'attention_mask': multiple_choice_input_mask,
       }
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_choices)		)



 def  lowerCAmelCase_    (				self    :						Union[str, Any]							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						str		):
       SCREAMING_SNAKE_CASE_      		=     self.num_labels
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForTokenClassification(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.num_labels)		)




 def  lowerCAmelCase_    (				self    :						int		):
       SCREAMING_SNAKE_CASE_      		=     self.prepare_config_and_inputs()
       ((SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_))      		=     config_and_inputs
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict





@require_tf
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  unittest.TestCase   ):


 '''simple docstring'''







 lowercase_							       =       (
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 lowercase_							       =       (
     {
         "feature-extraction": TFDistilBertModel,
         "fill-mask": TFDistilBertForMaskedLM,
         "question-answering": TFDistilBertForQuestionAnswering,
         "text-classification": TFDistilBertForSequenceClassification,
         "token-classification": TFDistilBertForTokenClassification,
         "zero-shot": TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 lowercase_							       =       False
 lowercase_							       =       False



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertModelTester(self		)
       SCREAMING_SNAKE_CASE_      		=     ConfigTester(self							,							config_class=_lowerCAmelCase							,							dim=37		)



 def  lowerCAmelCase_    (				self    :						int		):
       self.config_tester.run_common_tests()



 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						str		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*_lowerCAmelCase		)




 @slow
 def  lowerCAmelCase_    (				self    :						Dict		):
       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]		):
             SCREAMING_SNAKE_CASE_      		=     TFDistilBertModel.from_pretrained(_lowerCAmelCase		)
             self.assertIsNotNone(_lowerCAmelCase		)





@require_tf
class 			lowerCamelCase_				( unittest.TestCase   ):


 '''simple docstring'''







 @slow
 def  lowerCAmelCase_    (				self    :						Tuple		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertModel.from_pretrained('distilbert-base-uncased'		)
       SCREAMING_SNAKE_CASE_      		=     tf.constant([[0, 1, 2, 3, 4, 5]]		)
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)[0]

       SCREAMING_SNAKE_CASE_      		=     [1, 6, 768]
       self.assertEqual(output.shape							,							_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     tf.constant(
           [
               [
                   [0.1926_1885, -0.1373_2955, 0.411_9799],
                   [0.2215_0156, -0.0742_2661, 0.3903_7204],
                   [0.2275_6018, -0.089_6414, 0.370_1467],
               ]
           ]		)
       tf.debugging.assert_near(output[:, :3, :3]							,							_lowerCAmelCase							,							atol=1E-4		)





import warnings

from ...utils import logging
from .image_processing_mobilevit import MobileViTImageProcessor


lowerCamelCase__					:  Optional[int]      							=				logging.get_logger(__name__)





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 def __init__(				self    :						str							,							*_lowerCAmelCase    :						Tuple							,							**_lowerCAmelCase    :						int		):
       warnings.warn(
           'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'
           ' Please use MobileViTImageProcessor instead.'							,							_lowerCAmelCase							,							)
       super().__init__(*_lowerCAmelCase							,							**_lowerCAmelCase		)