



'''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 		_UpperCamelCase						:


			'''simple docstring'''

			def __init__(							self  :				Union[str, Any]							,  lowerCAmelCase__  :				Dict							,  ):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	parent
					__SCREAMING_SNAKE_CASE :	Any					 =	1_3
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	7
					__SCREAMING_SNAKE_CASE :	str					 =	True
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	True
					__SCREAMING_SNAKE_CASE :	str					 =	False
					__SCREAMING_SNAKE_CASE :	List[str]					 =	True
					__SCREAMING_SNAKE_CASE :	int					 =	9_9
					__SCREAMING_SNAKE_CASE :	Any					 =	3_2
					__SCREAMING_SNAKE_CASE :	Tuple					 =	2
					__SCREAMING_SNAKE_CASE :	int					 =	4
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	3_7
					__SCREAMING_SNAKE_CASE :	Tuple					 =	"""gelu"""
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	0.1
					__SCREAMING_SNAKE_CASE :	Dict					 =	0.1
					__SCREAMING_SNAKE_CASE :	str					 =	5_1_2
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	1_6
					__SCREAMING_SNAKE_CASE :	Any					 =	2
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	0.02
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	3
					__SCREAMING_SNAKE_CASE :	str					 =	4
					__SCREAMING_SNAKE_CASE :	str					 =	None

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






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

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	None
					if self.use_input_mask:
							__SCREAMING_SNAKE_CASE :	Tuple					 =	random_attention_mask([self.batch_size, self.seq_length]		)

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

					__SCREAMING_SNAKE_CASE :	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 UpperCamelCase__      (							self  :				Any							,  lowerCAmelCase__  :				List[str]							,  lowerCAmelCase__  :				Any							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				Optional[Any]							,  lowerCAmelCase__  :				Tuple		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	TFDistilBertModel(config=lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	{"""input_ids""": input_ids, """attention_mask""": input_mask}

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	model(lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	Dict					 =	[input_ids, input_mask]

					__SCREAMING_SNAKE_CASE :	Dict					 =	model(lowerCAmelCase__		)

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

			def UpperCamelCase__      (							self  :				Optional[int]							,  lowerCAmelCase__  :				List[str]							,  lowerCAmelCase__  :				List[Any]							,  lowerCAmelCase__  :				int							,  lowerCAmelCase__  :				List[str]							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				Union[str, Any]		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				List[Any]							,  lowerCAmelCase__  :				List[str]							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				List[Any]							,  lowerCAmelCase__  :				List[Any]							,  lowerCAmelCase__  :				Optional[Any]							,  lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	TFDistilBertForQuestionAnswering(config=lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Dict					 =	{
					    """input_ids""": input_ids,
					    """attention_mask""": input_mask,
					}
					__SCREAMING_SNAKE_CASE :	str					 =	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 UpperCamelCase__      (							self  :				Optional[Any]							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				Dict							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				Optional[Any]		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				List[str]							,  lowerCAmelCase__  :				int							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				List[str]							,  lowerCAmelCase__  :				int							,  lowerCAmelCase__  :				str		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				List[Any]							,  lowerCAmelCase__  :				List[Any]							,  lowerCAmelCase__  :				Optional[Any]							,  lowerCAmelCase__  :				int							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	List[str]					 =	self.num_labels
					__SCREAMING_SNAKE_CASE :	Any					 =	TFDistilBertForTokenClassification(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	str					 =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
					__SCREAMING_SNAKE_CASE :	Dict					 =	model(lowerCAmelCase__		)
					self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.seq_length, self.num_labels)		)



			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	self.prepare_config_and_inputs()
					(__SCREAMING_SNAKE_CASE) :	Dict					 =	config_and_inputs
					__SCREAMING_SNAKE_CASE :	int					 =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
					return config, inputs_dict






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


			'''simple docstring'''

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

			def UpperCamelCase__      (							self  :				int		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Tuple					 =	TFDistilBertModelTester(self		)
					__SCREAMING_SNAKE_CASE :	int					 =	ConfigTester(self							,  config_class=lowerCAmelCase__							,  dim=3_7		)

			def UpperCamelCase__      (							self  :				Any		):





					"""simple docstring"""






					self.config_tester.run_common_tests()

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Dict					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				int		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	int					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase__		)



			@slow
			def UpperCamelCase__      (							self  :				Any		):





					"""simple docstring"""






					for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]		):
							__SCREAMING_SNAKE_CASE :	Tuple					 =	TFDistilBertModel.from_pretrained(lowerCAmelCase__		)
							self.assertIsNotNone(lowerCAmelCase__		)






@require_tf
class 		_UpperCamelCase						(							unittest.TestCase					):


			'''simple docstring'''

			@slow
			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	TFDistilBertModel.from_pretrained("""distilbert-base-uncased"""		)
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	tf.constant([[0, 1, 2, 3, 4, 5]]		)
					__SCREAMING_SNAKE_CASE :	Any					 =	model(lowerCAmelCase__		)[0]

					__SCREAMING_SNAKE_CASE :	int					 =	[1, 6, 7_6_8]
					self.assertEqual(output.shape							,  lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	int					 =	tf.constant(
					    [
					        [
					            [0.19_26_18_85, -0.13_73_29_55, 0.4_11_97_99],
					            [0.22_15_01_56, -0.07_42_26_61, 0.39_03_72_04],
					            [0.22_75_60_18, -0.0_89_64_14, 0.3_70_14_67],
					        ]
					    ]		)
					tf.debugging.assert_near(output[:, :3, :3]							,  lowerCAmelCase__							,  atol=1E-4		)




'''simple docstring'''




from ..utils import DummyObject, requires_backends






class 		_UpperCamelCase						(							metaclass=lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Optional[int]    					=	['''onnx''']

			def __init__(							self  :				List[str]							,  *lowerCAmelCase__  :				Optional[int]							,  **lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					requires_backends(self							,  ["""onnx"""]		)

			@classmethod
			def UpperCamelCase__      (							cls  :				Optional[int]							,  *lowerCAmelCase__  :				int							,  **lowerCAmelCase__  :				int		):





					"""simple docstring"""






					requires_backends(cls							,  ["""onnx"""]		)



			@classmethod
			def UpperCamelCase__      (							cls  :				Tuple							,  *lowerCAmelCase__  :				List[str]							,  **lowerCAmelCase__  :				Optional[Any]		):





					"""simple docstring"""






					requires_backends(cls							,  ["""onnx"""]		)