
'''simple docstring'''
from dataclasses import dataclass
from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..models.embeddings import ImagePositionalEmbeddings
from ..utils import BaseOutput, deprecate
from .attention import BasicTransformerBlock
from .embeddings import PatchEmbed
from .modeling_utils import ModelMixin






@dataclass
class  UpperCAmelCase_					(						snake_case__       ):
 UpperCAmelCase_		     =    42






class  UpperCAmelCase_					(						snake_case__     ,    snake_case__       ):





 @register_to_config
 def __init__(     self			,							lowercase_ = 16			,							lowercase_ = 88			,							lowercase_ = None			,							lowercase_ = None			,							lowercase_ = 1			,							lowercase_ = 0.0			,							lowercase_ = 32			,							lowercase_ = None			,							lowercase_ = False			,							lowercase_ = None			,							lowercase_ = None			,							lowercase_ = None			,							lowercase_ = "geglu"			,							lowercase_ = None			,							lowercase_ = False			,							lowercase_ = False			,							lowercase_ = False			,							lowercase_ = "layer_norm"			,							lowercase_ = True			,							):
     super().__init__()
     snake_case_			:     List[str]        =    use_linear_projection
     snake_case_			:     Tuple        =    num_attention_heads
     snake_case_			:     Optional[int]        =    attention_head_dim
     snake_case_			:     Any        =    num_attention_heads * attention_head_dim

     # 1. Transformer2DModel can process both standard continuous images of shape `(batch_size, num_channels, width, height)` as well as quantized image embeddings of shape `(batch_size, num_image_vectors)`
     # Define whether input is continuous or discrete depending on configuration
     snake_case_			:     List[str]        =    (in_channels is not None) and (patch_size is None)
     snake_case_			:     List[Any]        =    num_vector_embeds is not None
     snake_case_			:     Union[str, Any]        =    in_channels is not None and patch_size is not None

     if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
         snake_case_			:     Optional[int]        =    (
             F'The configuration file of this model: {self.__class__} is outdated. `norm_type` is either not set or'
             " incorrectly set to `'layer_norm'`.Make sure to set `norm_type` to `'ada_norm'` in the config."
             " Please make sure to update the config accordingly as leaving `norm_type` might led to incorrect"
             " results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it"
             " would be very nice if you could open a Pull request for the `transformer/config.json` file"
         )
         deprecate("norm_type!=num_embeds_ada_norm"			,							"1.0.0"			,							lowercase_			,							standard_warn=lowercase_)
         snake_case_			:     Union[str, Any]        =    "ada_norm"

     if self.is_input_continuous and self.is_input_vectorized:
         raise ValueError(
             F'Cannot define both `in_channels`: {in_channels} and `num_vector_embeds`: {num_vector_embeds}. Make'
             " sure that either `in_channels` or `num_vector_embeds` is None.")
     elif self.is_input_vectorized and self.is_input_patches:
         raise ValueError(
             F'Cannot define both `num_vector_embeds`: {num_vector_embeds} and `patch_size`: {patch_size}. Make'
             " sure that either `num_vector_embeds` or `num_patches` is None.")
     elif not self.is_input_continuous and not self.is_input_vectorized and not self.is_input_patches:
         raise ValueError(
             F'Has to define `in_channels`: {in_channels}, `num_vector_embeds`: {num_vector_embeds}, or patch_size:'
             F' {patch_size}. Make sure that `in_channels`, `num_vector_embeds` or `num_patches` is not None.')

     # 2. Define input layers
     if self.is_input_continuous:
         snake_case_			:     Optional[Any]        =    in_channels

         snake_case_			:     Dict        =    torch.nn.GroupNorm(num_groups=lowercase_			,							num_channels=lowercase_			,							eps=1E-6			,							affine=lowercase_)
         if use_linear_projection:
             snake_case_			:     Dict        =    nn.Linear(lowercase_			,							lowercase_)
         else:
             snake_case_			:     Tuple        =    nn.Convad(lowercase_			,							lowercase_			,							kernel_size=1			,							stride=1			,							padding=0)
     elif self.is_input_vectorized:
         assert sample_size is not None, "Transformer2DModel over discrete input must provide sample_size"
         assert num_vector_embeds is not None, "Transformer2DModel over discrete input must provide num_embed"

         snake_case_			:     Optional[Any]        =    sample_size
         snake_case_			:     str        =    sample_size
         snake_case_			:     List[str]        =    num_vector_embeds
         snake_case_			:     Tuple        =    self.height * self.width

         snake_case_			:     Dict        =    ImagePositionalEmbeddings(
             num_embed=lowercase_			,							embed_dim=lowercase_			,							height=self.height			,							width=self.width)
     elif self.is_input_patches:
         assert sample_size is not None, "Transformer2DModel over patched input must provide sample_size"

         snake_case_			:     Dict        =    sample_size
         snake_case_			:     Optional[Any]        =    sample_size

         snake_case_			:     Dict        =    patch_size
         snake_case_			:     Optional[int]        =    PatchEmbed(
             height=lowercase_			,							width=lowercase_			,							patch_size=lowercase_			,							in_channels=lowercase_			,							embed_dim=lowercase_			,							)

     # 3. Define transformers blocks
     snake_case_			:     Dict        =    nn.ModuleList(
         [
             BasicTransformerBlock(
                 lowercase_			,							lowercase_			,							lowercase_			,							dropout=lowercase_			,							cross_attention_dim=lowercase_			,							activation_fn=lowercase_			,							num_embeds_ada_norm=lowercase_			,							attention_bias=lowercase_			,							only_cross_attention=lowercase_			,							upcast_attention=lowercase_			,							norm_type=lowercase_			,							norm_elementwise_affine=lowercase_			,							)
             for d in range(lowercase_)
         ])

     # 4. Define output layers
     snake_case_			:     Optional[int]        =    in_channels if out_channels is None else out_channels
     if self.is_input_continuous:
         # TODO: should use out_channels for continuous projections
         if use_linear_projection:
             snake_case_			:     Optional[int]        =    nn.Linear(lowercase_			,							lowercase_)
         else:
             snake_case_			:     Dict        =    nn.Convad(lowercase_			,							lowercase_			,							kernel_size=1			,							stride=1			,							padding=0)
     elif self.is_input_vectorized:
         snake_case_			:     Dict        =    nn.LayerNorm(lowercase_)
         snake_case_			:     Optional[int]        =    nn.Linear(lowercase_			,							self.num_vector_embeds - 1)
     elif self.is_input_patches:
         snake_case_			:     int        =    nn.LayerNorm(lowercase_			,							elementwise_affine=lowercase_			,							eps=1E-6)
         snake_case_			:     str        =    nn.Linear(lowercase_			,							2 * inner_dim)
         snake_case_			:     List[Any]        =    nn.Linear(lowercase_			,							patch_size * patch_size * self.out_channels)





 def 							snake_case__		(     self			,							lowercase_			,							lowercase_ = None			,							lowercase_ = None			,							lowercase_ = None			,							lowercase_ = None			,							lowercase_ = None			,							lowercase_ = None			,							lowercase_ = True			,							):
     # ensure attention_mask is a bias, and give it a singleton query_tokens dimension.
     #   we may have done this conversion already, e.g. if we came here via UNet2DConditionModel#forward.
     #   we can tell by counting dims; if ndim == 2: it's a mask rather than a bias.
     # expects mask of shape:
     #   [batch, key_tokens]
     # adds singleton query_tokens dimension:
     #   [batch,                    1, key_tokens]
     # this helps to broadcast it as a bias over attention scores, which will be in one of the following shapes:
     #   [batch,  heads, query_tokens, key_tokens] (e.g. torch sdp attn)
     #   [batch * heads, query_tokens, key_tokens] (e.g. xformers or classic attn)
     if attention_mask is not None and attention_mask.ndim == 2:
         # assume that mask is expressed as:
         #   (1 = keep,      0 = discard)
         # convert mask into a bias that can be added to attention scores:
         #       (keep = +0,     discard = -10000.0)
         snake_case_			:     int        =    (1 - attention_mask.to(hidden_states.dtype)) * -10_000.0
         snake_case_			:     Optional[int]        =    attention_mask.unsqueeze(1)

     # convert encoder_attention_mask to a bias the same way we do for attention_mask
     if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2:
         snake_case_			:     Union[str, Any]        =    (1 - encoder_attention_mask.to(hidden_states.dtype)) * -10_000.0
         snake_case_			:     List[Any]        =    encoder_attention_mask.unsqueeze(1)

     # 1. Input
     if self.is_input_continuous:
         snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_			:     str        =    hidden_states.shape
         snake_case_			:     Optional[int]        =    hidden_states

         snake_case_			:     str        =    self.norm(lowercase_)
         if not self.use_linear_projection:
             snake_case_			:     int        =    self.proj_in(lowercase_)
             snake_case_			:     List[str]        =    hidden_states.shape[1]
             snake_case_			:     Optional[int]        =    hidden_states.permute(0			,							2			,							3			,							1).reshape(lowercase_			,							height * width			,							lowercase_)
         else:
             snake_case_			:     List[str]        =    hidden_states.shape[1]
             snake_case_			:     Union[str, Any]        =    hidden_states.permute(0			,							2			,							3			,							1).reshape(lowercase_			,							height * width			,							lowercase_)
             snake_case_			:     List[Any]        =    self.proj_in(lowercase_)
     elif self.is_input_vectorized:
         snake_case_			:     Optional[Any]        =    self.latent_image_embedding(lowercase_)
     elif self.is_input_patches:
         snake_case_			:     Dict        =    self.pos_embed(lowercase_)

     # 2. Blocks
     for block in self.transformer_blocks:
         snake_case_			:     List[str]        =    block(
             lowercase_			,							attention_mask=lowercase_			,							encoder_hidden_states=lowercase_			,							encoder_attention_mask=lowercase_			,							timestep=lowercase_			,							cross_attention_kwargs=lowercase_			,							class_labels=lowercase_			,							)

     # 3. Output
     if self.is_input_continuous:
         if not self.use_linear_projection:
             snake_case_			:     Dict        =    hidden_states.reshape(lowercase_			,							lowercase_			,							lowercase_			,							lowercase_).permute(0			,							3			,							1			,							2).contiguous()
             snake_case_			:     Optional[Any]        =    self.proj_out(lowercase_)
         else:
             snake_case_			:     str        =    self.proj_out(lowercase_)
             snake_case_			:     Tuple        =    hidden_states.reshape(lowercase_			,							lowercase_			,							lowercase_			,							lowercase_).permute(0			,							3			,							1			,							2).contiguous()

         snake_case_			:     int        =    hidden_states + residual
     elif self.is_input_vectorized:
         snake_case_			:     str        =    self.norm_out(lowercase_)
         snake_case_			:     Union[str, Any]        =    self.out(lowercase_)
         # (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
         snake_case_			:     Any        =    logits.permute(0			,							2			,							1)

         # log(p(x_0))
         snake_case_			:     int        =    F.log_softmax(logits.double()			,							dim=1).float()
     elif self.is_input_patches:
         # TODO: cleanup!
         snake_case_			:     List[str]        =    self.transformer_blocks[0].norma.emb(
             lowercase_			,							lowercase_			,							hidden_dtype=hidden_states.dtype)
         snake_case_      ,			snake_case_			:     Optional[int]        =    self.proj_out_a(F.silu(lowercase_)).chunk(2			,							dim=1)
         snake_case_			:     Dict        =    self.norm_out(lowercase_) * (1 + scale[:, None]) + shift[:, None]
         snake_case_			:     Union[str, Any]        =    self.proj_out_a(lowercase_)

         # unpatchify
         snake_case_			:     str        =    int(hidden_states.shape[1] ** 0.5)
         snake_case_			:     Dict        =    hidden_states.reshape(
             shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels))
         snake_case_			:     Dict        =    torch.einsum("nhwpqc->nchpwq"			,							lowercase_)
         snake_case_			:     List[str]        =    hidden_states.reshape(
             shape=(-1, self.out_channels, height * self.patch_size, width * self.patch_size))

     if not return_dict:
         return (output,)

     return TransformeraDModelOutput(sample=lowercase_)






'''simple docstring'''
def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE							):
    """simple docstring"""






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

def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE							):
    """simple docstring"""






    if not isinstance(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							):
        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_           =    int(input("Enter a positive integer: ").strip() or 0)
  print(f"""factorial{n} is {factorial(n)}""")




