

'''simple docstring'''
from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxCrossAttnUpBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
    FlaxUpBlockaD,
)
@flax.struct.dataclass
class 					_snake_case		(__SCREAMING_SNAKE_CASE):
			__A  :	jnp.ndarray




@flax_register_to_config
class 					_snake_case		(nn.Module			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE):
			__A  :	int        =32
			__A  :	int        =4
			__A  :	int        =4
			__A  :	Tuple[str]        =(
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "DownBlock2D",
			)
			__A  :	Tuple[str]        =("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
			__A  :	Union[bool, Tuple[bool]]        =False
			__A  :	Tuple[int]        =(3_20, 6_40, 12_80, 12_80)
			__A  :	int        =2
			__A  :	Union[int, Tuple[int]]        =8
			__A  :	Optional[Union[int, Tuple[int]]]        =None
			__A  :	int        =12_80
			__A  :	float        =0.0
			__A  :	bool        =False
			__A  :	jnp.dtype        =jnp.floataa
			__A  :	bool        =True
			__A  :	int        =0
			__A  :	bool        =False






			def 		UpperCamelCase__							(  self			,_snake_case     ):
				# init input tensors
				UpperCAmelCase_     :      List[str]						=  (1, self.in_channels, self.sample_size, self.sample_size)
				UpperCAmelCase_     :      Tuple						=  jnp.zeros(_snake_case			,dtype=jnp.floataa     )
				UpperCAmelCase_     :      Optional[Any]						=  jnp.ones((1,)			,dtype=jnp.intaa     )
				UpperCAmelCase_     :      Optional[Any]						=  jnp.zeros((1, 1, self.cross_attention_dim)			,dtype=jnp.floataa     )

				UpperCAmelCase_					,					UpperCAmelCase_     :      Tuple						=  jax.random.split(_snake_case     )
				UpperCAmelCase_     :      int						=  {"params": params_rng, "dropout": dropout_rng}

				return self.init(_snake_case			,_snake_case			,_snake_case			,_snake_case     )["params"]






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      List[str]						=  self.block_out_channels
				UpperCAmelCase_     :      Tuple						=  block_out_channels[0] * 4

				if self.num_attention_heads is not None:
					raise ValueError(
					    "At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19."     )

				# If `num_attention_heads` is not defined (which is the case for most models)
				# it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
				# The reason for this behavior is to correct for incorrectly named variables that were introduced
				# when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
				# Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
				# which is why we correct for the naming here.
				UpperCAmelCase_     :      Union[str, Any]						=  self.num_attention_heads or self.attention_head_dim

				# input
				UpperCAmelCase_     :      List[Any]						=  nn.Conv(
				    block_out_channels[0]			,kernel_size=(3, 3)			,strides=(1, 1)			,padding=((1, 1), (1, 1))			,dtype=self.dtype			,)

				# time
				UpperCAmelCase_     :      Tuple						=  FlaxTimesteps(
				    block_out_channels[0]			,flip_sin_to_cos=self.flip_sin_to_cos			,freq_shift=self.config.freq_shift     )
				UpperCAmelCase_     :      int						=  FlaxTimestepEmbedding(_snake_case			,dtype=self.dtype     )

				UpperCAmelCase_     :      str						=  self.only_cross_attention
				if isinstance(_snake_case			,_snake_case     ):
					UpperCAmelCase_     :      Dict						=  (only_cross_attention,) * len(self.down_block_types     )

				if isinstance(_snake_case			,_snake_case     ):
					UpperCAmelCase_     :      List[str]						=  (num_attention_heads,) * len(self.down_block_types     )

				# down
				UpperCAmelCase_     :      int						=  []
				UpperCAmelCase_     :      Optional[Any]						=  block_out_channels[0]
				for i, down_block_type in enumerate(self.down_block_types     ):
					UpperCAmelCase_     :      Any						=  output_channel
					UpperCAmelCase_     :      List[Any]						=  block_out_channels[i]
					UpperCAmelCase_     :      str						=  i == len(_snake_case     ) - 1

					if down_block_type == "CrossAttnDownBlock2D":
						UpperCAmelCase_     :      Optional[Any]						=  FlaxCrossAttnDownBlockaD(
						    in_channels=_snake_case			,out_channels=_snake_case			,dropout=self.dropout			,num_layers=self.layers_per_block			,num_attention_heads=num_attention_heads[i]			,add_downsample=not is_final_block			,use_linear_projection=self.use_linear_projection			,only_cross_attention=only_cross_attention[i]			,use_memory_efficient_attention=self.use_memory_efficient_attention			,dtype=self.dtype			,)
					else:
						UpperCAmelCase_     :      Optional[int]						=  FlaxDownBlockaD(
						    in_channels=_snake_case			,out_channels=_snake_case			,dropout=self.dropout			,num_layers=self.layers_per_block			,add_downsample=not is_final_block			,dtype=self.dtype			,)

					down_blocks.append(_snake_case     )
				UpperCAmelCase_     :      Any						=  down_blocks

				# mid
				UpperCAmelCase_     :      Union[str, Any]						=  FlaxUNetMidBlockaDCrossAttn(
				    in_channels=block_out_channels[-1]			,dropout=self.dropout			,num_attention_heads=num_attention_heads[-1]			,use_linear_projection=self.use_linear_projection			,use_memory_efficient_attention=self.use_memory_efficient_attention			,dtype=self.dtype			,)

				# up
				UpperCAmelCase_     :      str						=  []
				UpperCAmelCase_     :      Union[str, Any]						=  list(reversed(_snake_case     )     )
				UpperCAmelCase_     :      Union[str, Any]						=  list(reversed(_snake_case     )     )
				UpperCAmelCase_     :      str						=  list(reversed(_snake_case     )     )
				UpperCAmelCase_     :      Any						=  reversed_block_out_channels[0]
				for i, up_block_type in enumerate(self.up_block_types     ):
					UpperCAmelCase_     :      Tuple						=  output_channel
					UpperCAmelCase_     :      Any						=  reversed_block_out_channels[i]
					UpperCAmelCase_     :      List[Any]						=  reversed_block_out_channels[min(i + 1			,len(_snake_case     ) - 1     )]

					UpperCAmelCase_     :      Optional[Any]						=  i == len(_snake_case     ) - 1

					if up_block_type == "CrossAttnUpBlock2D":
						UpperCAmelCase_     :      Dict						=  FlaxCrossAttnUpBlockaD(
						    in_channels=_snake_case			,out_channels=_snake_case			,prev_output_channel=_snake_case			,num_layers=self.layers_per_block + 1			,num_attention_heads=reversed_num_attention_heads[i]			,add_upsample=not is_final_block			,dropout=self.dropout			,use_linear_projection=self.use_linear_projection			,only_cross_attention=only_cross_attention[i]			,use_memory_efficient_attention=self.use_memory_efficient_attention			,dtype=self.dtype			,)
					else:
						UpperCAmelCase_     :      str						=  FlaxUpBlockaD(
						    in_channels=_snake_case			,out_channels=_snake_case			,prev_output_channel=_snake_case			,num_layers=self.layers_per_block + 1			,add_upsample=not is_final_block			,dropout=self.dropout			,dtype=self.dtype			,)

					up_blocks.append(_snake_case     )
					UpperCAmelCase_     :      List[Any]						=  output_channel
				UpperCAmelCase_     :      List[str]						=  up_blocks

				# out
				UpperCAmelCase_     :      Optional[int]						=  nn.GroupNorm(num_groups=32			,epsilon=1E-5     )
				UpperCAmelCase_     :      str						=  nn.Conv(
				    self.out_channels			,kernel_size=(3, 3)			,strides=(1, 1)			,padding=((1, 1), (1, 1))			,dtype=self.dtype			,)






			def __call__(  self			,_snake_case			,_snake_case			,_snake_case			,_snake_case=None			,_snake_case=None			,_snake_case = True			,_snake_case = False			,):
				# 1. time
				if not isinstance(_snake_case			,jnp.ndarray     ):
					UpperCAmelCase_     :      Optional[int]						=  jnp.array([timesteps]			,dtype=jnp.intaa     )
				elif isinstance(_snake_case			,jnp.ndarray     ) and len(timesteps.shape     ) == 0:
					UpperCAmelCase_     :      List[str]						=  timesteps.astype(dtype=jnp.floataa     )
					UpperCAmelCase_     :      Optional[int]						=  jnp.expand_dims(_snake_case			,0     )

				UpperCAmelCase_     :      Union[str, Any]						=  self.time_proj(_snake_case     )
				UpperCAmelCase_     :      Optional[Any]						=  self.time_embedding(_snake_case     )

				# 2. pre-process
				UpperCAmelCase_     :      Optional[int]						=  jnp.transpose(_snake_case			,(0, 2, 3, 1)     )
				UpperCAmelCase_     :      Union[str, Any]						=  self.conv_in(_snake_case     )

				# 3. down
				UpperCAmelCase_     :      int						=  (sample,)
				for down_block in self.down_blocks:
					if isinstance(_snake_case			,_snake_case     ):
						UpperCAmelCase_					,					UpperCAmelCase_     :      Union[str, Any]						=  down_block(_snake_case			,_snake_case			,_snake_case			,deterministic=not train     )
					else:
						UpperCAmelCase_					,					UpperCAmelCase_     :      Optional[Any]						=  down_block(_snake_case			,_snake_case			,deterministic=not train     )
					down_block_res_samples += res_samples

				if down_block_additional_residuals is not None:
					UpperCAmelCase_     :      int						=  ()

					for down_block_res_sample, down_block_additional_residual in zip(
					    _snake_case			,_snake_case     ):
						down_block_res_sample += down_block_additional_residual
						new_down_block_res_samples += (down_block_res_sample,)

					UpperCAmelCase_     :      Optional[Any]						=  new_down_block_res_samples

				# 4. mid
				UpperCAmelCase_     :      Dict						=  self.mid_block(_snake_case			,_snake_case			,_snake_case			,deterministic=not train     )

				if mid_block_additional_residual is not None:
					sample += mid_block_additional_residual

				# 5. up
				for up_block in self.up_blocks:
					UpperCAmelCase_     :      List[str]						=  down_block_res_samples[-(self.layers_per_block + 1) :]
					UpperCAmelCase_     :      Tuple						=  down_block_res_samples[: -(self.layers_per_block + 1)]
					if isinstance(_snake_case			,_snake_case     ):
						UpperCAmelCase_     :      Dict						=  up_block(
						    _snake_case			,temb=_snake_case			,encoder_hidden_states=_snake_case			,res_hidden_states_tuple=_snake_case			,deterministic=not train			,)
					else:
						UpperCAmelCase_     :      Tuple						=  up_block(_snake_case			,temb=_snake_case			,res_hidden_states_tuple=_snake_case			,deterministic=not train     )

        # 6. post-process
				UpperCAmelCase_     :      Optional[int]						=  self.conv_norm_out(_snake_case     )
				UpperCAmelCase_     :      List[Any]						=  nn.silu(_snake_case     )
				UpperCAmelCase_     :      str						=  self.conv_out(_snake_case     )
				UpperCAmelCase_     :      str						=  jnp.transpose(_snake_case			,(0, 3, 1, 2)     )

				if not return_dict:
					return (sample,)

				return FlaxUNetaDConditionOutput(sample=_snake_case     )





import datasets


__a    :	Optional[Any]         =    """\
@InProceedings{conneau2018xnli,
  author = \"Conneau, Alexis
                 and Rinott, Ruty
                 and Lample, Guillaume
                 and Williams, Adina
                 and Bowman, Samuel R.
                 and Schwenk, Holger
                 and Stoyanov, Veselin\",
  title = \"XNLI: Evaluating Cross-lingual Sentence Representations\",
  booktitle = \"Proceedings of the 2018 Conference on Empirical Methods
               in Natural Language Processing\",
  year = \"2018\",
  publisher = \"Association for Computational Linguistics\",
  location = \"Brussels, Belgium\",
}
"""

__a    :	Optional[Any]         =    """\
XNLI is a subset of a few thousand examples from MNLI which has been translated
into a 14 different languages (some low-ish resource). As with MNLI, the goal is
to predict textual entailment (does sentence A imply/contradict/neither sentence
B) and is a classification task (given two sentences, predict one of three
labels).
"""

__a    :	str         =    """
Computes XNLI score which is just simple accuracy.
Args:
    predictions: Predicted labels.
    references: Ground truth labels.
Returns:
    'accuracy': accuracy
Examples:

    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> xnli_metric = datasets.load_metric(\"xnli\")
    >>> results = xnli_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}
"""





def 							UpperCAmelCase		(       lowercase				,     lowercase				):







				"""simple docstring"""



				return (preds == labels).mean()




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,_KWARGS_DESCRIPTION					)
class   _UpperCamelCase (		datasets.Metric					):


			"""simple docstring"""






			def 			_SCREAMING_SNAKE_CASE     (  self		)	->   str:

							'''simple docstring'''


							return datasets.MetricInfo(
							    description=_DESCRIPTION ,  citation=_CITATION ,  inputs_description=_KWARGS_DESCRIPTION ,  features=datasets.Features(
							        {
							            '''predictions''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32'''		),
							            '''references''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32'''		),
							        }		) ,  codebase_urls=[] ,  reference_urls=[] ,  format='''numpy''' ,  )





			def 			_SCREAMING_SNAKE_CASE     (  self ,  lowerCAmelCase__ ,  lowerCAmelCase__		)	->   Dict:

							'''simple docstring'''


							return {"accuracy": simple_accuracy(lowerCAmelCase__ ,  lowerCAmelCase__		)}