
'''simple docstring'''
import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


lowercase__  :      List[str]        =					False







@skip_mps
class lowerCamelCase							(	lowerCamelCase     ,		lowerCamelCase     ,		lowerCamelCase     ,		unittest.TestCase					):




  '''simple docstring'''



  lowerCAmelCase__										=	StableDiffusionAttendAndExcitePipeline
  lowerCAmelCase__										=	False
  lowerCAmelCase__										=	TEXT_TO_IMAGE_PARAMS
  lowerCAmelCase__										=	TEXT_TO_IMAGE_BATCH_PARAMS.union({'''token_indices'''}					)
  lowerCAmelCase__										=	TEXT_TO_IMAGE_IMAGE_PARAMS
  lowerCAmelCase__										=	TEXT_TO_IMAGE_IMAGE_PARAMS






  @classmethod
  def  lowerCAmelCase__ (      cls			:       List[Any]  )				->Tuple:
        super().setUpClass()
        torch.use_deterministic_algorithms(UpperCAmelCase__  )






  @classmethod
  def  lowerCAmelCase__ (      cls			:       List[str]  )				->Any:
        super().tearDownClass()
        torch.use_deterministic_algorithms(UpperCAmelCase__  )






  def  lowerCAmelCase__ (      self			:       Tuple  )				->str:
        torch.manual_seed(0  )
        UpperCAmelCase_            =  UNetaDConditionModel(
            block_out_channels=(32, 64)  ,							layers_per_block=1  ,							sample_size=32  ,							in_channels=4  ,							out_channels=4  ,							down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')  ,							up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')  ,							cross_attention_dim=32  ,							attention_head_dim=(2, 4)  ,							use_linear_projection=UpperCAmelCase__  ,							)
        UpperCAmelCase_            =  DDIMScheduler(
            beta_start=0.0_0085  ,							beta_end=0.012  ,							beta_schedule='''scaled_linear'''  ,							clip_sample=UpperCAmelCase__  ,							set_alpha_to_one=UpperCAmelCase__  ,							)
        torch.manual_seed(0  )
        UpperCAmelCase_            =  AutoencoderKL(
            block_out_channels=[32, 64]  ,							in_channels=3  ,							out_channels=3  ,							down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']  ,							up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']  ,							latent_channels=4  ,							sample_size=128  ,							)
        torch.manual_seed(0  )
        UpperCAmelCase_            =  CLIPTextConfig(
            bos_token_id=0  ,							eos_token_id=2  ,							hidden_size=32  ,							intermediate_size=37  ,							layer_norm_eps=1e-05  ,							num_attention_heads=4  ,							num_hidden_layers=5  ,							pad_token_id=1  ,							vocab_size=1000  ,							hidden_act='''gelu'''  ,							projection_dim=512  ,							)
        UpperCAmelCase_            =  CLIPTextModel(UpperCAmelCase__  )
        UpperCAmelCase_            =  CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''  )

        UpperCAmelCase_            =  {
            'unet': unet,
            'scheduler': scheduler,
            'vae': vae,
            'text_encoder': text_encoder,
            'tokenizer': tokenizer,
            'safety_checker': None,
            'feature_extractor': None,
        }

        return components






  def  lowerCAmelCase__ (      self			:       Optional[Any]  ,							UpperCAmelCase__			:       int  ,							UpperCAmelCase__			:       Tuple=0  )				->Union[str, Any]:







        if str(UpperCAmelCase__  ).startswith('''mps'''  ):
              UpperCAmelCase_            =  torch.manual_seed(UpperCAmelCase__  )
        else:
              UpperCAmelCase_            =  torch.Generator(device=UpperCAmelCase__  ).manual_seed(UpperCAmelCase__  )
        UpperCAmelCase_            =  {
            'prompt': 'a cat and a frog',
            'token_indices': [2, 5],
            'generator': generator,
            'num_inference_steps': 1,
            'guidance_scale': 6.0,
            'output_type': 'numpy',
            'max_iter_to_alter': 2,
            'thresholds': {0: 0.7},
        }
        return inputs






  def  lowerCAmelCase__ (      self			:       Union[str, Any]  )				->str:
        UpperCAmelCase_            =  'cpu'

        UpperCAmelCase_            =  self.get_dummy_components()
        UpperCAmelCase_            =  self.pipeline_class(**UpperCAmelCase__  )
        pipe.to(UpperCAmelCase__  )
        pipe.set_progress_bar_config(disable=UpperCAmelCase__  )

        UpperCAmelCase_            =  self.get_dummy_inputs(UpperCAmelCase__  )
        UpperCAmelCase_            =  pipe(**UpperCAmelCase__  ).images
        UpperCAmelCase_            =  image[0, -3:, -3:, -1]

        self.assertEqual(image.shape  ,							(1, 64, 64, 3)  )
        UpperCAmelCase_            =  np.array(
            [0.6390_5364, 0.6289_7307, 0.4859_9017, 0.513_3624, 0.555_0048, 0.4576_9516, 0.5032_6973, 0.502_3139, 0.4538_4496]  )
        UpperCAmelCase_            =  np.abs(image_slice.flatten() - expected_slice  ).max()
        self.assertLessEqual(UpperCAmelCase__  ,							1e-3  )






  def  lowerCAmelCase__ (      self			:       Union[str, Any]  )				->Any:
        super().test_cpu_offload_forward_pass(expected_max_diff=5e-4  )






  def  lowerCAmelCase__ (      self			:       Optional[int]  )				->str:
        self._test_inference_batch_consistent(batch_sizes=[1, 2]  )






  def  lowerCAmelCase__ (      self			:       Dict  )				->List[str]:
        self._test_inference_batch_single_identical(batch_size=2  ,							expected_max_diff=7e-4  )






  def  lowerCAmelCase__ (      self			:       str  )				->List[str]:
        super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3  )






  def  lowerCAmelCase__ (      self			:       Optional[int]  )				->str:
        super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4  )






  def  lowerCAmelCase__ (      self			:       int  )				->Union[str, Any]:
        super().test_save_load_local(expected_max_difference=5e-4  )






  def  lowerCAmelCase__ (      self			:       Optional[Any]  )				->Optional[Any]:
        super().test_save_load_optional_components(expected_max_difference=4e-4  )







@require_torch_gpu
@slow
class lowerCamelCase							(	unittest.TestCase					):




  '''simple docstring'''



  @classmethod
  def  lowerCAmelCase__ (      cls			:       Any  )				->Dict:
        super().setUpClass()
        torch.use_deterministic_algorithms(UpperCAmelCase__  )






  @classmethod
  def  lowerCAmelCase__ (      cls			:       Union[str, Any]  )				->int:
        super().tearDownClass()
        torch.use_deterministic_algorithms(UpperCAmelCase__  )






  def  lowerCAmelCase__ (      self			:       Optional[Any]  )				->Any:
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()






  def  lowerCAmelCase__ (      self			:       int  )				->str:
        UpperCAmelCase_            =  torch.manual_seed(51  )

        UpperCAmelCase_            =  StableDiffusionAttendAndExcitePipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''  ,							safety_checker=UpperCAmelCase__  ,							torch_dtype=torch.floataa  )
        pipe.to('''cuda'''  )

        UpperCAmelCase_            =  'a painting of an elephant with glasses'
        UpperCAmelCase_            =  [5, 7]

        UpperCAmelCase_            =  pipe(
            prompt=UpperCAmelCase__  ,							token_indices=UpperCAmelCase__  ,							guidance_scale=7.5  ,							generator=UpperCAmelCase__  ,							num_inference_steps=5  ,							max_iter_to_alter=5  ,							output_type='''numpy'''  ,							).images[0]

        UpperCAmelCase_            =  load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy'''  )
        assert np.abs((expected_image - image).max()  ) < 5e-1






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





