



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				,			__a				,			__a=13				,			__a=10				,			__a=3				,			__a=2				,			__a=2				,			__a=True				,			__a=True				,			__a=32				,			__a=5				,			__a=4				,			__a=37				,			__a="gelu"				,			__a=0.1				,			__a=0.1				,			__a=10				,			__a=0.02				,			__a="divided_space_time"				,			__a=None				,			):






           '''simple docstring'''



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

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







     def 						_a      (self    ):






           '''simple docstring'''



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

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

           lowerCamelCase								=		self.get_config()

           return config, pixel_values, labels







     def 						_a      (self    ):






           '''simple docstring'''



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







     def 						_a      (self				,			__a				,			__a				,			__a    ):






           '''simple docstring'''



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







     def 						_a      (self				,			__a				,			__a				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		TimesformerForVideoClassification(__a    )
           model.to(__a    )
           model.eval()

           lowerCamelCase								=		model(__a    )

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







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.prepare_config_and_inputs()
           lowerCamelCase			,							lowerCamelCase			,							lowerCamelCase								=		config_and_inputs
           lowerCamelCase								=		{"pixel_values": pixel_values}
           return config, inputs_dict





@require_torch
class    lowerCamelCase__  (						UpperCAmelCase_       ,    UpperCAmelCase_       ,    unittest.TestCase):
     """simple docstring"""



     _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 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		TimesformerModelTester(self    )
           lowerCamelCase								=		ConfigTester(
               self				,			config_class=__a				,			has_text_modality=__a				,			hidden_size=37    )







     def 						_a      (self				,			__a				,			__a				,			__a=False    ):






           '''simple docstring'''



           lowerCamelCase								=		copy.deepcopy(__a    )

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

           return inputs_dict







     def 						_a      (self    ):






           '''simple docstring'''



           self.config_tester.run_common_tests()







     @unittest.skip(reason="TimeSformer does not use inputs_embeds"    )
     def 						_a      (self    ):






           '''simple docstring'''



           pass







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.model_tester.prepare_config_and_inputs_for_common()

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







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowerCamelCase								=		["pixel_values"]
                 self.assertListEqual(arg_names[:1]				,			__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_video_classification(*__a    )







     @slow
     def 						_a      (self    ):






           '''simple docstring'''



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







     def 						_a      (self    ):






           '''simple docstring'''



           if not self.has_attentions:
                 pass

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

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

                       lowerCamelCase								=		True
                       lowerCamelCase								=		False
                       lowerCamelCase								=		True
                       lowerCamelCase								=		model_class(__a    )
                       model.to(__a    )
                       model.eval()
                       with torch.no_grad():
                             lowerCamelCase								=		model(**self._prepare_for_class(__a				,			__a    )    )
                       lowerCamelCase								=		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"]
                       lowerCamelCase								=		True
                       lowerCamelCase								=		model_class(__a    )
                       model.to(__a    )
                       model.eval()
                       with torch.no_grad():
                             lowerCamelCase								=		model(**self._prepare_for_class(__a				,			__a    )    )
                       lowerCamelCase								=		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]				,			)
                       lowerCamelCase								=		len(__a    )

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

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

                       lowerCamelCase								=		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    ):






           '''simple docstring'''



           def check_hidden_states_output(__a				,			__a				,			__a    ):
                 lowerCamelCase								=		model_class(__a    )
                 model.to(__a    )
                 model.eval()

                 with torch.no_grad():
                       lowerCamelCase								=		model(**self._prepare_for_class(__a				,			__a    )    )

                 lowerCamelCase								=		outputs.hidden_states
                 lowerCamelCase								=		self.model_tester.num_hidden_layers + 1
                 self.assertEqual(len(__a    )				,			__a    )

                 lowerCamelCase								=		self.model_tester.seq_length

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

           lowerCamelCase			,							lowerCamelCase								=		self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 lowerCamelCase								=		True
                 check_hidden_states_output(__a				,			__a				,			__a    )

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

                 check_hidden_states_output(__a				,			__a				,			__a    )



def     __lowercase(	):


      """simple docstring"""



      lowerCamelCase								=		hf_hub_download(
          repo_id="hf-internal-testing/spaghetti-video"   ,   filename="eating_spaghetti.npy"   ,   repo_type="dataset" )
      lowerCamelCase								=		np.load(UpperCAmelCase__ )
      return list(UpperCAmelCase__ )






@require_torch
@require_vision
class    lowerCamelCase__  (						unittest.TestCase):
     """simple docstring"""



     @cached_property
     def 						_a      (self    ):






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






           '''simple docstring'''



           lowerCamelCase								=		TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"    ).to(
               __a    )

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

           # forward pass
           with torch.no_grad():
                 lowerCamelCase								=		model(**__a    )

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

           lowerCamelCase								=		torch.tensor([-0.3016, -0.7713, -0.4205]    ).to(__a    )

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




from __future__ import annotations

from collections.abc import Iterable, Iterator
from dataclasses import dataclass

a_						:     Optional[int]	     =		(3, 9, -1_1, 0, 7, 5, 1, -1)
a_						:     str	     =		(4, 6, 2, 0, 8, 1_0, 3, -2)





@dataclass
class    lowerCamelCase__  :
     """simple docstring"""



     _A       =			42
     _A       =			42





class    lowerCamelCase__  :
     """simple docstring"""



     def __init__(self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		None
           for i in sorted(__a				,			reverse=__a    ):
                 lowerCamelCase								=		Node(__a				,			self.head    )







     def __iter__(self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.head
           while node:
                 yield node.data
                 lowerCamelCase								=		node.next_node







     def __len__(self    ):






           '''simple docstring'''



           return sum(1 for _ in self    )







     def __str__(self    ):






           '''simple docstring'''



           return " -> ".join([str(__a    ) for node in self]    )



def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__ ):


      """simple docstring"""



      return SortedLinkedList(list(UpperCAmelCase__ ) + list(UpperCAmelCase__ ) )


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     a_						:     Any	     =		SortedLinkedList
     print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))