






"""simple docstring"""



import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel



def 						lowercase   (    __snake_case    :			int		, __snake_case    :			Tuple		, __snake_case    :			Dict     ):
  lowercase_	, lowercase_	, lowercase_			:			List[str]								=       pt_weights[F'''{prefix}.in_proj_weight'''].chunk(3		, dim=0     )
  lowercase_	, lowercase_	, lowercase_			:			List[Any]								=       pt_weights[F'''{prefix}.in_proj_bias'''].chunk(3		, dim=0     )

  lowercase_			:			Tuple								=       pt_weights[F'''{prefix}.out_proj.weight''']
  lowercase_			:			Optional[Any]								=       pt_weights[F'''{prefix}.out_proj.bias''']

  lowercase_			:			Optional[int]								=       q_proj
  lowercase_			:			List[str]								=       q_proj_bias

  lowercase_			:			List[Any]								=       k_proj
  lowercase_			:			List[Any]								=       k_proj_bias

  lowercase_			:			List[Any]								=       v_proj
  lowercase_			:			List[str]								=       v_proj_bias

  lowercase_			:			Union[str, Any]								=       out_proj_weights
  lowercase_			:			Dict								=       out_proj_bias



def 						lowercase   (    __snake_case    :			int		, __snake_case    :			List[str]		, __snake_case    :			int     ):
  copy_linear(hf_mlp.fca		, snake_case__		, F'''{prefix}.c_fc'''     )
  copy_linear(hf_mlp.fca		, snake_case__		, F'''{prefix}.c_proj'''     )



def 						lowercase   (    __snake_case    :			Optional[Any]		, __snake_case    :			int		, __snake_case    :			int     ):
  lowercase_			:			Optional[Any]								=       pt_weights[F'''{prefix}.weight'''].data
  lowercase_			:			Optional[Any]								=       pt_weights[F'''{prefix}.bias'''].data



def 						lowercase   (    __snake_case    :			Optional[Any]		, __snake_case    :			Dict		, __snake_case    :			List[Any]     ):
  # copy layer norms
  copy_linear(hf_layer.layer_norma		, snake_case__		, F'''{prefix}.ln_1'''     )
  copy_linear(hf_layer.layer_norma		, snake_case__		, F'''{prefix}.ln_2'''     )

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

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



def 						lowercase   (    __snake_case    :			List[Any]		, __snake_case    :			List[str]		, __snake_case    :			Optional[Any]     ):
  for layer_id, hf_layer in enumerate(snake_case__     ):
    copy_layer(snake_case__		, snake_case__		, F'''{prefix}.{layer_id}'''     )



def 						lowercase   (    __snake_case    :			Union[str, Any]		, __snake_case    :			Optional[int]     ):
  # copy projection
  lowercase_			:			Union[str, Any]								=       pt_weights['''text_projection'''].data.T

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



def 						lowercase   (    __snake_case    :			Union[str, Any]		, __snake_case    :			int     ):
  # copy projection
  lowercase_			:			Union[str, Any]								=       pt_weights['''visual.proj'''].data.T

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

  # copy embeddings
  lowercase_			:			List[Any]								=       pt_weights['''visual.conv1.weight'''].data
  lowercase_			:			Union[str, Any]								=       pt_weights['''visual.class_embedding'''].data
  lowercase_			:			Any								=       pt_weights['''visual.positional_embedding'''].data

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



@torch.no_grad()
def 						lowercase   (    __snake_case    :			List[Any]		, __snake_case    :			Optional[int]		, __snake_case    :			int=None     ):
  assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
  lowercase_			:			Any								=       ChineseCLIPConfig.from_pretrained(snake_case__     )

  lowercase_			:			Tuple								=       ChineseCLIPModel(snake_case__     ).eval()

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

  copy_text_model_and_projection(snake_case__		, snake_case__     )
  copy_vision_model_and_projection(snake_case__		, snake_case__     )
  lowercase_			:			Union[str, Any]								=       pt_weights['''logit_scale'''].data

  hf_model.save_pretrained(snake_case__     )


if __name__ == "__main__":
      __A		:	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.'''
      )
      __A		:	List[Any]      =		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 argparse
import os

import torch
from torchvision.datasets.utils import download_url

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, TransformeraDModel


__A		:	Union[str, Any]      =		{512: '''DiT-XL-2-512x512.pt''', 256: '''DiT-XL-2-256x256.pt'''}



def 						lowercase   (    __snake_case    :			Optional[int]     ):
		lowercase_			:			Any								=       F'''pretrained_models/{model_name}'''
		if not os.path.isfile(__snake_case     ):
				os.makedirs('''pretrained_models'''		, exist_ok=__snake_case     )
				lowercase_			:			str								=       F'''https://dl.fbaipublicfiles.com/DiT/models/{model_name}'''
				download_url(__snake_case		, '''pretrained_models'''     )
		lowercase_			:			List[str]								=       torch.load(__snake_case		, map_location=lambda __snake_case		, __snake_case     : storage     )
		return model



def 						lowercase   (    __snake_case    :			Optional[int]     ):
		lowercase_			:			Dict								=       download_model(pretrained_models[args.image_size]     )

		lowercase_			:			List[str]								=       state_dict['''x_embedder.proj.weight''']
		lowercase_			:			Tuple								=       state_dict['''x_embedder.proj.bias''']
		state_dict.pop('''x_embedder.proj.weight'''     )
		state_dict.pop('''x_embedder.proj.bias'''     )

		for depth in range(2_8     ):
				lowercase_			:			Optional[int]								=       state_dict[
				    '''t_embedder.mlp.0.weight'''
				]
				lowercase_			:			List[str]								=       state_dict[
				    '''t_embedder.mlp.0.bias'''
				]
				lowercase_			:			Tuple								=       state_dict[
				    '''t_embedder.mlp.2.weight'''
				]
				lowercase_			:			Union[str, Any]								=       state_dict[
				    '''t_embedder.mlp.2.bias'''
				]
				lowercase_			:			Dict								=       state_dict[
				    '''y_embedder.embedding_table.weight'''
				]

				lowercase_			:			List[str]								=       state_dict[
				    F'''blocks.{depth}.adaLN_modulation.1.weight'''
				]
				lowercase_			:			Optional[Any]								=       state_dict[
				    F'''blocks.{depth}.adaLN_modulation.1.bias'''
				]

				lowercase_	, lowercase_	, lowercase_			:			List[Any]								=       torch.chunk(state_dict[F'''blocks.{depth}.attn.qkv.weight''']		, 3		, dim=0     )
				lowercase_	, lowercase_	, lowercase_			:			Optional[int]								=       torch.chunk(state_dict[F'''blocks.{depth}.attn.qkv.bias''']		, 3		, dim=0     )

				lowercase_			:			Union[str, Any]								=       q
				lowercase_			:			Dict								=       q_bias
				lowercase_			:			Dict								=       k
				lowercase_			:			Tuple								=       k_bias
				lowercase_			:			str								=       v
				lowercase_			:			Union[str, Any]								=       v_bias

				lowercase_			:			Optional[Any]								=       state_dict[
				    F'''blocks.{depth}.attn.proj.weight'''
				]
				lowercase_			:			List[Any]								=       state_dict[F'''blocks.{depth}.attn.proj.bias''']

				lowercase_			:			List[Any]								=       state_dict[F'''blocks.{depth}.mlp.fc1.weight''']
				lowercase_			:			Optional[Any]								=       state_dict[F'''blocks.{depth}.mlp.fc1.bias''']
				lowercase_			:			Tuple								=       state_dict[F'''blocks.{depth}.mlp.fc2.weight''']
				lowercase_			:			List[str]								=       state_dict[F'''blocks.{depth}.mlp.fc2.bias''']

				state_dict.pop(F'''blocks.{depth}.attn.qkv.weight'''     )
				state_dict.pop(F'''blocks.{depth}.attn.qkv.bias'''     )
				state_dict.pop(F'''blocks.{depth}.attn.proj.weight'''     )
				state_dict.pop(F'''blocks.{depth}.attn.proj.bias'''     )
				state_dict.pop(F'''blocks.{depth}.mlp.fc1.weight'''     )
				state_dict.pop(F'''blocks.{depth}.mlp.fc1.bias'''     )
				state_dict.pop(F'''blocks.{depth}.mlp.fc2.weight'''     )
				state_dict.pop(F'''blocks.{depth}.mlp.fc2.bias'''     )
				state_dict.pop(F'''blocks.{depth}.adaLN_modulation.1.weight'''     )
				state_dict.pop(F'''blocks.{depth}.adaLN_modulation.1.bias'''     )

		state_dict.pop('''t_embedder.mlp.0.weight'''     )
		state_dict.pop('''t_embedder.mlp.0.bias'''     )
		state_dict.pop('''t_embedder.mlp.2.weight'''     )
		state_dict.pop('''t_embedder.mlp.2.bias'''     )
		state_dict.pop('''y_embedder.embedding_table.weight'''     )

		lowercase_			:			Union[str, Any]								=       state_dict['''final_layer.adaLN_modulation.1.weight''']
		lowercase_			:			Optional[Any]								=       state_dict['''final_layer.adaLN_modulation.1.bias''']
		lowercase_			:			Dict								=       state_dict['''final_layer.linear.weight''']
		lowercase_			:			Union[str, Any]								=       state_dict['''final_layer.linear.bias''']

		state_dict.pop('''final_layer.linear.weight'''     )
		state_dict.pop('''final_layer.linear.bias'''     )
		state_dict.pop('''final_layer.adaLN_modulation.1.weight'''     )
		state_dict.pop('''final_layer.adaLN_modulation.1.bias'''     )

		# DiT XL/2
		lowercase_			:			List[str]								=       TransformeraDModel(
		    sample_size=args.image_size // 8		, num_layers=2_8		, attention_head_dim=7_2		, in_channels=4		, out_channels=8		, patch_size=2		, attention_bias=__snake_case		, num_attention_heads=1_6		, activation_fn='''gelu-approximate'''		, num_embeds_ada_norm=1_0_0_0		, norm_type='''ada_norm_zero'''		, norm_elementwise_affine=__snake_case		, )
		transformer.load_state_dict(__snake_case		, strict=__snake_case     )

		lowercase_			:			Any								=       DDIMScheduler(
		    num_train_timesteps=1_0_0_0		, beta_schedule='''linear'''		, prediction_type='''epsilon'''		, clip_sample=__snake_case		, )

		lowercase_			:			Dict								=       AutoencoderKL.from_pretrained(args.vae_model     )

		lowercase_			:			str								=       DiTPipeline(transformer=__snake_case		, vae=__snake_case		, scheduler=__snake_case     )

		if args.save:
				pipeline.save_pretrained(args.checkpoint_path     )


if __name__ == "__main__":
						__A		:	int      =		argparse.ArgumentParser()

						parser.add_argument(
						    '''--image_size''',
						    default=256,
						    type=int,
						    required=False,
						    help='''Image size of pretrained model, either 256 or 512.''',
						)
						parser.add_argument(
						    '''--vae_model''',
						    default='''stabilityai/sd-vae-ft-ema''',
						    type=str,
						    required=False,
						    help='''Path to pretrained VAE model, either stabilityai/sd-vae-ft-mse or stabilityai/sd-vae-ft-ema.''',
						)
						parser.add_argument(
						    '''--save''', default=True, type=bool, required=False, help='''Whether to save the converted pipeline or not.'''
						)
						parser.add_argument(
						    '''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the output pipeline.'''
						)

						__A		:	Union[str, Any]      =		parser.parse_args()
						main(args)



