

'''simple docstring'''




from typing import List, Optional, Union

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


_lowerCAmelCase         =						logging.get_logger(__name__)

_lowerCAmelCase         =						{
    "huggingface/time-series-transformer-tourism-monthly": (
        "https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json"
    ),
    # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
}






class   _SCREAMING_SNAKE_CASE   (   snake_case__     ):
 __SCREAMING_SNAKE_CASE					:Optional[int]      	=					"""time_series_transformer"""
 __SCREAMING_SNAKE_CASE					:List[Any]      	=					{
     """hidden_size""": """d_model""",
     """num_attention_heads""": """encoder_attention_heads""",
     """num_hidden_layers""": """encoder_layers""",
 }



 def __init__(   self		:   List[Any]			,				a__		:   List[Any] = None			,				a__		:   Optional[Any] = None			,				a__		:   Optional[Any] = "student_t"			,				a__		:   Dict = "nll"			,				a__		:   Optional[Any] = 1			,				a__		:   List[Any] = [1, 2, 3, 4, 5, 6, 7]			,				a__		:   Optional[int] = "mean"			,				a__		:   List[Any] = 0			,				a__		:   Tuple = 0			,				a__		:   Optional[int] = 0			,				a__		:   str = 0			,				a__		:   Optional[int] = None			,				a__		:   List[str] = None			,				a__		:   Union[str, Any] = 32			,				a__		:   List[Any] = 32			,				a__		:   Tuple = 2			,				a__		:   Dict = 2			,				a__		:   Any = 2			,				a__		:   str = 2			,				a__		:   Optional[Any] = True			,				a__		:   Tuple = "gelu"			,				a__		:   List[str] = 64			,				a__		:   List[str] = 0.1			,				a__		:   Tuple = 0.1			,				a__		:   List[str] = 0.1			,				a__		:   Any = 0.1			,				a__		:   List[Any] = 0.1			,				a__		:   List[str] = 100			,				a__		:   int = 0.02			,				a__		:   str=True			,				**a__		:   str			,				):
      __magic_name__						       =    prediction_length
      __magic_name__						       =    context_length or prediction_length
      __magic_name__						       =    distribution_output
      __magic_name__						       =    loss
      __magic_name__						       =    input_size
      __magic_name__						       =    num_time_features
      __magic_name__						       =    lags_sequence
      __magic_name__						       =    scaling
      __magic_name__						       =    num_dynamic_real_features
      __magic_name__						       =    num_static_real_features
      __magic_name__						       =    num_static_categorical_features
      if cardinality and num_static_categorical_features > 0:
           if len(_A      ) != num_static_categorical_features:
                raise ValueError(
                    '''The cardinality should be a list of the same length as `num_static_categorical_features`'''      )
           __magic_name__						       =    cardinality
      else:
           __magic_name__						       =    [0]
      if embedding_dimension and num_static_categorical_features > 0:
           if len(_A      ) != num_static_categorical_features:
                raise ValueError(
                    '''The embedding dimension should be a list of the same length as `num_static_categorical_features`'''      )
           __magic_name__						       =    embedding_dimension
      else:
           __magic_name__						       =    [min(50			,				(cat + 1) // 2      ) for cat in self.cardinality]
      __magic_name__						       =    num_parallel_samples

      # Transformer architecture configuration
      __magic_name__						       =    input_size * len(_A      ) + self._number_of_features
      __magic_name__						       =    d_model
      __magic_name__						       =    encoder_attention_heads
      __magic_name__						       =    decoder_attention_heads
      __magic_name__						       =    encoder_ffn_dim
      __magic_name__						       =    decoder_ffn_dim
      __magic_name__						       =    encoder_layers
      __magic_name__						       =    decoder_layers

      __magic_name__						       =    dropout
      __magic_name__						       =    attention_dropout
      __magic_name__						       =    activation_dropout
      __magic_name__						       =    encoder_layerdrop
      __magic_name__						       =    decoder_layerdrop

      __magic_name__						       =    activation_function
      __magic_name__						       =    init_std

      __magic_name__						       =    use_cache

      super().__init__(is_encoder_decoder=_A			,				**_A      )



 @property
 def 			snake_case__  (   self		:   Optional[int]      ):
      return (
          sum(self.embedding_dimension      )
          + self.num_dynamic_real_features
          + self.num_time_features
          + self.num_static_real_features
          + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
      )




'''simple docstring'''




import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


_lowerCAmelCase         =						get_tests_dir("fixtures")
_lowerCAmelCase         =						get_tests_dir("fixtures/dummy_feature_extractor_config.json")
_lowerCAmelCase         =						get_tests_dir("fixtures/dummy-config.json")






class   _SCREAMING_SNAKE_CASE   (   unittest.TestCase     ):



 def 			snake_case__  (   self		:   Union[str, Any]      ):
      __magic_name__						       =    0



 def 			snake_case__  (   self		:   Optional[int]      ):
      __magic_name__						       =    AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''      )
      self.assertIsInstance(a__			,				a__      )



 def 			snake_case__  (   self		:   Optional[int]      ):
      __magic_name__						       =    AutoFeatureExtractor.from_pretrained(a__      )
      self.assertIsInstance(a__			,				a__      )



 def 			snake_case__  (   self		:   Optional[Any]      ):
      with tempfile.TemporaryDirectory() as tmpdirname:
           __magic_name__						       =    WavaVecaConfig()

           # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(a__      ).to_dict()

           config_dict.pop('''feature_extractor_type'''      )
           __magic_name__						       =    WavaVecaFeatureExtractor(**a__      )

           # save in new folder
           model_config.save_pretrained(a__      )
           config.save_pretrained(a__      )

           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(a__      )

           # make sure private variable is not incorrectly saved
           __magic_name__						       =    json.loads(config.to_json_string()      )
           self.assertTrue('''_processor_class''' not in dict_as_saved      )

      self.assertIsInstance(a__			,				a__      )



 def 			snake_case__  (   self		:   Optional[Any]      ):
      __magic_name__						       =    AutoFeatureExtractor.from_pretrained(a__      )
      self.assertIsInstance(a__			,				a__      )



 def 			snake_case__  (   self		:   str      ):
      with self.assertRaisesRegex(
          a__			,				'''bert-base is not a local folder and is not a valid model identifier'''      ):
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained('''bert-base'''      )



 def 			snake_case__  (   self		:   str      ):
      with self.assertRaisesRegex(
          a__			,				r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''      ):
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(a__			,				revision='''aaaaaa'''      )



 def 			snake_case__  (   self		:   Union[str, Any]      ):
      with self.assertRaisesRegex(
          a__			,				'''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''			,				):
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model'''      )



 def 			snake_case__  (   self		:   Dict      ):
      # If remote code is not set, we will time out when asking whether to load the model.
      with self.assertRaises(a__      ):
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(
               '''hf-internal-testing/test_dynamic_feature_extractor'''      )
      # If remote code is disabled, we can't load this config.
      with self.assertRaises(a__      ):
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(
               '''hf-internal-testing/test_dynamic_feature_extractor'''			,				trust_remote_code=a__      )

      __magic_name__						       =    AutoFeatureExtractor.from_pretrained(
          '''hf-internal-testing/test_dynamic_feature_extractor'''			,				trust_remote_code=a__      )
      self.assertEqual(feature_extractor.__class__.__name__			,				'''NewFeatureExtractor'''      )

      # Test feature extractor can be reloaded.
      with tempfile.TemporaryDirectory() as tmp_dir:
           feature_extractor.save_pretrained(a__      )
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(a__			,				trust_remote_code=a__      )
      self.assertEqual(reloaded_feature_extractor.__class__.__name__			,				'''NewFeatureExtractor'''      )



 def 			snake_case__  (   self		:   int      ):
      try:
           AutoConfig.register('''custom'''			,				a__      )
           AutoFeatureExtractor.register(a__			,				a__      )
           # Trying to register something existing in the Transformers library will raise an error
           with self.assertRaises(a__      ):
                AutoFeatureExtractor.register(a__			,				a__      )

           # Now that the config is registered, it can be used as any other config with the auto-API
           __magic_name__						       =    CustomFeatureExtractor.from_pretrained(a__      )
           with tempfile.TemporaryDirectory() as tmp_dir:
                feature_extractor.save_pretrained(a__      )
                __magic_name__						       =    AutoFeatureExtractor.from_pretrained(a__      )
                self.assertIsInstance(a__			,				a__      )

      finally:
           if "custom" in CONFIG_MAPPING._extra_content:
                del CONFIG_MAPPING._extra_content["custom"]
           if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
                del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]



 def 			snake_case__  (   self		:   int      ):






      class   _SCREAMING_SNAKE_CASE   (   __a     ):
       __SCREAMING_SNAKE_CASE					:Optional[int]      	=					True

      try:
           AutoConfig.register('''custom'''			,				a__      )
           AutoFeatureExtractor.register(a__			,				a__      )
           # If remote code is not set, the default is to use local
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(
               '''hf-internal-testing/test_dynamic_feature_extractor'''      )
           self.assertEqual(feature_extractor.__class__.__name__			,				'''NewFeatureExtractor'''      )
           self.assertTrue(feature_extractor.is_local      )

           # If remote code is disabled, we load the local one.
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(
               '''hf-internal-testing/test_dynamic_feature_extractor'''			,				trust_remote_code=a__      )
           self.assertEqual(feature_extractor.__class__.__name__			,				'''NewFeatureExtractor'''      )
           self.assertTrue(feature_extractor.is_local      )

           # If remote is enabled, we load from the Hub
           __magic_name__						       =    AutoFeatureExtractor.from_pretrained(
               '''hf-internal-testing/test_dynamic_feature_extractor'''			,				trust_remote_code=a__      )
           self.assertEqual(feature_extractor.__class__.__name__			,				'''NewFeatureExtractor'''      )
           self.assertTrue(not hasattr(a__			,				'''is_local'''      )      )

      finally:
           if "custom" in CONFIG_MAPPING._extra_content:
                del CONFIG_MAPPING._extra_content["custom"]
           if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
                del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]






