





'''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 					__snake_case  (					__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





   lowerCamelCase__    							=      42
   lowerCamelCase__    							=      42



class 					__snake_case  (					nn.Module							):






   '''simple docstring'''





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

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Optional[Any]    =  nn.Conv(
        self.block_out_channels[0]	,		kernel_size=(3, 3)	,		padding=((1, 1), (1, 1))	,		dtype=self.dtype	,		)

    snake_case__     :					Tuple    =  []
    for i in range(len(self.block_out_channels       ) - 1       ):
     snake_case__     :					Optional[int]    =  self.block_out_channels[i]
     snake_case__     :					Optional[int]    =  self.block_out_channels[i + 1]
     snake_case__     :					Tuple    =  nn.Conv(
         __SCREAMING_SNAKE_CASE	,		kernel_size=(3, 3)	,		padding=((1, 1), (1, 1))	,		dtype=self.dtype	,		)
     blocks.append(__SCREAMING_SNAKE_CASE       )
     snake_case__     :					str    =  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       )
    snake_case__     :					Optional[int]    =  blocks

    snake_case__     :					Optional[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	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					List[str]    =  self.conv_in(__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Union[str, Any]    =  nn.silu(__SCREAMING_SNAKE_CASE       )

    for block in self.blocks:
     snake_case__     :					List[str]    =  block(__SCREAMING_SNAKE_CASE       )
     snake_case__     :					List[str]    =  nn.silu(__SCREAMING_SNAKE_CASE       )

    snake_case__     :					Any    =  self.conv_out(__SCREAMING_SNAKE_CASE       )

    return embedding



@flax_register_to_config
class 					__snake_case  (					nn.Module		,				__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





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

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE       ):
    # init input tensors
    snake_case__     :					Optional[int]    =  (1, self.in_channels, self.sample_size, self.sample_size)
    snake_case__     :					Union[str, Any]    =  jnp.zeros(__SCREAMING_SNAKE_CASE	,		dtype=jnp.floataa       )
    snake_case__     :					str    =  jnp.ones((1,)	,		dtype=jnp.intaa       )
    snake_case__     :					Any    =  jnp.zeros((1, 1, self.cross_attention_dim)	,		dtype=jnp.floataa       )
    snake_case__     :					str    =  (1, 3, self.sample_size * 8, self.sample_size * 8)
    snake_case__     :					Any    =  jnp.zeros(__SCREAMING_SNAKE_CASE	,		dtype=jnp.floataa       )

    snake_case__  ,							snake_case__     :					Tuple    =  jax.random.split(__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Dict    =  {"""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 		__UpperCamelCase						(							self       ):
    snake_case__     :					Dict    =  self.block_out_channels
    snake_case__     :					List[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.
    snake_case__     :					str    =  self.num_attention_heads or self.attention_head_dim

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

    # time
    snake_case__     :					Tuple    =  FlaxTimesteps(
        block_out_channels[0]	,		flip_sin_to_cos=self.flip_sin_to_cos	,		freq_shift=self.config.freq_shift       )
    snake_case__     :					Optional[int]    =  FlaxTimestepEmbedding(__SCREAMING_SNAKE_CASE	,		dtype=self.dtype       )

    snake_case__     :					str    =  FlaxControlNetConditioningEmbedding(
        conditioning_embedding_channels=block_out_channels[0]	,		block_out_channels=self.conditioning_embedding_out_channels	,		)

    snake_case__     :					Tuple    =  self.only_cross_attention
    if isinstance(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
     snake_case__     :					Dict    =  (only_cross_attention,) * len(self.down_block_types       )

    if isinstance(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
     snake_case__     :					Dict    =  (num_attention_heads,) * len(self.down_block_types       )

    # down
    snake_case__     :					Dict    =  []
    snake_case__     :					Tuple    =  []

    snake_case__     :					Optional[int]    =  block_out_channels[0]

    snake_case__     :					Tuple    =  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       ):
     snake_case__     :					int    =  output_channel
     snake_case__     :					Optional[int]    =  block_out_channels[i]
     snake_case__     :					Optional[int]    =  i == len(__SCREAMING_SNAKE_CASE       ) - 1

     if down_block_type == "CrossAttnDownBlock2D":
      snake_case__     :					Tuple    =  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:
      snake_case__     :					Dict    =  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       ):
      snake_case__     :					Dict    =  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:
      snake_case__     :					int    =  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       )

    snake_case__     :					List[Any]    =  down_blocks
    snake_case__     :					Union[str, Any]    =  controlnet_down_blocks

    # mid
    snake_case__     :					int    =  block_out_channels[-1]
    snake_case__     :					Union[str, Any]    =  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	,		)

    snake_case__     :					int    =  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	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE = 1.0	,		__SCREAMING_SNAKE_CASE = True	,		__SCREAMING_SNAKE_CASE = False	,		):
    snake_case__     :					Tuple    =  self.controlnet_conditioning_channel_order
    if channel_order == "bgr":
     snake_case__     :					List[Any]    =  jnp.flip(__SCREAMING_SNAKE_CASE	,		axis=1       )

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

    snake_case__     :					Optional[int]    =  self.time_proj(__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Any    =  self.time_embedding(__SCREAMING_SNAKE_CASE       )

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

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

    # 3. down
    snake_case__     :					List[str]    =  (sample,)
    for down_block in self.down_blocks:
     if isinstance(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
      snake_case__  ,							snake_case__     :					Optional[Any]    =  down_block(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		deterministic=not train       )
     else:
      snake_case__  ,							snake_case__     :					Optional[int]    =  down_block(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		deterministic=not train       )
     down_block_res_samples += res_samples

    # 4. mid
    snake_case__     :					Union[str, Any]    =  self.mid_block(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		deterministic=not train       )

    # 5. contronet blocks
    snake_case__     :					Tuple    =  ()
    for down_block_res_sample, controlnet_block in zip(__SCREAMING_SNAKE_CASE	,		self.controlnet_down_blocks       ):
     snake_case__     :					Any    =  controlnet_block(__SCREAMING_SNAKE_CASE       )
     controlnet_down_block_res_samples += (down_block_res_sample,)

    snake_case__     :					List[str]    =  controlnet_down_block_res_samples

    snake_case__     :					int    =  self.controlnet_mid_block(__SCREAMING_SNAKE_CASE       )

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










import warnings

from ...utils import logging
from .image_processing_clip import CLIPImageProcessor


__a							     =					logging.get_logger(__name__)

class     __a(	_a  ):






    """simple docstring"""





    def __init__( self  ,*_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )					->						None:
         warnings.warn(
             '''The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'''
             ''' use CLIPImageProcessor instead.'''  ,_SCREAMING_SNAKE_CASE  ,)
         super().__init__(*_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )