

import warnings

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__       = logging.get_logger(__name__)

lowerCAmelCase__       = {
    "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json",
    "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json",
}






class 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						"""xlnet"""
 UpperCAmelCase							    =						["""mems"""]
 UpperCAmelCase							    =						{
     """n_token""": """vocab_size""",  # Backward compatibility
     """hidden_size""": """d_model""",
     """num_attention_heads""": """n_head""",
     """num_hidden_layers""": """n_layer""",
 }


 def __init__(						self		: int			,						lowerCAmelCase__		: List[Any]=3_2_0_0_0			,						lowerCAmelCase__		: Optional[Any]=1_0_2_4			,						lowerCAmelCase__		: str=2_4			,						lowerCAmelCase__		: Optional[Any]=1_6			,						lowerCAmelCase__		: List[Any]=4_0_9_6			,						lowerCAmelCase__		: List[Any]="gelu"			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: List[str]="bi"			,						lowerCAmelCase__		: Dict=0.02			,						lowerCAmelCase__		: Tuple=1e-1_2			,						lowerCAmelCase__		: str=0.1			,						lowerCAmelCase__		: Union[str, Any]=5_1_2			,						lowerCAmelCase__		: List[Any]=None			,						lowerCAmelCase__		: Dict=True			,						lowerCAmelCase__		: Dict=False			,						lowerCAmelCase__		: str=False			,						lowerCAmelCase__		: Tuple=-1			,						lowerCAmelCase__		: str=False			,						lowerCAmelCase__		: Any="last"			,						lowerCAmelCase__		: Union[str, Any]=True			,						lowerCAmelCase__		: Union[str, Any]="tanh"			,						lowerCAmelCase__		: int=0.1			,						lowerCAmelCase__		: Optional[int]=5			,						lowerCAmelCase__		: List[str]=5			,						lowerCAmelCase__		: Dict=5			,						lowerCAmelCase__		: str=1			,						lowerCAmelCase__		: Dict=2			,						**lowerCAmelCase__		: Tuple			,						)    ->      Any:
   UpperCAmelCase					      =  vocab_size
   UpperCAmelCase					      =  d_model
   UpperCAmelCase					      =  n_layer
   UpperCAmelCase					      =  n_head
   if d_model % n_head != 0:
     raise ValueError(f"'d_model % n_head' ({d_model % n_head}) should be equal to 0"    )
   if "d_head" in kwargs:
     if kwargs["d_head"] != d_model // n_head:
       raise ValueError(
           f"`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})"    )
   UpperCAmelCase					      =  d_model // n_head
   UpperCAmelCase					      =  ff_activation
   UpperCAmelCase					      =  d_inner
   UpperCAmelCase					      =  untie_r
   UpperCAmelCase					      =  attn_type

   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  layer_norm_eps

   UpperCAmelCase					      =  dropout
   UpperCAmelCase					      =  mem_len
   UpperCAmelCase					      =  reuse_len
   UpperCAmelCase					      =  bi_data
   UpperCAmelCase					      =  clamp_len
   UpperCAmelCase					      =  same_length

   UpperCAmelCase					      =  summary_type
   UpperCAmelCase					      =  summary_use_proj
   UpperCAmelCase					      =  summary_activation
   UpperCAmelCase					      =  summary_last_dropout
   UpperCAmelCase					      =  start_n_top
   UpperCAmelCase					      =  end_n_top

   UpperCAmelCase					      =  bos_token_id
   UpperCAmelCase					      =  pad_token_id
   UpperCAmelCase					      =  eos_token_id

   if "use_cache" in kwargs:
     warnings.warn(
         "The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`"
         " instead."			,						lowerCAmelCase__			,						)
     UpperCAmelCase					      =  kwargs["use_cache"]

   UpperCAmelCase					      =  use_mems_eval
   UpperCAmelCase					      =  use_mems_train
   super().__init__(pad_token_id=lowerCAmelCase__			,						bos_token_id=lowerCAmelCase__			,						eos_token_id=lowerCAmelCase__			,						**lowerCAmelCase__    )


 @property
 def _UpperCamelCase	(						self		: Dict    )    ->      List[Any]:
   logger.info(f"The model {self.model_type} is one of the few models that has no sequence length limit."    )
   return -1


 @max_position_embeddings.setter
 def _UpperCamelCase	(						self		: List[str]			,						lowerCAmelCase__		: int    )    ->      Optional[int]:
   # Message copied from Transformer-XL documentation
   raise NotImplementedError(
       f"The model {self.model_type} is one of the few models that has no sequence length limit."    )






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


lowerCAmelCase__       = get_tests_dir("fixtures")






class 						__magic_name__    (       unittest.TestCase    ):


 def _UpperCamelCase	(						self		: Tuple    )    ->      Union[str, Any]:
   # A mock response for an HTTP head request to emulate server down
   UpperCAmelCase					      =  mock.Mock()
   UpperCAmelCase					      =  5_0_0
   UpperCAmelCase					      =  {}
   UpperCAmelCase					      =  HTTPError
   UpperCAmelCase					      =  {}

   # Download this model to make sure it's in the cache.
   UpperCAmelCase					      =  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=lowerCAmelCase__    ) as mock_head:
     UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2"    )
     # This check we did call the fake head request
     mock_head.assert_called()


 def _UpperCamelCase	(						self		: List[Any]    )    ->      Dict:
   # This test is for deprecated behavior and can be removed in v5
   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(
       "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"    )






@is_staging_test
class 						__magic_name__    (       unittest.TestCase    ):


 @classmethod
 def _UpperCamelCase	(						cls		: List[str]    )    ->      List[Any]:
   UpperCAmelCase					      =  TOKEN
   HfFolder.save_token(lowerCAmelCase__    )


 @classmethod
 def _UpperCamelCase	(						cls		: Optional[int]    )    ->      Union[str, Any]:
   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 _UpperCamelCase	(						self		: Any    )    ->      Any:
   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__    )
   feature_extractor.push_to_hub("test-feature-extractor"			,						use_auth_token=self._token    )

   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"    )
   for k, v in feature_extractor.__dict__.items():
     self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )

   # 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(
         lowerCAmelCase__			,						repo_id="test-feature-extractor"			,						push_to_hub=lowerCAmelCase__			,						use_auth_token=self._token    )

   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"    )
   for k, v in feature_extractor.__dict__.items():
     self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )


 def _UpperCamelCase	(						self		: List[Any]    )    ->      Tuple:
   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__    )
   feature_extractor.push_to_hub("valid_org/test-feature-extractor"			,						use_auth_token=self._token    )

   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor"    )
   for k, v in feature_extractor.__dict__.items():
     self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )

   # 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(
         lowerCAmelCase__			,						repo_id="valid_org/test-feature-extractor-org"			,						push_to_hub=lowerCAmelCase__			,						use_auth_token=self._token    )

   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org"    )
   for k, v in feature_extractor.__dict__.items():
     self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )


 def _UpperCamelCase	(						self		: Dict    )    ->      List[str]:
   CustomFeatureExtractor.register_for_auto_class()
   UpperCAmelCase					      =  CustomFeatureExtractor.from_pretrained(lowerCAmelCase__    )

   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"}			,						)

   UpperCAmelCase					      =  AutoFeatureExtractor.from_pretrained(
       f"{USER}/test-dynamic-feature-extractor"			,						trust_remote_code=lowerCAmelCase__    )
   # 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"    )



