



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,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


lowerCamelCase__			= logging.get_logger(__name__)


if is_vision_available():
						import PIL






class    SCREAMING_SNAKE_CASE			(							lowerCamelCase__					):
							__lowerCamelCase	:	List[str]    =['pixel_values']





							def __init__(   self		:		Any      ,				__lowercase		:		bool = True      ,				__lowercase		:		Dict[str, int] = None      ,				__lowercase		:		PILImageResampling = PILImageResampling.BICUBIC      ,				__lowercase		:		bool = True      ,				__lowercase		:		Dict[str, int] = None      ,				__lowercase		:		bool = True      ,				__lowercase		:		Union[int, float] = 1 / 255      ,				__lowercase		:		bool = True      ,				__lowercase		:		Optional[Union[float, List[float]]] = None      ,				__lowercase		:		Optional[Union[float, List[float]]] = None      ,				__lowercase		:		bool = True      ,				**__lowercase		:		Union[str, Any]      ,				):

											'''simple docstring'''
											super().__init__(**__lowercase	)
											__a       =		size if size is not None else {"""shortest_edge""": 224}
											__a       =		get_size_dict(__lowercase      ,				default_to_square=__lowercase	)
											__a       =		crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
											__a       =		get_size_dict(__lowercase      ,				default_to_square=__lowercase      ,				param_name="""crop_size"""	)

											__a       =		do_resize
											__a       =		size
											__a       =		resample
											__a       =		do_center_crop
											__a       =		crop_size
											__a       =		do_rescale
											__a       =		rescale_factor
											__a       =		do_normalize
											__a       =		image_mean if image_mean is not None else OPENAI_CLIP_MEAN
											__a       =		image_std if image_std is not None else OPENAI_CLIP_STD
											__a       =		do_convert_rgb





							def 						UpperCamelCase_					(   self		:		str      ,				__lowercase		:		np.ndarray      ,				__lowercase		:		Dict[str, int]      ,				__lowercase		:		PILImageResampling = PILImageResampling.BICUBIC      ,				__lowercase		:		Optional[Union[str, ChannelDimension]] = None      ,				**__lowercase		:		Tuple      ,				):

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





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

											'''simple docstring'''
											__a       =		get_size_dict(__lowercase	)
											if "height" not in size or "width" not in size:
															raise ValueError(F"The `size` parameter must contain the keys (height, width). Got {size.keys()}"	)
											return center_crop(__lowercase      ,				size=(size["""height"""], size["""width"""])      ,				data_format=__lowercase      ,				**__lowercase	)





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

											'''simple docstring'''
											return rescale(__lowercase      ,				scale=__lowercase      ,				data_format=__lowercase      ,				**__lowercase	)





							def 						UpperCamelCase_					(   self		:		int      ,				__lowercase		:		np.ndarray      ,				__lowercase		:		Union[float, List[float]]      ,				__lowercase		:		Union[float, List[float]]      ,				__lowercase		:		Optional[Union[str, ChannelDimension]] = None      ,				**__lowercase		:		str      ,				):

											'''simple docstring'''
											return normalize(__lowercase      ,				mean=__lowercase      ,				std=__lowercase      ,				data_format=__lowercase      ,				**__lowercase	)







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

											'''simple docstring'''
											__a       =		do_resize if do_resize is not None else self.do_resize
											__a       =		size if size is not None else self.size
											__a       =		get_size_dict(__lowercase      ,				param_name="""size"""      ,				default_to_square=__lowercase	)
											__a       =		resample if resample is not None else self.resample
											__a       =		do_center_crop if do_center_crop is not None else self.do_center_crop
											__a       =		crop_size if crop_size is not None else self.crop_size
											__a       =		get_size_dict(__lowercase      ,				param_name="""crop_size"""      ,				default_to_square=__lowercase	)
											__a       =		do_rescale if do_rescale is not None else self.do_rescale
											__a       =		rescale_factor if rescale_factor is not None else self.rescale_factor
											__a       =		do_normalize if do_normalize is not None else self.do_normalize
											__a       =		image_mean if image_mean is not None else self.image_mean
											__a       =		image_std if image_std is not None else self.image_std
											__a       =		do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

											__a       =		make_list_of_images(__lowercase	)

											if not valid_images(__lowercase	):
															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."""	)

											# PIL RGBA images are converted to RGB
											if do_convert_rgb:
															__a       =		[convert_to_rgb(__lowercase	) for image in images]

											# All transformations expect numpy arrays.
											__a       =		[to_numpy_array(__lowercase	) for image in images]

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

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

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

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

											__a       =		[to_channel_dimension_format(__lowercase      ,				__lowercase	) for image in images]

											__a       =		{"""pixel_values""": images}
											return BatchFeature(data=__lowercase      ,				tensor_type=__lowercase	)





from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin






@dataclass
class    SCREAMING_SNAKE_CASE			(							lowerCamelCase__					):
							__lowerCamelCase	:	torch.FloatTensor






class    SCREAMING_SNAKE_CASE			(							lowerCamelCase__			,      lowerCamelCase__					):





							@register_to_config
							def __init__(   self		:		Tuple      ,				__lowercase		:		int = 16      ,				__lowercase		:		int = 88      ,				__lowercase		:		Optional[int] = None      ,				__lowercase		:		Optional[int] = None      ,				__lowercase		:		int = 1      ,				__lowercase		:		float = 0.0      ,				__lowercase		:		int = 32      ,				__lowercase		:		Optional[int] = None      ,				__lowercase		:		bool = False      ,				__lowercase		:		Optional[int] = None      ,				__lowercase		:		str = "geglu"      ,				__lowercase		:		bool = True      ,				__lowercase		:		bool = True      ,				):

											'''simple docstring'''
											super().__init__()
											__a       =		num_attention_heads
											__a       =		attention_head_dim
											__a       =		num_attention_heads * attention_head_dim

											__a       =		in_channels

											__a       =		torch.nn.GroupNorm(num_groups=__lowercase      ,				num_channels=__lowercase      ,				eps=1E-6      ,				affine=__lowercase	)
											__a       =		nn.Linear(__lowercase      ,				__lowercase	)

											# 3. Define transformers blocks
											__a       =		nn.ModuleList(
											    [
											        BasicTransformerBlock(
											            __lowercase      ,				__lowercase      ,				__lowercase      ,				dropout=__lowercase      ,				cross_attention_dim=__lowercase      ,				activation_fn=__lowercase      ,				attention_bias=__lowercase      ,				double_self_attention=__lowercase      ,				norm_elementwise_affine=__lowercase      ,				)
											        for d in range(__lowercase	)
											    ]	)

											__a       =		nn.Linear(__lowercase      ,				__lowercase	)







							def 						UpperCamelCase_					(   self		:		Any      ,				__lowercase		:		Optional[Any]      ,				__lowercase		:		str=None      ,				__lowercase		:		List[Any]=None      ,				__lowercase		:		List[Any]=None      ,				__lowercase		:		int=1      ,				__lowercase		:		Union[str, Any]=None      ,				__lowercase		:		bool = True      ,				):

											'''simple docstring'''
											__a     ,					__a     ,					__a     ,					__a       =		hidden_states.shape
											__a       =		batch_frames // num_frames

											__a       =		hidden_states

											__a       =		hidden_states[None, :].reshape(__lowercase      ,				__lowercase      ,				__lowercase      ,				__lowercase      ,				__lowercase	)
											__a       =		hidden_states.permute(0      ,				2      ,				1      ,				3      ,				4	)

											__a       =		self.norm(__lowercase	)
											__a       =		hidden_states.permute(0      ,				3      ,				4      ,				2      ,				1	).reshape(batch_size * height * width      ,				__lowercase      ,				__lowercase	)

											__a       =		self.proj_in(__lowercase	)

											# 2. Blocks
											for block in self.transformer_blocks:
															__a       =		block(
															    __lowercase      ,				encoder_hidden_states=__lowercase      ,				timestep=__lowercase      ,				cross_attention_kwargs=__lowercase      ,				class_labels=__lowercase      ,				)

											# 3. Output
											__a       =		self.proj_out(__lowercase	)
											__a       =		(
											    hidden_states[None, None, :]
											    .reshape(__lowercase      ,				__lowercase      ,				__lowercase      ,				__lowercase      ,				__lowercase	)
											    .permute(0      ,				3      ,				4      ,				1      ,				2	)
											    .contiguous()
											)
											__a       =		hidden_states.reshape(__lowercase      ,				__lowercase      ,				__lowercase      ,				__lowercase	)

											__a       =		hidden_states + residual

											if not return_dict:
															return (output,)

											return TransformerTemporalModelOutput(sample=__lowercase	)
