


'''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    __magic_name__				(						lowerCAmelCase							):
       UpperCAmelCase		 =42
       UpperCAmelCase		 =42






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






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




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =nn.Conv(
              self.block_out_channels[0]    ,				kernel_size=(3, 3)    ,				padding=((1, 1), (1, 1))    ,				dtype=self.dtype    ,				)

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

          _UpperCAmelCase  :  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    ,				snake_case)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[Any]				       =self.conv_in(snake_case)
          _UpperCAmelCase  :  Any				       =nn.silu(snake_case)

          for block in self.blocks:
             _UpperCAmelCase  :  Tuple				       =block(snake_case)
             _UpperCAmelCase  :  Optional[Any]				       =nn.silu(snake_case)

          _UpperCAmelCase  :  Union[str, Any]				       =self.conv_out(snake_case)

          return embedding






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






       def      lowerCAmelCase						(     self    ,				snake_case)		->    FrozenDict:




          '''simple docstring'''
          # init input tensors
          _UpperCAmelCase  :  str				       =(1, self.in_channels, self.sample_size, self.sample_size)
          _UpperCAmelCase  :  Union[str, Any]				       =jnp.zeros(snake_case    ,				dtype=jnp.floataa)
          _UpperCAmelCase  :  str				       =jnp.ones((1,)    ,				dtype=jnp.intaa)
          _UpperCAmelCase  :  List[str]				       =jnp.zeros((1, 1, self.cross_attention_dim)    ,				dtype=jnp.floataa)
          _UpperCAmelCase  :  Dict				       =(1, 3, self.sample_size * 8, self.sample_size * 8)
          _UpperCAmelCase  :  int				       =jnp.zeros(snake_case    ,				dtype=jnp.floataa)

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

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






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =self.block_out_channels
          _UpperCAmelCase  :  str				       =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.
          _UpperCAmelCase  :  Optional[Any]				       =self.num_attention_heads or self.attention_head_dim

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

          # time
          _UpperCAmelCase  :  Dict				       =FlaxTimesteps(
              block_out_channels[0]    ,				flip_sin_to_cos=self.flip_sin_to_cos    ,				freq_shift=self.config.freq_shift)
          _UpperCAmelCase  :  Any				       =FlaxTimestepEmbedding(snake_case    ,				dtype=self.dtype)

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

          _UpperCAmelCase  :  Dict				       =self.only_cross_attention
          if isinstance(snake_case    ,				snake_case):
             _UpperCAmelCase  :  Optional[Any]				       =(only_cross_attention,) * len(self.down_block_types)

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

          # down
          _UpperCAmelCase  :  Dict				       =[]
          _UpperCAmelCase  :  Dict				       =[]

          _UpperCAmelCase  :  int				       =block_out_channels[0]

          _UpperCAmelCase  :  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)

          for i, down_block_type in enumerate(self.down_block_types):
             _UpperCAmelCase  :  int				       =output_channel
             _UpperCAmelCase  :  Dict				       =block_out_channels[i]
             _UpperCAmelCase  :  Union[str, Any]				       =i == len(snake_case) - 1

             if down_block_type == "CrossAttnDownBlock2D":
                _UpperCAmelCase  :  Optional[int]				       =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:
                _UpperCAmelCase  :  int				       =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):
                _UpperCAmelCase  :  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)

             if not is_final_block:
                _UpperCAmelCase  :  List[str]				       =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)

          _UpperCAmelCase  :  Union[str, Any]				       =down_blocks
          _UpperCAmelCase  :  Dict				       =controlnet_down_blocks

          # mid
          _UpperCAmelCase  :  Optional[Any]				       =block_out_channels[-1]
          _UpperCAmelCase  :  List[Any]				       =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    ,				)

          _UpperCAmelCase  :  str				       =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'''
          _UpperCAmelCase  :  str				       =self.controlnet_conditioning_channel_order
          if channel_order == "bgr":
             _UpperCAmelCase  :  str				       =jnp.flip(snake_case    ,				axis=1)

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

          _UpperCAmelCase  :  Optional[Any]				       =self.time_proj(snake_case)
          _UpperCAmelCase  :  Optional[Any]				       =self.time_embedding(snake_case)

          # 2. pre-process
          _UpperCAmelCase  :  str				       =jnp.transpose(snake_case    ,				(0, 2, 3, 1))
          _UpperCAmelCase  :  int				       =self.conv_in(snake_case)

          _UpperCAmelCase  :  Any				       =jnp.transpose(snake_case    ,				(0, 2, 3, 1))
          _UpperCAmelCase  :  Dict				       =self.controlnet_cond_embedding(snake_case)
          sample += controlnet_cond

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

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

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

          _UpperCAmelCase  :  str				       =controlnet_down_block_res_samples

          _UpperCAmelCase  :  Optional[Any]				       =self.controlnet_mid_block(snake_case)

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





'''simple docstring'''






def lowerCamelCase__     (		__lowerCamelCase			:				str = "The quick brown fox jumps over the lazy dog"			,       ):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[Any]				       =set()

   # Replace all the whitespace in our sentence
   _UpperCAmelCase  :  Dict				       =input_str.replace(' '			,       ''				)
   for alpha in input_str:
      if "a" <= alpha.lower() <= "z":
         frequency.add(alpha.lower()				)
   return len(__lowerCamelCase				) == 2_6





def lowerCamelCase__     (		__lowerCamelCase			:				str = "The quick brown fox jumps over the lazy dog"			,       ):







   '''simple docstring'''




   _UpperCAmelCase  :  Tuple				       =[False] * 2_6
   for char in input_str:
      if char.islower():
         _UpperCAmelCase  :  Dict				       =True
      elif char.isupper():
         _UpperCAmelCase  :  List[str]				       =True
   return all(__lowerCamelCase				)





def lowerCamelCase__     (		__lowerCamelCase			:				str = "The quick brown fox jumps over the lazy dog"			,       ):







   '''simple docstring'''




   return len({char for char in input_str.lower() if char.isalpha()}				) == 2_6





def lowerCamelCase__     (		):







   '''simple docstring'''




   from timeit import timeit

   _UpperCAmelCase  :  List[Any]				       ='from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest'
   print(timeit('is_pangram()'			,       setup=__lowerCamelCase				)				)
   print(timeit('is_pangram_faster()'			,       setup=__lowerCamelCase				)				)
   print(timeit('is_pangram_fastest()'			,       setup=__lowerCamelCase				)				)
   # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
   # 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 benchmark()

