


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 ,  _lowerCamelCase ,  ):
       a						:str										=	parent
       a						:Any										=	13
       a						:Any										=	7
       a						:str										=	True
       a						:Union[str, Any]										=	True
       a						:Tuple										=	False
       a						:Optional[int]										=	True
       a						:List[str]										=	99
       a						:Optional[int]										=	32
       a						:str										=	2
       a						:Tuple										=	4
       a						:str										=	37
       a						:List[str]										=	'''gelu'''
       a						:Dict										=	0.1
       a						:Optional[int]										=	0.1
       a						:Dict										=	512
       a						:int										=	16
       a						:Any										=	2
       a						:Any										=	0.02
       a						:Union[str, Any]										=	3
       a						:Any										=	4
       a						:Any										=	None




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Any										=	ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size   )

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

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

       a						:List[str]										=	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 ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:str										=	TFDistilBertModel(config=_lowerCamelCase   )
       a						:Optional[Any]										=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}

       a						:Optional[Any]										=	model(_lowerCamelCase   )

       a						:Optional[int]										=	[input_ids, input_mask]

       a						:str										=	model(_lowerCamelCase   )

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




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:Optional[int]										=	TFDistilBertForMaskedLM(config=_lowerCamelCase   )
       a						:Any										=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       a						:Any										=	model(_lowerCamelCase   )
       self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.vocab_size)   )




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:str										=	TFDistilBertForQuestionAnswering(config=_lowerCamelCase   )
       a						:str										=	{
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
       }
       a						:List[Any]										=	model(_lowerCamelCase   )
       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 ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:Any										=	self.num_labels
       a						:str										=	TFDistilBertForSequenceClassification(_lowerCamelCase   )
       a						:List[Any]										=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       a						:Dict										=	model(_lowerCamelCase   )
       self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.num_labels)   )




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:int										=	self.num_choices
       a						:Any										=	TFDistilBertForMultipleChoice(_lowerCamelCase   )
       a						:Tuple										=	tf.tile(tf.expand_dims(_lowerCamelCase ,  1   ) ,  (1, self.num_choices, 1)   )
       a						:Optional[Any]										=	tf.tile(tf.expand_dims(_lowerCamelCase ,  1   ) ,  (1, self.num_choices, 1)   )
       a						:Optional[int]										=	{
           '''input_ids''': multiple_choice_inputs_ids,
           '''attention_mask''': multiple_choice_input_mask,
       }
       a						:List[Any]										=	model(_lowerCamelCase   )
       self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.num_choices)   )




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:Any										=	self.num_labels
       a						:List[str]										=	TFDistilBertForTokenClassification(_lowerCamelCase   )
       a						:int										=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       a						:int										=	model(_lowerCamelCase   )
       self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.num_labels)   )




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







@require_tf
class 		_snake_case  (				_snake_case						,   _snake_case						,   unittest.TestCase							):
 SCREAMING_SNAKE_CASE__				=	(
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 SCREAMING_SNAKE_CASE__				=	(
     {
         'feature-extraction': TFDistilBertModel,
         'fill-mask': TFDistilBertForMaskedLM,
         'question-answering': TFDistilBertForQuestionAnswering,
         'text-classification': TFDistilBertForSequenceClassification,
         'token-classification': TFDistilBertForTokenClassification,
         'zero-shot': TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 SCREAMING_SNAKE_CASE__				=	False
 SCREAMING_SNAKE_CASE__				=	False




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Dict										=	TFDistilBertModelTester(self   )
       a						:str										=	ConfigTester(self ,  config_class=_lowerCamelCase ,  dim=37   )




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




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Optional[int]										=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Dict										=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Dict										=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Union[str, Any]										=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Dict										=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Any										=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*_lowerCamelCase   )




 @slow
 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]   ):
             a						:Any										=	TFDistilBertModel.from_pretrained(_lowerCamelCase   )
             self.assertIsNotNone(_lowerCamelCase   )







@require_tf
class 		_snake_case  (				unittest.TestCase							):




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

       a						:Optional[Any]										=	[1, 6, 768]
       self.assertEqual(output.shape ,  _lowerCamelCase   )

       a						:List[str]										=	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] ,  _lowerCamelCase ,  atol=1e-4   )









from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


snake_case    :   Any	    =					{'''configuration_van''': ['''VAN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VanConfig''']}


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case    :   Union[str, Any]	    =					[
         '''VAN_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''VanForImageClassification''',
         '''VanModel''',
         '''VanPreTrainedModel''',
     ]

if TYPE_CHECKING:
     from .configuration_van import VAN_PRETRAINED_CONFIG_ARCHIVE_MAP, VanConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_van import (
              VAN_PRETRAINED_MODEL_ARCHIVE_LIST,
              VanForImageClassification,
              VanModel,
              VanPreTrainedModel,
          )

else:
     import sys

     snake_case    :   Optional[Any]	    =					_LazyModule(__name__, globals()['''__file__'''], _import_structure)





