





'''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 				a_       :





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





        """simple docstring"""




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

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





     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        lowercase_      :Dict         =			floats_tensor(
            [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]						)

        lowercase_      :Union[str, Any]         =			None
        if self.use_labels:
           lowercase_      :Optional[int]         =			ids_tensor([self.batch_size]     ,				self.num_labels						)

        lowercase_      :Any         =			self.get_config()

        return config, pixel_values, labels





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




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





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: int     ,				lowercase		: List[Any]     ,				lowercase		: str						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			TimesformerModel(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_      :List[Any]         =			model(lowercase						)
        self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.seq_length, self.hidden_size)						)





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: str     ,				lowercase		: int     ,				lowercase		: List[str]						):





        """simple docstring"""




        lowercase_      :Any         =			TimesformerForVideoClassification(lowercase						)
        model.to(lowercase						)
        model.eval()

        lowercase_      :int         =			model(lowercase						)

        # verify the logits shape
        lowercase_      :Any         =			torch.Size((self.batch_size, self.num_labels)						)
        self.parent.assertEqual(result.logits.shape     ,				lowercase						)







     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        lowercase_      :List[Any]         =			self.prepare_config_and_inputs()
        lowercase_      ,							lowercase_      ,							lowercase_      :List[Any]         =			config_and_inputs
        lowercase_      :Union[str, Any]         =			{"pixel_values": pixel_values}
        return config, inputs_dict







@require_torch
class 				a_       (    _lowerCAmelCase     ,						_lowerCAmelCase     ,						unittest.TestCase							):
     __A								=      (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
     __A								=      (
         {"feature-extraction": TimesformerModel, "video-classification": TimesformerForVideoClassification}
         if is_torch_available()
         else {}
     )

     __A								=      False
     __A								=      False
     __A								=      False
     __A								=      False





     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :Tuple         =			TimesformerModelTester(self						)
        lowercase_      :Any         =			ConfigTester(
            self     ,				config_class=lowercase     ,				has_text_modality=lowercase     ,				hidden_size=37						)





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: str     ,				lowercase		: Optional[Any]     ,				lowercase		: Union[str, Any]=False						):





        """simple docstring"""




        lowercase_      :Tuple         =			copy.deepcopy(lowercase						)

        if return_labels:
           if model_class in get_values(lowercase						):
              lowercase_      :Optional[Any]         =			torch.zeros(
                  self.model_tester.batch_size     ,				dtype=torch.long     ,				device=lowercase						)

        return inputs_dict





     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     @unittest.skip(reason="TimeSformer does not use inputs_embeds"						)
     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        pass





     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        lowercase_      ,							lowercase_      :Tuple         =			self.model_tester.prepare_config_and_inputs_for_common()

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





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      ,							lowercase_      :Any         =			self.model_tester.prepare_config_and_inputs_for_common()

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

           lowercase_      :str         =			["pixel_values"]
           self.assertListEqual(arg_names[:1]     ,				lowercase						)





     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :str         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase						)





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :List[str]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_video_classification(*lowercase						)





     @slow
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase_      :Dict         =			TimesformerModel.from_pretrained(lowercase						)
           self.assertIsNotNone(lowercase						)





     def 							lowercase__				(			self		: Optional[Any]						):





        """simple docstring"""




        if not self.has_attentions:
           pass

        else:
           lowercase_      ,							lowercase_      :Union[str, Any]         =			self.model_tester.prepare_config_and_inputs_for_common()
           lowercase_      :str         =			True

           for model_class in self.all_model_classes:
              lowercase_      :Optional[int]         =			self.model_tester.seq_length
              lowercase_      :List[str]         =			self.model_tester.num_frames

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

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

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

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

              lowercase_      :List[str]         =			outputs.attentions

              self.assertEqual(len(lowercase						)     ,				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 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        def check_hidden_states_output(lowercase		: Tuple     ,				lowercase		: str     ,				lowercase		: List[Any]						):
           lowercase_      :Optional[Any]         =			model_class(lowercase						)
           model.to(lowercase						)
           model.eval()

           with torch.no_grad():
              lowercase_      :Tuple         =			model(**self._prepare_for_class(lowercase     ,				lowercase						)						)

           lowercase_      :Dict         =			outputs.hidden_states
           lowercase_      :Optional[Any]         =			self.model_tester.num_hidden_layers + 1
           self.assertEqual(len(lowercase						)     ,				lowercase						)

           lowercase_      :Optional[int]         =			self.model_tester.seq_length

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

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

        for model_class in self.all_model_classes:
           lowercase_      :Union[str, Any]         =			True
           check_hidden_states_output(lowercase     ,				lowercase     ,				lowercase						)

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

           check_hidden_states_output(lowercase     ,				lowercase     ,				lowercase						)







def 							UpperCAmelCase_  (						):
   lowercase_      :Optional[int]         =			hf_hub_download(
       repo_id="hf-internal-testing/spaghetti-video"     ,filename="eating_spaghetti.npy"     ,repo_type="dataset"			)
   lowercase_      :str         =			np.load(__lowerCamelCase			)
   return list(__lowerCamelCase			)


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





     @cached_property
     def 							lowercase__				(			self		: 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 							lowercase__				(			self		: str						):





        """simple docstring"""




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

        lowercase_      :Union[str, Any]         =			self.default_image_processor
        lowercase_      :Any         =			prepare_video()
        lowercase_      :Any         =			image_processor(video[:8]     ,				return_tensors="pt"						).to(lowercase						)

        # forward pass
        with torch.no_grad():
           lowercase_      :List[Any]         =			model(**lowercase						)

        # verify the logits
        lowercase_      :Any         =			torch.Size((1, 400)						)
        self.assertEqual(outputs.logits.shape     ,				lowercase						)

        lowercase_      :Optional[int]         =			torch.tensor([-0.30_16, -0.77_13, -0.42_05]						).to(lowercase						)

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









'''simple docstring'''


def 							UpperCAmelCase_  (						__lowerCamelCase : str     ,__lowerCamelCase : str			):
   lowercase_      :Optional[Any]         =			len(__lowerCamelCase			)
   lowercase_      :List[Any]         =			[]
   for i in range(len(__lowerCamelCase			) - pat_len + 1			):
      lowercase_      :Dict         =			True
      for j in range(__lowerCamelCase			):
         if s[i + j] != pattern[j]:
            lowercase_      :int         =			False
            break
      if match_found:
         position.append(__lowerCamelCase			)
   return position


if __name__ == "__main__":
 assert naive_pattern_search('''ABCDEFG''', '''DE''') == [3]
 print(naive_pattern_search('''ABAAABCDBBABCDDEBCABC''', '''ABC'''))


