



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







@flax.struct.dataclass
class    __lowercase     (					_UpperCAmelCase):







						"""simple docstring"""







						_A		:							Optional[Any]							       =		42
						_A		:							List[str]							       =		42







class    __lowercase     (					nn.Module):







						"""simple docstring"""







						_A		:							int							       =		42
						_A		:							List[Any]							       =		(16, 32, 96, 256)
						_A		:							Optional[int]							       =		jnp.floataa







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Optional[Any] 					=					nn.Conv(
											    self.block_out_channels[0]   ,		kernel_size=(3, 3)   ,		padding=((1, 1), (1, 1))   ,		dtype=self.dtype   ,		)

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

											snake_case_					:		Any 					=					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   ,		lowercase__       ):
											snake_case_					:		Union[str, Any] 					=					self.conv_in(_lowerCAmelCase       )
											snake_case_					:		Tuple 					=					nn.silu(_lowerCAmelCase       )

											for block in self.blocks:
																snake_case_					:		str 					=					block(_lowerCAmelCase       )
																snake_case_					:		int 					=					nn.silu(_lowerCAmelCase       )

											snake_case_					:		str 					=					self.conv_out(_lowerCAmelCase       )

											return embedding







@flax_register_to_config
class    __lowercase     (					nn.Module					,    _UpperCAmelCase					,    _UpperCAmelCase):







						"""simple docstring"""







						_A		:							Dict							       =		32
						_A		:							List[str]							       =		4
						_A		:							Optional[int]							       =		(
						    """CrossAttnDownBlock2D""",
						    """CrossAttnDownBlock2D""",
						    """CrossAttnDownBlock2D""",
						    """DownBlock2D""",
						)
						_A		:							Optional[Any]							       =		False
						_A		:							Union[str, Any]							       =		(320, 640, 1280, 1280)
						_A		:							str							       =		2
						_A		:							Optional[int]							       =		8
						_A		:							str							       =		None
						_A		:							Any							       =		1280
						_A		:							List[Any]							       =		0.0
						_A		:							Optional[int]							       =		False
						_A		:							str							       =		jnp.floataa
						_A		:							List[Any]							       =		True
						_A		:							Optional[Any]							       =		0
						_A		:							Union[str, Any]							       =		"""rgb"""
						_A		:							str							       =		(16, 32, 96, 256)







						def 						__UpperCamelCase							(self   ,		lowercase__       ):
											# init input tensors
											snake_case_					:		Optional[Any] 					=					(1, self.in_channels, self.sample_size, self.sample_size)
											snake_case_					:		Any 					=					jnp.zeros(_lowerCAmelCase   ,		dtype=jnp.floataa       )
											snake_case_					:		Tuple 					=					jnp.ones((1,)   ,		dtype=jnp.intaa       )
											snake_case_					:		Tuple 					=					jnp.zeros((1, 1, self.cross_attention_dim)   ,		dtype=jnp.floataa       )
											snake_case_					:		int 					=					(1, 3, self.sample_size * 8, self.sample_size * 8)
											snake_case_					:		Dict 					=					jnp.zeros(_lowerCAmelCase   ,		dtype=jnp.floataa       )

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

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







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Dict 					=					self.block_out_channels
											snake_case_					:		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.
											snake_case_					:		int 					=					self.num_attention_heads or self.attention_head_dim

											# input
											snake_case_					:		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_					:		List[Any] 					=					FlaxTimesteps(
											    block_out_channels[0]   ,		flip_sin_to_cos=self.flip_sin_to_cos   ,		freq_shift=self.config.freq_shift       )
											snake_case_					:		Tuple 					=					FlaxTimestepEmbedding(_lowerCAmelCase   ,		dtype=self.dtype       )

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

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

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

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

											snake_case_					:		Tuple 					=					block_out_channels[0]

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

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

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

																down_blocks.append(_lowerCAmelCase       )

																for _ in range(self.layers_per_block       ):
																					snake_case_					:		int 					=					nn.Conv(
																					    _lowerCAmelCase   ,		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(_lowerCAmelCase       )

																if not is_final_block:
																					snake_case_					:		Union[str, Any] 					=					nn.Conv(
																					    _lowerCAmelCase   ,		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(_lowerCAmelCase       )

											snake_case_					:		int 					=					down_blocks
											snake_case_					:		int 					=					controlnet_down_blocks

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

											snake_case_					:		List[str] 					=					nn.Conv(
											    _lowerCAmelCase   ,		kernel_size=(1, 1)   ,		padding="""VALID"""   ,		kernel_init=nn.initializers.zeros_init()   ,		bias_init=nn.initializers.zeros_init()   ,		dtype=self.dtype   ,		)







						def __call__(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__ = 1.0   ,		lowercase__ = True   ,		lowercase__ = False   ,		):
											snake_case_					:		List[Any] 					=					self.controlnet_conditioning_channel_order
											if channel_order == "bgr":
																snake_case_					:		Dict 					=					jnp.flip(_lowerCAmelCase   ,		axis=1       )

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

											snake_case_					:		Any 					=					self.time_proj(_lowerCAmelCase       )
											snake_case_					:		List[str] 					=					self.time_embedding(_lowerCAmelCase       )

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

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

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

											# 4. mid
											snake_case_					:		Tuple 					=					self.mid_block(_lowerCAmelCase   ,		_lowerCAmelCase   ,		_lowerCAmelCase   ,		deterministic=not train       )

											# 5. contronet blocks
											snake_case_					:		str 					=					()
											for down_block_res_sample, controlnet_block in zip(_lowerCAmelCase   ,		self.controlnet_down_blocks       ):
																snake_case_					:		Union[str, Any] 					=					controlnet_block(_lowerCAmelCase       )
																controlnet_down_block_res_samples += (down_block_res_sample,)

											snake_case_					:		List[str] 					=					controlnet_down_block_res_samples

											snake_case_					:		int 					=					self.controlnet_mid_block(_lowerCAmelCase       )

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









"""simple docstring"""

import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import CONFIG_MAPPING


a_			   =					logging.get_logger(__name__)







class    __lowercase     (					_UpperCAmelCase):







						"""simple docstring"""







						_A		:							Optional[int]							       =		"""upernet"""







						def __init__(self   ,		lowercase__=None   ,		lowercase__=5_12   ,		lowercase__=0.02   ,		lowercase__=[1, 2, 3, 6]   ,		lowercase__=True   ,		lowercase__=0.4   ,		lowercase__=3_84   ,		lowercase__=2_56   ,		lowercase__=1   ,		lowercase__=False   ,		lowercase__=2_55   ,		**lowercase__   ,		):
											super().__init__(**lowercase__       )

											if backbone_config is None:
																logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""       )
																snake_case_					:		List[Any] 					=					CONFIG_MAPPING["""resnet"""](out_features=["""stage1""", """stage2""", """stage3""", """stage4"""]       )
											elif isinstance(lowercase__   ,		lowercase__       ):
																snake_case_					:		Tuple 					=					backbone_config.get("""model_type"""       )
																snake_case_					:		List[str] 					=					CONFIG_MAPPING[backbone_model_type]
																snake_case_					:		List[Any] 					=					config_class.from_dict(lowercase__       )

											snake_case_					:		List[Any] 					=					backbone_config
											snake_case_					:		Optional[Any] 					=					hidden_size
											snake_case_					:		Any 					=					initializer_range
											snake_case_					:		str 					=					pool_scales
											snake_case_					:		Dict 					=					use_auxiliary_head
											snake_case_					:		str 					=					auxiliary_loss_weight
											snake_case_					:		List[str] 					=					auxiliary_in_channels
											snake_case_					:		Optional[Any] 					=					auxiliary_channels
											snake_case_					:		Any 					=					auxiliary_num_convs
											snake_case_					:		List[Any] 					=					auxiliary_concat_input
											snake_case_					:		List[str] 					=					loss_ignore_index







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Dict 					=					copy.deepcopy(self.__dict__       )
											snake_case_					:		Union[str, Any] 					=					self.backbone_config.to_dict()
											snake_case_					:		Any 					=					self.__class__.model_type
											return output




