

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		(  __lowerCamelCase):
  SCREAMING_SNAKE_CASE    :      List[str]       =  42
  SCREAMING_SNAKE_CASE    :      Any       =  42

class  _A		(  nn.Module):
  SCREAMING_SNAKE_CASE    :      Optional[int]       =  42
  SCREAMING_SNAKE_CASE    :      Union[str, Any]       =  (16, 32, 96, 256)
  SCREAMING_SNAKE_CASE    :      List[Any]       =  jnp.floataa

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







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

        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     []
        for i in range(len(self.block_out_channels					) - 1					):
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.block_out_channels[i]
              SCREAMING_SNAKE_CASE_				:  List[str]           =     self.block_out_channels[i + 1]
              SCREAMING_SNAKE_CASE_				:  int           =     nn.Conv(
                  SCREAMING_SNAKE_CASE_ ,					kernel_size=(3, 3) ,					padding=((1, 1), (1, 1)) ,					dtype=self.dtype ,					)
              blocks.append(SCREAMING_SNAKE_CASE_					)
              SCREAMING_SNAKE_CASE_				:  Any           =     nn.Conv(
                  SCREAMING_SNAKE_CASE_ ,					kernel_size=(3, 3) ,					strides=(2, 2) ,					padding=((1, 1), (1, 1)) ,					dtype=self.dtype ,					)
              blocks.append(SCREAMING_SNAKE_CASE_					)
        SCREAMING_SNAKE_CASE_				:  Optional[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 ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.conv_in(SCREAMING_SNAKE_CASE_					)
        SCREAMING_SNAKE_CASE_				:  int           =     nn.silu(SCREAMING_SNAKE_CASE_					)

        for block in self.blocks:
              SCREAMING_SNAKE_CASE_				:  int           =     block(SCREAMING_SNAKE_CASE_					)
              SCREAMING_SNAKE_CASE_				:  Tuple           =     nn.silu(SCREAMING_SNAKE_CASE_					)

        SCREAMING_SNAKE_CASE_				:  str           =     self.conv_out(SCREAMING_SNAKE_CASE_					)

        return embedding

@flax_register_to_config
class  _A		(  nn.Module   ,     __lowerCamelCase   ,     __lowerCamelCase):
  SCREAMING_SNAKE_CASE    :      Optional[Any]       =  32
  SCREAMING_SNAKE_CASE    :      Any       =  4
  SCREAMING_SNAKE_CASE    :      Tuple       =  (
      '''CrossAttnDownBlock2D''',
      '''CrossAttnDownBlock2D''',
      '''CrossAttnDownBlock2D''',
      '''DownBlock2D''',
  )
  SCREAMING_SNAKE_CASE    :      List[Any]       =  False
  SCREAMING_SNAKE_CASE    :      Any       =  (320, 640, 1_280, 1_280)
  SCREAMING_SNAKE_CASE    :      List[str]       =  2
  SCREAMING_SNAKE_CASE    :      Optional[int]       =  8
  SCREAMING_SNAKE_CASE    :      Dict       =  None
  SCREAMING_SNAKE_CASE    :      Union[str, Any]       =  1_280
  SCREAMING_SNAKE_CASE    :      str       =  0.0
  SCREAMING_SNAKE_CASE    :      List[Any]       =  False
  SCREAMING_SNAKE_CASE    :      Optional[Any]       =  jnp.floataa
  SCREAMING_SNAKE_CASE    :      List[str]       =  True
  SCREAMING_SNAKE_CASE    :      List[Any]       =  0
  SCREAMING_SNAKE_CASE    :      int       =  '''rgb'''
  SCREAMING_SNAKE_CASE    :      Tuple       =  (16, 32, 96, 256)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     (1, self.in_channels, self.sample_size, self.sample_size)
        SCREAMING_SNAKE_CASE_				:  Any           =     jnp.zeros(SCREAMING_SNAKE_CASE_ ,					dtype=jnp.floataa					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     jnp.ones((1,) ,					dtype=jnp.intaa					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     jnp.zeros((1, 1, self.cross_attention_dim) ,					dtype=jnp.floataa					)
        SCREAMING_SNAKE_CASE_				:  str           =     (1, 3, self.sample_size * 8, self.sample_size * 8)
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     jnp.zeros(SCREAMING_SNAKE_CASE_ ,					dtype=jnp.floataa					)

        SCREAMING_SNAKE_CASE_       ,						SCREAMING_SNAKE_CASE_				:  str           =     jax.random.split(SCREAMING_SNAKE_CASE_					)
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     {'params': params_rng, 'dropout': dropout_rng}

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

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     self.block_out_channels
        SCREAMING_SNAKE_CASE_				:  int           =     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_				:  Dict           =     self.num_attention_heads or self.attention_head_dim

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

        SCREAMING_SNAKE_CASE_				:  str           =     FlaxControlNetConditioningEmbedding(
            conditioning_embedding_channels=block_out_channels[0] ,					block_out_channels=self.conditioning_embedding_out_channels ,					)

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

        if isinstance(SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_					):
              SCREAMING_SNAKE_CASE_				:  str           =     (num_attention_heads,) * len(self.down_block_types					)

        # down
        SCREAMING_SNAKE_CASE_				:  Tuple           =     []
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     []

        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     block_out_channels[0]

        SCREAMING_SNAKE_CASE_				:  List[Any]           =     nn.Conv(
            SCREAMING_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(SCREAMING_SNAKE_CASE_					)

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

              if down_block_type == "CrossAttnDownBlock2D":
                    SCREAMING_SNAKE_CASE_				:  Any           =     FlaxCrossAttnDownBlockaD(
                        in_channels=SCREAMING_SNAKE_CASE_ ,					out_channels=SCREAMING_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_				:  List[Any]           =     FlaxDownBlockaD(
                        in_channels=SCREAMING_SNAKE_CASE_ ,					out_channels=SCREAMING_SNAKE_CASE_ ,					dropout=self.dropout ,					num_layers=self.layers_per_block ,					add_downsample=not is_final_block ,					dtype=self.dtype ,					)

              down_blocks.append(SCREAMING_SNAKE_CASE_					)

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

              if not is_final_block:
                    SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     nn.Conv(
                        SCREAMING_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(SCREAMING_SNAKE_CASE_					)

        SCREAMING_SNAKE_CASE_				:  Any           =     down_blocks
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     controlnet_down_blocks

        # mid
        SCREAMING_SNAKE_CASE_				:  Tuple           =     block_out_channels[-1]
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     FlaxUNetMidBlockaDCrossAttn(
            in_channels=SCREAMING_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_				:  Any           =     nn.Conv(
            SCREAMING_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 ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE = 1.0 ,					_SCREAMING_SNAKE_CASE = True ,					_SCREAMING_SNAKE_CASE = False ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     self.controlnet_conditioning_channel_order
        if channel_order == "bgr":
              SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     jnp.flip(SCREAMING_SNAKE_CASE_ ,					axis=1					)

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

        SCREAMING_SNAKE_CASE_				:  Tuple           =     self.time_proj(SCREAMING_SNAKE_CASE_					)
        SCREAMING_SNAKE_CASE_				:  str           =     self.time_embedding(SCREAMING_SNAKE_CASE_					)

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

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

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

        # 4. mid
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     self.mid_block(SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					deterministic=not train					)

        # 5. contronet blocks
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     ()
        for down_block_res_sample, controlnet_block in zip(SCREAMING_SNAKE_CASE_ ,					self.controlnet_down_blocks					):
              SCREAMING_SNAKE_CASE_				:  List[str]           =     controlnet_block(SCREAMING_SNAKE_CASE_					)
              controlnet_down_block_res_samples += (down_block_res_sample,)

        SCREAMING_SNAKE_CASE_				:  str           =     controlnet_down_block_res_samples

        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.controlnet_mid_block(SCREAMING_SNAKE_CASE_					)

        # 6. scaling
        SCREAMING_SNAKE_CASE_				:  List[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=SCREAMING_SNAKE_CASE_ ,					mid_block_res_sample=SCREAMING_SNAKE_CASE_					)









def    A_  (							a  ):

      """simple docstring"""



      if upper_limit < 0:
            raise ValueError('Limit for the Catalan sequence must be ≥ 0'  )

      SCREAMING_SNAKE_CASE_				:  List[str]           =     [0] * (upper_limit + 1)

      # Base case: C(0) = C(1) = 1
      SCREAMING_SNAKE_CASE_				:  Any           =     1
      if upper_limit > 0:
            SCREAMING_SNAKE_CASE_				:  int           =     1

      # Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
      for i in range(2					,   upper_limit + 1  ):
            for j in range(a  ):
                  catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

      return catalan_list


if __name__ == "__main__":
    print('\n********* Catalan Numbers Using Dynamic Programming ************\n')
    print('\n*** Enter -1 at any time to quit ***')
    print('\nEnter the upper limit (≥ 0) for the Catalan number sequence: ', end='')
    try:
        while True:
            lowerCAmelCase     :   Union[str, Any]						     =  int(input().strip())
            if N < 0:
                print('\n********* Goodbye!! ************')
                break
            else:
                print(F'The Catalan numbers from 0 through {N} are:')
                print(catalan_numbers(N))
                print('Try another upper limit for the sequence: ', end='')
    except (NameError, ValueError):
        print('\n********* Invalid input, goodbye! ************\n')

    import doctest

    doctest.testmod()






