





from typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    flip_channel_order,
    get_resize_output_image_size,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging


if is_vision_available():
			import PIL

if is_torch_available():
			import torch


_a						=   logging.get_logger(__name__)






class       __A					(					__UpperCAmelCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		['''pixel_values''']





			def __init__(					self ,						__lowerCAmelCase = True ,						__lowerCAmelCase = None ,						__lowerCAmelCase = PILImageResampling.BILINEAR ,						__lowerCAmelCase = True ,						__lowerCAmelCase = 1 / 2_5_5 ,						__lowerCAmelCase = True ,						__lowerCAmelCase = None ,						__lowerCAmelCase = True ,						**__lowerCAmelCase ,						):



				'''simple docstring'''







				super().__init__(**__SCREAMING_SNAKE_CASE							)
				lowerCamelCase__              =	size if size is not None else {'''shortest_edge''': 2_2_4}
				lowerCamelCase__              =	get_size_dict(__SCREAMING_SNAKE_CASE ,						default_to_square=__SCREAMING_SNAKE_CASE							)
				lowerCamelCase__              =	crop_size if crop_size is not None else {'''height''': 2_5_6, '''width''': 2_5_6}
				lowerCamelCase__              =	get_size_dict(__SCREAMING_SNAKE_CASE ,						param_name='''crop_size'''							)

				lowerCamelCase__              =	do_resize
				lowerCamelCase__              =	size
				lowerCamelCase__              =	resample
				lowerCamelCase__              =	do_rescale
				lowerCamelCase__              =	rescale_factor
				lowerCamelCase__              =	do_center_crop
				lowerCamelCase__              =	crop_size
				lowerCamelCase__              =	do_flip_channel_order





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase = PIL.Image.BILINEAR ,						__lowerCAmelCase = None ,						**__lowerCAmelCase ,						):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase = None ,						**__lowerCAmelCase ,						):



				'''simple docstring'''







				lowerCamelCase__              =	get_size_dict(__SCREAMING_SNAKE_CASE							)
				if "height" not in size or "width" not in size:
					raise ValueError(F'The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'							)
				return center_crop(__SCREAMING_SNAKE_CASE ,						size=(size['''height'''], size['''width''']) ,						data_format=__SCREAMING_SNAKE_CASE ,						**__SCREAMING_SNAKE_CASE							)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase = None ,						**__lowerCAmelCase ,						):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase = None							):



				'''simple docstring'''







				return flip_channel_order(__SCREAMING_SNAKE_CASE ,						data_format=__SCREAMING_SNAKE_CASE							)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = ChannelDimension.FIRST ,						**__lowerCAmelCase ,						):



				'''simple docstring'''







				lowerCamelCase__              =	do_resize if do_resize is not None else self.do_resize
				lowerCamelCase__              =	resample if resample is not None else self.resample
				lowerCamelCase__              =	do_rescale if do_rescale is not None else self.do_rescale
				lowerCamelCase__              =	rescale_factor if rescale_factor is not None else self.rescale_factor
				lowerCamelCase__              =	do_center_crop if do_center_crop is not None else self.do_center_crop
				lowerCamelCase__              =	(
				    do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order
				)

				lowerCamelCase__              =	size if size is not None else self.size
				lowerCamelCase__              =	get_size_dict(__SCREAMING_SNAKE_CASE ,						default_to_square=__SCREAMING_SNAKE_CASE							)
				lowerCamelCase__              =	crop_size if crop_size is not None else self.crop_size
				lowerCamelCase__              =	get_size_dict(__SCREAMING_SNAKE_CASE ,						param_name='''crop_size'''							)

				lowerCamelCase__              =	make_list_of_images(__SCREAMING_SNAKE_CASE							)

				if not valid_images(__SCREAMING_SNAKE_CASE							):
					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_rescale and rescale_factor is None:
					raise ValueError('''Rescale factor must be specified if do_rescale is True.'''							)

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

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

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

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

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

				# the pretrained checkpoints assume images are BGR, not RGB
				if do_flip_channel_order:
					lowerCamelCase__              =	[self.flip_channel_order(image=__SCREAMING_SNAKE_CASE							) for image in images]

				lowerCamelCase__              =	[to_channel_dimension_format(__SCREAMING_SNAKE_CASE ,						__SCREAMING_SNAKE_CASE							) for image in images]

				lowerCamelCase__              =	{'''pixel_values''': images}
				return BatchFeature(data=__SCREAMING_SNAKE_CASE ,						tensor_type=__SCREAMING_SNAKE_CASE							)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase = None							):



				'''simple docstring'''







				lowerCamelCase__              =	outputs.logits

				# Resize logits and compute semantic segmentation maps
				if target_sizes is not None:
					if len(__SCREAMING_SNAKE_CASE							) != len(__SCREAMING_SNAKE_CASE							):
						raise ValueError(
						    '''Make sure that you pass in as many target sizes as the batch dimension of the logits'''							)

					if is_torch_tensor(__SCREAMING_SNAKE_CASE							):
						lowerCamelCase__              =	target_sizes.numpy()

					lowerCamelCase__              =	[]

					for idx in range(len(__SCREAMING_SNAKE_CASE							)							):
						lowerCamelCase__              =	torch.nn.functional.interpolate(
						    logits[idx].unsqueeze(dim=0							) ,						size=target_sizes[idx] ,						mode='''bilinear''' ,						align_corners=__SCREAMING_SNAKE_CASE							)
						lowerCamelCase__              =	resized_logits[0].argmax(dim=0							)
						semantic_segmentation.append(__SCREAMING_SNAKE_CASE							)
				else:
					lowerCamelCase__              =	logits.argmax(dim=1							)
					lowerCamelCase__              =	[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]							)]

				return semantic_segmentation









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       __A					(					lowerCAmelCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		42






class       __A					(					lowerCAmelCase    ,       lowerCAmelCase				):

			'''simple docstring'''







			@register_to_config
			def __init__(					self ,						__lowerCAmelCase = 1_6 ,						__lowerCAmelCase = 8_8 ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = 1 ,						__lowerCAmelCase = 0.0 ,						__lowerCAmelCase = 3_2 ,						__lowerCAmelCase = None ,						__lowerCAmelCase = False ,						__lowerCAmelCase = None ,						__lowerCAmelCase = "geglu" ,						__lowerCAmelCase = True ,						__lowerCAmelCase = True ,						):



				'''simple docstring'''







				super().__init__()
				lowerCamelCase__              =	num_attention_heads
				lowerCamelCase__              =	attention_head_dim
				lowerCamelCase__              =	num_attention_heads * attention_head_dim

				lowerCamelCase__              =	in_channels

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

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

				lowerCamelCase__              =	nn.Linear(__lowerCAmelCase ,						__lowerCAmelCase							)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase=None ,						__lowerCAmelCase=None ,						__lowerCAmelCase=None ,						__lowerCAmelCase=1 ,						__lowerCAmelCase=None ,						__lowerCAmelCase = True ,						):



				'''simple docstring'''







				lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__              =	hidden_states.shape
				lowerCamelCase__              =	batch_frames // num_frames

				lowerCamelCase__              =	hidden_states

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

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

				lowerCamelCase__              =	self.proj_in(__lowerCAmelCase							)

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

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

				lowerCamelCase__              =	hidden_states + residual

				if not return_dict:
					return (output,)

				return TransformerTemporalModelOutput(sample=__lowerCAmelCase							)



