





'''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 			_lowerCAmelCase		(			A__	):






     """simple docstring"""






     snake_case_						      =   42




class 			_lowerCAmelCase		(			A__							,     A__	):






     """simple docstring"""






     @register_to_config
     def __init__(   self							:			str   ,       __snake_case							:			int = 32   ,       __snake_case							:			int = 64   ,       __snake_case							:			int = 20   ,       __snake_case							:			int = 7_68   ,       __snake_case							:			Optional[int]=77   ,       __snake_case							:			Optional[int]=4   ,       __snake_case							:			float = 0.0   ,       __snake_case							:			str = "silu"   ,       __snake_case							:			Optional[str] = None   ,       __snake_case							:			Optional[str] = None   ,       __snake_case							:			Optional[str] = "linear"   ,       __snake_case							:			Optional[str] = "prd"   ,       __snake_case							:			Optional[int] = None   ,       __snake_case							:			Optional[int] = None   ,       __snake_case							:			Optional[int] = None   ,       )->	List[Any]:
        super().__init__()
        snake_case 						=   num_attention_heads
        snake_case 						=   attention_head_dim
        snake_case 						=   num_attention_heads * attention_head_dim
        snake_case 						=   additional_embeddings

        snake_case 						=   time_embed_dim or inner_dim
        snake_case 						=   embedding_proj_dim or embedding_dim
        snake_case 						=   clip_embed_dim or embedding_dim

        snake_case 						=   Timesteps(__snake_case   ,       __snake_case   ,       0						)
        snake_case 						=   TimestepEmbedding(__snake_case   ,       __snake_case   ,       out_dim=__snake_case   ,       act_fn=__snake_case						)

        snake_case 						=   nn.Linear(__snake_case   ,       __snake_case						)

        if embedding_proj_norm_type is None:
           snake_case 						=   None
        elif embedding_proj_norm_type == "layer":
           snake_case 						=   nn.LayerNorm(__snake_case						)
        else:
           raise ValueError(f'''unsupported embedding_proj_norm_type: {embedding_proj_norm_type}'''						)

        snake_case 						=   nn.Linear(__snake_case   ,       __snake_case						)

        if encoder_hid_proj_type is None:
           snake_case 						=   None
        elif encoder_hid_proj_type == "linear":
           snake_case 						=   nn.Linear(__snake_case   ,       __snake_case						)
        else:
           raise ValueError(f'''unsupported encoder_hid_proj_type: {encoder_hid_proj_type}'''						)

        snake_case 						=   nn.Parameter(torch.zeros(1   ,       num_embeddings + additional_embeddings   ,       __snake_case						)						)

        if added_emb_type == "prd":
           snake_case 						=   nn.Parameter(torch.zeros(1   ,       1   ,       __snake_case						)						)
        elif added_emb_type is None:
           snake_case 						=   None
        else:
           raise ValueError(
               f'''`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `\'prd\'` or `None`.'''						)

        snake_case 						=   nn.ModuleList(
            [
                BasicTransformerBlock(
                    __snake_case   ,       __snake_case   ,       __snake_case   ,       dropout=__snake_case   ,       activation_fn="""gelu"""   ,       attention_bias=__snake_case   ,       )
                for d in range(__snake_case						)
            ]						)

        if norm_in_type == "layer":
           snake_case 						=   nn.LayerNorm(__snake_case						)
        elif norm_in_type is None:
           snake_case 						=   None
        else:
           raise ValueError(f'''Unsupported norm_in_type: {norm_in_type}.'''						)

        snake_case 						=   nn.LayerNorm(__snake_case						)

        snake_case 						=   nn.Linear(__snake_case   ,       __snake_case						)

        snake_case 						=   torch.full(
            [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings]   ,       -1_00_00.0						)
        causal_attention_mask.triu_(1						)
        snake_case 						=   causal_attention_mask[None, ...]
        self.register_buffer("""causal_attention_mask"""   ,       __snake_case   ,       persistent=__snake_case						)

        snake_case 						=   nn.Parameter(torch.zeros(1   ,       __snake_case						)						)
        snake_case 						=   nn.Parameter(torch.zeros(1   ,       __snake_case						)						)



     @property
     # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
     def      lowerCAmelCase			(   self							:			Any						)->	Dict[str, AttentionProcessor]:
        snake_case 						=   {}

        def fn_recursive_add_processors(__snake_case							:			str   ,       __snake_case							:			torch.nn.Module   ,       __snake_case							:			Dict[str, AttentionProcessor]						):
           if hasattr(__snake_case   ,       """set_processor"""						):
              snake_case 						=   module.processor

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

           return processors

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

        return processors



     def      lowerCAmelCase			(   self							:			Optional[int]   ,       __snake_case							:			Union[AttentionProcessor, Dict[str, AttentionProcessor]]						)->	Optional[int]:
        snake_case 						=   len(self.attn_processors.keys()						)

        if isinstance(__snake_case   ,       __snake_case						) and len(__snake_case						) != count:
           raise ValueError(
               f'''A dict of processors was passed, but the number of processors {len(__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							:			str   ,       __snake_case							:			torch.nn.Module   ,       __snake_case							:			Tuple						):
           if hasattr(__snake_case   ,       """set_processor"""						):
              if not isinstance(__snake_case   ,       __snake_case						):
                 module.set_processor(__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}'''   ,       __snake_case   ,       __snake_case						)

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



     def      lowerCAmelCase			(   self							:			str						)->	str:
        self.set_attn_processor(AttnProcessor()						)



     def      lowerCAmelCase			(   self							:			str   ,       __snake_case							:			str   ,       __snake_case							:			Union[torch.Tensor, float, int]   ,       __snake_case							:			torch.FloatTensor   ,       __snake_case							:			Optional[torch.FloatTensor] = None   ,       __snake_case							:			Optional[torch.BoolTensor] = None   ,       __snake_case							:			bool = True   ,       )->	str:
        snake_case 						=   hidden_states.shape[0]

        snake_case 						=   timestep
        if not torch.is_tensor(__snake_case						):
           snake_case 						=   torch.tensor([timesteps]   ,       dtype=torch.long   ,       device=hidden_states.device						)
        elif torch.is_tensor(__snake_case						) and len(timesteps.shape						) == 0:
           snake_case 						=   timesteps[None].to(hidden_states.device						)

        # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
        snake_case 						=   timesteps * torch.ones(__snake_case   ,       dtype=timesteps.dtype   ,       device=timesteps.device						)

        snake_case 						=   self.time_proj(__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.
        snake_case 						=   timesteps_projected.to(dtype=self.dtype						)
        snake_case 						=   self.time_embedding(__snake_case						)

        if self.embedding_proj_norm is not None:
           snake_case 						=   self.embedding_proj_norm(__snake_case						)

        snake_case 						=   self.embedding_proj(__snake_case						)
        if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
           snake_case 						=   self.encoder_hidden_states_proj(__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"""						)

        snake_case 						=   self.proj_in(__snake_case						)

        snake_case 						=   self.positional_embedding.to(hidden_states.dtype						)

        snake_case 						=   []
        snake_case 						=   0

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

        if len(proj_embeddings.shape						) == 2:
           snake_case 						=   proj_embeddings[:, None, :]

        if len(hidden_states.shape						) == 2:
           snake_case 						=   hidden_states[:, None, :]

        snake_case 						=   additional_embeds + [
            proj_embeddings,
            time_embeddings[:, None, :],
            hidden_states,
        ]

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

        snake_case 						=   torch.cat(
            __snake_case   ,       dim=1   ,       )

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

        snake_case 						=   hidden_states + positional_embeddings

        if attention_mask is not None:
           snake_case 						=   (1 - attention_mask.to(hidden_states.dtype						)) * -1_00_00.0
           snake_case 						=   F.pad(__snake_case   ,       (0, self.additional_embeddings)   ,       value=0.0						)
           snake_case 						=   (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype						)
           snake_case 						=   attention_mask.repeat_interleave(self.config.num_attention_heads   ,       dim=0						)

        if self.norm_in is not None:
           snake_case 						=   self.norm_in(__snake_case						)

        for block in self.transformer_blocks:
           snake_case 						=   block(__snake_case   ,       attention_mask=__snake_case						)

        snake_case 						=   self.norm_out(__snake_case						)

        if self.prd_embedding is not None:
           snake_case 						=   hidden_states[:, -1]
        else:
           snake_case 						=   hidden_states[:, additional_embeddings_len:]

        snake_case 						=   self.proj_to_clip_embeddings(__snake_case						)

        if not return_dict:
           return (predicted_image_embedding,)

        return PriorTransformerOutput(predicted_image_embedding=__snake_case						)



     def      lowerCAmelCase			(   self							:			Optional[Any]   ,       __snake_case							:			Tuple						)->	str:
        snake_case 						=   (prior_latents * self.clip_std) + self.clip_mean
        return prior_latents








'''simple docstring'''




import re





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str		)		->  list:
   return [char.split() for char in re.split(r"""[^ a-z A-Z 0-9 \s]"""      ,    str_		)]





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str		)		->  str:
   snake_case 						=   split_input(str_		)
   return "".join(
       ["""""".join([char.capitalize() for char in sub_str]		) for sub_str in string_split]		)





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str      ,    __lowerCAmelCase       :							bool      ,    __lowerCAmelCase       :							str		)		->  str:
   try:
      snake_case 						=   split_input(__lowerCAmelCase		)
      if upper:
         snake_case 						=   """""".join(
             [
                 separator.join([char.upper() for char in sub_str]		)
                 for sub_str in string_split
             ]		)
      else:
         snake_case 						=   """""".join(
             [
                 separator.join([char.lower() for char in sub_str]		)
                 for sub_str in string_split
             ]		)
      return res_str
   except IndexError:
      return "not valid string"





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str		)		->  str:
   return to_simple_case(__lowerCAmelCase		)





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str		)		->  str:
   try:
      snake_case 						=   to_simple_case(__lowerCAmelCase		)
      return res_str[0].lower() + res_str[1:]
   except IndexError:
      return "not valid string"





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str      ,    __lowerCAmelCase       :							bool		)		->  str:
   return to_complex_case(__lowerCAmelCase      ,    __lowerCAmelCase      ,    """_"""		)





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str      ,    __lowerCAmelCase       :							bool		)		->  str:
   return to_complex_case(__lowerCAmelCase      ,    __lowerCAmelCase      ,    """-"""		)


if __name__ == "__main__":
 __import__("doctest").testmod()

