




"""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 _lowerCamelCase    (					a_						):
   _lowerCamelCase    :jnp.ndarray
   _lowerCamelCase    :jnp.ndarray






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






   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						Optional[int]:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	nn.Conv(
             self.block_out_channels[0]   ,     kernel_size=(3, 3)   ,     padding=((1, 1), (1, 1))   ,     dtype=self.dtype   ,     )

         lowerCAmelCase__      :			str   =	[]
         for i in range(len(self.block_out_channels   ) - 1   ):
               lowerCAmelCase__      :			Optional[Any]   =	self.block_out_channels[i]
               lowerCAmelCase__      :			Union[str, Any]   =	self.block_out_channels[i + 1]
               lowerCAmelCase__      :			List[str]   =	nn.Conv(
                   UpperCamelCase   ,     kernel_size=(3, 3)   ,     padding=((1, 1), (1, 1))   ,     dtype=self.dtype   ,     )
               blocks.append(UpperCamelCase   )
               lowerCAmelCase__      :			Optional[Any]   =	nn.Conv(
                   UpperCamelCase   ,     kernel_size=(3, 3)   ,     strides=(2, 2)   ,     padding=((1, 1), (1, 1))   ,     dtype=self.dtype   ,     )
               blocks.append(UpperCamelCase   )
         lowerCAmelCase__      :			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       :		List[Any]   ,     UpperCamelCase       :		Union[str, Any]   )			->						Union[str, Any]:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	self.conv_in(UpperCamelCase   )
         lowerCAmelCase__      :			Optional[Any]   =	nn.silu(UpperCamelCase   )

         for block in self.blocks:
               lowerCAmelCase__      :			Any   =	block(UpperCamelCase   )
               lowerCAmelCase__      :			List[str]   =	nn.silu(UpperCamelCase   )

         lowerCAmelCase__      :			Union[str, Any]   =	self.conv_out(UpperCamelCase   )

         return embedding






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






   def 	_lowerCAmelCase   (		self       :		Union[str, Any]   ,     UpperCamelCase       :		jax.random.KeyArray   )			->						FrozenDict:

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

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

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






   def 	_lowerCAmelCase   (		self       :		int   )			->						Dict:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	self.block_out_channels
         lowerCAmelCase__      :			Union[str, Any]   =	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__      :			List[str]   =	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__      :			str   =	FlaxTimesteps(
             block_out_channels[0]   ,     flip_sin_to_cos=self.flip_sin_to_cos   ,     freq_shift=self.config.freq_shift   )
         lowerCAmelCase__      :			Any   =	FlaxTimestepEmbedding(UpperCamelCase   ,     dtype=self.dtype   )

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

         lowerCAmelCase__      :			Optional[int]   =	self.only_cross_attention
         if isinstance(UpperCamelCase   ,     UpperCamelCase   ):
               lowerCAmelCase__      :			List[str]   =	(only_cross_attention,) * len(self.down_block_types   )

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

         # down
         lowerCAmelCase__      :			Any   =	[]
         lowerCAmelCase__      :			List[Any]   =	[]

         lowerCAmelCase__      :			Tuple   =	block_out_channels[0]

         lowerCAmelCase__      :			Optional[int]   =	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   ):
               lowerCAmelCase__      :			Optional[int]   =	output_channel
               lowerCAmelCase__      :			List[Any]   =	block_out_channels[i]
               lowerCAmelCase__      :			Union[str, Any]   =	i == len(UpperCamelCase   ) - 1

               if down_block_type == "CrossAttnDownBlock2D":
                     lowerCAmelCase__      :			Union[str, Any]   =	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:
                     lowerCAmelCase__      :			Tuple   =	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   ):
                     lowerCAmelCase__      :			List[Any]   =	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:
                     lowerCAmelCase__      :			Union[str, Any]   =	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   )

         lowerCAmelCase__      :			Dict   =	down_blocks
         lowerCAmelCase__      :			Tuple   =	controlnet_down_blocks

         # mid
         lowerCAmelCase__      :			str   =	block_out_channels[-1]
         lowerCAmelCase__      :			List[str]   =	FlaxUNetMidBlockaDCrossAttn(
             in_channels=UpperCamelCase   ,     dropout=self.dropout   ,     num_attention_heads=num_attention_heads[-1]   ,     use_linear_projection=self.use_linear_projection   ,     dtype=self.dtype   ,     )

         lowerCAmelCase__      :			int   =	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       :		Union[str, Any]   ,     UpperCamelCase       :		List[Any]   ,     UpperCamelCase       :		Union[str, Any]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		float = 1.0   ,     UpperCamelCase       :		bool = True   ,     UpperCamelCase       :		bool = False   ,     )			->						Union[FlaxControlNetOutput, Tuple]:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	self.controlnet_conditioning_channel_order
         if channel_order == "bgr":
               lowerCAmelCase__      :			List[str]   =	jnp.flip(UpperCamelCase   ,     axis=1   )

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

         lowerCAmelCase__      :			Dict   =	self.time_proj(UpperCamelCase   )
         lowerCAmelCase__      :			Union[str, Any]   =	self.time_embedding(UpperCamelCase   )

         # 2. pre-process
         lowerCAmelCase__      :			Optional[Any]   =	jnp.transpose(UpperCamelCase   ,     (0, 2, 3, 1)   )
         lowerCAmelCase__      :			str   =	self.conv_in(UpperCamelCase   )

         lowerCAmelCase__      :			Union[str, Any]   =	jnp.transpose(UpperCamelCase   ,     (0, 2, 3, 1)   )
         lowerCAmelCase__      :			Tuple   =	self.controlnet_cond_embedding(UpperCamelCase   )
         sample += controlnet_cond

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

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

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

         lowerCAmelCase__      :			List[Any]   =	controlnet_down_block_res_samples

         lowerCAmelCase__      :			Any   =	self.controlnet_mid_block(UpperCamelCase   )

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











"""simple docstring"""


from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType


class _lowerCamelCase    (					a_						):
   _lowerCamelCase    :Dict     				=     ["image_processor", "tokenizer"]
   _lowerCamelCase    :Dict     				=     "BlipImageProcessor"
   _lowerCamelCase    :Any     				=     ("BertTokenizer", "BertTokenizerFast")






   def __init__(		self       :		Dict   ,     UpperCamelCase       :		Optional[int]   ,     UpperCamelCase       :		Union[str, Any]   )			->						str:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[Any]   =	False
         super().__init__(UpperCamelCase   ,     UpperCamelCase   )
         lowerCAmelCase__      :			List[Any]   =	self.image_processor






   def __call__(		self       :		int   ,     UpperCamelCase       :		ImageInput = None   ,     UpperCamelCase       :		Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None   ,     UpperCamelCase       :		bool = True   ,     UpperCamelCase       :		Union[bool, str, PaddingStrategy] = False   ,     UpperCamelCase       :		Union[bool, str, TruncationStrategy] = None   ,     UpperCamelCase       :		Optional[int] = None   ,     UpperCamelCase       :		int = 0   ,     UpperCamelCase       :		Optional[int] = None   ,     UpperCamelCase       :		Optional[bool] = None   ,     UpperCamelCase       :		bool = False   ,     UpperCamelCase       :		bool = False   ,     UpperCamelCase       :		bool = False   ,     UpperCamelCase       :		bool = False   ,     UpperCamelCase       :		bool = False   ,     UpperCamelCase       :		bool = True   ,     UpperCamelCase       :		Optional[Union[str, TensorType]] = None   ,     **UpperCamelCase       :		Optional[int]   ,     )			->						BatchEncoding:

         """simple docstring"""
         if images is None and text is None:
               raise ValueError("""You have to specify either images or text."""   )

         # Get only text
         if images is None:
               lowerCAmelCase__      :			Any   =	self.tokenizer
               lowerCAmelCase__      :			Optional[int]   =	self.tokenizer(
                   text=UpperCamelCase   ,     add_special_tokens=UpperCamelCase   ,     padding=UpperCamelCase   ,     truncation=UpperCamelCase   ,     max_length=UpperCamelCase   ,     stride=UpperCamelCase   ,     pad_to_multiple_of=UpperCamelCase   ,     return_attention_mask=UpperCamelCase   ,     return_overflowing_tokens=UpperCamelCase   ,     return_special_tokens_mask=UpperCamelCase   ,     return_offsets_mapping=UpperCamelCase   ,     return_token_type_ids=UpperCamelCase   ,     return_length=UpperCamelCase   ,     verbose=UpperCamelCase   ,     return_tensors=UpperCamelCase   ,     **UpperCamelCase   ,     )
               return text_encoding

         # add pixel_values
         lowerCAmelCase__      :			Tuple   =	self.image_processor(UpperCamelCase   ,     return_tensors=UpperCamelCase   )

         if text is not None:
               lowerCAmelCase__      :			Optional[int]   =	self.tokenizer(
                   text=UpperCamelCase   ,     add_special_tokens=UpperCamelCase   ,     padding=UpperCamelCase   ,     truncation=UpperCamelCase   ,     max_length=UpperCamelCase   ,     stride=UpperCamelCase   ,     pad_to_multiple_of=UpperCamelCase   ,     return_attention_mask=UpperCamelCase   ,     return_overflowing_tokens=UpperCamelCase   ,     return_special_tokens_mask=UpperCamelCase   ,     return_offsets_mapping=UpperCamelCase   ,     return_token_type_ids=UpperCamelCase   ,     return_length=UpperCamelCase   ,     verbose=UpperCamelCase   ,     return_tensors=UpperCamelCase   ,     **UpperCamelCase   ,     )
         else:
               lowerCAmelCase__      :			Tuple   =	None

         if text_encoding is not None:
               encoding_image_processor.update(UpperCamelCase   )

         return encoding_image_processor






   def 	_lowerCAmelCase   (		self       :		int   ,     *UpperCamelCase       :		Union[str, Any]   ,     **UpperCamelCase       :		List[str]   )			->						Dict:

         """simple docstring"""
         return self.tokenizer.batch_decode(*UpperCamelCase   ,     **UpperCamelCase   )






   def 	_lowerCAmelCase   (		self       :		List[Any]   ,     *UpperCamelCase       :		Tuple   ,     **UpperCamelCase       :		List[str]   )			->						List[Any]:

         """simple docstring"""
         return self.tokenizer.decode(*UpperCamelCase   ,     **UpperCamelCase   )






   @property
   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						Tuple:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	self.tokenizer.model_input_names
         lowerCAmelCase__      :			Union[str, Any]   =	self.image_processor.model_input_names
         return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names   )   )





