



import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD

class  snake_case__(nn.Module     ):

					"""simple docstring"""






					lowercase_								=						4_2
					lowercase_								=						4_2
					lowercase_								=						0.0
					lowercase_								=						1
					lowercase_								=						1
					lowercase_								=						True
					lowercase_								=						False
					lowercase_								=						False
					lowercase_								=						False
					lowercase_								=						jnp.floataa







					def 					snake_case	(				self   :      Dict	):
									lowercase__	:       int						=	[]
									lowercase__	:       Tuple						=	[]

									for i in range(self.num_layers	):
													lowercase__	:       List[Any]						=	self.in_channels if i == 0 else self.out_channels

													lowercase__	:       Dict						=	FlaxResnetBlockaD(
													    in_channels=_lowerCAmelCase ,  out_channels=self.out_channels ,  dropout_prob=self.dropout ,  dtype=self.dtype ,  )
													resnets.append(_lowerCAmelCase	)

													lowercase__	:       Any						=	FlaxTransformeraDModel(
													    in_channels=self.out_channels ,  n_heads=self.num_attention_heads ,  d_head=self.out_channels // self.num_attention_heads ,  depth=1 ,  use_linear_projection=self.use_linear_projection ,  only_cross_attention=self.only_cross_attention ,  use_memory_efficient_attention=self.use_memory_efficient_attention ,  dtype=self.dtype ,  )
													attentions.append(_lowerCAmelCase	)

									lowercase__	:       Optional[Any]						=	resnets
									lowercase__	:       Union[str, Any]						=	attentions

									if self.add_downsample:
													lowercase__	:       Optional[Any]						=	FlaxDownsampleaD(self.out_channels ,  dtype=self.dtype	)







					def __call__(				self   :      List[str] ,  SCREAMING_SNAKE_CASE   :      str ,  SCREAMING_SNAKE_CASE   :      int ,  SCREAMING_SNAKE_CASE   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      Optional[int]=True	):
									lowercase__	:       str						=	()

									for resnet, attn in zip(self.resnets ,  self.attentions	):
													lowercase__	:       Optional[int]						=	resnet(_lowerCAmelCase ,  _lowerCAmelCase ,  deterministic=_lowerCAmelCase	)
													lowercase__	:       Tuple						=	attn(_lowerCAmelCase ,  _lowerCAmelCase ,  deterministic=_lowerCAmelCase	)
													output_states += (hidden_states,)

									if self.add_downsample:
													lowercase__	:       str						=	self.downsamplers_a(_lowerCAmelCase	)
													output_states += (hidden_states,)

									return hidden_states, output_states






class  snake_case__(nn.Module     ):

					"""simple docstring"""






					lowercase_								=						4_2
					lowercase_								=						4_2
					lowercase_								=						0.0
					lowercase_								=						1
					lowercase_								=						True
					lowercase_								=						jnp.floataa







					def 					snake_case	(				self   :      Optional[Any]	):
									lowercase__	:       str						=	[]

									for i in range(self.num_layers	):
													lowercase__	:       Tuple						=	self.in_channels if i == 0 else self.out_channels

													lowercase__	:       Union[str, Any]						=	FlaxResnetBlockaD(
													    in_channels=_lowerCAmelCase ,  out_channels=self.out_channels ,  dropout_prob=self.dropout ,  dtype=self.dtype ,  )
													resnets.append(_lowerCAmelCase	)
									lowercase__	:       Any						=	resnets

									if self.add_downsample:
													lowercase__	:       List[str]						=	FlaxDownsampleaD(self.out_channels ,  dtype=self.dtype	)







					def __call__(				self   :      List[str] ,  SCREAMING_SNAKE_CASE   :      List[str] ,  SCREAMING_SNAKE_CASE   :      Optional[Any] ,  SCREAMING_SNAKE_CASE   :      Union[str, Any]=True	):
									lowercase__	:       int						=	()

									for resnet in self.resnets:
													lowercase__	:       Optional[int]						=	resnet(_lowerCAmelCase ,  _lowerCAmelCase ,  deterministic=_lowerCAmelCase	)
													output_states += (hidden_states,)

									if self.add_downsample:
													lowercase__	:       Optional[int]						=	self.downsamplers_a(_lowerCAmelCase	)
													output_states += (hidden_states,)

									return hidden_states, output_states






class  snake_case__(nn.Module     ):

					"""simple docstring"""






					lowercase_								=						4_2
					lowercase_								=						4_2
					lowercase_								=						4_2
					lowercase_								=						0.0
					lowercase_								=						1
					lowercase_								=						1
					lowercase_								=						True
					lowercase_								=						False
					lowercase_								=						False
					lowercase_								=						False
					lowercase_								=						jnp.floataa







					def 					snake_case	(				self   :      int	):
									lowercase__	:       List[str]						=	[]
									lowercase__	:       int						=	[]

									for i in range(self.num_layers	):
													lowercase__	:       Optional[int]						=	self.in_channels if (i == self.num_layers - 1) else self.out_channels
													lowercase__	:       Any						=	self.prev_output_channel if i == 0 else self.out_channels

													lowercase__	:       str						=	FlaxResnetBlockaD(
													    in_channels=resnet_in_channels + res_skip_channels ,  out_channels=self.out_channels ,  dropout_prob=self.dropout ,  dtype=self.dtype ,  )
													resnets.append(_lowerCAmelCase	)

													lowercase__	:       Any						=	FlaxTransformeraDModel(
													    in_channels=self.out_channels ,  n_heads=self.num_attention_heads ,  d_head=self.out_channels // self.num_attention_heads ,  depth=1 ,  use_linear_projection=self.use_linear_projection ,  only_cross_attention=self.only_cross_attention ,  use_memory_efficient_attention=self.use_memory_efficient_attention ,  dtype=self.dtype ,  )
													attentions.append(_lowerCAmelCase	)

									lowercase__	:       Optional[Any]						=	resnets
									lowercase__	:       str						=	attentions

									if self.add_upsample:
													lowercase__	:       Optional[int]						=	FlaxUpsampleaD(self.out_channels ,  dtype=self.dtype	)







					def __call__(				self   :      Optional[Any] ,  SCREAMING_SNAKE_CASE   :      Optional[int] ,  SCREAMING_SNAKE_CASE   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      Tuple ,  SCREAMING_SNAKE_CASE   :      Optional[Any] ,  SCREAMING_SNAKE_CASE   :      Tuple=True	):
									for resnet, attn in zip(self.resnets ,  self.attentions	):
													# pop res hidden states
													lowercase__	:       str						=	res_hidden_states_tuple[-1]
													lowercase__	:       int						=	res_hidden_states_tuple[:-1]
													lowercase__	:       Optional[int]						=	jnp.concatenate((hidden_states, res_hidden_states) ,  axis=-1	)

													lowercase__	:       str						=	resnet(_lowerCAmelCase ,  _lowerCAmelCase ,  deterministic=_lowerCAmelCase	)
													lowercase__	:       Dict						=	attn(_lowerCAmelCase ,  _lowerCAmelCase ,  deterministic=_lowerCAmelCase	)

									if self.add_upsample:
													lowercase__	:       Tuple						=	self.upsamplers_a(_lowerCAmelCase	)

									return hidden_states






class  snake_case__(nn.Module     ):

					"""simple docstring"""






					lowercase_								=						4_2
					lowercase_								=						4_2
					lowercase_								=						4_2
					lowercase_								=						0.0
					lowercase_								=						1
					lowercase_								=						True
					lowercase_								=						jnp.floataa







					def 					snake_case	(				self   :      Optional[Any]	):
									lowercase__	:       Any						=	[]

									for i in range(self.num_layers	):
													lowercase__	:       Optional[int]						=	self.in_channels if (i == self.num_layers - 1) else self.out_channels
													lowercase__	:       Tuple						=	self.prev_output_channel if i == 0 else self.out_channels

													lowercase__	:       List[Any]						=	FlaxResnetBlockaD(
													    in_channels=resnet_in_channels + res_skip_channels ,  out_channels=self.out_channels ,  dropout_prob=self.dropout ,  dtype=self.dtype ,  )
													resnets.append(_lowerCAmelCase	)

									lowercase__	:       Tuple						=	resnets

									if self.add_upsample:
													lowercase__	:       List[str]						=	FlaxUpsampleaD(self.out_channels ,  dtype=self.dtype	)







					def __call__(				self   :      Tuple ,  SCREAMING_SNAKE_CASE   :      Optional[Any] ,  SCREAMING_SNAKE_CASE   :      Dict ,  SCREAMING_SNAKE_CASE   :      List[str] ,  SCREAMING_SNAKE_CASE   :      Optional[int]=True	):
									for resnet in self.resnets:
													# pop res hidden states
													lowercase__	:       Any						=	res_hidden_states_tuple[-1]
													lowercase__	:       Any						=	res_hidden_states_tuple[:-1]
													lowercase__	:       int						=	jnp.concatenate((hidden_states, res_hidden_states) ,  axis=-1	)

													lowercase__	:       Optional[Any]						=	resnet(_lowerCAmelCase ,  _lowerCAmelCase ,  deterministic=_lowerCAmelCase	)

									if self.add_upsample:
													lowercase__	:       Tuple						=	self.upsamplers_a(_lowerCAmelCase	)

									return hidden_states






class  snake_case__(nn.Module     ):

					"""simple docstring"""






					lowercase_								=						4_2
					lowercase_								=						0.0
					lowercase_								=						1
					lowercase_								=						1
					lowercase_								=						False
					lowercase_								=						False
					lowercase_								=						jnp.floataa







					def 					snake_case	(				self   :      Any	):
									lowercase__	:       Tuple						=	[
									    FlaxResnetBlockaD(
									        in_channels=self.in_channels ,  out_channels=self.in_channels ,  dropout_prob=self.dropout ,  dtype=self.dtype ,  )
									]

									lowercase__	:       List[str]						=	[]

									for _ in range(self.num_layers	):
													lowercase__	:       Tuple						=	FlaxTransformeraDModel(
													    in_channels=self.in_channels ,  n_heads=self.num_attention_heads ,  d_head=self.in_channels // self.num_attention_heads ,  depth=1 ,  use_linear_projection=self.use_linear_projection ,  use_memory_efficient_attention=self.use_memory_efficient_attention ,  dtype=self.dtype ,  )
													attentions.append(_lowerCAmelCase	)

													lowercase__	:       Tuple						=	FlaxResnetBlockaD(
													    in_channels=self.in_channels ,  out_channels=self.in_channels ,  dropout_prob=self.dropout ,  dtype=self.dtype ,  )
													resnets.append(_lowerCAmelCase	)

									lowercase__	:       Any						=	resnets
									lowercase__	:       Optional[Any]						=	attentions







					def __call__(				self   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      Tuple ,  SCREAMING_SNAKE_CASE   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      str=True	):
									lowercase__	:       List[Any]						=	self.resnets[0](_lowerCAmelCase ,  _lowerCAmelCase	)
									for attn, resnet in zip(self.attentions ,  self.resnets[1:]	):
													lowercase__	:       List[Any]						=	attn(_lowerCAmelCase ,  _lowerCAmelCase ,  deterministic=_lowerCAmelCase	)
													lowercase__	:       Tuple						=	resnet(_lowerCAmelCase ,  _lowerCAmelCase ,  deterministic=_lowerCAmelCase	)

									return hidden_states








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, 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


SCREAMING_SNAKE_CASE						:List[Any]			=  logging.get_logger(__name__)


class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							['pixel_values']



  def __init__(  self							:							Optional[int]     ,					_lowerCAmelCase							:							bool = True     ,					_lowerCAmelCase							:							Dict[str, int] = None     ,					_lowerCAmelCase							:							PILImageResampling = PIL.Image.BICUBIC     ,					_lowerCAmelCase							:							bool = True     ,					_lowerCAmelCase							:							Dict[str, int] = None     ,					_lowerCAmelCase							:							Union[int, float] = 1 / 2_5_5     ,					_lowerCAmelCase							:							bool = True     ,					_lowerCAmelCase							:							bool = True     ,					_lowerCAmelCase							:							Optional[Union[float, List[float]]] = None     ,					_lowerCAmelCase							:							Optional[Union[float, List[float]]] = None     ,					**_lowerCAmelCase							:							Optional[Any]     ,					) ->			None:




     """simple docstring"""
     super().__init__(**_lowerCAmelCase	)
     snake_case_            =       size if size is not None else {"height": 2_5_6, "width": 2_5_6}
     snake_case_            =       get_size_dict(_lowerCAmelCase	)
     snake_case_            =       crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
     snake_case_            =       get_size_dict(_lowerCAmelCase     ,					param_name="crop_size"	)

     snake_case_            =       do_resize
     snake_case_            =       size
     snake_case_            =       resample
     snake_case_            =       do_center_crop
     snake_case_            =       crop_size
     snake_case_            =       do_rescale
     snake_case_            =       rescale_factor
     snake_case_            =       do_normalize
     snake_case_            =       image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
     snake_case_            =       image_std if image_std is not None else IMAGENET_STANDARD_STD



  def        lowerCAmelCase__	(  self							:							List[str]     ,					_lowerCAmelCase							:							np.ndarray     ,					_lowerCAmelCase							:							Dict[str, int]     ,					_lowerCAmelCase							:							PILImageResampling = PIL.Image.BICUBIC     ,					_lowerCAmelCase							:							Optional[Union[str, ChannelDimension]] = None     ,					**_lowerCAmelCase							:							int     ,					) ->			np.ndarray:




     """simple docstring"""
     snake_case_            =       get_size_dict(_lowerCAmelCase	)
     if "height" not in size or "width" not in size:
        raise ValueError(F'''The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}'''	)
     return resize(
         _lowerCAmelCase     ,					size=(size["height"], size["width"])     ,					resample=_lowerCAmelCase     ,					data_format=_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							np.ndarray     ,					_lowerCAmelCase							:							Dict[str, int]     ,					_lowerCAmelCase							:							Optional[Union[str, ChannelDimension]] = None     ,					**_lowerCAmelCase							:							int     ,					) ->			np.ndarray:




     """simple docstring"""
     snake_case_            =       get_size_dict(_lowerCAmelCase	)
     if "height" not in size or "width" not in size:
        raise ValueError(F'''The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}'''	)
     return center_crop(_lowerCAmelCase     ,					size=(size["height"], size["width"])     ,					data_format=_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							np.ndarray     ,					_lowerCAmelCase							:							Union[int, float]     ,					_lowerCAmelCase							:							Optional[Union[str, ChannelDimension]] = None     ,					**_lowerCAmelCase							:							Union[str, Any]     ,					) ->			Optional[Any]:




     """simple docstring"""
     return rescale(_lowerCAmelCase     ,					scale=_lowerCAmelCase     ,					data_format=_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							np.ndarray     ,					_lowerCAmelCase							:							Union[float, List[float]]     ,					_lowerCAmelCase							:							Union[float, List[float]]     ,					_lowerCAmelCase							:							Optional[Union[str, ChannelDimension]] = None     ,					**_lowerCAmelCase							:							Any     ,					) ->			np.ndarray:




     """simple docstring"""
     return normalize(_lowerCAmelCase     ,					mean=_lowerCAmelCase     ,					std=_lowerCAmelCase     ,					data_format=_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							ImageInput     ,					_lowerCAmelCase							:							bool = None     ,					_lowerCAmelCase							:							Dict[str, int] = None     ,					_lowerCAmelCase							:							int=None     ,					_lowerCAmelCase							:							bool = None     ,					_lowerCAmelCase							:							Dict[str, int] = None     ,					_lowerCAmelCase							:							bool = None     ,					_lowerCAmelCase							:							float = None     ,					_lowerCAmelCase							:							bool = None     ,					_lowerCAmelCase							:							Optional[Union[float, List[float]]] = None     ,					_lowerCAmelCase							:							Optional[Union[float, List[float]]] = None     ,					_lowerCAmelCase							:							Optional[Union[str, TensorType]] = None     ,					_lowerCAmelCase							:							ChannelDimension = ChannelDimension.FIRST     ,					**_lowerCAmelCase							:							Tuple     ,					) ->			PIL.Image.Image:




     """simple docstring"""
     snake_case_            =       do_resize if do_resize is not None else self.do_resize
     snake_case_            =       resample if resample is not None else self.resample
     snake_case_            =       do_center_crop if do_center_crop is not None else self.do_center_crop
     snake_case_            =       do_rescale if do_rescale is not None else self.do_rescale
     snake_case_            =       rescale_factor if rescale_factor is not None else self.rescale_factor
     snake_case_            =       do_normalize if do_normalize is not None else self.do_normalize
     snake_case_            =       image_mean if image_mean is not None else self.image_mean
     snake_case_            =       image_std if image_std is not None else self.image_std

     snake_case_            =       size if size is not None else self.size
     snake_case_            =       get_size_dict(_lowerCAmelCase	)
     snake_case_            =       crop_size if crop_size is not None else self.crop_size
     snake_case_            =       get_size_dict(_lowerCAmelCase     ,					param_name="crop_size"	)

     snake_case_            =       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 or resample is None:
        raise ValueError("Size and resample 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.
     snake_case_            =       [to_numpy_array(_lowerCAmelCase	) for image in images]

     if do_resize:
        snake_case_            =       [self.resize(image=_lowerCAmelCase     ,					size=_lowerCAmelCase     ,					resample=_lowerCAmelCase	) for image in images]

     if do_center_crop:
        snake_case_            =       [self.center_crop(image=_lowerCAmelCase     ,					size=_lowerCAmelCase	) for image in images]

     if do_rescale:
        snake_case_            =       [self.rescale(image=_lowerCAmelCase     ,					scale=_lowerCAmelCase	) for image in images]

     if do_normalize:
        snake_case_            =       [self.normalize(image=_lowerCAmelCase     ,					mean=_lowerCAmelCase     ,					std=_lowerCAmelCase	) for image in images]

     snake_case_            =       [to_channel_dimension_format(_lowerCAmelCase     ,					_lowerCAmelCase	) for image in images]

     snake_case_            =       {"pixel_values": images}
     return BatchFeature(data=_lowerCAmelCase     ,					tensor_type=_lowerCAmelCase	)




