'''simple docstring'''
import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


lowercase__  :      Dict        =					logging.get_logger(__name__)

lowercase__  :      Optional[int]        =					"▁"

lowercase__  :      Any        =					{"vocab_file": "sentencepiece.bpe.model", "monolingual_vocab_file": "dict.txt"}

lowercase__  :      Union[str, Any]        =					{
    "vocab_file": {
        "vinai/bartpho-syllable": "https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model",
    },
    "monolingual_vocab_file": {
        "vinai/bartpho-syllable": "https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt",
    },
}

lowercase__  :      int        =					{"vinai/bartpho-syllable": 1024}







class lowerCamelCase							(	lowerCamelCase					):




  '''simple docstring'''



  lowerCAmelCase__										=	VOCAB_FILES_NAMES
  lowerCAmelCase__										=	PRETRAINED_VOCAB_FILES_MAP
  lowerCAmelCase__										=	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  lowerCAmelCase__										=	['''input_ids''', '''attention_mask''']






  def __init__(      self			:       List[str]  ,							UpperCAmelCase__			:       Any  ,							UpperCAmelCase__			:       int  ,							UpperCAmelCase__			:       List[Any]="<s>"  ,							UpperCAmelCase__			:       List[str]="</s>"  ,							UpperCAmelCase__			:       Dict="</s>"  ,							UpperCAmelCase__			:       List[str]="<s>"  ,							UpperCAmelCase__			:       int="<unk>"  ,							UpperCAmelCase__			:       Optional[int]="<pad>"  ,							UpperCAmelCase__			:       List[Any]="<mask>"  ,							UpperCAmelCase__			:       Optional[Dict[str, Any]] = None  ,							**UpperCAmelCase__			:       Union[str, Any]  ,							)				->None:
        # Mask token behave like a normal word, i.e. include the space before it
        UpperCAmelCase_            =  AddedToken(UpperCAmelCase__  ,							lstrip=UpperCAmelCase__  ,							rstrip=UpperCAmelCase__  ) if isinstance(UpperCAmelCase__  ,							UpperCAmelCase__  ) else mask_token

        UpperCAmelCase_            =  {} if sp_model_kwargs is None else sp_model_kwargs

        super().__init__(
            bos_token=UpperCAmelCase__  ,							eos_token=UpperCAmelCase__  ,							unk_token=UpperCAmelCase__  ,							sep_token=UpperCAmelCase__  ,							cls_token=UpperCAmelCase__  ,							pad_token=UpperCAmelCase__  ,							mask_token=UpperCAmelCase__  ,							sp_model_kwargs=self.sp_model_kwargs  ,							**UpperCAmelCase__  ,							)

        UpperCAmelCase_            =  vocab_file
        UpperCAmelCase_            =  monolingual_vocab_file
        UpperCAmelCase_            =  spm.SentencePieceProcessor(**self.sp_model_kwargs  )
        self.sp_model.Load(str(UpperCAmelCase__  )  )

        # Load the reduced vocab

        # Keep order of special tokens for backward compatibility
        UpperCAmelCase_            =  {}
        UpperCAmelCase_            =  0
        for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]:
              if str(UpperCAmelCase__  ) not in self.fairseq_tokens_to_ids:
                    UpperCAmelCase_            =  cnt
                    cnt += 1
        with open(UpperCAmelCase__  ,							'''r'''  ,							encoding='''utf-8'''  ) as f:
              for line in f.readlines():
                    UpperCAmelCase_            =  line.strip().split()[0]
                    UpperCAmelCase_            =  len(self.fairseq_tokens_to_ids  )
        if str(UpperCAmelCase__  ) not in self.fairseq_tokens_to_ids:
              UpperCAmelCase_            =  len(self.fairseq_tokens_to_ids  )

        UpperCAmelCase_            =  {v: k for k, v in self.fairseq_tokens_to_ids.items()}






  def __getstate__(      self			:       Optional[int]  )				->Any:
        UpperCAmelCase_            =  self.__dict__.copy()
        UpperCAmelCase_            =  None
        UpperCAmelCase_            =  self.sp_model.serialized_model_proto()
        return state






  def __setstate__(      self			:       List[str]  ,							UpperCAmelCase__			:       Union[str, Any]  )				->Optional[Any]:
        UpperCAmelCase_            =  d

        # for backward compatibility
        if not hasattr(self  ,							'''sp_model_kwargs'''  ):
              UpperCAmelCase_            =  {}

        UpperCAmelCase_            =  spm.SentencePieceProcessor(**self.sp_model_kwargs  )
        self.sp_model.LoadFromSerializedProto(self.sp_model_proto  )






  def  lowerCAmelCase__ (      self			:       Tuple  ,							UpperCAmelCase__			:       List[int]  ,							UpperCAmelCase__			:       Optional[List[int]] = None  )				->List[int]:

        if token_ids_a is None:
              return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
        UpperCAmelCase_            =  [self.cls_token_id]
        UpperCAmelCase_            =  [self.sep_token_id]
        return cls + token_ids_a + sep + sep + token_ids_a + sep






  def  lowerCAmelCase__ (      self			:       Optional[int]  ,							UpperCAmelCase__			:       List[int]  ,							UpperCAmelCase__			:       Optional[List[int]] = None  ,							UpperCAmelCase__			:       bool = False  )				->List[int]:

        if already_has_special_tokens:
              return super().get_special_tokens_mask(
                  token_ids_a=UpperCAmelCase__  ,							token_ids_a=UpperCAmelCase__  ,							already_has_special_tokens=UpperCAmelCase__  )

        if token_ids_a is None:
              return [1] + ([0] * len(UpperCAmelCase__  )) + [1]
        return [1] + ([0] * len(UpperCAmelCase__  )) + [1, 1] + ([0] * len(UpperCAmelCase__  )) + [1]






  def  lowerCAmelCase__ (      self			:       Dict  ,							UpperCAmelCase__			:       List[int]  ,							UpperCAmelCase__			:       Optional[List[int]] = None  )				->List[int]:
        UpperCAmelCase_            =  [self.sep_token_id]
        UpperCAmelCase_            =  [self.cls_token_id]

        if token_ids_a is None:
              return len(cls + token_ids_a + sep  ) * [0]
        return len(cls + token_ids_a + sep + sep + token_ids_a + sep  ) * [0]






  @property
  def  lowerCAmelCase__ (      self			:       Dict  )				->Union[str, Any]:
        return len(self.fairseq_ids_to_tokens  )






  def  lowerCAmelCase__ (      self			:       List[Any]  )				->Dict:
        UpperCAmelCase_            =  {self.convert_ids_to_tokens(UpperCAmelCase__  ): i for i in range(self.vocab_size  )}
        vocab.update(self.added_tokens_encoder  )
        return vocab






  def  lowerCAmelCase__ (      self			:       Dict  ,							UpperCAmelCase__			:       str  )				->List[str]:
        return self.sp_model.encode(UpperCAmelCase__  ,							out_type=UpperCAmelCase__  )






  def  lowerCAmelCase__ (      self			:       Any  ,							UpperCAmelCase__			:       str  )				->List[str]:
        if token in self.fairseq_tokens_to_ids:
              return self.fairseq_tokens_to_ids[token]
        else:
              return self.unk_token_id






  def  lowerCAmelCase__ (      self			:       Optional[int]  ,							UpperCAmelCase__			:       Tuple  )				->List[str]:
        return self.fairseq_ids_to_tokens[index]






  def  lowerCAmelCase__ (      self			:       Optional[Any]  ,							UpperCAmelCase__			:       int  )				->int:
        UpperCAmelCase_            =  ''''''.join(UpperCAmelCase__  ).replace(UpperCAmelCase__  ,							''' '''  ).strip()
        return out_string






  def  lowerCAmelCase__ (      self			:       Dict  ,							UpperCAmelCase__			:       str  ,							UpperCAmelCase__			:       Optional[str] = None  )				->Tuple[str]:
        if not os.path.isdir(UpperCAmelCase__  ):
              logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""  )
              return
        UpperCAmelCase_            =  os.path.join(
            UpperCAmelCase__  ,							(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']  )
        UpperCAmelCase_            =  os.path.join(
            UpperCAmelCase__  ,							(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''monolingual_vocab_file''']  ,							)

        if os.path.abspath(self.vocab_file  ) != os.path.abspath(UpperCAmelCase__  ) and os.path.isfile(self.vocab_file  ):
              copyfile(self.vocab_file  ,							UpperCAmelCase__  )
        elif not os.path.isfile(self.vocab_file  ):
              with open(UpperCAmelCase__  ,							'''wb'''  ) as fi:
                    UpperCAmelCase_            =  self.sp_model.serialized_model_proto()
                    fi.write(UpperCAmelCase__  )

        if os.path.abspath(self.monolingual_vocab_file  ) != os.path.abspath(
            UpperCAmelCase__  ) and os.path.isfile(self.monolingual_vocab_file  ):
              copyfile(self.monolingual_vocab_file  ,							UpperCAmelCase__  )
        elif not os.path.isfile(self.monolingual_vocab_file  ):
              with open(UpperCAmelCase__  ,							'''w'''  ,							encoding='''utf-8'''  ) as fp:
                    for token in self.fairseq_tokens_to_ids:
                          if token not in self.all_special_tokens:
                                fp.write(f"""{str(UpperCAmelCase__  )} \n"""  )

        return out_vocab_file, out_monolingual_vocab_file






'''simple docstring'''
from typing import List, Optional, Union

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


lowercase__  :      int        =					logging.get_logger(__name__)

lowercase__  :      List[str]        =					{
    "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							(	lowerCamelCase					):




  '''simple docstring'''



  lowerCAmelCase__										=	'''time_series_transformer'''
  lowerCAmelCase__										=	{
      '''hidden_size''': '''d_model''',
      '''num_attention_heads''': '''encoder_attention_heads''',
      '''num_hidden_layers''': '''encoder_layers''',
  }






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

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

        UpperCAmelCase_            =  dropout
        UpperCAmelCase_            =  attention_dropout
        UpperCAmelCase_            =  activation_dropout
        UpperCAmelCase_            =  encoder_layerdrop
        UpperCAmelCase_            =  decoder_layerdrop

        UpperCAmelCase_            =  activation_function
        UpperCAmelCase_            =  init_std

        UpperCAmelCase_            =  use_cache

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






  @property
  def  lowerCAmelCase__ (      self			:       List[str]  )				->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
        )





