

import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   List[str]:

     """simple docstring"""







     _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase :       Any	   =     pt_attn_layer.in_proj_weight.chunk(3		,				dim=0					)
     _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase :       Any	   =     pt_attn_layer.in_proj_bias.chunk(3		,				dim=0					)

     _lowerCAmelCase :       Tuple	   =     pt_attn_layer.out_proj.weight
     _lowerCAmelCase :       Optional[int]	   =     pt_attn_layer.out_proj.bias

     _lowerCAmelCase :       Optional[int]	   =     q_proj
     _lowerCAmelCase :       Union[str, Any]	   =     q_proj_bias

     _lowerCAmelCase :       Optional[int]	   =     k_proj
     _lowerCAmelCase :       List[str]	   =     k_proj_bias

     _lowerCAmelCase :       Any	   =     v_proj
     _lowerCAmelCase :       Optional[int]	   =     v_proj_bias

     _lowerCAmelCase :       Optional[int]	   =     out_proj_weights
     _lowerCAmelCase :       str	   =     out_proj_bias







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Optional[Any]:

     """simple docstring"""







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







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Optional[int]:

     """simple docstring"""







     _lowerCAmelCase :       str	   =     pt_linear.weight
     _lowerCAmelCase :       List[str]	   =     pt_linear.bias







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """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 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   List[Any]:

     """simple docstring"""







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







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   List[Any]:

     """simple docstring"""







     _lowerCAmelCase :       Optional[int]	   =     pt_model.token_embedding.weight
     _lowerCAmelCase :       Optional[Any]	   =     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 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Optional[Any]:

     """simple docstring"""







     _lowerCAmelCase :       Union[str, Any]	   =     pt_model.text_projection.data.T

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







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """simple docstring"""







     _lowerCAmelCase :       int	   =     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
     _lowerCAmelCase :       Union[str, Any]	   =     pt_model.visual.conva.weight.data
     _lowerCAmelCase :       Dict	   =     pt_model.visual.class_embedding
     _lowerCAmelCase :       Union[str, Any]	   =     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 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=None					)							->   str:

     """simple docstring"""







     if config_path is not None:
          _lowerCAmelCase :       Optional[int]	   =     CLIPConfig.from_pretrained(SCREAMING_SNAKE_CASE					)
     else:
          _lowerCAmelCase :       List[Any]	   =     CLIPConfig(projection_dim=512		,				text_config={}		,				vision_config={}					)

     _lowerCAmelCase :       List[str]	   =     CLIPModel(SCREAMING_SNAKE_CASE					).eval()

     _lowerCAmelCase   ,      _lowerCAmelCase :       str	   =     load(SCREAMING_SNAKE_CASE		,				device="cpu"		,				jit=SCREAMING_SNAKE_CASE					)
     _lowerCAmelCase :       Dict	   =     pt_model.eval()

     copy_text_model_and_projection(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)
     copy_vison_model_and_projection(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)
     _lowerCAmelCase :       Optional[int]	   =     pt_model.logit_scale

     _lowerCAmelCase :       Dict	   =     torch.arange(0		,				77					).unsqueeze(0					)
     _lowerCAmelCase :       int	   =     torch.randn(1		,				3		,				224		,				224					)

     _lowerCAmelCase :       Tuple	   =     hf_model(input_ids=SCREAMING_SNAKE_CASE		,				pixel_values=SCREAMING_SNAKE_CASE		,				return_dict=SCREAMING_SNAKE_CASE					)
     _lowerCAmelCase :       Dict	   =     hf_outputs.logits_per_image
     _lowerCAmelCase :       Dict	   =     hf_outputs.logits_per_text
     _lowerCAmelCase   ,      _lowerCAmelCase :       str	   =     pt_model(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)

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

     hf_model.save_pretrained(SCREAMING_SNAKE_CASE					)


if __name__ == "__main__":
  __UpperCAmelCase     					=  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     					=  parser.parse_args()

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









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



 """simple docstring"""







 def __init__(		self			:						Union[str, Any]  ,    A_			:						Optional[Any]  ,    A_			:						List[str]=7  ,    A_			:						Optional[Any]=3  ,    A_			:						int=1_8  ,    A_			:						List[Any]=3_0  ,    A_			:						str=4_0_0  ,    A_			:						Union[str, Any]=True  ,    A_			:						Union[str, Any]=None  ,    A_			:						Union[str, Any]=True  ,    A_			:						Optional[int]=None  ,    A_			:						List[str]=True  ,    A_			:						Union[str, Any]=[0.48145466, 0.4578275, 0.40821073]  ,    A_			:						int=[0.26862954, 0.26130258, 0.27577711]  ,    A_			:						Optional[int]=True  ,    ):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     size if size is not None else {"shortest_edge": 2_0}
      _lowerCAmelCase :       Union[str, Any]	   =     crop_size if crop_size is not None else {"height": 1_8, "width": 1_8}
      _lowerCAmelCase :       Tuple	   =     parent
      _lowerCAmelCase :       int	   =     batch_size
      _lowerCAmelCase :       str	   =     num_channels
      _lowerCAmelCase :       int	   =     image_size
      _lowerCAmelCase :       Optional[int]	   =     min_resolution
      _lowerCAmelCase :       Any	   =     max_resolution
      _lowerCAmelCase :       str	   =     do_resize
      _lowerCAmelCase :       Union[str, Any]	   =     size
      _lowerCAmelCase :       Optional[int]	   =     do_center_crop
      _lowerCAmelCase :       Union[str, Any]	   =     crop_size
      _lowerCAmelCase :       Optional[int]	   =     do_normalize
      _lowerCAmelCase :       Optional[int]	   =     image_mean
      _lowerCAmelCase :       int	   =     image_std
      _lowerCAmelCase :       Optional[Any]	   =     do_convert_rgb

 def  __magic_name__							(		self			:						Any							):






      '''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  __magic_name__							(		self			:						List[Any]  ,    A_			:						List[Any]=False  ,    A_			:						Optional[Any]=False  ,    A_			:						Tuple=False							):






      '''simple docstring'''




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

      if equal_resolution:
           _lowerCAmelCase :       Tuple	   =     []
           for i in range(self.batch_size							):
                image_inputs.append(
                    np.random.randint(
                        2_5_5  ,    size=(self.num_channels, self.max_resolution, self.max_resolution)  ,    dtype=np.uinta							)							)
      else:
           _lowerCAmelCase :       Optional[int]	   =     []
           for i in range(self.batch_size							):
                _lowerCAmelCase   ,      _lowerCAmelCase :       Union[str, Any]	   =     np.random.choice(np.arange(self.min_resolution  ,    self.max_resolution							)  ,    2							)
                image_inputs.append(np.random.randint(2_5_5  ,    size=(self.num_channels, width, height)  ,    dtype=np.uinta							)							)

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

      if torchify:
           _lowerCAmelCase :       Tuple	   =     [torch.from_numpy(A_							) for x in image_inputs]

      return image_inputs





@require_torch
@require_vision
class A__     (			A	,		unittest.TestCase			):



 """simple docstring"""







 _lowercase  :       Any  			=    CLIPImageProcessor if is_vision_available() else None

 def  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     CLIPImageProcessingTester(self							)

 @property
 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      return self.image_processor_tester.prepare_image_processor_dict()

 def  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[int]	   =     self.image_processing_class(**self.image_processor_dict							)
      self.assertTrue(hasattr(A_  ,    "do_resize"							)							)
      self.assertTrue(hasattr(A_  ,    "size"							)							)
      self.assertTrue(hasattr(A_  ,    "do_center_crop"							)							)
      self.assertTrue(hasattr(A_  ,    "center_crop"							)							)
      self.assertTrue(hasattr(A_  ,    "do_normalize"							)							)
      self.assertTrue(hasattr(A_  ,    "image_mean"							)							)
      self.assertTrue(hasattr(A_  ,    "image_std"							)							)
      self.assertTrue(hasattr(A_  ,    "do_convert_rgb"							)							)

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       Union[str, Any]	   =     self.image_processing_class.from_dict(self.image_processor_dict							)
      self.assertEqual(image_processor.size  ,    {"shortest_edge": 2_0}							)
      self.assertEqual(image_processor.crop_size  ,    {"height": 1_8, "width": 1_8}							)

      _lowerCAmelCase :       List[Any]	   =     self.image_processing_class.from_dict(self.image_processor_dict  ,    size=4_2  ,    crop_size=8_4							)
      self.assertEqual(image_processor.size  ,    {"shortest_edge": 4_2}							)
      self.assertEqual(image_processor.crop_size  ,    {"height": 8_4, "width": 8_4}							)

 def  __magic_name__							(		self			:						Dict							):






      '''simple docstring'''




      pass

 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random PIL images
      _lowerCAmelCase :       Union[str, Any]	   =     self.image_processor_tester.prepare_inputs(equal_resolution=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    Image.Image							)

      # Test not batched input
      _lowerCAmelCase :       Optional[Any]	   =     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
      _lowerCAmelCase :       Optional[Any]	   =     image_processing(A_  ,    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  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       List[Any]	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random numpy tensors
      _lowerCAmelCase :       Dict	   =     self.image_processor_tester.prepare_inputs(equal_resolution=A_  ,    numpify=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    np.ndarray							)

      # Test not batched input
      _lowerCAmelCase :       Optional[Any]	   =     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
      _lowerCAmelCase :       Union[str, Any]	   =     image_processing(A_  ,    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  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




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

      # Test not batched input
      _lowerCAmelCase :       Tuple	   =     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
      _lowerCAmelCase :       List[Any]	   =     image_processing(A_  ,    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 A__     (			A	,		unittest.TestCase			):



 """simple docstring"""







 _lowercase  :       List[str]  			=    CLIPImageProcessor if is_vision_available() else None

 def  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




      _lowerCAmelCase :       int	   =     CLIPImageProcessingTester(self  ,    num_channels=4							)
      _lowerCAmelCase :       Tuple	   =     3

 @property
 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      return self.image_processor_tester.prepare_image_processor_dict()

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       List[str]	   =     self.image_processing_class(**self.image_processor_dict							)
      self.assertTrue(hasattr(A_  ,    "do_resize"							)							)
      self.assertTrue(hasattr(A_  ,    "size"							)							)
      self.assertTrue(hasattr(A_  ,    "do_center_crop"							)							)
      self.assertTrue(hasattr(A_  ,    "center_crop"							)							)
      self.assertTrue(hasattr(A_  ,    "do_normalize"							)							)
      self.assertTrue(hasattr(A_  ,    "image_mean"							)							)
      self.assertTrue(hasattr(A_  ,    "image_std"							)							)
      self.assertTrue(hasattr(A_  ,    "do_convert_rgb"							)							)

 def  __magic_name__							(		self			:						Optional[Any]							):






      '''simple docstring'''




      pass


 def  __magic_name__							(		self			:						Any							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random PIL images
      _lowerCAmelCase :       Optional[int]	   =     self.image_processor_tester.prepare_inputs(equal_resolution=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    Image.Image							)

      # Test not batched input
      _lowerCAmelCase :       Optional[Any]	   =     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
      _lowerCAmelCase :       int	   =     image_processing(A_  ,    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"],
          )  ,    )






