




import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VivitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor


if is_torch_available():
							import torch
							from torch import nn

							from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VivitForVideoClassification, VivitModel
							from transformers.models.vivit.modeling_vivit import VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from transformers import VivitImageProcessor


class 				__SCREAMING_SNAKE_CASE							:

					def __init__(				self ,       __lowercase ,       __lowercase=2 ,       __lowercase=True ,       __lowercase=True ,       __lowercase=10 ,       __lowercase=10 ,       __lowercase=8 ,       __lowercase=[2, 4, 4] ,       __lowercase=3 ,       __lowercase=768 ,       __lowercase=5 ,       __lowercase=4 ,       __lowercase=37 ,       __lowercase="gelu_fast" ,       __lowercase=0.0 ,       __lowercase=0.0 ,       __lowercase=0.02 ,       __lowercase=1E-06 ,       __lowercase=True ,       __lowercase=None ,       ):







						'''simple docstring'''




						lowerCamelCase_							:		Tuple							   =							parent
						lowerCamelCase_							:		List[str]							   =							batch_size
						lowerCamelCase_							:		int							   =							is_training
						lowerCamelCase_							:		Any							   =							use_labels
						lowerCamelCase_							:		Union[str, Any]							   =							num_labels
						lowerCamelCase_							:		List[str]							   =							image_size
						lowerCamelCase_							:		int							   =							num_frames
						lowerCamelCase_							:		List[str]							   =							tubelet_size
						lowerCamelCase_							:		Tuple							   =							num_channels
						lowerCamelCase_							:		List[str]							   =							hidden_size
						lowerCamelCase_							:		Optional[Any]							   =							num_hidden_layers
						lowerCamelCase_							:		str							   =							num_attention_heads
						lowerCamelCase_							:		Union[str, Any]							   =							intermediate_size
						lowerCamelCase_							:		int							   =							hidden_act
						lowerCamelCase_							:		Union[str, Any]							   =							hidden_dropout_prob
						lowerCamelCase_							:		Optional[int]							   =							attention_probs_dropout_prob
						lowerCamelCase_							:		str							   =							initializer_range
						lowerCamelCase_							:		List[Any]							   =							layer_norm_eps
						lowerCamelCase_							:		Optional[int]							   =							qkv_bias
						lowerCamelCase_							:		str							   =							scope

						lowerCamelCase_							:		Any							   =							(
						    (self.image_size // self.tubelet_size[2])
						    * (self.image_size // self.tubelet_size[1])
						    * (self.num_frames // self.tubelet_size[0])
						) + 1  # CLS token

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		int							   =							floats_tensor(
						    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

						lowerCamelCase_							:		int							   =							None
						if self.use_labels:
							lowerCamelCase_							:		Union[str, Any]							   =							ids_tensor([self.batch_size] ,       self.num_labels					)

						lowerCamelCase_							:		Tuple							   =							self.get_config()

						return config, pixel_values, labels

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		str							   =							VivitConfig(
						    num_frames=self.num_frames ,       image_size=self.image_size ,       tubelet_size=self.tubelet_size ,       num_channels=self.num_channels ,       hidden_size=self.hidden_size ,       num_hidden_layers=self.num_hidden_layers ,       num_attention_heads=self.num_attention_heads ,       intermediate_size=self.intermediate_size ,       hidden_act=self.hidden_act ,       hidden_dropout_prob=self.hidden_dropout_prob ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob ,       initializer_range=self.initializer_range ,       layer_norm_eps=self.layer_norm_eps ,       qkv_bias=self.qkv_bias ,       )
						lowerCamelCase_							:		List[str]							   =							self.num_labels
						return config

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase					):







						'''simple docstring'''




						lowerCamelCase_							:		Any							   =							VivitModel(config=__lowercase					)
						model.to(__lowercase					)
						model.eval()
						lowerCamelCase_							:		str							   =							model(__lowercase					)
						self.parent.assertEqual(result.last_hidden_state.shape ,       (self.batch_size, self.seq_length, self.hidden_size)					)

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase					):







						'''simple docstring'''




						lowerCamelCase_							:		List[str]							   =							VivitForVideoClassification(__lowercase					)
						model.to(__lowercase					)
						model.eval()

						lowerCamelCase_							:		Optional[Any]							   =							model(__lowercase					)

						# verify the logits shape
						lowerCamelCase_							:		Optional[Any]							   =							torch.Size((self.batch_size, self.num_labels)					)
						self.parent.assertEqual(result.logits.shape ,       __lowercase					)





					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							   =							self.prepare_config_and_inputs()
						lowerCamelCase_   ,			lowerCamelCase_   ,			lowerCamelCase_							:		int							   =							config_and_inputs
						lowerCamelCase_							:		Optional[int]							   =							{'pixel_values': pixel_values}
						return config, inputs_dict


@require_torch
class 				__SCREAMING_SNAKE_CASE							(		lowerCamelCase   ,     unittest.TestCase  ):
					_lowerCamelCase		  = (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

					_lowerCamelCase		  = False
					_lowerCamelCase		  = False
					_lowerCamelCase		  = False
					_lowerCamelCase		  = False

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		int							   =							VivitModelTester(self					)
						lowerCamelCase_							:		List[Any]							   =							ConfigTester(self ,       config_class=__lowercase ,       has_text_modality=__lowercase ,       hidden_size=37					)

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase=False					):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[int]							   =							copy.deepcopy(__lowercase					)

						if return_labels:
							if model_class in get_values(__lowercase					):
								lowerCamelCase_							:		Optional[int]							   =							torch.zeros(
								    self.model_tester.batch_size ,       dtype=torch.long ,       device=__lowercase					)

						return inputs_dict

					def 	lowercase_							(				self					):







						'''simple docstring'''




						self.config_tester.run_common_tests()

					@unittest.skip(reason='Vivit does not use inputs_embeds'					)
					def 	lowercase_							(				self					):







						'''simple docstring'''




						pass

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_   ,			lowerCamelCase_							:		List[Any]							   =							self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCamelCase_							:		List[str]							   =							model_class(__lowercase					)
							self.assertIsInstance(model.get_input_embeddings() ,       (nn.Module)					)
							lowerCamelCase_							:		List[str]							   =							model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(__lowercase ,       nn.Linear					)					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_   ,			lowerCamelCase_							:		Optional[int]							   =							self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCamelCase_							:		List[str]							   =							model_class(__lowercase					)
							lowerCamelCase_							:		Any							   =							inspect.signature(model.forward					)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							lowerCamelCase_							:		List[str]							   =							[*signature.parameters.keys()]

							lowerCamelCase_							:		Union[str, Any]							   =							['pixel_values', 'head_mask']
							self.assertListEqual(arg_names[:2] ,       __lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Any							   =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Union[str, Any]							   =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_video_classification(*__lowercase					)

					@slow
					def 	lowercase_							(				self					):







						'''simple docstring'''




						for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCamelCase_							:		Dict							   =							VivitModel.from_pretrained(__lowercase					)
							self.assertIsNotNone(__lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_   ,			lowerCamelCase_							:		str							   =							self.model_tester.prepare_config_and_inputs_for_common()
						lowerCamelCase_							:		Optional[int]							   =							True

						for model_class in self.all_model_classes:
							lowerCamelCase_							:		List[Any]							   =							self.model_tester.seq_length

							lowerCamelCase_							:		Tuple							   =							True
							lowerCamelCase_							:		str							   =							False
							lowerCamelCase_							:		List[Any]							   =							True
							lowerCamelCase_							:		Optional[int]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()
							with torch.no_grad():
								lowerCamelCase_							:		Union[str, Any]							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)
							lowerCamelCase_							:		List[Any]							   =							outputs.attentions
							self.assertEqual(len(__lowercase					) ,       self.model_tester.num_hidden_layers					)

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							lowerCamelCase_							:		Optional[int]							   =							True
							lowerCamelCase_							:		List[str]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()
							with torch.no_grad():
								lowerCamelCase_							:		Any							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)
							lowerCamelCase_							:		Dict							   =							outputs.attentions
							self.assertEqual(len(__lowercase					) ,       self.model_tester.num_hidden_layers					)

							self.assertListEqual(
							    list(attentions[0].shape[-3:]					) ,       [self.model_tester.num_attention_heads, seq_len, seq_len] ,       )
							lowerCamelCase_							:		int							   =							len(__lowercase					)

							# Check attention is always last and order is fine
							lowerCamelCase_							:		Optional[int]							   =							True
							lowerCamelCase_							:		List[str]							   =							True
							lowerCamelCase_							:		str							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()
							with torch.no_grad():
								lowerCamelCase_							:		Optional[Any]							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)

							self.assertEqual(out_len + 1 ,       len(__lowercase					)					)

							lowerCamelCase_							:		List[Any]							   =							outputs.attentions

							self.assertEqual(len(__lowercase					) ,       self.model_tester.num_hidden_layers					)

							self.assertListEqual(
							    list(self_attentions[0].shape[-3:]					) ,       [self.model_tester.num_attention_heads, seq_len, seq_len] ,       )

					def 	lowercase_							(				self					):







						'''simple docstring'''




						def check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					):
							lowerCamelCase_							:		Optional[int]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()

							with torch.no_grad():
								lowerCamelCase_							:		Union[str, Any]							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)

							lowerCamelCase_							:		Union[str, Any]							   =							outputs.hidden_states
							lowerCamelCase_							:		List[Any]							   =							self.model_tester.num_hidden_layers + 1
							self.assertEqual(len(__lowercase					) ,       __lowercase					)

							lowerCamelCase_							:		Union[str, Any]							   =							self.model_tester.seq_length

							self.assertListEqual(
							    list(hidden_states[0].shape[-2:]					) ,       [seq_length, self.model_tester.hidden_size] ,       )

						lowerCamelCase_   ,			lowerCamelCase_							:		Optional[int]							   =							self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCamelCase_							:		Optional[Any]							   =							True
							check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							lowerCamelCase_							:		Any							   =							True

							check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					)





					@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'					)
					def 	lowercase_							(				self					):







						'''simple docstring'''




						pass






def 					snake_case					(							)							->							Optional[int]:




	'''simple docstring'''


	lowerCamelCase_							:		Any							   =							hf_hub_download(
	    repo_id='hf-internal-testing/spaghetti-video'						,   filename='eating_spaghetti_32_frames.npy'						,   repo_type='dataset'			)
	lowerCamelCase_							:		Union[str, Any]							   =							np.load(A			)
	return list(A			)



@require_torch
@require_vision
class 				__SCREAMING_SNAKE_CASE							(		unittest.TestCase  ):

					@cached_property
					def 	lowercase_							(				self					):







						'''simple docstring'''




						return VivitImageProcessor() if is_vision_available() else None





					@slow
					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Union[str, Any]							   =							VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400'					).to(__lowercase					)

						lowerCamelCase_							:		Any							   =							self.default_image_processor
						lowerCamelCase_							:		Any							   =							prepare_video()
						lowerCamelCase_							:		List[str]							   =							image_processor(__lowercase ,       return_tensors='pt'					).to(__lowercase					)

						# forward pass
						with torch.no_grad():
							lowerCamelCase_							:		List[Any]							   =							model(**__lowercase					)

						# verify the logits
						lowerCamelCase_							:		Optional[Any]							   =							torch.Size((1, 400)					)
						self.assertEqual(outputs.logits.shape ,       __lowercase					)

						# taken from original model
						lowerCamelCase_							:		List[Any]							   =							torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]					).to(__lowercase					)

						self.assertTrue(torch.allclose(outputs.logits[0, :5] ,       __lowercase ,       atol=1E-4					)					)


import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







class      lowerCamelCase		(  A_							, unittest.TestCase  ):
     UpperCAmelCase__				:							Tuple           =		KandinskyPriorPipeline
     UpperCAmelCase__				:							List[str]           =		["prompt"]
     UpperCAmelCase__				:							List[str]           =		["prompt", "negative_prompt"]
     UpperCAmelCase__				:							List[str]           =		[
         "num_images_per_prompt",
         "generator",
         "num_inference_steps",
         "latents",
         "negative_prompt",
         "guidance_scale",
         "output_type",
         "return_dict",
     ]
     UpperCAmelCase__				:							str           =		False





     @property
     def 		UpperCAmelCase(self      :    List[str]   )						->						Union[str, Any]:
         return 3_2





     @property
     def 		UpperCAmelCase(self      :    Any   )						->						Any:
         return 3_2





     @property
     def 		UpperCAmelCase(self      :    int   )						->						Dict:
         return self.time_input_dim





     @property
     def 		UpperCAmelCase(self      :    str   )						->						Tuple:
         return self.time_input_dim * 4





     @property
     def 		UpperCAmelCase(self      :    Dict   )						->						Dict:
         return 1_0_0





     @property
     def 		UpperCAmelCase(self      :    Any   )						->						int:
         snake_case      =					CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"   )
         return tokenizer





     @property
     def 		UpperCAmelCase(self      :    Optional[Any]   )						->						Optional[Any]:
         torch.manual_seed(0   )
         snake_case      =					CLIPTextConfig(
             bos_token_id=0				,    eos_token_id=2				,    hidden_size=self.text_embedder_hidden_size				,    projection_dim=self.text_embedder_hidden_size				,    intermediate_size=3_7				,    layer_norm_eps=1E-05				,    num_attention_heads=4				,    num_hidden_layers=5				,    pad_token_id=1				,    vocab_size=1_0_0_0				,    )
         return CLIPTextModelWithProjection(_A   )





     @property
     def 		UpperCAmelCase(self      :    List[Any]   )						->						Tuple:
         torch.manual_seed(0   )

         snake_case      =					{
             "num_attention_heads": 2,
             "attention_head_dim": 1_2,
             "embedding_dim": self.text_embedder_hidden_size,
             "num_layers": 1,
         }

         snake_case      =					PriorTransformer(**_A   )
         # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
         snake_case      =					nn.Parameter(torch.ones(model.clip_std.shape   )   )
         return model





     @property
     def 		UpperCAmelCase(self      :    str   )						->						Union[str, Any]:
         torch.manual_seed(0   )
         snake_case      =					CLIPVisionConfig(
             hidden_size=self.text_embedder_hidden_size				,    image_size=2_2_4				,    projection_dim=self.text_embedder_hidden_size				,    intermediate_size=3_7				,    num_attention_heads=4				,    num_channels=3				,    num_hidden_layers=5				,    patch_size=1_4				,    )

         snake_case      =					CLIPVisionModelWithProjection(_A   )
         return model





     @property
     def 		UpperCAmelCase(self      :    List[Any]   )						->						int:
         snake_case      =					CLIPImageProcessor(
             crop_size=2_2_4				,    do_center_crop=_A				,    do_normalize=_A				,    do_resize=_A				,    image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]				,    image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]				,    resample=3				,    size=2_2_4				,    )

         return image_processor





     def 		UpperCAmelCase(self      :    int   )						->						Union[str, Any]:
         snake_case      =					self.dummy_prior
         snake_case      =					self.dummy_image_encoder
         snake_case      =					self.dummy_text_encoder
         snake_case      =					self.dummy_tokenizer
         snake_case      =					self.dummy_image_processor

         snake_case      =					UnCLIPScheduler(
             variance_type="fixed_small_log"				,    prediction_type="sample"				,    num_train_timesteps=1_0_0_0				,    clip_sample=_A				,    clip_sample_range=10.0				,    )

         snake_case      =					{
             "prior": prior,
             "image_encoder": image_encoder,
             "text_encoder": text_encoder,
             "tokenizer": tokenizer,
             "scheduler": scheduler,
             "image_processor": image_processor,
         }

         return components





     def 		UpperCAmelCase(self      :    Optional[int]				,    _A      :    Tuple				,    _A      :    str=0   )						->						Union[str, Any]:
         if str(_A   ).startswith("mps"   ):
             snake_case      =					torch.manual_seed(_A   )
         else:
             snake_case      =					torch.Generator(device=_A   ).manual_seed(_A   )
         snake_case      =					{
             "prompt": "horse",
             "generator": generator,
             "guidance_scale": 4.0,
             "num_inference_steps": 2,
             "output_type": "np",
         }
         return inputs





     def 		UpperCAmelCase(self      :    List[Any]   )						->						int:
         snake_case      =					"cpu"

         snake_case      =					self.get_dummy_components()

         snake_case      =					self.pipeline_class(**_A   )
         snake_case      =					pipe.to(_A   )

         pipe.set_progress_bar_config(disable=_A   )

         snake_case      =					pipe(**self.get_dummy_inputs(_A   )   )
         snake_case      =					output.image_embeds

         snake_case      =					pipe(
             **self.get_dummy_inputs(_A   )				,    return_dict=_A				,    )[0]

         snake_case      =					image[0, -1_0:]
         snake_case      =					image_from_tuple[0, -1_0:]

         assert image.shape == (1, 3_2)

         snake_case      =					np.array(
             [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56]   )

         assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2
         assert np.abs(image_from_tuple_slice.flatten() - expected_slice   ).max() < 1E-2





     @skip_mps
     def 		UpperCAmelCase(self      :    List[Any]   )						->						str:
         snake_case      =					torch_device == "cpu"
         snake_case      =					True
         snake_case      =					False

         self._test_inference_batch_single_identical(
             test_max_difference=_A				,    relax_max_difference=_A				,    test_mean_pixel_difference=_A				,    )





     @skip_mps
     def 		UpperCAmelCase(self      :    Optional[int]   )						->						List[str]:
         snake_case      =					torch_device == "cpu"
         snake_case      =					False

         self._test_attention_slicing_forward_pass(
             test_max_difference=_A				,    test_mean_pixel_difference=_A				,    )

