"""simple docstring"""





import re
import time
from typing import Optional

import IPython.display as disp

from ..trainer_callback import TrainerCallback
from ..trainer_utils import IntervalStrategy, has_length





def        a_  (   _lowerCAmelCase						:				Tuple  ):





       '''simple docstring'''

       lowercase__    :	Union[str, Any]     					=			int(_lowerCAmelCase  )
       lowercase__					,   lowercase__					,   lowercase__    :	Tuple     					=			t // 3600, (t // 60) % 60, t % 60
       return f"""{h}:{m:02d}:{s:02d}""" if h != 0 else f"""{m:02d}:{s:02d}"""





def        a_  (   _lowerCAmelCase						:				Any     ,				_lowerCAmelCase						:				Optional[int]     ,				_lowerCAmelCase						:				Optional[Any]     ,				_lowerCAmelCase						:				Union[str, Any]     ,				_lowerCAmelCase						:				Optional[Any]=300  ):





       '''simple docstring'''

       return f"""
    <div>
      {prefix}
      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>
      {label}
    </div>
    """





def        a_  (   _lowerCAmelCase						:				int  ):





       '''simple docstring'''

       lowercase__    :	List[Any]     					=			'<table border="1" class="dataframe">\n'
       html_code += """  <thead>\n <tr style="text-align: left;">\n"""
       for i in items[0]:
              html_code += f"""      <th>{i}</th>\n"""
       html_code += "    </tr>\n  </thead>\n  <tbody>\n"
       for line in items[1:]:
              html_code += "    <tr>\n"
              for elt in line:
                     lowercase__    :	Dict     					=			f"""{elt:.6f}""" if isinstance(_lowerCAmelCase     ,				_lowerCAmelCase  ) else str(_lowerCAmelCase  )
                     html_code += f"""      <td>{elt}</td>\n"""
              html_code += "    </tr>\n"
       html_code += "  </tbody>\n</table><p>"
       return html_code


class   UpperCAmelCase_			:
      lowerCamelCase__	:					Tuple										=						5
      lowerCamelCase__	:					Optional[Any]										=						0.2


      def __init__(     self	,    a	,    a = None	,    a = True	,    a = None	,    a = 3_0_0	,    ) ->      Union[str, Any]:
             lowercase__    :	Optional[int]     					=			total
             lowercase__    :	Union[str, Any]     					=			'' if prefix is None else prefix
             lowercase__    :	Optional[int]     					=			leave
             lowercase__    :	Union[str, Any]     					=			parent
             lowercase__    :	int     					=			width
             lowercase__    :	Optional[Any]     					=			None
             lowercase__    :	Dict     					=			None
             lowercase__    :	List[Any]     					=			None


      def 	_UpperCAmelCase (     self	,    a	,    a = False	,    a = None							) ->      Optional[int]:
             lowercase__    :	List[Any]     					=			value
             if comment is not None:
                    lowercase__    :	Optional[int]     					=			comment
             if self.last_value is None:
                    lowercase__    :	Dict     					=			time.time()
                    lowercase__    :	Optional[Any]     					=			value
                    lowercase__    :	Any     					=			None
                    lowercase__    :	Union[str, Any]     					=			self.warmup
                    lowercase__    :	List[str]     					=			1
                    self.update_bar(a							)
             elif value <= self.last_value and not force_update:
                    return
             elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for	,    self.total							):
                    if self.first_calls > 0:
                           self.first_calls -= 1
                    lowercase__    :	Union[str, Any]     					=			time.time()
                    lowercase__    :	Any     					=			current_time - self.start_time
                    # We could have value = self.start_value if the update is called twixe with the same start value.
                    if value > self.start_value:
                           lowercase__    :	Optional[int]     					=			self.elapsed_time / (value - self.start_value)
                    else:
                           lowercase__    :	Tuple     					=			None
                    if value >= self.total:
                           lowercase__    :	str     					=			self.total
                           lowercase__    :	int     					=			None
                           if not self.leave:
                                  self.close()
                    elif self.average_time_per_item is not None:
                           lowercase__    :	Tuple     					=			self.average_time_per_item * (self.total - value)
                    self.update_bar(a							)
                    lowercase__    :	Any     					=			value
                    lowercase__    :	str     					=			current_time
                    if self.average_time_per_item is None:
                           lowercase__    :	List[str]     					=			1
                    else:
                           lowercase__    :	Optional[Any]     					=			max(int(self.update_every / self.average_time_per_item							)	,    1							)


      def 	_UpperCAmelCase (     self	,    a	,    a=None							) ->      Optional[int]:
             lowercase__    :	Optional[Any]     					=			' ' * (len(str(self.total							)							) - len(str(a							)							)) + str(a							)
             if self.elapsed_time is None:
                    lowercase__    :	Optional[int]     					=			f"""[{spaced_value}/{self.total} : < :"""
             elif self.predicted_remaining is None:
                    lowercase__    :	Optional[Any]     					=			f"""[{spaced_value}/{self.total} {format_time(self.elapsed_time							)}"""
             else:
                    lowercase__    :	Optional[int]     					=			(
                        f"""[{spaced_value}/{self.total} {format_time(self.elapsed_time							)} <"""
                        f""" {format_time(self.predicted_remaining							)}"""
                    )
                    self.label += f""", {1/self.average_time_per_item:.2f} it/s"""
             self.label += "]" if self.comment is None or len(self.comment							) == 0 else f""", {self.comment}]"""
             self.display()


      def 	_UpperCAmelCase (     self							) ->      Tuple:
             lowercase__    :	List[str]     					=			html_progress_bar(self.value	,    self.total	,    self.prefix	,    self.label	,    self.width							)
             if self.parent is not None:
                    # If this is a child bar, the parent will take care of the display.
                    self.parent.display()
                    return
             if self.output is None:
                    lowercase__    :	Union[str, Any]     					=			disp.display(disp.HTML(self.html_code							)	,    display_id=a							)
             else:
                    self.output.update(disp.HTML(self.html_code							)							)


      def 	_UpperCAmelCase (     self							) ->      Union[str, Any]:
             if self.parent is None and self.output is not None:
                    self.output.update(disp.HTML(''							)							)





class   UpperCAmelCase_			(					_a):


      def __init__(     self	,    a	,    a=None							) ->      Tuple:
             super().__init__(a							)
             lowercase__    :	Union[str, Any]     					=			None if column_names is None else [column_names]
             lowercase__    :	Optional[int]     					=			None


      def 	_UpperCAmelCase (     self							) ->      int:
             lowercase__    :	Any     					=			html_progress_bar(self.value	,    self.total	,    self.prefix	,    self.label	,    self.width							)
             if self.inner_table is not None:
                    self.html_code += text_to_html_table(self.inner_table							)
             if self.child_bar is not None:
                    self.html_code += self.child_bar.html_code
             if self.output is None:
                    lowercase__    :	str     					=			disp.display(disp.HTML(self.html_code							)	,    display_id=a							)
             else:
                    self.output.update(disp.HTML(self.html_code							)							)


      def 	_UpperCAmelCase (     self	,    a							) ->      str:
             if self.inner_table is None:
                    lowercase__    :	Optional[int]     					=			[list(values.keys()							), list(values.values()							)]
             else:
                    lowercase__    :	List[str]     					=			self.inner_table[0]
                    if len(self.inner_table							) == 1:
                           # We give a chance to update the column names at the first iteration
                           for key in values.keys():
                                  if key not in columns:
                                         columns.append(a							)
                           lowercase__    :	List[str]     					=			columns
                    self.inner_table.append([values[c] for c in columns]							)


      def 	_UpperCAmelCase (     self	,    a	,    a=None	,    a=3_0_0							) ->      Union[str, Any]:
             lowercase__    :	Any     					=			NotebookProgressBar(a	,    prefix=a	,    parent=self	,    width=a							)
             return self.child_bar


      def 	_UpperCAmelCase (     self							) ->      Optional[Any]:
             lowercase__    :	str     					=			None
             self.display()





class   UpperCAmelCase_			(					_a):


      def __init__(     self							) ->      Any:
             lowercase__    :	Tuple     					=			None
             lowercase__    :	Any     					=			None
             lowercase__    :	List[Any]     					=			False


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a	,    **a							) ->      List[str]:
             lowercase__    :	Tuple     					=			'Epoch' if args.evaluation_strategy == IntervalStrategy.EPOCH else 'Step'
             lowercase__    :	int     					=			0
             lowercase__    :	Tuple     					=			0
             lowercase__    :	Tuple     					=			[self.first_column] + ['Training Loss']
             if args.evaluation_strategy != IntervalStrategy.NO:
                    column_names.append('Validation Loss'							)
             lowercase__    :	Optional[int]     					=			NotebookTrainingTracker(state.max_steps	,    a							)


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a	,    **a							) ->      Optional[int]:
             lowercase__    :	int     					=			int(state.epoch							) if int(state.epoch							) == state.epoch else f"""{state.epoch:.2f}"""
             self.training_tracker.update(
                 state.global_step + 1	,    comment=f"""Epoch {epoch}/{state.num_train_epochs}"""	,    force_update=self._force_next_update	,    )
             lowercase__    :	int     					=			False


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a	,    a=None	,    **a							) ->      Optional[Any]:
             if not has_length(a							):
                    return
             if self.prediction_bar is None:
                    if self.training_tracker is not None:
                           lowercase__    :	List[Any]     					=			self.training_tracker.add_child(len(a							)							)
                    else:
                           lowercase__    :	Any     					=			NotebookProgressBar(len(a							)							)
                    self.prediction_bar.update(1							)
             else:
                    self.prediction_bar.update(self.prediction_bar.value + 1							)


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a	,    **a							) ->      Tuple:
             if self.prediction_bar is not None:
                    self.prediction_bar.close()
             lowercase__    :	str     					=			None


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a	,    a=None	,    **a							) ->      List[Any]:
             # Only for when there is no evaluation
             if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
                    lowercase__    :	Any     					=			{'Training Loss': logs['loss']}
                    # First column is necessarily Step sine we're not in epoch eval strategy
                    lowercase__    :	Optional[Any]     					=			state.global_step
                    self.training_tracker.write_line(a							)


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a	,    a=None	,    **a							) ->      List[str]:
             if self.training_tracker is not None:
                    lowercase__    :	List[Any]     					=			{'Training Loss': 'No log', 'Validation Loss': 'No log'}
                    for log in reversed(state.log_history							):
                           if "loss" in log:
                                  lowercase__    :	str     					=			log['loss']
                                  break

                    if self.first_column == "Epoch":
                           lowercase__    :	Union[str, Any]     					=			int(state.epoch							)
                    else:
                           lowercase__    :	str     					=			state.global_step
                    lowercase__    :	Optional[int]     					=			'eval'
                    for k in metrics:
                           if k.endswith('_loss'							):
                                  lowercase__    :	Dict     					=			re.sub(R'\_loss$'	,    ''	,    a							)
                    lowercase__    :	Dict     					=			metrics.pop('total_flos'	,    a							)
                    lowercase__    :	str     					=			metrics.pop('epoch'	,    a							)
                    lowercase__    :	Any     					=			metrics.pop(f"""{metric_key_prefix}_runtime"""	,    a							)
                    lowercase__    :	Optional[int]     					=			metrics.pop(f"""{metric_key_prefix}_samples_per_second"""	,    a							)
                    lowercase__    :	Tuple     					=			metrics.pop(f"""{metric_key_prefix}_steps_per_second"""	,    a							)
                    lowercase__    :	Optional[Any]     					=			metrics.pop(f"""{metric_key_prefix}_jit_compilation_time"""	,    a							)
                    for k, v in metrics.items():
                           if k == f"""{metric_key_prefix}_loss""":
                                  lowercase__    :	List[Any]     					=			v
                           else:
                                  lowercase__    :	Any     					=			k.split('_'							)
                                  lowercase__    :	str     					=			' '.join([part.capitalize() for part in splits[1:]]							)
                                  lowercase__    :	Optional[int]     					=			v
                    self.training_tracker.write_line(a							)
                    self.training_tracker.remove_child()
                    lowercase__    :	Optional[int]     					=			None
                    # Evaluation takes a long time so we should force the next update.
                    lowercase__    :	List[str]     					=			True


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a	,    **a							) ->      str:
             self.training_tracker.update(
                 state.global_step	,    comment=f"""Epoch {int(state.epoch							)}/{state.num_train_epochs}"""	,    force_update=a							)
             lowercase__    :	Optional[Any]     					=			None


"""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   UpperCAmelCase_			:


      def __init__(     self	,    a	,    a=1_3	,    a=1_0	,    a=3	,    a=2	,    a=2	,    a=2	,    a=True	,    a=True	,    a=3_2	,    a=5	,    a=4	,    a=3_7	,    a="gelu"	,    a=0.1	,    a=0.1	,    a=1_0	,    a=0.02	,    a=0.9	,    a=None	,    ) ->      Optional[Any]:
             lowercase__    :	str     					=			parent
             lowercase__    :	int     					=			batch_size
             lowercase__    :	Union[str, Any]     					=			image_size
             lowercase__    :	Optional[Any]     					=			num_channels
             lowercase__    :	Dict     					=			patch_size
             lowercase__    :	Tuple     					=			tubelet_size
             lowercase__    :	Optional[int]     					=			num_frames
             lowercase__    :	Optional[int]     					=			is_training
             lowercase__    :	int     					=			use_labels
             lowercase__    :	Optional[int]     					=			hidden_size
             lowercase__    :	Union[str, Any]     					=			num_hidden_layers
             lowercase__    :	Optional[int]     					=			num_attention_heads
             lowercase__    :	Any     					=			intermediate_size
             lowercase__    :	str     					=			hidden_act
             lowercase__    :	List[Any]     					=			hidden_dropout_prob
             lowercase__    :	str     					=			attention_probs_dropout_prob
             lowercase__    :	Union[str, Any]     					=			type_sequence_label_size
             lowercase__    :	List[Any]     					=			initializer_range
             lowercase__    :	str     					=			mask_ratio
             lowercase__    :	Optional[Any]     					=			scope

             # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
             lowercase__    :	Optional[Any]     					=			(image_size // patch_size) ** 2
             lowercase__    :	str     					=			(num_frames // tubelet_size) * self.num_patches_per_frame

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


      def 	_UpperCAmelCase (     self							) ->      Tuple:
             lowercase__    :	int     					=			floats_tensor(
                 [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]							)

             lowercase__    :	int     					=			None
             if self.use_labels:
                    lowercase__    :	Optional[Any]     					=			ids_tensor([self.batch_size]	,    self.type_sequence_label_size							)

             lowercase__    :	Dict     					=			self.get_config()

             return config, pixel_values, labels


      def 	_UpperCAmelCase (     self							) ->      Tuple:
             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=a	,    initializer_range=self.initializer_range	,    )


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a							) ->      Optional[int]:
             lowercase__    :	Dict     					=			VideoMAEModel(config=a							)
             model.to(a							)
             model.eval()
             lowercase__    :	Tuple     					=			model(a							)
             self.parent.assertEqual(result.last_hidden_state.shape	,    (self.batch_size, self.seq_length, self.hidden_size)							)


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a							) ->      Union[str, Any]:
             lowercase__    :	str     					=			VideoMAEForPreTraining(a							)
             model.to(a							)
             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
             lowercase__    :	Any     					=			torch.ones((self.num_masks,)							)
             lowercase__    :	str     					=			torch.cat([mask, torch.zeros(self.seq_length - mask.size(0							)							)]							)
             lowercase__    :	Optional[int]     					=			mask.expand(self.batch_size	,    -1							).bool()

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


      def 	_UpperCAmelCase (     self							) ->      str:
             lowercase__    :	Dict     					=			self.prepare_config_and_inputs()
             lowercase__					,   lowercase__					,   lowercase__    :	Union[str, Any]     					=			config_and_inputs
             lowercase__    :	List[str]     					=			{'pixel_values': pixel_values}
             return config, inputs_dict





@require_torch
class   UpperCAmelCase_			(					_a				,							_a				,							unittest.TestCase):
      lowerCamelCase__	:					Tuple										=						(
          (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
      )
      lowerCamelCase__	:					Optional[int]										=						(
          {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification}
          if is_torch_available()
          else {}
      )

      lowerCamelCase__	:					Any										=						False
      lowerCamelCase__	:					Any										=						False
      lowerCamelCase__	:					Union[str, Any]										=						False
      lowerCamelCase__	:					str										=						False


      def 	_UpperCAmelCase (     self							) ->      Tuple:
             lowercase__    :	Optional[Any]     					=			VideoMAEModelTester(self							)
             lowercase__    :	Optional[Any]     					=			ConfigTester(self	,    config_class=a	,    has_text_modality=a	,    hidden_size=3_7							)


      def 	_UpperCAmelCase (     self	,    a	,    a	,    a=False							) ->      Optional[int]:
             lowercase__    :	Union[str, Any]     					=			copy.deepcopy(a							)

             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
                    lowercase__    :	Optional[Any]     					=			torch.ones((self.model_tester.num_masks,)							)
                    lowercase__    :	Any     					=			torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0							)							)]							)
                    lowercase__    :	Any     					=			mask.expand(self.model_tester.batch_size	,    -1							).bool()
                    lowercase__    :	Union[str, Any]     					=			bool_masked_pos.to(a							)

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

             return inputs_dict


      def 	_UpperCAmelCase (     self							) ->      Tuple:
             self.config_tester.run_common_tests()


      @unittest.skip(reason='VideoMAE does not use inputs_embeds'							)
      def 	_UpperCAmelCase (     self							) ->      Dict:
             pass


      def 	_UpperCAmelCase (     self							) ->      List[Any]:
             lowercase__					,   lowercase__    :	Optional[int]     					=			self.model_tester.prepare_config_and_inputs_for_common()

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


      def 	_UpperCAmelCase (     self							) ->      Optional[int]:
             lowercase__					,   lowercase__    :	Any     					=			self.model_tester.prepare_config_and_inputs_for_common()

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

                    lowercase__    :	int     					=			['pixel_values']
                    self.assertListEqual(arg_names[:1]	,    a							)


      def 	_UpperCAmelCase (     self							) ->      Optional[Any]:
             lowercase__    :	List[str]     					=			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*a							)


      def 	_UpperCAmelCase (     self							) ->      Tuple:
             lowercase__    :	Union[str, Any]     					=			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_pretraining(*a							)


      @slow
      def 	_UpperCAmelCase (     self							) ->      str:
             for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase__    :	List[Any]     					=			VideoMAEModel.from_pretrained(a							)
                    self.assertIsNotNone(a							)


      def 	_UpperCAmelCase (     self							) ->      Optional[Any]:
             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__    :	Union[str, Any]     					=			self.model_tester.seq_length - self.model_tester.num_masks
                           lowercase__    :	Any     					=			(
                               num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                           )

                           lowercase__    :	Optional[Any]     					=			True
                           lowercase__    :	int     					=			False
                           lowercase__    :	Any     					=			True
                           lowercase__    :	List[str]     					=			model_class(a							)
                           model.to(a							)
                           model.eval()
                           with torch.no_grad():
                                  lowercase__    :	Optional[int]     					=			model(**self._prepare_for_class(a	,    a							)							)
                           lowercase__    :	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"]
                           lowercase__    :	str     					=			True
                           lowercase__    :	List[str]     					=			model_class(a							)
                           model.to(a							)
                           model.eval()
                           with torch.no_grad():
                                  lowercase__    :	List[Any]     					=			model(**self._prepare_for_class(a	,    a							)							)
                           lowercase__    :	Optional[Any]     					=			outputs.attentions
                           self.assertEqual(len(a							)	,    self.model_tester.num_hidden_layers							)

                           self.assertListEqual(
                               list(attentions[0].shape[-3:]							)	,    [self.model_tester.num_attention_heads, seq_len, seq_len]	,    )
                           lowercase__    :	List[str]     					=			len(a							)

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

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

                           lowercase__    :	int     					=			outputs.attentions

                           self.assertEqual(len(a							)	,    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 	_UpperCAmelCase (     self							) ->      Optional[int]:
             def check_hidden_states_output(a	,    a	,    a							):
                    lowercase__    :	Optional[int]     					=			model_class(a							)
                    model.to(a							)
                    model.eval()

                    with torch.no_grad():
                           lowercase__    :	Optional[Any]     					=			model(**self._prepare_for_class(a	,    a							)							)

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

                    lowercase__    :	Optional[Any]     					=			self.model_tester.seq_length - self.model_tester.num_masks
                    lowercase__    :	Union[str, Any]     					=			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]	,    )

             lowercase__					,   lowercase__    :	List[Any]     					=			self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    lowercase__    :	Tuple     					=			True
                    check_hidden_states_output(a	,    a	,    a							)

                    # check that output_hidden_states also work using config
                    del inputs_dict["output_hidden_states"]
                    lowercase__    :	Union[str, Any]     					=			True

                    check_hidden_states_output(a	,    a	,    a							)


      @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'							)
      def 	_UpperCAmelCase (     self							) ->      List[Any]:
             pass





def        a_  (   ):





       '''simple docstring'''

       lowercase__    :	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   UpperCAmelCase_			(					unittest.TestCase):


      @cached_property
      def 	_UpperCAmelCase (     self							) ->      Optional[Any]:
             # logits were tested with a different mean and std, so we use the same here
             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 	_UpperCAmelCase (     self							) ->      int:
             lowercase__    :	Dict     					=			VideoMAEForVideoClassification.from_pretrained('MCG-NJU/videomae-base-finetuned-kinetics'							).to(
                 a							)

             lowercase__    :	str     					=			self.default_image_processor
             lowercase__    :	List[str]     					=			prepare_video()
             lowercase__    :	int     					=			image_processor(a	,    return_tensors='pt'							).to(a							)

             # forward pass
             with torch.no_grad():
                    lowercase__    :	Union[str, Any]     					=			model(**a							)

             # verify the logits
             lowercase__    :	str     					=			torch.Size((1, 4_0_0)							)
             self.assertEqual(outputs.logits.shape	,    a							)

             lowercase__    :	List[Any]     					=			torch.tensor([0.3_669, -0.0_688, -0.2_421]							).to(a							)

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


      @slow
      def 	_UpperCAmelCase (     self							) ->      List[str]:
             lowercase__    :	Optional[int]     					=			VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'							).to(a							)

             lowercase__    :	Optional[Any]     					=			self.default_image_processor
             lowercase__    :	List[str]     					=			prepare_video()
             lowercase__    :	str     					=			image_processor(a	,    return_tensors='pt'							).to(a							)

             # add boolean mask, indicating which patches to mask
             lowercase__    :	Union[str, Any]     					=			hf_hub_download(repo_id='hf-internal-testing/bool-masked-pos'	,    filename='bool_masked_pos.pt'							)
             lowercase__    :	str     					=			torch.load(a							)

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

             # verify the logits
             lowercase__    :	Dict     					=			torch.Size([1, 1_4_0_8, 1_5_3_6]							)
             lowercase__    :	List[str]     					=			torch.tensor(
                 [[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]]	,    device=a							)
             self.assertEqual(outputs.logits.shape	,    a							)
             self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]	,    a	,    atol=1e-4							)							)

             # verify the loss (`config.norm_pix_loss` = `True`)
             lowercase__    :	List[Any]     					=			torch.tensor([0.5_142]	,    device=a							)
             self.assertTrue(torch.allclose(outputs.loss	,    a	,    atol=1e-4							)							)

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

             with torch.no_grad():
                    lowercase__    :	Any     					=			model(**a							)

             lowercase__    :	List[Any]     					=			torch.tensor(torch.tensor([0.6_469]							)	,    device=a							)
             self.assertTrue(torch.allclose(outputs.loss	,    a	,    atol=1e-4							)							)

