


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import CLIPImageProcessor

class   A__							(						unittest.TestCase					):




						def __init__(    self   : Optional[int]			,      _a   : int			,      _a   : List[str]=7			,      _a   : Optional[int]=3			,      _a   : int=18			,      _a   : Union[str, Any]=30			,      _a   : Optional[Any]=400			,      _a   : List[Any]=True			,      _a   : List[Any]=None			,      _a   : Optional[Any]=True			,      _a   : Union[str, Any]=None			,      _a   : str=True			,      _a   : Tuple=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]			,      _a   : Tuple=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]			,      _a   : Dict=True			,      )							->       Dict:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=size if size is not None else {'''shortest_edge''': 20}
										_SCREAMING_SNAKE_CASE     					=crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
										_SCREAMING_SNAKE_CASE     					=parent
										_SCREAMING_SNAKE_CASE     					=batch_size
										_SCREAMING_SNAKE_CASE     					=num_channels
										_SCREAMING_SNAKE_CASE     					=image_size
										_SCREAMING_SNAKE_CASE     					=min_resolution
										_SCREAMING_SNAKE_CASE     					=max_resolution
										_SCREAMING_SNAKE_CASE     					=do_resize
										_SCREAMING_SNAKE_CASE     					=size
										_SCREAMING_SNAKE_CASE     					=do_center_crop
										_SCREAMING_SNAKE_CASE     					=crop_size
										_SCREAMING_SNAKE_CASE     					=do_normalize
										_SCREAMING_SNAKE_CASE     					=image_mean
										_SCREAMING_SNAKE_CASE     					=image_std
										_SCREAMING_SNAKE_CASE     					=do_convert_rgb




						def 							__UpperCamelCase			(    self   : Tuple					)							->       int:


										"""simple docstring"""





										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_center_crop": self.do_center_crop,
										    "crop_size": self.crop_size,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_convert_rgb": self.do_convert_rgb,
										}




						def 							__UpperCamelCase			(    self   : List[str]			,      _a   : Any=False			,      _a   : int=False			,      _a   : List[str]=False					)							->       Optional[int]:


										"""simple docstring"""





										assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

										if equal_resolution:
														_SCREAMING_SNAKE_CASE     					=[]
														for i in range(self.batch_size					):
																		image_inputs.append(
																		    np.random.randint(
																		        255			,      size=(self.num_channels, self.max_resolution, self.max_resolution)			,      dtype=np.uinta					)					)
										else:
														_SCREAMING_SNAKE_CASE     					=[]
														for i in range(self.batch_size					):
																		_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE     					=np.random.choice(np.arange(self.min_resolution			,      self.max_resolution					)			,      2					)
																		image_inputs.append(np.random.randint(255			,      size=(self.num_channels, width, height)			,      dtype=np.uinta					)					)

										if not numpify and not torchify:
														# PIL expects the channel dimension as last dimension
														_SCREAMING_SNAKE_CASE     					=[Image.fromarray(np.moveaxis(_a			,      0			,      -1					)					) for x in image_inputs]

										if torchify:
														_SCREAMING_SNAKE_CASE     					=[torch.from_numpy(_a					) for x in image_inputs]

										return image_inputs

@require_torch
@require_vision
class   A__							(						UpperCamelCase__				,       unittest.TestCase					):
						UpperCAmelCase     =  CLIPImageProcessor if is_vision_available() else None




						def 							__UpperCamelCase			(    self   : Optional[Any]					)							->       Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=CLIPImageProcessingTester(self					)




						@property
						def 							__UpperCamelCase			(    self   : str					)							->       str:


										"""simple docstring"""





										return self.image_processor_tester.prepare_image_processor_dict()




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.image_processing_class(**self.image_processor_dict					)
										self.assertTrue(hasattr(_a			,      '''do_resize'''					)					)
										self.assertTrue(hasattr(_a			,      '''size'''					)					)
										self.assertTrue(hasattr(_a			,      '''do_center_crop'''					)					)
										self.assertTrue(hasattr(_a			,      '''center_crop'''					)					)
										self.assertTrue(hasattr(_a			,      '''do_normalize'''					)					)
										self.assertTrue(hasattr(_a			,      '''image_mean'''					)					)
										self.assertTrue(hasattr(_a			,      '''image_std'''					)					)
										self.assertTrue(hasattr(_a			,      '''do_convert_rgb'''					)					)




						def 							__UpperCamelCase			(    self   : str					)							->       Dict:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.image_processing_class.from_dict(self.image_processor_dict					)
										self.assertEqual(image_processor.size			,      {'''shortest_edge''': 20}					)
										self.assertEqual(image_processor.crop_size			,      {'''height''': 18, '''width''': 18}					)

										_SCREAMING_SNAKE_CASE     					=self.image_processing_class.from_dict(self.image_processor_dict			,      size=42			,      crop_size=84					)
										self.assertEqual(image_processor.size			,      {'''shortest_edge''': 42}					)
										self.assertEqual(image_processor.crop_size			,      {'''height''': 84, '''width''': 84}					)




						def 							__UpperCamelCase			(    self   : str					)							->       int:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase			(    self   : Dict					)							->       Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.image_processing_class(**self.image_processor_dict					)
										# create random PIL images
										_SCREAMING_SNAKE_CASE     					=self.image_processor_tester.prepare_inputs(equal_resolution=_a					)
										for image in image_inputs:
														self.assertIsInstance(_a			,      Image.Image					)

										# Test not batched input
										_SCREAMING_SNAKE_CASE     					=image_processing(image_inputs[0]			,      return_tensors='''pt'''					).pixel_values
										self.assertEqual(
										    encoded_images.shape			,      (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,      )

										# Test batched
										_SCREAMING_SNAKE_CASE     					=image_processing(_a			,      return_tensors='''pt'''					).pixel_values
										self.assertEqual(
										    encoded_images.shape			,      (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,      )




						def 							__UpperCamelCase			(    self   : Union[str, Any]					)							->       Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.image_processing_class(**self.image_processor_dict					)
										# create random numpy tensors
										_SCREAMING_SNAKE_CASE     					=self.image_processor_tester.prepare_inputs(equal_resolution=_a			,      numpify=_a					)
										for image in image_inputs:
														self.assertIsInstance(_a			,      np.ndarray					)

										# Test not batched input
										_SCREAMING_SNAKE_CASE     					=image_processing(image_inputs[0]			,      return_tensors='''pt'''					).pixel_values
										self.assertEqual(
										    encoded_images.shape			,      (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,      )

										# Test batched
										_SCREAMING_SNAKE_CASE     					=image_processing(_a			,      return_tensors='''pt'''					).pixel_values
										self.assertEqual(
										    encoded_images.shape			,      (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,      )




						def 							__UpperCamelCase			(    self   : Optional[Any]					)							->       str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.image_processing_class(**self.image_processor_dict					)
										# create random PyTorch tensors
										_SCREAMING_SNAKE_CASE     					=self.image_processor_tester.prepare_inputs(equal_resolution=_a			,      torchify=_a					)
										for image in image_inputs:
														self.assertIsInstance(_a			,      torch.Tensor					)

										# Test not batched input
										_SCREAMING_SNAKE_CASE     					=image_processing(image_inputs[0]			,      return_tensors='''pt'''					).pixel_values
										self.assertEqual(
										    encoded_images.shape			,      (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,      )

										# Test batched
										_SCREAMING_SNAKE_CASE     					=image_processing(_a			,      return_tensors='''pt'''					).pixel_values
										self.assertEqual(
										    encoded_images.shape			,      (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,      )

@require_torch
@require_vision
class   A__							(						UpperCamelCase__				,       unittest.TestCase					):
						UpperCAmelCase     =  CLIPImageProcessor if is_vision_available() else None




						def 							__UpperCamelCase			(    self   : Dict					)							->       Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=CLIPImageProcessingTester(self			,      num_channels=4					)
										_SCREAMING_SNAKE_CASE     					=3




						@property
						def 							__UpperCamelCase			(    self   : int					)							->       List[Any]:


										"""simple docstring"""





										return self.image_processor_tester.prepare_image_processor_dict()




						def 							__UpperCamelCase			(    self   : Any					)							->       str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.image_processing_class(**self.image_processor_dict					)
										self.assertTrue(hasattr(_a			,      '''do_resize'''					)					)
										self.assertTrue(hasattr(_a			,      '''size'''					)					)
										self.assertTrue(hasattr(_a			,      '''do_center_crop'''					)					)
										self.assertTrue(hasattr(_a			,      '''center_crop'''					)					)
										self.assertTrue(hasattr(_a			,      '''do_normalize'''					)					)
										self.assertTrue(hasattr(_a			,      '''image_mean'''					)					)
										self.assertTrue(hasattr(_a			,      '''image_std'''					)					)
										self.assertTrue(hasattr(_a			,      '''do_convert_rgb'''					)					)




						def 							__UpperCamelCase			(    self   : str					)							->       str:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase			(    self   : str					)							->       List[str]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.image_processing_class(**self.image_processor_dict					)
										# create random PIL images
										_SCREAMING_SNAKE_CASE     					=self.image_processor_tester.prepare_inputs(equal_resolution=_a					)
										for image in image_inputs:
														self.assertIsInstance(_a			,      Image.Image					)

										# Test not batched input
										_SCREAMING_SNAKE_CASE     					=image_processing(image_inputs[0]			,      return_tensors='''pt'''					).pixel_values
										self.assertEqual(
										    encoded_images.shape			,      (
										        1,
										        self.expected_encoded_image_num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,      )

										# Test batched
										_SCREAMING_SNAKE_CASE     					=image_processing(_a			,      return_tensors='''pt'''					).pixel_values
										self.assertEqual(
										    encoded_images.shape			,      (
										        self.image_processor_tester.batch_size,
										        self.expected_encoded_image_num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,      )



import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel







def   lowerCamelCase(				a__		,a__		,a__):
				_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE     					=pt_weights[f"{prefix}.in_proj_weight"].chunk(3		,dim=0)
				_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE     					=pt_weights[f"{prefix}.in_proj_bias"].chunk(3		,dim=0)

				_SCREAMING_SNAKE_CASE     					=pt_weights[f"{prefix}.out_proj.weight"]
				_SCREAMING_SNAKE_CASE     					=pt_weights[f"{prefix}.out_proj.bias"]

				_SCREAMING_SNAKE_CASE     					=q_proj
				_SCREAMING_SNAKE_CASE     					=q_proj_bias

				_SCREAMING_SNAKE_CASE     					=k_proj
				_SCREAMING_SNAKE_CASE     					=k_proj_bias

				_SCREAMING_SNAKE_CASE     					=v_proj
				_SCREAMING_SNAKE_CASE     					=v_proj_bias

				_SCREAMING_SNAKE_CASE     					=out_proj_weights
				_SCREAMING_SNAKE_CASE     					=out_proj_bias







def   lowerCamelCase(				a__		,a__		,a__):
				copy_linear(hf_mlp.fca		,a__		,f"{prefix}.c_fc")
				copy_linear(hf_mlp.fca		,a__		,f"{prefix}.c_proj")







def   lowerCamelCase(				a__		,a__		,a__):
				_SCREAMING_SNAKE_CASE     					=pt_weights[f"{prefix}.weight"].data
				_SCREAMING_SNAKE_CASE     					=pt_weights[f"{prefix}.bias"].data







def   lowerCamelCase(				a__		,a__		,a__):
				# copy layer norms
				copy_linear(hf_layer.layer_norma		,a__		,f"{prefix}.ln_1")
				copy_linear(hf_layer.layer_norma		,a__		,f"{prefix}.ln_2")

				# copy MLP
				copy_mlp(hf_layer.mlp		,a__		,f"{prefix}.mlp")

				# copy attn
				copy_attn_layer(hf_layer.self_attn		,a__		,f"{prefix}.attn")







def   lowerCamelCase(				a__		,a__		,a__):
				for layer_id, hf_layer in enumerate(a__):
								copy_layer(a__		,a__		,f"{prefix}.{layer_id}")







def   lowerCamelCase(				a__		,a__):
				# copy projection
				_SCREAMING_SNAKE_CASE     					=pt_weights['''text_projection'''].data.T

				# copy text encoder
				for name, param in hf_model.text_model.named_parameters():
								_SCREAMING_SNAKE_CASE     					=pt_weights[f"bert.{name}"].data







def   lowerCamelCase(				a__		,a__):
				# copy projection
				_SCREAMING_SNAKE_CASE     					=pt_weights['''visual.proj'''].data.T

				# copy layer norms
				copy_linear(hf_model.vision_model.pre_layrnorm		,a__		,'''visual.ln_pre''')
				copy_linear(hf_model.vision_model.post_layernorm		,a__		,'''visual.ln_post''')

				# copy embeddings
				_SCREAMING_SNAKE_CASE     					=pt_weights['''visual.conv1.weight'''].data
				_SCREAMING_SNAKE_CASE     					=pt_weights['''visual.class_embedding'''].data
				_SCREAMING_SNAKE_CASE     					=pt_weights['''visual.positional_embedding'''].data

				# copy encoder
				copy_layers(hf_model.vision_model.encoder.layers		,a__		,'''visual.transformer.resblocks''')







@torch.no_grad()
def   lowerCamelCase(				a__		,a__		,a__=None):
				assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
				_SCREAMING_SNAKE_CASE     					=ChineseCLIPConfig.from_pretrained(a__)

				_SCREAMING_SNAKE_CASE     					=ChineseCLIPModel(a__).eval()

				_SCREAMING_SNAKE_CASE     					=torch.load(a__		,map_location='''cpu''')['''state_dict''']
				_SCREAMING_SNAKE_CASE     					={(name[7:] if name.startswith('''module.''') else name): value for name, value in pt_weights.items()}

				copy_text_model_and_projection(a__		,a__)
				copy_vision_model_and_projection(a__		,a__)
				_SCREAMING_SNAKE_CASE     					=pt_weights['''logit_scale'''].data

				hf_model.save_pretrained(a__)


if __name__ == "__main__":
							snake_case_  :     Tuple							  =							argparse.ArgumentParser()
							parser.add_argument(
							    '''--pytorch_dump_folder_path''',
							    default=None,
							    type=str,
							    help='''Path to the output folder storing converted hf PyTorch model.''',
							)
							parser.add_argument(
							    '''--checkpoint_path''', default=None, type=str, help='''Path to original github format ChineseCLIP checkpoint.'''
							)
							parser.add_argument(
							    '''--config_path''', default=None, required=True, type=str, help='''Path to hf config.json of model to convert.'''
							)
							snake_case_  :     List[str]							  =							parser.parse_args()

							convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
							print('''The conversion is finished!''')