





"""simple docstring"""



import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel







def 	__lowercase  (     snake_case_						:						Dict       ,snake_case_						:						Union[str, Any]		)     ->Tuple:



    '''simple docstring'''







    __A				, __A				, __A					:		Any						=  pt_attn_layer.in_proj_weight.chunk(3       ,dim=0		)
    __A				, __A				, __A					:		Optional[int]						=  pt_attn_layer.in_proj_bias.chunk(3       ,dim=0		)

    __A					:		int						=  pt_attn_layer.out_proj.weight
    __A					:		int						=  pt_attn_layer.out_proj.bias

    __A					:		Optional[Any]						=  q_proj
    __A					:		Optional[Any]						=  q_proj_bias

    __A					:		List[str]						=  k_proj
    __A					:		Tuple						=  k_proj_bias

    __A					:		Dict						=  v_proj
    __A					:		List[str]						=  v_proj_bias

    __A					:		Optional[int]						=  out_proj_weights
    __A					:		Optional[Any]						=  out_proj_bias







def 	__lowercase  (     snake_case_						:						Union[str, Any]       ,snake_case_						:						Tuple		)     ->Optional[int]:



    '''simple docstring'''







    copy_linear(hf_mlp.fca       ,pt_mlp.c_fc		)
    copy_linear(hf_mlp.fca       ,pt_mlp.c_proj		)







def 	__lowercase  (     snake_case_						:						int       ,snake_case_						:						Tuple		)     ->Optional[int]:



    '''simple docstring'''







    __A					:		List[str]						=  pt_linear.weight
    __A					:		Optional[int]						=  pt_linear.bias







def 	__lowercase  (     snake_case_						:						Any       ,snake_case_						:						Union[str, Any]		)     ->Dict:



    '''simple docstring'''







    copy_linear(hf_layer.layer_norma       ,pt_layer.ln_a		)
    copy_linear(hf_layer.layer_norma       ,pt_layer.ln_a		)

    # copy MLP
    copy_mlp(hf_layer.mlp       ,pt_layer.mlp		)

    # copy attn
    copy_attn_layer(hf_layer.self_attn       ,pt_layer.attn		)







def 	__lowercase  (     snake_case_						:						Dict       ,snake_case_						:						str		)     ->Any:



    '''simple docstring'''







    for hf_layer, pt_layer in zip(snake_case_       ,snake_case_		):
        copy_layer(snake_case_       ,snake_case_		)







def 	__lowercase  (     snake_case_						:						Union[str, Any]       ,snake_case_						:						str		)     ->Tuple:



    '''simple docstring'''







    __A					:		Optional[int]						=  pt_model.token_embedding.weight
    __A					:		List[str]						=  pt_model.positional_embedding

    # copy layer norm
    copy_linear(hf_encoder.final_layer_norm       ,pt_model.ln_final		)

    # copy hidden layers
    copy_layers(hf_encoder.encoder.layers       ,pt_model.transformer.resblocks		)







def 	__lowercase  (     snake_case_						:						Union[str, Any]       ,snake_case_						:						Tuple		)     ->Any:



    '''simple docstring'''







    __A					:		List[Any]						=  pt_model.text_projection.data.T

    # copy text encoder
    copy_encoder(hf_model.text_model       ,snake_case_		)







def 	__lowercase  (     snake_case_						:						Optional[Any]       ,snake_case_						:						int		)     ->Union[str, Any]:



    '''simple docstring'''







    __A					:		Optional[Any]						=  pt_model.visual.proj.data.T

    # copy layer norms
    copy_linear(hf_model.vision_model.pre_layrnorm       ,pt_model.visual.ln_pre		)
    copy_linear(hf_model.vision_model.post_layernorm       ,pt_model.visual.ln_post		)

    # copy embeds
    __A					:		Any						=  pt_model.visual.conva.weight.data
    __A					:		Any						=  pt_model.visual.class_embedding
    __A					:		str						=  pt_model.visual.positional_embedding.data

    # copy encoder
    copy_layers(hf_model.vision_model.encoder.layers       ,pt_model.visual.transformer.resblocks		)







@torch.no_grad()
def 	__lowercase  (     snake_case_						:						str       ,snake_case_						:						List[str]       ,snake_case_						:						Dict=None		)     ->Any:



    '''simple docstring'''







    if config_path is not None:
        __A					:		Tuple						=  CLIPConfig.from_pretrained(snake_case_		)
    else:
        __A					:		Tuple						=  CLIPConfig(projection_dim=512       ,text_config={}       ,vision_config={}		)

    __A					:		Union[str, Any]						=  CLIPModel(snake_case_		).eval()

    __A				, __A					:		List[Any]						=  load(snake_case_       ,device='''cpu'''       ,jit=snake_case_		)
    __A					:		Optional[int]						=  pt_model.eval()

    copy_text_model_and_projection(snake_case_       ,snake_case_		)
    copy_vison_model_and_projection(snake_case_       ,snake_case_		)
    __A					:		Union[str, Any]						=  pt_model.logit_scale

    __A					:		Optional[int]						=  torch.arange(0       ,77		).unsqueeze(0		)
    __A					:		Any						=  torch.randn(1       ,3       ,224       ,224		)

    __A					:		Optional[Any]						=  hf_model(input_ids=snake_case_       ,pixel_values=snake_case_       ,return_dict=snake_case_		)
    __A					:		List[Any]						=  hf_outputs.logits_per_image
    __A					:		Tuple						=  hf_outputs.logits_per_text
    __A				, __A					:		Union[str, Any]						=  pt_model(snake_case_       ,snake_case_		)

    assert torch.allclose(snake_case_       ,snake_case_       ,atol=1e-3		)
    assert torch.allclose(snake_case_       ,snake_case_       ,atol=1e-3		)

    hf_model.save_pretrained(snake_case_		)


if __name__ == "__main__":
       a_				=    argparse.ArgumentParser()
       parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
       parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""")
       parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
       a_				=    parser.parse_args()

       convert_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)











"""simple docstring"""



import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import VideoMAEImageProcessor






class 	__snake_case      (	unittest.TestCase  ):





 """simple docstring"""


 def __init__(					self      ,		__lowerCamelCase      ,		__lowerCamelCase=7      ,		__lowerCamelCase=3      ,		__lowerCamelCase=10      ,		__lowerCamelCase=18      ,		__lowerCamelCase=30      ,		__lowerCamelCase=400      ,		__lowerCamelCase=True      ,		__lowerCamelCase=None      ,		__lowerCamelCase=True      ,		__lowerCamelCase=[0.5, 0.5, 0.5]      ,		__lowerCamelCase=[0.5, 0.5, 0.5]      ,		__lowerCamelCase=None      ,		):



     '''simple docstring'''




     __A					:		Optional[Any]						=  size if size is not None else {'''shortest_edge''': 18}
     __A					:		Optional[int]						=  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

     __A					:		Any						=  parent
     __A					:		Union[str, Any]						=  batch_size
     __A					:		Optional[int]						=  num_channels
     __A					:		int						=  num_frames
     __A					:		Optional[int]						=  image_size
     __A					:		Union[str, Any]						=  min_resolution
     __A					:		int						=  max_resolution
     __A					:		Tuple						=  do_resize
     __A					:		str						=  size
     __A					:		Any						=  do_normalize
     __A					:		Union[str, Any]						=  image_mean
     __A					:		List[str]						=  image_std
     __A					:		Union[str, Any]						=  crop_size







 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     return {
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_normalize": self.do_normalize,
         "do_resize": self.do_resize,
         "size": self.size,
         "crop_size": self.crop_size,
     }






@require_torch
@require_vision
class 	__snake_case      (	SCREAMING_SNAKE_CASE__							,							unittest.TestCase  ):





 """simple docstring"""


 _lowerCamelCase				 =  VideoMAEImageProcessor if is_vision_available() else None

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		List[str]						=  VideoMAEImageProcessingTester(self						)

 @property
 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     return self.image_processor_tester.prepare_image_processor_dict()

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Dict						=  self.image_processing_class(**self.image_processor_dict						)
     self.assertTrue(hasattr(__lowerCamelCase      ,		'''image_mean'''						)						)
     self.assertTrue(hasattr(__lowerCamelCase      ,		'''image_std'''						)						)
     self.assertTrue(hasattr(__lowerCamelCase      ,		'''do_normalize'''						)						)
     self.assertTrue(hasattr(__lowerCamelCase      ,		'''do_resize'''						)						)
     self.assertTrue(hasattr(__lowerCamelCase      ,		'''do_center_crop'''						)						)
     self.assertTrue(hasattr(__lowerCamelCase      ,		'''size'''						)						)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Optional[int]						=  self.image_processing_class.from_dict(self.image_processor_dict						)
     self.assertEqual(image_processor.size      ,		{'''shortest_edge''': 18}						)
     self.assertEqual(image_processor.crop_size      ,		{'''height''': 18, '''width''': 18}						)

     __A					:		Dict						=  self.image_processing_class.from_dict(self.image_processor_dict      ,		size=42      ,		crop_size=84						)
     self.assertEqual(image_processor.size      ,		{'''shortest_edge''': 42}						)
     self.assertEqual(image_processor.crop_size      ,		{'''height''': 84, '''width''': 84}						)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     pass

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		List[Any]						=  self.image_processing_class(**self.image_processor_dict						)
     # create random PIL videos
     __A					:		Optional[int]						=  prepare_video_inputs(self.image_processor_tester      ,		equal_resolution=__lowerCamelCase						)
     for video in video_inputs:
         self.assertIsInstance(__lowerCamelCase      ,		__lowerCamelCase						)
         self.assertIsInstance(video[0]      ,		Image.Image						)

     # Test not batched input
     __A					:		Any						=  image_processing(video_inputs[0]      ,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,		(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )      ,		)

     # Test batched
     __A					:		int						=  image_processing(__lowerCamelCase      ,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,		(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )      ,		)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		List[Any]						=  self.image_processing_class(**self.image_processor_dict						)
     # create random numpy tensors
     __A					:		List[str]						=  prepare_video_inputs(self.image_processor_tester      ,		equal_resolution=__lowerCamelCase      ,		numpify=__lowerCamelCase						)
     for video in video_inputs:
         self.assertIsInstance(__lowerCamelCase      ,		__lowerCamelCase						)
         self.assertIsInstance(video[0]      ,		np.ndarray						)

     # Test not batched input
     __A					:		List[str]						=  image_processing(video_inputs[0]      ,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,		(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )      ,		)

     # Test batched
     __A					:		int						=  image_processing(__lowerCamelCase      ,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,		(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )      ,		)







 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Optional[int]						=  self.image_processing_class(**self.image_processor_dict						)
     # create random PyTorch tensors
     __A					:		int						=  prepare_video_inputs(self.image_processor_tester      ,		equal_resolution=__lowerCamelCase      ,		torchify=__lowerCamelCase						)
     for video in video_inputs:
         self.assertIsInstance(__lowerCamelCase      ,		__lowerCamelCase						)
         self.assertIsInstance(video[0]      ,		torch.Tensor						)

     # Test not batched input
     __A					:		Optional[int]						=  image_processing(video_inputs[0]      ,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,		(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )      ,		)

     # Test batched
     __A					:		str						=  image_processing(__lowerCamelCase      ,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,		(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )      ,		)




