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		,     lowercase		,     ):



						"""simple docstring"""





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



						"""simple docstring"""





						A_		:    Union[str, Any]         =				ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size )

						A_		:    Optional[Any]         =				None
						if self.use_input_mask:
								A_		:    Any         =				random_attention_mask([self.batch_size, self.seq_length] )

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

						A_		:    Optional[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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    Union[str, Any]         =				TFDistilBertModel(config=lowercase )
						A_		:    str         =				{'input_ids': input_ids, 'attention_mask': input_mask}

						A_		:    Dict         =				model(lowercase )

						A_		:    List[Any]         =				[input_ids, input_mask]

						A_		:    Optional[int]         =				model(lowercase )

						self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size) )
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    int         =				TFDistilBertForMaskedLM(config=lowercase )
						A_		:    List[str]         =				{'input_ids': input_ids, 'attention_mask': input_mask}
						A_		:    Any         =				model(lowercase )
						self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size) )
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    str         =				TFDistilBertForQuestionAnswering(config=lowercase )
						A_		:    int         =				{
						    'input_ids': input_ids,
						    'attention_mask': input_mask,
						}
						A_		:    List[Any]         =				model(lowercase )
						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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    List[str]         =				self.num_labels
						A_		:    Dict         =				TFDistilBertForSequenceClassification(lowercase )
						A_		:    Tuple         =				{'input_ids': input_ids, 'attention_mask': input_mask}
						A_		:    List[str]         =				model(lowercase )
						self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels) )
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    str         =				self.num_choices
						A_		:    Any         =				TFDistilBertForMultipleChoice(lowercase )
						A_		:    List[str]         =				tf.tile(tf.expand_dims(lowercase		,     1 )		,     (1, self.num_choices, 1) )
						A_		:    Dict         =				tf.tile(tf.expand_dims(lowercase		,     1 )		,     (1, self.num_choices, 1) )
						A_		:    int         =				{
						    'input_ids': multiple_choice_inputs_ids,
						    'attention_mask': multiple_choice_input_mask,
						}
						A_		:    Dict         =				model(lowercase )
						self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices) )
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    List[Any]         =				self.num_labels
						A_		:    List[Any]         =				TFDistilBertForTokenClassification(lowercase )
						A_		:    Dict         =				{'input_ids': input_ids, 'attention_mask': input_mask}
						A_		:    Tuple         =				model(lowercase )
						self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels) )







				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Dict         =				self.prepare_config_and_inputs()
						((A_)       ,      (A_)       ,      (A_)       ,      (A_)       ,      (A_)       ,      (A_))		:    int         =				config_and_inputs
						A_		:    Optional[Any]         =				{'input_ids': input_ids, 'attention_mask': input_mask}
						return config, inputs_dict





@require_tf
class UpperCAmelCase						( __A ,	__A ,	unittest.TestCase  ):


				'''simple docstring'''






				lowerCamelCase_    = (
				    (
				        TFDistilBertModel,
				        TFDistilBertForMaskedLM,
				        TFDistilBertForQuestionAnswering,
				        TFDistilBertForSequenceClassification,
				        TFDistilBertForTokenClassification,
				        TFDistilBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else None
				)
				lowerCamelCase_    = (
				    {
				        '''feature-extraction''': TFDistilBertModel,
				        '''fill-mask''': TFDistilBertForMaskedLM,
				        '''question-answering''': TFDistilBertForQuestionAnswering,
				        '''text-classification''': TFDistilBertForSequenceClassification,
				        '''token-classification''': TFDistilBertForTokenClassification,
				        '''zero-shot''': TFDistilBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				lowerCamelCase_    = False
				lowerCamelCase_    = False
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Dict         =				TFDistilBertModelTester(self )
						A_		:    Any         =				ConfigTester(self		,     config_class=lowercase		,     dim=3_7 )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





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



						"""simple docstring"""





						A_		:    List[str]         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_model(*lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Tuple         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Optional[Any]         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Union[str, Any]         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[str]         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase )







				@slow
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1] ):
								A_		:    int         =				TFDistilBertModel.from_pretrained(lowercase )
								self.assertIsNotNone(lowercase )





@require_tf
class UpperCAmelCase						( unittest.TestCase  ):


				'''simple docstring'''






				@slow
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    str         =				TFDistilBertModel.from_pretrained('distilbert-base-uncased' )
						A_		:    Any         =				tf.constant([[0, 1, 2, 3, 4, 5]] )
						A_		:    List[Any]         =				model(lowercase )[0]

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

						A_		:    Optional[int]         =				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]		,     lowercase		,     atol=1E-4 )







import sys

from .dependency_versions_table import deps
from .utils.versions import require_version, require_version_core


# define which module versions we always want to check at run time
# (usually the ones defined in `install_requires` in setup.py)
#
# order specific notes:
# - tqdm must be checked before tokenizers

_UpperCAmelCase            =		"""python tqdm regex requests packaging filelock numpy tokenizers""".split()
if sys.version_info < (3, 7):
	pkgs_to_check_at_runtime.append("""dataclasses""")
if sys.version_info < (3, 8):
	pkgs_to_check_at_runtime.append("""importlib_metadata""")

for pkg in pkgs_to_check_at_runtime:
	if pkg in deps:
		if pkg == "tokenizers":
			# must be loaded here, or else tqdm check may fail
			from .utils import is_tokenizers_available

			if not is_tokenizers_available():
				continue  # not required, check version only if installed

		require_version_core(deps[pkg])
	else:
		raise ValueError(F"""can't find {pkg} in {deps.keys()}, check dependency_versions_table.py""")




def     UpperCamelCase						(	__lowercase						:			str						,__lowercase						:			Dict=None     ):
		'''simple docstring'''




		require_version(deps[pkg]						,__lowercase     )






