

import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel






def        __lowercase   (  _A    ,     _A      )  ->    List[Any]:
      SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE			:   Dict	  = pt_attn_layer.in_proj_weight.chunk(3    ,     dim=0      )
      SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE			:   Any	  = pt_attn_layer.in_proj_bias.chunk(3    ,     dim=0      )

      SCREAMING_SNAKE_CASE			:   Optional[int]	  = pt_attn_layer.out_proj.weight
      SCREAMING_SNAKE_CASE			:   int	  = pt_attn_layer.out_proj.bias

      SCREAMING_SNAKE_CASE			:   Dict	  = q_proj
      SCREAMING_SNAKE_CASE			:   Optional[Any]	  = q_proj_bias

      SCREAMING_SNAKE_CASE			:   Optional[Any]	  = k_proj
      SCREAMING_SNAKE_CASE			:   int	  = k_proj_bias

      SCREAMING_SNAKE_CASE			:   str	  = v_proj
      SCREAMING_SNAKE_CASE			:   Tuple	  = v_proj_bias

      SCREAMING_SNAKE_CASE			:   Optional[int]	  = out_proj_weights
      SCREAMING_SNAKE_CASE			:   int	  = out_proj_bias






def        __lowercase   (  _A    ,     _A      )  ->    str:
      copy_linear(hf_mlp.fca    ,     pt_mlp.c_fc      )
      copy_linear(hf_mlp.fca    ,     pt_mlp.c_proj      )






def        __lowercase   (  _A    ,     _A      )  ->    str:
      SCREAMING_SNAKE_CASE			:   Dict	  = pt_linear.weight
      SCREAMING_SNAKE_CASE			:   Any	  = pt_linear.bias






def        __lowercase   (  _A    ,     _A      )  ->    List[Any]:
      # copy layer norms
      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   (  _A    ,     _A      )  ->    str:
      for hf_layer, pt_layer in zip(_A    ,     _A      ):
            copy_layer(_A    ,     _A      )






def        __lowercase   (  _A    ,     _A      )  ->    str:
      # copy  embeds
      SCREAMING_SNAKE_CASE			:   Tuple	  = pt_model.token_embedding.weight
      SCREAMING_SNAKE_CASE			:   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        __lowercase   (  _A    ,     _A      )  ->    Dict:
      # copy projection
      SCREAMING_SNAKE_CASE			:   int	  = pt_model.text_projection.data.T

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






def        __lowercase   (  _A    ,     _A      )  ->    List[Any]:
      # copy projection
      SCREAMING_SNAKE_CASE			:   Optional[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
      SCREAMING_SNAKE_CASE			:   str	  = pt_model.visual.conva.weight.data
      SCREAMING_SNAKE_CASE			:   Optional[int]	  = pt_model.visual.class_embedding
      SCREAMING_SNAKE_CASE			:   List[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        __lowercase   (  _A    ,     _A    ,     _A=None      )  ->    List[Any]:
      if config_path is not None:
            SCREAMING_SNAKE_CASE			:   int	  = CLIPConfig.from_pretrained(_A      )
      else:
            SCREAMING_SNAKE_CASE			:   Tuple	  = CLIPConfig(projection_dim=512    ,     text_config={}    ,     vision_config={}      )

      SCREAMING_SNAKE_CASE			:   Union[str, Any]	  = CLIPModel(_A      ).eval()

      SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE			:   Union[str, Any]	  = load(_A    ,     device="""cpu"""    ,     jit=_A      )
      SCREAMING_SNAKE_CASE			:   Any	  = pt_model.eval()

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

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

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

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

      hf_model.save_pretrained(_A      )


if __name__ == "__main__":
  UpperCAmelCase__			:  Tuple					     =     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__			:  Any					     =     parser.parse_args()

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







from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
  import torch

UpperCAmelCase__			:  int					     =     logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
  from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):






  def        __lowercase   (  cls      )  ->    Any:
        return cls






def        __lowercase   (  _A    ,     _A = None    ,     _A = None    ,     _A = None    ,     _A = None    ,     )  ->    Tuple:
      SCREAMING_SNAKE_CASE			:   Optional[Any]	  = device
      SCREAMING_SNAKE_CASE			:   Tuple	  = shape[0]

      SCREAMING_SNAKE_CASE			:   Optional[int]	  = layout or torch.strided
      SCREAMING_SNAKE_CASE			:   Dict	  = device or torch.device("""cpu"""      )

      if generator is not None:
            SCREAMING_SNAKE_CASE			:   Optional[Any]	  = generator.device.type if not isinstance(_A    ,     _A      ) else generator[0].device.type
            if gen_device_type != device.type and gen_device_type == "cpu":
                  SCREAMING_SNAKE_CASE			:   Tuple	  = """cpu"""
                  if device != "mps":
                        logger.info(
                            F"The passed generator was created on 'cpu' even though a tensor on {device} was expected."
                            F" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"
                            F" slighly speed up this function by passing a generator that was created on the {device} device."      )
            elif gen_device_type != device.type and gen_device_type == "cuda":
                  raise ValueError(F"Cannot generate a {device} tensor from a generator of type {gen_device_type}."      )

      if isinstance(_A    ,     _A      ):
            SCREAMING_SNAKE_CASE			:   int	  = (1,) + shape[1:]
            SCREAMING_SNAKE_CASE			:   Any	  = [
                torch.randn(_A    ,     generator=generator[i]    ,     device=_A    ,     dtype=_A    ,     layout=_A      )
                for i in range(_A      )
            ]
            SCREAMING_SNAKE_CASE			:   Any	  = torch.cat(_A    ,     dim=0      ).to(_A      )
      else:
            SCREAMING_SNAKE_CASE			:   int	  = torch.randn(_A    ,     generator=_A    ,     device=_A    ,     dtype=_A    ,     layout=_A      ).to(_A      )

      return latents







def        __lowercase   (  _A      )  ->    Dict:
      if is_torch_version("""<"""    ,     """2.0.0"""      ) or not hasattr(_A    ,     """_dynamo"""      ):
            return False
      return isinstance(_A    ,     torch._dynamo.eval_frame.OptimizedModule      )




