


import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask


if is_torch_available():
							import torch

							from transformers import BlipTextModel
							from transformers.models.blip.modeling_blip import BLIP_PRETRAINED_MODEL_ARCHIVE_LIST







class     __lowerCamelCase     :


							"""simple docstring"""

							def __init__(  self						,       UpperCAmelCase						,       UpperCAmelCase=12						,       UpperCAmelCase=7						,       UpperCAmelCase=True						,       UpperCAmelCase=True						,       UpperCAmelCase=True						,       UpperCAmelCase=99						,       UpperCAmelCase=32						,       UpperCAmelCase=32						,       UpperCAmelCase=5						,       UpperCAmelCase=4						,       UpperCAmelCase=37						,       UpperCAmelCase=0.1						,       UpperCAmelCase=0.1						,       UpperCAmelCase=512						,       UpperCAmelCase=0.02						,       UpperCAmelCase=0						,       UpperCAmelCase=None						,       )     ->       str:






								'''simple docstring'''






								lowercase_			 = parent
								lowercase_			 = batch_size
								lowercase_			 = seq_length
								lowercase_			 = is_training
								lowercase_			 = use_input_mask
								lowercase_			 = use_labels
								lowercase_			 = vocab_size
								lowercase_			 = hidden_size
								lowercase_			 = projection_dim
								lowercase_			 = num_hidden_layers
								lowercase_			 = num_attention_heads
								lowercase_			 = intermediate_size
								lowercase_			 = dropout
								lowercase_			 = attention_dropout
								lowercase_			 = max_position_embeddings
								lowercase_			 = initializer_range
								lowercase_			 = scope
								lowercase_			 = bos_token_id




							def 				A__      (  self  )     ->       Any:






								'''simple docstring'''






								lowercase_			 = ids_tensor([self.batch_size, self.seq_length]						,       self.vocab_size  )

								lowercase_			 = None
								if self.use_input_mask:
									lowercase_			 = random_attention_mask([self.batch_size, self.seq_length]  )

								if input_mask is not None:
									lowercase_ ,	lowercase_			 = input_mask.shape
									lowercase_			 = np.random.randint(1						,       seq_length - 1						,       size=(batch_size,)  )
									for batch_idx, start_index in enumerate(UpperCAmelCase  ):
										lowercase_			 = 1
										lowercase_			 = 0

								lowercase_			 = self.get_config()

								return config, input_ids, input_mask




							def 				A__      (  self  )     ->       List[Any]:






								'''simple docstring'''






								return BlipTextConfig(
								    vocab_size=self.vocab_size						,       hidden_size=self.hidden_size						,       projection_dim=self.projection_dim						,       num_hidden_layers=self.num_hidden_layers						,       num_attention_heads=self.num_attention_heads						,       intermediate_size=self.intermediate_size						,       dropout=self.dropout						,       attention_dropout=self.attention_dropout						,       max_position_embeddings=self.max_position_embeddings						,       initializer_range=self.initializer_range						,       bos_token_id=self.bos_token_id						,       )




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase  )     ->       List[Any]:






								'''simple docstring'''






								lowercase_			 = BlipTextModel(config=UpperCAmelCase  )
								model.to(UpperCAmelCase  )
								model.eval()
								with torch.no_grad():
									lowercase_			 = model(UpperCAmelCase						,       attention_mask=UpperCAmelCase  )
									lowercase_			 = model(UpperCAmelCase  )
								self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.hidden_size)  )
								self.parent.assertEqual(result.pooler_output.shape						,       (self.batch_size, self.hidden_size)  )




							def 				A__      (  self  )     ->       List[Any]:






								'''simple docstring'''






								lowercase_			 = self.prepare_config_and_inputs()
								lowercase_ ,	lowercase_ ,	lowercase_			 = config_and_inputs
								lowercase_			 = {"input_ids": input_ids, "attention_mask": input_mask}
								return config, inputs_dict







@require_torch
class     __lowerCamelCase     (  snake_case_							,   unittest.TestCase     ):


							"""simple docstring"""

							lowerCAmelCase__          =				(BlipTextModel,) if is_torch_available() else ()
							lowerCAmelCase__          =				False
							lowerCAmelCase__          =				False
							lowerCAmelCase__          =				False




							def 				A__      (  self  )     ->       Dict:






								'''simple docstring'''






								lowercase_			 = BlipTextModelTester(self  )
								lowercase_			 = ConfigTester(self						,       config_class=UpperCAmelCase						,       hidden_size=37  )




							def 				A__      (  self  )     ->       Optional[int]:






								'''simple docstring'''






								self.config_tester.run_common_tests()




							def 				A__      (  self  )     ->       int:






								'''simple docstring'''






								lowercase_			 = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCAmelCase  )




							def 				A__      (  self  )     ->       Any:






								'''simple docstring'''






								pass




							def 				A__      (  self  )     ->       int:






								'''simple docstring'''






								pass




							@unittest.skip(reason="Blip does not use inputs_embeds"  )
							def 				A__      (  self  )     ->       Dict:






								'''simple docstring'''






								pass




							@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"  )
							def 				A__      (  self  )     ->       Optional[int]:






								'''simple docstring'''






								pass




							@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"  )
							def 				A__      (  self  )     ->       Dict:






								'''simple docstring'''






								pass




							@slow
							def 				A__      (  self  )     ->       Optional[Any]:






								'''simple docstring'''






								for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_			 = BlipTextModel.from_pretrained(UpperCAmelCase  )
									self.assertIsNotNone(UpperCAmelCase  )




							def 				A__      (  self  )     ->       int:






								'''simple docstring'''






								super().test_pt_tf_model_equivalence(allow_missing_keys=UpperCAmelCase  )





import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		Optional[int]  ,			__lowerCamelCase:		Optional[int]  ):







	'''simple docstring'''



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

	lowercase_			 = pt_attn_layer.out_proj.weight
	lowercase_			 = pt_attn_layer.out_proj.bias

	lowercase_			 = q_proj
	lowercase_			 = q_proj_bias

	lowercase_			 = k_proj
	lowercase_			 = k_proj_bias

	lowercase_			 = v_proj
	lowercase_			 = v_proj_bias

	lowercase_			 = out_proj_weights
	lowercase_			 = out_proj_bias




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		int  ,			__lowerCamelCase:		List[str]  ):







	'''simple docstring'''



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




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		List[str]  ,			__lowerCamelCase:		Dict  ):







	'''simple docstring'''



	lowercase_			 = pt_linear.weight
	lowercase_			 = pt_linear.bias




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		int  ,			__lowerCamelCase:		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 SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		str  ,			__lowerCamelCase:		Optional[int]  ):







	'''simple docstring'''



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




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		Optional[Any]  ,			__lowerCamelCase:		str  ):







	'''simple docstring'''



	lowercase_			 = pt_model.token_embedding.weight
	lowercase_			 = 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 SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		Optional[Any]  ,			__lowerCamelCase:		Any  ):







	'''simple docstring'''



	lowercase_			 = pt_model.text_projection.data.T

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




def SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		List[Any]  ,			__lowerCamelCase:		List[Any]  ):







	'''simple docstring'''



	lowercase_			 = 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
	lowercase_			 = pt_model.visual.conva.weight.data
	lowercase_			 = pt_model.visual.class_embedding
	lowercase_			 = 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 SCREAMING_SNAKE_CASE_  (				__lowerCamelCase:		int  ,			__lowerCamelCase:		Dict  ,			__lowerCamelCase:		Dict=None  ):







	'''simple docstring'''



	if config_path is not None:
		lowercase_			 = CLIPConfig.from_pretrained(__lowerCamelCase  )
	else:
		lowercase_			 = CLIPConfig(projection_dim=512  ,			text_config={}  ,			vision_config={}  )

	lowercase_			 = CLIPModel(__lowerCamelCase  ).eval()

	lowercase_ ,	lowercase_			 = load(__lowerCamelCase  ,			device="cpu"  ,			jit=__lowerCamelCase  )
	lowercase_			 = pt_model.eval()

	copy_text_model_and_projection(__lowerCamelCase  ,			__lowerCamelCase  )
	copy_vison_model_and_projection(__lowerCamelCase  ,			__lowerCamelCase  )
	lowercase_			 = pt_model.logit_scale

	lowercase_			 = torch.arange(0  ,			77  ).unsqueeze(0  )
	lowercase_			 = torch.randn(1  ,			3  ,			224  ,			224  )

	lowercase_			 = hf_model(input_ids=__lowerCamelCase  ,			pixel_values=__lowerCamelCase  ,			return_dict=__lowerCamelCase  )
	lowercase_			 = hf_outputs.logits_per_image
	lowercase_			 = hf_outputs.logits_per_text
	lowercase_ ,	lowercase_			 = 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__       			=				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__       			=				parser.parse_args()

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

