




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 							UpperCamelCase_       (					UpperCAmelCase__	):

    '''simple docstring'''






    UpperCAmelCase__				    = 42
    UpperCAmelCase__				    = 42


class 							UpperCamelCase_       (					nn.Module	):

    '''simple docstring'''






    UpperCAmelCase__				    = 42
    UpperCAmelCase__				    = (16, 32, 96, 256)
    UpperCAmelCase__				    = jnp.floataa


    def 			SCREAMING_SNAKE_CASE				(     self :      List[str])		->Tuple:





           '''simple docstring'''

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

           A__ 					=       []
           for i in range(len(self.block_out_channels) - 1):
                  A__ 					=       self.block_out_channels[i]
                  A__ 					=       self.block_out_channels[i + 1]
                  A__ 					=       nn.Conv(
                      UpperCAmelCase__   ,					kernel_size=(3, 3)   ,					padding=((1, 1), (1, 1))   ,					dtype=self.dtype   ,					)
                  blocks.append(UpperCAmelCase__)
                  A__ 					=       nn.Conv(
                      UpperCAmelCase__   ,					kernel_size=(3, 3)   ,					strides=(2, 2)   ,					padding=((1, 1), (1, 1))   ,					dtype=self.dtype   ,					)
                  blocks.append(UpperCAmelCase__)
           A__ 					=       blocks

           A__ 					=       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 :      int   ,					UpperCAmelCase__ :      str)		->Union[str, Any]:





           '''simple docstring'''

           A__ 					=       self.conv_in(UpperCAmelCase__)
           A__ 					=       nn.silu(UpperCAmelCase__)

           for block in self.blocks:
                  A__ 					=       block(UpperCAmelCase__)
                  A__ 					=       nn.silu(UpperCAmelCase__)

           A__ 					=       self.conv_out(UpperCAmelCase__)

           return embedding


@flax_register_to_config
class 							UpperCamelCase_       (					nn.Module						,   UpperCAmelCase__						,   UpperCAmelCase__	):

    '''simple docstring'''






    UpperCAmelCase__				    = 32
    UpperCAmelCase__				    = 4
    UpperCAmelCase__				    = (
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "DownBlock2D",
    )
    UpperCAmelCase__				    = False
    UpperCAmelCase__				    = (320, 640, 1280, 1280)
    UpperCAmelCase__				    = 2
    UpperCAmelCase__				    = 8
    UpperCAmelCase__				    = None
    UpperCAmelCase__				    = 1280
    UpperCAmelCase__				    = 0.0
    UpperCAmelCase__				    = False
    UpperCAmelCase__				    = jnp.floataa
    UpperCAmelCase__				    = True
    UpperCAmelCase__				    = 0
    UpperCAmelCase__				    = "rgb"
    UpperCAmelCase__				    = (16, 32, 96, 256)


    def 			SCREAMING_SNAKE_CASE				(     self :      Optional[Any]   ,					UpperCAmelCase__ :      jax.random.KeyArray)		->FrozenDict:





           '''simple docstring'''

           A__ 					=       (1, self.in_channels, self.sample_size, self.sample_size)
           A__ 					=       jnp.zeros(UpperCAmelCase__   ,					dtype=jnp.floataa)
           A__ 					=       jnp.ones((1,)   ,					dtype=jnp.intaa)
           A__ 					=       jnp.zeros((1, 1, self.cross_attention_dim)   ,					dtype=jnp.floataa)
           A__ 					=       (1, 3, self.sample_size * 8, self.sample_size * 8)
           A__ 					=       jnp.zeros(UpperCAmelCase__   ,					dtype=jnp.floataa)

           A__					,				A__ 					=       jax.random.split(UpperCAmelCase__)
           A__ 					=       {'''params''': params_rng, '''dropout''': dropout_rng}

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


    def 			SCREAMING_SNAKE_CASE				(     self :      Optional[int])		->str:





           '''simple docstring'''

           A__ 					=       self.block_out_channels
           A__ 					=       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__ 					=       self.num_attention_heads or self.attention_head_dim

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

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

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

           A__ 					=       self.only_cross_attention
           if isinstance(UpperCAmelCase__   ,					UpperCAmelCase__):
                  A__ 					=       (only_cross_attention,) * len(self.down_block_types)

           if isinstance(UpperCAmelCase__   ,					UpperCAmelCase__):
                  A__ 					=       (num_attention_heads,) * len(self.down_block_types)

           # down
           A__ 					=       []
           A__ 					=       []

           A__ 					=       block_out_channels[0]

           A__ 					=       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__ 					=       output_channel
                  A__ 					=       block_out_channels[i]
                  A__ 					=       i == len(UpperCAmelCase__) - 1

                  if down_block_type == "CrossAttnDownBlock2D":
                         A__ 					=       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__ 					=       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__ 					=       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__ 					=       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__ 					=       down_blocks
           A__ 					=       controlnet_down_blocks

           # mid
           A__ 					=       block_out_channels[-1]
           A__ 					=       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__ 					=       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 :      List[str]   ,					UpperCAmelCase__ :      Tuple   ,					UpperCAmelCase__ :      Optional[int]   ,					UpperCAmelCase__ :      Any   ,					UpperCAmelCase__ :      int   ,					UpperCAmelCase__ :      float = 1.0   ,					UpperCAmelCase__ :      bool = True   ,					UpperCAmelCase__ :      bool = False   ,					)		->Union[FlaxControlNetOutput, Tuple]:





           '''simple docstring'''

           A__ 					=       self.controlnet_conditioning_channel_order
           if channel_order == "bgr":
                  A__ 					=       jnp.flip(UpperCAmelCase__   ,					axis=1)

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

           A__ 					=       self.time_proj(UpperCAmelCase__)
           A__ 					=       self.time_embedding(UpperCAmelCase__)

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

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

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

           # 4. mid
           A__ 					=       self.mid_block(UpperCAmelCase__   ,					UpperCAmelCase__   ,					UpperCAmelCase__   ,					deterministic=not train)

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

           A__ 					=       controlnet_down_block_res_samples

           A__ 					=       self.controlnet_mid_block(UpperCAmelCase__)

           # 6. scaling
           A__ 					=       [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'''
import enum
import os
from hashlib import shaaaa
from typing import Optional

from .. import config
from .logging import get_logger


lowercase__						       =      get_logger(__name__)






class 	snake_case__       (      enum.Enum					):







		"""simple docstring"""

		lowerCamelCase     							=						"""all_checks"""
		lowerCamelCase     							=						"""basic_checks"""
		lowerCamelCase     							=						"""no_checks"""







class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""








class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""








class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""








class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""








def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__=None			)  ->							str:


		'''simple docstring'''


		if expected_checksums is None:
				logger.info('''Unable to verify checksums.'''			)
				return
		if len(set(SCREAMING_SNAKE_CASE__			) - set(SCREAMING_SNAKE_CASE__			)			) > 0:
				raise ExpectedMoreDownloadedFiles(str(set(SCREAMING_SNAKE_CASE__			) - set(SCREAMING_SNAKE_CASE__			)			)			)
		if len(set(SCREAMING_SNAKE_CASE__			) - set(SCREAMING_SNAKE_CASE__			)			) > 0:
				raise UnexpectedDownloadedFile(str(set(SCREAMING_SNAKE_CASE__			) - set(SCREAMING_SNAKE_CASE__			)			)			)
		snake_case  :       Any     			=						[url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]]
		snake_case  :       Union[str, Any]     			=						''' for ''' + verification_name if verification_name is not None else ''''''
		if len(SCREAMING_SNAKE_CASE__			) > 0:
				raise NonMatchingChecksumError(
				    F'Checksums didn\'t match{for_verification_name}:\n'
				    F'{bad_urls}\n'
				    '''Set `verification_mode=\'no_checks\'` to skip checksums verification and ignore this error'''			)
		logger.info('''All the checksums matched successfully''' + for_verification_name			)






class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""








class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""








class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""








class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""








def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__			)  ->							List[str]:


		'''simple docstring'''


		if expected_splits is None:
				logger.info('''Unable to verify splits sizes.'''			)
				return
		if len(set(SCREAMING_SNAKE_CASE__			) - set(SCREAMING_SNAKE_CASE__			)			) > 0:
				raise ExpectedMoreSplits(str(set(SCREAMING_SNAKE_CASE__			) - set(SCREAMING_SNAKE_CASE__			)			)			)
		if len(set(SCREAMING_SNAKE_CASE__			) - set(SCREAMING_SNAKE_CASE__			)			) > 0:
				raise UnexpectedSplits(str(set(SCREAMING_SNAKE_CASE__			) - set(SCREAMING_SNAKE_CASE__			)			)			)
		snake_case  :       Dict     			=						[
		    {'''expected''': expected_splits[name], '''recorded''': recorded_splits[name]}
		    for name in expected_splits
		    if expected_splits[name].num_examples != recorded_splits[name].num_examples
		]
		if len(SCREAMING_SNAKE_CASE__			) > 0:
				raise NonMatchingSplitsSizesError(str(SCREAMING_SNAKE_CASE__			)			)
		logger.info('''All the splits matched successfully.'''			)




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__ = True			)  ->							dict:


		'''simple docstring'''


		if record_checksum:
				snake_case  :       Any     			=						shaaaa()
				with open(SCREAMING_SNAKE_CASE__ ,       '''rb'''			) as f:
						for chunk in iter(lambda: f.read(1 << 20			) ,       B''''''			):
								m.update(SCREAMING_SNAKE_CASE__			)
						snake_case  :       Any     			=						m.hexdigest()
		else:
				snake_case  :       Tuple     			=						None
		return {"num_bytes": os.path.getsize(SCREAMING_SNAKE_CASE__			), "checksum": checksum}




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							Dict:


		'''simple docstring'''


		if dataset_size and config.IN_MEMORY_MAX_SIZE:
				return dataset_size < config.IN_MEMORY_MAX_SIZE
		else:
				return False


