






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			(		lowercase_      ):



				"""simple docstring"""



				_lowercase				:   torch.FloatTensor


class    _lowerCAmelCase			(		lowercase_     ,      lowercase_      ):



				"""simple docstring"""



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






											'''simple docstring'''







											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(UpperCamelCase__			,		UpperCamelCase__			,		0)
											snake_case__	  =       TimestepEmbedding(UpperCamelCase__			,		UpperCamelCase__			,		out_dim=UpperCamelCase__			,		act_fn=UpperCamelCase__)

											snake_case__	  =       nn.Linear(UpperCamelCase__			,		UpperCamelCase__)

											if embedding_proj_norm_type is None:
																		snake_case__	  =       None
											elif embedding_proj_norm_type == "layer":
																		snake_case__	  =       nn.LayerNorm(UpperCamelCase__)
											else:
																		raise ValueError(F'''unsupported embedding_proj_norm_type: {embedding_proj_norm_type}''')

											snake_case__	  =       nn.Linear(UpperCamelCase__			,		UpperCamelCase__)

											if encoder_hid_proj_type is None:
																		snake_case__	  =       None
											elif encoder_hid_proj_type == "linear":
																		snake_case__	  =       nn.Linear(UpperCamelCase__			,		UpperCamelCase__)
											else:
																		raise ValueError(F'''unsupported encoder_hid_proj_type: {encoder_hid_proj_type}''')

											snake_case__	  =       nn.Parameter(torch.zeros(1			,		num_embeddings + additional_embeddings			,		UpperCamelCase__))

											if added_emb_type == "prd":
																		snake_case__	  =       nn.Parameter(torch.zeros(1			,		1			,		UpperCamelCase__))
											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(
											            UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__			,		dropout=UpperCamelCase__			,		activation_fn="""gelu"""			,		attention_bias=UpperCamelCase__			,		)
											        for d in range(UpperCamelCase__)
											    ])

											if norm_in_type == "layer":
																		snake_case__	  =       nn.LayerNorm(UpperCamelCase__)
											elif norm_in_type is None:
																		snake_case__	  =       None
											else:
																		raise ValueError(F'''Unsupported norm_in_type: {norm_in_type}.''')

											snake_case__	  =       nn.LayerNorm(UpperCamelCase__)

											snake_case__	  =       nn.Linear(UpperCamelCase__			,		UpperCamelCase__)

											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"""			,		UpperCamelCase__			,		persistent=UpperCamelCase__)

											snake_case__	  =       nn.Parameter(torch.zeros(1			,		UpperCamelCase__))
											snake_case__	  =       nn.Parameter(torch.zeros(1			,		UpperCamelCase__))



				@property
				# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
				def 					__magic_name__							(      self    :    Optional[int]):






											'''simple docstring'''







											snake_case__	  =       {}

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

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

																		return processors

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

											return processors



				def 					__magic_name__							(      self    :    Optional[Any]			,		UpperCamelCase__    :    Union[AttentionProcessor, Dict[str, AttentionProcessor]]):






											'''simple docstring'''







											snake_case__	  =       len(self.attn_processors.keys())

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

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

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



				def 					__magic_name__							(      self    :    Dict):






											'''simple docstring'''







											self.set_attn_processor(AttnProcessor())



				def 					__magic_name__							(      self    :    List[str]			,		UpperCamelCase__    :    Dict			,		UpperCamelCase__    :    Union[torch.Tensor, float, int]			,		UpperCamelCase__    :    torch.FloatTensor			,		UpperCamelCase__    :    Optional[torch.FloatTensor] = None			,		UpperCamelCase__    :    Optional[torch.BoolTensor] = None			,		UpperCamelCase__    :    bool = True			,		):






											'''simple docstring'''







											snake_case__	  =       hidden_states.shape[0]

											snake_case__	  =       timestep
											if not torch.is_tensor(UpperCamelCase__):
																		snake_case__	  =       torch.tensor([timesteps]			,		dtype=torch.long			,		device=hidden_states.device)
											elif torch.is_tensor(UpperCamelCase__) 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(UpperCamelCase__			,		dtype=timesteps.dtype			,		device=timesteps.device)

											snake_case__	  =       self.time_proj(UpperCamelCase__)

											# 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(UpperCamelCase__)

											if self.embedding_proj_norm is not None:
																		snake_case__	  =       self.embedding_proj_norm(UpperCamelCase__)

											snake_case__	  =       self.embedding_proj(UpperCamelCase__)
											if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
																		snake_case__	  =       self.encoder_hidden_states_proj(UpperCamelCase__)
											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(UpperCamelCase__)

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

											snake_case__	  =       []
											snake_case__	  =       0

											if encoder_hidden_states is not None:
																		additional_embeds.append(UpperCamelCase__)
																		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(UpperCamelCase__			,		-1			,		-1)
																		additional_embeds.append(UpperCamelCase__)

											snake_case__	  =       torch.cat(
											    UpperCamelCase__			,		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(
																		    UpperCamelCase__			,		(
																		        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(UpperCamelCase__			,		(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(UpperCamelCase__)

											for block in self.transformer_blocks:
																		snake_case__	  =       block(UpperCamelCase__			,		attention_mask=UpperCamelCase__)

											snake_case__	  =       self.norm_out(UpperCamelCase__)

											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(UpperCamelCase__)

											if not return_dict:
																		return (predicted_image_embedding,)

											return PriorTransformerOutput(predicted_image_embedding=UpperCamelCase__)



				def 					__magic_name__							(      self    :    str			,		UpperCamelCase__    :    Any):






											'''simple docstring'''







											snake_case__	  =       (prior_latents * self.clip_std) + self.clip_mean
											return prior_latents








from collections.abc import Callable

def _UpperCAmelCase   (	a						:	Callable[[float], float]  ,					a						:	float  ,					a						:	float ):
							snake_case__	  =       a
							snake_case__	  =       b
							if function(a ) == 0:  # one of the a or b is a root for the function
														return a
							elif function(a ) == 0:
														return b
							elif (
							    function(a ) * function(a ) > 0
							):  # if none of these are root and they are both positive or negative,
														# then this algorithm can't find the root
														raise ValueError("""could not find root in given interval.""" )
							else:
														snake_case__	  =       start + (end - start) / 2.0
														while abs(start - mid ) > 10**-7:  # until precisely equals to 10^-7
																					if function(a ) == 0:
																												return mid
																					elif function(a ) * function(a ) < 0:
																												snake_case__	  =       mid
																					else:
																												snake_case__	  =       mid
																					snake_case__	  =       start + (end - start) / 2.0
														return mid

def _UpperCAmelCase   (	a						:	float ):
							return x**3 - 2 * x - 5


if __name__ == "__main__":
	print(bisection(f, 1, 1_0_0_0))

	import doctest

	doctest.testmod()
