"""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			(__lowerCamelCase     ):
				_snake_case     =  42
				_snake_case     =  42



class  _lowerCamelCase			(nn.Module     ):
				_snake_case     =  42
				_snake_case     =  (16, 32, 96, 256)
				_snake_case     =  jnp.floataa







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






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

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

					_lowercase :    List[str]							=							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			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					_lowercase :    int							=							self.conv_in(lowerCamelCase_						)
					_lowercase :    Optional[Any]							=							nn.silu(lowerCamelCase_						)

					for block in self.blocks:
						_lowercase :    Optional[int]							=							block(lowerCamelCase_						)
						_lowercase :    List[str]							=							nn.silu(lowerCamelCase_						)

					_lowercase :    List[str]							=							self.conv_out(lowerCamelCase_						)

					return embedding



@flax_register_to_config
class  _lowerCamelCase			(nn.Module						,							__lowerCamelCase						,							__lowerCamelCase     ):
				_snake_case     =  32
				_snake_case     =  4
				_snake_case     =  (
				    "CrossAttnDownBlock2D",
				    "CrossAttnDownBlock2D",
				    "CrossAttnDownBlock2D",
				    "DownBlock2D",
				)
				_snake_case     =  False
				_snake_case     =  (320, 640, 1_280, 1_280)
				_snake_case     =  2
				_snake_case     =  8
				_snake_case     =  None
				_snake_case     =  1_280
				_snake_case     =  0.0
				_snake_case     =  False
				_snake_case     =  jnp.floataa
				_snake_case     =  True
				_snake_case     =  0
				_snake_case     =  "rgb"
				_snake_case     =  (16, 32, 96, 256)







				def 							__UpperCAmelCase					( self    :       Any			,					lowerCamelCase_    :       jax.random.KeyArray						):

					"""simple docstring"""






					_lowercase :    Any							=							(1, self.in_channels, self.sample_size, self.sample_size)
					_lowercase :    int							=							jnp.zeros(lowerCamelCase_			,					dtype=jnp.floataa						)
					_lowercase :    Optional[Any]							=							jnp.ones((1,)			,					dtype=jnp.intaa						)
					_lowercase :    Tuple							=							jnp.zeros((1, 1, self.cross_attention_dim)			,					dtype=jnp.floataa						)
					_lowercase :    Tuple							=							(1, 3, self.sample_size * 8, self.sample_size * 8)
					_lowercase :    Optional[Any]							=							jnp.zeros(lowerCamelCase_			,					dtype=jnp.floataa						)

					_lowercase				,      _lowercase :    Any							=							jax.random.split(lowerCamelCase_						)
					_lowercase :    str							=							{'params': params_rng, 'dropout': dropout_rng}

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







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					_lowercase :    Dict							=							self.block_out_channels
					_lowercase :    Optional[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.
					_lowercase :    Optional[int]							=							self.num_attention_heads or self.attention_head_dim

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

					# time
					_lowercase :    Tuple							=							FlaxTimesteps(
					    block_out_channels[0]			,					flip_sin_to_cos=self.flip_sin_to_cos			,					freq_shift=self.config.freq_shift						)
					_lowercase :    Any							=							FlaxTimestepEmbedding(lowerCamelCase_			,					dtype=self.dtype						)

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

					_lowercase :    Union[str, Any]							=							self.only_cross_attention
					if isinstance(lowerCamelCase_			,					lowerCamelCase_						):
						_lowercase :    Optional[Any]							=							(only_cross_attention,) * len(self.down_block_types						)

					if isinstance(lowerCamelCase_			,					lowerCamelCase_						):
						_lowercase :    int							=							(num_attention_heads,) * len(self.down_block_types						)

					# down
					_lowercase :    Optional[int]							=							[]
					_lowercase :    str							=							[]

					_lowercase :    int							=							block_out_channels[0]

					_lowercase :    Any							=							nn.Conv(
					    lowerCamelCase_			,					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(lowerCamelCase_						)

					for i, down_block_type in enumerate(self.down_block_types						):
						_lowercase :    Optional[Any]							=							output_channel
						_lowercase :    Union[str, Any]							=							block_out_channels[i]
						_lowercase :    Tuple							=							i == len(lowerCamelCase_						) - 1

						if down_block_type == "CrossAttnDownBlock2D":
							_lowercase :    Dict							=							FlaxCrossAttnDownBlockaD(
							    in_channels=lowerCamelCase_			,					out_channels=lowerCamelCase_			,					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:
							_lowercase :    Any							=							FlaxDownBlockaD(
							    in_channels=lowerCamelCase_			,					out_channels=lowerCamelCase_			,					dropout=self.dropout			,					num_layers=self.layers_per_block			,					add_downsample=not is_final_block			,					dtype=self.dtype			,					)

						down_blocks.append(lowerCamelCase_						)

						for _ in range(self.layers_per_block						):
							_lowercase :    Any							=							nn.Conv(
							    lowerCamelCase_			,					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(lowerCamelCase_						)

						if not is_final_block:
							_lowercase :    Tuple							=							nn.Conv(
							    lowerCamelCase_			,					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(lowerCamelCase_						)

					_lowercase :    Optional[Any]							=							down_blocks
					_lowercase :    List[str]							=							controlnet_down_blocks

					# mid
					_lowercase :    int							=							block_out_channels[-1]
					_lowercase :    Union[str, Any]							=							FlaxUNetMidBlockaDCrossAttn(
					    in_channels=lowerCamelCase_			,					dropout=self.dropout			,					num_attention_heads=num_attention_heads[-1]			,					use_linear_projection=self.use_linear_projection			,					dtype=self.dtype			,					)

					_lowercase :    Tuple							=							nn.Conv(
					    lowerCamelCase_			,					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]			,					lowerCamelCase_    :       List[Any]			,					lowerCamelCase_    :       List[Any]			,					lowerCamelCase_    :       List[str]			,					lowerCamelCase_    :       Any			,					lowerCamelCase_    :       float = 1.0			,					lowerCamelCase_    :       bool = True			,					lowerCamelCase_    :       bool = False			,					):

					"""simple docstring"""






					_lowercase :    Union[str, Any]							=							self.controlnet_conditioning_channel_order
					if channel_order == "bgr":
						_lowercase :    Tuple							=							jnp.flip(lowerCamelCase_			,					axis=1						)

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

					_lowercase :    List[str]							=							self.time_proj(lowerCamelCase_						)
					_lowercase :    Optional[int]							=							self.time_embedding(lowerCamelCase_						)

					# 2. pre-process
					_lowercase :    Tuple							=							jnp.transpose(lowerCamelCase_			,					(0, 2, 3, 1)						)
					_lowercase :    Dict							=							self.conv_in(lowerCamelCase_						)

					_lowercase :    Tuple							=							jnp.transpose(lowerCamelCase_			,					(0, 2, 3, 1)						)
					_lowercase :    List[str]							=							self.controlnet_cond_embedding(lowerCamelCase_						)
					sample += controlnet_cond

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

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

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

					_lowercase :    Optional[Any]							=							controlnet_down_block_res_samples

					_lowercase :    Union[str, Any]							=							self.controlnet_mid_block(lowerCamelCase_						)

					# 6. scaling
					_lowercase :    int							=							[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=lowerCamelCase_			,					mid_block_res_sample=lowerCamelCase_						)




"""simple docstring"""







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


SCREAMING_SNAKE_CASE									=						logging.get_logger(__name__)



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  ["pixel_values"]







				def __init__( self    :       Optional[int]			,					lowerCamelCase_    :       bool = True			,					lowerCamelCase_    :       Optional[Dict[str, int]] = None			,					lowerCamelCase_    :       PILImageResampling = PILImageResampling.BILINEAR			,					lowerCamelCase_    :       bool = True			,					lowerCamelCase_    :       Dict[str, int] = None			,					lowerCamelCase_    :       bool = True			,					lowerCamelCase_    :       Union[int, float] = 1 / 2_5_5			,					lowerCamelCase_    :       bool = True			,					lowerCamelCase_    :       Optional[Union[float, List[float]]] = None			,					lowerCamelCase_    :       Optional[Union[float, List[float]]] = None			,					**lowerCamelCase_    :       int			,					):

					"""simple docstring"""






					super().__init__(**lowerCamelCase_						)
					_lowercase :    Optional[Any]							=							size if size is not None else {'shortest_edge': 2_5_6}
					_lowercase :    Optional[int]							=							get_size_dict(lowerCamelCase_			,					default_to_square=lowerCamelCase_						)
					_lowercase :    List[str]							=							crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
					_lowercase :    Any							=							get_size_dict(lowerCamelCase_						)
					_lowercase :    Optional[int]							=							do_resize
					_lowercase :    Optional[int]							=							size
					_lowercase :    Union[str, Any]							=							resample
					_lowercase :    Optional[Any]							=							do_center_crop
					_lowercase :    Union[str, Any]							=							crop_size
					_lowercase :    Any							=							do_rescale
					_lowercase :    Tuple							=							rescale_factor
					_lowercase :    Optional[int]							=							do_normalize
					_lowercase :    int							=							image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
					_lowercase :    Any							=							image_std if image_std is not None else IMAGENET_STANDARD_STD







				def 							__UpperCAmelCase					( self    :       List[Any]			,					lowerCamelCase_    :       np.ndarray			,					lowerCamelCase_    :       Dict[str, int]			,					lowerCamelCase_    :       PILImageResampling = PILImageResampling.BICUBIC			,					lowerCamelCase_    :       Optional[Union[str, ChannelDimension]] = None			,					**lowerCamelCase_    :       Tuple			,					):

					"""simple docstring"""






					_lowercase :    Optional[Any]							=							get_size_dict(lowerCamelCase_			,					default_to_square=lowerCamelCase_						)
					if "shortest_edge" not in size:
						raise ValueError(F'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''						)
					_lowercase :    Dict							=							get_resize_output_image_size(lowerCamelCase_			,					size=size['shortest_edge']			,					default_to_square=lowerCamelCase_						)
					return resize(lowerCamelCase_			,					size=lowerCamelCase_			,					resample=lowerCamelCase_			,					data_format=lowerCamelCase_			,					**lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       Dict			,					lowerCamelCase_    :       np.ndarray			,					lowerCamelCase_    :       Dict[str, int]			,					lowerCamelCase_    :       Optional[Union[str, ChannelDimension]] = None			,					**lowerCamelCase_    :       str			,					):

					"""simple docstring"""






					_lowercase :    Tuple							=							get_size_dict(lowerCamelCase_						)
					return center_crop(lowerCamelCase_			,					size=(size['height'], size['width'])			,					data_format=lowerCamelCase_			,					**lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       Any			,					lowerCamelCase_    :       np.ndarray			,					lowerCamelCase_    :       float			,					lowerCamelCase_    :       Optional[Union[str, ChannelDimension]] = None			,					**lowerCamelCase_    :       Optional[Any]						):

					"""simple docstring"""






					return rescale(lowerCamelCase_			,					scale=lowerCamelCase_			,					data_format=lowerCamelCase_			,					**lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       List[Any]			,					lowerCamelCase_    :       np.ndarray			,					lowerCamelCase_    :       Union[float, List[float]]			,					lowerCamelCase_    :       Union[float, List[float]]			,					lowerCamelCase_    :       Optional[Union[str, ChannelDimension]] = None			,					**lowerCamelCase_    :       List[Any]			,					):

					"""simple docstring"""






					return normalize(lowerCamelCase_			,					mean=lowerCamelCase_			,					std=lowerCamelCase_			,					data_format=lowerCamelCase_			,					**lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       List[Any]			,					lowerCamelCase_    :       ImageInput			,					lowerCamelCase_    :       Optional[bool] = None			,					lowerCamelCase_    :       Dict[str, int] = None			,					lowerCamelCase_    :       PILImageResampling = None			,					lowerCamelCase_    :       bool = None			,					lowerCamelCase_    :       Dict[str, int] = None			,					lowerCamelCase_    :       Optional[bool] = None			,					lowerCamelCase_    :       Optional[float] = None			,					lowerCamelCase_    :       Optional[bool] = None			,					lowerCamelCase_    :       Optional[Union[float, List[float]]] = None			,					lowerCamelCase_    :       Optional[Union[float, List[float]]] = None			,					lowerCamelCase_    :       Optional[Union[str, TensorType]] = None			,					lowerCamelCase_    :       Union[str, ChannelDimension] = ChannelDimension.FIRST			,					**lowerCamelCase_    :       int			,					):

					"""simple docstring"""






					_lowercase :    Optional[Any]							=							do_resize if do_resize is not None else self.do_resize
					_lowercase :    List[Any]							=							size if size is not None else self.size
					_lowercase :    Union[str, Any]							=							get_size_dict(lowerCamelCase_			,					default_to_square=lowerCamelCase_						)
					_lowercase :    Any							=							resample if resample is not None else self.resample
					_lowercase :    Tuple							=							do_center_crop if do_center_crop is not None else self.do_center_crop
					_lowercase :    List[Any]							=							crop_size if crop_size is not None else self.crop_size
					_lowercase :    Tuple							=							get_size_dict(lowerCamelCase_						)
					_lowercase :    List[Any]							=							do_rescale if do_rescale is not None else self.do_rescale
					_lowercase :    int							=							rescale_factor if rescale_factor is not None else self.rescale_factor
					_lowercase :    int							=							do_normalize if do_normalize is not None else self.do_normalize
					_lowercase :    str							=							image_mean if image_mean is not None else self.image_mean
					_lowercase :    List[Any]							=							image_std if image_std is not None else self.image_std

					_lowercase :    Union[str, Any]							=							make_list_of_images(lowerCamelCase_						)

					if not valid_images(lowerCamelCase_						):
						raise ValueError(
						    'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
						    'torch.Tensor, tf.Tensor or jax.ndarray.'						)

					if do_resize and size is None:
						raise ValueError('Size must be specified if do_resize is True.'						)

					if do_center_crop and crop_size is None:
						raise ValueError('Crop size must be specified if do_center_crop is True.'						)

					if do_rescale and rescale_factor is None:
						raise ValueError('Rescale factor must be specified if do_rescale is True.'						)

					if do_normalize and (image_mean is None or image_std is None):
						raise ValueError('Image mean and std must be specified if do_normalize is True.'						)

					# All transformations expect numpy arrays.
					_lowercase :    Optional[Any]							=							[to_numpy_array(lowerCamelCase_						) for image in images]

					if do_resize:
						_lowercase :    Optional[Any]							=							[self.resize(image=lowerCamelCase_			,					size=lowerCamelCase_			,					resample=lowerCamelCase_						) for image in images]

					if do_center_crop:
						_lowercase :    List[Any]							=							[self.center_crop(image=lowerCamelCase_			,					size=lowerCamelCase_						) for image in images]

					if do_rescale:
						_lowercase :    str							=							[self.rescale(image=lowerCamelCase_			,					scale=lowerCamelCase_						) for image in images]

					if do_normalize:
						_lowercase :    Any							=							[self.normalize(image=lowerCamelCase_			,					mean=lowerCamelCase_			,					std=lowerCamelCase_						) for image in images]

					_lowercase :    int							=							[to_channel_dimension_format(lowerCamelCase_			,					lowerCamelCase_						) for image in images]

					_lowercase :    Tuple							=							{'pixel_values': images}
					return BatchFeature(data=lowerCamelCase_			,					tensor_type=lowerCamelCase_						)



