




import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import TimesformerConfig
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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
		    TimesformerForVideoClassification,
		    TimesformerModel,
		)
		from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from transformers import VideoMAEImageProcessor




class     snake_case__							:
				"""simple docstring"""



				def __init__(      self				:     List[Any]					,      __lowerCamelCase				:     str					,      __lowerCamelCase				:     Optional[Any]=13					,      __lowerCamelCase				:     Tuple=10					,      __lowerCamelCase				:     Optional[int]=3					,      __lowerCamelCase				:     List[Any]=2					,      __lowerCamelCase				:     List[Any]=2					,      __lowerCamelCase				:     Union[str, Any]=True					,      __lowerCamelCase				:     List[Any]=True					,      __lowerCamelCase				:     Union[str, Any]=32					,      __lowerCamelCase				:     Any=5					,      __lowerCamelCase				:     str=4					,      __lowerCamelCase				:     Optional[Any]=37					,      __lowerCamelCase				:     str="gelu"					,      __lowerCamelCase				:     List[Any]=0.1					,      __lowerCamelCase				:     List[Any]=0.1					,      __lowerCamelCase				:     Dict=10					,      __lowerCamelCase				:     Optional[int]=0.02					,      __lowerCamelCase				:     Optional[int]="divided_space_time"					,      __lowerCamelCase				:     Tuple=None					,      )					->     Optional[int]:
							a    =			parent
							a    =			batch_size
							a    =			image_size
							a    =			num_channels
							a    =			patch_size
							a    =			num_frames
							a    =			is_training
							a    =			use_labels
							a    =			hidden_size
							a    =			num_hidden_layers
							a    =			num_attention_heads
							a    =			intermediate_size
							a    =			hidden_act
							a    =			hidden_dropout_prob
							a    =			attention_probs_dropout_prob
							a    =			attention_type
							a    =			initializer_range
							a    =			scope
							a    =			num_labels

							# in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
							a    =			(image_size // patch_size) ** 2
							a    =			(num_frames) * self.num_patches_per_frame + 1





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Dict:
							a    =			floats_tensor(
							    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]							)

							a    =			None
							if self.use_labels:
										a    =			ids_tensor([self.batch_size]					,      self.num_labels							)

							a    =			self.get_config()

							return config, pixel_values, labels





				def 							__UpperCAmelCase							(      self				:     Any							)					->     List[str]:
							a    =			TimesformerConfig(
							    image_size=self.image_size					,      patch_size=self.patch_size					,      num_channels=self.num_channels					,      num_frames=self.num_frames					,      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					,      attention_type=self.attention_type					,      )
							a    =			self.num_labels
							return config





				def 							__UpperCAmelCase							(      self				:     Any					,      __lowerCamelCase				:     Dict					,      __lowerCamelCase				:     List[str]					,      __lowerCamelCase				:     Optional[Any]							)					->     Optional[Any]:
							a    =			TimesformerModel(config=__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a    =			model(__lowerCamelCase							)
							self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)							)





				def 							__UpperCAmelCase							(      self				:     Optional[int]					,      __lowerCamelCase				:     Optional[Any]					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     Dict							)					->     Tuple:
							a    =			TimesformerForVideoClassification(__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()

							a    =			model(__lowerCamelCase							)

							# verify the logits shape
							a    =			torch.Size((self.batch_size, self.num_labels)							)
							self.parent.assertEqual(result.logits.shape					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     Any:
							a    =			self.prepare_config_and_inputs()
							a    ,		a    ,		a    =			config_and_inputs
							a    =			{"pixel_values": pixel_values}
							return config, inputs_dict




@require_torch
class     snake_case__							(_UpperCamelCase    ,       _UpperCamelCase    ,       unittest.TestCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							str							 =					(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_       :							str							 =					(
				    {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification}
				    if is_torch_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE_       :							Tuple							 =					False
				SCREAMING_SNAKE_CASE_       :							Union[str, Any]							 =					False
				SCREAMING_SNAKE_CASE_       :							Any							 =					False
				SCREAMING_SNAKE_CASE_       :							str							 =					False





				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     Any:
							a    =			TimesformerModelTester(self							)
							a    =			ConfigTester(
							    self					,      config_class=__lowerCamelCase					,      has_text_modality=__lowerCamelCase					,      hidden_size=37							)





				def 							__UpperCAmelCase							(      self				:     Any					,      __lowerCamelCase				:     Dict					,      __lowerCamelCase				:     Any					,      __lowerCamelCase				:     Any=False							)					->     Any:
							a    =			copy.deepcopy(__lowerCamelCase							)

							if return_labels:
										if model_class in get_values(__lowerCamelCase							):
													a    =			torch.zeros(
													    self.model_tester.batch_size					,      dtype=torch.long					,      device=__lowerCamelCase							)

							return inputs_dict





				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     Dict:
							self.config_tester.run_common_tests()





				@unittest.skip(reason="TimeSformer does not use inputs_embeds"							)
				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     int:
							pass





				def 							__UpperCAmelCase							(      self				:     Optional[int]							)					->     Any:
							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										a    =			model_class(__lowerCamelCase							)
										self.assertIsInstance(model.get_input_embeddings()					,      (nn.Module)							)
										a    =			model.get_output_embeddings()
										self.assertTrue(x is None or isinstance(__lowerCamelCase					,      nn.Linear							)							)





				def 							__UpperCAmelCase							(      self				:     int							)					->     Tuple:
							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

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

										a    =			["pixel_values"]
										self.assertListEqual(arg_names[:1]					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     str:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Optional[int]							)					->     Optional[Any]:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_video_classification(*__lowerCamelCase							)





				@slow
				def 							__UpperCAmelCase							(      self				:     int							)					->     List[str]:
							for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										a    =			TimesformerModel.from_pretrained(__lowerCamelCase							)
										self.assertIsNotNone(__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     str:
							if not self.has_attentions:
										pass

							else:
										a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()
										a    =			True

										for model_class in self.all_model_classes:
													a    =			self.model_tester.seq_length
													a    =			self.model_tester.num_frames

													a    =			True
													a    =			False
													a    =			True
													a    =			model_class(__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													with torch.no_grad():
																a    =			model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)							)
													a    =			outputs.attentions
													self.assertEqual(len(__lowerCamelCase							)					,      self.model_tester.num_hidden_layers							)

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													a    =			True
													a    =			model_class(__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													with torch.no_grad():
																a    =			model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)							)
													a    =			outputs.attentions
													self.assertEqual(len(__lowerCamelCase							)					,      self.model_tester.num_hidden_layers							)

													# attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
													self.assertListEqual(
													    list(attentions[0].shape[-3:]							)					,      [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]					,      )
													a    =			len(__lowerCamelCase							)

													# Check attention is always last and order is fine
													a    =			True
													a    =			True
													a    =			model_class(__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													with torch.no_grad():
																a    =			model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)							)

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

													a    =			outputs.attentions

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

													# attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
													self.assertListEqual(
													    list(self_attentions[0].shape[-3:]							)					,      [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]					,      )





				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     Dict:
							def check_hidden_states_output(__lowerCamelCase				:     Dict					,      __lowerCamelCase				:     str					,      __lowerCamelCase				:     Any							):
										a    =			model_class(__lowerCamelCase							)
										model.to(__lowerCamelCase							)
										model.eval()

										with torch.no_grad():
													a    =			model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)							)

										a    =			outputs.hidden_states
										a    =			self.model_tester.num_hidden_layers + 1
										self.assertEqual(len(__lowerCamelCase							)					,      __lowerCamelCase							)

										a    =			self.model_tester.seq_length

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

							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										a    =			True
										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										a    =			True

										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)




def 			__magic_name__				( ):






			'''simple docstring'''







			a    =			hf_hub_download(
			    repo_id="hf-internal-testing/spaghetti-video",  filename="eating_spaghetti.npy",  repo_type="dataset"   )
			a    =			np.load(A   )
			return list(A   )






@require_torch
@require_vision
class     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



				@cached_property
				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     List[Any]:
							# logits were tested with a different mean and std, so we use the same here
							return (
							    VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]					,      image_std=[0.5, 0.5, 0.5]							)
							    if is_vision_available()
							    else None
							)





				@slow
				def 							__UpperCAmelCase							(      self				:     int							)					->     List[Any]:
							a    =			TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"							).to(
							    __lowerCamelCase							)

							a    =			self.default_image_processor
							a    =			prepare_video()
							a    =			image_processor(video[:8]					,      return_tensors="pt"							).to(__lowerCamelCase							)

							# forward pass
							with torch.no_grad():
										a    =			model(**__lowerCamelCase							)

							# verify the logits
							a    =			torch.Size((1, 4_00)							)
							self.assertEqual(outputs.logits.shape					,      __lowerCamelCase							)

							a    =			torch.tensor([-0.3_016, -0.7_713, -0.4_205]							).to(__lowerCamelCase							)

							self.assertTrue(torch.allclose(outputs.logits[0, :3]					,      __lowerCamelCase					,      atol=1e-4							)							)














"""simple docstring"""



import unittest

import numpy as np

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

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 DPTImageProcessor

class        a     (   unittest.TestCase	):







    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=[0.5, 0.5, 0.5]							,		_lowerCamelCase=[0.5, 0.5, 0.5]							,		):
      lowercase							    =       size if size is not None else {'height': 1_8, 'width': 1_8}
      lowercase							    =       parent
      lowercase							    =       batch_size
      lowercase							    =       num_channels
      lowercase							    =       image_size
      lowercase							    =       min_resolution
      lowercase							    =       max_resolution
      lowercase							    =       do_resize
      lowercase							    =       size
      lowercase							    =       do_normalize
      lowercase							    =       image_mean
      lowercase							    =       image_std







    def 			UpperCamelCase_      (			self       ):
      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
      }


@require_torch
@require_vision
class        a     (   a_,				unittest.TestCase	):
    UpperCAmelCase_ :     Optional[int]	     =DPTImageProcessor if is_vision_available() else None







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       DPTImageProcessingTester(self       )







    @property
    def 			UpperCamelCase_      (			self       ):
      return self.image_processor_tester.prepare_image_processor_dict()







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       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							,		'size'       )       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.image_processing_class.from_dict(self.image_processor_dict       )
      self.assertEqual(image_processor.size							,		{'height': 1_8, 'width': 1_8}       )

      lowercase							    =       self.image_processing_class.from_dict(self.image_processor_dict							,		size=4_2       )
      self.assertEqual(image_processor.size							,		{'height': 4_2, 'width': 4_2}       )







    def 			UpperCamelCase_      (			self       ):
      # Initialize image_processing
      lowercase							    =       self.image_processing_class(**self.image_processor_dict       )
      # create random PIL images
      lowercase							    =       prepare_image_inputs(self.image_processor_tester							,		equal_resolution=_lowerCamelCase       )
      for image in image_inputs:
        self.assertIsInstance(_lowerCamelCase							,		Image.Image       )

      # Test not batched input
      lowercase							    =       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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							    =       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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)







    def 			UpperCamelCase_      (			self       ):
      # Initialize image_processing
      lowercase							    =       self.image_processing_class(**self.image_processor_dict       )
      # create random numpy tensors
      lowercase							    =       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
      lowercase							    =       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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							    =       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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)







    def 			UpperCamelCase_      (			self       ):
      # Initialize image_processing
      lowercase							    =       self.image_processing_class(**self.image_processor_dict       )
      # create random PyTorch tensors
      lowercase							    =       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
      lowercase							    =       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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							    =       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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)


