code: def mapping_hf_state_dict(hf_state_dict, model, clip_dict=None):
      nemo_state = model.state_dict()
      new_state_dict = {}
      for k, v in hf_state_dict.items():
          k = "model." + k
          # This is not necessary when you turn off model.inductor in config file
          # if 'diffusion_model' in k:
          #     k = k.replace('diffusion_model', 'diffusion_model._orig_mod')
          if "in_layers" in k or "out_layers" in k:
              s = k.split(".")
              idx = int(s[-2])
              if idx != 0:
                  k = ".".join(s[:-2] + [str(int(idx - 1))] + [s[-1]])
          if k in nemo_state:
              new_state_dict[k] = v
      if clip_dict:
          for k, v in clip_dict.items():
              k = k.replace(
                  "model.text_encoder", "model.cond_stage_model.model"
              )
              if k in nemo_state:
                  new_state_dict[k] = v
      for k in [
          "betas",
          "alphas_cumprod",
          "alphas_cumprod_prev",
          "sqrt_alphas_cumprod",
          "sqrt_one_minus_alphas_cumprod",
          "log_one_minus_alphas_cumprod",
          "sqrt_recip_alphas_cumprod",
          "sqrt_recipm1_alphas_cumprod",
          "posterior_variance",
          "posterior_log_variance_clipped",
          "posterior_mean_coef1",
          "posterior_mean_coef2",
      ]:
          new_state_dict["model." + k] = nemo_state["model." + k]
  
      return new_state_dict
  
file_path: NeMo/examples/multimodal/text_to_image/convert_hf_ckpt_to_nemo.py
imports: import os
  import tempfile
  from argparse import ArgumentParser
  import torch
  from lightning_fabric.utilities.cloud_io import _load
  from omegaconf import OmegaConf
  from pytorch_lightning.plugins.environments import TorchElasticEnvironment
  from pytorch_lightning.trainer.trainer import Trainer
  from nemo.collections.multimodal.models.text_to_image.controlnet.controlnet import MegatronControlNet
  from nemo.collections.multimodal.models.text_to_image.stable_diffusion.ldm.ddpm import MegatronLatentDiffusion
  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_state_dict, model, clip_dict) and returns: new_state_dict