






"""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__							(     lowercase__					):






      '''simple docstring'''

      __UpperCamelCase      					=    4_2




class 				lowerCAmelCase__							(     lowercase__							,						lowercase__					):






      '''simple docstring'''

      @register_to_config
      def __init__(		self	:    List[Any]		,      lowercase_	:    int = 3		,      lowercase_	:    int = 3		,      lowercase_	:    Tuple[str] = ("DownEncoderBlock2D",)		,      lowercase_	:    Tuple[str] = ("UpDecoderBlock2D",)		,      lowercase_	:    Tuple[int] = (64,)		,      lowercase_	:    int = 1		,      lowercase_	:    str = "silu"		,      lowercase_	:    int = 3		,      lowercase_	:    int = 32		,      lowercase_	:    int = 256		,      lowercase_	:    int = 32		,      lowercase_	:    Optional[int] = None		,      lowercase_	:    float = 0.1_82_15		,      lowercase_	:    str = "group"		,      ):




          '''simple docstring'''


          super().__init__()

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

          SCREAMING_SNAKE_CASE_ :    str       		=						vq_embed_dim if vq_embed_dim is not None else latent_channels

          SCREAMING_SNAKE_CASE_ :    List[Any]       		=						nn.Convad(SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      1)
          SCREAMING_SNAKE_CASE_ :    Optional[Any]       		=						VectorQuantizer(SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      beta=0.25		,      remap=SCREAMING_SNAKE_CASE_		,      sane_index_shape=SCREAMING_SNAKE_CASE_)
          SCREAMING_SNAKE_CASE_ :    Tuple       		=						nn.Convad(SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      1)

          # pass init params to Decoder
          SCREAMING_SNAKE_CASE_ :    Union[str, Any]       		=						Decoder(
              in_channels=SCREAMING_SNAKE_CASE_		,      out_channels=SCREAMING_SNAKE_CASE_		,      up_block_types=SCREAMING_SNAKE_CASE_		,      block_out_channels=SCREAMING_SNAKE_CASE_		,      layers_per_block=SCREAMING_SNAKE_CASE_		,      act_fn=SCREAMING_SNAKE_CASE_		,      norm_num_groups=SCREAMING_SNAKE_CASE_		,      norm_type=SCREAMING_SNAKE_CASE_		,      )
      @apply_forward_hook
      def _SCREAMING_SNAKE_CASE					(		self	:    Optional[Any]		,      lowercase_	:    torch.FloatTensor		,      lowercase_	:    bool = True):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_ :    List[str]       		=						self.encoder(SCREAMING_SNAKE_CASE_)
          SCREAMING_SNAKE_CASE_ :    Dict       		=						self.quant_conv(SCREAMING_SNAKE_CASE_)

          if not return_dict:
              return (h,)

          return VQEncoderOutput(latents=SCREAMING_SNAKE_CASE_)
      @apply_forward_hook
      def _SCREAMING_SNAKE_CASE					(		self	:    List[str]		,      lowercase_	:    torch.FloatTensor		,      lowercase_	:    bool = False		,      lowercase_	:    bool = True):




          '''simple docstring'''


          if not force_not_quantize:
              SCREAMING_SNAKE_CASE_		,     SCREAMING_SNAKE_CASE_		,     SCREAMING_SNAKE_CASE_ :    List[Any]       		=						self.quantize(SCREAMING_SNAKE_CASE_)
          else:
              SCREAMING_SNAKE_CASE_ :    Optional[Any]       		=						h
          SCREAMING_SNAKE_CASE_ :    Any       		=						self.post_quant_conv(SCREAMING_SNAKE_CASE_)
          SCREAMING_SNAKE_CASE_ :    Dict       		=						self.decoder(SCREAMING_SNAKE_CASE_		,      quant if self.config.norm_type == '''spatial''' else None)

          if not return_dict:
              return (dec,)

          return DecoderOutput(sample=SCREAMING_SNAKE_CASE_)



      def _SCREAMING_SNAKE_CASE					(		self	:    Optional[int]		,      lowercase_	:    torch.FloatTensor		,      lowercase_	:    bool = True):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_ :    List[str]       		=						sample
          SCREAMING_SNAKE_CASE_ :    List[str]       		=						self.encode(SCREAMING_SNAKE_CASE_).latents
          SCREAMING_SNAKE_CASE_ :    Any       		=						self.decode(SCREAMING_SNAKE_CASE_).sample

          if not return_dict:
              return (dec,)

          return DecoderOutput(sample=SCREAMING_SNAKE_CASE_)




"""simple docstring"""






def __magic_name__	(				_lowerCamelCase:		Tuple,					_lowerCamelCase:		Any			)					->						Optional[Any]:

	'''simple docstring'''




	lowerCAmelCase												=  [1]
	for i in range(2,					_lowerCamelCase			):
		factorials.append(factorials[-1] * i			)
	assert 0 <= k < factorials[-1] * n, "k out of bounds"

	lowerCAmelCase												=  []
	lowerCAmelCase												=  list(range(_lowerCamelCase			)			)

	# Find permutation
	while factorials:
		lowerCAmelCase												=  factorials.pop()
		lowerCAmelCase							,       lowerCAmelCase												=  divmod(_lowerCamelCase,					_lowerCamelCase			)
		permutation.append(elements[number]			)
		elements.remove(elements[number]			)
	permutation.append(elements[0]			)

	return permutation


if __name__ == "__main__":
					import doctest

					doctest.testmod()


