


"""simple docstring"""


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      :







		def __init__(					self      :       Optional[int]    ,   a__      :       Optional[int]    ,   a__      :       Optional[int]=13    ,   a__      :       Any=10    ,   a__      :       Any=3    ,   a__      :       str=2    ,   a__      :       Dict=2    ,   a__      :       int=True    ,   a__      :       Any=True    ,   a__      :       List[str]=32    ,   a__      :       str=5    ,   a__      :       int=4    ,   a__      :       int=37    ,   a__      :       Union[str, Any]="gelu"    ,   a__      :       Tuple=0.1    ,   a__      :       Optional[int]=0.1    ,   a__      :       List[str]=10    ,   a__      :       List[str]=0.0_2    ,   a__      :       str="divided_space_time"    ,   a__      :       Tuple=None    ,   )       ->						List[Any]:





						'''simple docstring'''







						_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  a_ (					self      :       Union[str, Any]		)       ->						Optional[Any]:





						'''simple docstring'''







						_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  a_ (					self      :       List[Any]		)       ->						Optional[Any]:





						'''simple docstring'''







						_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  a_ (					self      :       Dict    ,   a__      :       Tuple    ,   a__      :       str    ,   a__      :       Dict		)       ->						Optional[int]:





						'''simple docstring'''







						_A					  =			TimesformerModel(config=a__		)
						model.to(a__		)
						model.eval()
						_A					  =			model(a__		)
						self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, self.seq_length, self.hidden_size)		)







		def  a_ (					self      :       Optional[int]    ,   a__      :       str    ,   a__      :       Tuple    ,   a__      :       Optional[int]		)       ->						Any:





						'''simple docstring'''







						_A					  =			TimesformerForVideoClassification(a__		)
						model.to(a__		)
						model.eval()

						_A					  =			model(a__		)

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







		def  a_ (					self      :       Dict		)       ->						Dict:





						'''simple docstring'''







						_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):
		__UpperCamelCase           =			(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
		__UpperCamelCase           =			(
		    {'feature-extraction': TimesformerModel, 'video-classification': TimesformerForVideoClassification}
		    if is_torch_available()
		    else {}
		)

		__UpperCamelCase           =			False
		__UpperCamelCase           =			False
		__UpperCamelCase           =			False
		__UpperCamelCase           =			False







		def  a_ (					self      :       str		)       ->						str:





						'''simple docstring'''







						_A					  =			TimesformerModelTester(self		)
						_A					  =			ConfigTester(
						    self    ,   config_class=a__    ,   has_text_modality=a__    ,   hidden_size=37		)







		def  a_ (					self      :       Dict    ,   a__      :       Optional[Any]    ,   a__      :       Union[str, Any]    ,   a__      :       Optional[int]=False		)       ->						Dict:





						'''simple docstring'''







						_A					  =			copy.deepcopy(a__		)

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

						return inputs_dict







		def  a_ (					self      :       List[Any]		)       ->						Optional[Any]:





						'''simple docstring'''







						self.config_tester.run_common_tests()







		@unittest.skip(reason="TimeSformer does not use inputs_embeds"		)
		def  a_ (					self      :       Tuple		)       ->						int:





						'''simple docstring'''







						pass







		def  a_ (					self      :       Union[str, Any]		)       ->						str:





						'''simple docstring'''







						_A  ,		_A					  =			self.model_tester.prepare_config_and_inputs_for_common()

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







		def  a_ (					self      :       List[str]		)       ->						str:





						'''simple docstring'''







						_A  ,		_A					  =			self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										_A					  =			model_class(a__		)
										_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]    ,   a__		)







		def  a_ (					self      :       Tuple		)       ->						Any:





						'''simple docstring'''







						_A					  =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*a__		)







		def  a_ (					self      :       Optional[Any]		)       ->						Optional[Any]:





						'''simple docstring'''







						_A					  =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_video_classification(*a__		)







		@slow
		def  a_ (					self      :       Dict		)       ->						List[str]:





						'''simple docstring'''







						for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										_A					  =			TimesformerModel.from_pretrained(a__		)
										self.assertIsNotNone(a__		)







		def  a_ (					self      :       Optional[int]		)       ->						Dict:





						'''simple docstring'''







						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(a__		)
														model.to(a__		)
														model.eval()
														with torch.no_grad():
																		_A					  =			model(**self._prepare_for_class(a__    ,   a__		)		)
														_A					  =			outputs.attentions
														self.assertEqual(len(a__		)    ,   self.model_tester.num_hidden_layers		)

														# check that output_attentions also work using config
														del inputs_dict["output_attentions"]
														_A					  =			True
														_A					  =			model_class(a__		)
														model.to(a__		)
														model.eval()
														with torch.no_grad():
																		_A					  =			model(**self._prepare_for_class(a__    ,   a__		)		)
														_A					  =			outputs.attentions
														self.assertEqual(len(a__		)    ,   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(a__		)

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

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

														_A					  =			outputs.attentions

														self.assertEqual(len(a__		)    ,   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  a_ (					self      :       Any		)       ->						Optional[Any]:





						'''simple docstring'''







						def check_hidden_states_output(a__      :       List[str]    ,   a__      :       Dict    ,   a__      :       Union[str, Any]		):
										_A					  =			model_class(a__		)
										model.to(a__		)
										model.eval()

										with torch.no_grad():
														_A					  =			model(**self._prepare_for_class(a__    ,   a__		)		)

										_A					  =			outputs.hidden_states
										_A					  =			self.model_tester.num_hidden_layers + 1
										self.assertEqual(len(a__		)    ,   a__		)

										_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(a__    ,   a__    ,   a__		)

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

										check_hidden_states_output(a__    ,   a__    ,   a__		)






def      a__ (				)	->	Tuple:
				_A					  =			hf_hub_download(
				    repo_id="hf-internal-testing/spaghetti-video"     ,			filename="eating_spaghetti.npy"     ,			repo_type="dataset"   )
				_A					  =			np.load(__lowercase   )
				return list(__lowercase   )




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







		@cached_property
		def  a_ (					self      :       List[str]		)       ->						Optional[int]:





						'''simple docstring'''







						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  a_ (					self      :       Dict		)       ->						Union[str, Any]:





						'''simple docstring'''







						_A					  =			TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"		).to(
						    a__		)

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

						# forward pass
						with torch.no_grad():
										_A					  =			model(**a__		)

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

						_A					  =			torch.tensor([-0.3_0_1_6, -0.7_7_1_3, -0.4_2_0_5]		).to(a__		)

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



"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


a_       							=    {
    "configuration_upernet": ["UperNetConfig"],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a_       							=    [
		    "UperNetForSemanticSegmentation",
		    "UperNetPreTrainedModel",
		]


if TYPE_CHECKING:
		from .configuration_upernet import UperNetConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel


else:
		import sys

		a_       							=    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)