
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      A__							(     __magic_name__	):
				lowercase			    =  42
				lowercase			    =  42







class      A__							(     nn.Module	):
				lowercase			    =  42
				lowercase			    =  (16, 32, 96, 256)
				lowercase			    =  jnp.floataa






				def 							_lowerCamelCase		(						self					:      str   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Tuple         =		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__      :      Tuple         =		self.block_out_channels[i]
										lowerCAmelCase__      :      Dict         =		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__      :      Union[str, Any]         =		nn.Conv(
										    a    ,     kernel_size=(3, 3)    ,     strides=(2, 2)    ,     padding=((1, 1), (1, 1))    ,     dtype=self.dtype    ,     )
										blocks.append(a   )
							lowerCAmelCase__      :      Union[str, Any]         =		blocks

							lowerCAmelCase__      :      Optional[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					:      str    ,     a					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Union[str, Any]         =		self.conv_in(a   )
							lowerCAmelCase__      :      List[Any]         =		nn.silu(a   )

							for block in self.blocks:
										lowerCAmelCase__      :      Tuple         =		block(a   )
										lowerCAmelCase__      :      List[Any]         =		nn.silu(a   )

							lowerCAmelCase__      :      Dict         =		self.conv_out(a   )

							return embedding







@flax_register_to_config
class      A__							(     nn.Module						,   __magic_name__						,   __magic_name__	):
				lowercase			    =  32
				lowercase			    =  4
				lowercase			    =  (
				    "CrossAttnDownBlock2D",
				    "CrossAttnDownBlock2D",
				    "CrossAttnDownBlock2D",
				    "DownBlock2D",
				)
				lowercase			    =  False
				lowercase			    =  (320, 640, 1280, 1280)
				lowercase			    =  2
				lowercase			    =  8
				lowercase			    =  None
				lowercase			    =  1280
				lowercase			    =  0.0
				lowercase			    =  False
				lowercase			    =  jnp.floataa
				lowercase			    =  True
				lowercase			    =  0
				lowercase			    =  "rgb"
				lowercase			    =  (16, 32, 96, 256)






				def 							_lowerCamelCase		(						self					:      int    ,     a					:      jax.random.KeyArray   ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		(1, self.in_channels, self.sample_size, self.sample_size)
							lowerCAmelCase__      :      List[str]         =		jnp.zeros(a    ,     dtype=jnp.floataa   )
							lowerCAmelCase__      :      Dict         =		jnp.ones((1,)    ,     dtype=jnp.intaa   )
							lowerCAmelCase__      :      List[str]         =		jnp.zeros((1, 1, self.cross_attention_dim)    ,     dtype=jnp.floataa   )
							lowerCAmelCase__      :      Optional[int]         =		(1, 3, self.sample_size * 8, self.sample_size * 8)
							lowerCAmelCase__      :      Tuple         =		jnp.zeros(a    ,     dtype=jnp.floataa   )

							lowerCAmelCase__      ,				lowerCAmelCase__      :      List[Any]         =		jax.random.split(a   )
							lowerCAmelCase__      :      str         =		{'params': params_rng, 'dropout': dropout_rng}

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






				def 							_lowerCamelCase		(						self					:      Any   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Tuple         =		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__      :      Union[str, Any]         =		self.num_attention_heads or self.attention_head_dim

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

							# time
							lowerCAmelCase__      :      Any         =		FlaxTimesteps(
							    block_out_channels[0]    ,     flip_sin_to_cos=self.flip_sin_to_cos    ,     freq_shift=self.config.freq_shift   )
							lowerCAmelCase__      :      List[str]         =		FlaxTimestepEmbedding(a    ,     dtype=self.dtype   )

							lowerCAmelCase__      :      Optional[Any]         =		FlaxControlNetConditioningEmbedding(
							    conditioning_embedding_channels=block_out_channels[0]    ,     block_out_channels=self.conditioning_embedding_out_channels    ,     )

							lowerCAmelCase__      :      str         =		self.only_cross_attention
							if isinstance(a    ,     a   ):
										lowerCAmelCase__      :      List[Any]         =		(only_cross_attention,) * len(self.down_block_types   )

							if isinstance(a    ,     a   ):
										lowerCAmelCase__      :      Optional[int]         =		(num_attention_heads,) * len(self.down_block_types   )

							# down
							lowerCAmelCase__      :      Dict         =		[]
							lowerCAmelCase__      :      Optional[int]         =		[]

							lowerCAmelCase__      :      int         =		block_out_channels[0]

							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    ,     )
							controlnet_down_blocks.append(a   )

							for i, down_block_type in enumerate(self.down_block_types   ):
										lowerCAmelCase__      :      int         =		output_channel
										lowerCAmelCase__      :      List[str]         =		block_out_channels[i]
										lowerCAmelCase__      :      List[str]         =		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__      :      Optional[int]         =		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__      :      Tuple         =		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__      :      Union[str, 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   )

							lowerCAmelCase__      :      Union[str, Any]         =		down_blocks
							lowerCAmelCase__      :      List[Any]         =		controlnet_down_blocks

							# mid
							lowerCAmelCase__      :      List[Any]         =		block_out_channels[-1]
							lowerCAmelCase__      :      List[str]         =		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__      :      List[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    ,     )






				def __call__(						self					:      Union[str, Any]    ,     a					:      List[Any]    ,     a					:      List[Any]    ,     a					:      Optional[Any]    ,     a					:      str    ,     a					:      float = 1.0    ,     a					:      bool = True    ,     a					:      bool = False    ,     ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		self.controlnet_conditioning_channel_order
							if channel_order == "bgr":
										lowerCAmelCase__      :      Optional[int]         =		jnp.flip(a    ,     axis=1   )

							# 1. time
							if not isinstance(a    ,     jnp.ndarray   ):
										lowerCAmelCase__      :      Optional[Any]         =		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__      :      str         =		jnp.expand_dims(a    ,     0   )

							lowerCAmelCase__      :      Dict         =		self.time_proj(a   )
							lowerCAmelCase__      :      str         =		self.time_embedding(a   )

							# 2. pre-process
							lowerCAmelCase__      :      Optional[Any]         =		jnp.transpose(a    ,     (0, 2, 3, 1)   )
							lowerCAmelCase__      :      Any         =		self.conv_in(a   )

							lowerCAmelCase__      :      Any         =		jnp.transpose(a    ,     (0, 2, 3, 1)   )
							lowerCAmelCase__      :      Tuple         =		self.controlnet_cond_embedding(a   )
							sample += controlnet_cond

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

							# 4. mid
							lowerCAmelCase__      :      int         =		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__      :      Optional[int]         =		controlnet_block(a   )
										controlnet_down_block_res_samples += (down_block_res_sample,)

							lowerCAmelCase__      :      int         =		controlnet_down_block_res_samples

							lowerCAmelCase__      :      str         =		self.controlnet_mid_block(a   )

							# 6. scaling
							lowerCAmelCase__      :      Optional[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=a    ,     mid_block_res_sample=a   )

class      A__							:






				def __init__(						self					:      Optional[Any]    ,     a					:      list   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Dict         =		set_counts
							lowerCAmelCase__      :      str         =		max(a   )
							lowerCAmelCase__      :      Any         =		len(a   )
							lowerCAmelCase__      :      List[str]         =		[1] * num_sets
							lowerCAmelCase__      :      Dict         =		list(range(a   )   )






				def 							_lowerCamelCase		(						self					:      Dict    ,     a					:      int    ,     a					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[Any]         =		self.get_parent(a   )
							lowerCAmelCase__      :      Tuple         =		self.get_parent(a   )

							if src_parent == dst_parent:
										return False

							if self.ranks[dst_parent] >= self.ranks[src_parent]:
										self.set_counts[dst_parent] += self.set_counts[src_parent]
										lowerCAmelCase__      :      Tuple         =		0
										lowerCAmelCase__      :      str         =		dst_parent
										if self.ranks[dst_parent] == self.ranks[src_parent]:
													self.ranks[dst_parent] += 1
										lowerCAmelCase__      :      List[Any]         =		self.set_counts[dst_parent]
							else:
										self.set_counts[src_parent] += self.set_counts[dst_parent]
										lowerCAmelCase__      :      Optional[int]         =		0
										lowerCAmelCase__      :      Tuple         =		src_parent
										lowerCAmelCase__      :      Optional[int]         =		self.set_counts[src_parent]

							lowerCAmelCase__      :      Optional[Any]         =		max(self.max_set    ,     a   )
							return True






				def 							_lowerCamelCase		(						self					:      Any    ,     a					:      int   ):






							'''simple docstring'''







							if self.parents[disj_set] == disj_set:
										return disj_set
							lowerCAmelCase__      :      Tuple         =		self.get_parent(self.parents[disj_set]   )
							return self.parents[disj_set]