code: def convert(
      input_nemo_file, output_hf_file, precision=None, cpu_only=False
  ) -> None:
      """
      Convert NeMo weights to HF weights
      """
      dummy_trainer = Trainer(
          devices=1, accelerator="cpu", strategy=NLPDDPStrategy()
      )
      if cpu_only:
          map_location = torch.device("cpu")
          model_config = MegatronGPTModel.restore_from(
              input_nemo_file, trainer=dummy_trainer, return_config=True
          )
          model_config.use_cpu_initialization = True
          model_config.tensor_model_parallel_size = 1
      else:
          map_location, model_config = None, None
  
      if cpu_only:
          logging.info(
              "******** Loading model on CPU. This will take a significant amount of time."
          )
      model = MegatronGPTModel.restore_from(
          input_nemo_file,
          trainer=dummy_trainer,
          override_config_path=model_config,
          map_location=map_location,
      )
      if precision is None:
          precision = model.cfg.precision
      try:
          dtype = torch_dtype_from_precision(precision)
      except ValueError as e:
          logging.warning(
              str(e)
              + f", precision string '{precision}' is not recognized, falling back to fp32"
          )
          dtype = torch.float32  # fallback
  
      param_to_weights = lambda param: param.to(dtype)
      checkpoint = OrderedDict()
  
      def get_original_key(new_key):
          new_key = new_key[len(prefix) :]
  
          if new_key.startswith("embedding.word_embeddings.weight"):
              return "transformer.word_embeddings.weight"
          elif new_key.startswith("decoder.final_layernorm"):
              return new_key.replace(
                  "decoder.final_layernorm", "transformer.ln_f"
              )
          elif new_key.startswith("output_layer"):
              return new_key.replace("output_layer", "lm_head")
  
          key = new_key.replace("decoder.layers", "transformer.h")
  
          if (
              model.cfg.mcore_customization_config.new_decoder_architecture
          ):
              key = key.replace("input_layernorm", "ln_attn")
              key = key.replace("pre_mlp_layernorm", "ln_mlp")
          else:
              key = key.replace("input_layernorm", "input_layernorm")
              if (
                  not model.cfg.mcore_customization_config.parallel_attention
              ):
                  key = key.replace(
                      "post_self_attn_layernorm",
                      "post_attention_layernorm",
                  )
  
          key = key.replace(
              "self_attention.linear_proj", "self_attention.dense"
          )
          key = key.replace(
              "self_attention.linear_qkv",
              "self_attention.query_key_value",
          )
          key = key.replace("linear_fc1", "dense_h_to_4h")
          key = key.replace("linear_fc2", "dense_4h_to_h")
          return key
  
      prefix = (
          "model.module."
          if any(
              k.startswith("model.module.") for k in model.state_dict()
          )
          else "model."
      )
  
      for key, value in model.state_dict().items():
          if "_extra_state" in key:
              continue
          orig_key = get_original_key(key)
          checkpoint[orig_key] = param_to_weights(value)
  
      os.makedirs(os.path.dirname(output_hf_file), exist_ok=True)
      torch.save(checkpoint, output_hf_file)
      logging.info(f"Weights reverted and saved to {output_hf_file}")
  
file_path: NeMo/scripts/nlp_language_modeling/convert_nemo_falcon_to_hf.py
imports: import os
  from argparse import ArgumentParser
  from collections import OrderedDict
  import torch
  from pytorch_lightning import Trainer
  from transformers import AutoModelForCausalLM
  from nemo.collections.nlp.models.language_modeling.megatron_gpt_model import MegatronGPTModel
  from nemo.collections.nlp.parts.nlp_overrides import NLPDDPStrategy
  from nemo.collections.nlp.parts.utils_funcs import torch_dtype_from_precision
  from nemo.utils import logging
label_desc: Use image node_id 2 for calling a global function with example usage: convert(input_nemo_file, output_hf_file, precision, cpu_only) and returns: key, str, new_key, new_key