code: def _modify_config(gpt_cfg, cfg, add_cfg_to_tree=False):
      """
      This function modifies the original gpt pre-training config (gpt_cfg) with attributes from the finetuning config (cfg).
      The `add_cfg_to_tree` arg adds `cfg` to the top of the yaml tree which is needed for all `hparams.yaml` files when passed as an arg to `load_from_checkpoint()`.
      """
      OmegaConf.set_struct(gpt_cfg, True)
      OmegaConf.resolve(cfg)
      with open_dict(gpt_cfg):
          gpt_cfg.megatron_amp_O2 = cfg.model.get(
              "megatron_amp_O2", False
          )
          gpt_cfg.micro_batch_size = (
              cfg.model.data.train_ds.micro_batch_size
          )
          gpt_cfg.global_batch_size = (
              cfg.model.data.train_ds.global_batch_size
          )
          gpt_cfg.sequence_parallel = cfg.model.get(
              "sequence_parallel", False
          )
          gpt_cfg.activations_checkpoint_granularity = cfg.model.get(
              "activations_checkpoint_granularity", None
          )
          gpt_cfg.activations_checkpoint_num_layers = cfg.model.get(
              "activations_checkpoint_num_layers", None
          )
          gpt_cfg.activations_checkpoint_method = cfg.model.get(
              "activations_checkpoint_method", None
          )
          gpt_cfg.activations_checkpoint_layers_per_pipeline = (
              cfg.model.get(
                  "activations_checkpoint_layers_per_pipeline", None
              )
          )
          gpt_cfg.data = cfg.model.data
          gpt_cfg.optim = cfg.model.optim
          gpt_cfg.precision = cfg.trainer.precision
          gpt_cfg.answer_only_loss = cfg.model.answer_only_loss
          gpt_cfg.restore_from_path = cfg.model.restore_from_path
          gpt_cfg.resume_from_checkpoint = (
              cfg.model.resume_from_checkpoint
          )
          gpt_cfg.save_nemo_on_validation_end = (
              cfg.model.save_nemo_on_validation_end
          )
          gpt_cfg.gradient_as_bucket_view = (
              cfg.model.gradient_as_bucket_view
          )
          gpt_cfg.hidden_dropout = cfg.model.get("hidden_dropout", 0.0)
          gpt_cfg.attention_dropout = cfg.model.get(
              "attention_dropout", 0.0
          )
          gpt_cfg.ffn_dropout = cfg.model.ffn_dropout
          gpt_cfg.use_flash_attention = cfg.model.get(
              "use_flash_attention", False
          )
          gpt_cfg.tensor_model_parallel_size = cfg.model.get(
              "tensor_model_parallel_size", 1
          )
          gpt_cfg.pipeline_model_parallel_size = cfg.model.get(
              "pipeline_model_parallel_size", 1
          )
          gpt_cfg.pipeline_model_parallel_split_rank = cfg.model.get(
              "pipeline_model_parallel_split_rank", 0
          )
  
          if cfg.model.data.get("chat", False):
              # chat model, overwrite the prompt template
              prompt_template = get_prompt_template_example(
                  cfg.model.data.chat_prompt_tokens
              )
              gpt_cfg.data.train_ds.prompt_template = prompt_template
              gpt_cfg.data.validation_ds.prompt_template = (
                  prompt_template
              )
              gpt_cfg.data.test_ds.prompt_template = prompt_template
  
          sft_cls = MegatronGPTSFTModel
          gpt_cfg.target = f"{sft_cls.__module__}.{sft_cls.__name__}"
  
          if cfg.model.get("use_flash_attention", None) is not None:
              gpt_cfg.use_flash_attention = (
                  cfg.model.use_flash_attention
              )
  
          if (
              cfg.model.get("seq_len_interpolation_factor", None)
              is not None
          ):
              gpt_cfg.seq_len_interpolation_factor = (
                  cfg.model.seq_len_interpolation_factor
              )
  
          if cfg.model.get("rotary_base", None) is not None:
              gpt_cfg.rotary_base = cfg.model.rotary_base
  
          sft_cls = MegatronGPTSFTModel
          gpt_cfg.target = f"{sft_cls.__module__}.{sft_cls.__name__}"
  
          # This is needed when modifying a hparam file directly to load `.ckpt` files.
          # This is not needed to modify the cfg in `.nemo` files.
          if add_cfg_to_tree:
              OmegaConf.resolve(gpt_cfg)
              gpt_cfg.cfg = gpt_cfg
  
      return gpt_cfg
  
file_path: NeMo/examples/nlp/language_modeling/tuning/megatron_gpt_sft.py
imports: import os
  import tempfile
  import torch.multiprocessing
  from omegaconf.omegaconf import OmegaConf, open_dict
  from pytorch_lightning import Trainer
  from pytorch_lightning.plugins.environments import TorchElasticEnvironment
  from nemo.collections.nlp.data.language_modeling.megatron.gpt_sft_chat_dataset import get_prompt_template_example
  from nemo.collections.nlp.models.language_modeling.megatron_gpt_sft_model import MegatronGPTSFTModel
  from nemo.collections.nlp.modules.common.megatron.megatron_init import fake_initialize_model_parallel
  from nemo.collections.nlp.parts.nlp_overrides import CustomProgressBar, GradScaler, MegatronHalfPrecisionPlugin, NLPDDPStrategy, NLPSaveRestoreConnector, PipelineMixedPrecisionPlugin
  from nemo.core.config import hydra_runner
  from nemo.utils import AppState, logging
  from nemo.utils.decorators import deprecated
  from nemo.utils.exp_manager import exp_manager
  from nemo.utils.model_utils import inject_model_parallel_rank
label_desc: Use image node_id 1 for calling a global function with example usage: _modify_config(gpt_cfg, cfg, add_cfg_to_tree) and returns: gpt_cfg