


'''simple docstring'''

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					int     ,		_UpperCamelCase   :					List[str]     ,		_UpperCamelCase   :					List[str] )		->       List[str]:





   '''simple docstring'''







   UpperCamelCase__    ,		UpperCamelCase__    ,		UpperCamelCase__			    =    pt_weights[F'{prefix}.in_proj_weight'].chunk(3     ,		dim=0 )
   UpperCamelCase__    ,		UpperCamelCase__    ,		UpperCamelCase__			    =    pt_weights[F'{prefix}.in_proj_bias'].chunk(3     ,		dim=0 )

   UpperCamelCase__			    =    pt_weights[F'{prefix}.out_proj.weight']
   UpperCamelCase__			    =    pt_weights[F'{prefix}.out_proj.bias']

   UpperCamelCase__			    =    q_proj
   UpperCamelCase__			    =    q_proj_bias

   UpperCamelCase__			    =    k_proj
   UpperCamelCase__			    =    k_proj_bias

   UpperCamelCase__			    =    v_proj
   UpperCamelCase__			    =    v_proj_bias

   UpperCamelCase__			    =    out_proj_weights
   UpperCamelCase__			    =    out_proj_bias



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Union[str, Any]     ,		_UpperCamelCase   :					List[str]     ,		_UpperCamelCase   :					Dict )		->       Tuple:





   '''simple docstring'''







   copy_linear(hf_mlp.fca     ,		_UpperCamelCase     ,		F'{prefix}.c_fc' )
   copy_linear(hf_mlp.fca     ,		_UpperCamelCase     ,		F'{prefix}.c_proj' )



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Tuple     ,		_UpperCamelCase   :					str     ,		_UpperCamelCase   :					Optional[int] )		->       Any:





   '''simple docstring'''







   UpperCamelCase__			    =    pt_weights[F'{prefix}.weight'].data
   UpperCamelCase__			    =    pt_weights[F'{prefix}.bias'].data



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					int     ,		_UpperCamelCase   :					List[Any]     ,		_UpperCamelCase   :					Any )		->       Union[str, Any]:





   '''simple docstring'''







   copy_linear(hf_layer.layer_norma     ,		_UpperCamelCase     ,		F'{prefix}.ln_1' )
   copy_linear(hf_layer.layer_norma     ,		_UpperCamelCase     ,		F'{prefix}.ln_2' )

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

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



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Tuple     ,		_UpperCamelCase   :					Optional[int]     ,		_UpperCamelCase   :					Optional[int] )		->       List[Any]:





   '''simple docstring'''







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



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Tuple     ,		_UpperCamelCase   :					Dict )		->       Tuple:





   '''simple docstring'''







   UpperCamelCase__			    =    pt_weights["text_projection"].data.T

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



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					List[str]     ,		_UpperCamelCase   :					Dict )		->       List[Any]:





   '''simple docstring'''







   UpperCamelCase__			    =    pt_weights["visual.proj"].data.T

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

   # copy embeddings
   UpperCamelCase__			    =    pt_weights["visual.conv1.weight"].data
   UpperCamelCase__			    =    pt_weights["visual.class_embedding"].data
   UpperCamelCase__			    =    pt_weights["visual.positional_embedding"].data

   # copy encoder
   copy_layers(hf_model.vision_model.encoder.layers     ,		_UpperCamelCase     ,		"visual.transformer.resblocks" )



@torch.no_grad()
def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					int     ,		_UpperCamelCase   :					Union[str, Any]     ,		_UpperCamelCase   :					Any=None )		->       List[str]:





   '''simple docstring'''







   assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
   UpperCamelCase__			    =    ChineseCLIPConfig.from_pretrained(_UpperCamelCase )

   UpperCamelCase__			    =    ChineseCLIPModel(_UpperCamelCase ).eval()

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

   copy_text_model_and_projection(_UpperCamelCase     ,		_UpperCamelCase )
   copy_vision_model_and_projection(_UpperCamelCase     ,		_UpperCamelCase )
   UpperCamelCase__			    =    pt_weights["logit_scale"].data

   hf_model.save_pretrained(_UpperCamelCase )


if __name__ == "__main__":
       __lowercase:		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."
       )
       __lowercase:		Optional[int]    						=  parser.parse_args()

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







"""simple docstring"""



import torch

from diffusers import EulerAncestralDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest




class 							__lowercase(							_UpperCamelCase	):
						lowerCAmelCase_		:				Dict=							(EulerAncestralDiscreteScheduler,)
						lowerCAmelCase_		:				int=							10






						def 		lowerCAmelCase (       self     ,    **_SCREAMING_SNAKE_CASE)      ->	List[str]:




											"""simple docstring"""





											lowerCamelCase__					={
											    'num_train_timesteps': 11_00,
											    'beta_start': 0.0001,
											    'beta_end': 0.02,
											    'beta_schedule': 'linear',
											}

											config.update(**_SCREAMING_SNAKE_CASE)
											return config






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




											"""simple docstring"""





											for timesteps in [10, 50, 1_00, 10_00]:
																self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	Tuple:




											"""simple docstring"""





											for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]     ,    [0.0002, 0.002, 0.02]):
																self.check_over_configs(beta_start=_SCREAMING_SNAKE_CASE     ,    beta_end=_SCREAMING_SNAKE_CASE)






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




											"""simple docstring"""





											for schedule in ["linear", "scaled_linear"]:
																self.check_over_configs(beta_schedule=_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	Dict:




											"""simple docstring"""





											for prediction_type in ["epsilon", "v_prediction"]:
																self.check_over_configs(prediction_type=_SCREAMING_SNAKE_CASE)






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




											"""simple docstring"""





											lowerCamelCase__					=self.scheduler_classes[0]
											lowerCamelCase__					=self.get_scheduler_config()
											lowerCamelCase__					=scheduler_class(**_SCREAMING_SNAKE_CASE)

											scheduler.set_timesteps(self.num_inference_steps)

											lowerCamelCase__					=torch.manual_seed(0)

											lowerCamelCase__					=self.dummy_model()
											lowerCamelCase__					=self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
											lowerCamelCase__					=sample.to(_SCREAMING_SNAKE_CASE)

											for i, t in enumerate(scheduler.timesteps):
																lowerCamelCase__					=scheduler.scale_model_input(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=scheduler.step(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    generator=_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=output.prev_sample

											lowerCamelCase__					=torch.sum(torch.abs(_SCREAMING_SNAKE_CASE))
											lowerCamelCase__					=torch.mean(torch.abs(_SCREAMING_SNAKE_CASE))

											assert abs(result_sum.item() - 152.3192) < 1E-2
											assert abs(result_mean.item() - 0.1983) < 1E-3






						def 		lowerCAmelCase (       self)      ->	str:




											"""simple docstring"""





											lowerCamelCase__					=self.scheduler_classes[0]
											lowerCamelCase__					=self.get_scheduler_config(prediction_type='v_prediction')
											lowerCamelCase__					=scheduler_class(**_SCREAMING_SNAKE_CASE)

											scheduler.set_timesteps(self.num_inference_steps)

											lowerCamelCase__					=torch.manual_seed(0)

											lowerCamelCase__					=self.dummy_model()
											lowerCamelCase__					=self.dummy_sample_deter * scheduler.init_noise_sigma
											lowerCamelCase__					=sample.to(_SCREAMING_SNAKE_CASE)

											for i, t in enumerate(scheduler.timesteps):
																lowerCamelCase__					=scheduler.scale_model_input(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=scheduler.step(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    generator=_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=output.prev_sample

											lowerCamelCase__					=torch.sum(torch.abs(_SCREAMING_SNAKE_CASE))
											lowerCamelCase__					=torch.mean(torch.abs(_SCREAMING_SNAKE_CASE))

											assert abs(result_sum.item() - 108.4439) < 1E-2
											assert abs(result_mean.item() - 0.1412) < 1E-3






						def 		lowerCAmelCase (       self)      ->	Any:




											"""simple docstring"""





											lowerCamelCase__					=self.scheduler_classes[0]
											lowerCamelCase__					=self.get_scheduler_config()
											lowerCamelCase__					=scheduler_class(**_SCREAMING_SNAKE_CASE)

											scheduler.set_timesteps(self.num_inference_steps     ,    device=_SCREAMING_SNAKE_CASE)
											lowerCamelCase__					=torch.manual_seed(0)

											lowerCamelCase__					=self.dummy_model()
											lowerCamelCase__					=self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
											lowerCamelCase__					=sample.to(_SCREAMING_SNAKE_CASE)

											for t in scheduler.timesteps:
																lowerCamelCase__					=scheduler.scale_model_input(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=scheduler.step(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    generator=_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=output.prev_sample

											lowerCamelCase__					=torch.sum(torch.abs(_SCREAMING_SNAKE_CASE))
											lowerCamelCase__					=torch.mean(torch.abs(_SCREAMING_SNAKE_CASE))

											assert abs(result_sum.item() - 152.3192) < 1E-2
											assert abs(result_mean.item() - 0.1983) < 1E-3