


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		,				_UpperCAmelCase		,				_UpperCAmelCase=13		,				_UpperCAmelCase=10		,				_UpperCAmelCase=3		,				_UpperCAmelCase=2		,				_UpperCAmelCase=2		,				_UpperCAmelCase=True		,				_UpperCAmelCase=True		,				_UpperCAmelCase=32		,				_UpperCAmelCase=5		,				_UpperCAmelCase=4		,				_UpperCAmelCase=37		,				_UpperCAmelCase="gelu"		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=10		,				_UpperCAmelCase=0.02		,				_UpperCAmelCase="divided_space_time"		,				_UpperCAmelCase=None		,				):
       snake_case_	=				parent
       snake_case_	=				batch_size
       snake_case_	=				image_size
       snake_case_	=				num_channels
       snake_case_	=				patch_size
       snake_case_	=				num_frames
       snake_case_	=				is_training
       snake_case_	=				use_labels
       snake_case_	=				hidden_size
       snake_case_	=				num_hidden_layers
       snake_case_	=				num_attention_heads
       snake_case_	=				intermediate_size
       snake_case_	=				hidden_act
       snake_case_	=				hidden_dropout_prob
       snake_case_	=				attention_probs_dropout_prob
       snake_case_	=				attention_type
       snake_case_	=				initializer_range
       snake_case_	=				scope
       snake_case_	=				num_labels

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



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				floats_tensor(
           [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] )

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

       snake_case_	=				self.get_config()

       return config, pixel_values, labels



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				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		,				)
       snake_case_	=				self.num_labels
       return config



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				TimesformerModel(config=_UpperCAmelCase )
       model.to(_UpperCAmelCase )
       model.eval()
       snake_case_	=				model(_UpperCAmelCase )
       self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size) )



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				TimesformerForVideoClassification(_UpperCAmelCase )
       model.to(_UpperCAmelCase )
       model.eval()

       snake_case_	=				model(_UpperCAmelCase )

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





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







@require_torch
class lowerCAmelCase_						(			__lowercase ,						__lowercase ,						unittest.TestCase	):

 '''simple docstring'''

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

 __snake_case												=  False
 __snake_case												=  False
 __snake_case												=  False
 __snake_case												=  False



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				TimesformerModelTester(self )
       snake_case_	=				ConfigTester(
           self		,				config_class=_UpperCAmelCase		,				has_text_modality=_UpperCAmelCase		,				hidden_size=37 )



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase=False ):
       snake_case_	=				copy.deepcopy(_UpperCAmelCase )

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

       return inputs_dict



 def 						UpperCamelCase__					(     self ):
       self.config_tester.run_common_tests()



 @unittest.skip(reason='''TimeSformer does not use inputs_embeds''' )
 def 						UpperCamelCase__					(     self ):
       pass



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs_for_common()

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



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs_for_common()

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

             snake_case_	=				["""pixel_values"""]
             self.assertListEqual(arg_names[:1]		,				_UpperCAmelCase )



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase )



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_video_classification(*_UpperCAmelCase )



 @slow
 def 						UpperCamelCase__					(     self ):

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



 def 						UpperCamelCase__					(     self ):

       if not self.has_attentions:
             pass

       else:
             snake_case_	=				self.model_tester.prepare_config_and_inputs_for_common()
             snake_case_	=				True

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

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

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

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

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

                   snake_case_	=				outputs.attentions

                   self.assertEqual(len(_UpperCAmelCase )		,				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 						UpperCamelCase__					(     self ):

       def check_hidden_states_output(_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
             snake_case_	=				model_class(_UpperCAmelCase )
             model.to(_UpperCAmelCase )
             model.eval()

             with torch.no_grad():
                   snake_case_	=				model(**self._prepare_for_class(_UpperCAmelCase		,				_UpperCAmelCase ) )

             snake_case_	=				outputs.hidden_states
             snake_case_	=				self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(_UpperCAmelCase )		,				_UpperCAmelCase )

             snake_case_	=				self.model_tester.seq_length

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

       snake_case_	=				self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             snake_case_	=				True
             check_hidden_states_output(_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase )

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

             check_hidden_states_output(_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase )






def 	__lowerCAmelCase   ()->  List[Any]:




      """simple docstring"""

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







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

 '''simple docstring'''

 @cached_property
 def 						UpperCamelCase__					(     self ):
       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 ):
       snake_case_	=				TimesformerForVideoClassification.from_pretrained('''facebook/timesformer-base-finetuned-k400''' ).to(
           _UpperCAmelCase )

       snake_case_	=				self.default_image_processor
       snake_case_	=				prepare_video()
       snake_case_	=				image_processor(video[:8]		,				return_tensors='''pt''' ).to(_UpperCAmelCase )

       # forward pass
       with torch.no_grad():
             snake_case_	=				model(**_UpperCAmelCase )

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

       snake_case_	=				torch.tensor([-0.3_016, -0.7_713, -0.4_205] ).to(_UpperCAmelCase )

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



import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase      =   logging.get_logger(__name__)

UpperCAmelCase      =   {
    """microsoft/wavlm-base""": """https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json""",
    # See all WavLM models at https://huggingface.co/models?filter=wavlm
}







class lowerCAmelCase_						(			lowerCamelCase__	):

 '''simple docstring'''

 __snake_case												=  "wavlm"



 def __init__(     self		,				_UpperCAmelCase=32		,				_UpperCAmelCase=7_68		,				_UpperCAmelCase=12		,				_UpperCAmelCase=12		,				_UpperCAmelCase=30_72		,				_UpperCAmelCase="gelu"		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=0.0		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=0.02		,				_UpperCAmelCase=1E-5		,				_UpperCAmelCase="group"		,				_UpperCAmelCase="gelu"		,				_UpperCAmelCase=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)		,				_UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2)		,				_UpperCAmelCase=(10, 3, 3, 3, 3, 2, 2)		,				_UpperCAmelCase=False		,				_UpperCAmelCase=1_28		,				_UpperCAmelCase=16		,				_UpperCAmelCase=3_20		,				_UpperCAmelCase=8_00		,				_UpperCAmelCase=False		,				_UpperCAmelCase=True		,				_UpperCAmelCase=0.05		,				_UpperCAmelCase=10		,				_UpperCAmelCase=2		,				_UpperCAmelCase=0.0		,				_UpperCAmelCase=10		,				_UpperCAmelCase=3_20		,				_UpperCAmelCase=2		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=1_00		,				_UpperCAmelCase=2_56		,				_UpperCAmelCase=2_56		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase="mean"		,				_UpperCAmelCase=False		,				_UpperCAmelCase=False		,				_UpperCAmelCase=2_56		,				_UpperCAmelCase=(5_12, 5_12, 5_12, 5_12, 15_00)		,				_UpperCAmelCase=(5, 3, 3, 1, 1)		,				_UpperCAmelCase=(1, 2, 3, 1, 1)		,				_UpperCAmelCase=5_12		,				_UpperCAmelCase=80		,				_UpperCAmelCase=0		,				_UpperCAmelCase=1		,				_UpperCAmelCase=2		,				_UpperCAmelCase=False		,				_UpperCAmelCase=3		,				_UpperCAmelCase=2		,				_UpperCAmelCase=3		,				_UpperCAmelCase=None		,				**_UpperCAmelCase		,				):
       super().__init__(**_UpperCAmelCase		,				pad_token_id=_UpperCAmelCase		,				bos_token_id=_UpperCAmelCase		,				eos_token_id=_UpperCAmelCase )
       snake_case_	=				hidden_size
       snake_case_	=				feat_extract_norm
       snake_case_	=				feat_extract_activation
       snake_case_	=				list(_UpperCAmelCase )
       snake_case_	=				list(_UpperCAmelCase )
       snake_case_	=				list(_UpperCAmelCase )
       snake_case_	=				conv_bias
       snake_case_	=				num_buckets
       snake_case_	=				max_bucket_distance
       snake_case_	=				num_conv_pos_embeddings
       snake_case_	=				num_conv_pos_embedding_groups
       snake_case_	=				len(self.conv_dim )
       snake_case_	=				num_hidden_layers
       snake_case_	=				intermediate_size
       snake_case_	=				hidden_act
       snake_case_	=				num_attention_heads
       snake_case_	=				hidden_dropout
       snake_case_	=				attention_dropout
       snake_case_	=				activation_dropout
       snake_case_	=				feat_proj_dropout
       snake_case_	=				final_dropout
       snake_case_	=				layerdrop
       snake_case_	=				layer_norm_eps
       snake_case_	=				initializer_range
       snake_case_	=				num_ctc_classes
       snake_case_	=				vocab_size
       snake_case_	=				do_stable_layer_norm
       snake_case_	=				use_weighted_layer_sum
       snake_case_	=				classifier_proj_size

       if (
           (len(self.conv_stride ) != self.num_feat_extract_layers)
           or (len(self.conv_kernel ) != self.num_feat_extract_layers)
           or (len(self.conv_dim ) != self.num_feat_extract_layers)
       ):
             raise ValueError(
                 '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
                 ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
                 F''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,'''
                 F''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' )

       # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
       snake_case_	=				apply_spec_augment
       snake_case_	=				mask_time_prob
       snake_case_	=				mask_time_length
       snake_case_	=				mask_time_min_masks
       snake_case_	=				mask_feature_prob
       snake_case_	=				mask_feature_length

       # parameters for pretraining with codevector quantized representations
       snake_case_	=				num_codevectors_per_group
       snake_case_	=				num_codevector_groups
       snake_case_	=				contrastive_logits_temperature
       snake_case_	=				num_negatives
       snake_case_	=				codevector_dim
       snake_case_	=				proj_codevector_dim
       snake_case_	=				diversity_loss_weight

       # ctc loss
       snake_case_	=				ctc_loss_reduction
       snake_case_	=				ctc_zero_infinity

       # adapter
       snake_case_	=				add_adapter
       snake_case_	=				adapter_kernel_size
       snake_case_	=				adapter_stride
       snake_case_	=				num_adapter_layers
       snake_case_	=				output_hidden_size or hidden_size

       # SequenceClassification-specific parameter. Feel free to ignore for other classes.
       snake_case_	=				classifier_proj_size

       # XVector-specific parameters. Feel free to ignore for other classes.
       snake_case_	=				list(_UpperCAmelCase )
       snake_case_	=				list(_UpperCAmelCase )
       snake_case_	=				list(_UpperCAmelCase )
       snake_case_	=				xvector_output_dim





 @property
 def 						UpperCamelCase__					(     self ):
       return functools.reduce(operator.mul		,				self.conv_stride		,				1 )