


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      ,   )			->	Union[str, 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						)			->	int:




									"""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						)			->	Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=      TFDistilBertModel(config=lowerCAmelCase__						)
									_UpperCAmelCase					=      {"input_ids": input_ids, "attention_mask": input_mask}

									_UpperCAmelCase					=      model(lowerCAmelCase__						)

									_UpperCAmelCase					=      [input_ids, input_mask]

									_UpperCAmelCase					=      model(lowerCAmelCase__						)

									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						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      TFDistilBertForMaskedLM(config=lowerCAmelCase__						)
									_UpperCAmelCase					=      {"input_ids": input_ids, "attention_mask": input_mask}
									_UpperCAmelCase					=      model(lowerCAmelCase__						)
									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						)			->	str:




									"""simple docstring"""





									_UpperCAmelCase					=      TFDistilBertForQuestionAnswering(config=lowerCAmelCase__						)
									_UpperCAmelCase					=      {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									}
									_UpperCAmelCase					=      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      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)			->	Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      self.num_labels
									_UpperCAmelCase					=      TFDistilBertForSequenceClassification(lowerCAmelCase__						)
									_UpperCAmelCase					=      {"input_ids": input_ids, "attention_mask": input_mask}
									_UpperCAmelCase					=      model(lowerCAmelCase__						)
									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						)			->	Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=      self.num_choices
									_UpperCAmelCase					=      TFDistilBertForMultipleChoice(lowerCAmelCase__						)
									_UpperCAmelCase					=      tf.tile(tf.expand_dims(lowerCAmelCase__      ,   1						)      ,   (1, self.num_choices, 1)						)
									_UpperCAmelCase					=      tf.tile(tf.expand_dims(lowerCAmelCase__      ,   1						)      ,   (1, self.num_choices, 1)						)
									_UpperCAmelCase					=      {
									    "input_ids": multiple_choice_inputs_ids,
									    "attention_mask": multiple_choice_input_mask,
									}
									_UpperCAmelCase					=      model(lowerCAmelCase__						)
									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						)			->	List[str]:




									"""simple docstring"""





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






		def      UpperCAmelCase__    (      self						)			->	int:




									"""simple docstring"""





									_UpperCAmelCase					=      self.prepare_config_and_inputs()
									(_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     :  Optional[int]           =     (
		    (
		        TFDistilBertModel,
		        TFDistilBertForMaskedLM,
		        TFDistilBertForQuestionAnswering,
		        TFDistilBertForSequenceClassification,
		        TFDistilBertForTokenClassification,
		        TFDistilBertForMultipleChoice,
		    )
		    if is_tf_available()
		    else None
		)
		_a     :  Dict           =     (
		    {
		        '''feature-extraction''': TFDistilBertModel,
		        '''fill-mask''': TFDistilBertForMaskedLM,
		        '''question-answering''': TFDistilBertForQuestionAnswering,
		        '''text-classification''': TFDistilBertForSequenceClassification,
		        '''token-classification''': TFDistilBertForTokenClassification,
		        '''zero-shot''': TFDistilBertForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		_a     :  Any           =     False
		_a     :  Union[str, Any]           =     False
		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





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




									"""simple docstring"""





									self.config_tester.run_common_tests()
		def      UpperCAmelCase__    (      self						)			->	Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase__						)
		def      UpperCAmelCase__    (      self						)			->	Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase__						)
		def      UpperCAmelCase__    (      self						)			->	Any:




									"""simple docstring"""





									_UpperCAmelCase					=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase__						)
		def      UpperCAmelCase__    (      self						)			->	List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase__						)
		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(*lowerCAmelCase__						)
		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(*lowerCAmelCase__						)






		@slow
		def      UpperCAmelCase__    (      self						)			->	str:




									"""simple docstring"""





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



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




									"""simple docstring"""





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

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

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




import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin

class 		__a				(    UpperCAmelCase      ):
		_a     :  Optional[int]           =     'MCTCTFeatureExtractor'
		_a     :  int           =     'AutoTokenizer'
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)			->	List[str]:




									"""simple docstring"""





									super().__init__(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      self.feature_extractor
									_UpperCAmelCase					=      False
		def __call__(      self      ,   *_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)			->	Tuple:




									"""simple docstring"""





									if self._in_target_context_manager:
																return self.current_processor(*_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)

									if "raw_speech" in kwargs:
																warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'						)
																_UpperCAmelCase					=      kwargs.pop('raw_speech'						)
									else:
																_UpperCAmelCase					=      kwargs.pop('audio'      ,   _SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      kwargs.pop('sampling_rate'      ,   _SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      kwargs.pop('text'      ,   _SCREAMING_SNAKE_CASE						)
									if len(_SCREAMING_SNAKE_CASE						) > 0:
																_UpperCAmelCase					=      args[0]
																_UpperCAmelCase					=      args[1:]

									if audio is None and text is None:
																raise ValueError('You need to specify either an `audio` or `text` input to process.'						)

									if audio is not None:
																_UpperCAmelCase					=      self.feature_extractor(_SCREAMING_SNAKE_CASE      ,   *_SCREAMING_SNAKE_CASE      ,   sampling_rate=_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)
									if text is not None:
																_UpperCAmelCase					=      self.tokenizer(_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)

									if text is None:
																return inputs
									elif audio is None:
																return encodings
									else:
																_UpperCAmelCase					=      encodings['input_ids']
																return inputs
		def      UpperCAmelCase__    (      self      ,   *_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)			->	Optional[int]:




									"""simple docstring"""





									return self.tokenizer.batch_decode(*_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self      ,   *_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)			->	str:




									"""simple docstring"""





									if self._in_target_context_manager:
																return self.current_processor.pad(*_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase					=      kwargs.pop('input_features'      ,   _SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      kwargs.pop('labels'      ,   _SCREAMING_SNAKE_CASE						)
									if len(_SCREAMING_SNAKE_CASE						) > 0:
																_UpperCAmelCase					=      args[0]
																_UpperCAmelCase					=      args[1:]

									if input_features is not None:
																_UpperCAmelCase					=      self.feature_extractor.pad(_SCREAMING_SNAKE_CASE      ,   *_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)
									if labels is not None:
																_UpperCAmelCase					=      self.tokenizer.pad(_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)

									if labels is None:
																return input_features
									elif input_features is None:
																return labels
									else:
																_UpperCAmelCase					=      labels['input_ids']
																return input_features
		def      UpperCAmelCase__    (      self      ,   *_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)			->	int:




									"""simple docstring"""





									return self.tokenizer.decode(*_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)






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




									"""simple docstring"""





									warnings.warn(
									    '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
									    'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
									    'your audio inputs, or in a separate call.'						)
									_UpperCAmelCase					=      True
									_UpperCAmelCase					=      self.tokenizer
									yield
									_UpperCAmelCase					=      self.feature_extractor
									_UpperCAmelCase					=      False
