


'''simple docstring'''

import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
 import tensorflow as tf

if is_tensorflow_text_available():
 from transformers.models.bert import TFBertTokenizer


lowerCamelCase							:   Optional[int]  						=	['bert-base-uncased', 'bert-base-cased']
lowerCamelCase							:   Union[str, Any]  						=	'hf-internal-testing/tiny-bert-tf-only'

if is_tf_available():
 class   __lowerCAmelCase						(tf.keras.Model     ):





       '''simple docstring'''






       def __init__(self    :    List[str]   ,						UpperCamelCase    :    Optional[Any] ):

              '''simple docstring'''







              super().__init__()
              lowercase__      			= tokenizer
              lowercase__      			= AutoConfig.from_pretrained(UpperCamelCase )
              lowercase__      			= TFAutoModel.from_config(UpperCamelCase )






       def       UpperCamelCase__		(self    :    List[str]   ,						UpperCamelCase    :    List[str] ):

              '''simple docstring'''







              lowercase__      			= self.tokenizer(UpperCamelCase )
              lowercase__      			= self.bert(**UpperCamelCase )
              return out["pooler_output"]
@require_tf
@require_tensorflow_text
class   __lowerCAmelCase						(unittest.TestCase     ):





      '''simple docstring'''






      def       UpperCamelCase__		(self    :    Tuple ):

             '''simple docstring'''







             super().setUp()

             lowercase__      			= [
                 BertTokenizer.from_pretrained(UpperCamelCase ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
             ]  # repeat for when fast_bert_tokenizer=false
             lowercase__      			= [TFBertTokenizer.from_pretrained(UpperCamelCase ) for checkpoint in TOKENIZER_CHECKPOINTS] + [
                 TFBertTokenizer.from_pretrained(UpperCamelCase   ,						use_fast_bert_tokenizer=UpperCamelCase )
                 for checkpoint in TOKENIZER_CHECKPOINTS
             ]
             assert len(self.tokenizers ) == len(self.tf_tokenizers )

             lowercase__      			= [
                 '''This is a straightforward English test sentence.''',
                 '''This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.''',
                 '''Now we\'re going to add some Chinese: 一 二 三 一二三''',
                 '''And some much more rare Chinese: 齉 堃 齉堃''',
                 '''Je vais aussi écrire en français pour tester les accents''',
                 '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''',
             ]
             lowercase__      			= list(zip(self.test_sentences   ,						self.test_sentences[::-1] ) )



      def       UpperCamelCase__		(self    :    Union[str, Any] ):

             '''simple docstring'''







             for tokenizer, tf_tokenizer in zip(self.tokenizers   ,						self.tf_tokenizers ):
                    for test_inputs in (self.test_sentences, self.paired_sentences):
                           lowercase__      			= tokenizer(UpperCamelCase   ,						return_tensors='''tf'''   ,						padding='''longest''' )
                           lowercase__      			= tf_tokenizer(UpperCamelCase )

                           for key in python_outputs.keys():
                                  self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape ) )
                                  self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]   ,						tf.intaa ) == tf_outputs[key] ) )



      @slow
      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             for tf_tokenizer in self.tf_tokenizers:
                    lowercase__      			= tf_tokenizer(self.paired_sentences )
                    lowercase__      			= tf_tokenizer(
                        text=[sentence[0] for sentence in self.paired_sentences]   ,						text_pair=[sentence[1] for sentence in self.paired_sentences]   ,						)
                    for key in merged_outputs.keys():
                           self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]   ,						tf.intaa ) == separated_outputs[key] ) )



      @slow
      def       UpperCamelCase__		(self    :    Any ):

             '''simple docstring'''







             for tf_tokenizer in self.tf_tokenizers:
                    lowercase__      			= tf.function(UpperCamelCase )
                    for test_inputs in (self.test_sentences, self.paired_sentences):
                           lowercase__      			= tf.constant(UpperCamelCase )
                           lowercase__      			= compiled_tokenizer(UpperCamelCase )
                           lowercase__      			= tf_tokenizer(UpperCamelCase )

                           for key in eager_outputs.keys():
                                  self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) )






      @slow
      def       UpperCamelCase__		(self    :    Tuple ):

             '''simple docstring'''







             for tf_tokenizer in self.tf_tokenizers:
                    lowercase__      			= ModelToSave(tokenizer=UpperCamelCase )
                    lowercase__      			= tf.convert_to_tensor(self.test_sentences )
                    lowercase__      			= model(UpperCamelCase )  # Build model with some sample inputs
                    with TemporaryDirectory() as tempdir:
                           lowercase__      			= Path(UpperCamelCase ) / '''saved.model'''
                           model.save(UpperCamelCase )
                           lowercase__      			= tf.keras.models.load_model(UpperCamelCase )
                    lowercase__      			= loaded_model(UpperCamelCase )
                    # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
                    self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output ) )   ,						1E-5 )







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

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


lowerCamelCase :      List[Any]					 =     logging.get_logger(__name__)

lowerCamelCase :      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 							A__    (       A__  ):
  A__		 =				'time_series_transformer'
  A__		 =				{
      'hidden_size': 'd_model',
      'num_attention_heads': 'encoder_attention_heads',
      'num_hidden_layers': 'encoder_layers',
  }





  def __init__(		self      :	Optional[int]							,					_a      :	Optional[int] = None							,					_a      :	Optional[int] = None							,					_a      :	str = "student_t"							,					_a      :	str = "nll"							,					_a      :	int = 1							,					_a      :	List[int] = [1, 2, 3, 4, 5, 6, 7]							,					_a      :	Optional[Union[str, bool]] = "mean"							,					_a      :	int = 0							,					_a      :	int = 0							,					_a      :	int = 0							,					_a      :	int = 0							,					_a      :	Optional[List[int]] = None							,					_a      :	Optional[List[int]] = None							,					_a      :	int = 32							,					_a      :	int = 32							,					_a      :	int = 2							,					_a      :	int = 2							,					_a      :	int = 2							,					_a      :	int = 2							,					_a      :	bool = True							,					_a      :	str = "gelu"							,					_a      :	int = 64							,					_a      :	float = 0.1							,					_a      :	float = 0.1							,					_a      :	float = 0.1							,					_a      :	float = 0.1							,					_a      :	float = 0.1							,					_a      :	int = 100							,					_a      :	float = 0.02							,					_a      :	Union[str, Any]=True							,					**_a      :	Optional[Any]							,					)	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =prediction_length
       _SCREAMING_SNAKE_CASE							    =context_length or prediction_length
       _SCREAMING_SNAKE_CASE							    =distribution_output
       _SCREAMING_SNAKE_CASE							    =loss
       _SCREAMING_SNAKE_CASE							    =input_size
       _SCREAMING_SNAKE_CASE							    =num_time_features
       _SCREAMING_SNAKE_CASE							    =lags_sequence
       _SCREAMING_SNAKE_CASE							    =scaling
       _SCREAMING_SNAKE_CASE							    =num_dynamic_real_features
       _SCREAMING_SNAKE_CASE							    =num_static_real_features
       _SCREAMING_SNAKE_CASE							    =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`' )
            _SCREAMING_SNAKE_CASE							    =cardinality
       else:
            _SCREAMING_SNAKE_CASE							    =[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`' )
            _SCREAMING_SNAKE_CASE							    =embedding_dimension
       else:
            _SCREAMING_SNAKE_CASE							    =[min(50							,					(cat + 1) // 2 ) for cat in self.cardinality]
       _SCREAMING_SNAKE_CASE							    =num_parallel_samples

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

       _SCREAMING_SNAKE_CASE							    =dropout
       _SCREAMING_SNAKE_CASE							    =attention_dropout
       _SCREAMING_SNAKE_CASE							    =activation_dropout
       _SCREAMING_SNAKE_CASE							    =encoder_layerdrop
       _SCREAMING_SNAKE_CASE							    =decoder_layerdrop

       _SCREAMING_SNAKE_CASE							    =activation_function
       _SCREAMING_SNAKE_CASE							    =init_std

       _SCREAMING_SNAKE_CASE							    =use_cache

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





  @property
  def 						A  (		self      :	List[Any] )	->    int:





       '''simple docstring'''






       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
       )
