





"""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__  ):
 snake_case__             =       42







class       a							(   a__   , a__  ):
 snake_case__             =       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.18_215				,       ):







  """simple docstring"""






  super().__init__()

  # pass init params to Encoder
  lowerCAmelCase    				= 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    				= 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    				= nn.Convad(2 * latent_channels				,       2 * latent_channels				,       1     )
  lowerCAmelCase    				= nn.Convad(_snake_case				,       _snake_case				,       1     )

  lowerCAmelCase    				= False
  lowerCAmelCase    				= False

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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case=False     ):







  """simple docstring"""






  if isinstance(_snake_case				,       (Encoder, Decoder)     ):
   lowerCAmelCase    				= value



 def    UpperCamelCase__  (     self				,       _snake_case = True     ):







  """simple docstring"""






  lowerCAmelCase    				= use_tiling



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  self.enable_tiling(_snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= True



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= False



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







  """simple docstring"""






  lowerCAmelCase    				= {}

  def fn_recursive_add_processors(_snake_case				,       _snake_case				,       _snake_case     ):
   if hasattr(_snake_case				,       'set_processor'     ):
    lowerCAmelCase    				= 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    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= 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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  self.set_attn_processor(AttnProcessor()     )



 @apply_forward_hook
 def    UpperCamelCase__  (     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    				= [self.encoder(_snake_case     ) for x_slice in x.split(1     )]
   lowerCAmelCase    				= torch.cat(_snake_case     )
  else:
   lowerCAmelCase    				= self.encoder(_snake_case     )

  lowerCAmelCase    				= self.quant_conv(_snake_case     )
  lowerCAmelCase    				= DiagonalGaussianDistribution(_snake_case     )

  if not return_dict:
   return (posterior,)

  return AutoencoderKLOutput(latent_dist=_snake_case     )



 def    UpperCamelCase__  (     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    				= self.post_quant_conv(_snake_case     )
  lowerCAmelCase    				= self.decoder(_snake_case     )

  if not return_dict:
   return (dec,)

  return DecoderOutput(sample=_snake_case     )



 @apply_forward_hook
 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = True     ):







  """simple docstring"""






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

  if not return_dict:
   return (decoded,)

  return DecoderOutput(sample=_snake_case     )



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = True     ):







  """simple docstring"""






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

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

  if not return_dict:
   return (posterior,)

  return AutoencoderKLOutput(latent_dist=_snake_case     )



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = True     ):







  """simple docstring"""






  lowerCAmelCase    				= int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)     )
  lowerCAmelCase    				= int(self.tile_sample_min_size * self.tile_overlap_factor     )
  lowerCAmelCase    				= 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    				= []
  for i in range(0				,       z.shape[2]				,       _snake_case     ):
   lowerCAmelCase    				= []
   for j in range(0				,       z.shape[3]				,       _snake_case     ):
    lowerCAmelCase    				= z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
    lowerCAmelCase    				= self.post_quant_conv(_snake_case     )
    lowerCAmelCase    				= self.decoder(_snake_case     )
    row.append(_snake_case     )
   rows.append(_snake_case     )
  lowerCAmelCase    				= []
  for i, row in enumerate(_snake_case     ):
   lowerCAmelCase    				= []
   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    				= self.blend_v(rows[i - 1][j]				,       _snake_case				,       _snake_case     )
    if j > 0:
     lowerCAmelCase    				= 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    				= torch.cat(_snake_case				,       dim=2     )
  if not return_dict:
   return (dec,)

  return DecoderOutput(sample=_snake_case     )



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = False				,       _snake_case = True				,       _snake_case = None				,       ):







  """simple docstring"""






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

  if not return_dict:
   return (dec,)

  return DecoderOutput(sample=_snake_case     )












"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


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

__UpperCamelCase     :						List[Any]						       =  {
    # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert
}







class       a							(   a__  ):
 snake_case__             =       '''megatron-bert'''



 def __init__(     self				,       _snake_case=2_90_56				,       _snake_case=10_24				,       _snake_case=24				,       _snake_case=16				,       _snake_case=40_96				,       _snake_case="gelu"				,       _snake_case=0.1				,       _snake_case=0.1				,       _snake_case=5_12				,       _snake_case=2				,       _snake_case=0.02				,       _snake_case=1E-12				,       _snake_case=0				,       _snake_case="absolute"				,       _snake_case=True				,       **_snake_case				,       ):







  """simple docstring"""






  super().__init__(pad_token_id=_snake_case				,       **_snake_case     )

  lowerCAmelCase    				= vocab_size
  lowerCAmelCase    				= hidden_size
  lowerCAmelCase    				= num_hidden_layers
  lowerCAmelCase    				= num_attention_heads
  lowerCAmelCase    				= hidden_act
  lowerCAmelCase    				= intermediate_size
  lowerCAmelCase    				= hidden_dropout_prob
  lowerCAmelCase    				= attention_probs_dropout_prob
  lowerCAmelCase    				= max_position_embeddings
  lowerCAmelCase    				= type_vocab_size
  lowerCAmelCase    				= initializer_range
  lowerCAmelCase    				= layer_norm_eps
  lowerCAmelCase    				= position_embedding_type
  lowerCAmelCase    				= use_cache





