


"""simple docstring"""



from dataclasses import dataclass
from typing import Dict, Optional, Union

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

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .attention_processor import AttentionProcessor, AttnProcessor
from .embeddings import TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
@dataclass
class 					__magic_name__		(			__UpperCAmelCase     ):
       __A			:      List[str]        =   42







class 					__magic_name__		(			__UpperCAmelCase			,						__UpperCAmelCase     ):






       @register_to_config
       def __init__(   self  :    Any  ,     snake_case__  :    List[str] = 3_2  ,     snake_case__  :    Tuple = 6_4  ,     snake_case__  :    Dict = 2_0  ,     snake_case__  :    List[Any] = 7_6_8  ,     snake_case__  :    List[Any]=7_7  ,     snake_case__  :    List[Any]=4  ,     snake_case__  :    Union[str, Any] = 0.0  ,     snake_case__  :    str = "silu"  ,     snake_case__  :    int = None  ,     snake_case__  :    Any = None  ,     snake_case__  :    Any = "linear"  ,     snake_case__  :    Optional[Any] = "prd"  ,     snake_case__  :    Tuple = None  ,     snake_case__  :    Optional[Any] = None  ,     snake_case__  :    Any = None  ,     ):

              '''simple docstring'''






              super().__init__()
              lowercase		:Any       							=				num_attention_heads
              lowercase		:str       							=				attention_head_dim
              lowercase		:Dict       							=				num_attention_heads * attention_head_dim
              lowercase		:int       							=				additional_embeddings

              lowercase		:Optional[int]       							=				time_embed_dim or inner_dim
              lowercase		:Tuple       							=				embedding_proj_dim or embedding_dim
              lowercase		:str       							=				clip_embed_dim or embedding_dim

              lowercase		:int       							=				Timesteps(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE  ,     0				)
              lowercase		:Union[str, Any]       							=				TimestepEmbedding(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE  ,     out_dim=_SCREAMING_SNAKE_CASE  ,     act_fn=_SCREAMING_SNAKE_CASE				)

              lowercase		:Optional[Any]       							=				nn.Linear(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				)

              if embedding_proj_norm_type is None:
                     lowercase		:Dict       							=				None
              elif embedding_proj_norm_type == "layer":
                     lowercase		:List[str]       							=				nn.LayerNorm(_SCREAMING_SNAKE_CASE				)
              else:
                     raise ValueError(f"""unsupported embedding_proj_norm_type: {embedding_proj_norm_type}"""				)

              lowercase		:int       							=				nn.Linear(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				)

              if encoder_hid_proj_type is None:
                     lowercase		:Tuple       							=				None
              elif encoder_hid_proj_type == "linear":
                     lowercase		:int       							=				nn.Linear(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				)
              else:
                     raise ValueError(f"""unsupported encoder_hid_proj_type: {encoder_hid_proj_type}"""				)

              lowercase		:int       							=				nn.Parameter(torch.zeros(1  ,     num_embeddings + additional_embeddings  ,     _SCREAMING_SNAKE_CASE				)				)

              if added_emb_type == "prd":
                     lowercase		:Optional[int]       							=				nn.Parameter(torch.zeros(1  ,     1  ,     _SCREAMING_SNAKE_CASE				)				)
              elif added_emb_type is None:
                     lowercase		:Union[str, Any]       							=				None
              else:
                     raise ValueError(
                         f"""`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `'prd'` or `None`."""				)

              lowercase		:Any       							=				nn.ModuleList(
                  [
                      BasicTransformerBlock(
                          _SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE  ,     dropout=_SCREAMING_SNAKE_CASE  ,     activation_fn='''gelu'''  ,     attention_bias=_SCREAMING_SNAKE_CASE  ,     )
                      for d in range(_SCREAMING_SNAKE_CASE				)
                  ]				)

              if norm_in_type == "layer":
                     lowercase		:Union[str, Any]       							=				nn.LayerNorm(_SCREAMING_SNAKE_CASE				)
              elif norm_in_type is None:
                     lowercase		:List[str]       							=				None
              else:
                     raise ValueError(f"""Unsupported norm_in_type: {norm_in_type}."""				)

              lowercase		:Any       							=				nn.LayerNorm(_SCREAMING_SNAKE_CASE				)

              lowercase		:Dict       							=				nn.Linear(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				)

              lowercase		:Union[str, Any]       							=				torch.full(
                  [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings]  ,     -1_0_0_0_0.0				)
              causal_attention_mask.triu_(1				)
              lowercase		:int       							=				causal_attention_mask[None, ...]
              self.register_buffer('''causal_attention_mask'''  ,     _SCREAMING_SNAKE_CASE  ,     persistent=_SCREAMING_SNAKE_CASE				)

              lowercase		:Optional[int]       							=				nn.Parameter(torch.zeros(1  ,     _SCREAMING_SNAKE_CASE				)				)
              lowercase		:Optional[Any]       							=				nn.Parameter(torch.zeros(1  ,     _SCREAMING_SNAKE_CASE				)				)






       @property
       # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:Tuple       							=				{}

              def fn_recursive_add_processors(snake_case__  :    Dict  ,     snake_case__  :    List[Any]  ,     snake_case__  :    Optional[Any]				):
                     if hasattr(_SCREAMING_SNAKE_CASE  ,     '''set_processor'''				):
                            lowercase		:int       							=				module.processor

                     for sub_name, child in module.named_children():
                            fn_recursive_add_processors(f"""{name}.{sub_name}"""  ,     _SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				)

                     return processors

              for name, module in self.named_children():
                     fn_recursive_add_processors(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				)

              return processors






       def  __snake_case						(   self  :    List[str]  ,     snake_case__  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				len(self.attn_processors.keys()				)

              if isinstance(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				) and len(_SCREAMING_SNAKE_CASE				) != count:
                     raise ValueError(
                         f"""A dict of processors was passed, but the number of processors {len(_SCREAMING_SNAKE_CASE				)} does not match the"""
                         f""" number of attention layers: {count}. Please make sure to pass {count} processor classes."""				)

              def fn_recursive_attn_processor(snake_case__  :    Any  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    Union[str, Any]				):
                     if hasattr(_SCREAMING_SNAKE_CASE  ,     '''set_processor'''				):
                            if not isinstance(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				):
                                   module.set_processor(_SCREAMING_SNAKE_CASE				)
                            else:
                                   module.set_processor(processor.pop(f"""{name}.processor"""				)				)

                     for sub_name, child in module.named_children():
                            fn_recursive_attn_processor(f"""{name}.{sub_name}"""  ,     _SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				)

              for name, module in self.named_children():
                     fn_recursive_attn_processor(_SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE  ,     _SCREAMING_SNAKE_CASE				)






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              self.set_attn_processor(AttnProcessor()				)






       def  __snake_case						(   self  :    Any  ,     snake_case__  :    Any  ,     snake_case__  :    Tuple  ,     snake_case__  :    str  ,     snake_case__  :    Tuple = None  ,     snake_case__  :    Optional[Any] = None  ,     snake_case__  :    List[Any] = True  ,     ):

              '''simple docstring'''






              lowercase		:Any       							=				hidden_states.shape[0]

              lowercase		:int       							=				timestep
              if not torch.is_tensor(_SCREAMING_SNAKE_CASE				):
                     lowercase		:Optional[Any]       							=				torch.tensor([timesteps]  ,     dtype=torch.long  ,     device=hidden_states.device				)
              elif torch.is_tensor(_SCREAMING_SNAKE_CASE				) and len(timesteps.shape				) == 0:
                     lowercase		:Optional[Any]       							=				timesteps[None].to(hidden_states.device				)

              # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
              lowercase		:Optional[int]       							=				timesteps * torch.ones(_SCREAMING_SNAKE_CASE  ,     dtype=timesteps.dtype  ,     device=timesteps.device				)

              lowercase		:int       							=				self.time_proj(_SCREAMING_SNAKE_CASE				)

              # timesteps does not contain any weights and will always return f32 tensors
              # but time_embedding might be fp16, so we need to cast here.
              lowercase		:Tuple       							=				timesteps_projected.to(dtype=self.dtype				)
              lowercase		:Tuple       							=				self.time_embedding(_SCREAMING_SNAKE_CASE				)

              if self.embedding_proj_norm is not None:
                     lowercase		:Dict       							=				self.embedding_proj_norm(_SCREAMING_SNAKE_CASE				)

              lowercase		:Optional[int]       							=				self.embedding_proj(_SCREAMING_SNAKE_CASE				)
              if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
                     lowercase		:Tuple       							=				self.encoder_hidden_states_proj(_SCREAMING_SNAKE_CASE				)
              elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None:
                     raise ValueError('''`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set'''				)

              lowercase		:Optional[Any]       							=				self.proj_in(_SCREAMING_SNAKE_CASE				)

              lowercase		:str       							=				self.positional_embedding.to(hidden_states.dtype				)

              lowercase		:List[Any]       							=				[]
              lowercase		:str       							=				0

              if encoder_hidden_states is not None:
                     additional_embeds.append(_SCREAMING_SNAKE_CASE				)
                     additional_embeddings_len += encoder_hidden_states.shape[1]

              if len(proj_embeddings.shape				) == 2:
                     lowercase		:List[str]       							=				proj_embeddings[:, None, :]

              if len(hidden_states.shape				) == 2:
                     lowercase		:Union[str, Any]       							=				hidden_states[:, None, :]

              lowercase		:List[Any]       							=				additional_embeds + [
                  proj_embeddings,
                  time_embeddings[:, None, :],
                  hidden_states,
              ]

              if self.prd_embedding is not None:
                     lowercase		:Tuple       							=				self.prd_embedding.to(hidden_states.dtype				).expand(_SCREAMING_SNAKE_CASE  ,     -1  ,     -1				)
                     additional_embeds.append(_SCREAMING_SNAKE_CASE				)

              lowercase		:Dict       							=				torch.cat(
                  _SCREAMING_SNAKE_CASE  ,     dim=1  ,     )

              # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens
              lowercase		:Tuple       							=				additional_embeddings_len + proj_embeddings.shape[1] + 1
              if positional_embeddings.shape[1] < hidden_states.shape[1]:
                     lowercase		:Optional[Any]       							=				F.pad(
                         _SCREAMING_SNAKE_CASE  ,     (
                             0,
                             0,
                             additional_embeddings_len,
                             self.prd_embedding.shape[1] if self.prd_embedding is not None else 0,
                         )  ,     value=0.0  ,     )

              lowercase		:List[str]       							=				hidden_states + positional_embeddings

              if attention_mask is not None:
                     lowercase		:Optional[int]       							=				(1 - attention_mask.to(hidden_states.dtype				)) * -1_0_0_0_0.0
                     lowercase		:Tuple       							=				F.pad(_SCREAMING_SNAKE_CASE  ,     (0, self.additional_embeddings)  ,     value=0.0				)
                     lowercase		:Optional[Any]       							=				(attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype				)
                     lowercase		:int       							=				attention_mask.repeat_interleave(self.config.num_attention_heads  ,     dim=0				)

              if self.norm_in is not None:
                     lowercase		:List[str]       							=				self.norm_in(_SCREAMING_SNAKE_CASE				)

              for block in self.transformer_blocks:
                     lowercase		:Optional[int]       							=				block(_SCREAMING_SNAKE_CASE  ,     attention_mask=_SCREAMING_SNAKE_CASE				)

              lowercase		:List[Any]       							=				self.norm_out(_SCREAMING_SNAKE_CASE				)

              if self.prd_embedding is not None:
                     lowercase		:str       							=				hidden_states[:, -1]
              else:
                     lowercase		:Optional[Any]       							=				hidden_states[:, additional_embeddings_len:]

              lowercase		:List[str]       							=				self.proj_to_clip_embeddings(_SCREAMING_SNAKE_CASE				)

              if not return_dict:
                     return (predicted_image_embedding,)

              return PriorTransformerOutput(predicted_image_embedding=_SCREAMING_SNAKE_CASE				)







       def  __snake_case						(   self  :    Dict  ,     snake_case__  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:Any       							=				(prior_latents * self.clip_std) + self.clip_mean
              return prior_latents





"""simple docstring"""



UpperCAmelCase      		=			{str(digit): digit**5 for digit in range(10)}






def       lowerCamelCase					(a_						:int)     ->							int:
       return sum(DIGITS_FIFTH_POWER[digit] for digit in str(a_))






def       lowerCamelCase					()     ->							int:
       return sum(
           number
           for number in range(1000				,				100_0000)
           if number == digits_fifth_powers_sum(a_))


if __name__ == "__main__":
 print(solution())

