
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						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=1_3						,						__SCREAMING_SNAKE_CASE=1_0						,						__SCREAMING_SNAKE_CASE=3						,						__SCREAMING_SNAKE_CASE=2						,						__SCREAMING_SNAKE_CASE=2						,						__SCREAMING_SNAKE_CASE=True						,						__SCREAMING_SNAKE_CASE=True						,						__SCREAMING_SNAKE_CASE=3_2						,						__SCREAMING_SNAKE_CASE=5						,						__SCREAMING_SNAKE_CASE=4						,						__SCREAMING_SNAKE_CASE=3_7						,						__SCREAMING_SNAKE_CASE="gelu"						,						__SCREAMING_SNAKE_CASE=0.1						,						__SCREAMING_SNAKE_CASE=0.1						,						__SCREAMING_SNAKE_CASE=1_0						,						__SCREAMING_SNAKE_CASE=0.02						,						__SCREAMING_SNAKE_CASE="divided_space_time"						,						__SCREAMING_SNAKE_CASE=None						,						)  ->   Dict:




           """simple docstring"""







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

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






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Any:




           """simple docstring"""







           UpperCamelCase__    :   List[str]					=				floats_tensor(
               [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]  )

           UpperCamelCase__    :   Any					=				None
           if self.use_labels:
                 UpperCamelCase__    :   List[Any]					=				ids_tensor([self.batch_size]						,						self.num_labels  )

           UpperCamelCase__    :   Optional[Any]					=				self.get_config()

           return config, pixel_values, labels






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   int:




           """simple docstring"""







           UpperCamelCase__    :   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						,						)
           UpperCamelCase__    :   List[str]					=				self.num_labels
           return config






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   str:




           """simple docstring"""







           UpperCamelCase__    :   List[str]					=				TimesformerModel(config=_A  )
           model.to(_A  )
           model.eval()
           UpperCamelCase__    :   int					=				model(_A  )
           self.parent.assertEqual(result.last_hidden_state.shape						,						(self.batch_size, self.seq_length, self.hidden_size)  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   Optional[Any]:




           """simple docstring"""







           UpperCamelCase__    :   Dict					=				TimesformerForVideoClassification(_A  )
           model.to(_A  )
           model.eval()

           UpperCamelCase__    :   Optional[Any]					=				model(_A  )

           # verify the logits shape
           UpperCamelCase__    :   List[Any]					=				torch.Size((self.batch_size, self.num_labels)  )
           self.parent.assertEqual(result.logits.shape						,						_A  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   int:




           """simple docstring"""







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




@require_torch
class 				_lowerCamelCase (      a__	,							a__	,							unittest.TestCase ):





     """simple docstring"""







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

     SCREAMING_SNAKE_CASE_		   =  False
     SCREAMING_SNAKE_CASE_		   =  False
     SCREAMING_SNAKE_CASE_		   =  False
     SCREAMING_SNAKE_CASE_		   =  False






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




           """simple docstring"""







           UpperCamelCase__    :   List[str]					=				TimesformerModelTester(self  )
           UpperCamelCase__    :   Optional[int]					=				ConfigTester(
               self						,						config_class=_A						,						has_text_modality=_A						,						hidden_size=3_7  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=False  )  ->   int:




           """simple docstring"""







           UpperCamelCase__    :   Union[str, Any]					=				copy.deepcopy(_A  )

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

           return inputs_dict






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Dict:




           """simple docstring"""







           self.config_tester.run_common_tests()






     @unittest.skip(reason='''TimeSformer does not use inputs_embeds'''  )
     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Tuple:




           """simple docstring"""







           pass






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Optional[int]:




           """simple docstring"""







           UpperCamelCase__    :   Any					=				self.model_tester.prepare_config_and_inputs_for_common()

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






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[Any]:




           """simple docstring"""







           UpperCamelCase__    :   Dict					=				self.model_tester.prepare_config_and_inputs_for_common()

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

                 UpperCamelCase__    :   Any					=				['pixel_values']
                 self.assertListEqual(arg_names[:1]						,						_A  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[Any]:




           """simple docstring"""







           UpperCamelCase__    :   List[str]					=				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*_A  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Tuple:




           """simple docstring"""







           UpperCamelCase__    :   Optional[int]					=				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_video_classification(*_A  )






     @slow
     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   int:




           """simple docstring"""







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






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




           """simple docstring"""







           if not self.has_attentions:
                 pass

           else:
                 UpperCamelCase__    :   Dict					=				self.model_tester.prepare_config_and_inputs_for_common()
                 UpperCamelCase__    :   List[str]					=				True

                 for model_class in self.all_model_classes:
                       UpperCamelCase__    :   List[str]					=				self.model_tester.seq_length
                       UpperCamelCase__    :   Union[str, Any]					=				self.model_tester.num_frames

                       UpperCamelCase__    :   Optional[int]					=				True
                       UpperCamelCase__    :   str					=				False
                       UpperCamelCase__    :   Union[str, Any]					=				True
                       UpperCamelCase__    :   List[str]					=				model_class(_A  )
                       model.to(_A  )
                       model.eval()
                       with torch.no_grad():
                             UpperCamelCase__    :   int					=				model(**self._prepare_for_class(_A						,						_A  )  )
                       UpperCamelCase__    :   Dict					=				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"]
                       UpperCamelCase__    :   List[Any]					=				True
                       UpperCamelCase__    :   str					=				model_class(_A  )
                       model.to(_A  )
                       model.eval()
                       with torch.no_grad():
                             UpperCamelCase__    :   int					=				model(**self._prepare_for_class(_A						,						_A  )  )
                       UpperCamelCase__    :   str					=				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]						,						)
                       UpperCamelCase__    :   Optional[Any]					=				len(_A  )

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

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

                       UpperCamelCase__    :   Tuple					=				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    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[str]:




           """simple docstring"""







           def check_hidden_states_output(__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  ):
                 UpperCamelCase__    :   Union[str, Any]					=				model_class(_A  )
                 model.to(_A  )
                 model.eval()

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

                 UpperCamelCase__    :   List[Any]					=				outputs.hidden_states
                 UpperCamelCase__    :   int					=				self.model_tester.num_hidden_layers + 1
                 self.assertEqual(len(_A  )						,						_A  )

                 UpperCamelCase__    :   Any					=				self.model_tester.seq_length

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

           UpperCamelCase__    :   int					=				self.model_tester.prepare_config_and_inputs_for_common()

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

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

                 check_hidden_states_output(_A						,						_A						,						_A  )


def 							SCREAMING_SNAKE_CASE_      (			):
      UpperCamelCase__    :   Union[str, Any]					=				hf_hub_download(
          repo_id='''hf-internal-testing/spaghetti-video''' ,		filename='''eating_spaghetti.npy''' ,		repo_type='''dataset'''		)
      UpperCamelCase__    :   Any					=				np.load(_lowerCamelCase		)
      return list(_lowerCamelCase		)




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





     """simple docstring"""







     @cached_property
     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[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    __SCREAMING_SNAKE_CASE			(							self  )  ->   Tuple:




           """simple docstring"""







           UpperCamelCase__    :   Dict					=				TimesformerForVideoClassification.from_pretrained('''facebook/timesformer-base-finetuned-k400'''  ).to(
               _A  )

           UpperCamelCase__    :   List[Any]					=				self.default_image_processor
           UpperCamelCase__    :   Tuple					=				prepare_video()
           UpperCamelCase__    :   int					=				image_processor(video[:8]						,						return_tensors='''pt'''  ).to(_A  )

           # forward pass
           with torch.no_grad():
                 UpperCamelCase__    :   Optional[int]					=				model(**_A  )

           # verify the logits
           UpperCamelCase__    :   Optional[int]					=				torch.Size((1, 4_0_0)  )
           self.assertEqual(outputs.logits.shape						,						_A  )

           UpperCamelCase__    :   Optional[Any]					=				torch.tensor([-0.3016, -0.7713, -0.4205]  ).to(_A  )

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







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCamelCase      ={
    "configuration_clipseg": [
        "CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "CLIPSegConfig",
        "CLIPSegTextConfig",
        "CLIPSegVisionConfig",
    ],
    "processing_clipseg": ["CLIPSegProcessor"],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCamelCase      =[
        "CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST",
        "CLIPSegModel",
        "CLIPSegPreTrainedModel",
        "CLIPSegTextModel",
        "CLIPSegVisionModel",
        "CLIPSegForImageSegmentation",
    ]

if TYPE_CHECKING:
    from .configuration_clipseg import (
        CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP,
        CLIPSegConfig,
        CLIPSegTextConfig,
        CLIPSegVisionConfig,
    )
    from .processing_clipseg import CLIPSegProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_clipseg import (
            CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST,
            CLIPSegForImageSegmentation,
            CLIPSegModel,
            CLIPSegPreTrainedModel,
            CLIPSegTextModel,
            CLIPSegVisionModel,
        )

else:
    import sys

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


