






import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      Optional[int]:
 lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_							:    List[str]							=     pt_weights[F'{prefix}.in_proj_weight'].chunk(3 ,			dim=0				)
 lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_							:    Any							=     pt_weights[F'{prefix}.in_proj_bias'].chunk(3 ,			dim=0				)

 lowerCamelCase_							:    List[Any]							=     pt_weights[F'{prefix}.out_proj.weight']
 lowerCamelCase_							:    Any							=     pt_weights[F'{prefix}.out_proj.bias']

 lowerCamelCase_							:    Optional[Any]							=     q_proj
 lowerCamelCase_							:    int							=     q_proj_bias

 lowerCamelCase_							:    str							=     k_proj
 lowerCamelCase_							:    Optional[int]							=     k_proj_bias

 lowerCamelCase_							:    Any							=     v_proj
 lowerCamelCase_							:    List[Any]							=     v_proj_bias

 lowerCamelCase_							:    List[str]							=     out_proj_weights
 lowerCamelCase_							:    Any							=     out_proj_bias




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      int:
 copy_linear(hf_mlp.fca ,			lowerCamelCase_ ,			F'{prefix}.c_fc'				)
 copy_linear(hf_mlp.fca ,			lowerCamelCase_ ,			F'{prefix}.c_proj'				)




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      Dict:
 lowerCamelCase_							:    Any							=     pt_weights[F'{prefix}.weight'].data
 lowerCamelCase_							:    Dict							=     pt_weights[F'{prefix}.bias'].data




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      List[Any]:
 copy_linear(hf_layer.layer_norma ,			lowerCamelCase_ ,			F'{prefix}.ln_1'				)
 copy_linear(hf_layer.layer_norma ,			lowerCamelCase_ ,			F'{prefix}.ln_2'				)

 # copy MLP
 copy_mlp(hf_layer.mlp ,			lowerCamelCase_ ,			F'{prefix}.mlp'				)

 # copy attn
 copy_attn_layer(hf_layer.self_attn ,			lowerCamelCase_ ,			F'{prefix}.attn'				)




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      int:





 for layer_id, hf_layer in enumerate(lowerCamelCase_				):
  copy_layer(lowerCamelCase_ ,			lowerCamelCase_ ,			F'{prefix}.{layer_id}'				)




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      Optional[Any]:
 lowerCamelCase_							:    Tuple							=     pt_weights['text_projection'].data.T

 # copy text encoder
 for name, param in hf_model.text_model.named_parameters():
  lowerCamelCase_							:    Any							=     pt_weights[F'bert.{name}'].data




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      Optional[Any]:
 lowerCamelCase_							:    List[str]							=     pt_weights['visual.proj'].data.T

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

 # copy embeddings
 lowerCamelCase_							:    List[str]							=     pt_weights['visual.conv1.weight'].data
 lowerCamelCase_							:    int							=     pt_weights['visual.class_embedding'].data
 lowerCamelCase_							:    Optional[Any]							=     pt_weights['visual.positional_embedding'].data

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




@torch.no_grad()
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__=None				)  ->      List[Any]:
 assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
 lowerCamelCase_							:    int							=     ChineseCLIPConfig.from_pretrained(lowerCamelCase_				)

 lowerCamelCase_							:    Optional[Any]							=     ChineseCLIPModel(lowerCamelCase_				).eval()

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

 copy_text_model_and_projection(lowerCamelCase_ ,			lowerCamelCase_				)
 copy_vision_model_and_projection(lowerCamelCase_ ,			lowerCamelCase_				)
 lowerCamelCase_							:    str							=     pt_weights['logit_scale'].data

 hf_model.save_pretrained(lowerCamelCase_				)


if __name__ == "__main__":
       UpperCamelCase											=      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.'''
       )
       UpperCamelCase											=      parser.parse_args()

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

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


_SCREAMING_SNAKE_CASE:    List[Any]     						=							logging.get_logger(__name__)

class 						_SCREAMING_SNAKE_CASE				(  _lowercase				):






		"""simple docstring"""







		A_    			=   ['''pixel_values''']



		def __init__(      self     :					Tuple	,       __SCREAMING_SNAKE_CASE     :					bool = True	,       __SCREAMING_SNAKE_CASE     :					Optional[Dict[str, int]] = None	,       __SCREAMING_SNAKE_CASE     :					PILImageResampling = PILImageResampling.BILINEAR	,       __SCREAMING_SNAKE_CASE     :					bool = True	,       __SCREAMING_SNAKE_CASE     :					Union[int, float] = 1 / 255	,       __SCREAMING_SNAKE_CASE     :					bool = True	,       __SCREAMING_SNAKE_CASE     :					Optional[Union[float, List[float]]] = None	,       __SCREAMING_SNAKE_CASE     :					Optional[Union[float, List[float]]] = None	,       **__SCREAMING_SNAKE_CASE     :					str	,       ):






					"""simple docstring"""
					super().__init__(**__SCREAMING_SNAKE_CASE )
					_snake_case														=   size if size is not None else {"height": 224, "width": 224}
					_snake_case														=   get_size_dict(__SCREAMING_SNAKE_CASE )
					_snake_case														=   do_resize
					_snake_case														=   do_rescale
					_snake_case														=   do_normalize
					_snake_case														=   size
					_snake_case														=   resample
					_snake_case														=   rescale_factor
					_snake_case														=   image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
					_snake_case														=   image_std if image_std is not None else IMAGENET_STANDARD_STD



		def 					_a					(      self     :					int	,       __SCREAMING_SNAKE_CASE     :					np.ndarray	,       __SCREAMING_SNAKE_CASE     :					Dict[str, int]	,       __SCREAMING_SNAKE_CASE     :					PILImageResampling = PILImageResampling.BILINEAR	,       __SCREAMING_SNAKE_CASE     :					Optional[Union[str, ChannelDimension]] = None	,       **__SCREAMING_SNAKE_CASE     :					Tuple	,       ):






					"""simple docstring"""
					_snake_case														=   get_size_dict(__SCREAMING_SNAKE_CASE )
					if "height" not in size or "width" not in size:
								raise ValueError(F'The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}' )
					return resize(
					    __SCREAMING_SNAKE_CASE	,       size=(size["height"], size["width"])	,       resample=__SCREAMING_SNAKE_CASE	,       data_format=__SCREAMING_SNAKE_CASE	,       **__SCREAMING_SNAKE_CASE )



		def 					_a					(      self     :					Tuple	,       __SCREAMING_SNAKE_CASE     :					np.ndarray	,       __SCREAMING_SNAKE_CASE     :					float	,       __SCREAMING_SNAKE_CASE     :					Optional[Union[str, ChannelDimension]] = None	,       **__SCREAMING_SNAKE_CASE     :					List[str] ):






					"""simple docstring"""
					return rescale(__SCREAMING_SNAKE_CASE	,       scale=__SCREAMING_SNAKE_CASE	,       data_format=__SCREAMING_SNAKE_CASE	,       **__SCREAMING_SNAKE_CASE )



		def 					_a					(      self     :					Optional[Any]	,       __SCREAMING_SNAKE_CASE     :					np.ndarray	,       __SCREAMING_SNAKE_CASE     :					Union[float, List[float]]	,       __SCREAMING_SNAKE_CASE     :					Union[float, List[float]]	,       __SCREAMING_SNAKE_CASE     :					Optional[Union[str, ChannelDimension]] = None	,       **__SCREAMING_SNAKE_CASE     :					Optional[Any]	,       ):






					"""simple docstring"""
					return normalize(__SCREAMING_SNAKE_CASE	,       mean=__SCREAMING_SNAKE_CASE	,       std=__SCREAMING_SNAKE_CASE	,       data_format=__SCREAMING_SNAKE_CASE	,       **__SCREAMING_SNAKE_CASE )







		def 					_a					(      self     :					int	,       __SCREAMING_SNAKE_CASE     :					ImageInput	,       __SCREAMING_SNAKE_CASE     :					Optional[bool] = None	,       __SCREAMING_SNAKE_CASE     :					Dict[str, int] = None	,       __SCREAMING_SNAKE_CASE     :					PILImageResampling = None	,       __SCREAMING_SNAKE_CASE     :					Optional[bool] = None	,       __SCREAMING_SNAKE_CASE     :					Optional[float] = None	,       __SCREAMING_SNAKE_CASE     :					Optional[bool] = None	,       __SCREAMING_SNAKE_CASE     :					Optional[Union[float, List[float]]] = None	,       __SCREAMING_SNAKE_CASE     :					Optional[Union[float, List[float]]] = None	,       __SCREAMING_SNAKE_CASE     :					Optional[Union[str, TensorType]] = None	,       __SCREAMING_SNAKE_CASE     :					Union[str, ChannelDimension] = ChannelDimension.FIRST	,       **__SCREAMING_SNAKE_CASE     :					List[Any]	,       ):






					"""simple docstring"""
					_snake_case														=   do_resize if do_resize is not None else self.do_resize
					_snake_case														=   do_rescale if do_rescale is not None else self.do_rescale
					_snake_case														=   do_normalize if do_normalize is not None else self.do_normalize
					_snake_case														=   resample if resample is not None else self.resample
					_snake_case														=   rescale_factor if rescale_factor is not None else self.rescale_factor
					_snake_case														=   image_mean if image_mean is not None else self.image_mean
					_snake_case														=   image_std if image_std is not None else self.image_std

					_snake_case														=   size if size is not None else self.size
					_snake_case														=   get_size_dict(__SCREAMING_SNAKE_CASE )

					_snake_case														=   make_list_of_images(__SCREAMING_SNAKE_CASE )

					if not valid_images(__SCREAMING_SNAKE_CASE ):
								raise ValueError(
								    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
								    "torch.Tensor, tf.Tensor or jax.ndarray." )

					if do_resize and size is None:
								raise ValueError("Size must be specified if do_resize is True." )

					if do_rescale and rescale_factor is None:
								raise ValueError("Rescale factor must be specified if do_rescale is True." )

					# All transformations expect numpy arrays.
					_snake_case														=   [to_numpy_array(__SCREAMING_SNAKE_CASE ) for image in images]

					if do_resize:
								_snake_case														=   [self.resize(image=__SCREAMING_SNAKE_CASE	,       size=__SCREAMING_SNAKE_CASE	,       resample=__SCREAMING_SNAKE_CASE ) for image in images]

					if do_rescale:
								_snake_case														=   [self.rescale(image=__SCREAMING_SNAKE_CASE	,       scale=__SCREAMING_SNAKE_CASE ) for image in images]

					if do_normalize:
								_snake_case														=   [self.normalize(image=__SCREAMING_SNAKE_CASE	,       mean=__SCREAMING_SNAKE_CASE	,       std=__SCREAMING_SNAKE_CASE ) for image in images]

					_snake_case														=   [to_channel_dimension_format(__SCREAMING_SNAKE_CASE	,       __SCREAMING_SNAKE_CASE ) for image in images]

					_snake_case														=   {"pixel_values": images}
					return BatchFeature(data=__SCREAMING_SNAKE_CASE	,       tensor_type=__SCREAMING_SNAKE_CASE )

