





'''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
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block





@dataclass
class lowerCamelCase    (lowerCAmelCase_       ):
  _lowercase	:						Optional[int]							       =					42





class lowerCamelCase    (lowerCAmelCase_    ,   lowerCAmelCase_       ):







  @register_to_config
  def __init__(		self  ,  lowercase__ = None  ,  lowercase__ = 3  ,  lowercase__ = 3  ,  lowercase__ = False  ,  lowercase__ = "positional"  ,  lowercase__ = 0  ,  lowercase__ = True  ,  lowercase__ = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")  ,  lowercase__ = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")  ,  lowercase__ = (224, 448, 672, 896)  ,  lowercase__ = 2  ,  lowercase__ = 1  ,  lowercase__ = 1  ,  lowercase__ = "conv"  ,  lowercase__ = "conv"  ,  lowercase__ = "silu"  ,  lowercase__ = 8  ,  lowercase__ = 32  ,  lowercase__ = 1E-5  ,  lowercase__ = "default"  ,  lowercase__ = True  ,  lowercase__ = None  ,  lowercase__ = None  ,  )		->    Any:






        """simple docstring"""





        super().__init__()

        _snake_case		:						Optional[int]         =  sample_size
        _snake_case		:						Any         =  block_out_channels[0] * 4

        # Check inputs
        if len(__UpperCamelCase		) != len(__UpperCamelCase		):
              raise ValueError(
                  F'''Must provide the same number of `down_block_types` as `up_block_types`. `down_block_types`: {down_block_types}. `up_block_types`: {up_block_types}.'''		)

        if len(__UpperCamelCase		) != len(__UpperCamelCase		):
              raise ValueError(
                  F'''Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}.'''		)

        # input
        _snake_case		:						List[Any]         =  nn.Convad(__UpperCamelCase  ,  block_out_channels[0]  ,  kernel_size=3  ,  padding=(1, 1)		)

        # time
        if time_embedding_type == "fourier":
              _snake_case		:						int         =  GaussianFourierProjection(embedding_size=block_out_channels[0]  ,  scale=16		)
              _snake_case		:						Dict         =  2 * block_out_channels[0]
        elif time_embedding_type == "positional":
              _snake_case		:						List[str]         =  Timesteps(block_out_channels[0]  ,  __UpperCamelCase  ,  __UpperCamelCase		)
              _snake_case		:						Any         =  block_out_channels[0]

        _snake_case		:						List[str]         =  TimestepEmbedding(__UpperCamelCase  ,  __UpperCamelCase		)

        # class embedding
        if class_embed_type is None and num_class_embeds is not None:
              _snake_case		:						Optional[Any]         =  nn.Embedding(__UpperCamelCase  ,  __UpperCamelCase		)
        elif class_embed_type == "timestep":
              _snake_case		:						str         =  TimestepEmbedding(__UpperCamelCase  ,  __UpperCamelCase		)
        elif class_embed_type == "identity":
              _snake_case		:						Union[str, Any]         =  nn.Identity(__UpperCamelCase  ,  __UpperCamelCase		)
        else:
              _snake_case		:						Optional[int]         =  None

        _snake_case		:						str         =  nn.ModuleList([]		)
        _snake_case		:						Any         =  None
        _snake_case		:						Tuple         =  nn.ModuleList([]		)

        # down
        _snake_case		:						Any         =  block_out_channels[0]
        for i, down_block_type in enumerate(__UpperCamelCase		):
              _snake_case		:						List[Any]         =  output_channel
              _snake_case		:						Optional[int]         =  block_out_channels[i]
              _snake_case		:						Any         =  i == len(__UpperCamelCase		) - 1

              _snake_case		:						List[Any]         =  get_down_block(
                  __UpperCamelCase  ,  num_layers=__UpperCamelCase  ,  in_channels=__UpperCamelCase  ,  out_channels=__UpperCamelCase  ,  temb_channels=__UpperCamelCase  ,  add_downsample=not is_final_block  ,  resnet_eps=__UpperCamelCase  ,  resnet_act_fn=__UpperCamelCase  ,  resnet_groups=__UpperCamelCase  ,  attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel  ,  downsample_padding=__UpperCamelCase  ,  resnet_time_scale_shift=__UpperCamelCase  ,  downsample_type=__UpperCamelCase  ,  )
              self.down_blocks.append(__UpperCamelCase		)

        # mid
        _snake_case		:						List[str]         =  UNetMidBlockaD(
            in_channels=block_out_channels[-1]  ,  temb_channels=__UpperCamelCase  ,  resnet_eps=__UpperCamelCase  ,  resnet_act_fn=__UpperCamelCase  ,  output_scale_factor=__UpperCamelCase  ,  resnet_time_scale_shift=__UpperCamelCase  ,  attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]  ,  resnet_groups=__UpperCamelCase  ,  add_attention=__UpperCamelCase  ,  )

        # up
        _snake_case		:						Tuple         =  list(reversed(__UpperCamelCase		)		)
        _snake_case		:						Optional[Any]         =  reversed_block_out_channels[0]
        for i, up_block_type in enumerate(__UpperCamelCase		):
              _snake_case		:						int         =  output_channel
              _snake_case		:						List[Any]         =  reversed_block_out_channels[i]
              _snake_case		:						Dict         =  reversed_block_out_channels[min(i + 1  ,  len(__UpperCamelCase		) - 1		)]

              _snake_case		:						int         =  i == len(__UpperCamelCase		) - 1

              _snake_case		:						Dict         =  get_up_block(
                  __UpperCamelCase  ,  num_layers=layers_per_block + 1  ,  in_channels=__UpperCamelCase  ,  out_channels=__UpperCamelCase  ,  prev_output_channel=__UpperCamelCase  ,  temb_channels=__UpperCamelCase  ,  add_upsample=not is_final_block  ,  resnet_eps=__UpperCamelCase  ,  resnet_act_fn=__UpperCamelCase  ,  resnet_groups=__UpperCamelCase  ,  attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel  ,  resnet_time_scale_shift=__UpperCamelCase  ,  upsample_type=__UpperCamelCase  ,  )
              self.up_blocks.append(__UpperCamelCase		)
              _snake_case		:						Dict         =  output_channel

        # out
        _snake_case		:						str         =  norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4  ,  32		)
        _snake_case		:						Tuple         =  nn.GroupNorm(num_channels=block_out_channels[0]  ,  num_groups=__UpperCamelCase  ,  eps=__UpperCamelCase		)
        _snake_case		:						Any         =  nn.SiLU()
        _snake_case		:						Optional[int]         =  nn.Convad(block_out_channels[0]  ,  __UpperCamelCase  ,  kernel_size=3  ,  padding=1		)







  def 					UpperCAmelCase_					(		self  ,  lowercase__  ,  lowercase__  ,  lowercase__ = None  ,  lowercase__ = True  ,  )		->    Union[UNetaDOutput, Tuple]:






        """simple docstring"""





        if self.config.center_input_sample:
              _snake_case		:						Any         =  2 * sample - 1.0

        # 1. time
        _snake_case		:						Tuple         =  timestep
        if not torch.is_tensor(__UpperCamelCase		):
              _snake_case		:						Optional[int]         =  torch.tensor([timesteps]  ,  dtype=torch.long  ,  device=sample.device		)
        elif torch.is_tensor(__UpperCamelCase		) and len(timesteps.shape		) == 0:
              _snake_case		:						List[Any]         =  timesteps[None].to(sample.device		)

        # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
        _snake_case		:						Dict         =  timesteps * torch.ones(sample.shape[0]  ,  dtype=timesteps.dtype  ,  device=timesteps.device		)

        _snake_case		:						List[str]         =  self.time_proj(__UpperCamelCase		)

        # timesteps does not contain any weights and will always return f32 tensors
        # but time_embedding might actually be running in fp16. so we need to cast here.
        # there might be better ways to encapsulate this.
        _snake_case		:						Dict         =  t_emb.to(dtype=self.dtype		)
        _snake_case		:						Optional[int]         =  self.time_embedding(__UpperCamelCase		)

        if self.class_embedding is not None:
              if class_labels is None:
                    raise ValueError('''class_labels should be provided when doing class conditioning'''		)

              if self.config.class_embed_type == "timestep":
                    _snake_case		:						List[Any]         =  self.time_proj(__UpperCamelCase		)

              _snake_case		:						List[str]         =  self.class_embedding(__UpperCamelCase		).to(dtype=self.dtype		)
              _snake_case		:						Optional[int]         =  emb + class_emb

        # 2. pre-process
        _snake_case		:						List[str]         =  sample
        _snake_case		:						str         =  self.conv_in(__UpperCamelCase		)

        # 3. down
        _snake_case		:						List[str]         =  (sample,)
        for downsample_block in self.down_blocks:
              if hasattr(__UpperCamelCase  ,  '''skip_conv'''		):
                    _snake_case		:						Optional[Any]         =  downsample_block(
                        hidden_states=__UpperCamelCase  ,  temb=__UpperCamelCase  ,  skip_sample=__UpperCamelCase		)
              else:
                    _snake_case		:						Optional[Any]         =  downsample_block(hidden_states=__UpperCamelCase  ,  temb=__UpperCamelCase		)

              down_block_res_samples += res_samples

        # 4. mid
        _snake_case		:						Union[str, Any]         =  self.mid_block(__UpperCamelCase  ,  __UpperCamelCase		)

        # 5. up
        _snake_case		:						Dict         =  None
        for upsample_block in self.up_blocks:
              _snake_case		:						Union[str, Any]         =  down_block_res_samples[-len(upsample_block.resnets		) :]
              _snake_case		:						Optional[Any]         =  down_block_res_samples[: -len(upsample_block.resnets		)]

              if hasattr(__UpperCamelCase  ,  '''skip_conv'''		):
                    _snake_case		:						str         =  upsample_block(__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase		)
              else:
                    _snake_case		:						Tuple         =  upsample_block(__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase		)

        # 6. post-process
        _snake_case		:						List[Any]         =  self.conv_norm_out(__UpperCamelCase		)
        _snake_case		:						Any         =  self.conv_act(__UpperCamelCase		)
        _snake_case		:						Optional[Any]         =  self.conv_out(__UpperCamelCase		)

        if skip_sample is not None:
              sample += skip_sample

        if self.config.time_embedding_type == "fourier":
              _snake_case		:						str         =  timesteps.reshape((sample.shape[0], *([1] * len(sample.shape[1:]		)))		)
              _snake_case		:						int         =  sample / timesteps

        if not return_dict:
              return (sample,)

        return UNetaDOutput(sample=__UpperCamelCase		)




"""simple docstring"""




def 				a__			(					lowerCAmelCase						:							int		):






     '''simple docstring'''
     if number != int(lowerCAmelCase		):
          raise ValueError("factorial() only accepts integral values"		)
     if number < 0:
          raise ValueError("factorial() not defined for negative values"		)
     UpperCAmelCase__				:			int							=    1
     for i in range(1							,     number + 1		):
          value *= i
     return value



def 				a__			(					lowerCAmelCase						:							int		):






     '''simple docstring'''
     if not isinstance(lowerCAmelCase							,     lowerCAmelCase		):
          raise ValueError("factorial() only accepts integral values"		)
     if n < 0:
          raise ValueError("factorial() not defined for negative values"		)
     return 1 if n == 0 or n == 1 else n * factorial(n - 1		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    A__   :						Any 							=     int(input("""Enter a positive integer: """).strip() or 0)
    print(f"""factorial{n} is {factorial(n)}""")



