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

			def __init__(self			,   A			,   ):
								lowerCamelCase_					:      Optional[int]     =		parent
								lowerCamelCase_					:      List[Any]     =		1_3
								lowerCamelCase_					:      Optional[int]     =		7
								lowerCamelCase_					:      Any     =		True
								lowerCamelCase_					:      Tuple     =		True
								lowerCamelCase_					:      List[Any]     =		False
								lowerCamelCase_					:      Any     =		True
								lowerCamelCase_					:      Optional[int]     =		9_9
								lowerCamelCase_					:      int     =		3_2
								lowerCamelCase_					:      Dict     =		2
								lowerCamelCase_					:      Dict     =		4
								lowerCamelCase_					:      Optional[int]     =		3_7
								lowerCamelCase_					:      Tuple     =		'''gelu'''
								lowerCamelCase_					:      Optional[Any]     =		0.1
								lowerCamelCase_					:      Dict     =		0.1
								lowerCamelCase_					:      str     =		5_1_2
								lowerCamelCase_					:      List[Any]     =		1_6
								lowerCamelCase_					:      Dict     =		2
								lowerCamelCase_					:      List[str]     =		0.02
								lowerCamelCase_					:      int     =		3
								lowerCamelCase_					:      Optional[int]     =		4
								lowerCamelCase_					:      Tuple     =		None

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[Any]     =		ids_tensor([self.batch_size, self.seq_length]			,   self.vocab_size      )

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

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

								lowerCamelCase_					:      Any     =		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			,   A			,   A			,   A			,   A			,   A			,   A      ):
								lowerCamelCase_					:      Tuple     =		TFDistilBertModel(config=UpperCAmelCase__      )
								lowerCamelCase_					:      int     =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}

								lowerCamelCase_					:      Union[str, Any]     =		model(UpperCAmelCase__      )

								lowerCamelCase_					:      Tuple     =		[input_ids, input_mask]

								lowerCamelCase_					:      int     =		model(UpperCAmelCase__      )

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

			def        UpperCAmelCase__ (self			,   A			,   A			,   A			,   A			,   A			,   A      ):
								lowerCamelCase_					:      Optional[int]     =		TFDistilBertForMaskedLM(config=UpperCAmelCase__      )
								lowerCamelCase_					:      int     =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowerCamelCase_					:      Union[str, Any]     =		model(UpperCAmelCase__      )
								self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.seq_length, self.vocab_size)      )

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

			def        UpperCAmelCase__ (self			,   A			,   A			,   A			,   A			,   A			,   A      ):
								lowerCamelCase_					:      Dict     =		self.num_choices
								lowerCamelCase_					:      Tuple     =		TFDistilBertForMultipleChoice(UpperCAmelCase__      )
								lowerCamelCase_					:      Dict     =		tf.tile(tf.expand_dims(UpperCAmelCase__			,   1      )			,   (1, self.num_choices, 1)      )
								lowerCamelCase_					:      str     =		tf.tile(tf.expand_dims(UpperCAmelCase__			,   1      )			,   (1, self.num_choices, 1)      )
								lowerCamelCase_					:      Tuple     =		{
								    '''input_ids''': multiple_choice_inputs_ids,
								    '''attention_mask''': multiple_choice_input_mask,
								}
								lowerCamelCase_					:      Optional[Any]     =		model(UpperCAmelCase__      )
								self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.num_choices)      )

			def        UpperCAmelCase__ (self			,   A			,   A			,   A			,   A			,   A			,   A      ):
								lowerCamelCase_					:      Optional[Any]     =		self.num_labels
								lowerCamelCase_					:      Any     =		TFDistilBertForTokenClassification(UpperCAmelCase__      )
								lowerCamelCase_					:      Tuple     =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowerCamelCase_					:      List[Any]     =		model(UpperCAmelCase__      )
								self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.seq_length, self.num_labels)      )






			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      List[str]     =		self.prepare_config_and_inputs()
								(lowerCamelCase_)					:      Union[str, Any]     =		config_and_inputs
								lowerCamelCase_					:      Tuple     =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict

@require_tf
class 	__lowercase				(  _lowercase      ,     _lowercase      ,     unittest.TestCase    ):
			lowerCamelCase			:		str	      =			(
			    (
			        TFDistilBertModel,
			        TFDistilBertForMaskedLM,
			        TFDistilBertForQuestionAnswering,
			        TFDistilBertForSequenceClassification,
			        TFDistilBertForTokenClassification,
			        TFDistilBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else None
			)
			lowerCamelCase			:		int	      =			(
			    {
			        'feature-extraction': TFDistilBertModel,
			        'fill-mask': TFDistilBertForMaskedLM,
			        'question-answering': TFDistilBertForQuestionAnswering,
			        'text-classification': TFDistilBertForSequenceClassification,
			        'token-classification': TFDistilBertForTokenClassification,
			        'zero-shot': TFDistilBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			lowerCamelCase			:		Optional[Any]	      =			False
			lowerCamelCase			:		int	      =			False

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      List[Any]     =		TFDistilBertModelTester(self      )
								lowerCamelCase_					:      Optional[Any]     =		ConfigTester(self			,   config_class=UpperCAmelCase__			,   dim=3_7      )

			def        UpperCAmelCase__ (self      ):
								self.config_tester.run_common_tests()

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[Any]     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase__      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      str     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase__      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      List[str]     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase__      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[Any]     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase__      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      str     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase__      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[Any]     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase__      )






			@slow
			def        UpperCAmelCase__ (self      ):
								for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]      ):
													lowerCamelCase_					:      str     =		TFDistilBertModel.from_pretrained(UpperCAmelCase__      )
													self.assertIsNotNone(UpperCAmelCase__      )

@require_tf
class 	__lowercase				(  unittest.TestCase    ):

			@slow
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Union[str, Any]     =		TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''      )
								lowerCamelCase_					:      List[str]     =		tf.constant([[0, 1, 2, 3, 4, 5]]      )
								lowerCamelCase_					:      Tuple     =		model(UpperCAmelCase__      )[0]

								lowerCamelCase_					:      List[str]     =		[1, 6, 7_6_8]
								self.assertEqual(output.shape			,   UpperCAmelCase__      )

								lowerCamelCase_					:      Dict     =		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 typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


__lowercase							:   List[Any]					=     {}


try:
					if not is_sentencepiece_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:   List[Any]					=     ['''MLukeTokenizer''']

if TYPE_CHECKING:
					try:
										if not is_sentencepiece_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_mluke import MLukeTokenizer


else:
					import sys

					__lowercase							:   List[str]					=     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


