
'''simple docstring'''




import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel






def     snake_case__							(      _A: int				,       _A: str				,       _A: Optional[int]			)   ->     List[Any]:

		'''simple docstring'''


		lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase        =  pt_weights[f"{prefix}.in_proj_weight"].chunk(3				,       dim=0			)
		lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase        =  pt_weights[f"{prefix}.in_proj_bias"].chunk(3				,       dim=0			)

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

		lowerCAmelCase        =  q_proj
		lowerCAmelCase        =  q_proj_bias

		lowerCAmelCase        =  k_proj
		lowerCAmelCase        =  k_proj_bias

		lowerCAmelCase        =  v_proj
		lowerCAmelCase        =  v_proj_bias

		lowerCAmelCase        =  out_proj_weights
		lowerCAmelCase        =  out_proj_bias






def     snake_case__							(      _A: List[Any]				,       _A: str				,       _A: List[Any]			)   ->     Optional[Any]:

		'''simple docstring'''


		copy_linear(hf_mlp.fca				,       _A				,       f"{prefix}.c_fc"			)
		copy_linear(hf_mlp.fca				,       _A				,       f"{prefix}.c_proj"			)






def     snake_case__							(      _A: List[Any]				,       _A: Tuple				,       _A: Optional[Any]			)   ->     Optional[int]:

		'''simple docstring'''


		lowerCAmelCase        =  pt_weights[f"{prefix}.weight"].data
		lowerCAmelCase        =  pt_weights[f"{prefix}.bias"].data






def     snake_case__							(      _A: Union[str, Any]				,       _A: Dict				,       _A: Tuple			)   ->     int:

		'''simple docstring'''


		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     snake_case__							(      _A: Dict				,       _A: Optional[int]				,       _A: Any			)   ->     Optional[Any]:

		'''simple docstring'''


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






def     snake_case__							(      _A: Optional[int]				,       _A: int			)   ->     Any:

		'''simple docstring'''


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

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






def     snake_case__							(      _A: List[Any]				,       _A: List[Any]			)   ->     str:

		'''simple docstring'''


		lowerCAmelCase        =  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
		lowerCAmelCase        =  pt_weights["""visual.conv1.weight"""].data
		lowerCAmelCase        =  pt_weights["""visual.class_embedding"""].data
		lowerCAmelCase        =  pt_weights["""visual.positional_embedding"""].data

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






@torch.no_grad()
def     snake_case__							(      _A: Tuple				,       _A: List[Any]				,       _A: Any=None			)   ->     List[Any]:

		'''simple docstring'''


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

		lowerCAmelCase        =  ChineseCLIPModel(_A			).eval()

		lowerCAmelCase        =  torch.load(_A				,       map_location="""cpu"""			)["""state_dict"""]
		lowerCAmelCase        =  {(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			)
		lowerCAmelCase        =  pt_weights["""logit_scale"""].data

		hf_model.save_pretrained(_A			)


if __name__ == "__main__":
						__lowercase				   =  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				   =  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 torch
from flax.training.checkpoints import restore_checkpoint

from transformers import FNetConfig, FNetForPreTraining
from transformers.utils import logging


logging.set_verbosity_info()






def     snake_case__							(      _A: Dict				,       _A: str				,       _A: int			)   ->     Any:

		'''simple docstring'''


		lowerCAmelCase        =  FNetConfig.from_json_file(_A			)
		print(f"Building PyTorch model from configuration: {config}"			)
		lowerCAmelCase        =  FNetForPreTraining(_A			)

		lowerCAmelCase        =  restore_checkpoint(_A				,       _A			)
		lowerCAmelCase        =  checkpoint_dict["""target"""]

		# Embeddings
		# Position IDs
		lowerCAmelCase        =  fnet_pretraining_model.state_dict()

		lowerCAmelCase        =  state_dict["""fnet.embeddings.position_ids"""]
		lowerCAmelCase        =  {"""fnet.embeddings.position_ids""": position_ids}
		# Embedding Layers
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""encoder"""]["""embedder"""]["""word"""]["""embedding"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""encoder"""]["""embedder"""]["""position"""]["""embedding"""][0]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""encoder"""]["""embedder"""]["""type"""]["""embedding"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""encoder"""]["""embedder"""]["""hidden_mapping_in"""]["""kernel"""]			).T
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""encoder"""]["""embedder"""]["""hidden_mapping_in"""]["""bias"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""encoder"""]["""embedder"""]["""layer_norm"""]["""scale"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""encoder"""]["""embedder"""]["""layer_norm"""]["""bias"""]			)

		# Encoder Layers
		for layer in range(config.num_hidden_layers			):
				lowerCAmelCase        =  torch.tensor(
				    pretrained_model_params["""encoder"""][f"encoder_{layer}"]["""mixing_layer_norm"""]["""scale"""]			)
				lowerCAmelCase        =  torch.tensor(
				    pretrained_model_params["""encoder"""][f"encoder_{layer}"]["""mixing_layer_norm"""]["""bias"""]			)

				lowerCAmelCase        =  torch.tensor(
				    pretrained_model_params["""encoder"""][f"feed_forward_{layer}"]["""intermediate"""]["""kernel"""]			).T
				lowerCAmelCase        =  torch.tensor(
				    pretrained_model_params["""encoder"""][f"feed_forward_{layer}"]["""intermediate"""]["""bias"""]			)

				lowerCAmelCase        =  torch.tensor(
				    pretrained_model_params["""encoder"""][f"feed_forward_{layer}"]["""output"""]["""kernel"""]			).T
				lowerCAmelCase        =  torch.tensor(
				    pretrained_model_params["""encoder"""][f"feed_forward_{layer}"]["""output"""]["""bias"""]			)

				lowerCAmelCase        =  torch.tensor(
				    pretrained_model_params["""encoder"""][f"encoder_{layer}"]["""output_layer_norm"""]["""scale"""]			)
				lowerCAmelCase        =  torch.tensor(
				    pretrained_model_params["""encoder"""][f"encoder_{layer}"]["""output_layer_norm"""]["""bias"""]			)

		# Pooler Layers
		lowerCAmelCase        =  torch.tensor(pretrained_model_params["""encoder"""]["""pooler"""]["""kernel"""]			).T
		lowerCAmelCase        =  torch.tensor(pretrained_model_params["""encoder"""]["""pooler"""]["""bias"""]			)

		# Masked LM Layers
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""predictions_dense"""]["""kernel"""]			).T
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""predictions_dense"""]["""bias"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""predictions_layer_norm"""]["""scale"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""predictions_layer_norm"""]["""bias"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""encoder"""]["""embedder"""]["""word"""]["""embedding"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""predictions_output"""]["""output_bias"""]			)
		lowerCAmelCase        =  torch.tensor(pretrained_model_params["""predictions_output"""]["""output_bias"""]			)

		# Seq Relationship Layers
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""classification"""]["""output_kernel"""]			)
		lowerCAmelCase        =  torch.tensor(
		    pretrained_model_params["""classification"""]["""output_bias"""]			)

		# Load State Dict
		fnet_pretraining_model.load_state_dict(_A			)

		# Save PreTrained
		print(f"Saving pretrained model to {save_path}"			)
		fnet_pretraining_model.save_pretrained(_A			)


if __name__ == "__main__":
						__lowercase				   =  argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '''--flax_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
						)
						parser.add_argument(
						    '''--fnet_config_file''',
						    default=None,
						    type=str,
						    required=True,
						    help=(
						        '''The config json file corresponding to the pre-trained FNet model. \n'''
						        '''This specifies the model architecture.'''
						    ),
						)
						parser.add_argument('''--save_path''', default=None, type=str, required=True, help='''Path to the output model.''')
						__lowercase				   =  parser.parse_args()
						convert_flax_checkpoint_to_pytorch(args.flax_checkpoint_path, args.fnet_config_file, args.save_path)




