



"""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  _UpperCAmelCase :

 def __init__(  self	: Optional[int]						,  lowercase_	: int						,  ):
     snake_case_				:  Dict       =			parent
     snake_case_				:  Dict       =			13
     snake_case_				:  Tuple       =			7
     snake_case_				:  Dict       =			True
     snake_case_				:  List[Any]       =			True
     snake_case_				:  str       =			False
     snake_case_				:  List[str]       =			True
     snake_case_				:  Tuple       =			99
     snake_case_				:  Dict       =			32
     snake_case_				:  Optional[int]       =			2
     snake_case_				:  List[str]       =			4
     snake_case_				:  Tuple       =			37
     snake_case_				:  Dict       =			'''gelu'''
     snake_case_				:  Any       =			0.1
     snake_case_				:  List[Any]       =			0.1
     snake_case_				:  str       =			512
     snake_case_				:  Optional[Any]       =			16
     snake_case_				:  int       =			2
     snake_case_				:  str       =			0.02
     snake_case_				:  Union[str, Any]       =			3
     snake_case_				:  Optional[int]       =			4
     snake_case_				:  Tuple       =			None

 def 		_snake_case      (  self	: Optional[int]       ):
     snake_case_				:  str       =			ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size       )

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

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

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

     snake_case_				:  Optional[Any]       =			model(lowercase_       )

     snake_case_				:  Tuple       =			[input_ids, input_mask]

     snake_case_				:  List[Any]       =			model(lowercase_       )

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

 def 		_snake_case      (  self	: str						,  lowercase_	: Optional[int]						,  lowercase_	: Optional[int]						,  lowercase_	: int						,  lowercase_	: Optional[int]						,  lowercase_	: List[Any]						,  lowercase_	: int       ):
     snake_case_				:  List[Any]       =			TFDistilBertForMaskedLM(config=lowercase_       )
     snake_case_				:  Dict       =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
     snake_case_				:  List[Any]       =			model(lowercase_       )
     self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.vocab_size)       )

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

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

 def 		_snake_case      (  self	: Any						,  lowercase_	: int						,  lowercase_	: Tuple						,  lowercase_	: Optional[Any]						,  lowercase_	: List[str]						,  lowercase_	: List[Any]						,  lowercase_	: Optional[Any]       ):
     snake_case_				:  Union[str, Any]       =			self.num_labels
     snake_case_				:  Dict       =			TFDistilBertForTokenClassification(lowercase_       )
     snake_case_				:  Union[str, Any]       =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
     snake_case_				:  Union[str, Any]       =			model(lowercase_       )
     self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.num_labels)       )






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





@require_tf
class  _UpperCAmelCase (    lowerCAmelCase__					,  lowerCAmelCase__					,  unittest.TestCase):
 _lowerCAmelCase     :    Optional[Any]	      =	(
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 _lowerCAmelCase     :    Tuple	      =	(
     {
         """feature-extraction""": TFDistilBertModel,
         """fill-mask""": TFDistilBertForMaskedLM,
         """question-answering""": TFDistilBertForQuestionAnswering,
         """text-classification""": TFDistilBertForSequenceClassification,
         """token-classification""": TFDistilBertForTokenClassification,
         """zero-shot""": TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 _lowerCAmelCase     :    Dict	      =	False
 _lowerCAmelCase     :    str	      =	False

 def 		_snake_case      (  self	: int       ):
     snake_case_				:  Tuple       =			TFDistilBertModelTester(self       )
     snake_case_				:  str       =			ConfigTester(self						,  config_class=lowercase_						,  dim=37       )

 def 		_snake_case      (  self	: Optional[Any]       ):
     self.config_tester.run_common_tests()

 def 		_snake_case      (  self	: Any       ):
     snake_case_				:  Any       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_model(*lowercase_       )

 def 		_snake_case      (  self	: Union[str, Any]       ):
     snake_case_				:  Optional[Any]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase_       )

 def 		_snake_case      (  self	: int       ):
     snake_case_				:  str       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase_       )

 def 		_snake_case      (  self	: Dict       ):
     snake_case_				:  List[Any]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase_       )

 def 		_snake_case      (  self	: Optional[int]       ):
     snake_case_				:  Dict       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase_       )

 def 		_snake_case      (  self	: Optional[Any]       ):
     snake_case_				:  List[Any]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase_       )






 @slow
 def 		_snake_case      (  self	: str       ):
     for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]       ):
         snake_case_				:  Optional[int]       =			TFDistilBertModel.from_pretrained(lowercase_       )
         self.assertIsNotNone(lowercase_       )





@require_tf
class  _UpperCAmelCase (    unittest.TestCase):

 @slow
 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  Dict       =			TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''       )
     snake_case_				:  Union[str, Any]       =			tf.constant([[0, 1, 2, 3, 4, 5]]       )
     snake_case_				:  Dict       =			model(lowercase_       )[0]

     snake_case_				:  Optional[Any]       =			[1, 6, 768]
     self.assertEqual(output.shape						,  lowercase_       )

     snake_case_				:  Dict       =			tf.constant(
         [
             [
                 [0.19_26_18_85, -0.13_73_29_55, 0.4_11_97_99],
                 [0.22_15_01_56, -0.07_42_26_61, 0.39_03_72_04],
                 [0.22_75_60_18, -0.0_89_64_14, 0.3_70_14_67],
             ]
         ]       )
     tf.debugging.assert_near(output[:, :3, :3]						,  lowercase_						,  atol=1E-4       )










"""simple docstring"""

import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / '''utils'''))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


lowercase__					:						Any												=			get_tests_dir('''fixtures''')




class  _UpperCAmelCase (    unittest.TestCase):

 def 		_snake_case      (  self	: Tuple       ):
     # A mock response for an HTTP head request to emulate server down
     snake_case_				:  str       =			mock.Mock()
     snake_case_				:  Optional[Any]       =			500
     snake_case_				:  str       =			{}
     snake_case_				:  Optional[int]       =			HTTPError
     snake_case_				:  Tuple       =			{}

     # Download this model to make sure it's in the cache.
     snake_case_				:  List[str]       =			WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2'''       )
     # Under the mock environment we get a 500 error when trying to reach the model.
     with mock.patch('''requests.Session.request'''						,  return_value=lowercase_       ) as mock_head:
         snake_case_				:  int       =			WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2'''       )
         # This check we did call the fake head request
         mock_head.assert_called()






 def 		_snake_case      (  self	: Union[str, Any]       ):
     # This test is for deprecated behavior and can be removed in v5
     snake_case_				:  str       =			WavaVecaFeatureExtractor.from_pretrained(
         '''https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json'''       )





@is_staging_test
class  _UpperCAmelCase (    unittest.TestCase):

 @classmethod
 def 		_snake_case      (  cls	: int       ):
     snake_case_				:  Dict       =			TOKEN
     HfFolder.save_token(lowercase_       )

 @classmethod
 def 		_snake_case      (  cls	: str       ):
     try:
         delete_repo(token=cls._token						,  repo_id='''test-feature-extractor'''       )
     except HTTPError:
         pass

     try:
         delete_repo(token=cls._token						,  repo_id='''valid_org/test-feature-extractor-org'''       )
     except HTTPError:
         pass

     try:
         delete_repo(token=cls._token						,  repo_id='''test-dynamic-feature-extractor'''       )
     except HTTPError:
         pass

 def 		_snake_case      (  self	: Any       ):
     snake_case_				:  int       =			WavaVecaFeatureExtractor.from_pretrained(lowercase_       )
     feature_extractor.push_to_hub('''test-feature-extractor'''						,  use_auth_token=self._token       )

     snake_case_				:  Optional[Any]       =			WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"       )
     for k, v in feature_extractor.__dict__.items():
         self.assertEqual(lowercase_						,  getattr(lowercase_						,  lowercase_       )       )

     # Reset repo
     delete_repo(token=self._token						,  repo_id='''test-feature-extractor'''       )

     # Push to hub via save_pretrained
     with tempfile.TemporaryDirectory() as tmp_dir:
         feature_extractor.save_pretrained(
             lowercase_						,  repo_id='''test-feature-extractor'''						,  push_to_hub=lowercase_						,  use_auth_token=self._token       )

     snake_case_				:  Optional[int]       =			WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"       )
     for k, v in feature_extractor.__dict__.items():
         self.assertEqual(lowercase_						,  getattr(lowercase_						,  lowercase_       )       )

 def 		_snake_case      (  self	: Dict       ):
     snake_case_				:  Union[str, Any]       =			WavaVecaFeatureExtractor.from_pretrained(lowercase_       )
     feature_extractor.push_to_hub('''valid_org/test-feature-extractor'''						,  use_auth_token=self._token       )

     snake_case_				:  Optional[int]       =			WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor'''       )
     for k, v in feature_extractor.__dict__.items():
         self.assertEqual(lowercase_						,  getattr(lowercase_						,  lowercase_       )       )

     # Reset repo
     delete_repo(token=self._token						,  repo_id='''valid_org/test-feature-extractor'''       )

     # Push to hub via save_pretrained
     with tempfile.TemporaryDirectory() as tmp_dir:
         feature_extractor.save_pretrained(
             lowercase_						,  repo_id='''valid_org/test-feature-extractor-org'''						,  push_to_hub=lowercase_						,  use_auth_token=self._token       )

     snake_case_				:  int       =			WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor-org'''       )
     for k, v in feature_extractor.__dict__.items():
         self.assertEqual(lowercase_						,  getattr(lowercase_						,  lowercase_       )       )






 def 		_snake_case      (  self	: str       ):
     CustomFeatureExtractor.register_for_auto_class()
     snake_case_				:  Optional[Any]       =			CustomFeatureExtractor.from_pretrained(lowercase_       )

     feature_extractor.push_to_hub('''test-dynamic-feature-extractor'''						,  use_auth_token=self._token       )

     # This has added the proper auto_map field to the config
     self.assertDictEqual(
         feature_extractor.auto_map						,  {'''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor'''}						,  )

     snake_case_				:  Any       =			AutoFeatureExtractor.from_pretrained(
         f"{USER}/test-dynamic-feature-extractor"						,  trust_remote_code=lowercase_       )
     # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
     self.assertEqual(new_feature_extractor.__class__.__name__						,  '''CustomFeatureExtractor'''       )





