



'''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 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				torch.FloatTensor







class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  @register_to_config
  def __init__(   self  ,__UpperCAmelCase = 3  ,__UpperCAmelCase = 3  ,__UpperCAmelCase = ("DownEncoderBlock2D",)  ,__UpperCAmelCase = ("UpDecoderBlock2D",)  ,__UpperCAmelCase = (64,)  ,__UpperCAmelCase = 1  ,__UpperCAmelCase = "silu"  ,__UpperCAmelCase = 3  ,__UpperCAmelCase = 32  ,__UpperCAmelCase = 256  ,__UpperCAmelCase = 32  ,__UpperCAmelCase = None  ,__UpperCAmelCase = 0.1_8_2_1_5  ,__UpperCAmelCase = "group"  ,)   ->		List[str]:
      super().__init__()

      # pass init params to Encoder
      lowerCAmelCase__			:  Any											= 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__			:  Any											= vq_embed_dim if vq_embed_dim is not None else latent_channels

      lowerCAmelCase__			:  List[Any]											= nn.Convad(__UpperCAmelCase  ,__UpperCAmelCase  ,1				)
      lowerCAmelCase__			:  Optional[Any]											= VectorQuantizer(__UpperCAmelCase  ,__UpperCAmelCase  ,beta=0.2_5  ,remap=__UpperCAmelCase  ,sane_index_shape=__UpperCAmelCase				)
      lowerCAmelCase__			:  str											= nn.Convad(__UpperCAmelCase  ,__UpperCAmelCase  ,1				)

      # pass init params to Decoder
      lowerCAmelCase__			:  Optional[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  ,__UpperCAmelCase  ,__UpperCAmelCase = True				)   ->		VQEncoderOutput:
      lowerCAmelCase__			:  Any											= self.encoder(__UpperCAmelCase				)
      lowerCAmelCase__			:  List[str]											= self.quant_conv(__UpperCAmelCase				)

      if not return_dict:
          return (h,)

      return VQEncoderOutput(latents=__UpperCAmelCase				)




  @apply_forward_hook
  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase = False  ,__UpperCAmelCase = True				)   ->		Union[DecoderOutput, torch.FloatTensor]:
      # also go through quantization layer
      if not force_not_quantize:
          lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__			:  Dict											= self.quantize(__UpperCAmelCase				)
      else:
          lowerCAmelCase__			:  str											= h
      lowerCAmelCase__			:  Dict											= self.post_quant_conv(__UpperCAmelCase				)
      lowerCAmelCase__			:  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  ,__UpperCAmelCase  ,__UpperCAmelCase = True				)   ->		Union[DecoderOutput, torch.FloatTensor]:
      lowerCAmelCase__			:  int											= sample
      lowerCAmelCase__			:  List[Any]											= self.encode(__UpperCAmelCase				).latents
      lowerCAmelCase__			:  str											= self.decode(__UpperCAmelCase				).sample

      if not return_dict:
          return (dec,)

      return DecoderOutput(sample=__UpperCAmelCase				)







'''simple docstring'''





from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_lowerCAmelCase										=     logging.get_logger(__name__)

_lowerCAmelCase										=     {
    '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''',
}






class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Tuple           =				'''convnextv2'''




  def __init__(   self  ,__UpperCAmelCase=3  ,__UpperCAmelCase=4  ,__UpperCAmelCase=4  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=1E-12  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=224  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,**__UpperCAmelCase  ,)   ->		Union[str, Any]:
      super().__init__(**__UpperCAmelCase				)

      lowerCAmelCase__			:  int											= num_channels
      lowerCAmelCase__			:  List[Any]											= patch_size
      lowerCAmelCase__			:  Union[str, Any]											= num_stages
      lowerCAmelCase__			:  Tuple											= [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes
      lowerCAmelCase__			:  str											= [3, 3, 9, 3] if depths is None else depths
      lowerCAmelCase__			:  Optional[Any]											= hidden_act
      lowerCAmelCase__			:  str											= initializer_range
      lowerCAmelCase__			:  List[str]											= layer_norm_eps
      lowerCAmelCase__			:  Dict											= drop_path_rate
      lowerCAmelCase__			:  int											= image_size
      lowerCAmelCase__			:  int											= ["""stem"""] + [F"""stage{idx}""" for idx in range(1  ,len(self.depths				) + 1				)]
      lowerCAmelCase__				,       lowerCAmelCase__			:  Tuple											= get_aligned_output_features_output_indices(
          out_features=__UpperCAmelCase  ,out_indices=__UpperCAmelCase  ,stage_names=self.stage_names				)


