'''simple docstring'''





from typing import Any, Dict, Optional

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

from ..utils import maybe_allow_in_graph
from .activations import get_activation
from .attention_processor import Attention
from .embeddings import CombinedTimestepLabelEmbeddings




@maybe_allow_in_graph
class        __A      ( nn.Module	):



    def __init__(self    :    int   ,			__a    :    int   ,			__a    :    int   ,			__a    :    int   ,			__a    :    Dict=0.0   ,			__a    :    Optional[int] = None   ,			__a    :    str = "geglu"   ,			__a    :    Optional[int] = None   ,			__a    :    bool = False   ,			__a    :    bool = False   ,			__a    :    bool = False   ,			__a    :    bool = False   ,			__a    :    bool = True   ,			__a    :    str = "layer_norm"   ,			__a    :    bool = False   ,			):
         super().__init__()
         UpperCAmelCase_           =   only_cross_attention

         UpperCAmelCase_           =   (num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero"
         UpperCAmelCase_           =   (num_embeds_ada_norm is not None) and norm_type == "ada_norm"

         if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None:
              raise ValueError(
                  f"""`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to"""
                  f""" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.""" )

         # Define 3 blocks. Each block has its own normalization layer.
         # 1. Self-Attn
         if self.use_ada_layer_norm:
              UpperCAmelCase_           =   AdaLayerNorm(__a   ,			__a )
         elif self.use_ada_layer_norm_zero:
              UpperCAmelCase_           =   AdaLayerNormZero(__a   ,			__a )
         else:
              UpperCAmelCase_           =   nn.LayerNorm(__a   ,			elementwise_affine=__a )
         UpperCAmelCase_           =   Attention(
             query_dim=__a   ,			heads=__a   ,			dim_head=__a   ,			dropout=__a   ,			bias=__a   ,			cross_attention_dim=cross_attention_dim if only_cross_attention else None   ,			upcast_attention=__a   ,			)

         # 2. Cross-Attn
         if cross_attention_dim is not None or double_self_attention:
              # We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
              # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
              # the second cross attention block.
              UpperCAmelCase_           =   (
                  AdaLayerNorm(__a   ,			__a )
                  if self.use_ada_layer_norm
                  else nn.LayerNorm(__a   ,			elementwise_affine=__a )
              )
              UpperCAmelCase_           =   Attention(
                  query_dim=__a   ,			cross_attention_dim=cross_attention_dim if not double_self_attention else None   ,			heads=__a   ,			dim_head=__a   ,			dropout=__a   ,			bias=__a   ,			upcast_attention=__a   ,			)  # is self-attn if encoder_hidden_states is none
         else:
              UpperCAmelCase_           =   None
              UpperCAmelCase_           =   None

         # 3. Feed-forward
         UpperCAmelCase_           =   nn.LayerNorm(__a   ,			elementwise_affine=__a )
         UpperCAmelCase_           =   FeedForward(__a   ,			dropout=__a   ,			activation_fn=__a   ,			final_dropout=__a )

         # let chunk size default to None
         UpperCAmelCase_           =   None
         UpperCAmelCase_           =   0



    def 					_lowercase						(self    :    str   ,			__a    :    Optional[int]   ,			__a    :    int ):
         # Sets chunk feed-forward
         UpperCAmelCase_           =   chunk_size
         UpperCAmelCase_           =   dim






    def 					_lowercase						(self    :    Tuple   ,			__a    :    torch.FloatTensor   ,			__a    :    Optional[torch.FloatTensor] = None   ,			__a    :    Optional[torch.FloatTensor] = None   ,			__a    :    Optional[torch.FloatTensor] = None   ,			__a    :    Optional[torch.LongTensor] = None   ,			__a    :    Dict[str, Any] = None   ,			__a    :    Optional[torch.LongTensor] = None   ,			):
         # Notice that normalization is always applied before the real computation in the following blocks.
         # 1. Self-Attention
         if self.use_ada_layer_norm:
              UpperCAmelCase_           =   self.norma(__a   ,			__a )
         elif self.use_ada_layer_norm_zero:
              UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_           =   self.norma(
                  __a   ,			__a   ,			__a   ,			hidden_dtype=hidden_states.dtype )
         else:
              UpperCAmelCase_           =   self.norma(__a )

         UpperCAmelCase_           =   cross_attention_kwargs if cross_attention_kwargs is not None else {}

         UpperCAmelCase_           =   self.attna(
             __a   ,			encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None   ,			attention_mask=__a   ,			**__a   ,			)
         if self.use_ada_layer_norm_zero:
              UpperCAmelCase_           =   gate_msa.unsqueeze(1 ) * attn_output
         UpperCAmelCase_           =   attn_output + hidden_states

         # 2. Cross-Attention
         if self.attna is not None:
              UpperCAmelCase_           =   (
                  self.norma(__a   ,			__a ) if self.use_ada_layer_norm else self.norma(__a )
              )

              UpperCAmelCase_           =   self.attna(
                  __a   ,			encoder_hidden_states=__a   ,			attention_mask=__a   ,			**__a   ,			)
              UpperCAmelCase_           =   attn_output + hidden_states

         # 3. Feed-forward
         UpperCAmelCase_           =   self.norma(__a )

         if self.use_ada_layer_norm_zero:
              UpperCAmelCase_           =   norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]

         if self._chunk_size is not None:
              # "feed_forward_chunk_size" can be used to save memory
              if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0:
                   raise ValueError(
                       f"""`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.""" )

              UpperCAmelCase_           =   norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
              UpperCAmelCase_           =   torch.cat(
                  [self.ff(__a ) for hid_slice in norm_hidden_states.chunk(__a   ,			dim=self._chunk_dim )]   ,			dim=self._chunk_dim   ,			)
         else:
              UpperCAmelCase_           =   self.ff(__a )

         if self.use_ada_layer_norm_zero:
              UpperCAmelCase_           =   gate_mlp.unsqueeze(1 ) * ff_output

         UpperCAmelCase_           =   ff_output + hidden_states

         return hidden_states




class        __A      ( nn.Module	):



    def __init__(self    :    int   ,			__a    :    int   ,			__a    :    Optional[int] = None   ,			__a    :    int = 4   ,			__a    :    float = 0.0   ,			__a    :    str = "geglu"   ,			__a    :    bool = False   ,			):
         super().__init__()
         UpperCAmelCase_           =   int(dim * mult )
         UpperCAmelCase_           =   dim_out if dim_out is not None else dim

         if activation_fn == "gelu":
              UpperCAmelCase_           =   GELU(__a   ,			__a )
         if activation_fn == "gelu-approximate":
              UpperCAmelCase_           =   GELU(__a   ,			__a   ,			approximate="tanh" )
         elif activation_fn == "geglu":
              UpperCAmelCase_           =   GEGLU(__a   ,			__a )
         elif activation_fn == "geglu-approximate":
              UpperCAmelCase_           =   ApproximateGELU(__a   ,			__a )

         UpperCAmelCase_           =   nn.ModuleList([] )
         # project in
         self.net.append(__a )
         # project dropout
         self.net.append(nn.Dropout(__a ) )
         # project out
         self.net.append(nn.Linear(__a   ,			__a ) )
         # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
         if final_dropout:
              self.net.append(nn.Dropout(__a ) )






    def 					_lowercase						(self    :    Optional[int]   ,			__a    :    Optional[int] ):
         for module in self.net:
              UpperCAmelCase_           =   module(__a )
         return hidden_states




class        __A      ( nn.Module	):



    def __init__(self    :    Optional[int]   ,			__a    :    int   ,			__a    :    int   ,			__a    :    str = "none" ):
         super().__init__()
         UpperCAmelCase_           =   nn.Linear(__a   ,			__a )
         UpperCAmelCase_           =   approximate



    def 					_lowercase						(self    :    Optional[int]   ,			__a    :    Dict ):
         if gate.device.type != "mps":
              return F.gelu(__a   ,			approximate=self.approximate )
         # mps: gelu is not implemented for float16
         return F.gelu(gate.to(dtype=torch.floataa )   ,			approximate=self.approximate ).to(dtype=gate.dtype )






    def 					_lowercase						(self    :    List[str]   ,			__a    :    Union[str, Any] ):
         UpperCAmelCase_           =   self.proj(__a )
         UpperCAmelCase_           =   self.gelu(__a )
         return hidden_states




class        __A      ( nn.Module	):



    def __init__(self    :    List[str]   ,			__a    :    int   ,			__a    :    int ):
         super().__init__()
         UpperCAmelCase_           =   nn.Linear(__a   ,			dim_out * 2 )



    def 					_lowercase						(self    :    Union[str, Any]   ,			__a    :    List[Any] ):
         if gate.device.type != "mps":
              return F.gelu(__a )
         # mps: gelu is not implemented for float16
         return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype )






    def 					_lowercase						(self    :    List[str]   ,			__a    :    Union[str, Any] ):
         UpperCAmelCase_    , UpperCAmelCase_           =   self.proj(__a ).chunk(2   ,			dim=-1 )
         return hidden_states * self.gelu(__a )




class        __A      ( nn.Module	):



    def __init__(self    :    Optional[int]   ,			__a    :    int   ,			__a    :    int ):
         super().__init__()
         UpperCAmelCase_           =   nn.Linear(__a   ,			__a )






    def 					_lowercase						(self    :    Union[str, Any]   ,			__a    :    Optional[int] ):
         UpperCAmelCase_           =   self.proj(__a )
         return x * torch.sigmoid(1.7_02 * x )




class        __A      ( nn.Module	):



    def __init__(self    :    Any   ,			__a    :    str   ,			__a    :    Optional[Any] ):
         super().__init__()
         UpperCAmelCase_           =   nn.Embedding(__a   ,			__a )
         UpperCAmelCase_           =   nn.SiLU()
         UpperCAmelCase_           =   nn.Linear(__a   ,			embedding_dim * 2 )
         UpperCAmelCase_           =   nn.LayerNorm(__a   ,			elementwise_affine=__a )






    def 					_lowercase						(self    :    Tuple   ,			__a    :    int   ,			__a    :    List[Any] ):
         UpperCAmelCase_           =   self.linear(self.silu(self.emb(__a ) ) )
         UpperCAmelCase_    , UpperCAmelCase_           =   torch.chunk(__a   ,			2 )
         UpperCAmelCase_           =   self.norm(__a ) * (1 + scale) + shift
         return x




class        __A      ( nn.Module	):



    def __init__(self    :    List[str]   ,			__a    :    Optional[Any]   ,			__a    :    List[Any] ):
         super().__init__()

         UpperCAmelCase_           =   CombinedTimestepLabelEmbeddings(__a   ,			__a )

         UpperCAmelCase_           =   nn.SiLU()
         UpperCAmelCase_           =   nn.Linear(__a   ,			6 * embedding_dim   ,			bias=__a )
         UpperCAmelCase_           =   nn.LayerNorm(__a   ,			elementwise_affine=__a   ,			eps=1E-6 )






    def 					_lowercase						(self    :    str   ,			__a    :    Optional[int]   ,			__a    :    Any   ,			__a    :    Any   ,			__a    :    str=None ):
         UpperCAmelCase_           =   self.linear(self.silu(self.emb(__a   ,			__a   ,			hidden_dtype=__a ) ) )
         UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_           =   emb.chunk(6   ,			dim=1 )
         UpperCAmelCase_           =   self.norm(__a ) * (1 + scale_msa[:, None]) + shift_msa[:, None]
         return x, gate_msa, shift_mlp, scale_mlp, gate_mlp




class        __A      ( nn.Module	):



    def __init__(self    :    Optional[Any]   ,			__a    :    int   ,			__a    :    int   ,			__a    :    int   ,			__a    :    Optional[str] = None   ,			__a    :    float = 1E-5 ):
         super().__init__()
         UpperCAmelCase_           =   num_groups
         UpperCAmelCase_           =   eps

         if act_fn is None:
              UpperCAmelCase_           =   None
         else:
              UpperCAmelCase_           =   get_activation(__a )

         UpperCAmelCase_           =   nn.Linear(__a   ,			out_dim * 2 )






    def 					_lowercase						(self    :    Optional[int]   ,			__a    :    List[str]   ,			__a    :    str ):
         if self.act:
              UpperCAmelCase_           =   self.act(__a )
         UpperCAmelCase_           =   self.linear(__a )
         UpperCAmelCase_           =   emb[:, :, None, None]
         UpperCAmelCase_    , UpperCAmelCase_           =   emb.chunk(2   ,			dim=1 )

         UpperCAmelCase_           =   F.group_norm(__a   ,			self.num_groups   ,			eps=self.eps )
         UpperCAmelCase_           =   x * (1 + scale) + shift
         return x





'''simple docstring'''





import math





def 	lowerCAmelCase_				(					)    ->      None:


     '''simple docstring'''




     UpperCAmelCase_           =   input("Enter message: "			)
     UpperCAmelCase_           =   int(input(f"""Enter key [2-{len(snake_case_			) - 1}]: """			)			)
     UpperCAmelCase_           =   input("Encryption/Decryption [e/d]: "			)

     if mode.lower().startswith("e"			):
          UpperCAmelCase_           =   encrypt_message(snake_case_					,    snake_case_			)
     elif mode.lower().startswith("d"			):
          UpperCAmelCase_           =   decrypt_message(snake_case_					,    snake_case_			)

     # Append pipe symbol (vertical bar) to identify spaces at the end.
     print(f"""Output:\n{text + "|"}"""			)





def 	lowerCAmelCase_				(					snake_case_       : int					,    snake_case_       : str			)    ->      str:


     '''simple docstring'''




     UpperCAmelCase_           =   [""] * key
     for col in range(snake_case_			):
          UpperCAmelCase_           =   col
          while pointer < len(snake_case_			):
               cipher_text[col] += message[pointer]
               pointer += key
     return "".join(snake_case_			)





def 	lowerCAmelCase_				(					snake_case_       : int					,    snake_case_       : str			)    ->      str:


     '''simple docstring'''




     UpperCAmelCase_           =   math.ceil(len(snake_case_			) / key			)
     UpperCAmelCase_           =   key
     UpperCAmelCase_           =   (num_cols * num_rows) - len(snake_case_			)
     UpperCAmelCase_           =   [""] * num_cols
     UpperCAmelCase_           =   0
     UpperCAmelCase_           =   0

     for symbol in message:
          plain_text[col] += symbol
          col += 1

          if (
              (col == num_cols)
              or (col == num_cols - 1)
              and (row >= num_rows - num_shaded_boxes)
          ):
               UpperCAmelCase_           =   0
               row += 1

     return "".join(snake_case_			)


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    main()




