code: def mapping_hf_state_dict(hf_model):
      hf_state_dict = hf_model.state_dict()
      key_mapping = {
          "text_projection.weight": "text_encoder.head.weight",
          "visual_projection.weight": "vision_encoder.head.weight",
      }
  
      layer_mapping = {
          ".layer_norm1.weight": ".input_layernorm.weight",
          ".layer_norm1.bias": ".input_layernorm.bias",
          ".self_attn.out_proj.weight": ".self_attention.dense.weight",
          ".self_attn.out_proj.bias": ".self_attention.dense.bias",
          ".layer_norm2.weight": ".post_attention_layernorm.weight",
          ".layer_norm2.bias": ".post_attention_layernorm.bias",
          ".mlp.fc1.weight": ".mlp.dense_h_to_4h.weight",
          ".mlp.fc1.bias": ".mlp.dense_h_to_4h.bias",
          ".mlp.fc2.weight": ".mlp.dense_4h_to_h.weight",
          ".mlp.fc2.bias": ".mlp.dense_4h_to_h.bias",
          ".pre_layrnorm.weight": ".preprocess_layernorm.weight",
          ".pre_layrnorm.bias": ".preprocess_layernorm.bias",
          ".post_layernorm.weight": ".transformer.final_layernorm.weight",
          ".post_layernorm.bias": ".transformer.final_layernorm.bias",
          ".backbone.embeddings.position_embedding.weight": ".backbone.position_embeddings",
          ".language_model.embeddings.position_embedding.weight": ".language_model.embedding.position_embeddings",
          ".embeddings.class_embedding": ".cls_token",
          ".backbone.embeddings.patch_embedding.weight": ".backbone.linear_encoder.weight",
          ".final_layer_norm.weight": ".encoder.final_layernorm.weight",
          ".final_layer_norm.bias": ".encoder.final_layernorm.bias",
          ".embeddings.token_embedding.weight": ".embedding.word_embeddings.weight",
      }
  
      nemo_state_dict = {}
      for key in hf_state_dict.keys():
          if key.startswith("text_model.encoder.layers"):
              key_ = key.replace(
                  "text_model.encoder.layers",
                  "text_encoder.language_model.encoder.layers",
              )
          elif key.startswith("vision_model.encoder.layers"):
              key_ = key.replace(
                  "vision_model.encoder.layers",
                  "vision_encoder.backbone.transformer.layers",
              )
          elif key.startswith("vision_model."):
              key_ = key.replace(
                  "vision_model.", "vision_encoder.backbone."
              )
          elif key.startswith("text_model."):
              key_ = key.replace(
                  "text_model.", "text_encoder.language_model."
              )
          else:
              key_ = key
          for pat in key_mapping:
              if key_ == pat:
                  key_ = key_.replace(pat, key_mapping[pat])
          for pat in layer_mapping:
              if key_.endswith(pat):
                  key_ = key_[: -len(pat)] + layer_mapping[pat]
                  break
          if "q_proj" in key_:
              key_k = key.replace("q_proj", "k_proj")
              key_v = key.replace("q_proj", "v_proj")
              key_new = key_.replace(
                  "self_attn.q_proj", "self_attention.query_key_value"
              )
              value_new = torch.concat(
                  (
                      hf_state_dict[key],
                      hf_state_dict[key_k],
                      hf_state_dict[key_v],
                  ),
                  dim=0,
              )
              nemo_state_dict[key_new] = value_new
          elif not (
              "k_proj" in key_
              or "v_proj" in key_
              or "position_ids" in key_
          ):
              nemo_state_dict[key_] = hf_state_dict[key]
  
      nemo_state_dict[
          "vision_encoder.backbone.cls_token"
      ] = nemo_state_dict["vision_encoder.backbone.cls_token"].reshape(
          1, 1, -1
      )
      w = nemo_state_dict[
          "vision_encoder.backbone.linear_encoder.weight"
      ]
      nemo_state_dict[
          "vision_encoder.backbone.linear_encoder.weight"
      ] = einops.rearrange(
          w,
          "b c p1 p2 -> b (p1 p2 c)",
      )
      nemo_state_dict[
          "vision_encoder.backbone.linear_encoder.bias"
      ] = torch.zeros(w.shape[0])
  
      return nemo_state_dict
  
file_path: NeMo/examples/multimodal/vision_language_foundation/clip/convert_external_clip_to_nemo.py
imports: import os
  from argparse import ArgumentParser
  import einops
  import open_clip
  import torch
  from omegaconf import OmegaConf
  from pytorch_lightning.plugins.environments import TorchElasticEnvironment
  from pytorch_lightning.trainer.trainer import Trainer
  from transformers import CLIPModel
  from nemo.collections.multimodal.models.vision_language_foundation.clip.megatron_clip_models import MegatronCLIPModel
  from nemo.collections.nlp.parts.nlp_overrides import NLPSaveRestoreConnector
  from nemo.utils import AppState, logging
  from nemo.utils.distributed import initialize_distributed
label_desc: Use image node_id 3 for calling a global function with example usage: mapping_hf_state_dict(hf_model) and returns: nemo_state_dict