


'''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     __lowerCAmelCase		(lowercase_	):





      '''simple docstring'''






      lowerCAmelCase__   :	"DiagonalGaussianDistribution"






class     __lowerCAmelCase		(lowercase_    ,	lowercase_	):





      '''simple docstring'''






      lowerCAmelCase__   :	Union[str, Any]       =				True



      @register_to_config
      def __init__(self       :       Any  ,		UpperCamelCase       :       int = 3  ,		UpperCamelCase       :       int = 3  ,		UpperCamelCase       :       Tuple[str] = ("DownEncoderBlock2D",)  ,		UpperCamelCase       :       Tuple[str] = ("UpDecoderBlock2D",)  ,		UpperCamelCase       :       Tuple[int] = (64,)  ,		UpperCamelCase       :       int = 1  ,		UpperCamelCase       :       str = "silu"  ,		UpperCamelCase       :       int = 4  ,		UpperCamelCase       :       int = 32  ,		UpperCamelCase       :       int = 32  ,		UpperCamelCase       :       float = 0.1_82_15  ,		):

             '''simple docstring'''







             super().__init__()

             # pass init params to Encoder
             lowercase__      =					Encoder(
                 in_channels=UpperCamelCase  ,		out_channels=UpperCamelCase  ,		down_block_types=UpperCamelCase  ,		block_out_channels=UpperCamelCase  ,		layers_per_block=UpperCamelCase  ,		act_fn=UpperCamelCase  ,		norm_num_groups=UpperCamelCase  ,		double_z=UpperCamelCase  ,		)

             # pass init params to Decoder
             lowercase__      =					Decoder(
                 in_channels=UpperCamelCase  ,		out_channels=UpperCamelCase  ,		up_block_types=UpperCamelCase  ,		block_out_channels=UpperCamelCase  ,		layers_per_block=UpperCamelCase  ,		norm_num_groups=UpperCamelCase  ,		act_fn=UpperCamelCase  ,		)

             lowercase__      =					nn.Convad(2 * latent_channels  ,		2 * latent_channels  ,		1					)
             lowercase__      =					nn.Convad(UpperCamelCase  ,		UpperCamelCase  ,		1					)

             lowercase__      =					False
             lowercase__      =					False

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



      def  UpperCamelCase__						(self       :       Optional[Any]  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       List[str]=False					):

             '''simple docstring'''







             if isinstance(UpperCamelCase  ,		(Encoder, Decoder)					):
                    lowercase__      =					value



      def  UpperCamelCase__						(self       :       Any  ,		UpperCamelCase       :       bool = True					):

             '''simple docstring'''







             lowercase__      =					use_tiling



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             self.enable_tiling(UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					True



      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             lowercase__      =					False



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

             '''simple docstring'''







             lowercase__      =					{}

             def fn_recursive_add_processors(UpperCamelCase       :       str  ,		UpperCamelCase       :       torch.nn.Module  ,		UpperCamelCase       :       Dict[str, AttentionProcessor]					):
                    if hasattr(UpperCamelCase  ,		'''set_processor'''					):
                           lowercase__      =					module.processor

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

                    return processors

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

             return processors



      def  UpperCamelCase__						(self       :       Union[str, Any]  ,		UpperCamelCase       :       Union[AttentionProcessor, Dict[str, AttentionProcessor]]					):

             '''simple docstring'''







             lowercase__      =					len(self.attn_processors.keys()					)

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

             def fn_recursive_attn_processor(UpperCamelCase       :       str  ,		UpperCamelCase       :       torch.nn.Module  ,		UpperCamelCase       :       str					):
                    if hasattr(UpperCamelCase  ,		'''set_processor'''					):
                           if not isinstance(UpperCamelCase  ,		UpperCamelCase					):
                                  module.set_processor(UpperCamelCase					)
                           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}"  ,		UpperCamelCase  ,		UpperCamelCase					)

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



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

             '''simple docstring'''







             self.set_attn_processor(AttnProcessor()					)



      @apply_forward_hook
      def  UpperCamelCase__						(self       :       Optional[Any]  ,		UpperCamelCase       :       torch.FloatTensor  ,		UpperCamelCase       :       bool = 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(UpperCamelCase  ,		return_dict=UpperCamelCase					)

             if self.use_slicing and x.shape[0] > 1:
                    lowercase__      =					[self.encoder(UpperCamelCase					) for x_slice in x.split(1					)]
                    lowercase__      =					torch.cat(UpperCamelCase					)
             else:
                    lowercase__      =					self.encoder(UpperCamelCase					)

             lowercase__      =					self.quant_conv(UpperCamelCase					)
             lowercase__      =					DiagonalGaussianDistribution(UpperCamelCase					)

             if not return_dict:
                    return (posterior,)

             return AutoencoderKLOutput(latent_dist=UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Optional[Any]  ,		UpperCamelCase       :       torch.FloatTensor  ,		UpperCamelCase       :       bool = 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(UpperCamelCase  ,		return_dict=UpperCamelCase					)

             lowercase__      =					self.post_quant_conv(UpperCamelCase					)
             lowercase__      =					self.decoder(UpperCamelCase					)

             if not return_dict:
                    return (dec,)

             return DecoderOutput(sample=UpperCamelCase					)



      @apply_forward_hook
      def  UpperCamelCase__						(self       :       str  ,		UpperCamelCase       :       torch.FloatTensor  ,		UpperCamelCase       :       bool = True					):

             '''simple docstring'''







             if self.use_slicing and z.shape[0] > 1:
                    lowercase__      =					[self._decode(UpperCamelCase					).sample for z_slice in z.split(1					)]
                    lowercase__      =					torch.cat(UpperCamelCase					)
             else:
                    lowercase__      =					self._decode(UpperCamelCase					).sample

             if not return_dict:
                    return (decoded,)

             return DecoderOutput(sample=UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Optional[int]  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       Dict					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       Optional[int]  ,		UpperCamelCase       :       Union[str, Any]  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       Any					):

             '''simple docstring'''







             lowercase__      =					min(a.shape[3]  ,		b.shape[3]  ,		UpperCamelCase					)
             for x in range(UpperCamelCase					):
                    lowercase__      =					a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
             return b



      def  UpperCamelCase__						(self       :       str  ,		UpperCamelCase       :       torch.FloatTensor  ,		UpperCamelCase       :       bool = True					):

             '''simple docstring'''







             lowercase__      =					int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)					)
             lowercase__      =					int(self.tile_latent_min_size * self.tile_overlap_factor					)
             lowercase__      =					self.tile_latent_min_size - blend_extent

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

             lowercase__      =					torch.cat(UpperCamelCase  ,		dim=2					)
             lowercase__      =					DiagonalGaussianDistribution(UpperCamelCase					)

             if not return_dict:
                    return (posterior,)

             return AutoencoderKLOutput(latent_dist=UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Dict  ,		UpperCamelCase       :       torch.FloatTensor  ,		UpperCamelCase       :       bool = True					):

             '''simple docstring'''







             lowercase__      =					int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)					)
             lowercase__      =					int(self.tile_sample_min_size * self.tile_overlap_factor					)
             lowercase__      =					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.
             lowercase__      =					[]
             for i in range(0  ,		z.shape[2]  ,		UpperCamelCase					):
                    lowercase__      =					[]
                    for j in range(0  ,		z.shape[3]  ,		UpperCamelCase					):
                           lowercase__      =					z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
                           lowercase__      =					self.post_quant_conv(UpperCamelCase					)
                           lowercase__      =					self.decoder(UpperCamelCase					)
                           row.append(UpperCamelCase					)
                    rows.append(UpperCamelCase					)
             lowercase__      =					[]
             for i, row in enumerate(UpperCamelCase					):
                    lowercase__      =					[]
                    for j, tile in enumerate(UpperCamelCase					):
                           # blend the above tile and the left tile
                           # to the current tile and add the current tile to the result row
                           if i > 0:
                                  lowercase__      =					self.blend_v(rows[i - 1][j]  ,		UpperCamelCase  ,		UpperCamelCase					)
                           if j > 0:
                                  lowercase__      =					self.blend_h(row[j - 1]  ,		UpperCamelCase  ,		UpperCamelCase					)
                           result_row.append(tile[:, :, :row_limit, :row_limit]					)
                    result_rows.append(torch.cat(UpperCamelCase  ,		dim=3					)					)

             lowercase__      =					torch.cat(UpperCamelCase  ,		dim=2					)
             if not return_dict:
                    return (dec,)

             return DecoderOutput(sample=UpperCamelCase					)






      def  UpperCamelCase__						(self       :       Dict  ,		UpperCamelCase       :       torch.FloatTensor  ,		UpperCamelCase       :       bool = False  ,		UpperCamelCase       :       bool = True  ,		UpperCamelCase       :       Optional[torch.Generator] = None  ,		):

             '''simple docstring'''







             lowercase__      =					sample
             lowercase__      =					self.encode(UpperCamelCase					).latent_dist
             if sample_posterior:
                    lowercase__      =					posterior.sample(generator=UpperCamelCase					)
             else:
                    lowercase__      =					posterior.mode()
             lowercase__      =					self.decode(UpperCamelCase					).sample

             if not return_dict:
                    return (dec,)

             return DecoderOutput(sample=UpperCamelCase					)








'''simple docstring'''

def _SCREAMING_SNAKE_CASE (A			)			->			bool:






       """simple docstring"""







       if not isinstance(A    ,					A			):
              raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''			)
       if len(A			) == 0:
              raise ValueError('''Input list must be a non empty list'''			)
       if len(A			) == 1:
              return True
       lowercase__      =					series[1] - series[0]
       for index in range(len(A			) - 1			):
              if series[index + 1] - series[index] != common_diff:
                     return False
       return True

def _SCREAMING_SNAKE_CASE (A			)			->			float:






       """simple docstring"""







       if not isinstance(A    ,					A			):
              raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''			)
       if len(A			) == 0:
              raise ValueError('''Input list must be a non empty list'''			)
       lowercase__      =					0
       for val in series:
              answer += val
       return answer / len(A			)


if __name__ == "__main__":
 import doctest

 doctest.testmod()




