



"""simple docstring"""




import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      Dict:






      '''simple docstring'''


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

      lowercase	:   List[str]											= pt_attn_layer.out_proj.weight
      lowercase	:   Any											= pt_attn_layer.out_proj.bias

      lowercase	:   List[str]											= q_proj
      lowercase	:   List[Any]											= q_proj_bias

      lowercase	:   List[str]											= k_proj
      lowercase	:   Optional[Any]											= k_proj_bias

      lowercase	:   List[str]											= v_proj
      lowercase	:   List[Any]											= v_proj_bias

      lowercase	:   Dict											= out_proj_weights
      lowercase	:   int											= out_proj_bias


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      List[Any]:






      '''simple docstring'''


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


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      Dict:






      '''simple docstring'''


      lowercase	:   Dict											= pt_linear.weight
      lowercase	:   Tuple											= pt_linear.bias


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      str:






      '''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__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      List[str]:






      '''simple docstring'''


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


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      Union[str, Any]:






      '''simple docstring'''


      lowercase	:   Optional[int]											= pt_model.token_embedding.weight
      lowercase	:   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__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      Dict:






      '''simple docstring'''


      lowercase	:   Any											= pt_model.text_projection.data.T

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


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      List[str]:






      '''simple docstring'''


      lowercase	:   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
      lowercase	:   List[Any]											= pt_model.visual.conva.weight.data
      lowercase	:   Union[str, Any]											= pt_model.visual.class_embedding
      lowercase	:   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 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase=None					)  ->      Union[str, Any]:






      '''simple docstring'''


      if config_path is not None:
            lowercase	:   Optional[Any]											= CLIPConfig.from_pretrained(_UpperCAmelCase					)
      else:
            lowercase	:   int											= CLIPConfig(projection_dim=5_12						,   text_config={}						,   vision_config={}					)

      lowercase	:   Optional[int]											= CLIPModel(_UpperCAmelCase					).eval()

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

      copy_text_model_and_projection(_UpperCAmelCase						,   _UpperCAmelCase					)
      copy_vison_model_and_projection(_UpperCAmelCase						,   _UpperCAmelCase					)
      lowercase	:   List[Any]											= pt_model.logit_scale

      lowercase	:   Tuple											= torch.arange(0						,   77					).unsqueeze(0					)
      lowercase	:   Optional[Any]											= torch.randn(1						,   3						,   2_24						,   2_24					)

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

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

      hf_model.save_pretrained(_UpperCAmelCase					)


if __name__ == "__main__":
     _UpperCamelCase:					Dict     				=	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:					List[str]     				=	parser.parse_args()

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





"""simple docstring"""




def 	lowercase__  (      )  ->      Any:






      '''simple docstring'''


      lowercase	:   Dict											= 1

      while True:
            if (
                sorted(str(_UpperCAmelCase					)					)
                == sorted(str(2 * i					)					)
                == sorted(str(3 * i					)					)
                == sorted(str(4 * i					)					)
                == sorted(str(5 * i					)					)
                == sorted(str(6 * i					)					)
            ):
                  return i

            i += 1


if __name__ == "__main__":
     print(solution())
