



"""simple docstring"""




from dataclasses import dataclass
from typing import 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 .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer






@dataclass
class      UpperCAmelCase_  (    _UpperCamelCase						):
      __SCREAMING_SNAKE_CASE						:       torch.FloatTensor







class      UpperCAmelCase_  (    _UpperCamelCase	,      _UpperCamelCase						):



      @register_to_config
      def __init__(					self					:     Any					,     A					:     int = 3					,     A					:     int = 3					,     A					:     Tuple[str] = ("DownEncoderBlock2D",)					,     A					:     Tuple[str] = ("UpDecoderBlock2D",)					,     A					:     Tuple[int] = (6_4,)					,     A					:     int = 1					,     A					:     str = "silu"					,     A					:     int = 3					,     A					:     int = 3_2					,     A					:     int = 2_5_6					,     A					:     int = 3_2					,     A					:     Optional[int] = None					,     A					:     float = 0.18_215					,     A					:     str = "group"					,     ):
           super().__init__()

           # pass init params to Encoder
           _UpperCAmelCase      :     int        =   Encoder(
               in_channels=A					,     out_channels=A					,     down_block_types=A					,     block_out_channels=A					,     layers_per_block=A					,     act_fn=A					,     norm_num_groups=A					,     double_z=A					,     )

           _UpperCAmelCase      :     Optional[int]        =   vq_embed_dim if vq_embed_dim is not None else latent_channels

           _UpperCAmelCase      :     Union[str, Any]        =   nn.Convad(A					,     A					,     1						)
           _UpperCAmelCase      :     int        =   VectorQuantizer(A					,     A					,     beta=0.25					,     remap=A					,     sane_index_shape=A						)
           _UpperCAmelCase      :     str        =   nn.Convad(A					,     A					,     1						)

           # pass init params to Decoder
           _UpperCAmelCase      :     Any        =   Decoder(
               in_channels=A					,     out_channels=A					,     up_block_types=A					,     block_out_channels=A					,     layers_per_block=A					,     act_fn=A					,     norm_num_groups=A					,     norm_type=A					,     )



      @apply_forward_hook
      def 						snake_case_ (					self					:     List[str]					,     A					:     torch.FloatTensor					,     A					:     bool = True						):
           _UpperCAmelCase      :     List[Any]        =   self.encoder(A						)
           _UpperCAmelCase      :     Optional[Any]        =   self.quant_conv(A						)

           if not return_dict:
                return (h,)

           return VQEncoderOutput(latents=A						)



      @apply_forward_hook
      def 						snake_case_ (					self					:     Any					,     A					:     torch.FloatTensor					,     A					:     bool = False					,     A					:     bool = True						):
           # also go through quantization layer
           if not force_not_quantize:
                _UpperCAmelCase    ,	_UpperCAmelCase    ,	_UpperCAmelCase      :     Any        =   self.quantize(A						)
           else:
                _UpperCAmelCase      :     Union[str, Any]        =   h
           _UpperCAmelCase      :     Tuple        =   self.post_quant_conv(A						)
           _UpperCAmelCase      :     str        =   self.decoder(A					,     quant if self.config.norm_type == "spatial" else None						)

           if not return_dict:
                return (dec,)

           return DecoderOutput(sample=A						)



      def 						snake_case_ (					self					:     List[str]					,     A					:     torch.FloatTensor					,     A					:     bool = True						):
           _UpperCAmelCase      :     Optional[int]        =   sample
           _UpperCAmelCase      :     str        =   self.encode(A						).latents
           _UpperCAmelCase      :     Optional[Any]        =   self.decode(A						).sample

           if not return_dict:
                return (dec,)

           return DecoderOutput(sample=A						)





"""simple docstring"""




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_electra import ElectraTokenizer


_lowerCAmelCase    : List[str]							=							{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

_lowerCAmelCase    : int							=							{
    "vocab_file": {
        "google/electra-small-generator": (
            "https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt"
        ),
        "google/electra-base-generator": "https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt",
        "google/electra-large-generator": (
            "https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt"
        ),
        "google/electra-small-discriminator": (
            "https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt"
        ),
        "google/electra-base-discriminator": (
            "https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt"
        ),
        "google/electra-large-discriminator": (
            "https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "google/electra-small-generator": (
            "https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-base-generator": (
            "https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-large-generator": (
            "https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-small-discriminator": (
            "https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json"
        ),
        "google/electra-base-discriminator": (
            "https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json"
        ),
        "google/electra-large-discriminator": (
            "https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json"
        ),
    },
}

_lowerCAmelCase    : List[Any]							=							{
    "google/electra-small-generator": 5_12,
    "google/electra-base-generator": 5_12,
    "google/electra-large-generator": 5_12,
    "google/electra-small-discriminator": 5_12,
    "google/electra-base-discriminator": 5_12,
    "google/electra-large-discriminator": 5_12,
}

_lowerCAmelCase    : Optional[Any]							=							{
    "google/electra-small-generator": {"do_lower_case": True},
    "google/electra-base-generator": {"do_lower_case": True},
    "google/electra-large-generator": {"do_lower_case": True},
    "google/electra-small-discriminator": {"do_lower_case": True},
    "google/electra-base-discriminator": {"do_lower_case": True},
    "google/electra-large-discriminator": {"do_lower_case": True},
}






class      UpperCAmelCase_  (    _UpperCamelCase						):
      __SCREAMING_SNAKE_CASE						:       Optional[Any]								=			VOCAB_FILES_NAMES
      __SCREAMING_SNAKE_CASE						:       Any								=			PRETRAINED_VOCAB_FILES_MAP
      __SCREAMING_SNAKE_CASE						:       List[str]								=			PRETRAINED_INIT_CONFIGURATION
      __SCREAMING_SNAKE_CASE						:       List[str]								=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __SCREAMING_SNAKE_CASE						:       Tuple								=			ElectraTokenizer



      def __init__(					self					:     Dict					,     A					:     Dict=None					,     A					:     Optional[int]=None					,     A					:     Dict=True					,     A					:     Optional[Any]="[UNK]"					,     A					:     Any="[SEP]"					,     A					:     str="[PAD]"					,     A					:     Tuple="[CLS]"					,     A					:     Optional[Any]="[MASK]"					,     A					:     Any=True					,     A					:     Tuple=None					,     **A					:     Any					,     ):
           super().__init__(
               A					,     tokenizer_file=A					,     do_lower_case=A					,     unk_token=A					,     sep_token=A					,     pad_token=A					,     cls_token=A					,     mask_token=A					,     tokenize_chinese_chars=A					,     strip_accents=A					,     **A					,     )

           _UpperCAmelCase      :     List[str]        =   json.loads(self.backend_tokenizer.normalizer.__getstate__()						)
           if (
               normalizer_state.get("lowercase"					,     A						) != do_lower_case
               or normalizer_state.get("strip_accents"					,     A						) != strip_accents
               or normalizer_state.get("handle_chinese_chars"					,     A						) != tokenize_chinese_chars
           ):
                _UpperCAmelCase      :     Union[str, Any]        =   getattr(A					,     normalizer_state.pop("type"						)						)
                _UpperCAmelCase      :     Dict        =   do_lower_case
                _UpperCAmelCase      :     Optional[int]        =   strip_accents
                _UpperCAmelCase      :     Any        =   tokenize_chinese_chars
                _UpperCAmelCase      :     Optional[Any]        =   normalizer_class(**A						)

           _UpperCAmelCase      :     int        =   do_lower_case



      def 						snake_case_ (					self					:     Tuple					,     A					:     str					,     A					:     int=None						):
           _UpperCAmelCase      :     Dict        =   [self.cls_token_id] + token_ids_a + [self.sep_token_id]

           if token_ids_a:
                output += token_ids_a + [self.sep_token_id]

           return output



      def 						snake_case_ (					self					:     Any					,     A					:     List[int]					,     A					:     Optional[List[int]] = None						):
           _UpperCAmelCase      :     Any        =   [self.sep_token_id]
           _UpperCAmelCase      :     Dict        =   [self.cls_token_id]
           if token_ids_a is None:
                return len(cls + token_ids_a + sep						) * [0]
           return len(cls + token_ids_a + sep						) * [0] + len(token_ids_a + sep						) * [1]



      def 						snake_case_ (					self					:     Any					,     A					:     str					,     A					:     Optional[str] = None						):
           _UpperCAmelCase      :     List[Any]        =   self._tokenizer.model.save(A					,     name=A						)
           return tuple(A						)
