
"""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 			_SCREAMING_SNAKE_CASE					(			A__							):
			UpperCAmelCase_  :jnp.ndarray
			UpperCAmelCase_  :jnp.ndarray




class 			_SCREAMING_SNAKE_CASE					(			nn.Module							):
			UpperCAmelCase_  :int
			UpperCAmelCase_  :Tuple[int]				 =						(16, 32, 96, 256)
			UpperCAmelCase_  :jnp.dtype				 =						jnp.floataa






			def  __lowerCAmelCase (					self       )			->     Dict:
						lowerCAmelCase_    :Optional[int]			     =  nn.Conv(
						    self.block_out_channels[0]       ,   kernel_size=(3, 3)       ,   padding=((1, 1), (1, 1))       ,   dtype=self.dtype       ,   )

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

						lowerCAmelCase_    :int			     =  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       ,   __A       )			->     Tuple:
						lowerCAmelCase_    :Dict			     =  self.conv_in(__A       )
						lowerCAmelCase_    :List[str]			     =  nn.silu(__A       )

						for block in self.blocks:
									lowerCAmelCase_    :Any			     =  block(__A       )
									lowerCAmelCase_    :Optional[int]			     =  nn.silu(__A       )

						lowerCAmelCase_    :List[Any]			     =  self.conv_out(__A       )

						return embedding




@flax_register_to_config
class 			_SCREAMING_SNAKE_CASE					(			nn.Module	, A__	, A__							):
			UpperCAmelCase_  :int				 =						32
			UpperCAmelCase_  :int				 =						4
			UpperCAmelCase_  :Tuple[str]				 =						(
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "DownBlock2D",
			)
			UpperCAmelCase_  :Union[bool, Tuple[bool]]				 =						False
			UpperCAmelCase_  :Tuple[int]				 =						(320, 640, 1280, 1280)
			UpperCAmelCase_  :int				 =						2
			UpperCAmelCase_  :Union[int, Tuple[int]]				 =						8
			UpperCAmelCase_  :Optional[Union[int, Tuple[int]]]				 =						None
			UpperCAmelCase_  :int				 =						1280
			UpperCAmelCase_  :float				 =						0.0
			UpperCAmelCase_  :bool				 =						False
			UpperCAmelCase_  :jnp.dtype				 =						jnp.floataa
			UpperCAmelCase_  :bool				 =						True
			UpperCAmelCase_  :int				 =						0
			UpperCAmelCase_  :str				 =						"rgb"
			UpperCAmelCase_  :Tuple[int]				 =						(16, 32, 96, 256)






			def  __lowerCAmelCase (					self       ,   __A       )			->     FrozenDict:
						# init input tensors
						lowerCAmelCase_    :Optional[int]			     =  (1, self.in_channels, self.sample_size, self.sample_size)
						lowerCAmelCase_    :Dict			     =  jnp.zeros(__A       ,   dtype=jnp.floataa       )
						lowerCAmelCase_    :List[Any]			     =  jnp.ones((1,)       ,   dtype=jnp.intaa       )
						lowerCAmelCase_    :Optional[int]			     =  jnp.zeros((1, 1, self.cross_attention_dim)       ,   dtype=jnp.floataa       )
						lowerCAmelCase_    :Any			     =  (1, 3, self.sample_size * 8, self.sample_size * 8)
						lowerCAmelCase_    :Optional[int]			     =  jnp.zeros(__A       ,   dtype=jnp.floataa       )

						lowerCAmelCase_     ,       lowerCAmelCase_    :Optional[int]			     =  jax.random.split(__A       )
						lowerCAmelCase_    :Optional[int]			     =  {"""params""": params_rng, """dropout""": dropout_rng}

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






			def  __lowerCAmelCase (					self       )			->     List[str]:
						lowerCAmelCase_    :Union[str, Any]			     =  self.block_out_channels
						lowerCAmelCase_    :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.
						lowerCAmelCase_    :Dict			     =  self.num_attention_heads or self.attention_head_dim

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

						# time
						lowerCAmelCase_    :Optional[Any]			     =  FlaxTimesteps(
						    block_out_channels[0]       ,   flip_sin_to_cos=self.flip_sin_to_cos       ,   freq_shift=self.config.freq_shift       )
						lowerCAmelCase_    :Optional[Any]			     =  FlaxTimestepEmbedding(__A       ,   dtype=self.dtype       )

						lowerCAmelCase_    :int			     =  FlaxControlNetConditioningEmbedding(
						    conditioning_embedding_channels=block_out_channels[0]       ,   block_out_channels=self.conditioning_embedding_out_channels       ,   )

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

						if isinstance(__A       ,   __A       ):
									lowerCAmelCase_    :Optional[Any]			     =  (num_attention_heads,) * len(self.down_block_types       )

						# down
						lowerCAmelCase_    :Dict			     =  []
						lowerCAmelCase_    :Optional[Any]			     =  []

						lowerCAmelCase_    :Dict			     =  block_out_channels[0]

						lowerCAmelCase_    :List[Any]			     =  nn.Conv(
						    __A       ,   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(__A       )

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

									if down_block_type == "CrossAttnDownBlock2D":
												lowerCAmelCase_    :Tuple			     =  FlaxCrossAttnDownBlockaD(
												    in_channels=__A       ,   out_channels=__A       ,   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:
												lowerCAmelCase_    :Optional[int]			     =  FlaxDownBlockaD(
												    in_channels=__A       ,   out_channels=__A       ,   dropout=self.dropout       ,   num_layers=self.layers_per_block       ,   add_downsample=not is_final_block       ,   dtype=self.dtype       ,   )

									down_blocks.append(__A       )

									for _ in range(self.layers_per_block       ):
												lowerCAmelCase_    :List[str]			     =  nn.Conv(
												    __A       ,   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(__A       )

									if not is_final_block:
												lowerCAmelCase_    :str			     =  nn.Conv(
												    __A       ,   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(__A       )

						lowerCAmelCase_    :List[Any]			     =  down_blocks
						lowerCAmelCase_    :Optional[Any]			     =  controlnet_down_blocks

						# mid
						lowerCAmelCase_    :int			     =  block_out_channels[-1]
						lowerCAmelCase_    :List[Any]			     =  FlaxUNetMidBlockaDCrossAttn(
						    in_channels=__A       ,   dropout=self.dropout       ,   num_attention_heads=num_attention_heads[-1]       ,   use_linear_projection=self.use_linear_projection       ,   dtype=self.dtype       ,   )

						lowerCAmelCase_    :Dict			     =  nn.Conv(
						    __A       ,   kernel_size=(1, 1)       ,   padding="""VALID"""       ,   kernel_init=nn.initializers.zeros_init()       ,   bias_init=nn.initializers.zeros_init()       ,   dtype=self.dtype       ,   )






			def __call__(					self       ,   __A       ,   __A       ,   __A       ,   __A       ,   __A = 1.0       ,   __A = True       ,   __A = False       ,   )			->     Union[FlaxControlNetOutput, Tuple]:
						lowerCAmelCase_    :Union[str, Any]			     =  self.controlnet_conditioning_channel_order
						if channel_order == "bgr":
									lowerCAmelCase_    :Optional[int]			     =  jnp.flip(__A       ,   axis=1       )

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

						lowerCAmelCase_    :List[Any]			     =  self.time_proj(__A       )
						lowerCAmelCase_    :Optional[Any]			     =  self.time_embedding(__A       )

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

						lowerCAmelCase_    :Union[str, Any]			     =  jnp.transpose(__A       ,   (0, 2, 3, 1)       )
						lowerCAmelCase_    :List[str]			     =  self.controlnet_cond_embedding(__A       )
						sample += controlnet_cond

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

						# 4. mid
						lowerCAmelCase_    :int			     =  self.mid_block(__A       ,   __A       ,   __A       ,   deterministic=not train       )

						# 5. contronet blocks
						lowerCAmelCase_    :Dict			     =  ()
						for down_block_res_sample, controlnet_block in zip(__A       ,   self.controlnet_down_blocks       ):
									lowerCAmelCase_    :Union[str, Any]			     =  controlnet_block(__A       )
									controlnet_down_block_res_samples += (down_block_res_sample,)

						lowerCAmelCase_    :Optional[Any]			     =  controlnet_down_block_res_samples

						lowerCAmelCase_    :List[Any]			     =  self.controlnet_mid_block(__A       )

						# 6. scaling
						lowerCAmelCase_    :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=__A       ,   mid_block_res_sample=__A       )











"""simple docstring"""
def  lowerCamelCase_						(UpperCamelCase__		:		int							):
    if not isinstance(UpperCamelCase__     ,       UpperCamelCase__							) or number < 0:
        raise ValueError('''Input must be a non-negative integer'''							)

    _UpperCAmelCase							:  str											=    0
    while number:
        # This way we arrive at next set bit (next 1) instead of looping
        # through each bit and checking for 1s hence the
        # loop won't run 32 times it will only run the number of `1` times
        number &= number - 1
        count += 1
    return count


if __name__ == "__main__":
  import doctest

  doctest.testmod()


