


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__   (       snake_case__      ):







    """simple docstring"""

    __magic_name__         =						42






class     A__   (       snake_case__			,   snake_case__      ):







    """simple docstring"""

    __magic_name__         =						True
    @register_to_config
    def __init__(			self     ,   __snake_case = 3     ,   __snake_case = 3     ,   __snake_case = ("DownEncoderBlock2D",)     ,   __snake_case = ("UpDecoderBlock2D",)     ,   __snake_case = (6_4,)     ,   __snake_case = 1     ,   __snake_case = "silu"     ,   __snake_case = 4     ,   __snake_case = 3_2     ,   __snake_case = 3_2     ,   __snake_case = 0.1_8215     ,   ):
         super().__init__()

         # pass init params to Encoder
         snake_case											=							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
         snake_case											=							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     ,   )

         snake_case											=							nn.Convad(2 * latent_channels     ,   2 * latent_channels     ,   1       )
         snake_case											=							nn.Convad(__snake_case     ,   __snake_case     ,   1       )

         snake_case											=							False
         snake_case											=							False

         # only relevant if vae tiling is enabled
         snake_case											=							self.config.sample_size
         snake_case											=							(
             self.config.sample_size[0]
             if isinstance(self.config.sample_size     ,   (list, tuple)       )
             else self.config.sample_size
         )
         snake_case											=							int(sample_size / (2 ** (len(self.config.block_out_channels       ) - 1))       )
         snake_case											=							0.25
    def 						a_     (			self     ,   __snake_case     ,   __snake_case=False       ):
         if isinstance(__snake_case     ,   (Encoder, Decoder)       ):
              snake_case											=							value
    def 						a_     (			self     ,   __snake_case = True       ):
         snake_case											=							use_tiling
    def 						a_     (			self       ):
         self.enable_tiling(__snake_case       )
    def 						a_     (			self       ):
         snake_case											=							True
    def 						a_     (			self       ):
         snake_case											=							False
    @property
    # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
    def 						a_     (			self       ):
         snake_case											=							{}

         def fn_recursive_add_processors(__snake_case     ,   __snake_case     ,   __snake_case       ):
              if hasattr(__snake_case     ,   '''set_processor'''       ):
                   snake_case											=							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 						a_     (			self     ,   __snake_case       ):
         snake_case											=							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 						a_     (			self       ):
         self.set_attn_processor(AttnProcessor()       )
    @apply_forward_hook
    def 						a_     (			self     ,   __snake_case     ,   __snake_case = 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(__snake_case     ,   return_dict=__snake_case       )

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

         snake_case											=							self.quant_conv(__snake_case       )
         snake_case											=							DiagonalGaussianDistribution(__snake_case       )

         if not return_dict:
              return (posterior,)

         return AutoencoderKLOutput(latent_dist=__snake_case       )
    def 						a_     (			self     ,   __snake_case     ,   __snake_case = 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(__snake_case     ,   return_dict=__snake_case       )

         snake_case											=							self.post_quant_conv(__snake_case       )
         snake_case											=							self.decoder(__snake_case       )

         if not return_dict:
              return (dec,)

         return DecoderOutput(sample=__snake_case       )
    @apply_forward_hook
    def 						a_     (			self     ,   __snake_case     ,   __snake_case = True       ):
         if self.use_slicing and z.shape[0] > 1:
              snake_case											=							[self._decode(__snake_case       ).sample for z_slice in z.split(1       )]
              snake_case											=							torch.cat(__snake_case       )
         else:
              snake_case											=							self._decode(__snake_case       ).sample

         if not return_dict:
              return (decoded,)

         return DecoderOutput(sample=__snake_case       )
    def 						a_     (			self     ,   __snake_case     ,   __snake_case     ,   __snake_case       ):
         snake_case											=							min(a.shape[2]     ,   b.shape[2]     ,   __snake_case       )
         for y in range(__snake_case       ):
              snake_case											=							a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
         return b
    def 						a_     (			self     ,   __snake_case     ,   __snake_case     ,   __snake_case       ):
         snake_case											=							min(a.shape[3]     ,   b.shape[3]     ,   __snake_case       )
         for x in range(__snake_case       ):
              snake_case											=							a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
         return b
    def 						a_     (			self     ,   __snake_case     ,   __snake_case = True       ):
         snake_case											=							int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)       )
         snake_case											=							int(self.tile_latent_min_size * self.tile_overlap_factor       )
         snake_case											=							self.tile_latent_min_size - blend_extent

         # Split the image into 512x512 tiles and encode them separately.
         snake_case											=							[]
         for i in range(0     ,   x.shape[2]     ,   __snake_case       ):
              snake_case											=							[]
              for j in range(0     ,   x.shape[3]     ,   __snake_case       ):
                   snake_case											=							x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
                   snake_case											=							self.encoder(__snake_case       )
                   snake_case											=							self.quant_conv(__snake_case       )
                   row.append(__snake_case       )
              rows.append(__snake_case       )
         snake_case											=							[]
         for i, row in enumerate(__snake_case       ):
              snake_case											=							[]
              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:
                        snake_case											=							self.blend_v(rows[i - 1][j]     ,   __snake_case     ,   __snake_case       )
                   if j > 0:
                        snake_case											=							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       )       )

         snake_case											=							torch.cat(__snake_case     ,   dim=2       )
         snake_case											=							DiagonalGaussianDistribution(__snake_case       )

         if not return_dict:
              return (posterior,)

         return AutoencoderKLOutput(latent_dist=__snake_case       )
    def 						a_     (			self     ,   __snake_case     ,   __snake_case = True       ):
         snake_case											=							int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)       )
         snake_case											=							int(self.tile_sample_min_size * self.tile_overlap_factor       )
         snake_case											=							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.
         snake_case											=							[]
         for i in range(0     ,   z.shape[2]     ,   __snake_case       ):
              snake_case											=							[]
              for j in range(0     ,   z.shape[3]     ,   __snake_case       ):
                   snake_case											=							z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
                   snake_case											=							self.post_quant_conv(__snake_case       )
                   snake_case											=							self.decoder(__snake_case       )
                   row.append(__snake_case       )
              rows.append(__snake_case       )
         snake_case											=							[]
         for i, row in enumerate(__snake_case       ):
              snake_case											=							[]
              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:
                        snake_case											=							self.blend_v(rows[i - 1][j]     ,   __snake_case     ,   __snake_case       )
                   if j > 0:
                        snake_case											=							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       )       )

         snake_case											=							torch.cat(__snake_case     ,   dim=2       )
         if not return_dict:
              return (dec,)

         return DecoderOutput(sample=__snake_case       )




    def 						a_     (			self     ,   __snake_case     ,   __snake_case = False     ,   __snake_case = True     ,   __snake_case = None     ,   ):
         snake_case											=							sample
         snake_case											=							self.encode(__snake_case       ).latent_dist
         if sample_posterior:
              snake_case											=							posterior.sample(generator=__snake_case       )
         else:
              snake_case											=							posterior.mode()
         snake_case											=							self.decode(__snake_case       ).sample

         if not return_dict:
              return (dec,)

         return DecoderOutput(sample=__snake_case       )








from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE     :					Optional[Any]      =       logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE     :					int      =       {
    "facebook/deit-base-distilled-patch16-224": (
        "https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json"
    ),
    # See all DeiT models at https://huggingface.co/models?filter=deit
}

class     A__   (       snake_case__      ):







    """simple docstring"""

    __magic_name__         =						'deit'
    def __init__(			self     ,   __snake_case=7_6_8     ,   __snake_case=1_2     ,   __snake_case=1_2     ,   __snake_case=3_0_7_2     ,   __snake_case="gelu"     ,   __snake_case=0.0     ,   __snake_case=0.0     ,   __snake_case=0.02     ,   __snake_case=1E-12     ,   __snake_case=2_2_4     ,   __snake_case=1_6     ,   __snake_case=3     ,   __snake_case=True     ,   __snake_case=1_6     ,   **__snake_case     ,   ):
         super().__init__(**__snake_case       )

         snake_case											=							hidden_size
         snake_case											=							num_hidden_layers
         snake_case											=							num_attention_heads
         snake_case											=							intermediate_size
         snake_case											=							hidden_act
         snake_case											=							hidden_dropout_prob
         snake_case											=							attention_probs_dropout_prob
         snake_case											=							initializer_range
         snake_case											=							layer_norm_eps
         snake_case											=							image_size
         snake_case											=							patch_size
         snake_case											=							num_channels
         snake_case											=							qkv_bias
         snake_case											=							encoder_stride






class     A__   (       snake_case__      ):







    """simple docstring"""

    __magic_name__         =						version.parse('1.11'      )
    @property
    def 						a_     (			self       ):
         return OrderedDict(
             [
                 ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
             ]       )




    @property
    def 						a_     (			self       ):
         return 1E-4




