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

 """simple docstring"""

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






      """simple docstring"""



      UpperCamelCase						    =					parent
      UpperCamelCase						    =					batch_size
      UpperCamelCase						    =					image_size
      UpperCamelCase						    =					num_channels
      UpperCamelCase						    =					patch_size
      UpperCamelCase						    =					num_frames
      UpperCamelCase						    =					is_training
      UpperCamelCase						    =					use_labels
      UpperCamelCase						    =					hidden_size
      UpperCamelCase						    =					num_hidden_layers
      UpperCamelCase						    =					num_attention_heads
      UpperCamelCase						    =					intermediate_size
      UpperCamelCase						    =					hidden_act
      UpperCamelCase						    =					hidden_dropout_prob
      UpperCamelCase						    =					attention_probs_dropout_prob
      UpperCamelCase						    =					attention_type
      UpperCamelCase						    =					initializer_range
      UpperCamelCase						    =					scope
      UpperCamelCase						    =					num_labels

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

 def      __lowerCAmelCase  (						self			:			int   ):






      """simple docstring"""



      UpperCamelCase						    =					floats_tensor(
          [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]   )

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

      UpperCamelCase						    =					self.get_config()

      return config, pixel_values, labels

 def      __lowerCAmelCase  (						self			:			Any   ):






      """simple docstring"""



      UpperCamelCase						    =					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					, )
      UpperCamelCase						    =					self.num_labels
      return config

 def      __lowerCAmelCase  (						self			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Optional[Any]					, SCREAMING_SNAKE_CASE__			:			Optional[int]					, SCREAMING_SNAKE_CASE__			:			Dict   ):






      """simple docstring"""



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

 def      __lowerCAmelCase  (						self			:			Optional[int]					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Optional[int]					, SCREAMING_SNAKE_CASE__			:			Tuple   ):






      """simple docstring"""



      UpperCamelCase						    =					TimesformerForVideoClassification(SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()

      UpperCamelCase						    =					model(SCREAMING_SNAKE_CASE__   )

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






 def      __lowerCAmelCase  (						self			:			List[str]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.prepare_config_and_inputs()
      UpperCamelCase    ,		UpperCamelCase    ,		UpperCamelCase						    =					config_and_inputs
      UpperCamelCase						    =					{'pixel_values': pixel_values}
      return config, inputs_dict




@require_torch
class       _lowerCAmelCase  (		__magic_name__			,	__magic_name__			,	unittest.TestCase							):

 """simple docstring"""
 SCREAMING_SNAKE_CASE_				:					Any       						=(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
 SCREAMING_SNAKE_CASE_				:					Optional[Any]       						=(
     {"feature-extraction": TimesformerModel, "video-classification": TimesformerForVideoClassification}
     if is_torch_available()
     else {}
 )

 SCREAMING_SNAKE_CASE_				:					List[Any]       						=False
 SCREAMING_SNAKE_CASE_				:					Any       						=False
 SCREAMING_SNAKE_CASE_				:					Dict       						=False
 SCREAMING_SNAKE_CASE_				:					Tuple       						=False

 def      __lowerCAmelCase  (						self			:			Tuple   ):






      """simple docstring"""



      UpperCamelCase						    =					TimesformerModelTester(self   )
      UpperCamelCase						    =					ConfigTester(
          self					, config_class=SCREAMING_SNAKE_CASE__					, has_text_modality=SCREAMING_SNAKE_CASE__					, hidden_size=37   )

 def      __lowerCAmelCase  (						self			:			Dict					, SCREAMING_SNAKE_CASE__			:			int					, SCREAMING_SNAKE_CASE__			:			Dict					, SCREAMING_SNAKE_CASE__			:			Optional[Any]=False   ):






      """simple docstring"""



      UpperCamelCase						    =					copy.deepcopy(SCREAMING_SNAKE_CASE__   )

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

      return inputs_dict

 def      __lowerCAmelCase  (						self			:			Tuple   ):






      """simple docstring"""



      self.config_tester.run_common_tests()

 @unittest.skip(reason='TimeSformer does not use inputs_embeds'   )
 def      __lowerCAmelCase  (						self			:			Optional[Any]   ):






      """simple docstring"""



      pass

 def      __lowerCAmelCase  (						self			:			List[str]   ):






      """simple docstring"""



      UpperCamelCase    ,		UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs_for_common()

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

 def      __lowerCAmelCase  (						self			:			int   ):






      """simple docstring"""



      UpperCamelCase    ,		UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs_for_common()

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

           UpperCamelCase						    =					['pixel_values']
           self.assertListEqual(arg_names[:1]					, SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			str   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			Tuple   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_video_classification(*SCREAMING_SNAKE_CASE__   )

 @slow
 def      __lowerCAmelCase  (						self			:			int   ):






      """simple docstring"""



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

 def      __lowerCAmelCase  (						self			:			str   ):






      """simple docstring"""



      if not self.has_attentions:
           pass

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

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

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

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

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

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

                UpperCamelCase						    =					outputs.attentions

                self.assertEqual(len(SCREAMING_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      __lowerCAmelCase  (						self			:			Any   ):






      """simple docstring"""



      def check_hidden_states_output(SCREAMING_SNAKE_CASE__			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Optional[int]					, SCREAMING_SNAKE_CASE__			:			Any   ):
           UpperCamelCase						    =					model_class(SCREAMING_SNAKE_CASE__   )
           model.to(SCREAMING_SNAKE_CASE__   )
           model.eval()

           with torch.no_grad():
                UpperCamelCase						    =					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__   )   )

           UpperCamelCase						    =					outputs.hidden_states
           UpperCamelCase						    =					self.model_tester.num_hidden_layers + 1
           self.assertEqual(len(SCREAMING_SNAKE_CASE__   )					, SCREAMING_SNAKE_CASE__   )

           UpperCamelCase						    =					self.model_tester.seq_length

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

      UpperCamelCase    ,		UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           UpperCamelCase						    =					True
           check_hidden_states_output(SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__   )

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

           check_hidden_states_output(SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__   )




def  __lowerCamelCase					( )       ->      Dict:
     UpperCamelCase						    =					hf_hub_download(
         repo_id='hf-internal-testing/spaghetti-video'						,     filename='eating_spaghetti.npy'						,     repo_type='dataset'			)
     UpperCamelCase						    =					np.load(_lowercase			)
     return list(_lowercase			)







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

 """simple docstring"""

 @cached_property
 def      __lowerCAmelCase  (						self			:			str   ):






      """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      __lowerCAmelCase  (						self			:			Optional[Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400'   ).to(
          SCREAMING_SNAKE_CASE__   )

      UpperCamelCase						    =					self.default_image_processor
      UpperCamelCase						    =					prepare_video()
      UpperCamelCase						    =					image_processor(video[:8]					, return_tensors='pt'   ).to(SCREAMING_SNAKE_CASE__   )

      # forward pass
      with torch.no_grad():
           UpperCamelCase						    =					model(**SCREAMING_SNAKE_CASE__   )

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

      UpperCamelCase						    =					torch.tensor([-0.3016, -0.7713, -0.4205]   ).to(SCREAMING_SNAKE_CASE__   )

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




from ...utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_torch_available,
    is_transformers_available,
)


try:
 if not (is_transformers_available() and is_torch_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
 from .multicontrolnet import MultiControlNetModel
 from .pipeline_controlnet import StableDiffusionControlNetPipeline
 from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline
 from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline


if is_transformers_available() and is_flax_available():
 from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline


