




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 					__lowerCamelCase			(       UpperCamelCase__       ):




    """simple docstring"""

    snake_case__   = 42
    snake_case__   = 42






class 					__lowerCamelCase			(       nn.Module       ):




    """simple docstring"""

    snake_case__   = 42
    snake_case__   = (1_6, 3_2, 9_6, 2_5_6)
    snake_case__   = jnp.floataa


    def   a (			self	:	Any  ) ->			Dict:
         lowerCAmelCase__            =					nn.Conv(
             self.block_out_channels[0]  ,      kernel_size=(3, 3)  ,      padding=((1, 1), (1, 1))  ,      dtype=self.dtype  ,      )

         lowerCAmelCase__            =					[]
         for i in range(len(self.block_out_channels  ) - 1  ):
              lowerCAmelCase__            =					self.block_out_channels[i]
              lowerCAmelCase__            =					self.block_out_channels[i + 1]
              lowerCAmelCase__            =					nn.Conv(
                  SCREAMING_SNAKE_CASE__  ,      kernel_size=(3, 3)  ,      padding=((1, 1), (1, 1))  ,      dtype=self.dtype  ,      )
              blocks.append(SCREAMING_SNAKE_CASE__  )
              lowerCAmelCase__            =					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__  )
         lowerCAmelCase__            =					blocks

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

         for block in self.blocks:
              lowerCAmelCase__            =					block(SCREAMING_SNAKE_CASE__  )
              lowerCAmelCase__            =					nn.silu(SCREAMING_SNAKE_CASE__  )

         lowerCAmelCase__            =					self.conv_out(SCREAMING_SNAKE_CASE__  )

         return embedding






@flax_register_to_config
class 					__lowerCamelCase			(       nn.Module      ,							UpperCamelCase__      ,							UpperCamelCase__       ):




    """simple docstring"""

    snake_case__   = 3_2
    snake_case__   = 4
    snake_case__   = (
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "DownBlock2D",
    )
    snake_case__   = False
    snake_case__   = (3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
    snake_case__   = 2
    snake_case__   = 8
    snake_case__   = None
    snake_case__   = 1_2_8_0
    snake_case__   = 0.0
    snake_case__   = False
    snake_case__   = jnp.floataa
    snake_case__   = True
    snake_case__   = 0
    snake_case__   = "rgb"
    snake_case__   = (1_6, 3_2, 9_6, 2_5_6)


    def   a (			self	:	Optional[int]  ,      SCREAMING_SNAKE_CASE__	:	jax.random.KeyArray  ) ->			FrozenDict:
         # init input tensors
         lowerCAmelCase__            =					(1, self.in_channels, self.sample_size, self.sample_size)
         lowerCAmelCase__            =					jnp.zeros(SCREAMING_SNAKE_CASE__  ,      dtype=jnp.floataa  )
         lowerCAmelCase__            =					jnp.ones((1,)  ,      dtype=jnp.intaa  )
         lowerCAmelCase__            =					jnp.zeros((1, 1, self.cross_attention_dim)  ,      dtype=jnp.floataa  )
         lowerCAmelCase__            =					(1, 3, self.sample_size * 8, self.sample_size * 8)
         lowerCAmelCase__            =					jnp.zeros(SCREAMING_SNAKE_CASE__  ,      dtype=jnp.floataa  )

         lowerCAmelCase__      ,						lowerCAmelCase__            =					jax.random.split(SCREAMING_SNAKE_CASE__  )
         lowerCAmelCase__            =					{"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   a (			self	:	List[str]  ) ->			Optional[Any]:
         lowerCAmelCase__            =					self.block_out_channels
         lowerCAmelCase__            =					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__            =					self.num_attention_heads or self.attention_head_dim

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

         # time
         lowerCAmelCase__            =					FlaxTimesteps(
             block_out_channels[0]  ,      flip_sin_to_cos=self.flip_sin_to_cos  ,      freq_shift=self.config.freq_shift  )
         lowerCAmelCase__            =					FlaxTimestepEmbedding(SCREAMING_SNAKE_CASE__  ,      dtype=self.dtype  )

         lowerCAmelCase__            =					FlaxControlNetConditioningEmbedding(
             conditioning_embedding_channels=block_out_channels[0]  ,      block_out_channels=self.conditioning_embedding_out_channels  ,      )

         lowerCAmelCase__            =					self.only_cross_attention
         if isinstance(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ):
              lowerCAmelCase__            =					(only_cross_attention,) * len(self.down_block_types  )

         if isinstance(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ):
              lowerCAmelCase__            =					(num_attention_heads,) * len(self.down_block_types  )

         # down
         lowerCAmelCase__            =					[]
         lowerCAmelCase__            =					[]

         lowerCAmelCase__            =					block_out_channels[0]

         lowerCAmelCase__            =					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  ):
              lowerCAmelCase__            =					output_channel
              lowerCAmelCase__            =					block_out_channels[i]
              lowerCAmelCase__            =					i == len(SCREAMING_SNAKE_CASE__  ) - 1

              if down_block_type == "CrossAttnDownBlock2D":
                   lowerCAmelCase__            =					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:
                   lowerCAmelCase__            =					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  ):
                   lowerCAmelCase__            =					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:
                   lowerCAmelCase__            =					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__  )

         lowerCAmelCase__            =					down_blocks
         lowerCAmelCase__            =					controlnet_down_blocks

         # mid
         lowerCAmelCase__            =					block_out_channels[-1]
         lowerCAmelCase__            =					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  ,      )

         lowerCAmelCase__            =					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	:	Dict  ,      SCREAMING_SNAKE_CASE__	:	Tuple  ,      SCREAMING_SNAKE_CASE__	:	Optional[Any]  ,      SCREAMING_SNAKE_CASE__	:	Tuple  ,      SCREAMING_SNAKE_CASE__	:	int  ,      SCREAMING_SNAKE_CASE__	:	float = 1.0  ,      SCREAMING_SNAKE_CASE__	:	bool = True  ,      SCREAMING_SNAKE_CASE__	:	bool = False  ,      ) ->			Union[FlaxControlNetOutput, Tuple]:
         lowerCAmelCase__            =					self.controlnet_conditioning_channel_order
         if channel_order == "bgr":
              lowerCAmelCase__            =					jnp.flip(SCREAMING_SNAKE_CASE__  ,      axis=1  )

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

         lowerCAmelCase__            =					self.time_proj(SCREAMING_SNAKE_CASE__  )
         lowerCAmelCase__            =					self.time_embedding(SCREAMING_SNAKE_CASE__  )

         # 2. pre-process
         lowerCAmelCase__            =					jnp.transpose(SCREAMING_SNAKE_CASE__  ,      (0, 2, 3, 1)  )
         lowerCAmelCase__            =					self.conv_in(SCREAMING_SNAKE_CASE__  )

         lowerCAmelCase__            =					jnp.transpose(SCREAMING_SNAKE_CASE__  ,      (0, 2, 3, 1)  )
         lowerCAmelCase__            =					self.controlnet_cond_embedding(SCREAMING_SNAKE_CASE__  )
         sample += controlnet_cond

         # 3. down
         lowerCAmelCase__            =					(sample,)
         for down_block in self.down_blocks:
              if isinstance(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ):
                   lowerCAmelCase__      ,						lowerCAmelCase__            =					down_block(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      deterministic=not train  )
              else:
                   lowerCAmelCase__      ,						lowerCAmelCase__            =					down_block(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      deterministic=not train  )
              down_block_res_samples += res_samples

         # 4. mid
         lowerCAmelCase__            =					self.mid_block(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      deterministic=not train  )

         # 5. contronet blocks
         lowerCAmelCase__            =					()
         for down_block_res_sample, controlnet_block in zip(SCREAMING_SNAKE_CASE__  ,      self.controlnet_down_blocks  ):
              lowerCAmelCase__            =					controlnet_block(SCREAMING_SNAKE_CASE__  )
              controlnet_down_block_res_samples += (down_block_res_sample,)

         lowerCAmelCase__            =					controlnet_down_block_res_samples

         lowerCAmelCase__            =					self.controlnet_mid_block(SCREAMING_SNAKE_CASE__  )

         # 6. scaling
         lowerCAmelCase__            =					[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__  )


__a         =   'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'




def 							a		(				snake_case__:   bytes   ):


				'''simple docstring'''
				# Make sure the supplied data is a bytes-like object
				if not isinstance(snake_case__		,  snake_case__   ):
								lowercase_	    =   F'''a bytes-like object is required, not \'{data.__class__.__name__}\''''
								raise TypeError(snake_case__   )

				lowercase_	    =   ''''''.join(bin(snake_case__   )[2:].zfill(8   ) for byte in data   )

				lowercase_	    =   len(snake_case__   ) % 6 != 0

				if padding_needed:
								# The padding that will be added later
								lowercase_	    =   B'''=''' * ((6 - len(snake_case__   ) % 6) // 2)

								# Append binary_stream with arbitrary binary digits (0's by default) to make its
								# length a multiple of 6.
								binary_stream += "0" * (6 - len(snake_case__   ) % 6)
				else:
								lowercase_	    =   B''''''

				# Encode every 6 binary digits to their corresponding Base64 character
				return (
				    "".join(
				        B64_CHARSET[int(binary_stream[index : index + 6]		,  2   )]
				        for index in range(0		,  len(snake_case__   )		,  6   )   ).encode()
				    + padding
				)




def 							a		(				snake_case__:   str   ):


				'''simple docstring'''
				# Make sure encoded_data is either a string or a bytes-like object
				if not isinstance(snake_case__		,  snake_case__   ) and not isinstance(snake_case__		,  snake_case__   ):
								lowercase_	    =   (
								    '''argument should be a bytes-like object or ASCII string, '''
								    F'''not \'{encoded_data.__class__.__name__}\''''
								)
								raise TypeError(snake_case__   )

				# In case encoded_data is a bytes-like object, make sure it contains only
				# ASCII characters so we convert it to a string object
				if isinstance(snake_case__		,  snake_case__   ):
								try:
												lowercase_	    =   encoded_data.decode('''utf-8'''   )
								except UnicodeDecodeError:
												raise ValueError('''base64 encoded data should only contain ASCII characters'''   )

				lowercase_	    =   encoded_data.count('''='''   )

				# Check if the encoded string contains non base64 characters
				if padding:
								assert all(
								    char in B64_CHARSET for char in encoded_data[:-padding]   ), "Invalid base64 character(s) found."
				else:
								assert all(
								    char in B64_CHARSET for char in encoded_data   ), "Invalid base64 character(s) found."

				# Check the padding
				assert len(snake_case__   ) % 4 == 0 and padding < 3, "Incorrect padding"

				if padding:
								# Remove padding if there is one
								lowercase_	    =   encoded_data[:-padding]

								lowercase_	    =   ''''''.join(
								    bin(B64_CHARSET.index(snake_case__   )   )[2:].zfill(6   ) for char in encoded_data   )[: -padding * 2]
				else:
								lowercase_	    =   ''''''.join(
								    bin(B64_CHARSET.index(snake_case__   )   )[2:].zfill(6   ) for char in encoded_data   )

				lowercase_	    =   [
				    int(binary_stream[index : index + 8]		,  2   )
				    for index in range(0		,  len(snake_case__   )		,  8   )
				]

				return bytes(snake_case__   )


if __name__ == "__main__":
						import doctest

						doctest.testmod()




