






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






 '''simple docstring'''




 def __init__(	self							,  _A							,  ) ->		int:

     """simple docstring"""



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


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size)

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

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

     _UpperCAmelCase				:      str			  =				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  snake_case__				(	self							,  _A							,  _A							,  _A							,  _A							,  _A							,  _A) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Any			  =				TFDistilBertModel(config=_A)
     _UpperCAmelCase				:      Optional[int]			  =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}

     _UpperCAmelCase				:      Tuple			  =				model(_A)

     _UpperCAmelCase				:      List[str]			  =				[input_ids, input_mask]

     _UpperCAmelCase				:      Dict			  =				model(_A)

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


 def  snake_case__				(	self							,  _A							,  _A							,  _A							,  _A							,  _A							,  _A) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				TFDistilBertForMaskedLM(config=_A)
     _UpperCAmelCase				:      List[Any]			  =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
     _UpperCAmelCase				:      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) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				TFDistilBertForQuestionAnswering(config=_A)
     _UpperCAmelCase				:      List[str]			  =				{
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
     }
     _UpperCAmelCase				:      int			  =				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							,  _A							,  _A							,  _A							,  _A							,  _A							,  _A) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				self.num_labels
     _UpperCAmelCase				:      Any			  =				TFDistilBertForSequenceClassification(_A)
     _UpperCAmelCase				:      List[str]			  =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
     _UpperCAmelCase				:      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) ->		Tuple:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				self.num_choices
     _UpperCAmelCase				:      Optional[int]			  =				TFDistilBertForMultipleChoice(_A)
     _UpperCAmelCase				:      Any			  =				tf.tile(tf.expand_dims(_A							,  1)							,  (1, self.num_choices, 1))
     _UpperCAmelCase				:      Tuple			  =				tf.tile(tf.expand_dims(_A							,  1)							,  (1, self.num_choices, 1))
     _UpperCAmelCase				:      Any			  =				{
         '''input_ids''': multiple_choice_inputs_ids,
         '''attention_mask''': multiple_choice_input_mask,
     }
     _UpperCAmelCase				:      List[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) ->		Dict:

     """simple docstring"""



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


 def  snake_case__				(	self) ->		Tuple:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				self.prepare_config_and_inputs()
     ((_UpperCAmelCase)  , (_UpperCAmelCase)  , (_UpperCAmelCase)  , (_UpperCAmelCase)  , (_UpperCAmelCase)  , (_UpperCAmelCase))				:      Dict			  =				config_and_inputs
     _UpperCAmelCase				:      int			  =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
     return config, inputs_dict



@require_tf
class 							A_    (						__lowercase ,   __lowercase ,   unittest.TestCase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			List[str] =					(
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 _SCREAMING_SNAKE_CASE							:			str =					(
     {
         "feature-extraction": TFDistilBertModel,
         "fill-mask": TFDistilBertForMaskedLM,
         "question-answering": TFDistilBertForQuestionAnswering,
         "text-classification": TFDistilBertForSequenceClassification,
         "token-classification": TFDistilBertForTokenClassification,
         "zero-shot": TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 _SCREAMING_SNAKE_CASE							:			Any =					False
 _SCREAMING_SNAKE_CASE							:			Optional[Any] =					False


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				TFDistilBertModelTester(self)
     _UpperCAmelCase				:      Optional[int]			  =				ConfigTester(self							,  config_class=_A							,  dim=37)


 def  snake_case__				(	self) ->		List[str]:

     """simple docstring"""



     self.config_tester.run_common_tests()


 def  snake_case__				(	self) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_model(*_A)


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

     """simple docstring"""



     _UpperCAmelCase				:      Any			  =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_masked_lm(*_A)


 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_question_answering(*_A)


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Any			  =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_sequence_classification(*_A)


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_multiple_choice(*_A)


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

     """simple docstring"""



     _UpperCAmelCase				:      Optional[Any]			  =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_token_classification(*_A)


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

     """simple docstring"""



     for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]):
         _UpperCAmelCase				:      List[str]			  =				TFDistilBertModel.from_pretrained(_A)
         self.assertIsNotNone(_A)



@require_tf
class 							A_    (						unittest.TestCase			):






 '''simple docstring'''




 @slow
 def  snake_case__				(	self) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				TFDistilBertModel.from_pretrained('''distilbert-base-uncased''')
     _UpperCAmelCase				:      Dict			  =				tf.constant([[0, 1, 2, 3, 4, 5]])
     _UpperCAmelCase				:      int			  =				model(_A)[0]

     _UpperCAmelCase				:      str			  =				[1, 6, 768]
     self.assertEqual(output.shape							,  _A)

     _UpperCAmelCase				:      List[str]			  =				tf.constant(
         [
             [
                 [0.19261885, -0.13732955, 0.4119799],
                 [0.22150156, -0.07422661, 0.39037204],
                 [0.22756018, -0.0896414, 0.3701467],
             ]
         ])
     tf.debugging.assert_near(output[:, :3, :3]							,  _A							,  atol=1e-4)












import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


SCREAMING_SNAKE_CASE 							=						'\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n'
class 							A_    (						unittest.TestCase ,   __lowercase			):






 '''simple docstring'''




 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				load_tool('''text-question-answering''')
     self.tool.setup()
     _UpperCAmelCase				:      Any			  =				load_tool('''text-question-answering'''							,  remote=_A)


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

     """simple docstring"""



     _UpperCAmelCase				:      Optional[Any]			  =				self.tool(_A							,  '''What did Hugging Face do in April 2021?''')
     self.assertEqual(_A							,  '''launched the BigScience Research Workshop''')


 def  snake_case__				(	self) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				self.remote_tool(_A							,  '''What did Hugging Face do in April 2021?''')
     self.assertEqual(_A							,  '''launched the BigScience Research Workshop''')


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				self.tool(text=_A							,  question='''What did Hugging Face do in April 2021?''')
     self.assertEqual(_A							,  '''launched the BigScience Research Workshop''')


 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				self.remote_tool(text=_A							,  question='''What did Hugging Face do in April 2021?''')
     self.assertEqual(_A							,  '''launched the BigScience Research Workshop''')




