






'''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 	SCREAMING_SNAKE_CASE					(					_UpperCAmelCase       ):
			snake_case__  =				42





@flax_register_to_config
class 	SCREAMING_SNAKE_CASE					(					nn.Module	,   _UpperCAmelCase	,   _UpperCAmelCase       ):
			snake_case__  =				32
			snake_case__  =				4
			snake_case__  =				4
			snake_case__  =				(
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "DownBlock2D",
			)
			snake_case__  =				("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
			snake_case__  =				False
			snake_case__  =				(320, 640, 1280, 1280)
			snake_case__  =				2
			snake_case__  =				8
			snake_case__  =				None
			snake_case__  =				1280
			snake_case__  =				0.0
			snake_case__  =				False
			snake_case__  =				jnp.floataa
			snake_case__  =				True
			snake_case__  =				0
			snake_case__  =				False






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			jax.random.KeyArray					)			->			FrozenDict:
							a_		:  Optional[Any]											=      (1, self.in_channels, self.sample_size, self.sample_size)
							a_		:  Any											=      jnp.zeros(__UpperCamelCase  ,						dtype=jnp.floataa					)
							a_		:  Any											=      jnp.ones((1,)  ,						dtype=jnp.intaa					)
							a_		:  int											=      jnp.zeros((1, 1, self.cross_attention_dim)  ,						dtype=jnp.floataa					)

							a_				,			a_		:  int											=      jax.random.split(__UpperCamelCase					)
							a_		:  Dict											=      {'''params''': params_rng, '''dropout''': dropout_rng}

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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			str:
							a_		:  Any											=      self.block_out_channels
							a_		:  Optional[Any]											=      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.
							a_		:  str											=      self.num_attention_heads or self.attention_head_dim

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

							# time
							a_		:  List[str]											=      FlaxTimesteps(
							    block_out_channels[0]  ,						flip_sin_to_cos=self.flip_sin_to_cos  ,						freq_shift=self.config.freq_shift					)
							a_		:  int											=      FlaxTimestepEmbedding(__UpperCamelCase  ,						dtype=self.dtype					)

							a_		:  Tuple											=      self.only_cross_attention
							if isinstance(__UpperCamelCase  ,						__UpperCamelCase					):
											a_		:  Any											=      (only_cross_attention,) * len(self.down_block_types					)

							if isinstance(__UpperCamelCase  ,						__UpperCamelCase					):
											a_		:  str											=      (num_attention_heads,) * len(self.down_block_types					)

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

											if down_block_type == "CrossAttnDownBlock2D":
															a_		:  str											=      FlaxCrossAttnDownBlockaD(
															    in_channels=__UpperCamelCase  ,						out_channels=__UpperCamelCase  ,						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:
															a_		:  Optional[Any]											=      FlaxDownBlockaD(
															    in_channels=__UpperCamelCase  ,						out_channels=__UpperCamelCase  ,						dropout=self.dropout  ,						num_layers=self.layers_per_block  ,						add_downsample=not is_final_block  ,						dtype=self.dtype  ,						)

											down_blocks.append(__UpperCamelCase					)
							a_		:  Optional[int]											=      down_blocks

							# mid
							a_		:  Dict											=      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
							a_		:  Any											=      []
							a_		:  List[str]											=      list(reversed(__UpperCamelCase					)					)
							a_		:  int											=      list(reversed(__UpperCamelCase					)					)
							a_		:  int											=      list(reversed(__UpperCamelCase					)					)
							a_		:  str											=      reversed_block_out_channels[0]
							for i, up_block_type in enumerate(self.up_block_types					):
											a_		:  Optional[Any]											=      output_channel
											a_		:  Dict											=      reversed_block_out_channels[i]
											a_		:  Union[str, Any]											=      reversed_block_out_channels[min(i + 1  ,						len(__UpperCamelCase					) - 1					)]

											a_		:  str											=      i == len(__UpperCamelCase					) - 1

											if up_block_type == "CrossAttnUpBlock2D":
															a_		:  str											=      FlaxCrossAttnUpBlockaD(
															    in_channels=__UpperCamelCase  ,						out_channels=__UpperCamelCase  ,						prev_output_channel=__UpperCamelCase  ,						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:
															a_		:  str											=      FlaxUpBlockaD(
															    in_channels=__UpperCamelCase  ,						out_channels=__UpperCamelCase  ,						prev_output_channel=__UpperCamelCase  ,						num_layers=self.layers_per_block + 1  ,						add_upsample=not is_final_block  ,						dropout=self.dropout  ,						dtype=self.dtype  ,						)

											up_blocks.append(__UpperCamelCase					)
											a_		:  Any											=      output_channel
							a_		:  Optional[Any]											=      up_blocks

							# out
							a_		:  Union[str, Any]											=      nn.GroupNorm(num_groups=32  ,						epsilon=1e-5					)
							a_		:  List[Any]											=      nn.Conv(
							    self.out_channels  ,						kernel_size=(3, 3)  ,						strides=(1, 1)  ,						padding=((1, 1), (1, 1))  ,						dtype=self.dtype  ,						)







			def __call__( self      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			int=None  ,						__SCREAMING_SNAKE_CASE      :			Any=None  ,						__SCREAMING_SNAKE_CASE      :			bool = True  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						)			->			Union[FlaxUNetaDConditionOutput, Tuple]:






							if not isinstance(__UpperCamelCase  ,						jnp.ndarray					):
											a_		:  List[Any]											=      jnp.array([timesteps]  ,						dtype=jnp.intaa					)
							elif isinstance(__UpperCamelCase  ,						jnp.ndarray					) and len(timesteps.shape					) == 0:
											a_		:  List[Any]											=      timesteps.astype(dtype=jnp.floataa					)
											a_		:  Union[str, Any]											=      jnp.expand_dims(__UpperCamelCase  ,						0					)

							a_		:  List[str]											=      self.time_proj(__UpperCamelCase					)
							a_		:  List[Any]											=      self.time_embedding(__UpperCamelCase					)

							# 2. pre-process
							a_		:  Dict											=      jnp.transpose(__UpperCamelCase  ,						(0, 2, 3, 1)					)
							a_		:  List[Any]											=      self.conv_in(__UpperCamelCase					)

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

							if down_block_additional_residuals is not None:
											a_		:  str											=      ()

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

											a_		:  Optional[Any]											=      new_down_block_res_samples

							# 4. mid
							a_		:  Dict											=      self.mid_block(__UpperCamelCase  ,						__UpperCamelCase  ,						__UpperCamelCase  ,						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:
											a_		:  List[str]											=      down_block_res_samples[-(self.layers_per_block + 1) :]
											a_		:  List[Any]											=      down_block_res_samples[: -(self.layers_per_block + 1)]
											if isinstance(__UpperCamelCase  ,						__UpperCamelCase					):
															a_		:  Any											=      up_block(
															    __UpperCamelCase  ,						temb=__UpperCamelCase  ,						encoder_hidden_states=__UpperCamelCase  ,						res_hidden_states_tuple=__UpperCamelCase  ,						deterministic=not train  ,						)
											else:
															a_		:  List[str]											=      up_block(__UpperCamelCase  ,						temb=__UpperCamelCase  ,						res_hidden_states_tuple=__UpperCamelCase  ,						deterministic=not train					)

        # 6. post-process
							a_		:  Tuple											=      self.conv_norm_out(__UpperCamelCase					)
							a_		:  Union[str, Any]											=      nn.silu(__UpperCamelCase					)
							a_		:  Dict											=      self.conv_out(__UpperCamelCase					)
							a_		:  Optional[int]											=      jnp.transpose(__UpperCamelCase  ,						(0, 3, 1, 2)					)

							if not return_dict:
											return (sample,)

							return FlaxUNetaDConditionOutput(sample=__UpperCamelCase					)












'''simple docstring'''







from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union

import pyarrow as pa


if TYPE_CHECKING:
		from .features import FeatureType





@dataclass
class 	SCREAMING_SNAKE_CASE					:
			snake_case__  =				42
			snake_case__  =				None
			# Automatically constructed
			snake_case__  =				"dict"
			snake_case__  =				None
			snake_case__  =				field(default="Translation"	,   init=SCREAMING_SNAKE_CASE_	,   repr=SCREAMING_SNAKE_CASE_       )






			def __call__( self      :			Dict					)			->			Tuple:
							return pa.struct({lang: pa.string() for lang in sorted(self.languages					)}					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Union["FeatureType", Dict[str, "FeatureType"]]:
							from .features import Value

							return {k: Value('''string'''					) for k in sorted(self.languages					)}





@dataclass
class 	SCREAMING_SNAKE_CASE					:
			snake_case__  =				None
			snake_case__  =				None
			snake_case__  =				None
			# Automatically constructed
			snake_case__  =				"dict"
			snake_case__  =				None
			snake_case__  =				field(default="TranslationVariableLanguages"	,   init=SCREAMING_SNAKE_CASE_	,   repr=SCREAMING_SNAKE_CASE_       )






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Dict:
							a_		:  List[str]											=      sorted(set(self.languages					)					) if self.languages else None
							a_		:  Optional[Any]											=      len(self.languages					) if self.languages else None






			def __call__( self      :			Any					)			->			Optional[Any]:
							return pa.struct({'''language''': pa.list_(pa.string()					), '''translation''': pa.list_(pa.string()					)}					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]					)			->			Optional[Any]:
							a_		:  str											=      set(self.languages					)
							if self.languages and set(__SCREAMING_SNAKE_CASE					) - lang_set:
											raise ValueError(
											    f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE					) - lang_set					)					)}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE					)}).'					)

							# Convert dictionary into tuples, splitting out cases where there are
							# multiple translations for a single language.
							a_		:  int											=      []
							for lang, text in translation_dict.items():
											if isinstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					):
															translation_tuples.append((lang, text)					)
											else:
															translation_tuples.extend([(lang, el) for el in text]					)

        # Ensure translations are in ascending order by language code.
							a_				,			a_		:  List[Any]											=      zip(*sorted(__SCREAMING_SNAKE_CASE					)					)

							return {"language": languages, "translation": translations}







			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Union["FeatureType", Dict[str, "FeatureType"]]:
							from .features import Sequence, Value

							return {
							    "language": Sequence(Value('''string'''					)					),
							    "translation": Sequence(Value('''string'''					)					),
							}




