

'''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 							_snake_case (       _snake_case						):
   _A       :     jnp.ndarray
   _A       :     jnp.ndarray





class 							_snake_case (       nn.Module						):
   _A       :     int
   _A       :     Tuple[int]       =					(1_6, 3_2, 9_6, 2_5_6)
   _A       :     jnp.dtype       =					jnp.floataa
   def     __UpperCamelCase						( self			:      Dict							):
    SCREAMING_SNAKE_CASE:Any						      =					nn.Conv(
        self.block_out_channels[0]     ,kernel_size=(3, 3)     ,padding=((1, 1), (1, 1))     ,dtype=self.dtype     ,)

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

    SCREAMING_SNAKE_CASE: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			:      int     ,SCREAMING_SNAKE_CASE__			:      str							):
    SCREAMING_SNAKE_CASE:List[Any]						      =					self.conv_in(snake_case_							)
    SCREAMING_SNAKE_CASE:List[Any]						      =					nn.silu(snake_case_							)

    for block in self.blocks:
     SCREAMING_SNAKE_CASE:Tuple						      =					block(snake_case_							)
     SCREAMING_SNAKE_CASE:str						      =					nn.silu(snake_case_							)

    SCREAMING_SNAKE_CASE:Dict						      =					self.conv_out(snake_case_							)

    return embedding





@flax_register_to_config
class 							_snake_case (       nn.Module	,  _snake_case	,  _snake_case						):
   _A       :     int       =					3_2
   _A       :     int       =					4
   _A       :     Tuple[str]       =					(
       "CrossAttnDownBlock2D",
       "CrossAttnDownBlock2D",
       "CrossAttnDownBlock2D",
       "DownBlock2D",
   )
   _A       :     Union[bool, Tuple[bool]]       =					False
   _A       :     Tuple[int]       =					(3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
   _A       :     int       =					2
   _A       :     Union[int, Tuple[int]]       =					8
   _A       :     Optional[Union[int, Tuple[int]]]       =					None
   _A       :     int       =					1_2_8_0
   _A       :     float       =					0.0
   _A       :     bool       =					False
   _A       :     jnp.dtype       =					jnp.floataa
   _A       :     bool       =					True
   _A       :     int       =					0
   _A       :     str       =					"rgb"
   _A       :     Tuple[int]       =					(1_6, 3_2, 9_6, 2_5_6)
   def     __UpperCamelCase						( self			:      List[str]     ,SCREAMING_SNAKE_CASE__			:      Optional[Any]							):
    # init input tensors
    SCREAMING_SNAKE_CASE:int						      =					(1, self.in_channels, self.sample_size, self.sample_size)
    SCREAMING_SNAKE_CASE:Dict						      =					jnp.zeros(snake_case_     ,dtype=jnp.floataa							)
    SCREAMING_SNAKE_CASE:Any						      =					jnp.ones((1,)     ,dtype=jnp.intaa							)
    SCREAMING_SNAKE_CASE:Any						      =					jnp.zeros((1, 1, self.cross_attention_dim)     ,dtype=jnp.floataa							)
    SCREAMING_SNAKE_CASE:int						      =					(1, 3, self.sample_size * 8, self.sample_size * 8)
    SCREAMING_SNAKE_CASE:Dict						      =					jnp.zeros(snake_case_     ,dtype=jnp.floataa							)

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

    return self.init(snake_case_     ,snake_case_     ,snake_case_     ,snake_case_     ,snake_case_							)["params"]
   def     __UpperCamelCase						( self			:      Tuple							):
    SCREAMING_SNAKE_CASE:int						      =					self.block_out_channels
    SCREAMING_SNAKE_CASE:Tuple						      =					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.
    SCREAMING_SNAKE_CASE:int						      =					self.num_attention_heads or self.attention_head_dim

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

    # time
    SCREAMING_SNAKE_CASE:Tuple						      =					FlaxTimesteps(
        block_out_channels[0]     ,flip_sin_to_cos=self.flip_sin_to_cos     ,freq_shift=self.config.freq_shift							)
    SCREAMING_SNAKE_CASE:Tuple						      =					FlaxTimestepEmbedding(snake_case_     ,dtype=self.dtype							)

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

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

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

    # down
    SCREAMING_SNAKE_CASE:Optional[Any]						      =					[]
    SCREAMING_SNAKE_CASE:Optional[int]						      =					[]

    SCREAMING_SNAKE_CASE:List[Any]						      =					block_out_channels[0]

    SCREAMING_SNAKE_CASE:Union[str, Any]						      =					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							):
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					output_channel
     SCREAMING_SNAKE_CASE:Dict						      =					block_out_channels[i]
     SCREAMING_SNAKE_CASE:int						      =					i == len(snake_case_							) - 1

     if down_block_type == "CrossAttnDownBlock2D":
      SCREAMING_SNAKE_CASE:Tuple						      =					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:
      SCREAMING_SNAKE_CASE:Optional[Any]						      =					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							):
      SCREAMING_SNAKE_CASE:Tuple						      =					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:
      SCREAMING_SNAKE_CASE:Dict						      =					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_							)

    SCREAMING_SNAKE_CASE:int						      =					down_blocks
    SCREAMING_SNAKE_CASE:Dict						      =					controlnet_down_blocks

    # mid
    SCREAMING_SNAKE_CASE:Union[str, Any]						      =					block_out_channels[-1]
    SCREAMING_SNAKE_CASE:int						      =					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     ,)

    SCREAMING_SNAKE_CASE:Union[str, Any]						      =					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			:      Union[str, Any]     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      int     ,SCREAMING_SNAKE_CASE__			:      List[str] = 1.0     ,SCREAMING_SNAKE_CASE__			:      List[Any] = True     ,SCREAMING_SNAKE_CASE__			:      str = False     ,):
    SCREAMING_SNAKE_CASE:Union[str, Any]						      =					self.controlnet_conditioning_channel_order
    if channel_order == "bgr":
     SCREAMING_SNAKE_CASE:List[str]						      =					jnp.flip(snake_case_     ,axis=1							)

    # 1. time
    if not isinstance(snake_case_     ,jnp.ndarray							):
     SCREAMING_SNAKE_CASE:int						      =					jnp.array([timesteps]     ,dtype=jnp.intaa							)
    elif isinstance(snake_case_     ,jnp.ndarray							) and len(timesteps.shape							) == 0:
     SCREAMING_SNAKE_CASE:Optional[int]						      =					timesteps.astype(dtype=jnp.floataa							)
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					jnp.expand_dims(snake_case_     ,0							)

    SCREAMING_SNAKE_CASE:Any						      =					self.time_proj(snake_case_							)
    SCREAMING_SNAKE_CASE:Optional[int]						      =					self.time_embedding(snake_case_							)

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

    SCREAMING_SNAKE_CASE:str						      =					jnp.transpose(snake_case_     ,(0, 2, 3, 1)							)
    SCREAMING_SNAKE_CASE:Dict						      =					self.controlnet_cond_embedding(snake_case_							)
    sample += controlnet_cond

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

    # 4. mid
    SCREAMING_SNAKE_CASE:Any						      =					self.mid_block(snake_case_     ,snake_case_     ,snake_case_     ,deterministic=not train							)

    # 5. contronet blocks
    SCREAMING_SNAKE_CASE:Tuple						      =					()
    for down_block_res_sample, controlnet_block in zip(snake_case_     ,self.controlnet_down_blocks							):
     SCREAMING_SNAKE_CASE:Dict						      =					controlnet_block(snake_case_							)
     controlnet_down_block_res_samples += (down_block_res_sample,)

    SCREAMING_SNAKE_CASE:List[str]						      =					controlnet_down_block_res_samples

    SCREAMING_SNAKE_CASE:Tuple						      =					self.controlnet_mid_block(snake_case_							)

    # 6. scaling
    SCREAMING_SNAKE_CASE:int						      =					[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_							)









'''simple docstring'''

def    A_  ( snake_case = 100 ):
  SCREAMING_SNAKE_CASE:Dict						      =					0
  SCREAMING_SNAKE_CASE:Optional[int]						      =					0
  for i in range(1      ,       n + 1 ):
    sum_of_squares += i**2
    sum_of_ints += i
  return sum_of_ints**2 - sum_of_squares


if __name__ == "__main__":
    print(f'''{solution() = }''')






