


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   __a			:
 def __init__(   self				, lowerCAmelCase__				, )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   Union[str, Any]  			=   parent
        lowercase__:   List[str]  			=   13
        lowercase__:   Any  			=   7
        lowercase__:   Union[str, Any]  			=   True
        lowercase__:   Optional[Any]  			=   True
        lowercase__:   Optional[Any]  			=   False
        lowercase__:   Optional[int]  			=   True
        lowercase__:   Optional[Any]  			=   99
        lowercase__:   Tuple  			=   32
        lowercase__:   Dict  			=   2
        lowercase__:   Union[str, Any]  			=   4
        lowercase__:   str  			=   37
        lowercase__:   Dict  			=   'gelu'
        lowercase__:   Union[str, Any]  			=   0.1
        lowercase__:   List[Any]  			=   0.1
        lowercase__:   List[Any]  			=   512
        lowercase__:   List[str]  			=   16
        lowercase__:   Tuple  			=   2
        lowercase__:   Union[str, Any]  			=   0.0_2
        lowercase__:   Any  			=   3
        lowercase__:   Union[str, Any]  			=   4
        lowercase__:   Optional[int]  			=   None
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Tuple:




        '''simple docstring'''
        lowercase__:   int  			=   ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size )

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

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

        lowercase__:   Union[str, 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 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   Any  			=   TFDistilBertModel(config=lowerCAmelCase__ )
        lowercase__:   Optional[Any]  			=   {'input_ids': input_ids, 'attention_mask': input_mask}

        lowercase__:   Dict  			=   model(lowerCAmelCase__ )

        lowercase__:   Dict  			=   [input_ids, input_mask]

        lowercase__:   int  			=   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__ )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   Dict  			=   TFDistilBertForMaskedLM(config=lowerCAmelCase__ )
        lowercase__:   Any  			=   {'input_ids': input_ids, 'attention_mask': input_mask}
        lowercase__:   Tuple  			=   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__ )					->				Dict:




        '''simple docstring'''
        lowercase__:   Tuple  			=   TFDistilBertForQuestionAnswering(config=lowerCAmelCase__ )
        lowercase__:   List[str]  			=   {
            'input_ids': input_ids,
            'attention_mask': input_mask,
        }
        lowercase__:   Optional[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__ )					->				List[str]:




        '''simple docstring'''
        lowercase__:   Dict  			=   self.num_labels
        lowercase__:   Optional[Any]  			=   TFDistilBertForSequenceClassification(lowerCAmelCase__ )
        lowercase__:   Tuple  			=   {'input_ids': input_ids, 'attention_mask': input_mask}
        lowercase__:   int  			=   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__ )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   self.num_choices
        lowercase__:   int  			=   TFDistilBertForMultipleChoice(lowerCAmelCase__ )
        lowercase__:   Any  			=   tf.tile(tf.expand_dims(lowerCAmelCase__				, 1 )				, (1, self.num_choices, 1) )
        lowercase__:   List[str]  			=   tf.tile(tf.expand_dims(lowerCAmelCase__				, 1 )				, (1, self.num_choices, 1) )
        lowercase__:   Tuple  			=   {
            'input_ids': multiple_choice_inputs_ids,
            'attention_mask': multiple_choice_input_mask,
        }
        lowercase__:   Dict  			=   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__ )					->				Tuple:




        '''simple docstring'''
        lowercase__:   Tuple  			=   self.num_labels
        lowercase__:   Tuple  			=   TFDistilBertForTokenClassification(lowerCAmelCase__ )
        lowercase__:   int  			=   {'input_ids': input_ids, 'attention_mask': input_mask}
        lowercase__:   List[Any]  			=   model(lowerCAmelCase__ )
        self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.num_labels) )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				str:




        '''simple docstring'''
        lowercase__:   List[str]  			=   self.prepare_config_and_inputs()
        ((lowercase__)			,						(lowercase__)			,						(lowercase__)			,						(lowercase__)			,						(lowercase__)			,						(lowercase__)):   List[Any]  			=   config_and_inputs
        lowercase__:   str  			=   {'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict





@require_tf
class   __a			(    __UpperCamelCase       ,       __UpperCamelCase       ,       unittest.TestCase      ):
 __lowercase :							int        = (
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 __lowercase :							Tuple        = (
     {
         'feature-extraction': TFDistilBertModel,
         'fill-mask': TFDistilBertForMaskedLM,
         'question-answering': TFDistilBertForQuestionAnswering,
         'text-classification': TFDistilBertForSequenceClassification,
         'token-classification': TFDistilBertForTokenClassification,
         'zero-shot': TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __lowercase :							int        = False
 __lowercase :							Any        = False
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   Any  			=   TFDistilBertModelTester(self )
        lowercase__:   int  			=   ConfigTester(self				, config_class=lowerCAmelCase__				, dim=37 )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[str]:




        '''simple docstring'''
        self.config_tester.run_common_tests()
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[int]:




        '''simple docstring'''
        lowercase__:   List[str]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Tuple:




        '''simple docstring'''
        lowercase__:   List[Any]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Any:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[int]:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   List[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 )					->				Union[str, Any]:




        '''simple docstring'''
        for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1] ):
               lowercase__:   str  			=   TFDistilBertModel.from_pretrained(lowerCAmelCase__ )
               self.assertIsNotNone(lowerCAmelCase__ )





@require_tf
class   __a			(    unittest.TestCase      ):
 @slow
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   Any  			=   TFDistilBertModel.from_pretrained('distilbert-base-uncased' )
        lowercase__:   str  			=   tf.constant([[0, 1, 2, 3, 4, 5]] )
        lowercase__:   Tuple  			=   model(lowerCAmelCase__ )[0]

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

        lowercase__:   List[str]  			=   tf.constant(
            [
                [
                    [0.1_9_2_6_1_8_8_5, -0.1_3_7_3_2_9_5_5, 0.4_1_1_9_7_9_9],
                    [0.2_2_1_5_0_1_5_6, -0.0_7_4_2_2_6_6_1, 0.3_9_0_3_7_2_0_4],
                    [0.2_2_7_5_6_0_1_8, -0.0_8_9_6_4_1_4, 0.3_7_0_1_4_6_7],
                ]
            ] )
        tf.debugging.assert_near(output[:, :3, :3]				, lowerCAmelCase__				, atol=1E-4 )






from typing import TYPE_CHECKING

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


__lowerCAmelCase		       =							{
    '''configuration_canine''': ['''CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CanineConfig'''],
    '''tokenization_canine''': ['''CanineTokenizer'''],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __lowerCAmelCase		       =							[
     '''CANINE_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''CanineForMultipleChoice''',
     '''CanineForQuestionAnswering''',
     '''CanineForSequenceClassification''',
     '''CanineForTokenClassification''',
     '''CanineLayer''',
     '''CanineModel''',
     '''CaninePreTrainedModel''',
     '''load_tf_weights_in_canine''',
 ]


if TYPE_CHECKING:
 from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig
 from .tokenization_canine import CanineTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_canine import (
      CANINE_PRETRAINED_MODEL_ARCHIVE_LIST,
      CanineForMultipleChoice,
      CanineForQuestionAnswering,
      CanineForSequenceClassification,
      CanineForTokenClassification,
      CanineLayer,
      CanineModel,
      CaninePreTrainedModel,
      load_tf_weights_in_canine,
  )


else:
 import sys

 __lowerCAmelCase		       =							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


