
'''simple docstring'''


import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
   import jax
   import jax.numpy as jnp


@slow
@require_flax
class     lowercase__						(	unittest.TestCase ):






   '''simple docstring'''







   def    UpperCAmelCase					(      self			, lowerCamelCase__			, lowerCamelCase__ ):

      '''simple docstring'''







      return f'gaussian_noise_s={seed}_shape={"_".join([str(lowerCAmelCase_ ) for s in shape] )}.npy'







   def    UpperCAmelCase					(      self ):

      '''simple docstring'''







      super().tearDown()
      gc.collect()







   def    UpperCAmelCase					(      self			, lowerCamelCase__=0			, lowerCamelCase__=(4, 4, 6_4, 6_4)			, lowerCamelCase__=False ):

      '''simple docstring'''







      UpperCamelCase	  =	jnp.bfloataa if fpaa else jnp.floataa
      UpperCamelCase	  =	jnp.array(load_hf_numpy(self.get_file_format(lowerCAmelCase_			, lowerCAmelCase_ ) )			, dtype=lowerCAmelCase_ )
      return image







   def    UpperCAmelCase					(      self			, lowerCamelCase__=False			, lowerCamelCase__="CompVis/stable-diffusion-v1-4" ):

      '''simple docstring'''







      UpperCamelCase	  =	jnp.bfloataa if fpaa else jnp.floataa
      UpperCamelCase	  =	'bf16' if fpaa else None

      UpperCamelCase	  =	FlaxUNetaDConditionModel.from_pretrained(
          lowerCAmelCase_			, subfolder='''unet'''			, dtype=lowerCAmelCase_			, revision=lowerCAmelCase_ )
      return model, params







   def    UpperCAmelCase					(      self			, lowerCamelCase__=0			, lowerCamelCase__=(4, 7_7, 7_6_8)			, lowerCamelCase__=False ):

      '''simple docstring'''







      UpperCamelCase	  =	jnp.bfloataa if fpaa else jnp.floataa
      UpperCamelCase	  =	jnp.array(load_hf_numpy(self.get_file_format(lowerCAmelCase_			, lowerCAmelCase_ ) )			, dtype=lowerCAmelCase_ )
      return hidden_states







   @parameterized.expand(
       [
           # fmt: off
           [8_3, 4, [-0.2323, -0.1304, 0.0813, -0.3093, -0.0919, -0.1571, -0.1125, -0.5806]],
           [1_7, 0.55, [-0.0831, -0.2443, 0.0901, -0.0919, 0.3396, 0.0103, -0.3743, 0.0701]],
           [8, 0.89, [-0.4863, 0.0859, 0.0875, -0.1658, 0.9199, -0.0114, 0.4839, 0.4639]],
           [3, 1_0_0_0, [-0.5649, 0.2402, -0.5518, 0.1248, 1.1328, -0.2443, -0.0325, -1.0078]],
           # fmt: on
       ] )
   def    UpperCAmelCase					(      self			, lowerCamelCase__			, lowerCamelCase__			, lowerCamelCase__ ):

      '''simple docstring'''







      UpperCamelCase	  =	self.get_unet_model(model_id='''CompVis/stable-diffusion-v1-4'''			, fpaa=lowerCAmelCase_ )
      UpperCamelCase	  =	self.get_latents(lowerCAmelCase_			, fpaa=lowerCAmelCase_ )
      UpperCamelCase	  =	self.get_encoder_hidden_states(lowerCAmelCase_			, fpaa=lowerCAmelCase_ )

      UpperCamelCase	  =	model.apply(
          {'''params''': params}			, lowerCAmelCase_			, jnp.array(lowerCAmelCase_			, dtype=jnp.intaa )			, encoder_hidden_states=lowerCAmelCase_			, ).sample

      assert sample.shape == latents.shape

      UpperCamelCase	  =	jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) )			, dtype=jnp.floataa )
      UpperCamelCase	  =	jnp.array(lowerCAmelCase_			, dtype=jnp.floataa )

      # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
      assert jnp.allclose(lowerCAmelCase_			, lowerCAmelCase_			, atol=1e-2 )







   @parameterized.expand(
       [
           # fmt: off
           [8_3, 4, [0.1514, 0.0807, 0.1624, 0.1016, -0.1896, 0.0263, 0.0677, 0.2310]],
           [1_7, 0.55, [0.1164, -0.0216, 0.0170, 0.1589, -0.3120, 0.1005, -0.0581, -0.1458]],
           [8, 0.89, [-0.1758, -0.0169, 0.1004, -0.1411, 0.1312, 0.1103, -0.1996, 0.2139]],
           [3, 1_0_0_0, [0.1214, 0.0352, -0.0731, -0.1562, -0.0994, -0.0906, -0.2340, -0.0539]],
           # fmt: on
       ] )
   def    UpperCAmelCase					(      self			, lowerCamelCase__			, lowerCamelCase__			, lowerCamelCase__ ):

      '''simple docstring'''







      UpperCamelCase	  =	self.get_unet_model(model_id='''stabilityai/stable-diffusion-2'''			, fpaa=lowerCAmelCase_ )
      UpperCamelCase	  =	self.get_latents(lowerCAmelCase_			, shape=(4, 4, 9_6, 9_6)			, fpaa=lowerCAmelCase_ )
      UpperCamelCase	  =	self.get_encoder_hidden_states(lowerCAmelCase_			, shape=(4, 7_7, 1_0_2_4)			, fpaa=lowerCAmelCase_ )

      UpperCamelCase	  =	model.apply(
          {'''params''': params}			, lowerCAmelCase_			, jnp.array(lowerCAmelCase_			, dtype=jnp.intaa )			, encoder_hidden_states=lowerCAmelCase_			, ).sample

      assert sample.shape == latents.shape

      UpperCamelCase	  =	jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) )			, dtype=jnp.floataa )
      UpperCamelCase	  =	jnp.array(lowerCAmelCase_			, dtype=jnp.floataa )

      # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
      assert jnp.allclose(lowerCAmelCase_			, lowerCAmelCase_			, atol=1e-2 )













"""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 _lowerCAmelCase						(    __UpperCAmelCase       ):
						SCREAMING_SNAKE_CASE_:					Any 					=				'time_series_transformer'
						SCREAMING_SNAKE_CASE_:					List[str] 					=				{
						    '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.02   ,				lowerCAmelCase_=True   ,				**lowerCAmelCase_   ,				)		->							str:
										# time series specific configuration
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    prediction_length
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    context_length or prediction_length
										_SCREAMING_SNAKE_CASE   :		Any       		=    distribution_output
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    loss
										_SCREAMING_SNAKE_CASE   :		Any       		=    input_size
										_SCREAMING_SNAKE_CASE   :		Any       		=    num_time_features
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    lags_sequence
										_SCREAMING_SNAKE_CASE   :		Dict       		=    scaling
										_SCREAMING_SNAKE_CASE   :		Any       		=    num_dynamic_real_features
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    num_static_real_features
										_SCREAMING_SNAKE_CASE   :		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`'							)
														_SCREAMING_SNAKE_CASE   :		List[str]       		=    cardinality
										else:
														_SCREAMING_SNAKE_CASE   :		Dict       		=    [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`'							)
														_SCREAMING_SNAKE_CASE   :		int       		=    embedding_dimension
										else:
														_SCREAMING_SNAKE_CASE   :		List[str]       		=    [min(5_0   ,				(cat + 1) // 2							) for cat in self.cardinality]
										_SCREAMING_SNAKE_CASE   :		str       		=    num_parallel_samples

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

										_SCREAMING_SNAKE_CASE   :		Dict       		=    dropout
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    attention_dropout
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    activation_dropout
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    encoder_layerdrop
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    decoder_layerdrop

										_SCREAMING_SNAKE_CASE   :		Dict       		=    activation_function
										_SCREAMING_SNAKE_CASE   :		str       		=    init_std

										_SCREAMING_SNAKE_CASE   :		Tuple       		=    use_cache

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







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

