
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( a			):
				snake_case_						   =    42
				snake_case_						   =    42







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




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Optional[Any]:


								'''simple docstring'''




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

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

								__a										=    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		,     _snake_case				)    ->      int:


								'''simple docstring'''




								__a										=    self.conv_in(_snake_case				)
								__a										=    nn.silu(_snake_case				)

								for block in self.blocks:
												__a										=    block(_snake_case				)
												__a										=    nn.silu(_snake_case				)

								__a										=    self.conv_out(_snake_case				)

								return embedding







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




				def 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case				)    ->      FrozenDict:


								'''simple docstring'''




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

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

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




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      List[Any]:


								'''simple docstring'''




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

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

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

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

								__a										=    self.only_cross_attention
								if isinstance(_snake_case		,     _snake_case				):
												__a										=    (only_cross_attention,) * len(self.down_block_types				)

								if isinstance(_snake_case		,     _snake_case				):
												__a										=    (num_attention_heads,) * len(self.down_block_types				)

								# down
								__a										=    []
								__a										=    []

								__a										=    block_out_channels[0]

								__a										=    nn.Conv(
								    _snake_case		,     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(_snake_case				)

								for i, down_block_type in enumerate(self.down_block_types				):
												__a										=    output_channel
												__a										=    block_out_channels[i]
												__a										=    i == len(_snake_case				) - 1

												if down_block_type == "CrossAttnDownBlock2D":
																__a										=    FlaxCrossAttnDownBlockaD(
																    in_channels=_snake_case		,     out_channels=_snake_case		,     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:
																__a										=    FlaxDownBlockaD(
																    in_channels=_snake_case		,     out_channels=_snake_case		,     dropout=self.dropout		,     num_layers=self.layers_per_block		,     add_downsample=not is_final_block		,     dtype=self.dtype		,     )

												down_blocks.append(_snake_case				)

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

												if not is_final_block:
																__a										=    nn.Conv(
																    _snake_case		,     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(_snake_case				)

								__a										=    down_blocks
								__a										=    controlnet_down_blocks

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

								__a										=    nn.Conv(
								    _snake_case		,     kernel_size=(1, 1)		,     padding='''VALID'''		,     kernel_init=nn.initializers.zeros_init()		,     bias_init=nn.initializers.zeros_init()		,     dtype=self.dtype		,     )




				def __call__(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case = 1.0		,     _snake_case = True		,     _snake_case = False		,     )    ->      Union[FlaxControlNetOutput, Tuple]:


								'''simple docstring'''




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

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

								__a										=    self.time_proj(_snake_case				)
								__a										=    self.time_embedding(_snake_case				)

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

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

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

								# 4. mid
								__a										=    self.mid_block(_snake_case		,     _snake_case		,     _snake_case		,     deterministic=not train				)

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

								__a										=    controlnet_down_block_res_samples

								__a										=    self.controlnet_mid_block(_snake_case				)

								# 6. scaling
								__a										=    [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=_snake_case		,     mid_block_res_sample=_snake_case				)

from math import ceil


def     __lowerCAmelCase			(			a__ = 1001  )       ->  int:
				__a										=    1

				for i in range(1						,    int(ceil(n / 2.0  )  )  ):
								__a										=    2 * i + 1
								__a										=    2 * i
								__a										=    total + 4 * odd**2 - 6 * even

				return total


if __name__ == "__main__":
					import sys

					if len(sys.argv) == 1:
										print(solution())
					else:
										try:
															A     :	List[Any]    = int(sys.argv[1])
															print(solution(n))
										except ValueError:
															print('Invalid entry - please enter a number')