




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        __snake_case  (__snake_case		):
				lowerCAmelCase__				=     42
				lowerCAmelCase__				=     42






class        __snake_case  (nn.Module		):
				lowerCAmelCase__				=     42
				lowerCAmelCase__				=     (1_6, 3_2, 9_6, 2_5_6)
				lowerCAmelCase__				=     jnp.floataa



				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    int:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      nn.Conv(
								    self.block_out_channels[0]		,	kernel_size=(3, 3)		,	padding=((1, 1), (1, 1))		,	dtype=self.dtype		,	)

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

								_lowerCAmelCase    :    Dict										=      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      :		List[str]		,	_UpperCAmelCase      :		Tuple      )  ->    List[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Dict										=      self.conv_in(A_      )
								_lowerCAmelCase    :    Any										=      nn.silu(A_      )

								for block in self.blocks:
												_lowerCAmelCase    :    int										=      block(A_      )
												_lowerCAmelCase    :    Any										=      nn.silu(A_      )

								_lowerCAmelCase    :    List[Any]										=      self.conv_out(A_      )

								return embedding






@flax_register_to_config
class        __snake_case  (nn.Module						,	__snake_case						,	__snake_case		):
				lowerCAmelCase__				=     3_2
				lowerCAmelCase__				=     4
				lowerCAmelCase__				=     (
				    "CrossAttnDownBlock2D",
				    "CrossAttnDownBlock2D",
				    "CrossAttnDownBlock2D",
				    "DownBlock2D",
				)
				lowerCAmelCase__				=     False
				lowerCAmelCase__				=     (3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
				lowerCAmelCase__				=     2
				lowerCAmelCase__				=     8
				lowerCAmelCase__				=     None
				lowerCAmelCase__				=     1_2_8_0
				lowerCAmelCase__				=     0.0
				lowerCAmelCase__				=     False
				lowerCAmelCase__				=     jnp.floataa
				lowerCAmelCase__				=     True
				lowerCAmelCase__				=     0
				lowerCAmelCase__				=     "rgb"
				lowerCAmelCase__				=     (1_6, 3_2, 9_6, 2_5_6)



				def       SCREAMING_SNAKE_CASE       (      self      :		List[Any]		,	_UpperCAmelCase      :		List[str]      )  ->    List[str]:





								'''simple docstring'''




								_lowerCAmelCase    :    Dict										=      (1, self.in_channels, self.sample_size, self.sample_size)
								_lowerCAmelCase    :    Any										=      jnp.zeros(A_		,	dtype=jnp.floataa      )
								_lowerCAmelCase    :    Union[str, Any]										=      jnp.ones((1,)		,	dtype=jnp.intaa      )
								_lowerCAmelCase    :    Dict										=      jnp.zeros((1, 1, self.cross_attention_dim)		,	dtype=jnp.floataa      )
								_lowerCAmelCase    :    List[str]										=      (1, 3, self.sample_size * 8, self.sample_size * 8)
								_lowerCAmelCase    :    Optional[Any]										=      jnp.zeros(A_		,	dtype=jnp.floataa      )

								_lowerCAmelCase    :    Union[str, Any]										=      jax.random.split(A_      )
								_lowerCAmelCase    :    List[str]										=      {"params": params_rng, "dropout": dropout_rng}

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



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]      )  ->    str:





								'''simple docstring'''




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

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

								# time
								_lowerCAmelCase    :    Optional[Any]										=      FlaxTimesteps(
								    block_out_channels[0]		,	flip_sin_to_cos=self.flip_sin_to_cos		,	freq_shift=self.config.freq_shift      )
								_lowerCAmelCase    :    int										=      FlaxTimestepEmbedding(A_		,	dtype=self.dtype      )

								_lowerCAmelCase    :    int										=      FlaxControlNetConditioningEmbedding(
								    conditioning_embedding_channels=block_out_channels[0]		,	block_out_channels=self.conditioning_embedding_out_channels		,	)

								_lowerCAmelCase    :    Tuple										=      self.only_cross_attention
								if isinstance(A_		,	A_      ):
												_lowerCAmelCase    :    Dict										=      (only_cross_attention,) * len(self.down_block_types      )

								if isinstance(A_		,	A_      ):
												_lowerCAmelCase    :    Any										=      (num_attention_heads,) * len(self.down_block_types      )

								# down
								_lowerCAmelCase    :    str										=      []
								_lowerCAmelCase    :    Optional[Any]										=      []

								_lowerCAmelCase    :    int										=      block_out_channels[0]

								_lowerCAmelCase    :    str										=      nn.Conv(
								    A_		,	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(A_      )

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

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

												for _ in range(self.layers_per_block      ):
																_lowerCAmelCase    :    List[Any]										=      nn.Conv(
																    A_		,	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(A_      )

												if not is_final_block:
																_lowerCAmelCase    :    int										=      nn.Conv(
																    A_		,	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(A_      )

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

								# mid
								_lowerCAmelCase    :    int										=      block_out_channels[-1]
								_lowerCAmelCase    :    Any										=      FlaxUNetMidBlockaDCrossAttn(
								    in_channels=A_		,	dropout=self.dropout		,	num_attention_heads=num_attention_heads[-1]		,	use_linear_projection=self.use_linear_projection		,	dtype=self.dtype		,	)

								_lowerCAmelCase    :    Dict										=      nn.Conv(
								    A_		,	kernel_size=(1, 1)		,	padding="""VALID"""		,	kernel_init=nn.initializers.zeros_init()		,	bias_init=nn.initializers.zeros_init()		,	dtype=self.dtype		,	)



				def __call__(      self      :		str		,	_UpperCAmelCase      :		Union[str, Any]		,	_UpperCAmelCase      :		Tuple		,	_UpperCAmelCase      :		List[str]		,	_UpperCAmelCase      :		Optional[Any]		,	_UpperCAmelCase      :		int = 1.0		,	_UpperCAmelCase      :		Optional[Any] = True		,	_UpperCAmelCase      :		List[str] = False		,	)  ->    int:





								'''simple docstring'''




								_lowerCAmelCase    :    Tuple										=      self.controlnet_conditioning_channel_order
								if channel_order == "bgr":
												_lowerCAmelCase    :    str										=      jnp.flip(A_		,	axis=1      )

								# 1. time
								if not isinstance(A_		,	jnp.ndarray      ):
												_lowerCAmelCase    :    Tuple										=      jnp.array([timesteps]		,	dtype=jnp.intaa      )
								elif isinstance(A_		,	jnp.ndarray      ) and len(timesteps.shape      ) == 0:
												_lowerCAmelCase    :    Optional[Any]										=      timesteps.astype(dtype=jnp.floataa      )
												_lowerCAmelCase    :    List[Any]										=      jnp.expand_dims(A_		,	0      )

								_lowerCAmelCase    :    int										=      self.time_proj(A_      )
								_lowerCAmelCase    :    List[Any]										=      self.time_embedding(A_      )

								# 2. pre-process
								_lowerCAmelCase    :    int										=      jnp.transpose(A_		,	(0, 2, 3, 1)      )
								_lowerCAmelCase    :    Dict										=      self.conv_in(A_      )

								_lowerCAmelCase    :    List[Any]										=      jnp.transpose(A_		,	(0, 2, 3, 1)      )
								_lowerCAmelCase    :    Optional[int]										=      self.controlnet_cond_embedding(A_      )
								sample += controlnet_cond

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

								# 4. mid
								_lowerCAmelCase    :    Any										=      self.mid_block(A_		,	A_		,	A_		,	deterministic=not train      )

								# 5. contronet blocks
								_lowerCAmelCase    :    List[Any]										=      ()
								for down_block_res_sample, controlnet_block in zip(A_		,	self.controlnet_down_blocks      ):
												_lowerCAmelCase    :    Tuple										=      controlnet_block(A_      )
												controlnet_down_block_res_samples += (down_block_res_sample,)

								_lowerCAmelCase    :    int										=      controlnet_down_block_res_samples

								_lowerCAmelCase    :    Optional[int]										=      self.controlnet_mid_block(A_      )

								# 6. scaling
								_lowerCAmelCase    :    str										=      [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=A_		,	mid_block_res_sample=A_      )







import logging

from transformers.configuration_utils import PretrainedConfig


_lowerCamelCase      :  Union[str, Any]      						=						logging.getLogger(__name__)
class        __snake_case  (_a		):
				lowerCAmelCase__				=     "masked_bert"



				def __init__(      self      :		Union[str, Any]		,	_UpperCAmelCase      :		Union[str, Any]=3_0522		,	_UpperCAmelCase      :		Optional[Any]=768		,	_UpperCAmelCase      :		List[Any]=12		,	_UpperCAmelCase      :		Dict=12		,	_UpperCAmelCase      :		List[str]=3072		,	_UpperCAmelCase      :		List[Any]="gelu"		,	_UpperCAmelCase      :		Optional[Any]=0.1		,	_UpperCAmelCase      :		Optional[Any]=0.1		,	_UpperCAmelCase      :		List[Any]=512		,	_UpperCAmelCase      :		int=2		,	_UpperCAmelCase      :		Optional[int]=0.02		,	_UpperCAmelCase      :		str=1E-12		,	_UpperCAmelCase      :		Tuple=0		,	_UpperCAmelCase      :		Dict="topK"		,	_UpperCAmelCase      :		List[str]="constant"		,	_UpperCAmelCase      :		Optional[Any]=0.0		,	**_UpperCAmelCase      :		str		,	)  ->    Tuple:





								'''simple docstring'''




								super().__init__(pad_token_id=_UpperCAmelCase		,	**_UpperCAmelCase      )

								_lowerCAmelCase    :    str										=      vocab_size
								_lowerCAmelCase    :    List[str]										=      hidden_size
								_lowerCAmelCase    :    Any										=      num_hidden_layers
								_lowerCAmelCase    :    Optional[Any]										=      num_attention_heads
								_lowerCAmelCase    :    Optional[Any]										=      hidden_act
								_lowerCAmelCase    :    Union[str, Any]										=      intermediate_size
								_lowerCAmelCase    :    Tuple										=      hidden_dropout_prob
								_lowerCAmelCase    :    Optional[int]										=      attention_probs_dropout_prob
								_lowerCAmelCase    :    str										=      max_position_embeddings
								_lowerCAmelCase    :    int										=      type_vocab_size
								_lowerCAmelCase    :    List[str]										=      initializer_range
								_lowerCAmelCase    :    Optional[Any]										=      layer_norm_eps
								_lowerCAmelCase    :    Tuple										=      pruning_method
								_lowerCAmelCase    :    str										=      mask_init
								_lowerCAmelCase    :    List[str]										=      mask_scale

