


"""simple docstring"""







from dataclasses import dataclass
from typing import Dict, Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .attention_processor import AttentionProcessor, AttnProcessor
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder


@dataclass
class 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       a  :    "DiagonalGaussianDistribution"


class 				SCREAMING_SNAKE_CASE__    (		lowercase       ,   lowercase    ):
       """simple docstring"""


       a  :    List[Any]       =True

       @register_to_config
       def __init__(						self		,					snake_case__ = 3		,					snake_case__ = 3		,					snake_case__ = ("DownEncoderBlock2D",)		,					snake_case__ = ("UpDecoderBlock2D",)		,					snake_case__ = (64,)		,					snake_case__ = 1		,					snake_case__ = "silu"		,					snake_case__ = 4		,					snake_case__ = 32		,					snake_case__ = 32		,					snake_case__ = 0.18215		,					):






         """simple docstring"""





         super().__init__()

         # pass init params to Encoder
         lowerCAmelCase					:					int								=							Encoder(
             in_channels=snake_case__		,					out_channels=snake_case__		,					down_block_types=snake_case__		,					block_out_channels=snake_case__		,					layers_per_block=snake_case__		,					act_fn=snake_case__		,					norm_num_groups=snake_case__		,					double_z=snake_case__		,					)

         # pass init params to Decoder
         lowerCAmelCase					:					Optional[int]								=							Decoder(
             in_channels=snake_case__		,					out_channels=snake_case__		,					up_block_types=snake_case__		,					block_out_channels=snake_case__		,					layers_per_block=snake_case__		,					norm_num_groups=snake_case__		,					act_fn=snake_case__		,					)

         lowerCAmelCase					:					List[str]								=							nn.Convad(2 * latent_channels		,					2 * latent_channels		,					1						)
         lowerCAmelCase					:					Optional[int]								=							nn.Convad(snake_case__		,					snake_case__		,					1						)

         lowerCAmelCase					:					Tuple								=							False
         lowerCAmelCase					:					Any								=							False

         # only relevant if vae tiling is enabled
         lowerCAmelCase					:					Tuple								=							self.config.sample_size
         lowerCAmelCase					:					List[Any]								=							(
             self.config.sample_size[0]
             if isinstance(self.config.sample_size		,					(list, tuple)						)
             else self.config.sample_size
         )
         lowerCAmelCase					:					Any								=							int(sample_size / (2 ** (len(self.config.block_out_channels						) - 1))						)
         lowerCAmelCase					:					Optional[int]								=							0.25

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__=False						):






         """simple docstring"""





         if isinstance(snake_case__		,					(Encoder, Decoder)						):
           lowerCAmelCase					:					Union[str, Any]								=							value

       def 	lowercase__							(						self		,					snake_case__ = True						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							use_tiling

       def 	lowercase__							(						self						):






         """simple docstring"""





         self.enable_tiling(snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Union[str, Any]								=							True

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							False

       @property
       # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							{}

         def fn_recursive_add_processors(snake_case__		,					snake_case__		,					snake_case__						):
           if hasattr(snake_case__		,					"set_processor"						):
             lowerCAmelCase					:					Union[str, Any]								=							module.processor

           for sub_name, child in module.named_children():
             fn_recursive_add_processors(f"""{name}.{sub_name}"""		,					snake_case__		,					snake_case__						)

           return processors

         for name, module in self.named_children():
           fn_recursive_add_processors(snake_case__		,					snake_case__		,					snake_case__						)

         return processors

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							len(self.attn_processors.keys()						)

         if isinstance(snake_case__		,					snake_case__						) and len(snake_case__						) != count:
           raise ValueError(
               f"""A dict of processors was passed, but the number of processors {len(snake_case__						)} does not match the"""
               f""" number of attention layers: {count}. Please make sure to pass {count} processor classes."""						)

         def fn_recursive_attn_processor(snake_case__		,					snake_case__		,					snake_case__						):
           if hasattr(snake_case__		,					"set_processor"						):
             if not isinstance(snake_case__		,					snake_case__						):
               module.set_processor(snake_case__						)
             else:
               module.set_processor(processor.pop(f"""{name}.processor"""						)						)

           for sub_name, child in module.named_children():
             fn_recursive_attn_processor(f"""{name}.{sub_name}"""		,					snake_case__		,					snake_case__						)

         for name, module in self.named_children():
           fn_recursive_attn_processor(snake_case__		,					snake_case__		,					snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         self.set_attn_processor(AttnProcessor()						)

       @apply_forward_hook
       def 	lowercase__							(						self		,					snake_case__		,					snake_case__ = True						):






         """simple docstring"""





         if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size):
           return self.tiled_encode(snake_case__		,					return_dict=snake_case__						)

         if self.use_slicing and x.shape[0] > 1:
           lowerCAmelCase					:					Tuple								=							[self.encoder(snake_case__						) for x_slice in x.split(1						)]
           lowerCAmelCase					:					List[Any]								=							torch.cat(snake_case__						)
         else:
           lowerCAmelCase					:					Optional[Any]								=							self.encoder(snake_case__						)

         lowerCAmelCase					:					Tuple								=							self.quant_conv(snake_case__						)
         lowerCAmelCase					:					Any								=							DiagonalGaussianDistribution(snake_case__						)

         if not return_dict:
           return (posterior,)

         return AutoencoderKLOutput(latent_dist=snake_case__						)

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__ = True						):






         """simple docstring"""





         if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size):
           return self.tiled_decode(snake_case__		,					return_dict=snake_case__						)

         lowerCAmelCase					:					List[Any]								=							self.post_quant_conv(snake_case__						)
         lowerCAmelCase					:					Any								=							self.decoder(snake_case__						)

         if not return_dict:
           return (dec,)

         return DecoderOutput(sample=snake_case__						)

       @apply_forward_hook
       def 	lowercase__							(						self		,					snake_case__		,					snake_case__ = True						):






         """simple docstring"""





         if self.use_slicing and z.shape[0] > 1:
           lowerCAmelCase					:					Optional[int]								=							[self._decode(snake_case__						).sample for z_slice in z.split(1						)]
           lowerCAmelCase					:					List[str]								=							torch.cat(snake_case__						)
         else:
           lowerCAmelCase					:					Optional[int]								=							self._decode(snake_case__						).sample

         if not return_dict:
           return (decoded,)

         return DecoderOutput(sample=snake_case__						)

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							min(a.shape[2]		,					b.shape[2]		,					snake_case__						)
         for y in range(snake_case__						):
           lowerCAmelCase					:					int								=							a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
         return b

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Union[str, Any]								=							min(a.shape[3]		,					b.shape[3]		,					snake_case__						)
         for x in range(snake_case__						):
           lowerCAmelCase					:					List[str]								=							a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
         return b

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__ = True						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)						)
         lowerCAmelCase					:					List[str]								=							int(self.tile_latent_min_size * self.tile_overlap_factor						)
         lowerCAmelCase					:					Union[str, Any]								=							self.tile_latent_min_size - blend_extent

         # Split the image into 512x512 tiles and encode them separately.
         lowerCAmelCase					:					int								=							[]
         for i in range(0		,					x.shape[2]		,					snake_case__						):
           lowerCAmelCase					:					int								=							[]
           for j in range(0		,					x.shape[3]		,					snake_case__						):
             lowerCAmelCase					:					List[str]								=							x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
             lowerCAmelCase					:					List[str]								=							self.encoder(snake_case__						)
             lowerCAmelCase					:					List[Any]								=							self.quant_conv(snake_case__						)
             row.append(snake_case__						)
           rows.append(snake_case__						)
         lowerCAmelCase					:					List[Any]								=							[]
         for i, row in enumerate(snake_case__						):
           lowerCAmelCase					:					List[Any]								=							[]
           for j, tile in enumerate(snake_case__						):
             # blend the above tile and the left tile
             # to the current tile and add the current tile to the result row
             if i > 0:
               lowerCAmelCase					:					Any								=							self.blend_v(rows[i - 1][j]		,					snake_case__		,					snake_case__						)
             if j > 0:
               lowerCAmelCase					:					Dict								=							self.blend_h(row[j - 1]		,					snake_case__		,					snake_case__						)
             result_row.append(tile[:, :, :row_limit, :row_limit]						)
           result_rows.append(torch.cat(snake_case__		,					dim=3						)						)

         lowerCAmelCase					:					Optional[int]								=							torch.cat(snake_case__		,					dim=2						)
         lowerCAmelCase					:					Any								=							DiagonalGaussianDistribution(snake_case__						)

         if not return_dict:
           return (posterior,)

         return AutoencoderKLOutput(latent_dist=snake_case__						)

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__ = True						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)						)
         lowerCAmelCase					:					int								=							int(self.tile_sample_min_size * self.tile_overlap_factor						)
         lowerCAmelCase					:					Dict								=							self.tile_sample_min_size - blend_extent

         # Split z into overlapping 64x64 tiles and decode them separately.
         # The tiles have an overlap to avoid seams between tiles.
         lowerCAmelCase					:					Dict								=							[]
         for i in range(0		,					z.shape[2]		,					snake_case__						):
           lowerCAmelCase					:					Optional[Any]								=							[]
           for j in range(0		,					z.shape[3]		,					snake_case__						):
             lowerCAmelCase					:					int								=							z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
             lowerCAmelCase					:					List[str]								=							self.post_quant_conv(snake_case__						)
             lowerCAmelCase					:					Optional[Any]								=							self.decoder(snake_case__						)
             row.append(snake_case__						)
           rows.append(snake_case__						)
         lowerCAmelCase					:					List[str]								=							[]
         for i, row in enumerate(snake_case__						):
           lowerCAmelCase					:					List[str]								=							[]
           for j, tile in enumerate(snake_case__						):
             # blend the above tile and the left tile
             # to the current tile and add the current tile to the result row
             if i > 0:
               lowerCAmelCase					:					Dict								=							self.blend_v(rows[i - 1][j]		,					snake_case__		,					snake_case__						)
             if j > 0:
               lowerCAmelCase					:					Dict								=							self.blend_h(row[j - 1]		,					snake_case__		,					snake_case__						)
             result_row.append(tile[:, :, :row_limit, :row_limit]						)
           result_rows.append(torch.cat(snake_case__		,					dim=3						)						)

         lowerCAmelCase					:					Tuple								=							torch.cat(snake_case__		,					dim=2						)
         if not return_dict:
           return (dec,)

         return DecoderOutput(sample=snake_case__						)







       def 	lowercase__							(						self		,					snake_case__		,					snake_case__ = False		,					snake_case__ = True		,					snake_case__ = None		,					):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							sample
         lowerCAmelCase					:					Dict								=							self.encode(snake_case__						).latent_dist
         if sample_posterior:
           lowerCAmelCase					:					str								=							posterior.sample(generator=snake_case__						)
         else:
           lowerCAmelCase					:					str								=							posterior.mode()
         lowerCAmelCase					:					Optional[Any]								=							self.decode(snake_case__						).sample

         if not return_dict:
           return (dec,)

         return DecoderOutput(sample=snake_case__						)









"""simple docstring"""







from typing import List, Optional

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


lowerCAmelCase__				    =       logging.get_logger(__name__)

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


class 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       a  :    Dict       ="autoformer"
       a  :    Dict       ={
           "hidden_size": "d_model",
           "num_attention_heads": "encoder_attention_heads",
           "num_hidden_layers": "encoder_layers",
       }

       def __init__(						self		,					snake_case__ = None		,					snake_case__ = None		,					snake_case__ = "student_t"		,					snake_case__ = "nll"		,					snake_case__ = 1		,					snake_case__ = [1, 2, 3, 4, 5, 6, 7]		,					snake_case__ = True		,					snake_case__ = 0		,					snake_case__ = 0		,					snake_case__ = 0		,					snake_case__ = 0		,					snake_case__ = None		,					snake_case__ = None		,					snake_case__ = 64		,					snake_case__ = 2		,					snake_case__ = 2		,					snake_case__ = 2		,					snake_case__ = 2		,					snake_case__ = 32		,					snake_case__ = 32		,					snake_case__ = "gelu"		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 100		,					snake_case__ = 0.02		,					snake_case__ = True		,					snake_case__=True		,					snake_case__ = 10		,					snake_case__ = 25		,					snake_case__ = 3		,					**snake_case__		,					):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							prediction_length
         lowerCAmelCase					:					Dict								=							context_length if context_length is not None else prediction_length
         lowerCAmelCase					:					Tuple								=							distribution_output
         lowerCAmelCase					:					List[Any]								=							loss
         lowerCAmelCase					:					int								=							input_size
         lowerCAmelCase					:					str								=							num_time_features
         lowerCAmelCase					:					str								=							lags_sequence
         lowerCAmelCase					:					List[str]								=							scaling
         lowerCAmelCase					:					List[Any]								=							num_dynamic_real_features
         lowerCAmelCase					:					Tuple								=							num_static_real_features
         lowerCAmelCase					:					Dict								=							num_static_categorical_features
         if cardinality is not None and num_static_categorical_features > 0:
           if len(snake_case__						) != num_static_categorical_features:
             raise ValueError(
                 "The cardinality should be a list of the same length as `num_static_categorical_features`"						)
           lowerCAmelCase					:					Any								=							cardinality
         else:
           lowerCAmelCase					:					Union[str, Any]								=							[0]
         if embedding_dimension is not None and num_static_categorical_features > 0:
           if len(snake_case__						) != num_static_categorical_features:
             raise ValueError(
                 "The embedding dimension should be a list of the same length as `num_static_categorical_features`"						)
           lowerCAmelCase					:					Tuple								=							embedding_dimension
         else:
           lowerCAmelCase					:					Any								=							[min(50		,					(cat + 1) // 2						) for cat in self.cardinality]
         lowerCAmelCase					:					Any								=							num_parallel_samples

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

         lowerCAmelCase					:					List[Any]								=							dropout
         lowerCAmelCase					:					Optional[int]								=							attention_dropout
         lowerCAmelCase					:					Union[str, Any]								=							activation_dropout
         lowerCAmelCase					:					Optional[int]								=							encoder_layerdrop
         lowerCAmelCase					:					Dict								=							decoder_layerdrop

         lowerCAmelCase					:					Tuple								=							activation_function
         lowerCAmelCase					:					Optional[Any]								=							init_std

         lowerCAmelCase					:					List[Any]								=							use_cache

         # Autoformer
         lowerCAmelCase					:					Any								=							label_length
         lowerCAmelCase					:					Any								=							moving_average
         lowerCAmelCase					:					Optional[Any]								=							autocorrelation_factor

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







       @property
       def 	lowercase__							(						self						):






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





