




'''simple docstring'''


import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
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,
        VideoMAEForPreTraining,
        VideoMAEForVideoClassification,
        VideoMAEModel,
    )
    from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from transformers import VideoMAEImageProcessor

class       A   :







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


              """simple docstring"""


              _a     					=    parent
              _a     					=    batch_size
              _a     					=    image_size
              _a     					=    num_channels
              _a     					=    patch_size
              _a     					=    tubelet_size
              _a     					=    num_frames
              _a     					=    is_training
              _a     					=    use_labels
              _a     					=    hidden_size
              _a     					=    num_hidden_layers
              _a     					=    num_attention_heads
              _a     					=    intermediate_size
              _a     					=    hidden_act
              _a     					=    hidden_dropout_prob
              _a     					=    attention_probs_dropout_prob
              _a     					=    type_sequence_label_size
              _a     					=    initializer_range
              _a     					=    mask_ratio
              _a     					=    scope

              # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
              _a     					=    (image_size // patch_size) ** 2
              _a     					=    (num_frames // tubelet_size) * self.num_patches_per_frame

              # use this variable to define bool_masked_pos
              _a     					=    int(mask_ratio * self.seq_length )







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


              """simple docstring"""


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

              _a     					=    None
              if self.use_labels:
                     _a     					=    ids_tensor([self.batch_size]							,      self.type_sequence_label_size )

              _a     					=    self.get_config()

              return config, pixel_values, labels







       def        __lowerCAmelCase      (  self	:  List[str] )      ->							Any:


              """simple docstring"""


              return VideoMAEConfig(
                  image_size=self.image_size							,      patch_size=self.patch_size							,      num_channels=self.num_channels							,      num_frames=self.num_frames							,      tubelet_size=self.tubelet_size							,      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							,      is_decoder=lowerCAmelCase_							,      initializer_range=self.initializer_range							,      )







       def        __lowerCAmelCase      (  self	:  List[str]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Union[str, Any] )      ->							Optional[int]:


              """simple docstring"""


              _a     					=    VideoMAEModel(config=lowerCAmelCase_ )
              model.to(lowerCAmelCase_ )
              model.eval()
              _a     					=    model(lowerCAmelCase_ )
              self.parent.assertEqual(result.last_hidden_state.shape							,      (self.batch_size, self.seq_length, self.hidden_size) )







       def        __lowerCAmelCase      (  self	:  Any							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  Optional[Any] )      ->							Optional[Any]:


              """simple docstring"""


              _a     					=    VideoMAEForPreTraining(lowerCAmelCase_ )
              model.to(lowerCAmelCase_ )
              model.eval()
              # important: each video needs to have the same number of masked patches
              # hence we define a single mask, which we then repeat for each example in the batch
              _a     					=    torch.ones((self.num_masks,) )
              _a     					=    torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] )
              _a     					=    mask.expand(self.batch_size							,      -1 ).bool()

              _a     					=    model(lowerCAmelCase_							,      lowerCAmelCase_ )
              # model only returns predictions for masked patches
              _a     					=    mask.sum().item()
              _a     					=    3 * self.tubelet_size * self.patch_size**2
              self.parent.assertEqual(result.logits.shape							,      (self.batch_size, num_masked_patches, decoder_num_labels) )







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


              """simple docstring"""


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



@require_torch
class       A   (		_a					,_a					,unittest.TestCase   ):
       lowercase_          =    (
           (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
       )
       lowercase_          =    (
           {'feature-extraction': VideoMAEModel, 'video-classification': VideoMAEForVideoClassification}
           if is_torch_available()
           else {}
       )

       lowercase_          =    False
       lowercase_          =    False
       lowercase_          =    False
       lowercase_          =    False







       def        __lowerCAmelCase      (  self	:  Dict )      ->							int:


              """simple docstring"""


              _a     					=    VideoMAEModelTester(self )
              _a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_							,      has_text_modality=lowerCAmelCase_							,      hidden_size=37 )







       def        __lowerCAmelCase      (  self	:  Optional[int]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  List[Any]=False )      ->							Tuple:


              """simple docstring"""


              _a     					=    copy.deepcopy(lowerCAmelCase_ )

              if model_class == VideoMAEForPreTraining:
                     # important: each video needs to have the same number of masked patches
                     # hence we define a single mask, which we then repeat for each example in the batch
                     _a     					=    torch.ones((self.model_tester.num_masks,) )
                     _a     					=    torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] )
                     _a     					=    mask.expand(self.model_tester.batch_size							,      -1 ).bool()
                     _a     					=    bool_masked_pos.to(lowerCAmelCase_ )

              if return_labels:
                     if model_class in [
                         *get_values(lowerCAmelCase_ ),
                     ]:
                            _a     					=    torch.zeros(
                                self.model_tester.batch_size							,      dtype=torch.long							,      device=lowerCAmelCase_ )

              return inputs_dict







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


              """simple docstring"""


              self.config_tester.run_common_tests()







       @unittest.skip(reason='''VideoMAE does not use inputs_embeds''' )
       def        __lowerCAmelCase      (  self	:  Any )      ->							Any:


              """simple docstring"""


              pass







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


              """simple docstring"""


              _a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

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







       def        __lowerCAmelCase      (  self	:  Any )      ->							Any:


              """simple docstring"""


              _a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

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

                     _a     					=    ['''pixel_values''']
                     self.assertListEqual(arg_names[:1]							,      lowerCAmelCase_ )







       def        __lowerCAmelCase      (  self	:  int )      ->							Any:


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*lowerCAmelCase_ )







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


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase_ )







       @slow
       def        __lowerCAmelCase      (  self	:  Dict )      ->							Optional[int]:


              """simple docstring"""


              for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     _a     					=    VideoMAEModel.from_pretrained(lowerCAmelCase_ )
                     self.assertIsNotNone(lowerCAmelCase_ )







       def        __lowerCAmelCase      (  self	:  int )      ->							Any:


              """simple docstring"""


              if not self.has_attentions:
                     pass

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

                     for model_class in self.all_model_classes:
                            _a     					=    self.model_tester.seq_length - self.model_tester.num_masks
                            _a     					=    (
                                num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                            )

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

                            # check that output_attentions also work using config
                            del inputs_dict["output_attentions"]
                            _a     					=    True
                            _a     					=    model_class(lowerCAmelCase_ )
                            model.to(lowerCAmelCase_ )
                            model.eval()
                            with torch.no_grad():
                                   _a     					=    model(**self._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_ ) )
                            _a     					=    outputs.attentions
                            self.assertEqual(len(lowerCAmelCase_ )							,      self.model_tester.num_hidden_layers )

                            self.assertListEqual(
                                list(attentions[0].shape[-3:] )							,      [self.model_tester.num_attention_heads, seq_len, seq_len]							,      )
                            _a     					=    len(lowerCAmelCase_ )

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

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

                            _a     					=    outputs.attentions

                            self.assertEqual(len(lowerCAmelCase_ )							,      self.model_tester.num_hidden_layers )
                            self.assertListEqual(
                                list(self_attentions[0].shape[-3:] )							,      [self.model_tester.num_attention_heads, seq_len, seq_len]							,      )







       def        __lowerCAmelCase      (  self	:  Tuple )      ->							Tuple:


              """simple docstring"""


              def check_hidden_states_output(lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  Tuple ):
                     _a     					=    model_class(lowerCAmelCase_ )
                     model.to(lowerCAmelCase_ )
                     model.eval()

                     with torch.no_grad():
                            _a     					=    model(**self._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_ ) )

                     _a     					=    outputs.hidden_states
                     _a     					=    self.model_tester.num_hidden_layers + 1
                     self.assertEqual(len(lowerCAmelCase_ )							,      lowerCAmelCase_ )

                     _a     					=    self.model_tester.seq_length - self.model_tester.num_masks
                     _a     					=    num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

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

              _a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     _a     					=    True
                     check_hidden_states_output(lowerCAmelCase_							,      lowerCAmelCase_							,      lowerCAmelCase_ )

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

                     check_hidden_states_output(lowerCAmelCase_							,      lowerCAmelCase_							,      lowerCAmelCase_ )







       @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' )
       def        __lowerCAmelCase      (  self	:  Optional[int] )      ->							Dict:


              """simple docstring"""


              pass



def 		snake_case_   ():





       '''simple docstring'''


       _a     					=    hf_hub_download(
           repo_id='''hf-internal-testing/spaghetti-video'''			,						filename='''eating_spaghetti.npy'''			,						repo_type='''dataset'''				)
       _a     					=    np.load(UpperCamelCase				)
       return list(UpperCamelCase				)

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







       @cached_property
       def        __lowerCAmelCase      (  self	:  str )      ->							List[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        __lowerCAmelCase      (  self	:  Dict )      ->							Dict:


              """simple docstring"""


              _a     					=    VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics''' ).to(
                  lowerCAmelCase_ )

              _a     					=    self.default_image_processor
              _a     					=    prepare_video()
              _a     					=    image_processor(lowerCAmelCase_							,      return_tensors='''pt''' ).to(lowerCAmelCase_ )

              # forward pass
              with torch.no_grad():
                     _a     					=    model(**lowerCAmelCase_ )

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

              _a     					=    torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ).to(lowerCAmelCase_ )

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







       @slow
       def        __lowerCAmelCase      (  self	:  Optional[Any] )      ->							Optional[Any]:


              """simple docstring"""


              _a     					=    VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' ).to(lowerCAmelCase_ )

              _a     					=    self.default_image_processor
              _a     					=    prepare_video()
              _a     					=    image_processor(lowerCAmelCase_							,      return_tensors='''pt''' ).to(lowerCAmelCase_ )

              # add boolean mask, indicating which patches to mask
              _a     					=    hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos'''							,      filename='''bool_masked_pos.pt''' )
              _a     					=    torch.load(lowerCAmelCase_ )

              # forward pass
              with torch.no_grad():
                     _a     					=    model(**lowerCAmelCase_ )

              # verify the logits
              _a     					=    torch.Size([1, 14_08, 15_36] )
              _a     					=    torch.tensor(
                  [[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]]							,      device=lowerCAmelCase_ )
              self.assertEqual(outputs.logits.shape							,      lowerCAmelCase_ )
              self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]							,      lowerCAmelCase_							,      atol=1e-4 ) )

              # verify the loss (`config.norm_pix_loss` = `True`)
              _a     					=    torch.tensor([0.5_1_4_2]							,      device=lowerCAmelCase_ )
              self.assertTrue(torch.allclose(outputs.loss							,      lowerCAmelCase_							,      atol=1e-4 ) )

              # verify the loss (`config.norm_pix_loss` = `False`)
              _a     					=    VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''							,      norm_pix_loss=lowerCAmelCase_ ).to(
                  lowerCAmelCase_ )

              with torch.no_grad():
                     _a     					=    model(**lowerCAmelCase_ )

              _a     					=    torch.tensor(torch.tensor([0.6_4_6_9] )							,      device=lowerCAmelCase_ )
              self.assertTrue(torch.allclose(outputs.loss							,      lowerCAmelCase_							,      atol=1e-4 ) )







'''simple docstring'''


def 		snake_case_   (UpperCamelCase	:  str			,						UpperCamelCase	:  Any				):





							'''simple docstring'''


							return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2
def 		snake_case_   (UpperCamelCase	:  Any			,						UpperCamelCase	:  str=0				):





							'''simple docstring'''


							return sorted(UpperCamelCase			,						key=lambda UpperCamelCase				: x[column]				)
def 		snake_case_   (UpperCamelCase	:  Optional[int]			,						UpperCamelCase	:  Any			,						UpperCamelCase	:  Union[str, Any]=float('''inf'''				)				):





							'''simple docstring'''


							for i in range(points_counts - 1				):
														for j in range(i + 1			,						UpperCamelCase				):
																					_a     					=    euclidean_distance_sqr(points[i]			,						points[j]				)
																					if current_dis < min_dis:
																												_a     					=    current_dis
							return min_dis
def 		snake_case_   (UpperCamelCase	:  int			,						UpperCamelCase	:  Tuple			,						UpperCamelCase	:  List[str]=float('''inf'''				)				):





							'''simple docstring'''


							for i in range(min(6			,						points_counts - 1				)			,						UpperCamelCase				):
														for j in range(max(0			,						i - 6				)			,						UpperCamelCase				):
																					_a     					=    euclidean_distance_sqr(points[i]			,						points[j]				)
																					if current_dis < min_dis:
																												_a     					=    current_dis
							return min_dis
def 		snake_case_   (UpperCamelCase	:  int			,						UpperCamelCase	:  List[Any]			,						UpperCamelCase	:  int				):





							'''simple docstring'''


							if points_counts <= 3:
														return dis_between_closest_pair(UpperCamelCase			,						UpperCamelCase				)

							# recursion
							_a     					=    points_counts // 2
							_a     					=    closest_pair_of_points_sqr(
							    UpperCamelCase			,						points_sorted_on_y[:mid]			,						UpperCamelCase				)
							_a     					=    closest_pair_of_points_sqr(
							    UpperCamelCase			,						points_sorted_on_y[mid:]			,						points_counts - mid				)
							_a     					=    min(UpperCamelCase			,						UpperCamelCase				)
							_a     					=    []
							for point in points_sorted_on_x:
														if abs(point[0] - points_sorted_on_x[mid][0]				) < closest_pair_dis:
																					cross_strip.append(UpperCamelCase				)

							_a     					=    dis_between_closest_in_strip(
							    UpperCamelCase			,						len(UpperCamelCase				)			,						UpperCamelCase				)
							return min(UpperCamelCase			,						UpperCamelCase				)
def 		snake_case_   (UpperCamelCase	:  Optional[int]			,						UpperCamelCase	:  List[str]				):





							'''simple docstring'''


							_a     					=    column_based_sort(UpperCamelCase			,						column=0				)
							_a     					=    column_based_sort(UpperCamelCase			,						column=1				)
							return (
							    closest_pair_of_points_sqr(
							        UpperCamelCase			,						UpperCamelCase			,						UpperCamelCase				)
							) ** 0.5


if __name__ == "__main__":
				_snake_case				:				int       							=		[(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)]
				print('Distance:', closest_pair_of_points(points, len(points)))

