
"""simple docstring"""




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


     '''simple docstring'''





     def __init__(      self ,			__UpperCamelCase ,			)->					str:
          UpperCAmelCase__							: List[Any] 							=						parent
          UpperCAmelCase__							: Tuple 							=						13
          UpperCAmelCase__							: Optional[int] 							=						7
          UpperCAmelCase__							: Optional[int] 							=						True
          UpperCAmelCase__							: Dict 							=						True
          UpperCAmelCase__							: Union[str, Any] 							=						False
          UpperCAmelCase__							: str 							=						True
          UpperCAmelCase__							: str 							=						99
          UpperCAmelCase__							: Tuple 							=						32
          UpperCAmelCase__							: Optional[int] 							=						2
          UpperCAmelCase__							: List[str] 							=						4
          UpperCAmelCase__							: str 							=						37
          UpperCAmelCase__							: List[Any] 							=						"gelu"
          UpperCAmelCase__							: Union[str, Any] 							=						0.1
          UpperCAmelCase__							: Tuple 							=						0.1
          UpperCAmelCase__							: Union[str, Any] 							=						5_12
          UpperCAmelCase__							: Union[str, Any] 							=						16
          UpperCAmelCase__							: Dict 							=						2
          UpperCAmelCase__							: str 							=						0.02
          UpperCAmelCase__							: Any 							=						3
          UpperCAmelCase__							: Optional[int] 							=						4
          UpperCAmelCase__							: Dict 							=						None



     def  lowerCAmelCase__       (      self  )->					Dict:
          UpperCAmelCase__							: List[str] 							=						ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size  )

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

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

          UpperCAmelCase__							: Dict 							=						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 ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase  )->					str:
          UpperCAmelCase__							: Union[str, Any] 							=						TFDistilBertModel(config=__UpperCamelCase  )
          UpperCAmelCase__							: Union[str, Any] 							=						{"input_ids": input_ids, "attention_mask": input_mask}

          UpperCAmelCase__							: Any 							=						model(__UpperCamelCase  )

          UpperCAmelCase__							: Union[str, Any] 							=						[input_ids, input_mask]

          UpperCAmelCase__							: Optional[int] 							=						model(__UpperCamelCase  )

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



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase  )->					Dict:
          UpperCAmelCase__							: int 							=						TFDistilBertForMaskedLM(config=__UpperCamelCase  )
          UpperCAmelCase__							: Optional[int] 							=						{"input_ids": input_ids, "attention_mask": input_mask}
          UpperCAmelCase__							: Union[str, Any] 							=						model(__UpperCamelCase  )
          self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.vocab_size)  )



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



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase  )->					Any:
          UpperCAmelCase__							: List[Any] 							=						self.num_choices
          UpperCAmelCase__							: Dict 							=						TFDistilBertForMultipleChoice(__UpperCamelCase  )
          UpperCAmelCase__							: Dict 							=						tf.tile(tf.expand_dims(__UpperCamelCase ,			1  ) ,			(1, self.num_choices, 1)  )
          UpperCAmelCase__							: str 							=						tf.tile(tf.expand_dims(__UpperCamelCase ,			1  ) ,			(1, self.num_choices, 1)  )
          UpperCAmelCase__							: List[Any] 							=						{
              "input_ids": multiple_choice_inputs_ids,
              "attention_mask": multiple_choice_input_mask,
          }
          UpperCAmelCase__							: Tuple 							=						model(__UpperCamelCase  )
          self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_choices)  )



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase  )->					Tuple:
          UpperCAmelCase__							: int 							=						self.num_labels
          UpperCAmelCase__							: Union[str, Any] 							=						TFDistilBertForTokenClassification(__UpperCamelCase  )
          UpperCAmelCase__							: str 							=						{"input_ids": input_ids, "attention_mask": input_mask}
          UpperCAmelCase__							: int 							=						model(__UpperCamelCase  )
          self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.num_labels)  )



     def  lowerCAmelCase__       (      self  )->					Optional[Any]:
          UpperCAmelCase__							: Any 							=						self.prepare_config_and_inputs()
          ((UpperCAmelCase__)							,	(UpperCAmelCase__)							,	(UpperCAmelCase__)							,	(UpperCAmelCase__)							,	(UpperCAmelCase__)							,	(UpperCAmelCase__))							: Optional[Any] 							=						config_and_inputs
          UpperCAmelCase__							: Union[str, Any] 							=						{"input_ids": input_ids, "attention_mask": input_mask}
          return config, inputs_dict







@require_tf
class   _lowercase						(       lowerCAmelCase_	,   lowerCAmelCase_	,   unittest.TestCase				):


     '''simple docstring'''





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



     def  lowerCAmelCase__       (      self  )->					Optional[Any]:
          UpperCAmelCase__							: Tuple 							=						TFDistilBertModelTester(self  )
          UpperCAmelCase__							: List[Any] 							=						ConfigTester(self ,			config_class=__UpperCamelCase ,			dim=37  )



     def  lowerCAmelCase__       (      self  )->					str:
          self.config_tester.run_common_tests()



     def  lowerCAmelCase__       (      self  )->					List[str]:
          UpperCAmelCase__							: str 							=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_model(*__UpperCamelCase  )



     def  lowerCAmelCase__       (      self  )->					int:
          UpperCAmelCase__							: Dict 							=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_masked_lm(*__UpperCamelCase  )



     def  lowerCAmelCase__       (      self  )->					List[Any]:
          UpperCAmelCase__							: List[Any] 							=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_question_answering(*__UpperCamelCase  )



     def  lowerCAmelCase__       (      self  )->					Tuple:
          UpperCAmelCase__							: Union[str, Any] 							=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_sequence_classification(*__UpperCamelCase  )



     def  lowerCAmelCase__       (      self  )->					List[Any]:
          UpperCAmelCase__							: int 							=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_multiple_choice(*__UpperCamelCase  )



     def  lowerCAmelCase__       (      self  )->					Optional[int]:
          UpperCAmelCase__							: str 							=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_token_classification(*__UpperCamelCase  )



     @slow
     def  lowerCAmelCase__       (      self  )->					Optional[Any]:
          for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]  ):
               UpperCAmelCase__							: int 							=						TFDistilBertModel.from_pretrained(__UpperCamelCase  )
               self.assertIsNotNone(__UpperCamelCase  )







@require_tf
class   _lowercase						(       unittest.TestCase				):


     '''simple docstring'''





     @slow
     def  lowerCAmelCase__       (      self  )->					int:
          UpperCAmelCase__							: str 							=						TFDistilBertModel.from_pretrained("distilbert-base-uncased"  )
          UpperCAmelCase__							: List[Any] 							=						tf.constant([[0, 1, 2, 3, 4, 5]]  )
          UpperCAmelCase__							: Tuple 							=						model(__UpperCamelCase  )[0]

          UpperCAmelCase__							: Optional[int] 							=						[1, 6, 7_68]
          self.assertEqual(output.shape ,			__UpperCamelCase  )

          UpperCAmelCase__							: str 							=						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  )





"""simple docstring"""




import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin







class   _lowercase						(       unittest.TestCase	,   lowerCAmelCase_				):


     '''simple docstring'''





     def  lowerCAmelCase__       (      self  )->					Dict:
          UpperCAmelCase__							: Optional[Any] 							=						load_tool("text-classification"  )
          self.tool.setup()
          UpperCAmelCase__							: List[str] 							=						load_tool("text-classification" ,			remote=__UpperCamelCase  )



     def  lowerCAmelCase__       (      self  )->					Union[str, Any]:
          UpperCAmelCase__							: Dict 							=						self.tool("That's quite cool" ,			["positive", "negative"]  )
          self.assertEqual(__UpperCamelCase ,			"positive"  )



     def  lowerCAmelCase__       (      self  )->					str:
          UpperCAmelCase__							: List[Any] 							=						self.remote_tool("That's quite cool" ,			["positive", "negative"]  )
          self.assertEqual(__UpperCamelCase ,			"positive"  )



     def  lowerCAmelCase__       (      self  )->					Optional[int]:
          UpperCAmelCase__							: Any 							=						self.tool(text="That's quite cool" ,			labels=["positive", "negative"]  )
          self.assertEqual(__UpperCamelCase ,			"positive"  )



     def  lowerCAmelCase__       (      self  )->					str:
          UpperCAmelCase__							: str 							=						self.remote_tool(text="That's quite cool" ,			labels=["positive", "negative"]  )
          self.assertEqual(__UpperCamelCase ,			"positive"  )



