



'''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 _UpperCamelCase		(     A		):





				'''simple docstring'''





				lowerCAmelCase__					    =    42
				lowerCAmelCase__					    =    42





class _UpperCamelCase		(     nn.Module		):





				'''simple docstring'''





				lowerCAmelCase__					    =    42
				lowerCAmelCase__					    =    (16, 32, 96, 2_56)
				lowerCAmelCase__					    =    jnp.floataa

				def 				__lowerCamelCase     (							self  :  str):







											'''simple docstring'''

											__lowercase       	=nn.Conv(
											    self.block_out_channels[0]  ,       kernel_size=(3, 3)  ,       padding=((1, 1), (1, 1))  ,       dtype=self.dtype  ,       )

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

											__lowercase       	=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  :  int  ,       _lowerCAmelCase  :  Dict):







											'''simple docstring'''

											__lowercase       	=self.conv_in(_lowerCAmelCase)
											__lowercase       	=nn.silu(_lowerCAmelCase)

											for block in self.blocks:
																		__lowercase       	=block(_lowerCAmelCase)
																		__lowercase       	=nn.silu(_lowerCAmelCase)

											__lowercase       	=self.conv_out(_lowerCAmelCase)

											return embedding





@flax_register_to_config
class _UpperCamelCase		(     nn.Module      ,       A      ,       A		):





				'''simple docstring'''





				lowerCAmelCase__					    =    32
				lowerCAmelCase__					    =    4
				lowerCAmelCase__					    =    (
				    """CrossAttnDownBlock2D""",
				    """CrossAttnDownBlock2D""",
				    """CrossAttnDownBlock2D""",
				    """DownBlock2D""",
				)
				lowerCAmelCase__					    =    False
				lowerCAmelCase__					    =    (3_20, 6_40, 12_80, 12_80)
				lowerCAmelCase__					    =    2
				lowerCAmelCase__					    =    8
				lowerCAmelCase__					    =    None
				lowerCAmelCase__					    =    12_80
				lowerCAmelCase__					    =    0.0
				lowerCAmelCase__					    =    False
				lowerCAmelCase__					    =    jnp.floataa
				lowerCAmelCase__					    =    True
				lowerCAmelCase__					    =    0
				lowerCAmelCase__					    =    """rgb"""
				lowerCAmelCase__					    =    (16, 32, 96, 2_56)

				def 				__lowerCamelCase     (							self  :  Optional[int]  ,       _lowerCAmelCase  :  jax.random.KeyArray):







											'''simple docstring'''

											__lowercase       	=(1, self.in_channels, self.sample_size, self.sample_size)
											__lowercase       	=jnp.zeros(_lowerCAmelCase  ,       dtype=jnp.floataa)
											__lowercase       	=jnp.ones((1,)  ,       dtype=jnp.intaa)
											__lowercase       	=jnp.zeros((1, 1, self.cross_attention_dim)  ,       dtype=jnp.floataa)
											__lowercase       	=(1, 3, self.sample_size * 8, self.sample_size * 8)
											__lowercase       	=jnp.zeros(_lowerCAmelCase  ,       dtype=jnp.floataa)

											__lowercase	,				__lowercase       	=jax.random.split(_lowerCAmelCase)
											__lowercase       	={'params': params_rng, 'dropout': dropout_rng}

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

				def 				__lowerCamelCase     (							self  :  Tuple):







											'''simple docstring'''

											__lowercase       	=self.block_out_channels
											__lowercase       	=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.
											__lowercase       	=self.num_attention_heads or self.attention_head_dim

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

											# time
											__lowercase       	=FlaxTimesteps(
											    block_out_channels[0]  ,       flip_sin_to_cos=self.flip_sin_to_cos  ,       freq_shift=self.config.freq_shift)
											__lowercase       	=FlaxTimestepEmbedding(_lowerCAmelCase  ,       dtype=self.dtype)

											__lowercase       	=FlaxControlNetConditioningEmbedding(
											    conditioning_embedding_channels=block_out_channels[0]  ,       block_out_channels=self.conditioning_embedding_out_channels  ,       )

											__lowercase       	=self.only_cross_attention
											if isinstance(_lowerCAmelCase  ,       _lowerCAmelCase):
																		__lowercase       	=(only_cross_attention,) * len(self.down_block_types)

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

											# down
											__lowercase       	=[]
											__lowercase       	=[]

											__lowercase       	=block_out_channels[0]

											__lowercase       	=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):
																		__lowercase       	=output_channel
																		__lowercase       	=block_out_channels[i]
																		__lowercase       	=i == len(_lowerCAmelCase) - 1

																		if down_block_type == "CrossAttnDownBlock2D":
																									__lowercase       	=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:
																									__lowercase       	=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):
																									__lowercase       	=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:
																									__lowercase       	=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)

											__lowercase       	=down_blocks
											__lowercase       	=controlnet_down_blocks

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

											__lowercase       	=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  :  Tuple  ,       _lowerCAmelCase  :  Optional[int]  ,       _lowerCAmelCase  :  Optional[int]  ,       _lowerCAmelCase  :  str  ,       _lowerCAmelCase  :  Dict  ,       _lowerCAmelCase  :  float = 1.0  ,       _lowerCAmelCase  :  bool = True  ,       _lowerCAmelCase  :  bool = False  ,       ):







											'''simple docstring'''

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

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

											__lowercase       	=self.time_proj(_lowerCAmelCase)
											__lowercase       	=self.time_embedding(_lowerCAmelCase)

											# 2. pre-process
											__lowercase       	=jnp.transpose(_lowerCAmelCase  ,       (0, 2, 3, 1))
											__lowercase       	=self.conv_in(_lowerCAmelCase)

											__lowercase       	=jnp.transpose(_lowerCAmelCase  ,       (0, 2, 3, 1))
											__lowercase       	=self.controlnet_cond_embedding(_lowerCAmelCase)
											sample += controlnet_cond

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

											# 4. mid
											__lowercase       	=self.mid_block(_lowerCAmelCase  ,       _lowerCAmelCase  ,       _lowerCAmelCase  ,       deterministic=not train)

											# 5. contronet blocks
											__lowercase       	=()
											for down_block_res_sample, controlnet_block in zip(_lowerCAmelCase  ,       self.controlnet_down_blocks):
																		__lowercase       	=controlnet_block(_lowerCAmelCase)
																		controlnet_down_block_res_samples += (down_block_res_sample,)

											__lowercase       	=controlnet_down_block_res_samples

											__lowercase       	=self.controlnet_mid_block(_lowerCAmelCase)

											# 6. scaling
											__lowercase       	=[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'''



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							if not isinstance(_lowerCAmelCase  ,   _lowerCAmelCase					):
														raise TypeError('only integers accepted as input'					)
							else:
														__lowercase       	=str(abs(_lowerCAmelCase					)					)
														__lowercase       	=[list(_lowerCAmelCase					) for char in range(len(_lowerCAmelCase					)					)]
														for index in range(len(_lowerCAmelCase					)					):
																					num_transpositions[index].pop(_lowerCAmelCase					)
														return max(
														    int(''.join(list(_lowerCAmelCase					)					)					) for transposition in num_transpositions					)


if __name__ == "__main__":
		__import__("""doctest""").testmod()



