


import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin







class lowerCAmelCase_		(				lowerCamelCase__      ,		lowerCamelCase__ ):

 '''simple docstring'''

 @register_to_config
 def __init__(			self	,   *,
     _UpperCAmelCase = 4	,   _UpperCAmelCase = 7_68	,   _UpperCAmelCase	,   _UpperCAmelCase	,   ):
       super().__init__()

       snake_case_ =   nn.Parameter(torch.zeros(_UpperCAmelCase      )      )

       # parameters for additional clip time embeddings
       snake_case_ =   nn.Linear(_UpperCAmelCase	,   _UpperCAmelCase      )
       snake_case_ =   nn.Linear(_UpperCAmelCase	,   _UpperCAmelCase      )

       # parameters for encoder hidden states
       snake_case_ =   clip_extra_context_tokens
       snake_case_ =   nn.Linear(
           _UpperCAmelCase	,   self.clip_extra_context_tokens * cross_attention_dim      )
       snake_case_ =   nn.Linear(_UpperCAmelCase	,   _UpperCAmelCase      )
       snake_case_ =   nn.LayerNorm(_UpperCAmelCase      )





 def 		UpperCamelCase__  (			self	,   *, _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase      ):
       if do_classifier_free_guidance:
             # Add the classifier free guidance embeddings to the image embeddings
             snake_case_ =   image_embeddings.shape[0]
             snake_case_ =   self.learned_classifier_free_guidance_embeddings.unsqueeze(0      )
             snake_case_ =   classifier_free_guidance_embeddings.expand(
                 _UpperCAmelCase	,   -1      )
             snake_case_ =   torch.cat([classifier_free_guidance_embeddings, image_embeddings]	,   dim=0      )

       # The image embeddings batch size and the text embeddings batch size are equal
       assert image_embeddings.shape[0] == prompt_embeds.shape[0]

       snake_case_ =   prompt_embeds.shape[0]

       # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
       # adding CLIP embeddings to the existing timestep embedding, ...
       snake_case_ =   self.embedding_proj(_UpperCAmelCase      )
       snake_case_ =   self.clip_image_embeddings_project_to_time_embeddings(_UpperCAmelCase      )
       snake_case_ =   time_projected_image_embeddings + time_projected_prompt_embeds

       # ... and by projecting CLIP embeddings into four
       # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
       snake_case_ =   self.clip_extra_context_tokens_proj(_UpperCAmelCase      )
       snake_case_ =   clip_extra_context_tokens.reshape(_UpperCAmelCase	,   -1	,   self.clip_extra_context_tokens      )
       snake_case_ =   clip_extra_context_tokens.permute(0	,   2	,   1      )

       snake_case_ =   self.encoder_hidden_states_proj(_UpperCAmelCase      )
       snake_case_ =   self.text_encoder_hidden_states_norm(_UpperCAmelCase      )
       snake_case_ =   torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]	,   dim=1      )

       return text_encoder_hidden_states, additive_clip_time_embeddings



import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY







@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class lowerCAmelCase_		(				unittest.TestCase ):

 '''simple docstring'''

 __snake_case				=						MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING



 def 		UpperCamelCase__  (			self	,   _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase      ):
       snake_case_ =   hf_hub_download(
           repo_id='''nateraw/video-demo'''	,   filename='''archery.mp4'''	,   repo_type='''dataset'''      )
       snake_case_ =   VideoClassificationPipeline(model=_UpperCAmelCase	,   image_processor=_UpperCAmelCase	,   top_k=2      )
       snake_case_ =   [
           example_video_filepath,
           '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''',
       ]
       return video_classifier, examples



 def 		UpperCamelCase__  (			self	,   _UpperCAmelCase	,   _UpperCAmelCase      ):
       for example in examples:
             snake_case_ =   video_classifier(_UpperCAmelCase      )

             self.assertEqual(
                 _UpperCAmelCase	,   [
                     {'''score''': ANY(_UpperCAmelCase      ), '''label''': ANY(_UpperCAmelCase      )},
                     {'''score''': ANY(_UpperCAmelCase      ), '''label''': ANY(_UpperCAmelCase      )},
                 ]	,   )



 @require_torch
 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   '''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
       snake_case_ =   VideoMAEFeatureExtractor(
           size={'''shortest_edge''': 10}	,   crop_size={'''height''': 10, '''width''': 10}      )
       snake_case_ =   pipeline(
           '''video-classification'''	,   model=_UpperCAmelCase	,   feature_extractor=_UpperCAmelCase	,   frame_sampling_rate=4      )

       snake_case_ =   hf_hub_download(repo_id='''nateraw/video-demo'''	,   filename='''archery.mp4'''	,   repo_type='''dataset'''      )
       snake_case_ =   video_classifier(_UpperCAmelCase	,   top_k=2      )
       self.assertEqual(
           nested_simplify(_UpperCAmelCase	,   decimals=4      )	,   [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}]	,   )

       snake_case_ =   video_classifier(
           [
               video_file_path,
               video_file_path,
           ]	,   top_k=2	,   )
       self.assertEqual(
           nested_simplify(_UpperCAmelCase	,   decimals=4      )	,   [
               [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}],
               [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}],
           ]	,   )





 @require_tf
 def 		UpperCamelCase__  (			self      ):
       pass