




'''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'''



from typing import Any

import numpy as np







def 			_UpperCamelCase (_lowerCamelCase							:     np.ndarray	)->   bool:




     '''simple docstring'''





     assert np.shape(_lowerCamelCase	)[0] == np.shape(_lowerCamelCase	)[1]

     # If matrix not symmetric, exit right away.
     if np.allclose(_lowerCamelCase   ,	matrix.T	) is False:
          return False

     # Get eigenvalues and eignevectors for a symmetric matrix.
     __snake_case					,    __snake_case    					=  np.linalg.eigh(_lowerCamelCase	)

     # Check sign of all eigenvalues.
     # np.all returns a value of type np.bool_
     return bool(np.all(eigen_values > 0	)	)







def 			_UpperCamelCase (_lowerCamelCase							:     int	)->   Any:




     '''simple docstring'''





     __snake_case    					=  np.random.randn(_lowerCamelCase   ,	_lowerCamelCase	)
     __snake_case    					=  np.dot(_lowerCamelCase   ,	random_matrix.T	)
     assert _is_matrix_spd(_lowerCamelCase	)
     return spd_matrix







def 			_UpperCamelCase (_lowerCamelCase							:     np.ndarray   ,	_lowerCamelCase							:     np.ndarray   ,	_lowerCamelCase							:     int = 10_00   ,	_lowerCamelCase							:     float = 1E-8   ,	)->   Any:




     '''simple docstring'''





     assert np.shape(_lowerCamelCase	)[0] == np.shape(_lowerCamelCase	)[1]
     assert np.shape(_lowerCamelCase	)[0] == np.shape(_lowerCamelCase	)[0]
     assert _is_matrix_spd(_lowerCamelCase	)

     # Initialize solution guess, residual, search direction.
     __snake_case    					=  np.zeros((np.shape(_lowerCamelCase	)[0], 1)	)
     __snake_case    					=  np.copy(_lowerCamelCase	)
     __snake_case    					=  np.copy(_lowerCamelCase	)

     # Set initial errors in solution guess and residual.
     __snake_case    					=  1E9
     __snake_case    					=  1E9
     __snake_case    					=  1E9

     # Set iteration counter to threshold number of iterations.
     __snake_case    					=  0

     while error > tol:
          # Save this value so we only calculate the matrix-vector product once.
          __snake_case    					=  np.dot(_lowerCamelCase   ,	_lowerCamelCase	)

          # The main algorithm.

          # Update search direction magnitude.
          __snake_case    					=  np.dot(ra.T   ,	_lowerCamelCase	) / np.dot(pa.T   ,	_lowerCamelCase	)
          # Update solution guess.
          __snake_case    					=  xa + alpha * pa
          # Calculate new residual.
          __snake_case    					=  ra - alpha * w
          # Calculate new Krylov subspace scale.
          __snake_case    					=  np.dot(r.T   ,	_lowerCamelCase	) / np.dot(ra.T   ,	_lowerCamelCase	)
          # Calculate new A conjuage search direction.
          __snake_case    					=  r + beta * pa

          # Calculate errors.
          __snake_case    					=  np.linalg.norm(r - ra	)
          __snake_case    					=  np.linalg.norm(x - xa	)
          __snake_case    					=  np.maximum(_lowerCamelCase   ,	_lowerCamelCase	)

          # Update variables.
          __snake_case    					=  np.copy(_lowerCamelCase	)
          __snake_case    					=  np.copy(_lowerCamelCase	)
          __snake_case    					=  np.copy(_lowerCamelCase	)

          # Update number of iterations.
          iterations += 1
          if iterations > max_iterations:
               break

     return x







def 			_UpperCamelCase ()->   None:




     '''simple docstring'''





     __snake_case    					=  3
     __snake_case    					=  _create_spd_matrix(_lowerCamelCase	)
     __snake_case    					=  np.random.randn(_lowerCamelCase   ,	1	)
     __snake_case    					=  np.dot(_lowerCamelCase   ,	_lowerCamelCase	)

     # Numpy solution.
     __snake_case    					=  np.linalg.solve(_lowerCamelCase   ,	_lowerCamelCase	)

     # Our implementation.
     __snake_case    					=  conjugate_gradient(_lowerCamelCase   ,	_lowerCamelCase	)

     # Ensure both solutions are close to x_true (and therefore one another).
     assert np.linalg.norm(x_numpy - x_true	) <= 1E-6
     assert np.linalg.norm(x_conjugate_gradient - x_true	) <= 1E-6


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     test_conjugate_gradient()






