
"""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 				a			(					lowercase      ):
      UpperCamelCase							:   "DiagonalGaussianDistribution"





class 				a			(					lowercase						,		lowercase      ):
      UpperCamelCase							:   Tuple           =     True



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

          # pass init params to Encoder
          UpperCAmelCase__	:      Dict     							=					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
          UpperCAmelCase__	:      Union[str, Any]     							=					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_     ,	)

          UpperCAmelCase__	:      Tuple     							=					nn.Convad(2 * latent_channels     ,	2 * latent_channels     ,	1      )
          UpperCAmelCase__	:      Dict     							=					nn.Convad(UpperCamelCase_     ,	UpperCamelCase_     ,	1      )

          UpperCAmelCase__	:      Dict     							=					False
          UpperCAmelCase__	:      List[Any]     							=					False

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



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_=False      ):
          if isinstance(UpperCamelCase_     ,	(Encoder, Decoder)      ):
              UpperCAmelCase__	:      int     							=					value



      def  __snake_case							(   self     ,	UpperCamelCase_ = True      ):
          UpperCAmelCase__	:      str     							=					use_tiling



      def  __snake_case							(   self      ):
          self.enable_tiling(UpperCamelCase_      )



      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Optional[Any]     							=					True



      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Dict     							=					False



      @property
      # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Dict     							=					{}

          def fn_recursive_add_processors(UpperCamelCase_     ,	UpperCamelCase_     ,	UpperCamelCase_      ):
              if hasattr(UpperCamelCase_     ,	'set_processor'      ):
                  UpperCAmelCase__	:      Dict     							=					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  __snake_case							(   self     ,	UpperCamelCase_      ):
          UpperCAmelCase__	:      Any     							=					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_     ,	UpperCamelCase_     ,	UpperCamelCase_      ):
              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  __snake_case							(   self      ):
          self.set_attn_processor(AttnProcessor()      )



      @apply_forward_hook
      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_ = True      ):
          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:
              UpperCAmelCase__	:      Optional[Any]     							=					[self.encoder(UpperCamelCase_      ) for x_slice in x.split(1      )]
              UpperCAmelCase__	:      Union[str, Any]     							=					torch.cat(UpperCamelCase_      )
          else:
              UpperCAmelCase__	:      Optional[Any]     							=					self.encoder(UpperCamelCase_      )

          UpperCAmelCase__	:      List[Any]     							=					self.quant_conv(UpperCamelCase_      )
          UpperCAmelCase__	:      int     							=					DiagonalGaussianDistribution(UpperCamelCase_      )

          if not return_dict:
              return (posterior,)

          return AutoencoderKLOutput(latent_dist=UpperCamelCase_      )



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_ = True      ):
          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_      )

          UpperCAmelCase__	:      Optional[int]     							=					self.post_quant_conv(UpperCamelCase_      )
          UpperCAmelCase__	:      List[Any]     							=					self.decoder(UpperCamelCase_      )

          if not return_dict:
              return (dec,)

          return DecoderOutput(sample=UpperCamelCase_      )



      @apply_forward_hook
      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_ = True      ):
          if self.use_slicing and z.shape[0] > 1:
              UpperCAmelCase__	:      Dict     							=					[self._decode(UpperCamelCase_      ).sample for z_slice in z.split(1      )]
              UpperCAmelCase__	:      str     							=					torch.cat(UpperCamelCase_      )
          else:
              UpperCAmelCase__	:      str     							=					self._decode(UpperCamelCase_      ).sample

          if not return_dict:
              return (decoded,)

          return DecoderOutput(sample=UpperCamelCase_      )



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_     ,	UpperCamelCase_      ):
          UpperCAmelCase__	:      Optional[int]     							=					min(a.shape[2]     ,	b.shape[2]     ,	UpperCamelCase_      )
          for y in range(UpperCamelCase_      ):
              UpperCAmelCase__	:      Optional[int]     							=					a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
          return b



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_     ,	UpperCamelCase_      ):
          UpperCAmelCase__	:      List[str]     							=					min(a.shape[3]     ,	b.shape[3]     ,	UpperCamelCase_      )
          for x in range(UpperCamelCase_      ):
              UpperCAmelCase__	:      List[Any]     							=					a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
          return b



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_ = True      ):
          UpperCAmelCase__	:      Dict     							=					int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)      )
          UpperCAmelCase__	:      Union[str, Any]     							=					int(self.tile_latent_min_size * self.tile_overlap_factor      )
          UpperCAmelCase__	:      Dict     							=					self.tile_latent_min_size - blend_extent

          # Split the image into 512x512 tiles and encode them separately.
          UpperCAmelCase__	:      Optional[int]     							=					[]
          for i in range(0     ,	x.shape[2]     ,	UpperCamelCase_      ):
              UpperCAmelCase__	:      Optional[int]     							=					[]
              for j in range(0     ,	x.shape[3]     ,	UpperCamelCase_      ):
                  UpperCAmelCase__	:      Dict     							=					x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
                  UpperCAmelCase__	:      Any     							=					self.encoder(UpperCamelCase_      )
                  UpperCAmelCase__	:      Any     							=					self.quant_conv(UpperCamelCase_      )
                  row.append(UpperCamelCase_      )
              rows.append(UpperCamelCase_      )
          UpperCAmelCase__	:      Optional[int]     							=					[]
          for i, row in enumerate(UpperCamelCase_      ):
              UpperCAmelCase__	:      int     							=					[]
              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:
                      UpperCAmelCase__	:      Tuple     							=					self.blend_v(rows[i - 1][j]     ,	UpperCamelCase_     ,	UpperCamelCase_      )
                  if j > 0:
                      UpperCAmelCase__	:      Optional[Any]     							=					self.blend_h(row[j - 1]     ,	UpperCamelCase_     ,	UpperCamelCase_      )
                  result_row.append(tile[:, :, :row_limit, :row_limit]      )
              result_rows.append(torch.cat(UpperCamelCase_     ,	dim=3      )      )

          UpperCAmelCase__	:      Any     							=					torch.cat(UpperCamelCase_     ,	dim=2      )
          UpperCAmelCase__	:      int     							=					DiagonalGaussianDistribution(UpperCamelCase_      )

          if not return_dict:
              return (posterior,)

          return AutoencoderKLOutput(latent_dist=UpperCamelCase_      )



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_ = True      ):
          UpperCAmelCase__	:      Union[str, Any]     							=					int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)      )
          UpperCAmelCase__	:      str     							=					int(self.tile_sample_min_size * self.tile_overlap_factor      )
          UpperCAmelCase__	:      Tuple     							=					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.
          UpperCAmelCase__	:      List[str]     							=					[]
          for i in range(0     ,	z.shape[2]     ,	UpperCamelCase_      ):
              UpperCAmelCase__	:      Any     							=					[]
              for j in range(0     ,	z.shape[3]     ,	UpperCamelCase_      ):
                  UpperCAmelCase__	:      Optional[Any]     							=					z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
                  UpperCAmelCase__	:      List[Any]     							=					self.post_quant_conv(UpperCamelCase_      )
                  UpperCAmelCase__	:      int     							=					self.decoder(UpperCamelCase_      )
                  row.append(UpperCamelCase_      )
              rows.append(UpperCamelCase_      )
          UpperCAmelCase__	:      Any     							=					[]
          for i, row in enumerate(UpperCamelCase_      ):
              UpperCAmelCase__	:      Dict     							=					[]
              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:
                      UpperCAmelCase__	:      str     							=					self.blend_v(rows[i - 1][j]     ,	UpperCamelCase_     ,	UpperCamelCase_      )
                  if j > 0:
                      UpperCAmelCase__	:      Tuple     							=					self.blend_h(row[j - 1]     ,	UpperCamelCase_     ,	UpperCamelCase_      )
                  result_row.append(tile[:, :, :row_limit, :row_limit]      )
              result_rows.append(torch.cat(UpperCamelCase_     ,	dim=3      )      )

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

          return DecoderOutput(sample=UpperCamelCase_      )



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_ = False     ,	UpperCamelCase_ = True     ,	UpperCamelCase_ = None     ,	):
          UpperCAmelCase__	:      Optional[int]     							=					sample
          UpperCAmelCase__	:      Optional[Any]     							=					self.encode(UpperCamelCase_      ).latent_dist
          if sample_posterior:
              UpperCAmelCase__	:      List[Any]     							=					posterior.sample(generator=UpperCamelCase_      )
          else:
              UpperCAmelCase__	:      Union[str, Any]     							=					posterior.mode()
          UpperCAmelCase__	:      List[str]     							=					self.decode(UpperCamelCase_      ).sample

          if not return_dict:
              return (dec,)

          return DecoderOutput(sample=UpperCamelCase_      )



"""simple docstring"""



from itertools import permutations
def 					lowerCamelCase   (     _snake_case ):
    if num[3] % 2 != 0:
        return False

    if (num[2] + num[3] + num[4]) % 3 != 0:
        return False

    if num[5] % 5 != 0:
        return False

    UpperCAmelCase__	:      List[str]     							=					[7, 11, 13, 17]
    for i, test in enumerate(_snake_case ):
        if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0:
            return False
    return True
def 					lowerCamelCase   (     _snake_case = 10 ):
    return sum(
        int(''.join(map(_snake_case    ,_snake_case ) ) )
        for num in permutations(range(_snake_case ) )
        if is_substring_divisible(_snake_case ) )


if __name__ == "__main__":
    print(f'{solution() = }')

