



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      :




     def __init__(					self			,    a__			,    ):
         A__					=       parent
         A__					=       1_3
         A__					=       7
         A__					=       True
         A__					=       True
         A__					=       False
         A__					=       True
         A__					=       9_9
         A__					=       3_2
         A__					=       2
         A__					=       4
         A__					=       3_7
         A__					=       '''gelu'''
         A__					=       0.1
         A__					=       0.1
         A__					=       5_1_2
         A__					=       1_6
         A__					=       2
         A__					=       0.0_2
         A__					=       3
         A__					=       4
         A__					=       None




     def 		snake_case_		(					self):
         A__					=       ids_tensor([self.batch_size, self.seq_length]			,    self.vocab_size)

         A__					=       None
         if self.use_input_mask:
             A__					=       random_attention_mask([self.batch_size, self.seq_length])

         A__					=       None
         A__					=       None
         A__					=       None
         if self.use_labels:
             A__					=       ids_tensor([self.batch_size]			,    self.type_sequence_label_size)
             A__					=       ids_tensor([self.batch_size, self.seq_length]			,    self.num_labels)
             A__					=       ids_tensor([self.batch_size]			,    self.num_choices)

         A__					=       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 		snake_case_		(					self			,    a__			,    a__			,    a__			,    a__			,    a__			,    a__):
         A__					=       TFDistilBertModel(config=a__)
         A__					=       {'''input_ids''': input_ids, '''attention_mask''': input_mask}

         A__					=       model(a__)

         A__					=       [input_ids, input_mask]

         A__					=       model(a__)

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




     def 		snake_case_		(					self			,    a__			,    a__			,    a__			,    a__			,    a__			,    a__):
         A__					=       TFDistilBertForMaskedLM(config=a__)
         A__					=       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
         A__					=       model(a__)
         self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.vocab_size))




     def 		snake_case_		(					self			,    a__			,    a__			,    a__			,    a__			,    a__			,    a__):
         A__					=       TFDistilBertForQuestionAnswering(config=a__)
         A__					=       {
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
         }
         A__					=       model(a__)
         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 		snake_case_		(					self			,    a__			,    a__			,    a__			,    a__			,    a__			,    a__):
         A__					=       self.num_labels
         A__					=       TFDistilBertForSequenceClassification(a__)
         A__					=       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
         A__					=       model(a__)
         self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels))




     def 		snake_case_		(					self			,    a__			,    a__			,    a__			,    a__			,    a__			,    a__):
         A__					=       self.num_choices
         A__					=       TFDistilBertForMultipleChoice(a__)
         A__					=       tf.tile(tf.expand_dims(a__			,    1)			,    (1, self.num_choices, 1))
         A__					=       tf.tile(tf.expand_dims(a__			,    1)			,    (1, self.num_choices, 1))
         A__					=       {
             '''input_ids''': multiple_choice_inputs_ids,
             '''attention_mask''': multiple_choice_input_mask,
         }
         A__					=       model(a__)
         self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_choices))




     def 		snake_case_		(					self			,    a__			,    a__			,    a__			,    a__			,    a__			,    a__):
         A__					=       self.num_labels
         A__					=       TFDistilBertForTokenClassification(a__)
         A__					=       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
         A__					=       model(a__)
         self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.num_labels))




     def 		snake_case_		(					self):
         A__					=       self.prepare_config_and_inputs()
         ((A__)			,						(A__)			,						(A__)			,						(A__)			,						(A__)			,						(A__))					=       config_and_inputs
         A__					=       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
         return config, inputs_dict







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




     def 		snake_case_		(					self):
         A__					=       TFDistilBertModelTester(self)
         A__					=       ConfigTester(self			,    config_class=a__			,    dim=3_7)




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




     def 		snake_case_		(					self):
         A__					=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_model(*a__)




     def 		snake_case_		(					self):
         A__					=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_masked_lm(*a__)




     def 		snake_case_		(					self):
         A__					=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_question_answering(*a__)




     def 		snake_case_		(					self):
         A__					=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_sequence_classification(*a__)




     def 		snake_case_		(					self):
         A__					=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_multiple_choice(*a__)




     def 		snake_case_		(					self):
         A__					=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_token_classification(*a__)




     @slow
     def 		snake_case_		(					self):
         for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]):
             A__					=       TFDistilBertModel.from_pretrained(a__)
             self.assertIsNotNone(a__)







@require_tf
class      _UpperCAmelCase      (							unittest.TestCase  ):




     @slow
     def 		snake_case_		(					self):
         A__					=       TFDistilBertModel.from_pretrained('''distilbert-base-uncased''')
         A__					=       tf.constant([[0, 1, 2, 3, 4, 5]])
         A__					=       model(a__)[0]

         A__					=       [1, 6, 7_6_8]
         self.assertEqual(output.shape			,    a__)

         A__					=       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]			,    a__			,    atol=1e-4)





from collections import OrderedDict
from typing import Mapping

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


_lowercase    	=		logging.get_logger(__name__)

_lowercase    	=		{
    "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/config.json",
    "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/config.json",
    "xlm-roberta-large-finetuned-conll02-dutch": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll02-spanish": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll03-english": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll03-german": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json"
    ),
}



class      _UpperCAmelCase      (							A__  ):
     UpperCamelCase__		     =					'''xlm-roberta'''




     def __init__(					self			,    a__=3_0_5_2_2			,    a__=7_6_8			,    a__=1_2			,    a__=1_2			,    a__=3_0_7_2			,    a__="gelu"			,    a__=0.1			,    a__=0.1			,    a__=5_1_2			,    a__=2			,    a__=0.0_2			,    a__=1e-12			,    a__=1			,    a__=0			,    a__=2			,    a__="absolute"			,    a__=True			,    a__=None			,    **a__			,    ):
         super().__init__(pad_token_id=a__			,    bos_token_id=a__			,    eos_token_id=a__			,    **a__)

         A__					=       vocab_size
         A__					=       hidden_size
         A__					=       num_hidden_layers
         A__					=       num_attention_heads
         A__					=       hidden_act
         A__					=       intermediate_size
         A__					=       hidden_dropout_prob
         A__					=       attention_probs_dropout_prob
         A__					=       max_position_embeddings
         A__					=       type_vocab_size
         A__					=       initializer_range
         A__					=       layer_norm_eps
         A__					=       position_embedding_type
         A__					=       use_cache
         A__					=       classifier_dropout







class      _UpperCAmelCase      (							A__  ):




     @property
     def 		snake_case_		(					self):
         if self.task == "multiple-choice":
             A__					=       {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
         else:
             A__					=       {0: '''batch''', 1: '''sequence'''}
         return OrderedDict(
             [
                 ('''input_ids''', dynamic_axis),
                 ('''attention_mask''', dynamic_axis),
             ])
