



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	,__lowerCamelCase	,)  ->    Optional[Any]:

      """simple docstring"""






      lowerCAmelCase__  :       int				=  parent
      lowerCAmelCase__  :       List[Any]				=  13
      lowerCAmelCase__  :       Optional[Any]				=  7
      lowerCAmelCase__  :       Any				=  True
      lowerCAmelCase__  :       Any				=  True
      lowerCAmelCase__  :       List[Any]				=  False
      lowerCAmelCase__  :       str				=  True
      lowerCAmelCase__  :       str				=  99
      lowerCAmelCase__  :       List[str]				=  32
      lowerCAmelCase__  :       Optional[int]				=  2
      lowerCAmelCase__  :       Any				=  4
      lowerCAmelCase__  :       Optional[Any]				=  37
      lowerCAmelCase__  :       str				=  '''gelu'''
      lowerCAmelCase__  :       Optional[Any]				=  0.1
      lowerCAmelCase__  :       Any				=  0.1
      lowerCAmelCase__  :       Dict				=  5_12
      lowerCAmelCase__  :       List[str]				=  16
      lowerCAmelCase__  :       Union[str, Any]				=  2
      lowerCAmelCase__  :       List[str]				=  0.02
      lowerCAmelCase__  :       int				=  3
      lowerCAmelCase__  :       Optional[Any]				=  4
      lowerCAmelCase__  :       Union[str, Any]				=  None



  def 	lowerCAmelCase__   (self				)  ->    Union[str, Any]:

      """simple docstring"""






      lowerCAmelCase__  :       Dict				=  ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size				)

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

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

      lowerCAmelCase__  :       Optional[int]				=  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	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase				)  ->    List[Any]:

      """simple docstring"""






      lowerCAmelCase__  :       List[str]				=  TFDistilBertModel(config=UpperCamelCase__				)
      lowerCAmelCase__  :       Any				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}

      lowerCAmelCase__  :       Tuple				=  model(UpperCamelCase__				)

      lowerCAmelCase__  :       int				=  [input_ids, input_mask]

      lowerCAmelCase__  :       Dict				=  model(UpperCamelCase__				)

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



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase				)  ->    List[Any]:

      """simple docstring"""






      lowerCAmelCase__  :       Union[str, Any]				=  TFDistilBertForMaskedLM(config=UpperCamelCase__				)
      lowerCAmelCase__  :       Optional[Any]				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
      lowerCAmelCase__  :       int				=  model(UpperCamelCase__				)
      self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.vocab_size)				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase				)  ->    List[Any]:

      """simple docstring"""






      lowerCAmelCase__  :       Tuple				=  TFDistilBertForQuestionAnswering(config=UpperCamelCase__				)
      lowerCAmelCase__  :       Any				=  {
          '''input_ids''': input_ids,
          '''attention_mask''': input_mask,
      }
      lowerCAmelCase__  :       Tuple				=  model(UpperCamelCase__				)
      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	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase				)  ->    List[str]:

      """simple docstring"""






      lowerCAmelCase__  :       Tuple				=  self.num_labels
      lowerCAmelCase__  :       Dict				=  TFDistilBertForSequenceClassification(UpperCamelCase__				)
      lowerCAmelCase__  :       Optional[int]				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
      lowerCAmelCase__  :       List[str]				=  model(UpperCamelCase__				)
      self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_labels)				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase				)  ->    Tuple:

      """simple docstring"""






      lowerCAmelCase__  :       Dict				=  self.num_choices
      lowerCAmelCase__  :       Dict				=  TFDistilBertForMultipleChoice(UpperCamelCase__				)
      lowerCAmelCase__  :       Optional[int]				=  tf.tile(tf.expand_dims(UpperCamelCase__	,1				)	,(1, self.num_choices, 1)				)
      lowerCAmelCase__  :       str				=  tf.tile(tf.expand_dims(UpperCamelCase__	,1				)	,(1, self.num_choices, 1)				)
      lowerCAmelCase__  :       Union[str, Any]				=  {
          '''input_ids''': multiple_choice_inputs_ids,
          '''attention_mask''': multiple_choice_input_mask,
      }
      lowerCAmelCase__  :       Union[str, Any]				=  model(UpperCamelCase__				)
      self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_choices)				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase				)  ->    Union[str, Any]:

      """simple docstring"""






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



  def 	lowerCAmelCase__   (self				)  ->    Dict:

      """simple docstring"""






      lowerCAmelCase__  :       Union[str, Any]				=  self.prepare_config_and_inputs()
      ((lowerCAmelCase__)		,	(lowerCAmelCase__)		,	(lowerCAmelCase__)		,	(lowerCAmelCase__)		,	(lowerCAmelCase__)		,	(lowerCAmelCase__))  :       int				=  config_and_inputs
      lowerCAmelCase__  :       Dict				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict



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




  '''simple docstring'''



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



  def 	lowerCAmelCase__   (self				)  ->    List[Any]:

      """simple docstring"""






      lowerCAmelCase__  :       Dict				=  TFDistilBertModelTester(self				)
      lowerCAmelCase__  :       Dict				=  ConfigTester(self	,config_class=UpperCamelCase__	,dim=37				)



  def 	lowerCAmelCase__   (self				)  ->    Any:

      """simple docstring"""






      self.config_tester.run_common_tests()



  def 	lowerCAmelCase__   (self				)  ->    List[Any]:

      """simple docstring"""






      lowerCAmelCase__  :       Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__				)



  def 	lowerCAmelCase__   (self				)  ->    Tuple:

      """simple docstring"""






      lowerCAmelCase__  :       Tuple				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__				)



  def 	lowerCAmelCase__   (self				)  ->    Any:

      """simple docstring"""






      lowerCAmelCase__  :       Any				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__				)



  def 	lowerCAmelCase__   (self				)  ->    Tuple:

      """simple docstring"""






      lowerCAmelCase__  :       Any				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__				)



  def 	lowerCAmelCase__   (self				)  ->    str:

      """simple docstring"""






      lowerCAmelCase__  :       List[str]				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__				)



  def 	lowerCAmelCase__   (self				)  ->    Union[str, Any]:

      """simple docstring"""






      lowerCAmelCase__  :       Dict				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__				)



  @slow
  def 	lowerCAmelCase__   (self				)  ->    Optional[Any]:

      """simple docstring"""






      for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]				):
          lowerCAmelCase__  :       Optional[Any]				=  TFDistilBertModel.from_pretrained(UpperCamelCase__				)
          self.assertIsNotNone(UpperCamelCase__				)



@require_tf
class  lowerCamelCase__    (							unittest.TestCase):




  '''simple docstring'''



  @slow
  def 	lowerCAmelCase__   (self				)  ->    Dict:

      """simple docstring"""






      lowerCAmelCase__  :       int				=  TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''				)
      lowerCAmelCase__  :       Any				=  tf.constant([[0, 1, 2, 3, 4, 5]]				)
      lowerCAmelCase__  :       int				=  model(UpperCamelCase__				)[0]

      lowerCAmelCase__  :       List[str]				=  [1, 6, 7_68]
      self.assertEqual(output.shape	,UpperCamelCase__				)

      lowerCAmelCase__  :       Optional[int]				=  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]	,UpperCamelCase__	,atol=1e-4				)






from __future__ import annotations






def 			lowerCAmelCase__	(					lowerCamelCase_	:	list[int]):





				'''simple docstring'''

				return len(set(lowerCamelCase_)) == len(lowerCamelCase_)


if __name__ == "__main__":
							import doctest

							doctest.testmod()

