



"""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        _UpperCAmelCase       (			lowerCAmelCase__):
 _lowerCAmelCase   :			torch.FloatTensor





class        _UpperCAmelCase       (			lowerCAmelCase__    ,     lowerCAmelCase__):

 @register_to_config
 def __init__(     self		:       Optional[Any]  ,     lowercase_		:       int = 32  ,     lowercase_		:       int = 64  ,     lowercase_		:       int = 20  ,     lowercase_		:       int = 768  ,     lowercase_		:       Dict=77  ,     lowercase_		:       Any=4  ,     lowercase_		:       float = 0.0  ,     lowercase_		:       str = "silu"  ,     lowercase_		:       Optional[str] = None  ,     lowercase_		:       Optional[str] = None  ,     lowercase_		:       Optional[str] = "linear"  ,     lowercase_		:       Optional[str] = "prd"  ,     lowercase_		:       Optional[int] = None  ,     lowercase_		:       Optional[int] = None  ,     lowercase_		:       Optional[int] = None  ,     ):
     super().__init__()
     snake_case_ :     Optional[int]				=	num_attention_heads
     snake_case_ :     Union[str, Any]				=	attention_head_dim
     snake_case_ :     List[Any]				=	num_attention_heads * attention_head_dim
     snake_case_ :     str				=	additional_embeddings

     snake_case_ :     int				=	time_embed_dim or inner_dim
     snake_case_ :     int				=	embedding_proj_dim or embedding_dim
     snake_case_ :     List[str]				=	clip_embed_dim or embedding_dim

     snake_case_ :     Union[str, Any]				=	Timesteps(lowercase_  ,     lowercase_  ,     0				)
     snake_case_ :     Any				=	TimestepEmbedding(lowercase_  ,     lowercase_  ,     out_dim=lowercase_  ,     act_fn=lowercase_				)

     snake_case_ :     Optional[Any]				=	nn.Linear(lowercase_  ,     lowercase_				)

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

     snake_case_ :     Dict				=	nn.Linear(lowercase_  ,     lowercase_				)

     if encoder_hid_proj_type is None:
         snake_case_ :     str				=	None
     elif encoder_hid_proj_type == "linear":
         snake_case_ :     Union[str, Any]				=	nn.Linear(lowercase_  ,     lowercase_				)
     else:
         raise ValueError(f"unsupported encoder_hid_proj_type: {encoder_hid_proj_type}"				)

     snake_case_ :     Tuple				=	nn.Parameter(torch.zeros(1  ,     num_embeddings + additional_embeddings  ,     lowercase_				)				)

     if added_emb_type == "prd":
         snake_case_ :     Optional[int]				=	nn.Parameter(torch.zeros(1  ,     1  ,     lowercase_				)				)
     elif added_emb_type is None:
         snake_case_ :     Tuple				=	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_ :     Union[str, Any]				=	nn.ModuleList(
         [
             BasicTransformerBlock(
                 lowercase_  ,     lowercase_  ,     lowercase_  ,     dropout=lowercase_  ,     activation_fn='''gelu'''  ,     attention_bias=lowercase_  ,     )
             for d in range(lowercase_				)
         ]				)

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

     snake_case_ :     Any				=	nn.LayerNorm(lowercase_				)

     snake_case_ :     Optional[Any]				=	nn.Linear(lowercase_  ,     lowercase_				)

     snake_case_ :     Dict				=	torch.full(
         [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings]  ,     -1_00_00.0				)
     causal_attention_mask.triu_(1				)
     snake_case_ :     Optional[Any]				=	causal_attention_mask[None, ...]
     self.register_buffer('''causal_attention_mask'''  ,     lowercase_  ,     persistent=lowercase_				)

     snake_case_ :     List[str]				=	nn.Parameter(torch.zeros(1  ,     lowercase_				)				)
     snake_case_ :     Tuple				=	nn.Parameter(torch.zeros(1  ,     lowercase_				)				)

 @property
 # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
 def 						_snake_case					(     self		:       Union[str, Any]				):
     snake_case_ :     Optional[int]				=	{}

     def fn_recursive_add_processors(lowercase_		:       str  ,     lowercase_		:       torch.nn.Module  ,     lowercase_		:       Dict[str, AttentionProcessor]				):
         if hasattr(lowercase_  ,     '''set_processor'''				):
             snake_case_ :     List[Any]				=	module.processor

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

         return processors

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

     return processors

 def 						_snake_case					(     self		:       Union[str, Any]  ,     lowercase_		:       Union[AttentionProcessor, Dict[str, AttentionProcessor]]				):
     snake_case_ :     str				=	len(self.attn_processors.keys()				)

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

     def fn_recursive_attn_processor(lowercase_		:       str  ,     lowercase_		:       torch.nn.Module  ,     lowercase_		:       Optional[int]				):
         if hasattr(lowercase_  ,     '''set_processor'''				):
             if not isinstance(lowercase_  ,     lowercase_				):
                 module.set_processor(lowercase_				)
             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}"  ,     lowercase_  ,     lowercase_				)

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

 def 						_snake_case					(     self		:       int				):
     self.set_attn_processor(AttnProcessor()				)

 def 						_snake_case					(     self		:       Union[str, Any]  ,     lowercase_		:       str  ,     lowercase_		:       Union[torch.Tensor, float, int]  ,     lowercase_		:       torch.FloatTensor  ,     lowercase_		:       Optional[torch.FloatTensor] = None  ,     lowercase_		:       Optional[torch.BoolTensor] = None  ,     lowercase_		:       bool = True  ,     ):
     snake_case_ :     Dict				=	hidden_states.shape[0]

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

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

     snake_case_ :     Tuple				=	self.time_proj(lowercase_				)

     # 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_ :     Any				=	timesteps_projected.to(dtype=self.dtype				)
     snake_case_ :     List[str]				=	self.time_embedding(lowercase_				)

     if self.embedding_proj_norm is not None:
         snake_case_ :     List[Any]				=	self.embedding_proj_norm(lowercase_				)

     snake_case_ :     List[Any]				=	self.embedding_proj(lowercase_				)
     if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
         snake_case_ :     List[str]				=	self.encoder_hidden_states_proj(lowercase_				)
     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_ :     Dict				=	self.proj_in(lowercase_				)

     snake_case_ :     List[Any]				=	self.positional_embedding.to(hidden_states.dtype				)

     snake_case_ :     Any				=	[]
     snake_case_ :     int				=	0

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

     if len(proj_embeddings.shape				) == 2:
         snake_case_ :     List[Any]				=	proj_embeddings[:, None, :]

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

     snake_case_ :     Tuple				=	additional_embeds + [
         proj_embeddings,
         time_embeddings[:, None, :],
         hidden_states,
     ]

     if self.prd_embedding is not None:
         snake_case_ :     Optional[Any]				=	self.prd_embedding.to(hidden_states.dtype				).expand(lowercase_  ,     -1  ,     -1				)
         additional_embeds.append(lowercase_				)

     snake_case_ :     str				=	torch.cat(
         lowercase_  ,     dim=1  ,     )

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

     snake_case_ :     List[str]				=	hidden_states + positional_embeddings

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

     if self.norm_in is not None:
         snake_case_ :     Union[str, Any]				=	self.norm_in(lowercase_				)

     for block in self.transformer_blocks:
         snake_case_ :     Union[str, Any]				=	block(lowercase_  ,     attention_mask=lowercase_				)

     snake_case_ :     Any				=	self.norm_out(lowercase_				)

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

     snake_case_ :     int				=	self.proj_to_clip_embeddings(lowercase_				)

     if not return_dict:
         return (predicted_image_embedding,)

     return PriorTransformerOutput(predicted_image_embedding=lowercase_				)






 def 						_snake_case					(     self		:       int  ,     lowercase_		:       Optional[int]				):
     snake_case_ :     List[Any]				=	(prior_latents * self.clip_std) + self.clip_mean
     return prior_latents










"""simple docstring"""

from __future__ import annotations




def __lowercase			(							_a		,			_a		,			_a		,			):
    if (stress, tangential_force, area).count(0    ) != 1:
        raise ValueError('''You cannot supply more or less than 2 values'''    )
    elif stress < 0:
        raise ValueError('''Stress cannot be negative'''    )
    elif tangential_force < 0:
        raise ValueError('''Tangential Force cannot be negative'''    )
    elif area < 0:
        raise ValueError('''Area cannot be negative'''    )
    elif stress == 0:
        return (
            "stress",
            tangential_force / area,
        )
    elif tangential_force == 0:
        return (
            "tangential_force",
            stress * area,
        )
    else:
        return (
            "area",
            tangential_force / stress,
        )


if __name__ == "__main__":
     import doctest

     doctest.testmod()





