



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




				def __init__(						self					:						List[str],					__lowercase					:						List[Any],					):
							lowercase__					  =				parent
							lowercase__					  =				13
							lowercase__					  =				7
							lowercase__					  =				True
							lowercase__					  =				True
							lowercase__					  =				False
							lowercase__					  =				True
							lowercase__					  =				99
							lowercase__					  =				32
							lowercase__					  =				2
							lowercase__					  =				4
							lowercase__					  =				37
							lowercase__					  =				"gelu"
							lowercase__					  =				0.1
							lowercase__					  =				0.1
							lowercase__					  =				512
							lowercase__					  =				16
							lowercase__					  =				2
							lowercase__					  =				0.02
							lowercase__					  =				3
							lowercase__					  =				4
							lowercase__					  =				None




				def 							A__      (						self					:						str						):
							lowercase__					  =				ids_tensor([self.batch_size, self.seq_length],					self.vocab_size						)

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

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

							lowercase__					  =				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 							A__      (						self					:						Optional[Any],					__lowercase					:						str,					__lowercase					:						int,					__lowercase					:						str,					__lowercase					:						List[Any],					__lowercase					:						Optional[int],					__lowercase					:						str						):
							lowercase__					  =				TFDistilBertModel(config=UpperCamelCase__						)
							lowercase__					  =				{"input_ids": input_ids, "attention_mask": input_mask}

							lowercase__					  =				model(UpperCamelCase__						)

							lowercase__					  =				[input_ids, input_mask]

							lowercase__					  =				model(UpperCamelCase__						)

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




				def 							A__      (						self					:						int,					__lowercase					:						Tuple,					__lowercase					:						Tuple,					__lowercase					:						Union[str, Any],					__lowercase					:						Any,					__lowercase					:						Optional[int],					__lowercase					:						int						):
							lowercase__					  =				TFDistilBertForMaskedLM(config=UpperCamelCase__						)
							lowercase__					  =				{"input_ids": input_ids, "attention_mask": input_mask}
							lowercase__					  =				model(UpperCamelCase__						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.vocab_size)						)




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




				def 							A__      (						self					:						List[Any],					__lowercase					:						Optional[Any],					__lowercase					:						Tuple,					__lowercase					:						Dict,					__lowercase					:						int,					__lowercase					:						List[str],					__lowercase					:						Any						):
							lowercase__					  =				self.num_choices
							lowercase__					  =				TFDistilBertForMultipleChoice(UpperCamelCase__						)
							lowercase__					  =				tf.tile(tf.expand_dims(UpperCamelCase__,					1						),					(1, self.num_choices, 1)						)
							lowercase__					  =				tf.tile(tf.expand_dims(UpperCamelCase__,					1						),					(1, self.num_choices, 1)						)
							lowercase__					  =				{
							    "input_ids": multiple_choice_inputs_ids,
							    "attention_mask": multiple_choice_input_mask,
							}
							lowercase__					  =				model(UpperCamelCase__						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_choices)						)




				def 							A__      (						self					:						str,					__lowercase					:						List[str],					__lowercase					:						Tuple,					__lowercase					:						Optional[int],					__lowercase					:						Optional[int],					__lowercase					:						Optional[Any],					__lowercase					:						int						):
							lowercase__					  =				self.num_labels
							lowercase__					  =				TFDistilBertForTokenClassification(UpperCamelCase__						)
							lowercase__					  =				{"input_ids": input_ids, "attention_mask": input_mask}
							lowercase__					  =				model(UpperCamelCase__						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.num_labels)						)




				def 							A__      (						self					:						Dict						):
							lowercase__					  =				self.prepare_config_and_inputs()
							((lowercase__)  ,       (lowercase__)  ,       (lowercase__)  ,       (lowercase__)  ,       (lowercase__)  ,       (lowercase__))					  =				config_and_inputs
							lowercase__					  =				{"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict
@require_tf
class 				_snake_case			(					lowercase__    ,	lowercase__    ,	unittest.TestCase):
				UpperCamelCase__				:					Optional[int]     		=(
				    (
				        TFDistilBertModel,
				        TFDistilBertForMaskedLM,
				        TFDistilBertForQuestionAnswering,
				        TFDistilBertForSequenceClassification,
				        TFDistilBertForTokenClassification,
				        TFDistilBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else None
				)
				UpperCamelCase__				:					Union[str, Any]     		=(
				    {
				        "feature-extraction": TFDistilBertModel,
				        "fill-mask": TFDistilBertForMaskedLM,
				        "question-answering": TFDistilBertForQuestionAnswering,
				        "text-classification": TFDistilBertForSequenceClassification,
				        "token-classification": TFDistilBertForTokenClassification,
				        "zero-shot": TFDistilBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				UpperCamelCase__				:					List[Any]     		=False
				UpperCamelCase__				:					int     		=False




				def 							A__      (						self					:						str						):
							lowercase__					  =				TFDistilBertModelTester(self						)
							lowercase__					  =				ConfigTester(self,					config_class=UpperCamelCase__,					dim=37						)




				def 							A__      (						self					:						Optional[Any]						):
							self.config_tester.run_common_tests()




				def 							A__      (						self					:						str						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__						)




				def 							A__      (						self					:						List[str]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__						)




				def 							A__      (						self					:						int						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__						)




				def 							A__      (						self					:						Any						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__						)




				def 							A__      (						self					:						int						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__						)




				def 							A__      (						self					:						List[str]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__						)




				@slow
				def 							A__      (						self					:						List[Any]						):





							for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]						):
										lowercase__					  =				TFDistilBertModel.from_pretrained(UpperCamelCase__						)
										self.assertIsNotNone(UpperCamelCase__						)
@require_tf
class 				_snake_case			(					unittest.TestCase):




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

							lowercase__					  =				[1, 6, 768]
							self.assertEqual(output.shape,					UpperCamelCase__						)

							lowercase__					  =				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],					UpperCamelCase__,					atol=1e-4						)









def __lowerCAmelCase (     SCREAMING_SNAKE_CASE_  ):
			lowercase__					  =				0
			for ch in input_str:
						lowercase__					  =				ord(SCREAMING_SNAKE_CASE_  )
						lowercase__					  =				pow(2						,  SCREAMING_SNAKE_CASE_  )

						# If we already turned on bit for current character's unicode
						if bitmap >> ch_unicode & 1 == 1:
									return False
						bitmap |= ch_bit_index_on
			return True


if __name__ == "__main__":
							import doctest

							doctest.testmod()




