





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
			import torch

if is_vision_available():
			from PIL import Image

			from transformers import LevitImageProcessor






class       __A					(					unittest.TestCase				):

			'''simple docstring'''







			def __init__(					self ,						__lowerCAmelCase ,						__lowerCAmelCase=7 ,						__lowerCAmelCase=3 ,						__lowerCAmelCase=1_8 ,						__lowerCAmelCase=3_0 ,						__lowerCAmelCase=4_0_0 ,						__lowerCAmelCase=True ,						__lowerCAmelCase=None ,						__lowerCAmelCase=True ,						__lowerCAmelCase=None ,						__lowerCAmelCase=True ,						__lowerCAmelCase=[0.5, 0.5, 0.5] ,						__lowerCAmelCase=[0.5, 0.5, 0.5] ,						):



				'''simple docstring'''







				lowerCamelCase__              =	size if size is not None else {'''shortest_edge''': 1_8}
				lowerCamelCase__              =	crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
				lowerCamelCase__              =	parent
				lowerCamelCase__              =	batch_size
				lowerCamelCase__              =	num_channels
				lowerCamelCase__              =	image_size
				lowerCamelCase__              =	min_resolution
				lowerCamelCase__              =	max_resolution
				lowerCamelCase__              =	do_resize
				lowerCamelCase__              =	size
				lowerCamelCase__              =	do_center_crop
				lowerCamelCase__              =	crop_size
				lowerCamelCase__              =	do_normalize
				lowerCamelCase__              =	image_mean
				lowerCamelCase__              =	image_std





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				return {
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				    "do_normalize": self.do_normalize,
				    "do_resize": self.do_resize,
				    "do_center_crop": self.do_center_crop,
				    "size": self.size,
				    "crop_size": self.crop_size,
				}






@require_torch
@require_vision
class       __A					(					lowerCAmelCase    ,       unittest.TestCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		LevitImageProcessor if is_vision_available() else None





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	LevitImageProcessingTester(self							)





			@property
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				return self.image_processor_tester.prepare_image_processor_dict()





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.image_processing_class(**self.image_processor_dict							)
				self.assertTrue(hasattr(__lowerCAmelCase ,						'''image_mean'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase ,						'''image_std'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase ,						'''do_normalize'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase ,						'''do_resize'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase ,						'''do_center_crop'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase ,						'''size'''							)							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.image_processing_class.from_dict(self.image_processor_dict							)
				self.assertEqual(image_processor.size ,						{'''shortest_edge''': 1_8}							)
				self.assertEqual(image_processor.crop_size ,						{'''height''': 1_8, '''width''': 1_8}							)

				lowerCamelCase__              =	self.image_processing_class.from_dict(self.image_processor_dict ,						size=4_2 ,						crop_size=8_4							)
				self.assertEqual(image_processor.size ,						{'''shortest_edge''': 4_2}							)
				self.assertEqual(image_processor.crop_size ,						{'''height''': 8_4, '''width''': 8_4}							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.image_processing_class(**self.image_processor_dict							)
				# create random PIL images
				lowerCamelCase__              =	prepare_image_inputs(self.image_processor_tester ,						equal_resolution=__lowerCAmelCase							)
				for image in image_inputs:
					self.assertIsInstance(__lowerCAmelCase ,						Image.Image							)

				# Test not batched input
				lowerCamelCase__              =	image_processing(image_inputs[0] ,						return_tensors='''pt'''							).pixel_values
				self.assertEqual(
				    encoded_images.shape ,						(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.crop_size['''height'''],
				        self.image_processor_tester.crop_size['''width'''],
				    ) ,						)

				# Test batched
				lowerCamelCase__              =	image_processing(__lowerCAmelCase ,						return_tensors='''pt'''							).pixel_values
				self.assertEqual(
				    encoded_images.shape ,						(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.crop_size['''height'''],
				        self.image_processor_tester.crop_size['''width'''],
				    ) ,						)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.image_processing_class(**self.image_processor_dict							)
				# create random numpy tensors
				lowerCamelCase__              =	prepare_image_inputs(self.image_processor_tester ,						equal_resolution=__lowerCAmelCase ,						numpify=__lowerCAmelCase							)
				for image in image_inputs:
					self.assertIsInstance(__lowerCAmelCase ,						np.ndarray							)

				# Test not batched input
				lowerCamelCase__              =	image_processing(image_inputs[0] ,						return_tensors='''pt'''							).pixel_values
				self.assertEqual(
				    encoded_images.shape ,						(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.crop_size['''height'''],
				        self.image_processor_tester.crop_size['''width'''],
				    ) ,						)

				# Test batched
				lowerCamelCase__              =	image_processing(__lowerCAmelCase ,						return_tensors='''pt'''							).pixel_values
				self.assertEqual(
				    encoded_images.shape ,						(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.crop_size['''height'''],
				        self.image_processor_tester.crop_size['''width'''],
				    ) ,						)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.image_processing_class(**self.image_processor_dict							)
				# create random PyTorch tensors
				lowerCamelCase__              =	prepare_image_inputs(self.image_processor_tester ,						equal_resolution=__lowerCAmelCase ,						torchify=__lowerCAmelCase							)
				for image in image_inputs:
					self.assertIsInstance(__lowerCAmelCase ,						torch.Tensor							)

				# Test not batched input
				lowerCamelCase__              =	image_processing(image_inputs[0] ,						return_tensors='''pt'''							).pixel_values
				self.assertEqual(
				    encoded_images.shape ,						(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.crop_size['''height'''],
				        self.image_processor_tester.crop_size['''width'''],
				    ) ,						)

				# Test batched
				lowerCamelCase__              =	image_processing(__lowerCAmelCase ,						return_tensors='''pt'''							).pixel_values
				self.assertEqual(
				    encoded_images.shape ,						(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.crop_size['''height'''],
				        self.image_processor_tester.crop_size['''width'''],
				    ) ,						)










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							)



