



"""simple docstring"""







import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel

def 							UpperCamelCase_       (       lowerCAmelCase__ :							Dict       ,  lowerCAmelCase__ :							Dict		)		->  List[str]:

  """simple docstring"""




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

  lowerCAmelCase_  :							str								=					pt_attn_layer.out_proj.weight
  lowerCAmelCase_  :							int								=					pt_attn_layer.out_proj.bias

  lowerCAmelCase_  :							Optional[int]								=					q_proj
  lowerCAmelCase_  :							Dict								=					q_proj_bias

  lowerCAmelCase_  :							Tuple								=					k_proj
  lowerCAmelCase_  :							Tuple								=					k_proj_bias

  lowerCAmelCase_  :							Union[str, Any]								=					v_proj
  lowerCAmelCase_  :							Optional[int]								=					v_proj_bias

  lowerCAmelCase_  :							Dict								=					out_proj_weights
  lowerCAmelCase_  :							List[Any]								=					out_proj_bias

def 							UpperCamelCase_       (       lowerCAmelCase__ :							str       ,  lowerCAmelCase__ :							int		)		->  Tuple:

  """simple docstring"""




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

def 							UpperCamelCase_       (       lowerCAmelCase__ :							Optional[Any]       ,  lowerCAmelCase__ :							Any		)		->  Optional[int]:

  """simple docstring"""




  lowerCAmelCase_  :							Dict								=					pt_linear.weight
  lowerCAmelCase_  :							Optional[int]								=					pt_linear.bias

def 							UpperCamelCase_       (       lowerCAmelCase__ :							List[Any]       ,  lowerCAmelCase__ :							Any		)		->  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__ :							List[str]		)		->  str:

  """simple docstring"""




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

def 							UpperCamelCase_       (       lowerCAmelCase__ :							Union[str, Any]       ,  lowerCAmelCase__ :							Dict		)		->  List[Any]:

  """simple docstring"""




  lowerCAmelCase_  :							List[str]								=					pt_model.token_embedding.weight
  lowerCAmelCase_  :							int								=					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__ :							Optional[Any]       ,  lowerCAmelCase__ :							Dict		)		->  Optional[int]:

  """simple docstring"""




  lowerCAmelCase_  :							Dict								=					pt_model.text_projection.data.T

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

def 							UpperCamelCase_       (       lowerCAmelCase__ :							List[Any]       ,  lowerCAmelCase__ :							Any		)		->  Optional[Any]:

  """simple docstring"""




  lowerCAmelCase_  :							List[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
  lowerCAmelCase_  :							Tuple								=					pt_model.visual.conva.weight.data
  lowerCAmelCase_  :							Dict								=					pt_model.visual.class_embedding
  lowerCAmelCase_  :							Optional[int]								=					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__ :							List[str]       ,  lowerCAmelCase__ :							Dict       ,  lowerCAmelCase__ :							str=None		)		->  int:

  """simple docstring"""




  if config_path is not None:
    lowerCAmelCase_  :							List[str]								=					CLIPConfig.from_pretrained(lowerCAmelCase__		)
  else:
    lowerCAmelCase_  :							Union[str, Any]								=					CLIPConfig(projection_dim=512       ,  text_config={}       ,  vision_config={}		)

  lowerCAmelCase_  :							Optional[Any]								=					CLIPModel(lowerCAmelCase__		).eval()

  lowerCAmelCase_		,lowerCAmelCase_  :							str								=					load(lowerCAmelCase__       ,  device='cpu'       ,  jit=lowerCAmelCase__		)
  lowerCAmelCase_  :							List[Any]								=					pt_model.eval()

  copy_text_model_and_projection(lowerCAmelCase__       ,  lowerCAmelCase__		)
  copy_vison_model_and_projection(lowerCAmelCase__       ,  lowerCAmelCase__		)
  lowerCAmelCase_  :							List[Any]								=					pt_model.logit_scale

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

  lowerCAmelCase_  :							Dict								=					hf_model(input_ids=lowerCAmelCase__       ,  pixel_values=lowerCAmelCase__       ,  return_dict=lowerCAmelCase__		)
  lowerCAmelCase_  :							Dict								=					hf_outputs.logits_per_image
  lowerCAmelCase_  :							List[str]								=					hf_outputs.logits_per_text
  lowerCAmelCase_		,lowerCAmelCase_  :							Any								=					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__":
     lowercase__						:							List[str]        =      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""")
     lowercase__						:							Optional[int]        =      parser.parse_args()

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










"""simple docstring"""







import unittest
from timeit import timeit

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  lowerCAmelCase_  :							Any								=					first_num if first_num >= second_num else second_num
  lowerCAmelCase_  :							Union[str, Any]								=					max_num
  while (common_mult % first_num > 0) or (common_mult % second_num > 0):
    common_mult += max_num
  return common_mult

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  return b if a == 0 else greatest_common_divisor(b % a       ,  lowerCAmelCase__		)

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  return first_num // greatest_common_divisor(lowerCAmelCase__       ,  lowerCAmelCase__		) * second_num

def 							UpperCamelCase_       (       )		->  Dict:

  """simple docstring"""




  lowerCAmelCase_  :							str								=					(
      'from __main__ import least_common_multiple_slow, least_common_multiple_fast'
  )
  print(
      'least_common_multiple_slow():'       ,  timeit('least_common_multiple_slow(1000, 999)'       ,  setup=lowerCAmelCase__		)       ,  )
  print(
      'least_common_multiple_fast():'       ,  timeit('least_common_multiple_fast(1000, 999)'       ,  setup=lowerCAmelCase__		)       ,  )







class  UpperCamelCase__  (      unittest.TestCase    ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE    							=	(
          (10, 20),
          (13, 15),
          (4, 31),
          (10, 42),
          (43, 34),
          (5, 12),
          (12, 25),
          (10, 25),
          (6, 9),
      )
      _SCREAMING_SNAKE_CASE    							=	(20, 195, 124, 210, 1462, 60, 300, 50, 18)



      def 	SCREAMING_SNAKE_CASE__					( self							: List[str]			):
        for i, (first_num, second_num) in enumerate(self.test_inputs			):
          lowerCAmelCase_  :							Union[str, Any]								=					least_common_multiple_slow(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)
          lowerCAmelCase_  :							Any								=					least_common_multiple_fast(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)
          with self.subTest(i=SCREAMING_SNAKE_CASE_			):
            self.assertEqual(SCREAMING_SNAKE_CASE_       ,     self.expected_results[i]			)
            self.assertEqual(SCREAMING_SNAKE_CASE_       ,     self.expected_results[i]			)


if __name__ == "__main__":
     benchmark()
     unittest.main()





