






'''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 a_    (    UpperCamelCase_							):
   _snake_case        =							42
   _snake_case        =							42




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







   def     SCREAMING_SNAKE_CASE__					(self)			->      str:
      """simple docstring"""






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

      __snake_case				:      Union[str, Any]     							= []
      for i in range(len(self.block_out_channels) - 1):
         __snake_case				:      str     							= self.block_out_channels[i]
         __snake_case				:      Optional[Any]     							= self.block_out_channels[i + 1]
         __snake_case				:      Tuple     							= nn.Conv(
             __a					,   kernel_size=(3, 3)					,   padding=((1, 1), (1, 1))					,   dtype=self.dtype					,   )
         blocks.append(__a)
         __snake_case				:      Tuple     							= nn.Conv(
             __a					,   kernel_size=(3, 3)					,   strides=(2, 2)					,   padding=((1, 1), (1, 1))					,   dtype=self.dtype					,   )
         blocks.append(__a)
      __snake_case				:      Optional[Any]     							= 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					,   __a)			->      Dict:
      """simple docstring"""






      __snake_case				:      Tuple     							= self.conv_in(__a)
      __snake_case				:      Dict     							= nn.silu(__a)

      for block in self.blocks:
         __snake_case				:      Optional[Any]     							= block(__a)
         __snake_case				:      Any     							= nn.silu(__a)

      __snake_case				:      List[Any]     							= self.conv_out(__a)

      return embedding




@flax_register_to_config
class a_    (    nn.Module		,      UpperCamelCase_		,      UpperCamelCase_							):
   _snake_case        =							32
   _snake_case        =							4
   _snake_case        =							(
       "CrossAttnDownBlock2D",
       "CrossAttnDownBlock2D",
       "CrossAttnDownBlock2D",
       "DownBlock2D",
   )
   _snake_case        =							False
   _snake_case        =							(320, 640, 1280, 1280)
   _snake_case        =							2
   _snake_case        =							8
   _snake_case        =							None
   _snake_case        =							1280
   _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     SCREAMING_SNAKE_CASE__					(self					,   __a)			->      FrozenDict:
      """simple docstring"""






      __snake_case				:      Dict     							= (1, self.in_channels, self.sample_size, self.sample_size)
      __snake_case				:      str     							= jnp.zeros(__a					,   dtype=jnp.floataa)
      __snake_case				:      Union[str, Any]     							= jnp.ones((1,)					,   dtype=jnp.intaa)
      __snake_case				:      Tuple     							= jnp.zeros((1, 1, self.cross_attention_dim)					,   dtype=jnp.floataa)
      __snake_case				:      Dict     							= (1, 3, self.sample_size * 8, self.sample_size * 8)
      __snake_case				:      int     							= jnp.zeros(__a					,   dtype=jnp.floataa)

      __snake_case							,__snake_case				:      int     							= jax.random.split(__a)
      __snake_case				:      Optional[Any]     							= {'params': params_rng, 'dropout': dropout_rng}

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







   def     SCREAMING_SNAKE_CASE__					(self)			->      List[str]:
      """simple docstring"""






      __snake_case				:      int     							= self.block_out_channels
      __snake_case				:      List[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.
      __snake_case				:      Union[str, Any]     							= 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				:      List[str]     							= FlaxTimesteps(
          block_out_channels[0]					,   flip_sin_to_cos=self.flip_sin_to_cos					,   freq_shift=self.config.freq_shift)
      __snake_case				:      Union[str, Any]     							= FlaxTimestepEmbedding(__a					,   dtype=self.dtype)

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

      __snake_case				:      Any     							= self.only_cross_attention
      if isinstance(__a					,   __a):
         __snake_case				:      List[str]     							= (only_cross_attention,) * len(self.down_block_types)

      if isinstance(__a					,   __a):
         __snake_case				:      Any     							= (num_attention_heads,) * len(self.down_block_types)

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

      __snake_case				:      Optional[Any]     							= block_out_channels[0]

      __snake_case				:      Optional[int]     							= nn.Conv(
          __a					,   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(__a)

      for i, down_block_type in enumerate(self.down_block_types):
         __snake_case				:      Optional[int]     							= output_channel
         __snake_case				:      Dict     							= block_out_channels[i]
         __snake_case				:      Optional[Any]     							= i == len(__a) - 1

         if down_block_type == "CrossAttnDownBlock2D":
            __snake_case				:      List[str]     							= FlaxCrossAttnDownBlockaD(
                in_channels=__a					,   out_channels=__a					,   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				:      Tuple     							= FlaxDownBlockaD(
                in_channels=__a					,   out_channels=__a					,   dropout=self.dropout					,   num_layers=self.layers_per_block					,   add_downsample=not is_final_block					,   dtype=self.dtype					,   )

         down_blocks.append(__a)

         for _ in range(self.layers_per_block):
            __snake_case				:      Tuple     							= nn.Conv(
                __a					,   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(__a)

         if not is_final_block:
            __snake_case				:      int     							= nn.Conv(
                __a					,   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(__a)

      __snake_case				:      Optional[int]     							= down_blocks
      __snake_case				:      str     							= controlnet_down_blocks

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

      __snake_case				:      Tuple     							= nn.Conv(
          __a					,   kernel_size=(1, 1)					,   padding='VALID'					,   kernel_init=nn.initializers.zeros_init()					,   bias_init=nn.initializers.zeros_init()					,   dtype=self.dtype					,   )







   def __call__(self					,   __a					,   __a					,   __a					,   __a					,   __a = 1.0					,   __a = True					,   __a = False					,   )			->      Union[FlaxControlNetOutput, Tuple]:
      """simple docstring"""






      __snake_case				:      str     							= self.controlnet_conditioning_channel_order
      if channel_order == "bgr":
         __snake_case				:      Tuple     							= jnp.flip(__a					,   axis=1)

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

      __snake_case				:      int     							= self.time_proj(__a)
      __snake_case				:      List[Any]     							= self.time_embedding(__a)

      # 2. pre-process
      __snake_case				:      Any     							= jnp.transpose(__a					,   (0, 2, 3, 1))
      __snake_case				:      int     							= self.conv_in(__a)

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

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

      # 4. mid
      __snake_case				:      str     							= self.mid_block(__a					,   __a					,   __a					,   deterministic=not train)

      # 5. contronet blocks
      __snake_case				:      Optional[int]     							= ()
      for down_block_res_sample, controlnet_block in zip(__a					,   self.controlnet_down_blocks):
         __snake_case				:      int     							= controlnet_block(__a)
         controlnet_down_block_res_samples += (down_block_res_sample,)

      __snake_case				:      Union[str, Any]     							= controlnet_down_block_res_samples

      __snake_case				:      Dict     							= self.controlnet_mid_block(__a)

      # 6. scaling
      __snake_case				:      Union[str, Any]     							= [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=__a					,   mid_block_res_sample=__a)







'''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, is_vision_available, logging


if is_vision_available():
      import PIL


__A    	=   logging.get_logger(__name__)




class a_    (    UpperCamelCase_							):
   _snake_case        =							["""pixel_values"""]







   def __init__(self					,   __a = True					,   __a = None					,   __a = None					,   __a = PILImageResampling.BILINEAR					,   __a = True					,   __a = 1 / 2_5_5					,   __a = True					,   __a = None					,   __a = None					,   **__a					,   )			->      None:
      """simple docstring"""






      super().__init__(**__a)
      __snake_case				:      Tuple     							= size if size is not None else {'shortest_edge': 3_8_4}
      __snake_case				:      List[Any]     							= get_size_dict(__a					,   default_to_square=__a)

      __snake_case				:      int     							= do_resize
      __snake_case				:      List[str]     							= size
      # Default value set here for backwards compatibility where the value in config is None
      __snake_case				:      Any     							= crop_pct if crop_pct is not None else 2_2_4 / 2_5_6
      __snake_case				:      Tuple     							= resample
      __snake_case				:      Dict     							= do_rescale
      __snake_case				:      Any     							= rescale_factor
      __snake_case				:      str     							= do_normalize
      __snake_case				:      Union[str, Any]     							= image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
      __snake_case				:      Dict     							= image_std if image_std is not None else IMAGENET_STANDARD_STD







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a					,   __a = PILImageResampling.BICUBIC					,   __a = None					,   **__a					,   )			->      np.ndarray:
      """simple docstring"""






      __snake_case				:      Dict     							= get_size_dict(__a					,   default_to_square=__a)
      if "shortest_edge" not in size:
         raise ValueError(F"""Size dictionary must contain 'shortest_edge' key. Got {size.keys()}""")
      __snake_case				:      List[str]     							= size['shortest_edge']

      if shortest_edge < 3_8_4:
         # maintain same ratio, resizing shortest edge to shortest_edge/crop_pct
         __snake_case				:      Any     							= int(shortest_edge / crop_pct)
         __snake_case				:      Any     							= get_resize_output_image_size(__a					,   size=__a					,   default_to_square=__a)
         __snake_case				:      int     							= resize(image=__a					,   size=__a					,   resample=__a					,   data_format=__a					,   **__a)
         # then crop to (shortest_edge, shortest_edge)
         return center_crop(image=__a					,   size=(shortest_edge, shortest_edge)					,   data_format=__a					,   **__a)
      else:
         # warping (no cropping) when evaluated at 384 or larger
         return resize(
             __a					,   size=(shortest_edge, shortest_edge)					,   resample=__a					,   data_format=__a					,   **__a)







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a = None					,   **__a					,   )			->      Any:
      """simple docstring"""






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







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a					,   __a = None					,   **__a					,   )			->      np.ndarray:
      """simple docstring"""






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







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a = None					,   __a = None					,   __a = None					,   __a = None					,   __a = None					,   __a = None					,   __a = None					,   __a = None					,   __a = None					,   __a = None					,   __a = ChannelDimension.FIRST					,   **__a					,   )			->      PIL.Image.Image:
      """simple docstring"""






      __snake_case				:      Optional[int]     							= do_resize if do_resize is not None else self.do_resize
      __snake_case				:      Dict     							= crop_pct if crop_pct is not None else self.crop_pct
      __snake_case				:      Tuple     							= resample if resample is not None else self.resample
      __snake_case				:      Any     							= do_rescale if do_rescale is not None else self.do_rescale
      __snake_case				:      Any     							= rescale_factor if rescale_factor is not None else self.rescale_factor
      __snake_case				:      Union[str, Any]     							= do_normalize if do_normalize is not None else self.do_normalize
      __snake_case				:      Optional[int]     							= image_mean if image_mean is not None else self.image_mean
      __snake_case				:      Optional[Any]     							= image_std if image_std is not None else self.image_std

      __snake_case				:      List[str]     							= size if size is not None else self.size
      __snake_case				:      Any     							= get_size_dict(__a					,   default_to_square=__a)

      __snake_case				:      Dict     							= make_list_of_images(__a)

      if not valid_images(__a):
         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 or resample is None:
         raise ValueError('Size and resample must be specified if do_resize is True.')

      if do_resize and size["shortest_edge"] < 3_8_4 and crop_pct is None:
         raise ValueError('crop_pct must be specified if size < 384.')

      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.
      __snake_case				:      Tuple     							= [to_numpy_array(__a) for image in images]

      if do_resize:
         __snake_case				:      Optional[int]     							= [self.resize(image=__a					,   size=__a					,   crop_pct=__a					,   resample=__a) for image in images]

      if do_rescale:
         __snake_case				:      Optional[int]     							= [self.rescale(image=__a					,   scale=__a) for image in images]

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

      __snake_case				:      Dict     							= [to_channel_dimension_format(__a					,   __a) for image in images]

      __snake_case				:      Union[str, Any]     							= {'pixel_values': images}
      return BatchFeature(data=__a					,   tensor_type=__a)