
"""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__					(						A__       ):
     UpperCAmelCase__         =      42





class 				a__					(						A__   ,							A__       ):
     UpperCAmelCase__         =      True



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







      '''simple docstring'''



      super().__init__()

      # pass init params to Encoder
      UpperCamelCase_     :					Dict   	=Encoder(
          in_channels=_lowerCamelCase      ,  out_channels=_lowerCamelCase      ,  down_block_types=_lowerCamelCase      ,  block_out_channels=_lowerCamelCase      ,  layers_per_block=_lowerCamelCase      ,  act_fn=_lowerCamelCase      ,  norm_num_groups=_lowerCamelCase      ,  double_z=_lowerCamelCase      ,  )

      # pass init params to Decoder
      UpperCamelCase_     :					Dict   	=Decoder(
          in_channels=_lowerCamelCase      ,  out_channels=_lowerCamelCase      ,  up_block_types=_lowerCamelCase      ,  block_out_channels=_lowerCamelCase      ,  layers_per_block=_lowerCamelCase      ,  norm_num_groups=_lowerCamelCase      ,  act_fn=_lowerCamelCase      ,  )

      UpperCamelCase_     :					int   	=nn.Convad(2 * latent_channels      ,  2 * latent_channels      ,  1   )
      UpperCamelCase_     :					int   	=nn.Convad(_lowerCamelCase      ,  _lowerCamelCase      ,  1   )

      UpperCamelCase_     :					int   	=False
      UpperCamelCase_     :					List[str]   	=False

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



     def  lowerCamelCase_						(							self :List[str]      ,  _lowerCamelCase :Dict      ,  _lowerCamelCase :Optional[int]=False   ):







      '''simple docstring'''



      if isinstance(_lowerCamelCase      ,  (Encoder, Decoder)   ):
       UpperCamelCase_     :					Tuple   	=value



     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :bool = True   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[int]   	=use_tiling



     def  lowerCamelCase_						(							self :int   ):







      '''simple docstring'''



      self.enable_tiling(_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Any   	=True



     def  lowerCamelCase_						(							self :List[str]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Dict   	=False



     @property
     # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
     def  lowerCamelCase_						(							self :Union[str, Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[Any]   	={}

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

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

       return processors

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

      return processors



     def  lowerCamelCase_						(							self :str      ,  _lowerCamelCase :Union[AttentionProcessor, Dict[str, AttentionProcessor]]   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[str]   	=len(self.attn_processors.keys()   )

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

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

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



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







      '''simple docstring'''



      self.set_attn_processor(AttnProcessor()   )



     @apply_forward_hook
     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :torch.FloatTensor      ,  _lowerCamelCase :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(_lowerCamelCase      ,  return_dict=_lowerCamelCase   )

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

      UpperCamelCase_     :					List[str]   	=self.quant_conv(_lowerCamelCase   )
      UpperCamelCase_     :					Union[str, Any]   	=DiagonalGaussianDistribution(_lowerCamelCase   )

      if not return_dict:
       return (posterior,)

      return AutoencoderKLOutput(latent_dist=_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :torch.FloatTensor      ,  _lowerCamelCase :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(_lowerCamelCase      ,  return_dict=_lowerCamelCase   )

      UpperCamelCase_     :					List[Any]   	=self.post_quant_conv(_lowerCamelCase   )
      UpperCamelCase_     :					Tuple   	=self.decoder(_lowerCamelCase   )

      if not return_dict:
       return (dec,)

      return DecoderOutput(sample=_lowerCamelCase   )



     @apply_forward_hook
     def  lowerCamelCase_						(							self :int      ,  _lowerCamelCase :torch.FloatTensor      ,  _lowerCamelCase :bool = True   ):







      '''simple docstring'''



      if self.use_slicing and z.shape[0] > 1:
       UpperCamelCase_     :					List[Any]   	=[self._decode(_lowerCamelCase   ).sample for z_slice in z.split(1   )]
       UpperCamelCase_     :					Tuple   	=torch.cat(_lowerCamelCase   )
      else:
       UpperCamelCase_     :					List[str]   	=self._decode(_lowerCamelCase   ).sample

      if not return_dict:
       return (decoded,)

      return DecoderOutput(sample=_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Tuple      ,  _lowerCamelCase :Tuple      ,  _lowerCamelCase :List[str]      ,  _lowerCamelCase :str   ):







      '''simple docstring'''



      UpperCamelCase_     :					Dict   	=min(a.shape[2]      ,  b.shape[2]      ,  _lowerCamelCase   )
      for y in range(_lowerCamelCase   ):
       UpperCamelCase_     :					Optional[Any]   	=a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
      return b



     def  lowerCamelCase_						(							self :List[Any]      ,  _lowerCamelCase :int      ,  _lowerCamelCase :str      ,  _lowerCamelCase :Optional[Any]   ):







      '''simple docstring'''



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



     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :torch.FloatTensor      ,  _lowerCamelCase :bool = True   ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)   )
      UpperCamelCase_     :					Any   	=int(self.tile_latent_min_size * self.tile_overlap_factor   )
      UpperCamelCase_     :					Optional[int]   	=self.tile_latent_min_size - blend_extent

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

      UpperCamelCase_     :					List[str]   	=torch.cat(_lowerCamelCase      ,  dim=2   )
      UpperCamelCase_     :					Optional[Any]   	=DiagonalGaussianDistribution(_lowerCamelCase   )

      if not return_dict:
       return (posterior,)

      return AutoencoderKLOutput(latent_dist=_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :torch.FloatTensor      ,  _lowerCamelCase :bool = True   ):







      '''simple docstring'''



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

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

      return DecoderOutput(sample=_lowerCamelCase   )




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







      '''simple docstring'''



      UpperCamelCase_     :					List[str]   	=sample
      UpperCamelCase_     :					Tuple   	=self.encode(_lowerCamelCase   ).latent_dist
      if sample_posterior:
       UpperCamelCase_     :					Union[str, Any]   	=posterior.sample(generator=_lowerCamelCase   )
      else:
       UpperCamelCase_     :					Any   	=posterior.mode()
      UpperCamelCase_     :					Tuple   	=self.decode(_lowerCamelCase   ).sample

      if not return_dict:
       return (dec,)

      return DecoderOutput(sample=_lowerCamelCase   )


"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__SCREAMING_SNAKE_CASE							=     logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE							=     {
    'microsoft/focalnet-tiny': 'https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json',
}





class 				a__					(						A__   ,							A__       ):
     UpperCAmelCase__         =      '''focalnet'''



     def __init__(							self :Any      ,  _lowerCamelCase :List[Any]=224      ,  _lowerCamelCase :str=4      ,  _lowerCamelCase :List[str]=3      ,  _lowerCamelCase :Optional[Any]=96      ,  _lowerCamelCase :Dict=False      ,  _lowerCamelCase :Optional[Any]=[192, 384, 768, 768]      ,  _lowerCamelCase :Tuple=[2, 2, 6, 2]      ,  _lowerCamelCase :List[Any]=[2, 2, 2, 2]      ,  _lowerCamelCase :Optional[int]=[3, 3, 3, 3]      ,  _lowerCamelCase :Optional[Any]="gelu"      ,  _lowerCamelCase :Tuple=4.0      ,  _lowerCamelCase :Optional[Any]=0.0      ,  _lowerCamelCase :int=0.1      ,  _lowerCamelCase :List[str]=False      ,  _lowerCamelCase :str=1E-4      ,  _lowerCamelCase :Optional[int]=False      ,  _lowerCamelCase :List[str]=False      ,  _lowerCamelCase :str=False      ,  _lowerCamelCase :Optional[int]=0.02      ,  _lowerCamelCase :int=1E-5      ,  _lowerCamelCase :Tuple=32      ,  _lowerCamelCase :List[str]=None      ,  _lowerCamelCase :Dict=None      ,  **_lowerCamelCase :List[Any]      ,  ):







      '''simple docstring'''



      super().__init__(**_lowerCamelCase   )

      UpperCamelCase_     :					Any   	=image_size
      UpperCamelCase_     :					int   	=patch_size
      UpperCamelCase_     :					int   	=num_channels
      UpperCamelCase_     :					Union[str, Any]   	=embed_dim
      UpperCamelCase_     :					int   	=use_conv_embed
      UpperCamelCase_     :					Optional[int]   	=hidden_sizes
      UpperCamelCase_     :					str   	=depths
      UpperCamelCase_     :					Any   	=focal_levels
      UpperCamelCase_     :					List[str]   	=focal_windows
      UpperCamelCase_     :					str   	=hidden_act
      UpperCamelCase_     :					Dict   	=mlp_ratio
      UpperCamelCase_     :					List[str]   	=hidden_dropout_prob
      UpperCamelCase_     :					Optional[int]   	=drop_path_rate
      UpperCamelCase_     :					str   	=use_layerscale
      UpperCamelCase_     :					List[Any]   	=layerscale_value
      UpperCamelCase_     :					Optional[int]   	=use_post_layernorm
      UpperCamelCase_     :					Dict   	=use_post_layernorm_in_modulation
      UpperCamelCase_     :					Optional[Any]   	=normalize_modulator
      UpperCamelCase_     :					List[Any]   	=initializer_range
      UpperCamelCase_     :					List[str]   	=layer_norm_eps
      UpperCamelCase_     :					List[Any]   	=encoder_stride
      UpperCamelCase_     :					str   	=['stem'] + [f'''stage{idx}''' for idx in range(1      ,  len(self.depths   ) + 1   )]
      UpperCamelCase_	,     UpperCamelCase_     :					int   	=get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase      ,  out_indices=_lowerCamelCase      ,  stage_names=self.stage_names   )
