

"""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 					_lowerCAmelCase  :







 """simple docstring"""







 def __init__(			self							:			List[str],					UpperCAmelCase__							:			Tuple,					):
    __lowercase								=						parent
    __lowercase								=						1_3
    __lowercase								=						7
    __lowercase								=						True
    __lowercase								=						True
    __lowercase								=						False
    __lowercase								=						True
    __lowercase								=						9_9
    __lowercase								=						3_2
    __lowercase								=						2
    __lowercase								=						4
    __lowercase								=						3_7
    __lowercase								=						"gelu"
    __lowercase								=						0.1
    __lowercase								=						0.1
    __lowercase								=						5_1_2
    __lowercase								=						1_6
    __lowercase								=						2
    __lowercase								=						0.02
    __lowercase								=						3
    __lowercase								=						4
    __lowercase								=						None


 def 					_lowercase (			self							:			int	):
    __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 					_lowercase (			self							:			Union[str, Any],					UpperCAmelCase__							:			str,					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Optional[int]	):
    __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 					_lowercase (			self							:			Tuple,					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			List[str]	):
    __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 					_lowercase (			self							:			int,					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			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 					_lowercase (			self							:			Any,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			List[Any],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Any	):
    __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 					_lowercase (			self							:			Optional[int],					UpperCAmelCase__							:			str,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			List[Any],					UpperCAmelCase__							:			Tuple	):
    __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 					_lowercase (			self							:			str,					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			str,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			List[Any]	):
    __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 					_lowercase (			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 					_lowerCAmelCase  (	lowercase     ,lowercase     ,unittest.TestCase   ):







 """simple docstring"""







 __UpperCAmelCase	:					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	:					str   		=	False
 __UpperCAmelCase	:					List[str]   		=	False


 def 					_lowercase (			self							:			Optional[int]	):
    __lowercase								=						TFDistilBertModelTester(self	)
    __lowercase								=						ConfigTester(self,					config_class=UpperCAmelCase__,					dim=3_7	)


 def 					_lowercase (			self							:			str	):
    self.config_tester.run_common_tests()


 def 					_lowercase (			self							:			Optional[int]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			List[Any]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase__	)



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







@require_tf
class 					_lowerCAmelCase  (	unittest.TestCase   ):







 """simple docstring"""







 @slow
 def 					_lowercase (			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, 7_6_8]
    self.assertEqual(output.shape,					UpperCAmelCase__	)

    __lowercase								=						tf.constant(
        [
            [
                [0.19_261_885, -0.13_732_955, 0.4_119_799],
                [0.22_150_156, -0.07_422_661, 0.39_037_204],
                [0.22_756_018, -0.0_896_414, 0.3_701_467],
            ]
        ]	)
    tf.debugging.assert_near(output[:, :3, :3],					UpperCAmelCase__,					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

snake_case							:       List[str]     			=      '''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 							__lowercase   ( __lowerCAmelCase							:				Optional[Any]					,    __lowerCAmelCase							:				Tuple=None   ):
							require_version(deps[pkg]					,    __lowerCAmelCase   )




