class_docstr: Builder type to hide complex configuration of PTL Trainers for Megatron LLM models.
  Can be extended to change behavior for a specific model.
docstr: Returns a DDP or a FSDP strategy passed to Trainer.strategy.
docstr_tok: ["Returns","a","DDP","or","a","FSDP","strategy","passed","to","Trainer.strategy","."]
code: def _training_strategy(
      self,
  ) -> Union[NLPDDPStrategy, NLPFSDPStrategy]:
      """
      Returns a DDP or a FSDP strategy passed to Trainer.strategy.
      """
      # check interactive environment
      _IS_INTERACTIVE = hasattr(sys, "ps1") or bool(
          sys.flags.interactive
      )
      if _IS_INTERACTIVE and self.cfg.trainer.devices == 1:
          logging.info(
              "Detected interactive environment, using NLPDDPStrategyNotebook"
          )
          return NLPDDPStrategyNotebook(
              no_ddp_communication_hook=True,
              find_unused_parameters=False,
          )
  
      if self.cfg.model.get("fsdp", False):
          assert (
              not self.cfg.model.optim.get("name")
              == "distributed_fused_adam"
          ), "Distributed optimizer cannot be used with FSDP."
          sharded_checkpoint = self.cfg.model.get(
              "fsdp_sharded_checkpoint", False
          )
          if self.cfg.model.get("tensor_model_parallel_size", 1) > 1:
              assert (
                  not sharded_checkpoint
              ), "FSDP sharded checkpoint is not supported when TP size > 1."
          if self.cfg.model.get("megatron_amp_O2", False):
              logging.info(
                  "Torch FSDP is not compatible with O2 precision recipe. Setting O2 `False`."
              )
              self.cfg.model.megatron_amp_O2 = False
          return NLPFSDPStrategy(
              limit_all_gathers=self.cfg.model.get(
                  "fsdp_limit_all_gathers", True
              ),
              sharding_strategy=self.cfg.model.get(
                  "fsdp_sharding_strategy", "full"
              ),
              cpu_offload=self.cfg.model.get("fsdp_cpu_offload", False),
              grad_reduce_dtype=self.cfg.model.get(
                  "fsdp_grad_reduce_dtype", 32
              ),
              sharded_checkpoint=sharded_checkpoint,
              precision=self.cfg.trainer.precision,
              nccl_communicator_config_path=self.cfg.model.get(
                  "nccl_communicator_config_path", None
              ),
          )
  
      return NLPDDPStrategy(
          no_ddp_communication_hook=True,
          gradient_as_bucket_view=self.cfg.model.gradient_as_bucket_view,
          find_unused_parameters=False,
          nccl_communicator_config_path=self.cfg.model.get(
              "nccl_communicator_config_path", None
          ),
          sharp=self.cfg.model.get("sharp", False),
      )
  
imports: import sys
  from typing import Union
  from omegaconf import DictConfig
  from pytorch_lightning import Trainer
  from pytorch_lightning.callbacks import ModelSummary
  from pytorch_lightning.plugins.environments import TorchElasticEnvironment
  from nemo.collections.nlp.parts.nlp_overrides import CustomProgressBar, GradScaler, MegatronHalfPrecisionPlugin, NLPDDPStrategy, NLPDDPStrategyNotebook, NLPFSDPStrategy, PipelineMixedPrecisionPlugin
  from nemo.utils import logging
label_desc: Use image node_id 2 for calling the MegatronTrainerBuilder obj's underlying member method code with example usage: obj._training_strategy() and returns: NLPDDPStrategy, NLPDDPStrategyNotebook, NLPFSDPStrategy