




"""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       SCREAMING_SNAKE_CASE__					(					unittest.TestCase					):


						def   __lowercase (							self				:			Dict						,						lowerCAmelCase				:			str						,						lowerCAmelCase				:			Union[str, Any]					):
										return f'''gaussian_noise_s={seed}_shape={'_'.join([str(lowerCAmelCase					) for s in shape]					)}.npy'''


						def   __lowercase (							self				:			List[Any]					):
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()


						def   __lowercase (							self				:			Optional[int]						,						lowerCAmelCase				:			int=0						,						lowerCAmelCase				:			Tuple=(4, 4, 64, 64)						,						lowerCAmelCase				:			Tuple=False					):
										lowerCAmelCase  =				jnp.bfloataa if fpaa else jnp.floataa
										lowerCAmelCase  =				jnp.array(load_hf_numpy(self.get_file_format(lowerCAmelCase						,						lowerCAmelCase					)					)						,						dtype=lowerCAmelCase					)
										return image


						def   __lowercase (							self				:			Optional[int]						,						lowerCAmelCase				:			str=False						,						lowerCAmelCase				:			List[str]="CompVis/stable-diffusion-v1-4"					):
										lowerCAmelCase  =				jnp.bfloataa if fpaa else jnp.floataa
										lowerCAmelCase  =				"""bf16""" if fpaa else None

										lowerCAmelCase ,						lowerCAmelCase  =				FlaxUNetaDConditionModel.from_pretrained(
										    lowerCAmelCase						,						subfolder="""unet"""						,						dtype=lowerCAmelCase						,						revision=lowerCAmelCase					)
										return model, params


						def   __lowercase (							self				:			List[str]						,						lowerCAmelCase				:			Dict=0						,						lowerCAmelCase				:			Optional[int]=(4, 77, 768)						,						lowerCAmelCase				:			List[Any]=False					):
										lowerCAmelCase  =				jnp.bfloataa if fpaa else jnp.floataa
										lowerCAmelCase  =				jnp.array(load_hf_numpy(self.get_file_format(lowerCAmelCase						,						lowerCAmelCase					)					)						,						dtype=lowerCAmelCase					)
										return hidden_states


						@parameterized.expand(
						    [
						        # fmt: off
						        [83, 4, [-0.2323, -0.1304, 0.0813, -0.3093, -0.0919, -0.1571, -0.1125, -0.5806]],
						        [17, 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, 1000, [-0.5649, 0.2402, -0.5518, 0.1248, 1.1328, -0.2443, -0.0325, -1.0078]],
						        # fmt: on
						    ]					)
						def   __lowercase (							self				:			List[str]						,						lowerCAmelCase				:			List[str]						,						lowerCAmelCase				:			int						,						lowerCAmelCase				:			List[Any]					):
										lowerCAmelCase ,						lowerCAmelCase  =				self.get_unet_model(model_id="""CompVis/stable-diffusion-v1-4"""						,						fpaa=lowerCAmelCase					)
										lowerCAmelCase  =				self.get_latents(lowerCAmelCase						,						fpaa=lowerCAmelCase					)
										lowerCAmelCase  =				self.get_encoder_hidden_states(lowerCAmelCase						,						fpaa=lowerCAmelCase					)

										lowerCAmelCase  =				model.apply(
										    {"""params""": params}						,						lowerCAmelCase						,						jnp.array(lowerCAmelCase						,						dtype=jnp.intaa					)						,						encoder_hidden_states=lowerCAmelCase						,						).sample

										assert sample.shape == latents.shape

										lowerCAmelCase  =				jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())					)						,						dtype=jnp.floataa					)
										lowerCAmelCase  =				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
						        [83, 4, [0.1514, 0.0807, 0.1624, 0.1016, -0.1896, 0.0263, 0.0677, 0.2310]],
						        [17, 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, 1000, [0.1214, 0.0352, -0.0731, -0.1562, -0.0994, -0.0906, -0.2340, -0.0539]],
						        # fmt: on
						    ]					)
						def   __lowercase (							self				:			Tuple						,						lowerCAmelCase				:			str						,						lowerCAmelCase				:			Dict						,						lowerCAmelCase				:			Tuple					):
										lowerCAmelCase ,						lowerCAmelCase  =				self.get_unet_model(model_id="""stabilityai/stable-diffusion-2"""						,						fpaa=lowerCAmelCase					)
										lowerCAmelCase  =				self.get_latents(lowerCAmelCase						,						shape=(4, 4, 96, 96)						,						fpaa=lowerCAmelCase					)
										lowerCAmelCase  =				self.get_encoder_hidden_states(lowerCAmelCase						,						shape=(4, 77, 1024)						,						fpaa=lowerCAmelCase					)

										lowerCAmelCase  =				model.apply(
										    {"""params""": params}						,						lowerCAmelCase						,						jnp.array(lowerCAmelCase						,						dtype=jnp.intaa					)						,						encoder_hidden_states=lowerCAmelCase						,						).sample

										assert sample.shape == latents.shape

										lowerCAmelCase  =				jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())					)						,						dtype=jnp.floataa					)
										lowerCAmelCase  =				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

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


a      =							logging.get_logger(__name__)

a      =							{
    'huggingface/autoformer-tourism-monthly': 'https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json',
}






class       SCREAMING_SNAKE_CASE__					(					_a					):
						_a    						=						'autoformer'
						_a    						=						{
						    'hidden_size': 'd_model',
						    'num_attention_heads': 'encoder_attention_heads',
						    'num_hidden_layers': 'encoder_layers',
						}


						def __init__(							self				:			Dict						,						lowerCAmelCase				:			Optional[int] = None						,						lowerCAmelCase				:			Optional[int] = None						,						lowerCAmelCase				:			str = "student_t"						,						lowerCAmelCase				:			str = "nll"						,						lowerCAmelCase				:			int = 1						,						lowerCAmelCase				:			List[int] = [1, 2, 3, 4, 5, 6, 7]						,						lowerCAmelCase				:			bool = True						,						lowerCAmelCase				:			int = 0						,						lowerCAmelCase				:			int = 0						,						lowerCAmelCase				:			int = 0						,						lowerCAmelCase				:			int = 0						,						lowerCAmelCase				:			Optional[List[int]] = None						,						lowerCAmelCase				:			Optional[List[int]] = None						,						lowerCAmelCase				:			int = 64						,						lowerCAmelCase				:			int = 2						,						lowerCAmelCase				:			int = 2						,						lowerCAmelCase				:			int = 2						,						lowerCAmelCase				:			int = 2						,						lowerCAmelCase				:			int = 32						,						lowerCAmelCase				:			int = 32						,						lowerCAmelCase				:			str = "gelu"						,						lowerCAmelCase				:			float = 0.1						,						lowerCAmelCase				:			float = 0.1						,						lowerCAmelCase				:			float = 0.1						,						lowerCAmelCase				:			float = 0.1						,						lowerCAmelCase				:			float = 0.1						,						lowerCAmelCase				:			int = 100						,						lowerCAmelCase				:			float = 0.02						,						lowerCAmelCase				:			bool = True						,						lowerCAmelCase				:			Tuple=True						,						lowerCAmelCase				:			int = 10						,						lowerCAmelCase				:			int = 25						,						lowerCAmelCase				:			int = 3						,						**lowerCAmelCase				:			Tuple						,						):
										# time series specific configuration
										lowerCAmelCase  =				prediction_length
										lowerCAmelCase  =				context_length if context_length is not None else prediction_length
										lowerCAmelCase  =				distribution_output
										lowerCAmelCase  =				loss
										lowerCAmelCase  =				input_size
										lowerCAmelCase  =				num_time_features
										lowerCAmelCase  =				lags_sequence
										lowerCAmelCase  =				scaling
										lowerCAmelCase  =				num_dynamic_real_features
										lowerCAmelCase  =				num_static_real_features
										lowerCAmelCase  =				num_static_categorical_features
										if cardinality is not None 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`"""					)
														lowerCAmelCase  =				cardinality
										else:
														lowerCAmelCase  =				[0]
										if embedding_dimension is not None 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`"""					)
														lowerCAmelCase  =				embedding_dimension
										else:
														lowerCAmelCase  =				[min(50						,						(cat + 1) // 2					) for cat in self.cardinality]
										lowerCAmelCase  =				num_parallel_samples

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

										lowerCAmelCase  =				dropout
										lowerCAmelCase  =				attention_dropout
										lowerCAmelCase  =				activation_dropout
										lowerCAmelCase  =				encoder_layerdrop
										lowerCAmelCase  =				decoder_layerdrop

										lowerCAmelCase  =				activation_function
										lowerCAmelCase  =				init_std

										lowerCAmelCase  =				use_cache

										# Autoformer
										lowerCAmelCase  =				label_length
										lowerCAmelCase  =				moving_average
										lowerCAmelCase  =				autocorrelation_factor

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




						@property
						def   __lowercase (							self				:			Tuple					):
										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
										)





