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





	def __init__(       self      :							Optional[Any]   ,				__magic_name__      :							str   ,				):


				"""simple docstring"""

				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 				__SCREAMING_SNAKE_CASE	(       self      :							Optional[int]				):


				"""simple docstring"""

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


				"""simple docstring"""

				lowerCAmelCase__										=				TFDistilBertModel(config=__magic_name__				)
				lowerCAmelCase__										=				{"input_ids": input_ids, "attention_mask": input_mask}

				lowerCAmelCase__										=				model(__magic_name__				)

				lowerCAmelCase__										=				[input_ids, input_mask]

				lowerCAmelCase__										=				model(__magic_name__				)

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





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Tuple   ,				__magic_name__      :							Optional[Any]   ,				__magic_name__      :							Tuple   ,				__magic_name__      :							Optional[int]   ,				__magic_name__      :							List[Any]   ,				__magic_name__      :							List[Any]   ,				__magic_name__      :							Any				):


				"""simple docstring"""

				lowerCAmelCase__										=				TFDistilBertForMaskedLM(config=__magic_name__				)
				lowerCAmelCase__										=				{"input_ids": input_ids, "attention_mask": input_mask}
				lowerCAmelCase__										=				model(__magic_name__				)
				self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Optional[int]   ,				__magic_name__      :							Optional[int]   ,				__magic_name__      :							str   ,				__magic_name__      :							Tuple   ,				__magic_name__      :							Dict   ,				__magic_name__      :							str   ,				__magic_name__      :							List[Any]				):


				"""simple docstring"""

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


				"""simple docstring"""

				lowerCAmelCase__										=				self.num_labels
				lowerCAmelCase__										=				TFDistilBertForSequenceClassification(__magic_name__				)
				lowerCAmelCase__										=				{"input_ids": input_ids, "attention_mask": input_mask}
				lowerCAmelCase__										=				model(__magic_name__				)
				self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels)				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							List[Any]   ,				__magic_name__      :							Any   ,				__magic_name__      :							Optional[int]   ,				__magic_name__      :							Optional[Any]   ,				__magic_name__      :							List[str]   ,				__magic_name__      :							Optional[Any]   ,				__magic_name__      :							Tuple				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.num_choices
				lowerCAmelCase__										=				TFDistilBertForMultipleChoice(__magic_name__				)
				lowerCAmelCase__										=				tf.tile(tf.expand_dims(__magic_name__   ,				1				)   ,				(1, self.num_choices, 1)				)
				lowerCAmelCase__										=				tf.tile(tf.expand_dims(__magic_name__   ,				1				)   ,				(1, self.num_choices, 1)				)
				lowerCAmelCase__										=				{
				    "input_ids": multiple_choice_inputs_ids,
				    "attention_mask": multiple_choice_input_mask,
				}
				lowerCAmelCase__										=				model(__magic_name__				)
				self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_choices)				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Union[str, Any]   ,				__magic_name__      :							List[Any]   ,				__magic_name__      :							int   ,				__magic_name__      :							Dict   ,				__magic_name__      :							List[Any]   ,				__magic_name__      :							str   ,				__magic_name__      :							Dict				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.num_labels
				lowerCAmelCase__										=				TFDistilBertForTokenClassification(__magic_name__				)
				lowerCAmelCase__										=				{"input_ids": input_ids, "attention_mask": input_mask}
				lowerCAmelCase__										=				model(__magic_name__				)
				self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.num_labels)				)







	def 				__SCREAMING_SNAKE_CASE	(       self      :							str				):


				"""simple docstring"""

				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  A  (						SCREAMING_SNAKE_CASE__	,   SCREAMING_SNAKE_CASE__	,   unittest.TestCase     ):
	snake_case__   :List[Any]   =  (
	    (
	        TFDistilBertModel,
	        TFDistilBertForMaskedLM,
	        TFDistilBertForQuestionAnswering,
	        TFDistilBertForSequenceClassification,
	        TFDistilBertForTokenClassification,
	        TFDistilBertForMultipleChoice,
	    )
	    if is_tf_available()
	    else None
	)
	snake_case__   :Dict   =  (
	    {
	        '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__   :List[str]   =  False
	snake_case__   :Tuple   =  False





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Union[str, Any]				):


				"""simple docstring"""

				lowerCAmelCase__										=				TFDistilBertModelTester(self				)
				lowerCAmelCase__										=				ConfigTester(self   ,				config_class=__magic_name__   ,				dim=37				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Optional[Any]				):


				"""simple docstring"""

				self.config_tester.run_common_tests()





	def 				__SCREAMING_SNAKE_CASE	(       self      :							str				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_model(*__magic_name__				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Optional[Any]				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_masked_lm(*__magic_name__				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Any				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_question_answering(*__magic_name__				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							str				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_sequence_classification(*__magic_name__				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Optional[int]				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_multiple_choice(*__magic_name__				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Union[str, Any]				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_token_classification(*__magic_name__				)







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


				"""simple docstring"""

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






@require_tf
class  A  (						unittest.TestCase     ):





	@slow
	def 				__SCREAMING_SNAKE_CASE	(       self      :							Optional[Any]				):


				"""simple docstring"""

				lowerCAmelCase__										=				TFDistilBertModel.from_pretrained("distilbert-base-uncased"				)
				lowerCAmelCase__										=				tf.constant([[0, 1, 2, 3, 4, 5]]				)
				lowerCAmelCase__										=				model(__magic_name__				)[0]

				lowerCAmelCase__										=				[1, 6, 768]
				self.assertEqual(output.shape   ,				__magic_name__				)

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













lowerCamelCase      :       List[Any]    =						{
    '''meter''': '''m''',
    '''kilometer''': '''km''',
    '''megametre''': '''Mm''',
    '''gigametre''': '''Gm''',
    '''terametre''': '''Tm''',
    '''petametre''': '''Pm''',
    '''exametre''': '''Em''',
    '''zettametre''': '''Zm''',
    '''yottametre''': '''Ym''',
}
# Exponent of the factor(meter)
lowerCamelCase      :       Union[str, Any]    =						{
    '''m''': 0,
    '''km''': 3,
    '''Mm''': 6,
    '''Gm''': 9,
    '''Tm''': 12,
    '''Pm''': 15,
    '''Em''': 18,
    '''Zm''': 21,
    '''Ym''': 24,
}





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case       ,			__snake_case							):
     __lowerCAmelCase             =       from_type.lower().strip("s"							)
     __lowerCAmelCase             =       to_type.lower().strip("s"							)

     __lowerCAmelCase             =       UNIT_SYMBOL.get(__snake_case       ,			__snake_case							)
     __lowerCAmelCase             =       UNIT_SYMBOL.get(__snake_case       ,			__snake_case							)

     if from_sanitized not in METRIC_CONVERSION:
          __lowerCAmelCase             =       (
              F"""Invalid 'from_type' value: {from_type!r}.\n"""
              F"""Conversion abbreviations are: {", ".join(__snake_case							)}"""
          )
          raise ValueError(__snake_case							)
     if to_sanitized not in METRIC_CONVERSION:
          __lowerCAmelCase             =       (
              F"""Invalid 'to_type' value: {to_type!r}.\n"""
              F"""Conversion abbreviations are: {", ".join(__snake_case							)}"""
          )
          raise ValueError(__snake_case							)
     __lowerCAmelCase             =       METRIC_CONVERSION[from_sanitized]
     __lowerCAmelCase             =       METRIC_CONVERSION[to_sanitized]
     __lowerCAmelCase             =       1

     if from_exponent > to_exponent:
          __lowerCAmelCase             =       from_exponent - to_exponent
     else:
          __lowerCAmelCase             =       -(to_exponent - from_exponent)

     return value * pow(10       ,			__snake_case							)


if __name__ == "__main__":
 from doctest import testmod

 testmod()






