


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     ,       _SCREAMING_SNAKE_CASE     ,       )					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     parent
									_UpperCAmelCase											=     13
									_UpperCAmelCase											=     7
									_UpperCAmelCase											=     True
									_UpperCAmelCase											=     True
									_UpperCAmelCase											=     False
									_UpperCAmelCase											=     True
									_UpperCAmelCase											=     99
									_UpperCAmelCase											=     32
									_UpperCAmelCase											=     2
									_UpperCAmelCase											=     4
									_UpperCAmelCase											=     37
									_UpperCAmelCase											=     'gelu'
									_UpperCAmelCase											=     0.1
									_UpperCAmelCase											=     0.1
									_UpperCAmelCase											=     512
									_UpperCAmelCase											=     16
									_UpperCAmelCase											=     2
									_UpperCAmelCase											=     0.02
									_UpperCAmelCase											=     3
									_UpperCAmelCase											=     4
									_UpperCAmelCase											=     None
		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     ids_tensor([self.batch_size, self.seq_length]     ,       self.vocab_size		)

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

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

									_UpperCAmelCase											=     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       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     TFDistilBertModel(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     {'input_ids': input_ids, 'attention_mask': input_mask}

									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     [input_ids, input_mask]

									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)

									self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, self.seq_length, self.hidden_size)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     TFDistilBertForMaskedLM(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     {'input_ids': input_ids, 'attention_mask': input_mask}
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.seq_length, self.vocab_size)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Any:




									"""simple docstring"""





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




									"""simple docstring"""





									_UpperCAmelCase											=     self.num_labels
									_UpperCAmelCase											=     TFDistilBertForSequenceClassification(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     {'input_ids': input_ids, 'attention_mask': input_mask}
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.num_choices
									_UpperCAmelCase											=     TFDistilBertForMultipleChoice(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE     ,       1		)     ,       (1, self.num_choices, 1)		)
									_UpperCAmelCase											=     tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE     ,       1		)     ,       (1, self.num_choices, 1)		)
									_UpperCAmelCase											=     {
									    'input_ids': multiple_choice_inputs_ids,
									    'attention_mask': multiple_choice_input_mask,
									}
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_choices)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Any:




									"""simple docstring"""





									_UpperCAmelCase											=     self.num_labels
									_UpperCAmelCase											=     TFDistilBertForTokenClassification(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     {'input_ids': input_ids, 'attention_mask': input_mask}
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.seq_length, self.num_labels)		)






		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





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



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




									"""simple docstring"""





									_UpperCAmelCase											=     TFDistilBertModelTester(self		)
									_UpperCAmelCase											=     ConfigTester(self     ,       config_class=_SCREAMING_SNAKE_CASE     ,       dim=37		)
		def       UpperCAmelCase__							(     self		)					->			Any:




									"""simple docstring"""





									self.config_tester.run_common_tests()
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_model(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_masked_lm(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_question_answering(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Any:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_sequence_classification(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_multiple_choice(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_token_classification(*_SCREAMING_SNAKE_CASE		)






		@slow
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





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



@require_tf
class 						__a (							unittest.TestCase     ):
		@slow
		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     TFDistilBertModel.from_pretrained('distilbert-base-uncased'		)
									_UpperCAmelCase											=     tf.constant([[0, 1, 2, 3, 4, 5]]		)
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)[0]

									_UpperCAmelCase											=     [1, 6, 768]
									self.assertEqual(output.shape     ,       _SCREAMING_SNAKE_CASE		)

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




from collections import OrderedDict
from typing import Mapping

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


lowerCAmelCase__    :Any       =				logging.get_logger(__name__)

lowerCAmelCase__    :Union[str, Any]       =				{
    '''facebook/xmod-base''': '''https://huggingface.co/facebook/xmod-base/resolve/main/config.json''',
    '''facebook/xmod-large-prenorm''': '''https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json''',
    '''facebook/xmod-base-13-125k''': '''https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json''',
    '''facebook/xmod-base-30-125k''': '''https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json''',
    '''facebook/xmod-base-30-195k''': '''https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json''',
    '''facebook/xmod-base-60-125k''': '''https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json''',
    '''facebook/xmod-base-60-265k''': '''https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json''',
    '''facebook/xmod-base-75-125k''': '''https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json''',
    '''facebook/xmod-base-75-269k''': '''https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json''',
}

class 						__a (							UpperCAmelCase     ):
		_a      :    Any           =      'xmod'
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE=30522     ,       _SCREAMING_SNAKE_CASE=768     ,       _SCREAMING_SNAKE_CASE=12     ,       _SCREAMING_SNAKE_CASE=12     ,       _SCREAMING_SNAKE_CASE=3072     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=512     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=1e-1_2     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=0     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE="absolute"     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE=False     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=False     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=("en_XX",)     ,       _SCREAMING_SNAKE_CASE=None     ,       **_SCREAMING_SNAKE_CASE     ,       )					->			Dict:




									"""simple docstring"""





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

									_UpperCAmelCase											=     vocab_size
									_UpperCAmelCase											=     hidden_size
									_UpperCAmelCase											=     num_hidden_layers
									_UpperCAmelCase											=     num_attention_heads
									_UpperCAmelCase											=     hidden_act
									_UpperCAmelCase											=     intermediate_size
									_UpperCAmelCase											=     hidden_dropout_prob
									_UpperCAmelCase											=     attention_probs_dropout_prob
									_UpperCAmelCase											=     max_position_embeddings
									_UpperCAmelCase											=     type_vocab_size
									_UpperCAmelCase											=     initializer_range
									_UpperCAmelCase											=     layer_norm_eps
									_UpperCAmelCase											=     position_embedding_type
									_UpperCAmelCase											=     use_cache
									_UpperCAmelCase											=     classifier_dropout
									_UpperCAmelCase											=     pre_norm
									_UpperCAmelCase											=     adapter_reduction_factor
									_UpperCAmelCase											=     adapter_layer_norm
									_UpperCAmelCase											=     adapter_reuse_layer_norm
									_UpperCAmelCase											=     ln_before_adapter
									_UpperCAmelCase											=     list(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     default_language



class 						__a (							UpperCAmelCase     ):
		@property
		def       UpperCAmelCase__							(     self		)					->			Mapping[str, Mapping[int, str]]:




									"""simple docstring"""





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