code: 
  
  
  import argparse
  
  import torch
  
  from transformers import ChineseCLIPConfig, ChineseCLIPModel
  
  
  
  
  
  
  
  def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__			)		->			Union[str, Any]:
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						__lowercase,							__lowercase,							__lowercase=    pt_weights[F'{prefix}.in_proj_weight'].chunk(3     ,				dim=0			)
  						__lowercase,							__lowercase,							__lowercase=    pt_weights[F'{prefix}.in_proj_bias'].chunk(3     ,				dim=0			)
  
  						__lowercase=    pt_weights[F'{prefix}.out_proj.weight']
  						__lowercase=    pt_weights[F'{prefix}.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  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__			)		->			List[str]:
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						copy_linear(hf_mlp.fca     ,				lowercase__     ,				F'{prefix}.c_fc'			)
  						copy_linear(hf_mlp.fca     ,				lowercase__     ,				F'{prefix}.c_proj'			)
  
  
  
  
  
  
  
  def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__			)		->			List[str]:
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						__lowercase=    pt_weights[F'{prefix}.weight'].data
  						__lowercase=    pt_weights[F'{prefix}.bias'].data
  
  
  
  
  
  
  
  def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__			)		->			str:
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						copy_linear(hf_layer.layer_norma     ,				lowercase__     ,				F'{prefix}.ln_1'			)
  						copy_linear(hf_layer.layer_norma     ,				lowercase__     ,				F'{prefix}.ln_2'			)
  
  						# copy MLP
  						copy_mlp(hf_layer.mlp     ,				lowercase__     ,				F'{prefix}.mlp'			)
  
  						# copy attn
  						copy_attn_layer(hf_layer.self_attn     ,				lowercase__     ,				F'{prefix}.attn'			)
  
  
  
  
  
  
  
  def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__			)		->			Union[str, Any]:
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						for layer_id, hf_layer in enumerate(lowercase__			):
  												copy_layer(lowercase__     ,				lowercase__     ,				F'{prefix}.{layer_id}'			)
  
  
  
  
  
  
  
  def  _lowerCamelCase(			lowercase__     ,				lowercase__			)		->			Union[str, Any]:
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						__lowercase=    pt_weights['text_projection'].data.T
  
  						# copy text encoder
  						for name, param in hf_model.text_model.named_parameters():
  												__lowercase=    pt_weights[F'bert.{name}'].data
  
  
  
  
  
  
  
  def  _lowerCamelCase(			lowercase__     ,				lowercase__			)		->			Optional[int]:
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						__lowercase=    pt_weights['visual.proj'].data.T
  
  						# copy layer norms
  						copy_linear(hf_model.vision_model.pre_layrnorm     ,				lowercase__     ,				'visual.ln_pre'			)
  						copy_linear(hf_model.vision_model.post_layernorm     ,				lowercase__     ,				'visual.ln_post'			)
  
  						# copy embeddings
  						__lowercase=    pt_weights['visual.conv1.weight'].data
  						__lowercase=    pt_weights['visual.class_embedding'].data
  						__lowercase=    pt_weights['visual.positional_embedding'].data
  
  						# copy encoder
  						copy_layers(hf_model.vision_model.encoder.layers     ,				lowercase__     ,				'visual.transformer.resblocks'			)
  
  
  
  
  
  
  
  @torch.no_grad()
  def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__=None			)		->			Any:
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
  						__lowercase=    ChineseCLIPConfig.from_pretrained(lowercase__			)
  
  						__lowercase=    ChineseCLIPModel(lowercase__			).eval()
  
  						__lowercase=    torch.load(lowercase__     ,				map_location='cpu'			)['state_dict']
  						__lowercase=    {(name[7:] if name.startswith('module.'			) else name): value for name, value in pt_weights.items()}
  
  						copy_text_model_and_projection(lowercase__     ,				lowercase__			)
  						copy_vision_model_and_projection(lowercase__     ,				lowercase__			)
  						__lowercase=    pt_weights['logit_scale'].data
  
  						hf_model.save_pretrained(lowercase__			)
  
  
  if __name__ == "__main__":
  	lowerCAmelCase					 =	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.'''
  	)
  	lowerCAmelCase					 =	parser.parse_args()
  
  	convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
  	print('''The conversion is finished!''')
  
  
  
  
  
  
  
style_context: 
  
  
  from dataclasses import dataclass
  from typing import List, Optional, Union
  
  import numpy as np
  import PIL
  from PIL import Image
  
  from ...utils import BaseOutput, is_torch_available, is_transformers_available
  
  
  
  
  
  @dataclass
  # Copied from diffusers.pipelines.stable_diffusion.__init__.StableDiffusionPipelineOutput with Stable->Alt
  class    A					(     A_				):
  							UpperCamelCase_     :							Union[List[PIL.Image.Image], np.ndarray]
  							UpperCamelCase_     :							Optional[List[bool]]
  
  
  if is_transformers_available() and is_torch_available():
  	from .modeling_roberta_series import RobertaSeriesModelWithTransformation
  	from .pipeline_alt_diffusion import AltDiffusionPipeline
  	from .pipeline_alt_diffusion_imgaimg import AltDiffusionImgaImgPipeline
  
  
  
  
  
  
  