

import inspect
import tempfile
from collections import OrderedDict, UserDict
from collections.abc import MutableMapping
from contextlib import ExitStack, contextmanager
from dataclasses import fields
from enum import Enum
from typing import Any, ContextManager, List, Tuple

import numpy as np

from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy


if is_flax_available():
       import jax.numpy as jnp



class       __snake_case  (    UpperCamelCase_       ):

  def __get__(					self						,		_A						,		_A=None):






      if obj is None:
          return self
      if self.fget is None:
          raise AttributeError('unreadable attribute')
      SCREAMING_SNAKE_CASE_									=						'__cached_' + self.fget.__name__
      SCREAMING_SNAKE_CASE_									=						getattr(__a						,		__a						,		__a)
      if cached is None:
          SCREAMING_SNAKE_CASE_									=						self.fget(__a)
          setattr(__a						,		__a						,		__a)
      return cached






def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Optional[int]       ):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_									=						val.lower()
    if val in {"y", "yes", "t", "true", "on", "1"}:
        return 1
    if val in {"n", "no", "f", "false", "off", "0"}:
        return 0
    raise ValueError(f"""invalid truth value {val!r}"""       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		str       ):





    """simple docstring"""


    if is_torch_fx_proxy(_SCREAMING_SNAKE_CASE       ):
        return True
    if is_torch_available():
        import torch

        if isinstance(_SCREAMING_SNAKE_CASE ,   torch.Tensor       ):
            return True
    if is_tf_available():
        import tensorflow as tf

        if isinstance(_SCREAMING_SNAKE_CASE ,   tf.Tensor       ):
            return True

    if is_flax_available():
        import jax.numpy as jnp
        from jax.core import Tracer

        if isinstance(_SCREAMING_SNAKE_CASE ,   (jnp.ndarray, Tracer)       ):
            return True

    return isinstance(_SCREAMING_SNAKE_CASE ,   np.ndarray       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Union[str, Any]       ):





    """simple docstring"""


    return isinstance(_SCREAMING_SNAKE_CASE ,   np.ndarray       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		str       ):





    """simple docstring"""


    return _is_numpy(_SCREAMING_SNAKE_CASE       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Optional[Any]       ):





    """simple docstring"""


    import torch

    return isinstance(_SCREAMING_SNAKE_CASE ,   torch.Tensor       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Dict       ):





    """simple docstring"""


    return False if not is_torch_available() else _is_torch(_SCREAMING_SNAKE_CASE       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Any       ):





    """simple docstring"""


    import torch

    return isinstance(_SCREAMING_SNAKE_CASE ,   torch.device       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Dict       ):





    """simple docstring"""


    return False if not is_torch_available() else _is_torch_device(_SCREAMING_SNAKE_CASE       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Optional[Any]       ):





    """simple docstring"""


    import torch

    if isinstance(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       ):
        if hasattr(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       ):
            SCREAMING_SNAKE_CASE_									=						getattr(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )
        else:
            return False
    return isinstance(_SCREAMING_SNAKE_CASE ,   torch.dtype       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Optional[Any]       ):





    """simple docstring"""


    return False if not is_torch_available() else _is_torch_dtype(_SCREAMING_SNAKE_CASE       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Dict       ):





    """simple docstring"""


    import tensorflow as tf

    return isinstance(_SCREAMING_SNAKE_CASE ,   tf.Tensor       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Dict       ):





    """simple docstring"""


    return False if not is_tf_available() else _is_tensorflow(_SCREAMING_SNAKE_CASE       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Tuple       ):





    """simple docstring"""


    import tensorflow as tf

    # the `is_symbolic_tensor` predicate is only available starting with TF 2.14
    if hasattr(_SCREAMING_SNAKE_CASE ,   'is_symbolic_tensor'       ):
        return tf.is_symbolic_tensor(_SCREAMING_SNAKE_CASE       )
    return type(_SCREAMING_SNAKE_CASE       ) == tf.Tensor


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		List[str]       ):





    """simple docstring"""


    return False if not is_tf_available() else _is_tf_symbolic_tensor(_SCREAMING_SNAKE_CASE       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		List[str]       ):





    """simple docstring"""


    import jax.numpy as jnp  # noqa: F811

    return isinstance(_SCREAMING_SNAKE_CASE ,   jnp.ndarray       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Optional[Any]       ):





    """simple docstring"""


    return False if not is_flax_available() else _is_jax(_SCREAMING_SNAKE_CASE       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		List[Any]       ):





    """simple docstring"""


    if isinstance(_SCREAMING_SNAKE_CASE ,   (dict, UserDict)       ):
        return {k: to_py_obj(_SCREAMING_SNAKE_CASE       ) for k, v in obj.items()}
    elif isinstance(_SCREAMING_SNAKE_CASE ,   (list, tuple)       ):
        return [to_py_obj(_SCREAMING_SNAKE_CASE       ) for o in obj]
    elif is_tf_tensor(_SCREAMING_SNAKE_CASE       ):
        return obj.numpy().tolist()
    elif is_torch_tensor(_SCREAMING_SNAKE_CASE       ):
        return obj.detach().cpu().tolist()
    elif is_jax_tensor(_SCREAMING_SNAKE_CASE       ):
        return np.asarray(_SCREAMING_SNAKE_CASE       ).tolist()
    elif isinstance(_SCREAMING_SNAKE_CASE ,   (np.ndarray, np.number)       ):  # tolist also works on 0d np arrays
        return obj.tolist()
    else:
        return obj


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		List[str]       ):





    """simple docstring"""


    if isinstance(_SCREAMING_SNAKE_CASE ,   (dict, UserDict)       ):
        return {k: to_numpy(_SCREAMING_SNAKE_CASE       ) for k, v in obj.items()}
    elif isinstance(_SCREAMING_SNAKE_CASE ,   (list, tuple)       ):
        return np.array(_SCREAMING_SNAKE_CASE       )
    elif is_tf_tensor(_SCREAMING_SNAKE_CASE       ):
        return obj.numpy()
    elif is_torch_tensor(_SCREAMING_SNAKE_CASE       ):
        return obj.detach().cpu().numpy()
    elif is_jax_tensor(_SCREAMING_SNAKE_CASE       ):
        return np.asarray(_SCREAMING_SNAKE_CASE       )
    else:
        return obj



class       __snake_case  (    UpperCamelCase_       ):

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						fields(self)

      # Safety and consistency checks
      if not len(__a):
          raise ValueError(f"""{self.__class__.__name__} has no fields.""")
      if not all(field.default is None for field in class_fields[1:]):
          raise ValueError(f"""{self.__class__.__name__} should not have more than one required field.""")

      SCREAMING_SNAKE_CASE_									=						getattr(self						,		class_fields[0].name)
      SCREAMING_SNAKE_CASE_									=						all(getattr(self						,		field.name) is None for field in class_fields[1:])

      if other_fields_are_none and not is_tensor(__a):
          if isinstance(__a						,		__a):
              SCREAMING_SNAKE_CASE_									=						first_field.items()
              SCREAMING_SNAKE_CASE_									=						True
          else:
              try:
                  SCREAMING_SNAKE_CASE_									=						iter(__a)
                  SCREAMING_SNAKE_CASE_									=						True
              except TypeError:
                  SCREAMING_SNAKE_CASE_									=						False

              # if we provided an iterator as first field and the iterator is a (key, value) iterator
              # set the associated fields
          if first_field_iterator:
              for idx, element in enumerate(__a):
                  if (
                      not isinstance(__a						,		(list, tuple))
                      or not len(__a) == 2
                      or not isinstance(element[0]						,		__a)
                  ):
                      if idx == 0:
                          # If we do not have an iterator of key/values, set it as attribute
                          SCREAMING_SNAKE_CASE_									=						first_field
                      else:
                          # If we have a mixed iterator, raise an error
                          raise ValueError(
                              f"""Cannot set key/value for {element}. It needs to be a tuple (key, value).""")
                      break
                  setattr(self						,		element[0]						,		element[1])
                  if element[1] is not None:
                      SCREAMING_SNAKE_CASE_									=						element[1]
          elif first_field is not None:
              SCREAMING_SNAKE_CASE_									=						first_field
      else:
          for field in class_fields:
              SCREAMING_SNAKE_CASE_									=						getattr(self						,		field.name)
              if v is not None:
                  SCREAMING_SNAKE_CASE_									=						v

  def __delitem__(					self						,		*_A						,		**_A):
      raise Exception(f"""You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.""")

  def 						lowerCAmelCase__     (					self						,		*_A						,		**_A):
      raise Exception(f"""You cannot use ``setdefault`` on a {self.__class__.__name__} instance.""")

  def 						lowerCAmelCase__     (					self						,		*_A						,		**_A):
      raise Exception(f"""You cannot use ``pop`` on a {self.__class__.__name__} instance.""")

  def 						lowerCAmelCase__     (					self						,		*_A						,		**_A):
      raise Exception(f"""You cannot use ``update`` on a {self.__class__.__name__} instance.""")

  def __getitem__(					self						,		_A):






      if isinstance(__a						,		__a):
          SCREAMING_SNAKE_CASE_									=						dict(self.items())
          return inner_dict[k]
      else:
          return self.to_tuple()[k]

  def __setattr__(					self						,		_A						,		_A):






      if name in self.keys() and value is not None:
          # Don't call self.__setitem__ to avoid recursion errors
          super().__setitem__(__a						,		__a)
      super().__setattr__(__a						,		__a)

  def __setitem__(					self						,		_A						,		_A):
      super().__setitem__(__a						,		__a)
      # Don't call self.__setattr__ to avoid recursion errors
      super().__setattr__(__a						,		__a)


  def 						lowerCAmelCase__     (					self):
      return tuple(self[k] for k in self.keys())






class       __snake_case  (    UpperCamelCase_     , UpperCamelCase_       ):

  @classmethod
  def 						lowerCAmelCase__     (					cls						,		_A):
      raise ValueError(
          f"""{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys())}""")






class       __snake_case  (    UpperCamelCase_       ):
  __lowerCAmelCase     :	Tuple						 =  'longest'
  __lowerCAmelCase     :	Any						 =  'max_length'
  __lowerCAmelCase     :	Optional[int]						 =  'do_not_pad'






class       __snake_case  (    UpperCamelCase_       ):
  __lowerCAmelCase     :	int						 =  'pt'
  __lowerCAmelCase     :	Any						 =  'tf'
  __lowerCAmelCase     :	int						 =  'np'
  __lowerCAmelCase     :	List[Any]						 =  'jax'






class       __snake_case  :

  def __init__(					self						,		_A):
      SCREAMING_SNAKE_CASE_									=						context_managers
      SCREAMING_SNAKE_CASE_									=						ExitStack()

  def __enter__(					self):






      for context_manager in self.context_managers:
          self.stack.enter_context(__a)


  def __exit__(					self						,		*_A						,		**_A):
      self.stack.__exit__(*__a						,		**__a)






def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		List[Any]       ):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_									=						infer_framework(_SCREAMING_SNAKE_CASE       )
    if framework == "tf":
        SCREAMING_SNAKE_CASE_									=						inspect.signature(model_class.call       )  # TensorFlow models
    elif framework == "pt":
        SCREAMING_SNAKE_CASE_									=						inspect.signature(model_class.forward       )  # PyTorch models
    else:
        SCREAMING_SNAKE_CASE_									=						inspect.signature(model_class.__call__       )  # Flax models

    for p in signature.parameters:
        if p == "return_loss" and signature.parameters[p].default is True:
            return True

    return False


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		List[Any]       ):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_									=						model_class.__name__
    SCREAMING_SNAKE_CASE_									=						infer_framework(_SCREAMING_SNAKE_CASE       )
    if framework == "tf":
        SCREAMING_SNAKE_CASE_									=						inspect.signature(model_class.call       )  # TensorFlow models
    elif framework == "pt":
        SCREAMING_SNAKE_CASE_									=						inspect.signature(model_class.forward       )  # PyTorch models
    else:
        SCREAMING_SNAKE_CASE_									=						inspect.signature(model_class.__call__       )  # Flax models

    if "QuestionAnswering" in model_name:
        return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")]
    else:
        return [p for p in signature.parameters if "label" in p]


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		MutableMapping ,   _SCREAMING_SNAKE_CASE					:		str = "" ,   _SCREAMING_SNAKE_CASE					:		str = "."       ):





    """simple docstring"""


    def _flatten_dict(_SCREAMING_SNAKE_CASE					:		int ,   _SCREAMING_SNAKE_CASE					:		Union[str, Any]="" ,   _SCREAMING_SNAKE_CASE					:		List[str]="."       ):
        for k, v in d.items():
            SCREAMING_SNAKE_CASE_									=						str(_SCREAMING_SNAKE_CASE       ) + delimiter + str(_SCREAMING_SNAKE_CASE       ) if parent_key else k
            if v and isinstance(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       ):
                yield from flatten_dict(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE ,   delimiter=_SCREAMING_SNAKE_CASE       ).items()
            else:
                yield key, v

    return dict(_flatten_dict(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )       )


@contextmanager
def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Optional[int] ,   _SCREAMING_SNAKE_CASE					:		bool = False       ):





    """simple docstring"""


    if use_temp_dir:
        with tempfile.TemporaryDirectory() as tmp_dir:
            yield tmp_dir
    else:
        yield working_dir


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Dict ,   _SCREAMING_SNAKE_CASE					:		Optional[int]=None       ):





    """simple docstring"""


    if is_numpy_array(_SCREAMING_SNAKE_CASE       ):
        return np.transpose(_SCREAMING_SNAKE_CASE ,   axes=_SCREAMING_SNAKE_CASE       )
    elif is_torch_tensor(_SCREAMING_SNAKE_CASE       ):
        return array.T if axes is None else array.permute(*_SCREAMING_SNAKE_CASE       )
    elif is_tf_tensor(_SCREAMING_SNAKE_CASE       ):
        import tensorflow as tf

        return tf.transpose(_SCREAMING_SNAKE_CASE ,   perm=_SCREAMING_SNAKE_CASE       )
    elif is_jax_tensor(_SCREAMING_SNAKE_CASE       ):
        return jnp.transpose(_SCREAMING_SNAKE_CASE ,   axes=_SCREAMING_SNAKE_CASE       )
    else:
        raise ValueError(f"""Type not supported for transpose: {type(_SCREAMING_SNAKE_CASE       )}."""       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		str ,   _SCREAMING_SNAKE_CASE					:		List[Any]       ):





    """simple docstring"""


    if is_numpy_array(_SCREAMING_SNAKE_CASE       ):
        return np.reshape(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )
    elif is_torch_tensor(_SCREAMING_SNAKE_CASE       ):
        return array.reshape(*_SCREAMING_SNAKE_CASE       )
    elif is_tf_tensor(_SCREAMING_SNAKE_CASE       ):
        import tensorflow as tf

        return tf.reshape(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )
    elif is_jax_tensor(_SCREAMING_SNAKE_CASE       ):
        return jnp.reshape(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )
    else:
        raise ValueError(f"""Type not supported for reshape: {type(_SCREAMING_SNAKE_CASE       )}."""       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Any ,   _SCREAMING_SNAKE_CASE					:		List[Any]=None       ):





    """simple docstring"""


    if is_numpy_array(_SCREAMING_SNAKE_CASE       ):
        return np.squeeze(_SCREAMING_SNAKE_CASE ,   axis=_SCREAMING_SNAKE_CASE       )
    elif is_torch_tensor(_SCREAMING_SNAKE_CASE       ):
        return array.squeeze() if axis is None else array.squeeze(dim=_SCREAMING_SNAKE_CASE       )
    elif is_tf_tensor(_SCREAMING_SNAKE_CASE       ):
        import tensorflow as tf

        return tf.squeeze(_SCREAMING_SNAKE_CASE ,   axis=_SCREAMING_SNAKE_CASE       )
    elif is_jax_tensor(_SCREAMING_SNAKE_CASE       ):
        return jnp.squeeze(_SCREAMING_SNAKE_CASE ,   axis=_SCREAMING_SNAKE_CASE       )
    else:
        raise ValueError(f"""Type not supported for squeeze: {type(_SCREAMING_SNAKE_CASE       )}."""       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Dict ,   _SCREAMING_SNAKE_CASE					:		Optional[int]       ):





    """simple docstring"""


    if is_numpy_array(_SCREAMING_SNAKE_CASE       ):
        return np.expand_dims(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )
    elif is_torch_tensor(_SCREAMING_SNAKE_CASE       ):
        return array.unsqueeze(dim=_SCREAMING_SNAKE_CASE       )
    elif is_tf_tensor(_SCREAMING_SNAKE_CASE       ):
        import tensorflow as tf

        return tf.expand_dims(_SCREAMING_SNAKE_CASE ,   axis=_SCREAMING_SNAKE_CASE       )
    elif is_jax_tensor(_SCREAMING_SNAKE_CASE       ):
        return jnp.expand_dims(_SCREAMING_SNAKE_CASE ,   axis=_SCREAMING_SNAKE_CASE       )
    else:
        raise ValueError(f"""Type not supported for expand_dims: {type(_SCREAMING_SNAKE_CASE       )}."""       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		List[Any]       ):





    """simple docstring"""


    if is_numpy_array(_SCREAMING_SNAKE_CASE       ):
        return np.size(_SCREAMING_SNAKE_CASE       )
    elif is_torch_tensor(_SCREAMING_SNAKE_CASE       ):
        return array.numel()
    elif is_tf_tensor(_SCREAMING_SNAKE_CASE       ):
        import tensorflow as tf

        return tf.size(_SCREAMING_SNAKE_CASE       )
    elif is_jax_tensor(_SCREAMING_SNAKE_CASE       ):
        return array.size
    else:
        raise ValueError(f"""Type not supported for expand_dims: {type(_SCREAMING_SNAKE_CASE       )}."""       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		int ,   _SCREAMING_SNAKE_CASE					:		Dict       ):





    """simple docstring"""


    for key, value in auto_map.items():
        if isinstance(_SCREAMING_SNAKE_CASE ,   (tuple, list)       ):
            SCREAMING_SNAKE_CASE_									=						[f"""{repo_id}--{v}""" if (v is not None and '--' not in v) else v for v in value]
        elif value is not None and "--" not in value:
            SCREAMING_SNAKE_CASE_									=						f"""{repo_id}--{value}"""

    return auto_map





def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Optional[Any]       ):





    """simple docstring"""


    for base_class in inspect.getmro(_SCREAMING_SNAKE_CASE       ):
        SCREAMING_SNAKE_CASE_									=						base_class.__module__
        SCREAMING_SNAKE_CASE_									=						base_class.__name__
        if module.startswith('tensorflow'       ) or module.startswith('keras'       ) or name == "TFPreTrainedModel":
            return "tf"
        elif module.startswith('torch'       ) or name == "PreTrainedModel":
            return "pt"
        elif module.startswith('flax'       ) or module.startswith('jax'       ) or name == "FlaxPreTrainedModel":
            return "flax"
    else:
        raise TypeError(f"""Could not infer framework from class {model_class}."""       )








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       __snake_case  (    lowerCAmelCase__       ):
  __lowerCAmelCase     :	torch.FloatTensor






class       __snake_case  (    lowerCAmelCase__     , lowerCAmelCase__       ):

  @register_to_config
  def __init__(					self						,		_A = 3						,		_A = 3						,		_A = ("DownEncoderBlock2D",)						,		_A = ("UpDecoderBlock2D",)						,		_A = (64,)						,		_A = 1						,		_A = "silu"						,		_A = 3						,		_A = 32						,		_A = 256						,		_A = 32						,		_A = None						,		_A = 0.1_8_2_1_5						,		_A = "group"						,		):
      super().__init__()

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

      SCREAMING_SNAKE_CASE_									=						vq_embed_dim if vq_embed_dim is not None else latent_channels

      SCREAMING_SNAKE_CASE_									=						nn.Convad(_A						,		_A						,		1)
      SCREAMING_SNAKE_CASE_									=						VectorQuantizer(_A						,		_A						,		beta=0.2_5						,		remap=_A						,		sane_index_shape=_A)
      SCREAMING_SNAKE_CASE_									=						nn.Convad(_A						,		_A						,		1)

      # pass init params to Decoder
      SCREAMING_SNAKE_CASE_									=						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 						lowerCAmelCase__     (					self						,		_A						,		_A = True):
      SCREAMING_SNAKE_CASE_									=						self.encoder(_A)
      SCREAMING_SNAKE_CASE_									=						self.quant_conv(_A)

      if not return_dict:
          return (h,)

      return VQEncoderOutput(latents=_A)

  @apply_forward_hook
  def 						lowerCAmelCase__     (					self						,		_A						,		_A = False						,		_A = True):
      # also go through quantization layer
      if not force_not_quantize:
          SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_									=						self.quantize(_A)
      else:
          SCREAMING_SNAKE_CASE_									=						h
      SCREAMING_SNAKE_CASE_									=						self.post_quant_conv(_A)
      SCREAMING_SNAKE_CASE_									=						self.decoder(_A						,		quant if self.config.norm_type == 'spatial' else None)

      if not return_dict:
          return (dec,)

      return DecoderOutput(sample=_A)


  def 						lowerCAmelCase__     (					self						,		_A						,		_A = True):
      SCREAMING_SNAKE_CASE_									=						sample
      SCREAMING_SNAKE_CASE_									=						self.encode(_A).latents
      SCREAMING_SNAKE_CASE_									=						self.decode(_A).sample

      if not return_dict:
          return (dec,)

      return DecoderOutput(sample=_A)


