






"""simple docstring"""







import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel

def _a (		_snake_case      ,       _snake_case      ,       _snake_case							):







      """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 _a (		_snake_case      ,       _snake_case      ,       _snake_case							):







      """simple docstring"""






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

def _a (		_snake_case      ,       _snake_case      ,       _snake_case							):







      """simple docstring"""






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

def _a (		_snake_case      ,       _snake_case      ,       _snake_case							):







      """simple docstring"""






      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 _a (		_snake_case      ,       _snake_case      ,       _snake_case							):







      """simple docstring"""






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

def _a (		_snake_case      ,       _snake_case							):







      """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 _a (		_snake_case      ,       _snake_case							):







      """simple docstring"""






      UpperCAmelCase							=       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
      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      ,       _snake_case      ,       """visual.transformer.resblocks"""							)

@torch.no_grad()
def _a (		_snake_case      ,       _snake_case      ,       _snake_case=None							):







      """simple docstring"""






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

      UpperCAmelCase							=       ChineseCLIPModel(_snake_case							).eval()

      UpperCAmelCase							=       torch.load(_snake_case      ,       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(_snake_case      ,       _snake_case							)
      copy_vision_model_and_projection(_snake_case      ,       _snake_case							)
      UpperCAmelCase							=       pt_weights["""logit_scale"""].data

      hf_model.save_pretrained(_snake_case							)


if __name__ == "__main__":
  _UpperCamelCase     			=  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."""
  )
  _UpperCamelCase     			=  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 sys

def _a (		_snake_case      ,       _snake_case							):







      """simple docstring"""






      if not numbers:
            return 0
      if target == 0 and target in numbers:
            return 0
      if not isinstance(_snake_case      ,       (list, tuple)							) or not all(
          isinstance(_snake_case      ,       _snake_case							) for number in numbers							):
            raise ValueError("""numbers must be an iterable of integers"""							)

      UpperCAmelCase							=       UpperCAmelCase							=       UpperCAmelCase							=       0
      UpperCAmelCase							=       sys.maxsize

      while right < len(_snake_case							):
            curr_sum += numbers[right]
            while curr_sum >= target and left <= right:
                  UpperCAmelCase							=       min(_snake_case      ,       right - left + 1							)
                  curr_sum -= numbers[left]
                  left += 1
            right += 1

      return 0 if min_len == sys.maxsize else min_len



