



'''simple docstring'''







import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel





def     snake_case__			(lowerCAmelCase_       ,	lowerCAmelCase_       ,	lowerCAmelCase_						):
    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     snake_case__			(lowerCAmelCase_       ,	lowerCAmelCase_       ,	lowerCAmelCase_						):
    copy_linear(hf_mlp.fca       ,	lowerCAmelCase_       ,	F'{prefix}.c_fc'						)
    copy_linear(hf_mlp.fca       ,	lowerCAmelCase_       ,	F'{prefix}.c_proj'						)





def     snake_case__			(lowerCAmelCase_       ,	lowerCAmelCase_       ,	lowerCAmelCase_						):
    UpperCamelCase__	     =      pt_weights[F'{prefix}.weight'].data
    UpperCamelCase__	     =      pt_weights[F'{prefix}.bias'].data





def     snake_case__			(lowerCAmelCase_       ,	lowerCAmelCase_       ,	lowerCAmelCase_						):
    # copy layer norms
    copy_linear(hf_layer.layer_norma       ,	lowerCAmelCase_       ,	F'{prefix}.ln_1'						)
    copy_linear(hf_layer.layer_norma       ,	lowerCAmelCase_       ,	F'{prefix}.ln_2'						)

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

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





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





def     snake_case__			(lowerCAmelCase_       ,	lowerCAmelCase_						):
    # copy projection
    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     snake_case__			(lowerCAmelCase_       ,	lowerCAmelCase_						):
    # copy projection
    UpperCamelCase__	     =      pt_weights["visual.proj"].data.T

    # copy layer norms
    copy_linear(hf_model.vision_model.pre_layrnorm       ,	lowerCAmelCase_       ,	"visual.ln_pre"						)
    copy_linear(hf_model.vision_model.post_layernorm       ,	lowerCAmelCase_       ,	"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       ,	lowerCAmelCase_       ,	"visual.transformer.resblocks"						)





@torch.no_grad()
def     snake_case__			(lowerCAmelCase_       ,	lowerCAmelCase_       ,	lowerCAmelCase_=None						):
    assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
    UpperCamelCase__	     =      ChineseCLIPConfig.from_pretrained(lowerCAmelCase_						)

    UpperCamelCase__	     =      ChineseCLIPModel(lowerCAmelCase_						).eval()

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

    hf_model.save_pretrained(lowerCAmelCase_						)


if __name__ == "__main__":
      lowercase__  :	List[str]            =      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__  :	Union[str, 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'''







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase__  :	Optional[Any]            =      {
    'configuration_marian': ['MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MarianConfig', 'MarianOnnxConfig'],
}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase__  :	List[Any]            =      ['MarianTokenizer']

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase__  :	List[str]            =      [
          'MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST',
          'MarianForCausalLM',
          'MarianModel',
          'MarianMTModel',
          'MarianPreTrainedModel',
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase__  :	Optional[Any]            =      ['TFMarianModel', 'TFMarianMTModel', 'TFMarianPreTrainedModel']

try:
      if not is_flax_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase__  :	Any            =      ['FlaxMarianModel', 'FlaxMarianMTModel', 'FlaxMarianPreTrainedModel']

if TYPE_CHECKING:
      from .configuration_marian import MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP, MarianConfig, MarianOnnxConfig

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_marian import MarianTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_marian import (
                MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST,
                MarianForCausalLM,
                MarianModel,
                MarianMTModel,
                MarianPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_marian import TFMarianModel, TFMarianMTModel, TFMarianPreTrainedModel

      try:
            if not is_flax_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_flax_marian import FlaxMarianModel, FlaxMarianMTModel, FlaxMarianPreTrainedModel

else:
      import sys

      lowercase__  :	Any            =      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




