




import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel

def       lowerCAmelCase_      (	lowerCamelCase:							int  ,					lowerCamelCase:							str    )       ->      List[Any]:


      '''simple docstring'''




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

      lowerCamelCase__   	=					pt_attn_layer.out_proj.weight
      lowerCamelCase__   	=					pt_attn_layer.out_proj.bias

      lowerCamelCase__   	=					q_proj
      lowerCamelCase__   	=					q_proj_bias

      lowerCamelCase__   	=					k_proj
      lowerCamelCase__   	=					k_proj_bias

      lowerCamelCase__   	=					v_proj
      lowerCamelCase__   	=					v_proj_bias

      lowerCamelCase__   	=					out_proj_weights
      lowerCamelCase__   	=					out_proj_bias

def       lowerCAmelCase_      (	lowerCamelCase:							List[Any]  ,					lowerCamelCase:							Any    )       ->      int:


      '''simple docstring'''




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

def       lowerCAmelCase_      (	lowerCamelCase:							str  ,					lowerCamelCase:							Optional[Any]    )       ->      Optional[int]:


      '''simple docstring'''




      lowerCamelCase__   	=					pt_linear.weight
      lowerCamelCase__   	=					pt_linear.bias

def       lowerCAmelCase_      (	lowerCamelCase:							str  ,					lowerCamelCase:							List[str]    )       ->      Optional[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       lowerCAmelCase_      (	lowerCamelCase:							Optional[int]  ,					lowerCamelCase:							Optional[Any]    )       ->      str:


      '''simple docstring'''




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

def       lowerCAmelCase_      (	lowerCamelCase:							Optional[int]  ,					lowerCamelCase:							Optional[Any]    )       ->      List[Any]:


      '''simple docstring'''




      lowerCamelCase__   	=					pt_model.token_embedding.weight
      lowerCamelCase__   	=					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       lowerCAmelCase_      (	lowerCamelCase:							Tuple  ,					lowerCamelCase:							List[str]    )       ->      List[Any]:


      '''simple docstring'''




      lowerCamelCase__   	=					pt_model.text_projection.data.T

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

def       lowerCAmelCase_      (	lowerCamelCase:							str  ,					lowerCamelCase:							Optional[Any]    )       ->      Optional[Any]:


      '''simple docstring'''




      lowerCamelCase__   	=					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__   	=					pt_model.visual.conva.weight.data
      lowerCamelCase__   	=					pt_model.visual.class_embedding
      lowerCamelCase__   	=					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       lowerCAmelCase_      (	lowerCamelCase:							Any  ,					lowerCamelCase:							Any  ,					lowerCamelCase:							List[str]=None    )       ->      List[Any]:


      '''simple docstring'''




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

      lowerCamelCase__   	=					CLIPModel(lowerCamelCase    ).eval()

      lowerCamelCase__	,		lowerCamelCase__   	=					load(lowerCamelCase  ,					device='cpu'  ,					jit=lowerCamelCase    )
      lowerCamelCase__   	=					pt_model.eval()

      copy_text_model_and_projection(lowerCamelCase  ,					lowerCamelCase    )
      copy_vison_model_and_projection(lowerCamelCase  ,					lowerCamelCase    )
      lowerCamelCase__   	=					pt_model.logit_scale

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

      lowerCamelCase__   	=					hf_model(input_ids=lowerCamelCase  ,					pixel_values=lowerCamelCase  ,					return_dict=lowerCamelCase    )
      lowerCamelCase__   	=					hf_outputs.logits_per_image
      lowerCamelCase__   	=					hf_outputs.logits_per_text
      lowerCamelCase__	,		lowerCamelCase__   	=					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__":
      __SCREAMING_SNAKE_CASE     : 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""")
      __SCREAMING_SNAKE_CASE     : List[str]      =parser.parse_args()

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





import math

def       lowerCAmelCase_      (	lowerCamelCase:							int    )       ->      str:


      '''simple docstring'''




      if number == 0:
            lowerCamelCase__   	=					'Zero'
            return words
      else:
            lowerCamelCase__   	=					math.logaa(lowerCamelCase    )
            lowerCamelCase__   	=					digits + 1
            lowerCamelCase__   	=					{}
            lowerCamelCase__   	=					''
            lowerCamelCase__   	=					'One'
            lowerCamelCase__   	=					'Two'
            lowerCamelCase__   	=					'Three'
            lowerCamelCase__   	=					'Four'
            lowerCamelCase__   	=					'Five'
            lowerCamelCase__   	=					'Six'
            lowerCamelCase__   	=					'Seven'
            lowerCamelCase__   	=					'Eight'
            lowerCamelCase__   	=					'Nine'

            lowerCamelCase__   	=					{}
            lowerCamelCase__   	=					''
            lowerCamelCase__   	=					'Twenty'
            lowerCamelCase__   	=					'Thirty'
            lowerCamelCase__   	=					'Forty'
            lowerCamelCase__   	=					'Fifty'
            lowerCamelCase__   	=					'Sixty'
            lowerCamelCase__   	=					'Seventy'
            lowerCamelCase__   	=					'Eighty'
            lowerCamelCase__   	=					'Ninety'

            lowerCamelCase__   	=					{}
            lowerCamelCase__   	=					'Ten'
            lowerCamelCase__   	=					'Eleven'
            lowerCamelCase__   	=					'Twelve'
            lowerCamelCase__   	=					'Thirteen'
            lowerCamelCase__   	=					'Fourteen'
            lowerCamelCase__   	=					'Fifteen'
            lowerCamelCase__   	=					'Sixteen'
            lowerCamelCase__   	=					'Seventeen'
            lowerCamelCase__   	=					'Eighteen'
            lowerCamelCase__   	=					'Nineteen'

            lowerCamelCase__   	=					{}
            lowerCamelCase__   	=					'Hundred,'
            lowerCamelCase__   	=					'Thousand,'
            lowerCamelCase__   	=					'Lakh,'
            lowerCamelCase__   	=					'Crore,'

            lowerCamelCase__   	=					number
            lowerCamelCase__   	=					''
            lowerCamelCase__   	=					0
            lowerCamelCase__   	=					int(lowerCamelCase    )
            while counter < digits:
                  lowerCamelCase__   	=					temp_num % 10
                  if counter % 2 == 0:
                        lowerCamelCase__   	=					''
                        if counter in placevalue and current != 0:
                              lowerCamelCase__   	=					placevalue[counter]
                        if counter == 2:
                              lowerCamelCase__   	=					singles[current] + addition + words
                        elif counter == 0:
                              if ((temp_num % 1_00) // 10) == 1:
                                    lowerCamelCase__   	=					teens[current] + addition + words
                                    lowerCamelCase__   	=					temp_num // 10
                                    counter += 1
                              else:
                                    lowerCamelCase__   	=					singles[current] + addition + words

                        else:
                              lowerCamelCase__   	=					doubles[current] + addition + words

                  else:
                        if counter == 1:
                              if current == 1:
                                    lowerCamelCase__   	=					teens[number % 10] + words
                              else:
                                    lowerCamelCase__   	=					''
                                    if counter in placevalue:
                                          lowerCamelCase__   	=					placevalue[counter]
                                    lowerCamelCase__   	=					doubles[current] + addition + words
                        else:
                              lowerCamelCase__   	=					''
                              if counter in placevalue:
                                    if current == 0 and ((temp_num % 1_00) // 10) == 0:
                                          lowerCamelCase__   	=					''
                                    else:
                                          lowerCamelCase__   	=					placevalue[counter]
                              if ((temp_num % 1_00) // 10) == 1:
                                    lowerCamelCase__   	=					teens[current] + addition + words
                                    lowerCamelCase__   	=					temp_num // 10
                                    counter += 1
                              else:
                                    lowerCamelCase__   	=					singles[current] + addition + words
                  counter += 1
                  lowerCamelCase__   	=					temp_num // 10
      return words


if __name__ == "__main__":
      import doctest

      doctest.testmod()