class_docstr: Megatron GPT Supervised Fine-Tuning
class_docstr_tok: ["Megatron","GPT","Supervised","Fine-Tuning"]
code: def __init__(self, cfg: DictConfig, trainer: Trainer):
      if not HAVE_APEX:
          raise ImportError(
              "Apex was not found. Please see the NeMo README for installation instructions: https://github.com/NVIDIA/NeMo#megatron-gpt."
          )
      super().__init__(cfg, trainer=trainer)
      self.sep_id = cfg.get("sep_id", 49704)
      if hasattr(self.cfg.data, "validation_ds"):
          self.val_metric, self.val_metric_name = self.setup_metric(
              self.cfg.data.validation_ds
          )
          self.val_metric = (
              torch.nn.ModuleList(self.val_metric)
              if self.val_metric is not None
              else None
          )
          # Used other keys from metadata to calulate metrics
          if hasattr(self.cfg.data.validation_ds, "metric"):
              self.val_metric_label_key = (
                  self.cfg.data.validation_ds.metric.get(
                      "label_key", "labels"
                  )
              )
  
      if hasattr(self.cfg.data, "test_ds"):
          self.test_metric, self.test_metric_name = self.setup_metric(
              self.cfg.data.test_ds
          )
          self.test_metric = (
              torch.nn.ModuleList(self.test_metric)
              if self.test_metric is not None
              else None
          )
          # Used other keys from metadata to calulate metrics
          if hasattr(self.cfg.data.test_ds, "metric"):
              self.test_metric_label_key = (
                  self.cfg.data.test_ds.metric.get(
                      "label_key", "labels"
                  )
              )
  
      if self.cfg.get("megatron_amp_O2", False):
          base_module = self.model.module
      else:
          base_module = self.model
  
      # Set the profile start and end steps in the unit of global batach
      if hasattr(self, "_nsys_profile_enabled"):
          self._nsys_profile_start_step = self.cfg.nsys_profile.get(
              "start_step", 0
          )
          self._nsys_profile_end_step = self.cfg.nsys_profile.get(
              "end_step", 0
          )
  
      self._reset_activation_checkpointing_args()
      self.virtual_tokens = 0
  
imports: import itertools
  import json
  from functools import partial
  from typing import Any, Optional
  import torch
  from omegaconf import DictConfig, ListConfig
  from pytorch_lightning.trainer.trainer import Trainer
  from nemo.collections.common.metrics import MetricStringToTorchMetric
  from nemo.collections.nlp.data.language_modeling.megatron.base_dataset_utils import get_datasets_weights_and_num_samples
  from nemo.collections.nlp.data.language_modeling.megatron.blendable_dataset import BlendableDataset
  from nemo.collections.nlp.data.language_modeling.megatron.gpt_sft_chat_dataset import GPTSFTChatDataset
  from nemo.collections.nlp.data.language_modeling.megatron.gpt_sft_dataset import GPTSFTDataset, GPTSFTPackedDataset
  from nemo.collections.nlp.data.language_modeling.megatron.megatron_batch_samplers import MegatronPretrainingBatchSampler
  from nemo.collections.nlp.models.language_modeling.megatron_gpt_model import MegatronGPTModel
  from nemo.collections.nlp.modules.common.megatron.utils import get_iterator_k_split
  from nemo.collections.nlp.modules.common.text_generation_utils import generate, get_computeprob_response
  from nemo.collections.nlp.parts.mixins.nlp_adapter_mixins import NLPAdapterModelMixin
  from nemo.collections.nlp.parts.utils_funcs import get_last_rank
  from nemo.utils import AppState, logging
label_desc: Use image node_id 1 to create a new MegatronGPTSFTModel object from inherited base classes: NLPAdapterModelMixin, MegatronGPTModel with example: obj = MegatronGPTSFTModel(cfg, trainer)