

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 							A_ (		snake_case__					):
     _lowercase			:     Optional[Any]								=	4_2







class 							A_ (		snake_case__							,	snake_case__					):


     @register_to_config
     def __init__(							self			:		List[str]			,  UpperCAmelCase			:		Dict = 3			,  UpperCAmelCase			:		Dict = 3			,  UpperCAmelCase			:		List[Any] = ("DownEncoderBlock2D",)			,  UpperCAmelCase			:		Union[str, Any] = ("UpDecoderBlock2D",)			,  UpperCAmelCase			:		Optional[Any] = (6_4,)			,  UpperCAmelCase			:		Optional[Any] = 1			,  UpperCAmelCase			:		List[Any] = "silu"			,  UpperCAmelCase			:		str = 3			,  UpperCAmelCase			:		Optional[int] = 3_2			,  UpperCAmelCase			:		int = 2_5_6			,  UpperCAmelCase			:		Tuple = 3_2			,  UpperCAmelCase			:		Optional[int] = None			,  UpperCAmelCase			:		Any = 0.18215			,  UpperCAmelCase			:		str = "group"			,  )							->	Any:
      super().__init__()

      # pass init params to Encoder
      __lowerCAmelCase:   List[str]   	=      Encoder(
          in_channels=UpperCAmelCase			,  out_channels=UpperCAmelCase			,  down_block_types=UpperCAmelCase			,  block_out_channels=UpperCAmelCase			,  layers_per_block=UpperCAmelCase			,  act_fn=UpperCAmelCase			,  norm_num_groups=UpperCAmelCase			,  double_z=UpperCAmelCase			,  )

      __lowerCAmelCase:   Dict   	=      vq_embed_dim if vq_embed_dim is not None else latent_channels

      __lowerCAmelCase:   str   	=      nn.Convad(UpperCAmelCase			,  UpperCAmelCase			,  1					)
      __lowerCAmelCase:   Dict   	=      VectorQuantizer(UpperCAmelCase			,  UpperCAmelCase			,  beta=0.25			,  remap=UpperCAmelCase			,  sane_index_shape=UpperCAmelCase					)
      __lowerCAmelCase:   int   	=      nn.Convad(UpperCAmelCase			,  UpperCAmelCase			,  1					)

      # pass init params to Decoder
      __lowerCAmelCase:   List[Any]   	=      Decoder(
          in_channels=UpperCAmelCase			,  out_channels=UpperCAmelCase			,  up_block_types=UpperCAmelCase			,  block_out_channels=UpperCAmelCase			,  layers_per_block=UpperCAmelCase			,  act_fn=UpperCAmelCase			,  norm_num_groups=UpperCAmelCase			,  norm_type=UpperCAmelCase			,  )


     @apply_forward_hook
     def   UpperCAmelCase  (							self			:		List[Any]			,  UpperCAmelCase			:		Any			,  UpperCAmelCase			:		Optional[int] = True					)							->	VQEncoderOutput:
      __lowerCAmelCase:   Optional[Any]   	=      self.encoder(UpperCAmelCase					)
      __lowerCAmelCase:   Optional[Any]   	=      self.quant_conv(UpperCAmelCase					)

      if not return_dict:
       return (h,)

      return VQEncoderOutput(latents=UpperCAmelCase					)


     @apply_forward_hook
     def   UpperCAmelCase  (							self			:		Optional[int]			,  UpperCAmelCase			:		Any			,  UpperCAmelCase			:		Union[str, Any] = False			,  UpperCAmelCase			:		Union[str, Any] = True					)							->	Union[DecoderOutput, torch.FloatTensor]:
      # also go through quantization layer
      if not force_not_quantize:
       __lowerCAmelCase	,							__lowerCAmelCase	,							__lowerCAmelCase:   List[Any]   	=      self.quantize(UpperCAmelCase					)
      else:
       __lowerCAmelCase:   str   	=      h
      __lowerCAmelCase:   Any   	=      self.post_quant_conv(UpperCAmelCase					)
      __lowerCAmelCase:   List[str]   	=      self.decoder(UpperCAmelCase			,  quant if self.config.norm_type == 'spatial' else None					)

      if not return_dict:
       return (dec,)

      return DecoderOutput(sample=UpperCAmelCase					)


     def   UpperCAmelCase  (							self			:		Any			,  UpperCAmelCase			:		List[Any]			,  UpperCAmelCase			:		Dict = True					)							->	Union[DecoderOutput, torch.FloatTensor]:
      __lowerCAmelCase:   Optional[int]   	=      sample
      __lowerCAmelCase:   Optional[Any]   	=      self.encode(UpperCAmelCase					).latents
      __lowerCAmelCase:   Dict   	=      self.decode(UpperCAmelCase					).sample

      if not return_dict:
       return (dec,)

      return DecoderOutput(sample=UpperCAmelCase					)









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCAmelCase__   	=							{
    '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''],
    '''tokenization_xlm''': ['''XLMTokenizer'''],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__   	=							[
      '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''XLMForMultipleChoice''',
      '''XLMForQuestionAnswering''',
      '''XLMForQuestionAnsweringSimple''',
      '''XLMForSequenceClassification''',
      '''XLMForTokenClassification''',
      '''XLMModel''',
      '''XLMPreTrainedModel''',
      '''XLMWithLMHeadModel''',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__   	=							[
      '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TFXLMForMultipleChoice''',
      '''TFXLMForQuestionAnsweringSimple''',
      '''TFXLMForSequenceClassification''',
      '''TFXLMForTokenClassification''',
      '''TFXLMMainLayer''',
      '''TFXLMModel''',
      '''TFXLMPreTrainedModel''',
      '''TFXLMWithLMHeadModel''',
  ]


if TYPE_CHECKING:
  from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
  from .tokenization_xlm import XLMTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlm import (
        XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLMForMultipleChoice,
        XLMForQuestionAnswering,
        XLMForQuestionAnsweringSimple,
        XLMForSequenceClassification,
        XLMForTokenClassification,
        XLMModel,
        XLMPreTrainedModel,
        XLMWithLMHeadModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlm import (
        TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLMForMultipleChoice,
        TFXLMForQuestionAnsweringSimple,
        TFXLMForSequenceClassification,
        TFXLMForTokenClassification,
        TFXLMMainLayer,
        TFXLMModel,
        TFXLMPreTrainedModel,
        TFXLMWithLMHeadModel,
    )

else:
  import sys

  UpperCAmelCase__   	=							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

