

import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
						import os

						import jax.numpy as jnp
						from jax import jit

						from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
						from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

						A__						=							"0.12"  # assumed parallelism: 8


if is_torch_available():
						import torch




def 					_lowercase     (				a_				:					Dict					,a_				:					Any					,a_				:					str=None				)				->	Tuple:




					'''simple docstring'''







					if rng is None:
										__magic_name__							=    random.Random()

					__magic_name__							=    1
					for dim in shape:
										total_dims *= dim

					__magic_name__							=    []
					for _ in range(a_				):
										values.append(rng.randint(0					,vocab_size - 1				)				)

					__magic_name__							=    np.array(a_					,dtype=jnp.intaa				).reshape(a_				)

					return output




def 					_lowercase     (				a_				:					Dict					,a_				:					List[str]=None				)				->	Dict:




					'''simple docstring'''







					__magic_name__							=    ids_tensor(a_					,vocab_size=2					,rng=a_				)
					# make sure that at least one token is attended to for each batch
					__magic_name__							=    1
					return attn_mask

@require_flax
class 				__UpperCamelCase				:
	_lowercase  :   List[Any]       				=  None
	_lowercase  :   Optional[Any]       				=  ()







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__							=    self.model_tester.prepare_config_and_inputs_for_common()

						# cut to half length & take max batch_size 3
						__magic_name__							=    2
						__magic_name__							=    inputs['input_ids'].shape[-1] // 2
						__magic_name__							=    inputs['input_ids'][:max_batch_size, :sequence_length]

						__magic_name__							=    jnp.ones_like(__UpperCamelCase      )
						__magic_name__							=    attention_mask[:max_batch_size, :sequence_length]

						# generate max 5 tokens
						__magic_name__							=    input_ids.shape[-1] + 5
						if config.eos_token_id is not None and config.pad_token_id is None:
											# hack to allow generate for models such as GPT2 as is done in `generate()`
											__magic_name__							=    config.eos_token_id
						return config, input_ids, attention_mask, max_length







	@is_pt_flax_cross_test
	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()
						__magic_name__							=    False
						__magic_name__							=    max_length
						__magic_name__							=    0

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model_class.__name__[4:]  # Skip the "Flax" at the beginning
											__magic_name__							=    getattr(__UpperCamelCase  , __UpperCamelCase      )
											__magic_name__							=    pt_model_class(__UpperCamelCase      ).eval()
											__magic_name__							=    load_flax_weights_in_pytorch_model(__UpperCamelCase  , flax_model.params      )

											__magic_name__							=    flax_model.generate(__UpperCamelCase      ).sequences
											__magic_name__							=    pt_model.generate(torch.tensor(__UpperCamelCase  , dtype=torch.long      )      )

											if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
																__magic_name__							=    flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

											self.assertListEqual(pt_generation_outputs.numpy().tolist()  , flax_generation_outputs.tolist()      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()
						__magic_name__							=    False
						__magic_name__							=    max_length

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()
						__magic_name__							=    True
						__magic_name__							=    max_length

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()
						__magic_name__							=    False
						__magic_name__							=    max_length
						__magic_name__							=    2

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()
						__magic_name__							=    False
						__magic_name__							=    max_length
						__magic_name__							=    2
						__magic_name__							=    2

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[0]  , input_ids.shape[0] * config.num_return_sequences      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()
						__magic_name__							=    True
						__magic_name__							=    max_length
						__magic_name__							=    0.8
						__magic_name__							=    10
						__magic_name__							=    0.3
						__magic_name__							=    1
						__magic_name__							=    8
						__magic_name__							=    9

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )







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

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()
						__magic_name__							=    max_length
						__magic_name__							=    1
						__magic_name__							=    8
						__magic_name__							=    9

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()
						__magic_name__							=    max_length
						__magic_name__							=    2
						__magic_name__							=    1
						__magic_name__							=    8
						__magic_name__							=    9

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[int]      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()

						# pad attention mask on the left
						__magic_name__							=    attention_mask.at[(0, 0)].set(0      )

						__magic_name__							=    False
						__magic_name__							=    max_length

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase  , attention_mask=__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase  , attention_mask=__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )







	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()

						# pad attention mask on the left
						__magic_name__							=    attention_mask.at[(0, 0)].set(0      )

						__magic_name__							=    True
						__magic_name__							=    max_length

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase  , attention_mask=__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase  , attention_mask=__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							=    self._get_input_ids_and_config()

						# pad attention mask on the left
						__magic_name__							=    attention_mask.at[(0, 0)].set(0      )

						__magic_name__							=    2
						__magic_name__							=    max_length

						for model_class in self.all_generative_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )

											__magic_name__							=    model.generate(__UpperCamelCase  , attention_mask=__UpperCamelCase      ).sequences
											self.assertEqual(generation_outputs.shape[-1]  , __UpperCamelCase      )

											__magic_name__							=    jit(model.generate      )
											__magic_name__							=    jit_generate(__UpperCamelCase  , attention_mask=__UpperCamelCase      ).sequences

											self.assertListEqual(generation_outputs.tolist()  , jit_generation_outputs.tolist()      )




@require_flax
class 				__UpperCamelCase				(					unittest.TestCase			):







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]      ):

						'''simple docstring'''





						__magic_name__							=    AutoTokenizer.from_pretrained('hf-internal-testing/tiny-bert'      )
						__magic_name__							=    FlaxAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-bert-flax-only'      )

						__magic_name__							=    'Hello world'
						__magic_name__							=    tokenizer(__UpperCamelCase  , return_tensors='np'      ).input_ids

						# typos are quickly detected (the correct argument is `do_sample`)
						with self.assertRaisesRegex(__UpperCamelCase  , 'do_samples'      ):
											model.generate(__UpperCamelCase  , do_samples=__UpperCamelCase      )

						# arbitrary arguments that will not be used anywhere are also not accepted
						with self.assertRaisesRegex(__UpperCamelCase  , 'foo'      ):
											__magic_name__							=    {'foo': 'bar'}
											model.generate(__UpperCamelCase  , **__UpperCamelCase      )







from typing import List, Optional, Union

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


A__						=							logging.get_logger(__name__)

A__						=							{
    "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 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE			):
	_lowercase  :   Optional[int]       				=  "time_series_transformer"
	_lowercase  :   Dict       				=  {
	    "hidden_size": "d_model",
	    "num_attention_heads": "encoder_attention_heads",
	    "num_hidden_layers": "encoder_layers",
	}







	def __init__(   self: str  , __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 = 1_00  , __UpperCamelCase: float = 0.02  , __UpperCamelCase: Any=True  , **__UpperCamelCase: Optional[Any]  , ):

						'''simple docstring'''





						__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(__UpperCamelCase      ) != 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(__UpperCamelCase      ) != 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(__UpperCamelCase      ) + 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=__UpperCamelCase  , **__UpperCamelCase      )







	@property
	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''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
						)




