





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


     def __init__(						self						,	_UpperCAmelCase						,	):

          '''simple docstring'''






          __A						: Optional[Any]      =       parent
          __A						: int      =       13
          __A						: int      =       7
          __A						: Tuple      =       True
          __A						: Dict      =       True
          __A						: Union[str, Any]      =       False
          __A						: int      =       True
          __A						: Union[str, Any]      =       99
          __A						: List[Any]      =       32
          __A						: str      =       2
          __A						: Dict      =       4
          __A						: List[Any]      =       37
          __A						: Tuple      =       'gelu'
          __A						: Optional[int]      =       0.1
          __A						: Tuple      =       0.1
          __A						: List[Any]      =       512
          __A						: Union[str, Any]      =       16
          __A						: List[str]      =       2
          __A						: Tuple      =       0.02
          __A						: Optional[Any]      =       3
          __A						: Optional[int]      =       4
          __A						: Tuple      =       None


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size)

          __A						: Optional[int]      =       None
          if self.use_input_mask:
               __A						: Union[str, Any]      =       random_attention_mask([self.batch_size, self.seq_length])

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

          __A						: 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						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          __A						: Union[str, Any]      =       TFDistilBertModel(config=_UpperCAmelCase)
          __A						: Dict      =       {'input_ids': input_ids, 'attention_mask': input_mask}

          __A						: Optional[int]      =       model(_UpperCAmelCase)

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

          __A						: Optional[int]      =       model(_UpperCAmelCase)

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


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          __A						: Tuple      =       TFDistilBertForMaskedLM(config=_UpperCAmelCase)
          __A						: Dict      =       {'input_ids': input_ids, 'attention_mask': input_mask}
          __A						: Any      =       model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size))


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          __A						: Dict      =       TFDistilBertForQuestionAnswering(config=_UpperCAmelCase)
          __A						: Tuple      =       {
              'input_ids': input_ids,
              'attention_mask': input_mask,
          }
          __A						: 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 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          __A						: List[Any]      =       self.num_labels
          __A						: List[Any]      =       TFDistilBertForSequenceClassification(_UpperCAmelCase)
          __A						: Dict      =       {'input_ids': input_ids, 'attention_mask': input_mask}
          __A						: int      =       model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels))


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






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


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          __A						: Union[str, Any]      =       self.num_labels
          __A						: Optional[Any]      =       TFDistilBertForTokenClassification(_UpperCAmelCase)
          __A						: str      =       {'input_ids': input_ids, 'attention_mask': input_mask}
          __A						: Union[str, Any]      =       model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.num_labels))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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




@require_tf
class  SCREAMING_SNAKE_CASE    (a__					,		a__					,		unittest.TestCase							):
     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 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       TFDistilBertModelTester(self)
          __A						: List[Any]      =       ConfigTester(self						,	config_class=_UpperCAmelCase						,	dim=37)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          self.config_tester.run_common_tests()


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Union[str, Any]      =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_model(*_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: str      =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_masked_lm(*_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_question_answering(*_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Dict      =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_sequence_classification(*_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_multiple_choice(*_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[int]      =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_token_classification(*_UpperCAmelCase)


     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]):
               __A						: Tuple      =       TFDistilBertModel.from_pretrained(_UpperCAmelCase)
               self.assertIsNotNone(_UpperCAmelCase)




@require_tf
class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):


     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       TFDistilBertModel.from_pretrained('distilbert-base-uncased')
          __A						: Union[str, Any]      =       tf.constant([[0, 1, 2, 3, 4, 5]])
          __A						: Optional[Any]      =       model(_UpperCAmelCase)[0]

          __A						: int      =       [1, 6, 768]
          self.assertEqual(output.shape						,	_UpperCAmelCase)

          __A						: List[str]      =       tf.constant(
              [
                  [
                      [0.19261885, -0.13732955, 0.4119799],
                      [0.22150156, -0.07422661, 0.39037204],
                      [0.22756018, -0.0896414, 0.3701467],
                  ]
              ])
          tf.debugging.assert_near(output[:, :3, :3]						,	_UpperCAmelCase						,	atol=1e-4)






'''simple docstring'''




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase__    :      int						=    logging.get_logger(__name__)

lowercase__    :      int						=    {
    '''andreasmadsen/efficient_mlm_m0.40''': (
        '''https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json'''
    ),
}


class  SCREAMING_SNAKE_CASE    (a__							):
     lowerCAmelCase					=       '''roberta-prelayernorm'''


     def __init__(						self						,	_UpperCAmelCase=5_0265						,	_UpperCAmelCase=768						,	_UpperCAmelCase=12						,	_UpperCAmelCase=12						,	_UpperCAmelCase=3072						,	_UpperCAmelCase="gelu"						,	_UpperCAmelCase=0.1						,	_UpperCAmelCase=0.1						,	_UpperCAmelCase=512						,	_UpperCAmelCase=2						,	_UpperCAmelCase=0.02						,	_UpperCAmelCase=1e-1_2						,	_UpperCAmelCase=1						,	_UpperCAmelCase=0						,	_UpperCAmelCase=2						,	_UpperCAmelCase="absolute"						,	_UpperCAmelCase=True						,	_UpperCAmelCase=None						,	**_UpperCAmelCase						,	):

          '''simple docstring'''






          super().__init__(pad_token_id=_UpperCAmelCase						,	bos_token_id=_UpperCAmelCase						,	eos_token_id=_UpperCAmelCase						,	**_UpperCAmelCase)

          __A						: Optional[int]      =       vocab_size
          __A						: List[Any]      =       hidden_size
          __A						: Optional[int]      =       num_hidden_layers
          __A						: Optional[Any]      =       num_attention_heads
          __A						: List[str]      =       hidden_act
          __A						: Dict      =       intermediate_size
          __A						: Optional[int]      =       hidden_dropout_prob
          __A						: Optional[Any]      =       attention_probs_dropout_prob
          __A						: Tuple      =       max_position_embeddings
          __A						: Union[str, Any]      =       type_vocab_size
          __A						: Any      =       initializer_range
          __A						: str      =       layer_norm_eps
          __A						: int      =       position_embedding_type
          __A						: Optional[Any]      =       use_cache
          __A						: Any      =       classifier_dropout




class  SCREAMING_SNAKE_CASE    (a__							):


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          if self.task == "multiple-choice":
               __A						: Dict      =       {0: 'batch', 1: 'choice', 2: 'sequence'}
          else:
               __A						: Union[str, Any]      =       {0: 'batch', 1: 'sequence'}
          return OrderedDict(
              [
                  ('input_ids', dynamic_axis),
                  ('attention_mask', dynamic_axis),
              ])