
'''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 	__lowercase				(  _lowercase    ):
			lowerCamelCase			:		jnp.ndarray

@flax_register_to_config
class 	__lowercase				(  nn.Module      ,     _lowercase      ,     _lowercase    ):
			lowerCamelCase			:		int	      =			32
			lowerCamelCase			:		int	      =			4
			lowerCamelCase			:		int	      =			4
			lowerCamelCase			:		Tuple[str]	      =			(
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "DownBlock2D",
			)
			lowerCamelCase			:		Tuple[str]	      =			("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
			lowerCamelCase			:		Union[bool, Tuple[bool]]	      =			False
			lowerCamelCase			:		Tuple[int]	      =			(320, 640, 1280, 1280)
			lowerCamelCase			:		int	      =			2
			lowerCamelCase			:		Union[int, Tuple[int]]	      =			8
			lowerCamelCase			:		Optional[Union[int, Tuple[int]]]	      =			None
			lowerCamelCase			:		int	      =			1280
			lowerCamelCase			:		float	      =			0.0
			lowerCamelCase			:		bool	      =			False
			lowerCamelCase			:		jnp.dtype	      =			jnp.floataa
			lowerCamelCase			:		bool	      =			True
			lowerCamelCase			:		int	      =			0
			lowerCamelCase			:		bool	      =			False

			def        UpperCAmelCase__ (self			,   A      ):
								# init input tensors
								lowerCamelCase_					:      Optional[int]     =		(1, self.in_channels, self.sample_size, self.sample_size)
								lowerCamelCase_					:      Dict     =		jnp.zeros(A			,   dtype=jnp.floataa      )
								lowerCamelCase_					:      Optional[int]     =		jnp.ones((1,)			,   dtype=jnp.intaa      )
								lowerCamelCase_					:      Optional[Any]     =		jnp.zeros((1, 1, self.cross_attention_dim)			,   dtype=jnp.floataa      )

								lowerCamelCase_					:      Any     =		jax.random.split(A      )
								lowerCamelCase_					:      Optional[Any]     =		{'''params''': params_rng, '''dropout''': dropout_rng}

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

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      str     =		self.block_out_channels
								lowerCamelCase_					:      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.
								lowerCamelCase_					:      Dict     =		self.num_attention_heads or self.attention_head_dim

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

								# time
								lowerCamelCase_					:      Dict     =		FlaxTimesteps(
								    block_out_channels[0]			,   flip_sin_to_cos=self.flip_sin_to_cos			,   freq_shift=self.config.freq_shift      )
								lowerCamelCase_					:      Tuple     =		FlaxTimestepEmbedding(A			,   dtype=self.dtype      )

								lowerCamelCase_					:      str     =		self.only_cross_attention
								if isinstance(A			,   A      ):
													lowerCamelCase_					:      List[str]     =		(only_cross_attention,) * len(self.down_block_types      )

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

								# down
								lowerCamelCase_					:      Dict     =		[]
								lowerCamelCase_					:      int     =		block_out_channels[0]
								for i, down_block_type in enumerate(self.down_block_types      ):
													lowerCamelCase_					:      Tuple     =		output_channel
													lowerCamelCase_					:      List[str]     =		block_out_channels[i]
													lowerCamelCase_					:      Tuple     =		i == len(A      ) - 1

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

													down_blocks.append(A      )
								lowerCamelCase_					:      List[Any]     =		down_blocks

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

													lowerCamelCase_					:      Union[str, Any]     =		i == len(A      ) - 1

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

													up_blocks.append(A      )
													lowerCamelCase_					:      List[Any]     =		output_channel
								lowerCamelCase_					:      Tuple     =		up_blocks

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






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

								lowerCamelCase_					:      Optional[Any]     =		self.time_proj(A      )
								lowerCamelCase_					:      List[str]     =		self.time_embedding(A      )

								# 2. pre-process
								lowerCamelCase_					:      List[Any]     =		jnp.transpose(A			,   (0, 2, 3, 1)      )
								lowerCamelCase_					:      List[str]     =		self.conv_in(A      )

								# 3. down
								lowerCamelCase_					:      int     =		(sample,)
								for down_block in self.down_blocks:
													if isinstance(A			,   A      ):
																		lowerCamelCase_					:      Tuple     =		down_block(A			,   A			,   A			,   deterministic=not train      )
													else:
																		lowerCamelCase_					:      Tuple     =		down_block(A			,   A			,   deterministic=not train      )
													down_block_res_samples += res_samples

								if down_block_additional_residuals is not None:
													lowerCamelCase_					:      Optional[Any]     =		()

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

													lowerCamelCase_					:      Union[str, Any]     =		new_down_block_res_samples

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

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

								if not return_dict:
													return (sample,)

								return FlaxUNetaDConditionOutput(sample=A      )




'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


__lowercase							:   int					=     {}

try:
					if not is_sentencepiece_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:   Any					=     ['''BartphoTokenizer''']

if TYPE_CHECKING:
					try:
										if not is_sentencepiece_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_bartpho import BartphoTokenizer

else:
					import sys

					__lowercase							:   Union[str, Any]					=     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


