





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







class   _lowercase						(							nn.Module				):
   _UpperCamelCase       		=   42
   _UpperCamelCase       		=   (16, 32, 96, 2_56)
   _UpperCamelCase       		=   jnp.floataa


   def 					snake_case				( self					):
       A       :       Optional[int]							=						nn.Conv(
           self.block_out_channels[0]    ,   kernel_size=(3, 3)    ,   padding=((1, 1), (1, 1))    ,   dtype=self.dtype    ,   )

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

       A       :       List[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    ,   _UpperCAmelCase					):
       A       :       Optional[Any]							=						self.conv_in(_UpperCAmelCase					)
       A       :       Any							=						nn.silu(_UpperCAmelCase					)

       for block in self.blocks:
           A       :       Union[str, Any]							=						block(_UpperCAmelCase					)
           A       :       Optional[int]							=						nn.silu(_UpperCAmelCase					)

       A       :       Dict							=						self.conv_out(_UpperCAmelCase					)

       return embedding







@flax_register_to_config
class   _lowercase						(							nn.Module		,					a		,					a				):
   _UpperCamelCase       		=   32
   _UpperCamelCase       		=   4
   _UpperCamelCase       		=   (
       "CrossAttnDownBlock2D",
       "CrossAttnDownBlock2D",
       "CrossAttnDownBlock2D",
       "DownBlock2D",
   )
   _UpperCamelCase       		=   False
   _UpperCamelCase       		=   (3_20, 6_40, 12_80, 12_80)
   _UpperCamelCase       		=   2
   _UpperCamelCase       		=   8
   _UpperCamelCase       		=   None
   _UpperCamelCase       		=   12_80
   _UpperCamelCase       		=   0.0
   _UpperCamelCase       		=   False
   _UpperCamelCase       		=   jnp.floataa
   _UpperCamelCase       		=   True
   _UpperCamelCase       		=   0
   _UpperCamelCase       		=   "rgb"
   _UpperCamelCase       		=   (16, 32, 96, 2_56)


   def 					snake_case				( self    ,   _UpperCAmelCase					):
       # init input tensors
       A       :       Dict							=						(1, self.in_channels, self.sample_size, self.sample_size)
       A       :       Dict							=						jnp.zeros(_UpperCAmelCase    ,   dtype=jnp.floataa					)
       A       :       List[Any]							=						jnp.ones((1,)    ,   dtype=jnp.intaa					)
       A       :       Optional[Any]							=						jnp.zeros((1, 1, self.cross_attention_dim)    ,   dtype=jnp.floataa					)
       A       :       Tuple							=						(1, 3, self.sample_size * 8, self.sample_size * 8)
       A       :       Tuple							=						jnp.zeros(_UpperCAmelCase    ,   dtype=jnp.floataa					)

       A,	A       :       List[Any]							=						jax.random.split(_UpperCAmelCase					)
       A       :       Union[str, Any]							=						{'''params''': params_rng, '''dropout''': dropout_rng}

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


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

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

       # time
       A       :       Optional[int]							=						FlaxTimesteps(
           block_out_channels[0]    ,   flip_sin_to_cos=self.flip_sin_to_cos    ,   freq_shift=self.config.freq_shift					)
       A       :       Union[str, Any]							=						FlaxTimestepEmbedding(_UpperCAmelCase    ,   dtype=self.dtype					)

       A       :       Tuple							=						FlaxControlNetConditioningEmbedding(
           conditioning_embedding_channels=block_out_channels[0]    ,   block_out_channels=self.conditioning_embedding_out_channels    ,   )

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

       if isinstance(_UpperCAmelCase    ,   _UpperCAmelCase					):
           A       :       List[str]							=						(num_attention_heads,) * len(self.down_block_types					)

       # down
       A       :       List[str]							=						[]
       A       :       Optional[Any]							=						[]

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

       A       :       int							=						nn.Conv(
           _UpperCAmelCase    ,   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(_UpperCAmelCase					)

       for i, down_block_type in enumerate(self.down_block_types					):
           A       :       List[Any]							=						output_channel
           A       :       Optional[Any]							=						block_out_channels[i]
           A       :       Tuple							=						i == len(_UpperCAmelCase					) - 1

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

           down_blocks.append(_UpperCAmelCase					)

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

           if not is_final_block:
               A       :       Tuple							=						nn.Conv(
                   _UpperCAmelCase    ,   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(_UpperCAmelCase					)

       A       :       Tuple							=						down_blocks
       A       :       Dict							=						controlnet_down_blocks

       # mid
       A       :       Union[str, Any]							=						block_out_channels[-1]
       A       :       int							=						FlaxUNetMidBlockaDCrossAttn(
           in_channels=_UpperCAmelCase    ,   dropout=self.dropout    ,   num_attention_heads=num_attention_heads[-1]    ,   use_linear_projection=self.use_linear_projection    ,   dtype=self.dtype    ,   )

       A       :       Optional[int]							=						nn.Conv(
           _UpperCAmelCase    ,   kernel_size=(1, 1)    ,   padding='''VALID'''    ,   kernel_init=nn.initializers.zeros_init()    ,   bias_init=nn.initializers.zeros_init()    ,   dtype=self.dtype    ,   )


   def __call__( self    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase = 1.0    ,   _UpperCAmelCase = True    ,   _UpperCAmelCase = False    ,   ):
       A       :       int							=						self.controlnet_conditioning_channel_order
       if channel_order == "bgr":
           A       :       Optional[int]							=						jnp.flip(_UpperCAmelCase    ,   axis=1					)

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

       A       :       List[str]							=						self.time_proj(_UpperCAmelCase					)
       A       :       Tuple							=						self.time_embedding(_UpperCAmelCase					)

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

       A       :       Optional[int]							=						jnp.transpose(_UpperCAmelCase    ,   (0, 2, 3, 1)					)
       A       :       int							=						self.controlnet_cond_embedding(_UpperCAmelCase					)
       sample += controlnet_cond

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

       # 4. mid
       A       :       List[str]							=						self.mid_block(_UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   deterministic=not train					)

       # 5. contronet blocks
       A       :       int							=						()
       for down_block_res_sample, controlnet_block in zip(_UpperCAmelCase    ,   self.controlnet_down_blocks					):
           A       :       Optional[Any]							=						controlnet_block(_UpperCAmelCase					)
           controlnet_down_block_res_samples += (down_block_res_sample,)

       A       :       str							=						controlnet_down_block_res_samples

       A       :       List[Any]							=						self.controlnet_mid_block(_UpperCAmelCase					)

       # 6. scaling
       A       :       str							=						[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=_UpperCAmelCase    ,   mid_block_res_sample=_UpperCAmelCase					)







'''simple docstring'''






def 							_lowerCamelCase(  UpperCamelCase__    : int		)		->  None:
    A       :       List[Any]							=						generate_pascal_triangle(UpperCamelCase__		)
    for row_idx in range(UpperCamelCase__		):
        # Print left spaces
        for _ in range(num_rows - row_idx - 1		):
            print(end=''' '''		)
        # Print row values
        for col_idx in range(row_idx + 1		):
            if col_idx != row_idx:
                print(triangle[row_idx][col_idx]    ,       end=''' '''		)
            else:
                print(triangle[row_idx][col_idx]    ,       end=''''''		)
        print()
def 							_lowerCamelCase(  UpperCamelCase__    : int		)		->  list[list[int]]:

    if not isinstance(UpperCamelCase__    ,       UpperCamelCase__		):
        raise TypeError('''The input value of \'num_rows\' should be \'int\''''		)

    if num_rows == 0:
        return []
    elif num_rows < 0:
        raise ValueError(
            '''The input value of \'num_rows\' should be greater than or equal to 0'''		)

    A       :       list[list[int]]							=						[]
    for current_row_idx in range(UpperCamelCase__		):
        A       :       Optional[int]							=						populate_current_row(UpperCamelCase__    ,       UpperCamelCase__		)
        triangle.append(UpperCamelCase__		)
    return triangle
def 							_lowerCamelCase(  UpperCamelCase__    : list[list[int]]    ,       UpperCamelCase__    : int		)		->  list[int]:
    A       :       Dict							=						[-1] * (current_row_idx + 1)
    # first and last elements of current row are equal to 1
    A,	A       :       Union[str, Any]							=						1, 1
    for current_col_idx in range(1    ,       UpperCamelCase__		):
        calculate_current_element(
            UpperCamelCase__    ,       UpperCamelCase__    ,       UpperCamelCase__    ,       UpperCamelCase__		)
    return current_row
def 							_lowerCamelCase(  UpperCamelCase__    : list[list[int]]    ,       UpperCamelCase__    : list[int]    ,       UpperCamelCase__    : int    ,       UpperCamelCase__    : int    ,       )		->  None:
    A       :       Dict							=						triangle[current_row_idx - 1][current_col_idx - 1]
    A       :       Any							=						triangle[current_row_idx - 1][current_col_idx]
    A       :       Optional[int]							=						above_to_left_elt + above_to_right_elt
def 							_lowerCamelCase(  UpperCamelCase__    : int		)		->  list[list[int]]:

    if not isinstance(UpperCamelCase__    ,       UpperCamelCase__		):
        raise TypeError('''The input value of \'num_rows\' should be \'int\''''		)

    if num_rows == 0:
        return []
    elif num_rows < 0:
        raise ValueError(
            '''The input value of \'num_rows\' should be greater than or equal to 0'''		)

    A       :       list[list[int]]							=						[[1]]

    for row_index in range(1    ,       UpperCamelCase__		):
        A       :       str							=						[0] + result[-1] + [0]
        A       :       Optional[Any]							=						row_index + 1
        # Calculate the number of distinct elements in a row
        A       :       str							=						sum(divmod(UpperCamelCase__    ,       2		)		)
        A       :       Optional[int]							=						[
            temp_row[i - 1] + temp_row[i] for i in range(1    ,       distinct_elements + 1		)
        ]
        A       :       Any							=						row_first_half[: (row_index + 1) // 2]
        row_second_half.reverse()
        A       :       Any							=						row_first_half + row_second_half
        result.append(UpperCamelCase__		)

    return result
def 							_lowerCamelCase(  )		->  None:
    from collections.abc import Callable
    from timeit import timeit

    def benchmark_a_function(UpperCamelCase__    : Callable    ,       UpperCamelCase__    : int		) -> None:
        A       :       Dict							=						F'''{func.__name__}({value})'''
        A       :       List[Any]							=						timeit(F'''__main__.{call}'''    ,       setup='''import __main__'''		)
        # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
        print(F'''{call:38} -- {timing:.4f} seconds'''		)

    for value in range(15		):  # (1, 7, 14):
        for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
            benchmark_a_function(UpperCamelCase__    ,       UpperCamelCase__		)
        print()


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     benchmark()
