
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     UpperCAmelCase						:



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


          '''simple docstring'''


          snake_case			:  List[Any]     							= parent
          snake_case			:  int     							= batch_size
          snake_case			:  str     							= image_size
          snake_case			:  Any     							= num_channels
          snake_case			:  Any     							= patch_size
          snake_case			:  Optional[int]     							= num_frames
          snake_case			:  Optional[Any]     							= is_training
          snake_case			:  Tuple     							= use_labels
          snake_case			:  List[Any]     							= hidden_size
          snake_case			:  int     							= num_hidden_layers
          snake_case			:  str     							= num_attention_heads
          snake_case			:  int     							= intermediate_size
          snake_case			:  List[Any]     							= hidden_act
          snake_case			:  Optional[int]     							= hidden_dropout_prob
          snake_case			:  Optional[Any]     							= attention_probs_dropout_prob
          snake_case			:  int     							= attention_type
          snake_case			:  str     							= initializer_range
          snake_case			:  Tuple     							= scope
          snake_case			:  List[Any]     							= num_labels

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



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


          '''simple docstring'''


          snake_case			:  List[Any]     							= floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

          snake_case			:  Tuple     							= None
          if self.use_labels:
                 snake_case			:  Optional[int]     							= ids_tensor([self.batch_size]       ,		self.num_labels					)

          snake_case			:  Optional[int]     							= self.get_config()

          return config, pixel_values, labels



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


          '''simple docstring'''


          snake_case			:  Optional[int]     							= 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       ,		)
          snake_case			:  Optional[Any]     							= self.num_labels
          return config



   def 							_SCREAMING_SNAKE_CASE  (self						:   str       ,		snake_case__						:   str       ,		snake_case__						:   List[Any]       ,		snake_case__						:   int					)						->      Union[str, Any]:


          '''simple docstring'''


          snake_case			:  Any     							= TimesformerModel(config=snake_case__					)
          model.to(snake_case__					)
          model.eval()
          snake_case			:  str     							= model(snake_case__					)
          self.parent.assertEqual(result.last_hidden_state.shape       ,		(self.batch_size, self.seq_length, self.hidden_size)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Any       ,		snake_case__						:   Optional[int]       ,		snake_case__						:   Tuple       ,		snake_case__						:   Optional[Any]					)						->      str:


          '''simple docstring'''


          snake_case			:  int     							= TimesformerForVideoClassification(snake_case__					)
          model.to(snake_case__					)
          model.eval()

          snake_case			:  Dict     							= model(snake_case__					)

          # verify the logits shape
          snake_case			:  Dict     							= torch.Size((self.batch_size, self.num_labels)					)
          self.parent.assertEqual(result.logits.shape       ,		snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Dict					)						->      List[Any]:


          '''simple docstring'''


          snake_case			:  Optional[Any]     							= self.prepare_config_and_inputs()
          snake_case							,   snake_case							,   snake_case			:  Tuple     							= config_and_inputs
          snake_case			:  Optional[int]     							= {"pixel_values": pixel_values}
          return config, inputs_dict






@require_torch
class     UpperCAmelCase						(		__snake_case			,__snake_case			,unittest.TestCase					):
   A__   :						Dict			=	(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
   A__   :						int			=	(
       {'feature-extraction': TimesformerModel, 'video-classification': TimesformerForVideoClassification}
       if is_torch_available()
       else {}
   )

   A__   :						str			=	False
   A__   :						Optional[int]			=	False
   A__   :						str			=	False
   A__   :						Optional[int]			=	False



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


          '''simple docstring'''


          snake_case			:  Dict     							= TimesformerModelTester(self					)
          snake_case			:  Optional[int]     							= ConfigTester(
              self       ,		config_class=snake_case__       ,		has_text_modality=snake_case__       ,		hidden_size=37					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   List[Any]       ,		snake_case__						:   List[str]       ,		snake_case__						:   int       ,		snake_case__						:   Tuple=False					)						->      Tuple:


          '''simple docstring'''


          snake_case			:  Dict     							= copy.deepcopy(snake_case__					)

          if return_labels:
                 if model_class in get_values(snake_case__					):
                        snake_case			:  List[Any]     							= torch.zeros(
                            self.model_tester.batch_size       ,		dtype=torch.long       ,		device=snake_case__					)

          return inputs_dict



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple					)						->      Any:


          '''simple docstring'''


          self.config_tester.run_common_tests()



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


          '''simple docstring'''


          pass



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


          '''simple docstring'''


          snake_case							,   snake_case			:  Dict     							= self.model_tester.prepare_config_and_inputs_for_common()

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



   def 							_SCREAMING_SNAKE_CASE  (self						:   str					)						->      Optional[Any]:


          '''simple docstring'''


          snake_case							,   snake_case			:  int     							= self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 snake_case			:  Optional[int]     							= model_class(snake_case__					)
                 snake_case			:  str     							= inspect.signature(model.forward					)
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 snake_case			:  Union[str, Any]     							= [*signature.parameters.keys()]

                 snake_case			:  Union[str, Any]     							= ["pixel_values"]
                 self.assertListEqual(arg_names[:1]       ,		snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   List[Any]					)						->      int:


          '''simple docstring'''


          snake_case			:  int     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case__					)



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


          '''simple docstring'''


          snake_case			:  Optional[Any]     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_video_classification(*snake_case__					)



   @slow
   def 							_SCREAMING_SNAKE_CASE  (self						:   List[str]					)						->      Union[str, Any]:


          '''simple docstring'''


          for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case			:  int     							= TimesformerModel.from_pretrained(snake_case__					)
                 self.assertIsNotNone(snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   str					)						->      str:


          '''simple docstring'''


          if not self.has_attentions:
                 pass

          else:
                 snake_case							,   snake_case			:  List[Any]     							= self.model_tester.prepare_config_and_inputs_for_common()
                 snake_case			:  Optional[int]     							= True

                 for model_class in self.all_model_classes:
                        snake_case			:  Optional[Any]     							= self.model_tester.seq_length
                        snake_case			:  Tuple     							= self.model_tester.num_frames

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

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

                        # Check attention is always last and order is fine
                        snake_case			:  Dict     							= True
                        snake_case			:  Optional[Any]     							= True
                        snake_case			:  Union[str, Any]     							= model_class(snake_case__					)
                        model.to(snake_case__					)
                        model.eval()
                        with torch.no_grad():
                               snake_case			:  str     							= model(**self._prepare_for_class(snake_case__       ,		snake_case__					)					)

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

                        snake_case			:  Dict     							= outputs.attentions

                        self.assertEqual(len(snake_case__					)       ,		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 							_SCREAMING_SNAKE_CASE  (self						:   Union[str, Any]					)						->      Optional[int]:


          '''simple docstring'''


          def check_hidden_states_output(snake_case__						:   Optional[Any]       ,		snake_case__						:   str       ,		snake_case__						:   Any					):
                 snake_case			:  List[Any]     							= model_class(snake_case__					)
                 model.to(snake_case__					)
                 model.eval()

                 with torch.no_grad():
                        snake_case			:  Any     							= model(**self._prepare_for_class(snake_case__       ,		snake_case__					)					)

                 snake_case			:  Any     							= outputs.hidden_states
                 snake_case			:  Optional[int]     							= self.model_tester.num_hidden_layers + 1
                 self.assertEqual(len(snake_case__					)       ,		snake_case__					)

                 snake_case			:  List[Any]     							= self.model_tester.seq_length

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

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

          for model_class in self.all_model_classes:
                 snake_case			:  Tuple     							= True
                 check_hidden_states_output(snake_case__       ,		snake_case__       ,		snake_case__					)

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

                 check_hidden_states_output(snake_case__       ,		snake_case__       ,		snake_case__					)



def      UpperCamelCase   (       ):
       snake_case			:  List[str]     							= hf_hub_download(
           repo_id="hf-internal-testing/spaghetti-video"							,   filename="eating_spaghetti.npy"							,   repo_type="dataset"       )
       snake_case			:  int     							= np.load(_lowercase       )
       return list(_lowercase       )






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



   @cached_property
   def 							_SCREAMING_SNAKE_CASE  (self						:   int					)						->      Any:


          '''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 							_SCREAMING_SNAKE_CASE  (self						:   Union[str, Any]					)						->      List[str]:


          '''simple docstring'''


          snake_case			:  Union[str, Any]     							= TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"					).to(
              snake_case__					)

          snake_case			:  List[Any]     							= self.default_image_processor
          snake_case			:  Dict     							= prepare_video()
          snake_case			:  Dict     							= image_processor(video[:8]       ,		return_tensors="pt"					).to(snake_case__					)

          # forward pass
          with torch.no_grad():
                 snake_case			:  int     							= model(**snake_case__					)

          # verify the logits
          snake_case			:  List[Any]     							= torch.Size((1, 4_00)					)
          self.assertEqual(outputs.logits.shape       ,		snake_case__					)

          snake_case			:  str     							= torch.tensor([-0.3016, -0.7713, -0.4205]					).to(snake_case__					)

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









"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


lowercase_							=							{
    'configuration_layoutlmv2': ['LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMv2Config'],
    'processing_layoutlmv2': ['LayoutLMv2Processor'],
    'tokenization_layoutlmv2': ['LayoutLMv2Tokenizer'],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							['LayoutLMv2TokenizerFast']

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							['LayoutLMv2FeatureExtractor']
		lowercase_							=							['LayoutLMv2ImageProcessor']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							[
		    'LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'LayoutLMv2ForQuestionAnswering',
		    'LayoutLMv2ForSequenceClassification',
		    'LayoutLMv2ForTokenClassification',
		    'LayoutLMv2Layer',
		    'LayoutLMv2Model',
		    'LayoutLMv2PreTrainedModel',
		]

if TYPE_CHECKING:
		from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig
		from .processing_layoutlmva import LayoutLMvaProcessor
		from .tokenization_layoutlmva import LayoutLMvaTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_layoutlmva import (
				    LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST,
				    LayoutLMvaForQuestionAnswering,
				    LayoutLMvaForSequenceClassification,
				    LayoutLMvaForTokenClassification,
				    LayoutLMvaLayer,
				    LayoutLMvaModel,
				    LayoutLMvaPreTrainedModel,
				)
else:
		import sys

		lowercase_							=							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)