

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 	snake_case_   :







			'''simple docstring'''

			def __init__(     self :				Optional[Any]							,   _UpperCamelCase :				Union[str, Any]							,   )      ->Optional[Any]:
									snake_case_   =	parent
									snake_case_   =	1_3
									snake_case_   =	7
									snake_case_   =	True
									snake_case_   =	True
									snake_case_   =	False
									snake_case_   =	True
									snake_case_   =	9_9
									snake_case_   =	3_2
									snake_case_   =	2
									snake_case_   =	4
									snake_case_   =	3_7
									snake_case_   =	'''gelu'''
									snake_case_   =	0.1
									snake_case_   =	0.1
									snake_case_   =	5_1_2
									snake_case_   =	1_6
									snake_case_   =	2
									snake_case_   =	0.02
									snake_case_   =	3
									snake_case_   =	4
									snake_case_   =	None







			def 				snake_case__(     self :				str							)      ->str:
									snake_case_   =	ids_tensor([self.batch_size, self.seq_length]							,   self.vocab_size							)

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

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

									snake_case_   =	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 :				Dict							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				List[str]							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				Dict							,   _UpperCamelCase :				Dict							)      ->int:
									snake_case_   =	TFDistilBertModel(config=_UpperCamelCase							)
									snake_case_   =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}

									snake_case_   =	model(_UpperCamelCase							)

									snake_case_   =	[input_ids, input_mask]

									snake_case_   =	model(_UpperCamelCase							)

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







			def 				snake_case__(     self :				Tuple							,   _UpperCamelCase :				List[str]							,   _UpperCamelCase :				Any							,   _UpperCamelCase :				Dict							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				Tuple							,   _UpperCamelCase :				Union[str, Any]							)      ->Dict:
									snake_case_   =	TFDistilBertForMaskedLM(config=_UpperCamelCase							)
									snake_case_   =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									snake_case_   =	model(_UpperCamelCase							)
									self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.seq_length, self.vocab_size)							)







			def 				snake_case__(     self :				Tuple							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				str							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				Dict							)      ->str:
									snake_case_   =	TFDistilBertForQuestionAnswering(config=_UpperCamelCase							)
									snake_case_   =	{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									}
									snake_case_   =	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 				snake_case__(     self :				Union[str, Any]							,   _UpperCamelCase :				Any							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				str							,   _UpperCamelCase :				Any							,   _UpperCamelCase :				Optional[int]							)      ->List[str]:
									snake_case_   =	self.num_labels
									snake_case_   =	TFDistilBertForSequenceClassification(_UpperCamelCase							)
									snake_case_   =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									snake_case_   =	model(_UpperCamelCase							)
									self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.num_labels)							)







			def 				snake_case__(     self :				Optional[Any]							,   _UpperCamelCase :				Any							,   _UpperCamelCase :				int							,   _UpperCamelCase :				List[Any]							,   _UpperCamelCase :				str							,   _UpperCamelCase :				Any							,   _UpperCamelCase :				str							)      ->Optional[Any]:
									snake_case_   =	self.num_choices
									snake_case_   =	TFDistilBertForMultipleChoice(_UpperCamelCase							)
									snake_case_   =	tf.tile(tf.expand_dims(_UpperCamelCase							,   1							)							,   (1, self.num_choices, 1)							)
									snake_case_   =	tf.tile(tf.expand_dims(_UpperCamelCase							,   1							)							,   (1, self.num_choices, 1)							)
									snake_case_   =	{
									    '''input_ids''': multiple_choice_inputs_ids,
									    '''attention_mask''': multiple_choice_input_mask,
									}
									snake_case_   =	model(_UpperCamelCase							)
									self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.num_choices)							)







			def 				snake_case__(     self :				Optional[int]							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				Optional[int]							,   _UpperCamelCase :				int							,   _UpperCamelCase :				List[Any]							,   _UpperCamelCase :				int							,   _UpperCamelCase :				List[str]							)      ->int:
									snake_case_   =	self.num_labels
									snake_case_   =	TFDistilBertForTokenClassification(_UpperCamelCase							)
									snake_case_   =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									snake_case_   =	model(_UpperCamelCase							)
									self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.seq_length, self.num_labels)							)







			def 				snake_case__(     self :				int							)      ->Optional[Any]:
									snake_case_   =	self.prepare_config_and_inputs()
									((snake_case_),    (snake_case_),    (snake_case_),    (snake_case_),    (snake_case_),    (snake_case_))   =	config_and_inputs
									snake_case_   =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_tf
class 	snake_case_   (		__A	,       __A	,       unittest.TestCase					):







			'''simple docstring'''

			SCREAMING_SNAKE_CASE     :   List[Any]									=				(
			    (
			        TFDistilBertModel,
			        TFDistilBertForMaskedLM,
			        TFDistilBertForQuestionAnswering,
			        TFDistilBertForSequenceClassification,
			        TFDistilBertForTokenClassification,
			        TFDistilBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else None
			)
			SCREAMING_SNAKE_CASE     :   List[Any]									=				(
			    {
			        'feature-extraction': TFDistilBertModel,
			        'fill-mask': TFDistilBertForMaskedLM,
			        'question-answering': TFDistilBertForQuestionAnswering,
			        'text-classification': TFDistilBertForSequenceClassification,
			        'token-classification': TFDistilBertForTokenClassification,
			        'zero-shot': TFDistilBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			SCREAMING_SNAKE_CASE     :   List[Any]									=				False
			SCREAMING_SNAKE_CASE     :   Dict									=				False







			def 				snake_case__(     self :				int							)      ->Tuple:
									snake_case_   =	TFDistilBertModelTester(self							)
									snake_case_   =	ConfigTester(self							,   config_class=_UpperCamelCase							,   dim=3_7							)







			def 				snake_case__(     self :				int							)      ->Tuple:
									self.config_tester.run_common_tests()







			def 				snake_case__(     self :				Any							)      ->Tuple:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_model(*_UpperCamelCase							)







			def 				snake_case__(     self :				Tuple							)      ->List[str]:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_masked_lm(*_UpperCamelCase							)







			def 				snake_case__(     self :				List[str]							)      ->Optional[Any]:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_question_answering(*_UpperCamelCase							)







			def 				snake_case__(     self :				int							)      ->Optional[Any]:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_sequence_classification(*_UpperCamelCase							)







			def 				snake_case__(     self :				Union[str, Any]							)      ->List[Any]:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_multiple_choice(*_UpperCamelCase							)







			def 				snake_case__(     self :				Tuple							)      ->Optional[int]:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_token_classification(*_UpperCamelCase							)







			@slow
			def 				snake_case__(     self :				Dict							)      ->int:



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






@require_tf
class 	snake_case_   (		unittest.TestCase					):







			'''simple docstring'''

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

									snake_case_   =	[1, 6, 7_6_8]
									self.assertEqual(output.shape							,   _UpperCamelCase							)

									snake_case_   =	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'''





import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin






class __A			(  A						):



 '''simple docstring'''







 __lowerCamelCase    :       Optional[Any]          =       'MCTCTFeatureExtractor'
 __lowerCamelCase    :       Optional[Any]          =       'AutoTokenizer'



 def __init__(self ,   A ,   A       )    ->     Dict:


      """simple docstring"""



      super().__init__(A ,   A       )
      _a      =  self.feature_extractor
      _a      =  False



 def __call__(self ,   *A ,   **A       )    ->     Optional[int]:


      """simple docstring"""



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

      if "raw_speech" in kwargs:
           warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''       )
           _a      =  kwargs.pop('''raw_speech'''       )
      else:
           _a      =  kwargs.pop('''audio''' ,   A       )
      _a      =  kwargs.pop('''sampling_rate''' ,   A       )
      _a      =  kwargs.pop('''text''' ,   A       )
      if len(A       ) > 0:
           _a      =  args[0]
           _a      =  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:
           _a      =  self.feature_extractor(A ,   *A ,   sampling_rate=A ,   **A       )
      if text is not None:
           _a      =  self.tokenizer(A ,   **A       )

      if text is None:
           return inputs
      elif audio is None:
           return encodings
      else:
           _a      =  encodings['''input_ids''']
           return inputs



 def 					a__  (self ,   *A ,   **A       )    ->     int:


      """simple docstring"""



      return self.tokenizer.batch_decode(*A ,   **A       )



 def 					a__  (self ,   *A ,   **A       )    ->     Tuple:


      """simple docstring"""



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

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

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

      if labels is None:
           return input_features
      elif input_features is None:
           return labels
      else:
           _a      =  labels['''input_ids''']
           return input_features



 def 					a__  (self ,   *A ,   **A       )    ->     List[str]:


      """simple docstring"""



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



 @contextmanager
 def 					a__  (self       )    ->     Dict:


      """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.'''       )
      _a      =  True
      _a      =  self.tokenizer
      yield
      _a      =  self.feature_extractor
      _a      =  False



