






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,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)



@flax.struct.dataclass
class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		_snake_case    :    Optional[int]       						=  4_2
		_snake_case    :    Optional[int]       						=  4_2





class   lowerCamelCase      (nn.Module			):







		'''simple docstring'''



		_snake_case    :    Tuple       						=  4_2
		_snake_case    :    Optional[Any]       						=  (1_6, 3_2, 9_6, 2_5_6)
		_snake_case    :    List[str]       						=  jnp.floataa



		def 					__UpperCAmelCase			(							self   )	->							int:
							UpperCAmelCase_    :      Optional[Any]            =						nn.Conv(
							    self.block_out_channels[0]				,		kernel_size=(3, 3)				,		padding=((1, 1), (1, 1))				,		dtype=self.dtype				,		)

							UpperCAmelCase_    :      List[str]            =						[]
							for i in range(len(self.block_out_channels   ) - 1   ):
												UpperCAmelCase_    :      Union[str, Any]            =						self.block_out_channels[i]
												UpperCAmelCase_    :      Optional[int]            =						self.block_out_channels[i + 1]
												UpperCAmelCase_    :      List[str]            =						nn.Conv(
												    __UpperCAmelCase				,		kernel_size=(3, 3)				,		padding=((1, 1), (1, 1))				,		dtype=self.dtype				,		)
												blocks.append(__UpperCAmelCase   )
												UpperCAmelCase_    :      Union[str, Any]            =						nn.Conv(
												    __UpperCAmelCase				,		kernel_size=(3, 3)				,		strides=(2, 2)				,		padding=((1, 1), (1, 1))				,		dtype=self.dtype				,		)
												blocks.append(__UpperCAmelCase   )
							UpperCAmelCase_    :      int            =						blocks

							UpperCAmelCase_    :      int            =						nn.Conv(
							    self.conditioning_embedding_channels				,		kernel_size=(3, 3)				,		padding=((1, 1), (1, 1))				,		kernel_init=nn.initializers.zeros_init()				,		bias_init=nn.initializers.zeros_init()				,		dtype=self.dtype				,		)



		def __call__(							self				,		_UpperCamelCase   )	->							Dict:
							UpperCAmelCase_    :      Dict            =						self.conv_in(__UpperCAmelCase   )
							UpperCAmelCase_    :      int            =						nn.silu(__UpperCAmelCase   )

							for block in self.blocks:
												UpperCAmelCase_    :      Optional[Any]            =						block(__UpperCAmelCase   )
												UpperCAmelCase_    :      Tuple            =						nn.silu(__UpperCAmelCase   )

							UpperCAmelCase_    :      List[str]            =						self.conv_out(__UpperCAmelCase   )

							return embedding





@flax_register_to_config
class   lowerCamelCase      (nn.Module ,  _snake_case ,  _snake_case			):







		'''simple docstring'''



		_snake_case    :    Dict       						=  3_2
		_snake_case    :    int       						=  4
		_snake_case    :    str       						=  (
		    '''CrossAttnDownBlock2D''',
		    '''CrossAttnDownBlock2D''',
		    '''CrossAttnDownBlock2D''',
		    '''DownBlock2D''',
		)
		_snake_case    :    List[str]       						=  False
		_snake_case    :    List[str]       						=  (3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
		_snake_case    :    List[str]       						=  2
		_snake_case    :    Any       						=  8
		_snake_case    :    Optional[int]       						=  None
		_snake_case    :    str       						=  1_2_8_0
		_snake_case    :    Optional[Any]       						=  0.0
		_snake_case    :    Union[str, Any]       						=  False
		_snake_case    :    Optional[int]       						=  jnp.floataa
		_snake_case    :    Optional[Any]       						=  True
		_snake_case    :    Optional[Any]       						=  0
		_snake_case    :    Optional[Any]       						=  '''rgb'''
		_snake_case    :    Optional[Any]       						=  (1_6, 3_2, 9_6, 2_5_6)



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase   )	->							FrozenDict:
							UpperCAmelCase_    :      List[Any]            =						(1, self.in_channels, self.sample_size, self.sample_size)
							UpperCAmelCase_    :      Dict            =						jnp.zeros(__UpperCAmelCase				,		dtype=jnp.floataa   )
							UpperCAmelCase_    :      Union[str, Any]            =						jnp.ones((1,)				,		dtype=jnp.intaa   )
							UpperCAmelCase_    :      str            =						jnp.zeros((1, 1, self.cross_attention_dim)				,		dtype=jnp.floataa   )
							UpperCAmelCase_    :      Tuple            =						(1, 3, self.sample_size * 8, self.sample_size * 8)
							UpperCAmelCase_    :      Union[str, Any]            =						jnp.zeros(__UpperCAmelCase				,		dtype=jnp.floataa   )

							UpperCAmelCase_       ,  UpperCAmelCase_    :      int            =						jax.random.split(__UpperCAmelCase   )
							UpperCAmelCase_    :      str            =						{'params': params_rng, 'dropout': dropout_rng}

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



		def 					__UpperCAmelCase			(							self   )	->							List[str]:
							UpperCAmelCase_    :      Any            =						self.block_out_channels
							UpperCAmelCase_    :      List[str]            =						block_out_channels[0] * 4

							# 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_    :      Dict            =						self.num_attention_heads or self.attention_head_dim

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

							# time
							UpperCAmelCase_    :      Any            =						FlaxTimesteps(
							    block_out_channels[0]				,		flip_sin_to_cos=self.flip_sin_to_cos				,		freq_shift=self.config.freq_shift   )
							UpperCAmelCase_    :      Dict            =						FlaxTimestepEmbedding(__UpperCAmelCase				,		dtype=self.dtype   )

							UpperCAmelCase_    :      List[Any]            =						FlaxControlNetConditioningEmbedding(
							    conditioning_embedding_channels=block_out_channels[0]				,		block_out_channels=self.conditioning_embedding_out_channels				,		)

							UpperCAmelCase_    :      Optional[int]            =						self.only_cross_attention
							if isinstance(__UpperCAmelCase				,		__UpperCAmelCase   ):
												UpperCAmelCase_    :      Tuple            =						(only_cross_attention,) * len(self.down_block_types   )

							if isinstance(__UpperCAmelCase				,		__UpperCAmelCase   ):
												UpperCAmelCase_    :      Tuple            =						(num_attention_heads,) * len(self.down_block_types   )

							# down
							UpperCAmelCase_    :      int            =						[]
							UpperCAmelCase_    :      List[Any]            =						[]

							UpperCAmelCase_    :      List[str]            =						block_out_channels[0]

							UpperCAmelCase_    :      Union[str, Any]            =						nn.Conv(
							    __UpperCAmelCase				,		kernel_size=(1, 1)				,		padding='VALID'				,		kernel_init=nn.initializers.zeros_init()				,		bias_init=nn.initializers.zeros_init()				,		dtype=self.dtype				,		)
							controlnet_down_blocks.append(__UpperCAmelCase   )

							for i, down_block_type in enumerate(self.down_block_types   ):
												UpperCAmelCase_    :      Union[str, Any]            =						output_channel
												UpperCAmelCase_    :      Optional[Any]            =						block_out_channels[i]
												UpperCAmelCase_    :      int            =						i == len(__UpperCAmelCase   ) - 1

												if down_block_type == "CrossAttnDownBlock2D":
																	UpperCAmelCase_    :      Optional[Any]            =						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]				,		dtype=self.dtype				,		)
												else:
																	UpperCAmelCase_    :      List[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   )

												for _ in range(self.layers_per_block   ):
																	UpperCAmelCase_    :      int            =						nn.Conv(
																	    __UpperCAmelCase				,		kernel_size=(1, 1)				,		padding='VALID'				,		kernel_init=nn.initializers.zeros_init()				,		bias_init=nn.initializers.zeros_init()				,		dtype=self.dtype				,		)
																	controlnet_down_blocks.append(__UpperCAmelCase   )

												if not is_final_block:
																	UpperCAmelCase_    :      List[str]            =						nn.Conv(
																	    __UpperCAmelCase				,		kernel_size=(1, 1)				,		padding='VALID'				,		kernel_init=nn.initializers.zeros_init()				,		bias_init=nn.initializers.zeros_init()				,		dtype=self.dtype				,		)
																	controlnet_down_blocks.append(__UpperCAmelCase   )

							UpperCAmelCase_    :      str            =						down_blocks
							UpperCAmelCase_    :      List[str]            =						controlnet_down_blocks

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

							UpperCAmelCase_    :      Tuple            =						nn.Conv(
							    __UpperCAmelCase				,		kernel_size=(1, 1)				,		padding='VALID'				,		kernel_init=nn.initializers.zeros_init()				,		bias_init=nn.initializers.zeros_init()				,		dtype=self.dtype				,		)



		def __call__(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase = 1.0				,		_UpperCamelCase = True				,		_UpperCamelCase = False				,		)	->							Union[FlaxControlNetOutput, Tuple]:
							UpperCAmelCase_    :      List[str]            =						self.controlnet_conditioning_channel_order
							if channel_order == "bgr":
												UpperCAmelCase_    :      List[str]            =						jnp.flip(__UpperCAmelCase				,		axis=1   )

							# 1. time
							if not isinstance(__UpperCAmelCase				,		jnp.ndarray   ):
												UpperCAmelCase_    :      Optional[int]            =						jnp.array([timesteps]				,		dtype=jnp.intaa   )
							elif isinstance(__UpperCAmelCase				,		jnp.ndarray   ) and len(timesteps.shape   ) == 0:
												UpperCAmelCase_    :      str            =						timesteps.astype(dtype=jnp.floataa   )
												UpperCAmelCase_    :      int            =						jnp.expand_dims(__UpperCAmelCase				,		0   )

							UpperCAmelCase_    :      Tuple            =						self.time_proj(__UpperCAmelCase   )
							UpperCAmelCase_    :      int            =						self.time_embedding(__UpperCAmelCase   )

							# 2. pre-process
							UpperCAmelCase_    :      Dict            =						jnp.transpose(__UpperCAmelCase				,		(0, 2, 3, 1)   )
							UpperCAmelCase_    :      str            =						self.conv_in(__UpperCAmelCase   )

							UpperCAmelCase_    :      Dict            =						jnp.transpose(__UpperCAmelCase				,		(0, 2, 3, 1)   )
							UpperCAmelCase_    :      Tuple            =						self.controlnet_cond_embedding(__UpperCAmelCase   )
							sample += controlnet_cond

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

							# 4. mid
							UpperCAmelCase_    :      List[str]            =						self.mid_block(__UpperCAmelCase				,		__UpperCAmelCase				,		__UpperCAmelCase				,		deterministic=not train   )

							# 5. contronet blocks
							UpperCAmelCase_    :      Optional[int]            =						()
							for down_block_res_sample, controlnet_block in zip(__UpperCAmelCase				,		self.controlnet_down_blocks   ):
												UpperCAmelCase_    :      List[Any]            =						controlnet_block(__UpperCAmelCase   )
												controlnet_down_block_res_samples += (down_block_res_sample,)

							UpperCAmelCase_    :      Dict            =						controlnet_down_block_res_samples

							UpperCAmelCase_    :      Optional[Any]            =						self.controlnet_mid_block(__UpperCAmelCase   )

							# 6. scaling
							UpperCAmelCase_    :      Union[str, Any]            =						[sample * conditioning_scale for sample in down_block_res_samples]
							mid_block_res_sample *= conditioning_scale

							if not return_dict:
												return (down_block_res_samples, mid_block_res_sample)

							return FlaxControlNetOutput(
							    down_block_res_samples=__UpperCAmelCase				,		mid_block_res_sample=__UpperCAmelCase   )














from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCAmelCase__				   =		{
    "configuration_nllb_moe": [
        "NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "NllbMoeConfig",
    ]
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCAmelCase__				   =		[
         "NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST",
         "NllbMoeForConditionalGeneration",
         "NllbMoeModel",
         "NllbMoePreTrainedModel",
         "NllbMoeTop2Router",
         "NllbMoeSparseMLP",
     ]


if TYPE_CHECKING:
     from .configuration_nllb_moe import (
         NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
         NllbMoeConfig,
     )

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_nllb_moe import (
              NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
              NllbMoeForConditionalGeneration,
              NllbMoeModel,
              NllbMoePreTrainedModel,
              NllbMoeSparseMLP,
              NllbMoeTopaRouter,
          )


else:
     import sys

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


