



'''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 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 __magic_name__					: "DiagonalGaussianDistribution"







class 	UpperCAmelCase	(		__a					,	__a):


 '''simple docstring'''




 __magic_name__					: List[str]  =      True




 @register_to_config
 def __init__(   self							,  lowerCAmelCase_ = 3							,  lowerCAmelCase_ = 3							,  lowerCAmelCase_ = ("DownEncoderBlock2D",)							,  lowerCAmelCase_ = ("UpDecoderBlock2D",)							,  lowerCAmelCase_ = (6_4,)							,  lowerCAmelCase_ = 1							,  lowerCAmelCase_ = "silu"							,  lowerCAmelCase_ = 4							,  lowerCAmelCase_ = 3_2							,  lowerCAmelCase_ = 3_2							,  lowerCAmelCase_ = 0.1_8_2_1_5							,  ) ->      str:



      """simple docstring"""







      super().__init__()

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

      a_       		=nn.Convad(2 * latent_channels							,  2 * latent_channels							,  1)
      a_       		=nn.Convad(lowerCAmelCase_							,  lowerCAmelCase_							,  1)

      a_       		=False
      a_       		=False

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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_=False) ->      Tuple:



      """simple docstring"""







      if isinstance(lowerCAmelCase_							,  (Encoder, Decoder)):
           a_       		=value




 def       lowercase_  (   self							,  lowerCAmelCase_ = True) ->      Union[str, Any]:



      """simple docstring"""







      a_       		=use_tiling




 def       lowercase_  (   self) ->      str:



      """simple docstring"""







      self.enable_tiling(lowerCAmelCase_)




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      a_       		=True




 def       lowercase_  (   self) ->      Dict:



      """simple docstring"""







      a_       		=False




 @property
 # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
 def       lowercase_  (   self) ->      Dict[str, AttentionProcessor]:



      """simple docstring"""







      a_       		={}

      def fn_recursive_add_processors(lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_):
           if hasattr(lowerCAmelCase_							,  "set_processor"):
                a_       		=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       lowercase_  (   self							,  lowerCAmelCase_) ->      int:



      """simple docstring"""







      a_       		=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_							,  lowerCAmelCase_							,  lowerCAmelCase_):
           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       lowercase_  (   self) ->      int:



      """simple docstring"""







      self.set_attn_processor(AttnProcessor())




 @apply_forward_hook
 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = True) ->      AutoencoderKLOutput:



      """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:
           a_       		=[self.encoder(lowerCAmelCase_) for x_slice in x.split(1)]
           a_       		=torch.cat(lowerCAmelCase_)
      else:
           a_       		=self.encoder(lowerCAmelCase_)

      a_       		=self.quant_conv(lowerCAmelCase_)
      a_       		=DiagonalGaussianDistribution(lowerCAmelCase_)

      if not return_dict:
           return (posterior,)

      return AutoencoderKLOutput(latent_dist=lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = True) ->      Union[DecoderOutput, torch.FloatTensor]:



      """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_)

      a_       		=self.post_quant_conv(lowerCAmelCase_)
      a_       		=self.decoder(lowerCAmelCase_)

      if not return_dict:
           return (dec,)

      return DecoderOutput(sample=lowerCAmelCase_)




 @apply_forward_hook
 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = True) ->      Union[DecoderOutput, torch.FloatTensor]:



      """simple docstring"""







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

      if not return_dict:
           return (decoded,)

      return DecoderOutput(sample=lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      int:



      """simple docstring"""







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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      Union[str, Any]:



      """simple docstring"""







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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = True) ->      AutoencoderKLOutput:



      """simple docstring"""







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

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

      a_       		=torch.cat(lowerCAmelCase_							,  dim=2)
      a_       		=DiagonalGaussianDistribution(lowerCAmelCase_)

      if not return_dict:
           return (posterior,)

      return AutoencoderKLOutput(latent_dist=lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = True) ->      Union[DecoderOutput, torch.FloatTensor]:



      """simple docstring"""







      a_       		=int(self.tile_latent_min_size * (1 - self.tile_overlap_factor))
      a_       		=int(self.tile_sample_min_size * self.tile_overlap_factor)
      a_       		=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.
      a_       		=[]
      for i in range(0							,  z.shape[2]							,  lowerCAmelCase_):
           a_       		=[]
           for j in range(0							,  z.shape[3]							,  lowerCAmelCase_):
                a_       		=z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
                a_       		=self.post_quant_conv(lowerCAmelCase_)
                a_       		=self.decoder(lowerCAmelCase_)
                row.append(lowerCAmelCase_)
           rows.append(lowerCAmelCase_)
      a_       		=[]
      for i, row in enumerate(lowerCAmelCase_):
           a_       		=[]
           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:
                     a_       		=self.blend_v(rows[i - 1][j]							,  lowerCAmelCase_							,  lowerCAmelCase_)
                if j > 0:
                     a_       		=self.blend_h(row[j - 1]							,  lowerCAmelCase_							,  lowerCAmelCase_)
                result_row.append(tile[:, :, :row_limit, :row_limit])
           result_rows.append(torch.cat(lowerCAmelCase_							,  dim=3))

      a_       		=torch.cat(lowerCAmelCase_							,  dim=2)
      if not return_dict:
           return (dec,)

      return DecoderOutput(sample=lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = False							,  lowerCAmelCase_ = True							,  lowerCAmelCase_ = None							,  ) ->      Union[DecoderOutput, torch.FloatTensor]:



      """simple docstring"""







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

      if not return_dict:
           return (dec,)

      return DecoderOutput(sample=lowerCAmelCase_)







'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


lowercase							       =			{
    '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''',
    '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''',
    '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''',
    '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''',
    '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''',
    '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''',
    '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''',
    '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''',
}







class 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 __magic_name__					: int  =      "albert"




 def __init__(   self							,  lowerCAmelCase_=3_0_0_0_0							,  lowerCAmelCase_=1_2_8							,  lowerCAmelCase_=4_0_9_6							,  lowerCAmelCase_=1_2							,  lowerCAmelCase_=1							,  lowerCAmelCase_=6_4							,  lowerCAmelCase_=1_6_3_8_4							,  lowerCAmelCase_=1							,  lowerCAmelCase_="gelu_new"							,  lowerCAmelCase_=0							,  lowerCAmelCase_=0							,  lowerCAmelCase_=5_1_2							,  lowerCAmelCase_=2							,  lowerCAmelCase_=0.0_2							,  lowerCAmelCase_=1e-12							,  lowerCAmelCase_=0.1							,  lowerCAmelCase_="absolute"							,  lowerCAmelCase_=0							,  lowerCAmelCase_=2							,  lowerCAmelCase_=3							,  **lowerCAmelCase_							,  ) ->      Any:



      """simple docstring"""







      super().__init__(pad_token_id=lowerCAmelCase_							,  bos_token_id=lowerCAmelCase_							,  eos_token_id=lowerCAmelCase_							,  **lowerCAmelCase_)

      a_       		=vocab_size
      a_       		=embedding_size
      a_       		=hidden_size
      a_       		=num_hidden_layers
      a_       		=num_hidden_groups
      a_       		=num_attention_heads
      a_       		=inner_group_num
      a_       		=hidden_act
      a_       		=intermediate_size
      a_       		=hidden_dropout_prob
      a_       		=attention_probs_dropout_prob
      a_       		=max_position_embeddings
      a_       		=type_vocab_size
      a_       		=initializer_range
      a_       		=layer_norm_eps
      a_       		=classifier_dropout_prob
      a_       		=position_embedding_type







class 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 @property
 def       lowercase_  (   self) ->      Mapping[str, Mapping[int, str]]:



      """simple docstring"""







      if self.task == "multiple-choice":
           a_       		={0: "batch", 1: "choice", 2: "sequence"}
      else:
           a_       		={0: "batch", 1: "sequence"}
      return OrderedDict(
          [
              ("input_ids", dynamic_axis),
              ("attention_mask", dynamic_axis),
              ("token_type_ids", dynamic_axis),
          ])


