



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



					def __init__(    self			,				UpperCamelCase			,				)     ->			Tuple:
						__lowerCAmelCase												=  parent
						__lowerCAmelCase												=  13
						__lowerCAmelCase												=  7
						__lowerCAmelCase												=  True
						__lowerCAmelCase												=  True
						__lowerCAmelCase												=  False
						__lowerCAmelCase												=  True
						__lowerCAmelCase												=  99
						__lowerCAmelCase												=  32
						__lowerCAmelCase												=  2
						__lowerCAmelCase												=  4
						__lowerCAmelCase												=  37
						__lowerCAmelCase												=  "gelu"
						__lowerCAmelCase												=  0.1
						__lowerCAmelCase												=  0.1
						__lowerCAmelCase												=  512
						__lowerCAmelCase												=  16
						__lowerCAmelCase												=  2
						__lowerCAmelCase												=  0.02
						__lowerCAmelCase												=  3
						__lowerCAmelCase												=  4
						__lowerCAmelCase												=  None



					def        UpperCAmelCase_ (    self     )     ->			List[Any]:
						__lowerCAmelCase												=  ids_tensor([self.batch_size, self.seq_length]			,				self.vocab_size     )

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

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

						__lowerCAmelCase												=  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			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[str]:
						__lowerCAmelCase												=  TFDistilBertModel(config=UpperCamelCase     )
						__lowerCAmelCase												=  {"input_ids": input_ids, "attention_mask": input_mask}

						__lowerCAmelCase												=  model(UpperCamelCase     )

						__lowerCAmelCase												=  [input_ids, input_mask]

						__lowerCAmelCase												=  model(UpperCamelCase     )

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



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Any:
						__lowerCAmelCase												=  TFDistilBertForMaskedLM(config=UpperCamelCase     )
						__lowerCAmelCase												=  {"input_ids": input_ids, "attention_mask": input_mask}
						__lowerCAmelCase												=  model(UpperCamelCase     )
						self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.seq_length, self.vocab_size)     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[Any]:
						__lowerCAmelCase												=  TFDistilBertForQuestionAnswering(config=UpperCamelCase     )
						__lowerCAmelCase												=  {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						}
						__lowerCAmelCase												=  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        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			str:
						__lowerCAmelCase												=  self.num_labels
						__lowerCAmelCase												=  TFDistilBertForSequenceClassification(UpperCamelCase     )
						__lowerCAmelCase												=  {"input_ids": input_ids, "attention_mask": input_mask}
						__lowerCAmelCase												=  model(UpperCamelCase     )
						self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.num_labels)     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[str]:
						__lowerCAmelCase												=  self.num_choices
						__lowerCAmelCase												=  TFDistilBertForMultipleChoice(UpperCamelCase     )
						__lowerCAmelCase												=  tf.tile(tf.expand_dims(UpperCamelCase			,				1     )			,				(1, self.num_choices, 1)     )
						__lowerCAmelCase												=  tf.tile(tf.expand_dims(UpperCamelCase			,				1     )			,				(1, self.num_choices, 1)     )
						__lowerCAmelCase												=  {
						    "input_ids": multiple_choice_inputs_ids,
						    "attention_mask": multiple_choice_input_mask,
						}
						__lowerCAmelCase												=  model(UpperCamelCase     )
						self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.num_choices)     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[Any]:
						__lowerCAmelCase												=  self.num_labels
						__lowerCAmelCase												=  TFDistilBertForTokenClassification(UpperCamelCase     )
						__lowerCAmelCase												=  {"input_ids": input_ids, "attention_mask": input_mask}
						__lowerCAmelCase												=  model(UpperCamelCase     )
						self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.seq_length, self.num_labels)     )



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






@require_tf
class 					UpperCAmelCase__	(		UpperCamelCase__							,       UpperCamelCase__							,       unittest.TestCase ):
					a      :     Tuple        =							(
					    (
					        TFDistilBertModel,
					        TFDistilBertForMaskedLM,
					        TFDistilBertForQuestionAnswering,
					        TFDistilBertForSequenceClassification,
					        TFDistilBertForTokenClassification,
					        TFDistilBertForMultipleChoice,
					    )
					    if is_tf_available()
					    else None
					)
					a      :     Optional[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      :     Any        =							False
					a      :     Optional[Any]        =							False



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						__lowerCAmelCase												=  TFDistilBertModelTester(self     )
						__lowerCAmelCase												=  ConfigTester(self			,				config_class=UpperCamelCase			,				dim=37     )



					def        UpperCAmelCase_ (    self     )     ->			int:
						self.config_tester.run_common_tests()



					def        UpperCAmelCase_ (    self     )     ->			Any:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_model(*UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			List[Any]:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Dict:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			str:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Any:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase     )



					@slow
					def        UpperCAmelCase_ (    self     )     ->			int:
						for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]     ):
							__lowerCAmelCase												=  TFDistilBertModel.from_pretrained(UpperCamelCase     )
							self.assertIsNotNone(UpperCamelCase     )






@require_tf
class 					UpperCAmelCase__	(		unittest.TestCase ):



					@slow
					def        UpperCAmelCase_ (    self     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  TFDistilBertModel.from_pretrained("distilbert-base-uncased"     )
						__lowerCAmelCase												=  tf.constant([[0, 1, 2, 3, 4, 5]]     )
						__lowerCAmelCase												=  model(UpperCamelCase     )[0]

						__lowerCAmelCase												=  [1, 6, 768]
						self.assertEqual(output.shape			,				UpperCamelCase     )

						__lowerCAmelCase												=  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]			,				UpperCamelCase			,				atol=1E-4     )




'''simple docstring'''



from __future__ import annotations



def   __lowerCAmelCase					(							lowerCamelCase   :						list					):






	'''simple docstring'''




	if not nums:
		raise ValueError("List is empty"					)
	return sum(lowerCamelCase					) / len(lowerCamelCase					)


if __name__ == "__main__":
	import doctest

	doctest.testmod()