




'''simple docstring'''



import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel







def 			_UpperCamelCase (_lowerCamelCase							:     List[Any]   ,	_lowerCamelCase							:     Tuple	)->   str:




     '''simple docstring'''





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

     __snake_case    					=  pt_attn_layer.out_proj.weight
     __snake_case    					=  pt_attn_layer.out_proj.bias

     __snake_case    					=  q_proj
     __snake_case    					=  q_proj_bias

     __snake_case    					=  k_proj
     __snake_case    					=  k_proj_bias

     __snake_case    					=  v_proj
     __snake_case    					=  v_proj_bias

     __snake_case    					=  out_proj_weights
     __snake_case    					=  out_proj_bias







def 			_UpperCamelCase (_lowerCamelCase							:     Dict   ,	_lowerCamelCase							:     Optional[Any]	)->   Dict:




     '''simple docstring'''





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







def 			_UpperCamelCase (_lowerCamelCase							:     Any   ,	_lowerCamelCase							:     Any	)->   List[str]:




     '''simple docstring'''





     __snake_case    					=  pt_linear.weight
     __snake_case    					=  pt_linear.bias







def 			_UpperCamelCase (_lowerCamelCase							:     int   ,	_lowerCamelCase							:     Union[str, Any]	)->   Dict:




     '''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 			_UpperCamelCase (_lowerCamelCase							:     Optional[Any]   ,	_lowerCamelCase							:     Tuple	)->   Union[str, Any]:




     '''simple docstring'''





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







def 			_UpperCamelCase (_lowerCamelCase							:     Tuple   ,	_lowerCamelCase							:     Union[str, Any]	)->   Optional[int]:




     '''simple docstring'''





     __snake_case    					=  pt_model.token_embedding.weight
     __snake_case    					=  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 			_UpperCamelCase (_lowerCamelCase							:     Dict   ,	_lowerCamelCase							:     Dict	)->   List[Any]:




     '''simple docstring'''





     __snake_case    					=  pt_model.text_projection.data.T

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







def 			_UpperCamelCase (_lowerCamelCase							:     Dict   ,	_lowerCamelCase							:     Optional[Any]	)->   str:




     '''simple docstring'''





     __snake_case    					=  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
     __snake_case    					=  pt_model.visual.conva.weight.data
     __snake_case    					=  pt_model.visual.class_embedding
     __snake_case    					=  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 			_UpperCamelCase (_lowerCamelCase							:     Optional[Any]   ,	_lowerCamelCase							:     str   ,	_lowerCamelCase							:     Tuple=None	)->   Optional[Any]:




     '''simple docstring'''





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

     __snake_case    					=  CLIPModel(_lowerCamelCase	).eval()

     __snake_case					,    __snake_case    					=  load(_lowerCamelCase   ,	device='''cpu'''   ,	jit=_lowerCamelCase	)
     __snake_case    					=  pt_model.eval()

     copy_text_model_and_projection(_lowerCamelCase   ,	_lowerCamelCase	)
     copy_vison_model_and_projection(_lowerCamelCase   ,	_lowerCamelCase	)
     __snake_case    					=  pt_model.logit_scale

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

     __snake_case    					=  hf_model(input_ids=_lowerCamelCase   ,	pixel_values=_lowerCamelCase   ,	return_dict=_lowerCamelCase	)
     __snake_case    					=  hf_outputs.logits_per_image
     __snake_case    					=  hf_outputs.logits_per_text
     __snake_case					,    __snake_case    					=  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__":
     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_       :  Union[str, Any]      =		parser.parse_args()

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












'''simple docstring'''



# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from typing import List







def 			_UpperCamelCase (_lowerCamelCase							:     List[str]   ,	_lowerCamelCase							:     str	)->   str:




     '''simple docstring'''





     if name != "training_function" and name != "main":
          raise ValueError(f'''Incorrect function name passed: {name}, choose either \'main\' or \'training_function\''''	)
     __snake_case					,    __snake_case    					=  [], False
     for line in lines:
          if not found_start and f'''def {name}''' in line:
               __snake_case    					=  True
               good_lines.append(_lowerCamelCase	)
               continue
          if found_start:
               if name == "training_function" and "def main" in line:
                    return good_lines
               if name == "main" and "if __name__" in line:
                    return good_lines
               good_lines.append(_lowerCamelCase	)







def 			_UpperCamelCase (_lowerCamelCase							:     List[str]	)->   int:




     '''simple docstring'''





     return [line for line in lines if not line.lstrip().startswith('''#'''	) and line != "\n"]







def 			_UpperCamelCase (_lowerCamelCase							:     str   ,	_lowerCamelCase							:     str   ,	_lowerCamelCase							:     bool   ,	_lowerCamelCase							:     str = None	)->   Optional[Any]:




     '''simple docstring'''





     with open(_lowerCamelCase   ,	'''r'''	) as f:
          __snake_case    					=  f.readlines()
     with open(os.path.abspath(os.path.join('''examples'''   ,	'''nlp_example.py'''	)	)   ,	'''r'''	) as f:
          __snake_case    					=  f.readlines()
     with open(_lowerCamelCase   ,	'''r'''	) as f:
          __snake_case    					=  f.readlines()
     if secondary_filename is not None:
          with open(_lowerCamelCase   ,	'''r'''	) as f:
               __snake_case    					=  f.readlines()

    # This is our base, we remove all the code from here in our `full_filename` and `feature_filename` to find the new content
     if parser_only:
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''main'''	)	)
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''main'''	)	)
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''main'''	)	)
          if secondary_filename is not None:
               __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''main'''	)	)
     else:
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''training_function'''	)	)
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''training_function'''	)	)
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''training_function'''	)	)
          if secondary_filename is not None:
               __snake_case    					=  clean_lines(
                   get_function_contents_by_name(_lowerCamelCase   ,	'''training_function'''	)	)

     __snake_case    					=  '''train_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)\n'''

     # Specific code in our script that differs from the full version, aka what is new
     __snake_case    					=  []
     __snake_case    					=  []  # We keep track of the idxs just in case it's a repeated statement
     __snake_case    					=  iter(_lowerCamelCase	)
     for i in range(len(_lowerCamelCase	) - 1	):
          if i not in passed_idxs:
               __snake_case    					=  next(_lowerCamelCase	)
               if (line not in full_file_func) and (line.lstrip() != _dl_line):
                    if "TESTING_MOCKED_DATALOADERS" not in line:
                         new_feature_code.append(_lowerCamelCase	)
                         passed_idxs.append(_lowerCamelCase	)
                    else:
                         # Skip over the `config['num_epochs'] = 2` statement
                         __snake_case    					=  next(_lowerCamelCase	)

    # Extract out just the new parts from the full_file_training_func
     __snake_case    					=  []
     __snake_case    					=  []  # We keep track of the idxs just in case it's a repeated statement
     for i, line in enumerate(_lowerCamelCase	):
          if i not in passed_idxs:
               if (line not in full_file_func) and (line.lstrip() != _dl_line):
                    if "TESTING_MOCKED_DATALOADERS" not in line:
                         new_full_example_parts.append(_lowerCamelCase	)
                         passed_idxs.append(_lowerCamelCase	)

    # Finally, get the overall diff
     __snake_case    					=  [line for line in new_feature_code if line not in new_full_example_parts]
     if secondary_filename is not None:
          __snake_case    					=  [line for line in full_file_contents if line not in secondary_file_func]
          __snake_case    					=  [line for line in diff_from_example if line not in diff_from_two]

     return diff_from_example






