





import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel



def _a							(      lowerCamelCase:  int   ,							lowerCamelCase:  int					)      ->		Optional[int]:







      '''simple docstring'''






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

      __A			  =							pt_attn_layer.out_proj.weight
      __A			  =							pt_attn_layer.out_proj.bias

      __A			  =							q_proj
      __A			  =							q_proj_bias

      __A			  =							k_proj
      __A			  =							k_proj_bias

      __A			  =							v_proj
      __A			  =							v_proj_bias

      __A			  =							out_proj_weights
      __A			  =							out_proj_bias



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







      '''simple docstring'''






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



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







      '''simple docstring'''






      __A			  =							pt_linear.weight
      __A			  =							pt_linear.bias



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







      '''simple docstring'''






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



def _a							(      lowerCamelCase:  List[str]   ,							lowerCamelCase:  Tuple					)      ->		List[Any]:







      '''simple docstring'''






      __A			  =							pt_model.token_embedding.weight
      __A			  =							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 _a							(      lowerCamelCase:  Optional[int]   ,							lowerCamelCase:  List[str]					)      ->		Dict:







      '''simple docstring'''






      __A			  =							pt_model.text_projection.data.T

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



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







      '''simple docstring'''






      __A			  =							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
      __A			  =							pt_model.visual.conva.weight.data
      __A			  =							pt_model.visual.class_embedding
      __A			  =							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 _a							(      lowerCamelCase:  Union[str, Any]   ,							lowerCamelCase:  Any   ,							lowerCamelCase:  Dict=None					)      ->		Dict:







      '''simple docstring'''






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

      __A			  =							CLIPModel(lowerCamelCase					).eval()

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

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

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

      __A			  =							hf_model(input_ids=lowerCamelCase   ,							pixel_values=lowerCamelCase   ,							return_dict=lowerCamelCase					)
      __A			  =							hf_outputs.logits_per_image
      __A			  =							hf_outputs.logits_per_text
      __A  ,		__A			  =							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__":
       snake_case__   :				Optional[int]						=	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')
       snake_case__   :				Union[str, Any]						=	parser.parse_args()

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












import os



def _a							(      )      ->		int:







      '''simple docstring'''






      __A			  =							os.path.dirname(os.path.realpath(lowerCamelCase					)					)
      __A			  =							os.path.join(lowerCamelCase   ,							'''triangle.txt'''					)

      with open(lowerCamelCase					) as in_file:
            __A			  =							[[int(lowerCamelCase					) for i in line.split()] for line in in_file]

      while len(lowerCamelCase					) != 1:
            __A			  =							triangle.pop()
            __A			  =							triangle[-1]
            for j in range(len(lowerCamelCase					) - 1					):
                  curr_row[j] += max(last_row[j]   ,							last_row[j + 1]					)
      return triangle[0][0]


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





