





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

   '''simple docstring'''


   a		:				Optional[int]  						=						42






class        lowercase__			(      A__     ,							A__							):

   '''simple docstring'''


   @register_to_config
   def __init__(       self,						__magic_name__ = 32,						__magic_name__ = 64,						__magic_name__ = 20,						__magic_name__ = 768,						__magic_name__=77,						__magic_name__=4,						__magic_name__ = 0.0,						__magic_name__ = "silu",						__magic_name__ = None,						__magic_name__ = None,						__magic_name__ = "linear",						__magic_name__ = "prd",						__magic_name__ = None,						__magic_name__ = None,						__magic_name__ = None,						)					-> Any:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__				:							Tuple			  = num_attention_heads
       UpperCamelCase__				:							Any			  = attention_head_dim
       UpperCamelCase__				:							Optional[Any]			  = num_attention_heads * attention_head_dim
       UpperCamelCase__				:							Dict			  = additional_embeddings

       UpperCamelCase__				:							Optional[Any]			  = time_embed_dim or inner_dim
       UpperCamelCase__				:							int			  = embedding_proj_dim or embedding_dim
       UpperCamelCase__				:							Any			  = clip_embed_dim or embedding_dim

       UpperCamelCase__				:							Optional[int]			  = Timesteps(snake_case_,						snake_case_,						0				)
       UpperCamelCase__				:							Optional[Any]			  = TimestepEmbedding(snake_case_,						snake_case_,						out_dim=snake_case_,						act_fn=snake_case_				)

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

       if embedding_proj_norm_type is None:
           UpperCamelCase__				:							int			  = None
       elif embedding_proj_norm_type == "layer":
           UpperCamelCase__				:							str			  = nn.LayerNorm(snake_case_				)
       else:
           raise ValueError(f"unsupported embedding_proj_norm_type: {embedding_proj_norm_type}"				)

       UpperCamelCase__				:							List[Any]			  = nn.Linear(snake_case_,						snake_case_				)

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

       UpperCamelCase__				:							Any			  = nn.Parameter(torch.zeros(1,						num_embeddings + additional_embeddings,						snake_case_				)				)

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

       UpperCamelCase__				:							List[Any]			  = 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":
           UpperCamelCase__				:							Optional[int]			  = nn.LayerNorm(snake_case_				)
       elif norm_in_type is None:
           UpperCamelCase__				:							Optional[int]			  = None
       else:
           raise ValueError(f"Unsupported norm_in_type: {norm_in_type}."				)

       UpperCamelCase__				:							Optional[int]			  = nn.LayerNorm(snake_case_				)

       UpperCamelCase__				:							List[Any]			  = nn.Linear(snake_case_,						snake_case_				)

       UpperCamelCase__				:							int			  = torch.full(
           [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings],						-10000.0				)
       causal_attention_mask.triu_(1				)
       UpperCamelCase__				:							List[Any]			  = causal_attention_mask[None, ...]
       self.register_buffer('''causal_attention_mask''',						snake_case_,						persistent=snake_case_				)

       UpperCamelCase__				:							int			  = nn.Parameter(torch.zeros(1,						snake_case_				)				)
       UpperCamelCase__				:							Dict			  = nn.Parameter(torch.zeros(1,						snake_case_				)				)




   @property
   # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
   def  UpperCamelCase__    (       self				)					-> Dict[str, AttentionProcessor]:






       """simple docstring"""
       UpperCamelCase__				:							List[str]			  = {}

       def fn_recursive_add_processors(__magic_name__,						__magic_name__,						__magic_name__				):
           if hasattr(snake_case_,						'''set_processor'''				):
               UpperCamelCase__				:							int			  = 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  UpperCamelCase__    (       self,						__magic_name__				)					-> Dict:






       """simple docstring"""
       UpperCamelCase__				:							Optional[int]			  = 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(__magic_name__,						__magic_name__,						__magic_name__				):
           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  UpperCamelCase__    (       self				)					-> Optional[Any]:






       """simple docstring"""
       self.set_attn_processor(AttnProcessor()				)




   def  UpperCamelCase__    (       self,						__magic_name__,						__magic_name__,						__magic_name__,						__magic_name__ = None,						__magic_name__ = None,						__magic_name__ = True,						)					-> List[str]:






       """simple docstring"""
       UpperCamelCase__				:							List[str]			  = hidden_states.shape[0]

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

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

       UpperCamelCase__				:							Optional[Any]			  = 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.
       UpperCamelCase__				:							Tuple			  = timesteps_projected.to(dtype=self.dtype				)
       UpperCamelCase__				:							Any			  = self.time_embedding(snake_case_				)

       if self.embedding_proj_norm is not None:
           UpperCamelCase__				:							Optional[Any]			  = self.embedding_proj_norm(snake_case_				)

       UpperCamelCase__				:							List[str]			  = self.embedding_proj(snake_case_				)
       if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
           UpperCamelCase__				:							Union[str, Any]			  = 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'''				)

       UpperCamelCase__				:							Dict			  = self.proj_in(snake_case_				)

       UpperCamelCase__				:							int			  = self.positional_embedding.to(hidden_states.dtype				)

       UpperCamelCase__				:							List[str]			  = []
       UpperCamelCase__				:							List[Any]			  = 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:
           UpperCamelCase__				:							Tuple			  = proj_embeddings[:, None, :]

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

       UpperCamelCase__				:							Union[str, Any]			  = additional_embeds + [
           proj_embeddings,
           time_embeddings[:, None, :],
           hidden_states,
       ]

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

       UpperCamelCase__				:							Union[str, Any]			  = 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
       UpperCamelCase__				:							Optional[int]			  = additional_embeddings_len + proj_embeddings.shape[1] + 1
       if positional_embeddings.shape[1] < hidden_states.shape[1]:
           UpperCamelCase__				:							List[Any]			  = 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,						)

       UpperCamelCase__				:							Union[str, Any]			  = hidden_states + positional_embeddings

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

       if self.norm_in is not None:
           UpperCamelCase__				:							Optional[Any]			  = self.norm_in(snake_case_				)

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

       UpperCamelCase__				:							Dict			  = self.norm_out(snake_case_				)

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

       UpperCamelCase__				:							Union[str, Any]			  = self.proj_to_clip_embeddings(snake_case_				)

       if not return_dict:
           return (predicted_image_embedding,)

       return PriorTransformerOutput(predicted_image_embedding=snake_case_				)




   def  UpperCamelCase__    (       self,						__magic_name__				)					-> Tuple:






       """simple docstring"""
       UpperCamelCase__				:							Any			  = (prior_latents * self.clip_std) + self.clip_mean
       return prior_latents



"""simple docstring"""
from scipy.stats import pearsonr

import datasets


SCREAMING_SNAKE_CASE_					     =	'''
Pearson correlation coefficient and p-value for testing non-correlation.
The Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.
The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.
'''


SCREAMING_SNAKE_CASE_					     =	'''
Args:
    predictions (`list` of `int`): Predicted class labels, as returned by a model.
    references (`list` of `int`): Ground truth labels.
    return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.

Returns:
    pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.
    p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.

Examples:

    Example 1-A simple example using only predictions and references.
        >>> pearsonr_metric = datasets.load_metric("pearsonr")
        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])
        >>> print(round(results[\'pearsonr\'], 2))
        -0.74

    Example 2-The same as Example 1, but that also returns the `p-value`.
        >>> pearsonr_metric = datasets.load_metric("pearsonr")
        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)
        >>> print(sorted(list(results.keys())))
        [\'p-value\', \'pearsonr\']
        >>> print(round(results[\'pearsonr\'], 2))
        -0.74
        >>> print(round(results[\'p-value\'], 2))
        0.15
'''


SCREAMING_SNAKE_CASE_					     =	'''
@article{2020SciPy-NMeth,
author  = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and
      Haberland, Matt and Reddy, Tyler and Cournapeau, David and
      Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and
      Bright, Jonathan and {van der Walt}, St{\'e}fan J. and
      Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and
      Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and
      Kern, Robert and Larson, Eric and Carey, C J and
      Polat, Ilhan and Feng, Yu and Moore, Eric W. and
      {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and
      Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and
      Harris, Charles R. and Archibald, Anne M. and
      Ribeiro, Antonio H. and Pedregosa, Fabian and
      {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},
title   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific
      Computing in Python}},
journal = {Nature Methods},
year    = {2020},
volume  = {17},
pages   = {261--272},
adsurl  = {https://rdcu.be/b08Wh},
doi = {10.1038/s41592-019-0686-2},
}
'''
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION				,				_KWARGS_DESCRIPTION )
class       lowerCAmelCase_		(				datasets.Metric ):



							'''simple docstring'''





							def    A__						( self				)       ->  Union[str, Any]:
													return datasets.MetricInfo(
													    description=_DESCRIPTION    ,				citation=_CITATION    ,				inputs_description=_KWARGS_DESCRIPTION    ,				features=datasets.Features(
													        {
													            """predictions""": datasets.Value("""float"""				),
													            """references""": datasets.Value("""float"""				),
													        }				)    ,				reference_urls=["""https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html"""]    ,				)




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_=False				)       ->  Dict:
													if return_pvalue:
																			__lowerCAmelCase     =  pearsonr(snake_case_    ,				snake_case_				)
																			return {"pearsonr": results[0], "p-value": results[1]}
													else:
																			return {"pearsonr": float(pearsonr(snake_case_    ,				snake_case_				)[0]				)}
