





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_						(					snake_case_					):
						__magic_name__:			Any       =       42
@flax_register_to_config
class SCREAMING_SNAKE_CASE_						(					nn.Module       ,				snake_case_       ,				snake_case_					):
						__magic_name__:			Union[str, Any]       =       32
						__magic_name__:			Tuple       =       4
						__magic_name__:			List[str]       =       4
						__magic_name__:			Tuple       =       (
						    "CrossAttnDownBlock2D",
						    "CrossAttnDownBlock2D",
						    "CrossAttnDownBlock2D",
						    "DownBlock2D",
						)
						__magic_name__:			Optional[Any]       =       ("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
						__magic_name__:			Any       =       False
						__magic_name__:			Optional[Any]       =       (320, 640, 1280, 1280)
						__magic_name__:			Dict       =       2
						__magic_name__:			Dict       =       8
						__magic_name__:			int       =       None
						__magic_name__:			Any       =       1280
						__magic_name__:			Tuple       =       0.0
						__magic_name__:			Optional[Any]       =       False
						__magic_name__:			Union[str, Any]       =       jnp.floataa
						__magic_name__:			Optional[int]       =       True
						__magic_name__:			Optional[Any]       =       0
						__magic_name__:			Union[str, Any]       =       False






						def UpperCAmelCase_				(      self      :      str		,   _A      :      Any	)							->    FrozenDict:







							"""simple docstring"""


							snake_case_		:			Tuple											=	(1, self.in_channels, self.sample_size, self.sample_size)
							snake_case_		:			List[str]											=	jnp.zeros(_A		,   dtype=jnp.floataa	)
							snake_case_		:			Tuple											=	jnp.ones((1,)		,   dtype=jnp.intaa	)
							snake_case_		:			Union[str, Any]											=	jnp.zeros((1, 1, self.cross_attention_dim)		,   dtype=jnp.floataa	)

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

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






						def UpperCAmelCase_				(      self      :      Any	)							->    List[str]:







							"""simple docstring"""


							snake_case_		:			Dict											=	self.block_out_channels
							snake_case_		:			int											=	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.
							snake_case_		:			Optional[Any]											=	self.num_attention_heads or self.attention_head_dim

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

							# time
							snake_case_		:			Tuple											=	FlaxTimesteps(
							    block_out_channels[0]		,   flip_sin_to_cos=self.flip_sin_to_cos		,   freq_shift=self.config.freq_shift	)
							snake_case_		:			Optional[Any]											=	FlaxTimestepEmbedding(_A		,   dtype=self.dtype	)

							snake_case_		:			Any											=	self.only_cross_attention
							if isinstance(_A		,   _A	):
								snake_case_		:			int											=	(only_cross_attention,) * len(self.down_block_types	)

							if isinstance(_A		,   _A	):
								snake_case_		:			Dict											=	(num_attention_heads,) * len(self.down_block_types	)

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

								if down_block_type == "CrossAttnDownBlock2D":
									snake_case_		:			Optional[Any]											=	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:
									snake_case_		:			List[Any]											=	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	)
							snake_case_		:			int											=	down_blocks

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

								snake_case_		:			Union[str, Any]											=	i == len(_A	) - 1

								if up_block_type == "CrossAttnUpBlock2D":
									snake_case_		:			str											=	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:
									snake_case_		:			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	)
								snake_case_		:			List[Any]											=	output_channel
							snake_case_		:			List[str]											=	up_blocks

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






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







							"""simple docstring"""


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

							snake_case_		:			List[Any]											=	self.time_proj(_A	)
							snake_case_		:			int											=	self.time_embedding(_A	)

							# 2. pre-process
							snake_case_		:			Dict											=	jnp.transpose(_A		,   (0, 2, 3, 1)	)
							snake_case_		:			Any											=	self.conv_in(_A	)

							# 3. down
							snake_case_		:			Tuple											=	(sample,)
							for down_block in self.down_blocks:
								if isinstance(_A		,   _A	):
									snake_case_						,snake_case_		:			Any											=	down_block(_A		,   _A		,   _A		,   deterministic=not train	)
								else:
									snake_case_						,snake_case_		:			Dict											=	down_block(_A		,   _A		,   deterministic=not train	)
								down_block_res_samples += res_samples

							if down_block_additional_residuals is not None:
								snake_case_		:			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,)

								snake_case_		:			Tuple											=	new_down_block_res_samples

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

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

							if not return_dict:
								return (sample,)

							return FlaxUNetaDConditionOutput(sample=_A	)








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE      	=		{
    """configuration_lxmert""": ["""LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """LxmertConfig"""],
    """tokenization_lxmert""": ["""LxmertTokenizer"""],
}

try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_SCREAMING_SNAKE_CASE      	=		["""LxmertTokenizerFast"""]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_SCREAMING_SNAKE_CASE      	=		[
						    """LxmertEncoder""",
						    """LxmertForPreTraining""",
						    """LxmertForQuestionAnswering""",
						    """LxmertModel""",
						    """LxmertPreTrainedModel""",
						    """LxmertVisualFeatureEncoder""",
						    """LxmertXLayer""",
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_SCREAMING_SNAKE_CASE      	=		[
						    """TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """TFLxmertForPreTraining""",
						    """TFLxmertMainLayer""",
						    """TFLxmertModel""",
						    """TFLxmertPreTrainedModel""",
						    """TFLxmertVisualFeatureEncoder""",
						]


if TYPE_CHECKING:
						from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig
						from .tokenization_lxmert import LxmertTokenizer

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_lxmert_fast import LxmertTokenizerFast

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_lxmert import (
												    LxmertEncoder,
												    LxmertForPreTraining,
												    LxmertForQuestionAnswering,
												    LxmertModel,
												    LxmertPreTrainedModel,
												    LxmertVisualFeatureEncoder,
												    LxmertXLayer,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_lxmert import (
												    TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFLxmertForPreTraining,
												    TFLxmertMainLayer,
												    TFLxmertModel,
												    TFLxmertPreTrainedModel,
												    TFLxmertVisualFeatureEncoder,
												)

else:
						import sys

						_SCREAMING_SNAKE_CASE      	=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

