



"""simple docstring"""




import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	List[Any]		,				UpperCamelCase     :	Optional[Any]		,				UpperCamelCase     :	Optional[int]    ):
  A__											=							pt_weights[F"""{prefix}.in_proj_weight"""].chunk(3		,				dim=0    )
  A__											=							pt_weights[F"""{prefix}.in_proj_bias"""].chunk(3		,				dim=0    )

  A__											=							pt_weights[F"""{prefix}.out_proj.weight"""]
  A__											=							pt_weights[F"""{prefix}.out_proj.bias"""]

  A__											=							q_proj
  A__											=							q_proj_bias

  A__											=							k_proj
  A__											=							k_proj_bias

  A__											=							v_proj
  A__											=							v_proj_bias

  A__											=							out_proj_weights
  A__											=							out_proj_bias






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Optional[Any]		,				UpperCamelCase     :	Dict		,				UpperCamelCase     :	Optional[Any]    ):
  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     :	Optional[int]		,				UpperCamelCase     :	Any		,				UpperCamelCase     :	Any    ):
  A__											=							pt_weights[F"""{prefix}.weight"""].data
  A__											=							pt_weights[F"""{prefix}.bias"""].data






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Any		,				UpperCamelCase     :	int		,				UpperCamelCase     :	str    ):
  # copy layer norms
  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     :	Any		,				UpperCamelCase     :	List[str]		,				UpperCamelCase     :	Optional[int]    ):
  for layer_id, hf_layer in enumerate(UpperCamelCase    ):
    copy_layer(UpperCamelCase		,				UpperCamelCase		,				F"""{prefix}.{layer_id}"""    )






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Any		,				UpperCamelCase     :	Optional[Any]    ):
  # copy projection
  A__											=							pt_weights["""text_projection"""].data.T

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






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Tuple		,				UpperCamelCase     :	Any    ):
  # copy projection
  A__											=							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
  A__											=							pt_weights["""visual.conv1.weight"""].data
  A__											=							pt_weights["""visual.class_embedding"""].data
  A__											=							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     :	Dict		,				UpperCamelCase     :	Optional[Any]		,				UpperCamelCase     :	List[str]=None    ):
  assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
  A__											=							ChineseCLIPConfig.from_pretrained(UpperCamelCase    )

  A__											=							ChineseCLIPModel(UpperCamelCase    ).eval()

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

  hf_model.save_pretrained(UpperCamelCase    )


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!")












"""simple docstring"""




def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	int		,				UpperCamelCase     :	int = 1000    ):
  if (not isinstance(UpperCamelCase		,				UpperCamelCase    )) or (digit_position <= 0):
    raise ValueError("""Digit position must be a positive integer"""    )
  elif (not isinstance(UpperCamelCase		,				UpperCamelCase    )) or (precision < 0):
    raise ValueError("""Precision must be a nonnegative integer"""    )

  # compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
  # accurate
  A__											=							(
      4 * _subsum(UpperCamelCase		,				1		,				UpperCamelCase    )
      - 2 * _subsum(UpperCamelCase		,				4		,				UpperCamelCase    )
      - _subsum(UpperCamelCase		,				5		,				UpperCamelCase    )
      - _subsum(UpperCamelCase		,				6		,				UpperCamelCase    )
  )

  # return the first hex digit of the fractional part of the result
  return hex(int((sum_result % 1) * 16    )    )[2:]






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	int		,				UpperCamelCase     :	int		,				UpperCamelCase     :	int    ):
  A__											=							0.0
  for sum_index in range(digit_pos_to_extract + precision    ):
    A__											=							8 * sum_index + denominator_addend
    if sum_index < digit_pos_to_extract:
      # if the exponential term is an integer and we mod it by the denominator
      # before dividing, only the integer part of the sum will change;
      # the fractional part will not
      A__											=							pow(
          16		,				digit_pos_to_extract - 1 - sum_index		,				UpperCamelCase    )
    else:
      A__											=							pow(16		,				digit_pos_to_extract - 1 - sum_index    )
    total += exponential_term / denominator
  return total


if __name__ == "__main__":
      import doctest

      doctest.testmod()




