




'''simple docstring'''



import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel







def      _UpperCamelCase	(_lowerCamelCase							:       str				,		_lowerCamelCase							:       Dict		)->				List[Any]:




      '''simple docstring'''





      __snake_case         =					pt_attn_layer.in_proj_weight.chunk(3				,		dim=0		)
      __snake_case         =					pt_attn_layer.in_proj_bias.chunk(3				,		dim=0		)

      __snake_case         =					pt_attn_layer.out_proj.weight
      __snake_case         =					pt_attn_layer.out_proj.bias

      __snake_case         =					q_proj
      __snake_case         =					q_proj_bias

      __snake_case         =					k_proj
      __snake_case         =					k_proj_bias

      __snake_case         =					v_proj
      __snake_case         =					v_proj_bias

      __snake_case         =					out_proj_weights
      __snake_case         =					out_proj_bias







def      _UpperCamelCase	(_lowerCamelCase							:       List[Any]				,		_lowerCamelCase							:       List[Any]		)->				Dict:




      '''simple docstring'''





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







def      _UpperCamelCase	(_lowerCamelCase							:       Union[str, Any]				,		_lowerCamelCase							:       List[str]		)->				Optional[Any]:




      '''simple docstring'''





      __snake_case         =					pt_linear.weight
      __snake_case         =					pt_linear.bias







def      _UpperCamelCase	(_lowerCamelCase							:       Optional[Any]				,		_lowerCamelCase							:       int		)->				Optional[Any]:




      '''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      _UpperCamelCase	(_lowerCamelCase							:       Optional[int]				,		_lowerCamelCase							:       Union[str, Any]		)->				Optional[Any]:




      '''simple docstring'''





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







def      _UpperCamelCase	(_lowerCamelCase							:       List[Any]				,		_lowerCamelCase							:       Tuple		)->				Optional[int]:




      '''simple docstring'''





      __snake_case         =					pt_model.token_embedding.weight
      __snake_case         =					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      _UpperCamelCase	(_lowerCamelCase							:       Dict				,		_lowerCamelCase							:       Tuple		)->				Tuple:




      '''simple docstring'''





      __snake_case         =					pt_model.text_projection.data.T

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







def      _UpperCamelCase	(_lowerCamelCase							:       Dict				,		_lowerCamelCase							:       Union[str, Any]		)->				List[Any]:




      '''simple docstring'''





      __snake_case         =					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
      __snake_case         =					pt_model.visual.conva.weight.data
      __snake_case         =					pt_model.visual.class_embedding
      __snake_case         =					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      _UpperCamelCase	(_lowerCamelCase							:       Union[str, Any]				,		_lowerCamelCase							:       List[str]				,		_lowerCamelCase							:       Any=None		)->				int:




      '''simple docstring'''





      if config_path is not None:
            __snake_case         =					CLIPConfig.from_pretrained(_lowerCamelCase		)
      else:
            __snake_case         =					CLIPConfig(projection_dim=5_12				,		text_config={}				,		vision_config={}		)

      __snake_case         =					CLIPModel(_lowerCamelCase		).eval()

      __snake_case         =					load(_lowerCamelCase				,		device='''cpu'''				,		jit=_lowerCamelCase		)
      __snake_case         =					pt_model.eval()

      copy_text_model_and_projection(_lowerCamelCase				,		_lowerCamelCase		)
      copy_vison_model_and_projection(_lowerCamelCase				,		_lowerCamelCase		)
      __snake_case         =					pt_model.logit_scale

      __snake_case         =					torch.arange(0				,		77		).unsqueeze(0		)
      __snake_case         =					torch.randn(1				,		3				,		2_24				,		2_24		)

      __snake_case         =					hf_model(input_ids=_lowerCamelCase				,		pixel_values=_lowerCamelCase				,		return_dict=_lowerCamelCase		)
      __snake_case         =					hf_outputs.logits_per_image
      __snake_case         =					hf_outputs.logits_per_text
      __snake_case         =					pt_model(_lowerCamelCase				,		_lowerCamelCase		)

      assert torch.allclose(_lowerCamelCase				,		_lowerCamelCase				,		atol=1E-3		)
      assert torch.allclose(_lowerCamelCase				,		_lowerCamelCase				,		atol=1E-3		)

      hf_model.save_pretrained(_lowerCamelCase		)


if __name__ == "__main__":
     UpperCAmelCase_    :					Any					      =						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''')
     UpperCAmelCase_    :					Union[str, Any]					      =						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


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import CLIPImageProcessor





class       lowerCAmelCase				(	unittest.TestCase):


       def __init__(				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE=7      ,					__SCREAMING_SNAKE_CASE=3      ,					__SCREAMING_SNAKE_CASE=18      ,					__SCREAMING_SNAKE_CASE=30      ,					__SCREAMING_SNAKE_CASE=400      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=None      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=None      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=[0.48_145_466, 0.4_578_275, 0.40_821_073]      ,					__SCREAMING_SNAKE_CASE=[0.26_862_954, 0.26_130_258, 0.27_577_711]      ,					__SCREAMING_SNAKE_CASE=True      ,					) ->					Optional[Any]:



            '''simple docstring'''




            __snake_case         =					size if size is not None else {'''shortest_edge''': 20}
            __snake_case         =					crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
            __snake_case         =					parent
            __snake_case         =					batch_size
            __snake_case         =					num_channels
            __snake_case         =					image_size
            __snake_case         =					min_resolution
            __snake_case         =					max_resolution
            __snake_case         =					do_resize
            __snake_case         =					size
            __snake_case         =					do_center_crop
            __snake_case         =					crop_size
            __snake_case         =					do_normalize
            __snake_case         =					image_mean
            __snake_case         =					image_std
            __snake_case         =					do_convert_rgb


       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




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




       def  lowerCAmelCase (				self      ,					__SCREAMING_SNAKE_CASE=False      ,					__SCREAMING_SNAKE_CASE=False      ,					__SCREAMING_SNAKE_CASE=False					) ->					str:



            '''simple docstring'''




            assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

            if equal_resolution:
                 __snake_case         =					[]
                 for i in range(self.batch_size					):
                      image_inputs.append(
                          np.random.randint(
                              255      ,					size=(self.num_channels, self.max_resolution, self.max_resolution)      ,					dtype=np.uinta					)					)
            else:
                 __snake_case         =					[]
                 for i in range(self.batch_size					):
                      __snake_case   ,      __snake_case         =					np.random.choice(np.arange(self.min_resolution      ,					self.max_resolution					)      ,					2					)
                      image_inputs.append(np.random.randint(255      ,					size=(self.num_channels, width, height)      ,					dtype=np.uinta					)					)

            if not numpify and not torchify:
                 # PIL expects the channel dimension as last dimension
                 __snake_case         =					[Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE      ,					0      ,					-1					)					) for x in image_inputs]

            if torchify:
                 __snake_case         =					[torch.from_numpy(__SCREAMING_SNAKE_CASE					) for x in image_inputs]

            return image_inputs





@require_torch
@require_vision
class       lowerCAmelCase				(	__lowerCAmelCase			,				unittest.TestCase):
       __lowercase					: Dict	    =     CLIPImageProcessor if is_vision_available() else None


       def  lowerCAmelCase (				self					) ->					int:



            '''simple docstring'''




            __snake_case         =					CLIPImageProcessingTester(self					)


       @property
       def  lowerCAmelCase (				self					) ->					List[str]:



            '''simple docstring'''




            return self.image_processor_tester.prepare_image_processor_dict()


       def  lowerCAmelCase (				self					) ->					Optional[Any]:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''do_resize'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''size'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''do_center_crop'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''center_crop'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''do_normalize'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''image_mean'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''image_std'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''do_convert_rgb'''					)					)


       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class.from_dict(self.image_processor_dict					)
            self.assertEqual(image_processor.size      ,					{'''shortest_edge''': 20}					)
            self.assertEqual(image_processor.crop_size      ,					{'''height''': 18, '''width''': 18}					)

            __snake_case         =					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  lowerCAmelCase (				self					) ->					Dict:



            '''simple docstring'''




            pass


       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            # create random PIL images
            __snake_case         =					self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE					)
            for image in image_inputs:
                 self.assertIsInstance(__SCREAMING_SNAKE_CASE      ,					Image.Image					)

            # Test not batched input
            __snake_case         =					image_processing(image_inputs[0]      ,					return_tensors='''pt'''					).pixel_values
            self.assertEqual(
                encoded_images.shape      ,					(
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )      ,					)

            # Test batched
            __snake_case         =					image_processing(__SCREAMING_SNAKE_CASE      ,					return_tensors='''pt'''					).pixel_values
            self.assertEqual(
                encoded_images.shape      ,					(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )      ,					)


       def  lowerCAmelCase (				self					) ->					Dict:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            # create random numpy tensors
            __snake_case         =					self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE      ,					numpify=__SCREAMING_SNAKE_CASE					)
            for image in image_inputs:
                 self.assertIsInstance(__SCREAMING_SNAKE_CASE      ,					np.ndarray					)

            # Test not batched input
            __snake_case         =					image_processing(image_inputs[0]      ,					return_tensors='''pt'''					).pixel_values
            self.assertEqual(
                encoded_images.shape      ,					(
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )      ,					)

            # Test batched
            __snake_case         =					image_processing(__SCREAMING_SNAKE_CASE      ,					return_tensors='''pt'''					).pixel_values
            self.assertEqual(
                encoded_images.shape      ,					(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )      ,					)




       def  lowerCAmelCase (				self					) ->					int:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            # create random PyTorch tensors
            __snake_case         =					self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE      ,					torchify=__SCREAMING_SNAKE_CASE					)
            for image in image_inputs:
                 self.assertIsInstance(__SCREAMING_SNAKE_CASE      ,					torch.Tensor					)

            # Test not batched input
            __snake_case         =					image_processing(image_inputs[0]      ,					return_tensors='''pt'''					).pixel_values
            self.assertEqual(
                encoded_images.shape      ,					(
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )      ,					)

            # Test batched
            __snake_case         =					image_processing(__SCREAMING_SNAKE_CASE      ,					return_tensors='''pt'''					).pixel_values
            self.assertEqual(
                encoded_images.shape      ,					(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )      ,					)





@require_torch
@require_vision
class       lowerCAmelCase				(	__lowerCAmelCase			,				unittest.TestCase):
       __lowercase					: Dict	    =     CLIPImageProcessor if is_vision_available() else None


       def  lowerCAmelCase (				self					) ->					Any:



            '''simple docstring'''




            __snake_case         =					CLIPImageProcessingTester(self      ,					num_channels=4					)
            __snake_case         =					3


       @property
       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            return self.image_processor_tester.prepare_image_processor_dict()


       def  lowerCAmelCase (				self					) ->					List[Any]:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''do_resize'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''size'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''do_center_crop'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''center_crop'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''do_normalize'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''image_mean'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''image_std'''					)					)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,					'''do_convert_rgb'''					)					)


       def  lowerCAmelCase (				self					) ->					Tuple:



            '''simple docstring'''




            pass




       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            # create random PIL images
            __snake_case         =					self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE					)
            for image in image_inputs:
                 self.assertIsInstance(__SCREAMING_SNAKE_CASE      ,					Image.Image					)

            # Test not batched input
            __snake_case         =					image_processing(image_inputs[0]      ,					return_tensors='''pt'''					).pixel_values
            self.assertEqual(
                encoded_images.shape      ,					(
                    1,
                    self.expected_encoded_image_num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )      ,					)

            # Test batched
            __snake_case         =					image_processing(__SCREAMING_SNAKE_CASE      ,					return_tensors='''pt'''					).pixel_values
            self.assertEqual(
                encoded_images.shape      ,					(
                    self.image_processor_tester.batch_size,
                    self.expected_encoded_image_num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )      ,					)






