


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      lowerCAmelCase__       (				a):




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE  			= "time_series_transformer"
     __SCREAMING_SNAKE_CASE  			= {
         "hidden_size": "d_model",
         "num_attention_heads": "encoder_attention_heads",
         "num_hidden_layers": "encoder_layers",
     }


     def __init__(						self ,						__lowerCamelCase = None ,						__lowerCamelCase = None ,						__lowerCamelCase = "student_t" ,						__lowerCamelCase = "nll" ,						__lowerCamelCase = 1 ,						__lowerCamelCase = [1, 2, 3, 4, 5, 6, 7] ,						__lowerCamelCase = "mean" ,						__lowerCamelCase = 0 ,						__lowerCamelCase = 0 ,						__lowerCamelCase = 0 ,						__lowerCamelCase = 0 ,						__lowerCamelCase = None ,						__lowerCamelCase = None ,						__lowerCamelCase = 3_2 ,						__lowerCamelCase = 3_2 ,						__lowerCamelCase = 2 ,						__lowerCamelCase = 2 ,						__lowerCamelCase = 2 ,						__lowerCamelCase = 2 ,						__lowerCamelCase = True ,						__lowerCamelCase = "gelu" ,						__lowerCamelCase = 6_4 ,						__lowerCamelCase = 0.1 ,						__lowerCamelCase = 0.1 ,						__lowerCamelCase = 0.1 ,						__lowerCamelCase = 0.1 ,						__lowerCamelCase = 0.1 ,						__lowerCamelCase = 1_0_0 ,						__lowerCamelCase = 0.0_2 ,						__lowerCamelCase=True ,						**__lowerCamelCase ,						)			->				List[str]:
            # time series specific configuration
            _A      :       Optional[int]				    = prediction_length
            _A      :       Tuple				    = context_length or prediction_length
            _A      :       str				    = distribution_output
            _A      :       Optional[int]				    = loss
            _A      :       Optional[int]				    = input_size
            _A      :       str				    = num_time_features
            _A      :       str				    = lags_sequence
            _A      :       Optional[Any]				    = scaling
            _A      :       Union[str, Any]				    = num_dynamic_real_features
            _A      :       int				    = num_static_real_features
            _A      :       List[Any]				    = num_static_categorical_features
            if cardinality and num_static_categorical_features > 0:
                   if len(__lowerCamelCase) != num_static_categorical_features:
                          raise ValueError(
                              "The cardinality should be a list of the same length as `num_static_categorical_features`")
                   _A      :       List[Any]				    = cardinality
            else:
                   _A      :       int				    = [0]
            if embedding_dimension and num_static_categorical_features > 0:
                   if len(__lowerCamelCase) != num_static_categorical_features:
                          raise ValueError(
                              "The embedding dimension should be a list of the same length as `num_static_categorical_features`")
                   _A      :       Dict				    = embedding_dimension
            else:
                   _A      :       List[str]				    = [min(5_0 ,						(cat + 1) // 2) for cat in self.cardinality]
            _A      :       Optional[int]				    = num_parallel_samples

            # Transformer architecture configuration
            _A      :       int				    = input_size * len(__lowerCamelCase) + self._number_of_features
            _A      :       List[str]				    = d_model
            _A      :       Union[str, Any]				    = encoder_attention_heads
            _A      :       str				    = decoder_attention_heads
            _A      :       Any				    = encoder_ffn_dim
            _A      :       Optional[Any]				    = decoder_ffn_dim
            _A      :       Optional[int]				    = encoder_layers
            _A      :       Optional[Any]				    = decoder_layers

            _A      :       Tuple				    = dropout
            _A      :       Tuple				    = attention_dropout
            _A      :       str				    = activation_dropout
            _A      :       List[Any]				    = encoder_layerdrop
            _A      :       List[Any]				    = decoder_layerdrop

            _A      :       Optional[Any]				    = activation_function
            _A      :       Any				    = init_std

            _A      :       List[Any]				    = use_cache

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


     @property
     def  _lowerCamelCase (						self)			->				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
            )




import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
lowerCAmelCase__      		= object()

# For specifying empty leaf dict `{}`
lowerCAmelCase__      		= object()
def 							_UpperCAmelCase				(UpperCamelCase__     :  str				,							UpperCamelCase__     :  Union[str, Any]						):
       _A      :       str				    = tuple((re.compile(x + "$"						) for x in qs)						)
       for i in range(len(UpperCamelCase__						) - len(UpperCamelCase__						) + 1						):
              _A      :       Tuple				    = [x.match(UpperCamelCase__						) for x, y in zip(UpperCamelCase__				,							ks[i:]						)]
              if matches and all(UpperCamelCase__						):
                     return True
       return False
def 							_UpperCAmelCase				(UpperCamelCase__     :  str						):
       def replace(UpperCamelCase__     :  List[str]				,							UpperCamelCase__     :  Optional[int]						):
              for rule, replacement in rules:
                     if _match(UpperCamelCase__				,							UpperCamelCase__						):
                            return replacement
              return val

       return replace
def 							_UpperCAmelCase				():
       return [
           # embeddings
           (("transformer", "wpe", "embedding"), P("mp"				,							UpperCamelCase__						)),
           (("transformer", "wte", "embedding"), P("mp"				,							UpperCamelCase__						)),
           # atention
           (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(UpperCamelCase__				,							"mp"						)),
           (("attention", "out_proj", "kernel"), P("mp"				,							UpperCamelCase__						)),
           (("attention", "out_proj", "bias"), None),
           # mlp
           (("mlp", "c_fc", "kernel"), P(UpperCamelCase__				,							"mp"						)),
           (("mlp", "c_fc", "bias"), P("mp"						)),
           (("mlp", "c_proj", "kernel"), P("mp"				,							UpperCamelCase__						)),
           (("mlp", "c_proj", "bias"), None),
           # layer norms
           ((r"ln_\d+", "bias"), None),
           ((r"\d+", r"ln_\d+", "scale"), None),
           (("ln_f", "bias"), None),
           (("ln_f", "scale"), None),
       ]






def 							_UpperCAmelCase				(UpperCamelCase__     :  List[str]						):
       _A      :       int				    = _get_partition_rules()
       _A      :       Optional[int]				    = _replacement_rules(UpperCamelCase__						)
       _A      :       Optional[int]				    = {k: _unmatched for k in flatten_dict(UpperCamelCase__						)}
       _A      :       List[str]				    = {k: replace(UpperCamelCase__				,							UpperCamelCase__						) for k, v in initd.items()}
       assert _unmatched not in result.values(), "Incomplete partition spec."
       return freeze(unflatten_dict(UpperCamelCase__						)						)
