

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



    """simple docstring"""



    UpperCAmelCase   =					4_2



class 					a						(       lowercase__,	lowercase__				):



    """simple docstring"""



    @register_to_config
    def __init__(     self: Union[str, Any]    ,						UpperCamelCase: int = 3    ,						UpperCamelCase: int = 3    ,						UpperCamelCase: Tuple[str] = ("DownEncoderBlock2D",)    ,						UpperCamelCase: Tuple[str] = ("UpDecoderBlock2D",)    ,						UpperCamelCase: Tuple[int] = (64,)    ,						UpperCamelCase: int = 1    ,						UpperCamelCase: str = "silu"    ,						UpperCamelCase: int = 3    ,						UpperCamelCase: int = 32    ,						UpperCamelCase: int = 2_56    ,						UpperCamelCase: int = 32    ,						UpperCamelCase: Optional[int] = None    ,						UpperCamelCase: float = 0.18_215    ,						UpperCamelCase: str = "group"    ,						):






         """simple docstring"""


         super().__init__()

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

         A__										=  vq_embed_dim if vq_embed_dim is not None else latent_channels

         A__										=  nn.Convad(UpperCAmelCase__    ,						UpperCAmelCase__    ,						1       )
         A__										=  VectorQuantizer(UpperCAmelCase__    ,						UpperCAmelCase__    ,						beta=0.25    ,						remap=UpperCAmelCase__    ,						sane_index_shape=UpperCAmelCase__       )
         A__										=  nn.Convad(UpperCAmelCase__    ,						UpperCAmelCase__    ,						1       )

         # pass init params to Decoder
         A__										=  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: torch.FloatTensor    ,						UpperCamelCase: bool = True       ):






         """simple docstring"""


         A__										=  self.encoder(UpperCAmelCase__       )
         A__										=  self.quant_conv(UpperCAmelCase__       )

         if not return_dict:
              return (h,)

         return VQEncoderOutput(latents=UpperCAmelCase__       )

    @apply_forward_hook
    def 				UpperCamelCase	(     self: Tuple    ,						UpperCamelCase: torch.FloatTensor    ,						UpperCamelCase: bool = False    ,						UpperCamelCase: bool = True       ):






         """simple docstring"""


         if not force_not_quantize:
              A__										=  self.quantize(UpperCAmelCase__       )
         else:
              A__										=  h
         A__										=  self.post_quant_conv(UpperCAmelCase__       )
         A__										=  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: Dict    ,						UpperCamelCase: torch.FloatTensor    ,						UpperCamelCase: bool = True       ):






         """simple docstring"""


         A__										=  sample
         A__										=  self.encode(UpperCAmelCase__       ).latents
         A__										=  self.decode(UpperCAmelCase__       ).sample

         if not return_dict:
              return (dec,)

         return DecoderOutput(sample=UpperCAmelCase__       )









"""simple docstring"""



def  _snake_case     (    UpperCAmelCase_  :					int			):
     A__										=  generate_pascal_triangle(UpperCAmelCase_			)
     for row_idx in range(UpperCAmelCase_			):
          # Print left spaces
          for _ in range(num_rows - row_idx - 1			):
               print(end=""" """			)
          # Print row values
          for col_idx in range(row_idx + 1			):
               if col_idx != row_idx:
                    print(triangle[row_idx][col_idx]					,   end=""" """			)
               else:
                    print(triangle[row_idx][col_idx]					,   end=""""""			)
          print()





def  _snake_case     (    UpperCAmelCase_  :					int			):

     if not isinstance(UpperCAmelCase_					,   UpperCAmelCase_			):
          raise TypeError("""The input value of 'num_rows' should be 'int'"""			)

     if num_rows == 0:
          return []
     elif num_rows < 0:
          raise ValueError(
              """The input value of 'num_rows' should be greater than or equal to 0"""			)

     A__										=  []
     for current_row_idx in range(UpperCAmelCase_			):
          A__										=  populate_current_row(UpperCAmelCase_					,   UpperCAmelCase_			)
          triangle.append(UpperCAmelCase_			)
     return triangle





def  _snake_case     (    UpperCAmelCase_  :					list[list[int]]					,   UpperCAmelCase_  :					int			):
     A__										=  [-1] * (current_row_idx + 1)
     # first and last elements of current row are equal to 1
     A__			,				A__										=  1, 1
     for current_col_idx in range(1					,   UpperCAmelCase_			):
          calculate_current_element(
              UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_			)
     return current_row





def  _snake_case     (    UpperCAmelCase_  :					list[list[int]]					,   UpperCAmelCase_  :					list[int]					,   UpperCAmelCase_  :					int					,   UpperCAmelCase_  :					int					,   ):
     A__										=  triangle[current_row_idx - 1][current_col_idx - 1]
     A__										=  triangle[current_row_idx - 1][current_col_idx]
     A__										=  above_to_left_elt + above_to_right_elt





def  _snake_case     (    UpperCAmelCase_  :					int			):

     if not isinstance(UpperCAmelCase_					,   UpperCAmelCase_			):
          raise TypeError("""The input value of 'num_rows' should be 'int'"""			)

     if num_rows == 0:
          return []
     elif num_rows < 0:
          raise ValueError(
              """The input value of 'num_rows' should be greater than or equal to 0"""			)

     A__										=  [[1]]

     for row_index in range(1					,   UpperCAmelCase_			):
          A__										=  [0] + result[-1] + [0]
          A__										=  row_index + 1
          # Calculate the number of distinct elements in a row
          A__										=  sum(divmod(UpperCAmelCase_					,   2			)			)
          A__										=  [
              temp_row[i - 1] + temp_row[i] for i in range(1					,   distinct_elements + 1			)
          ]
          A__										=  row_first_half[: (row_index + 1) // 2]
          row_second_half.reverse()
          A__										=  row_first_half + row_second_half
          result.append(UpperCAmelCase_			)

     return result





def  _snake_case     (    ):
     from collections.abc import Callable
     from timeit import timeit

     def benchmark_a_function(UpperCAmelCase_  :					Callable					,   UpperCAmelCase_  :					int			) -> None:
          A__										=  F"""{func.__name__}({value})"""
          A__										=  timeit(F"""__main__.{call}"""					,   setup="""import __main__"""			)
          # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
          print(F"""{call:38} -- {timing:.4f} seconds"""			)

     for value in range(15			):  # (1, 7, 14):
          for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
               benchmark_a_function(UpperCAmelCase_					,   UpperCAmelCase_			)
          print()


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 benchmark()






