




import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel

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


      '''simple docstring'''




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

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


      '''simple docstring'''




      lowerCamelCase__ 					=			pt_linear.weight
      lowerCamelCase__ 					=			pt_linear.bias

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


      '''simple docstring'''




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

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


      '''simple docstring'''




      lowerCamelCase__ 					=			pt_model.text_projection.data.T

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

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


      '''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      :				Union[str, Any]				      =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      :				int				      =parser.parse_args()

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





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


      '''simple docstring'''




      lowerCamelCase__ 					=			[7_01, 3_01, 1_32, 57, 23, 10, 4, 1]
      for gap in gaps:
            for i in range(lowerCamelCase       ,			len(lowerCamelCase  )  ):
                  lowerCamelCase__ 					=			collection[i]
                  lowerCamelCase__ 					=			i
                  while j >= gap and collection[j - gap] > insert_value:
                        lowerCamelCase__ 					=			collection[j - gap]
                        j -= gap
                  if j != i:
                        lowerCamelCase__ 					=			insert_value
      return collection


if __name__ == "__main__":
      from doctest import testmod

      testmod()
      __SCREAMING_SNAKE_CASE      :				List[Any]				      =input("""Enter numbers separated by a comma:\n""").strip()
      __SCREAMING_SNAKE_CASE      :				Dict				      =[int(item) for item in user_input.split(""",""")]
      print(shell_sort(unsorted))