code: def build_key_mapping(nemo_cfg):
      num_layers = nemo_cfg.num_layers
      has_bias = nemo_cfg.get("bias", True)
      has_layernorm_bias = (
          nemo_cfg.get("normalization", "layernorm") != "rmsnorm"
      )  # llama model uses rmsnorm which does not have bias
      model_str = (
          "model.module"
          if nemo_cfg.get("megatron_amp_O2", False)
          else "model"
      )
  
      # For GPT there is a 1:1 mapping of keys
      mcore_to_nemo_mapping = {
          f"{model_str}.embedding.word_embeddings.weight": "model.language_model.embedding.word_embeddings.weight",
          f"{model_str}.decoder.final_layernorm.weight": "model.language_model.encoder.final_layernorm.weight",
      }
      if has_layernorm_bias:
          mcore_to_nemo_mapping[
              f"{model_str}.decoder.final_layernorm.bias"
          ] = "model.language_model.encoder.final_layernorm.bias"
  
      if not nemo_cfg.get("share_embeddings_and_output_weights", True):
          mcore_to_nemo_mapping[
              f"{model_str}.output_layer.weight"
          ] = "model.language_model.output_layer.weight"
  
      if (
          nemo_cfg.get("position_embedding_type", "learned_absolute")
          == "rope"
      ):
          mcore_to_nemo_mapping[
              f"{model_str}.rotary_pos_emb.inv_freq"
          ] = "model.language_model.rotary_pos_emb.inv_freq"
      else:
          mcore_to_nemo_mapping[
              f"{model_str}.embedding.position_embeddings.weight"
          ] = "model.language_model.embedding.position_embeddings.weight"
  
      nemo_prefix = "model.language_model.encoder.layers"
      mcore_prefix = f"{model_str}.decoder.layers"
      for i in range(num_layers):
          for wb in ("weight", "bias") if has_bias else ("weight",):
              mcore_to_nemo_mapping.update(
                  {
                      f"{mcore_prefix}.{i}.mlp.linear_fc2.{wb}": f"{nemo_prefix}.{i}.mlp.dense_4h_to_h.{wb}",
                      f"{mcore_prefix}.{i}.mlp.linear_fc1.{wb}": f"{nemo_prefix}.{i}.mlp.dense_h_to_4h.{wb}",
                      f"{mcore_prefix}.{i}.self_attention.linear_proj.{wb}": f"{nemo_prefix}.{i}.self_attention.dense.{wb}",
                      f"{mcore_prefix}.{i}.self_attention.linear_qkv.{wb}": f"{nemo_prefix}.{i}.self_attention.query_key_value.{wb}",
                  }
              )
          # layernorm layers always have bias, but llama model uses rmsnorm which does not have bias
          for wb in (
              ("weight", "bias") if has_layernorm_bias else ("weight",)
          ):
              mcore_to_nemo_mapping.update(
                  {
                      f"{mcore_prefix}.{i}.self_attention.linear_qkv.layer_norm_{wb}": f"{nemo_prefix}.{i}.input_layernorm.{wb}",
                      f"{mcore_prefix}.{i}.mlp.linear_fc1.layer_norm_{wb}": f"{nemo_prefix}.{i}.post_attention_layernorm.{wb}",
                  }
              )
  
      return mcore_to_nemo_mapping
  
file_path: NeMo/scripts/nlp_language_modeling/convert_nemo_gpt_to_mcore.py
imports: import os
  from argparse import ArgumentParser
  from collections import OrderedDict
  import torch
  from omegaconf import OmegaConf, open_dict
  from pytorch_lightning.trainer.trainer import Trainer
  from nemo.collections.nlp.models.language_modeling.megatron_gpt_model import MegatronGPTModel
  from nemo.collections.nlp.parts.nlp_overrides import NLPDDPStrategy
  from nemo.utils import AppState, logging
label_desc: Use image node_id 4 for calling a global function with example usage: build_key_mapping(nemo_cfg) and returns: mcore_to_nemo_mapping