code: def convert(local_rank, rank, world_size, args):
      app_state = AppState()
      app_state.data_parallel_rank = 0
  
      cfg = OmegaConf.load(args.hparams_file)
      with open_dict(cfg):
          cfg["model"] = cfg["cfg"]
          cfg["trainer"] = {"precision": cfg["model"]["precision"]}
          if args.bcp:
              cfg["cluster_type"] = "BCP"
      trainer = MegatronTrainerBuilder(cfg).create_trainer()
  
      app_state.pipeline_model_parallel_size = (
          args.pipeline_model_parallel_size
      )
      app_state.tensor_model_parallel_size = (
          args.tensor_model_parallel_size
      )
  
      # no use atm, use to split ranks in encoder/decoder models.
      if (
          args.pipeline_model_parallel_size > 1
          and args.model_type in []
      ):
          if args.pipeline_model_parallel_split_rank is not None:
              app_state.pipeline_model_parallel_split_rank = (
                  args.pipeline_model_parallel_split_rank
              )
          else:
              if args.pipeline_model_parallel_size % 2 != 0:
                  raise ValueError(
                      f"Pipeline model parallel size {args.pipeline_model_parallel_size} must be even if split rank is not specified."
                  )
              else:
                  # If split rank is not set, then we set it to be pipeline_model_parallel_size // 2 - this is because in most cases we have the same number of enc/dec layers.
                  app_state.pipeline_model_parallel_split_rank = (
                      args.pipeline_model_parallel_size // 2
                  )
      else:
          app_state.pipeline_model_parallel_split_rank = None
  
      app_state.model_parallel_size = (
          app_state.tensor_model_parallel_size
          * app_state.pipeline_model_parallel_size
      )
  
      parallel_state.initialize_model_parallel(
          tensor_model_parallel_size=app_state.tensor_model_parallel_size,
          pipeline_model_parallel_size=app_state.pipeline_model_parallel_size,
          pipeline_model_parallel_split_rank=app_state.pipeline_model_parallel_split_rank,
      )
  
      app_state.pipeline_model_parallel_rank = (
          parallel_state.get_pipeline_model_parallel_rank()
      )
      app_state.tensor_model_parallel_rank = (
          parallel_state.get_tensor_model_parallel_rank()
      )
  
      # inject model parallel rank
      checkpoint_path = inject_model_parallel_rank(
          os.path.join(args.checkpoint_folder, args.checkpoint_name)
      )
  
      logging.info(
          f"rank: {rank}, local_rank: {local_rank}, is loading checkpoint: {checkpoint_path} for tp_rank: {app_state.tensor_model_parallel_rank} and pp_rank: {app_state.pipeline_model_parallel_rank}"
      )
  
      if args.model_type == "megatron_clip":
          model = MegatronCLIPModel.load_from_checkpoint(
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
          )
      elif args.model_type == "stable_diffusion":
          model = MegatronLatentDiffusion.load_from_checkpoint(
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
          )
      elif args.model_type == "instruct_pix2pix":
          model = MegatronLatentDiffusionEdit.load_from_checkpoint(
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
          )
      elif args.model_type == "dreambooth":
          model = MegatronLatentDiffusion.load_from_checkpoint(
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
          )
      elif args.model_type == "imagen":
          model = MegatronImagen.load_from_checkpoint(
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
          )
      elif args.model_type == "controlnet":
          model = MegatronControlNet.load_from_checkpoint(
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
          )
      elif args.model_type == "kosmos":
          model = MegatronKosmosModel.load_from_checkpoint(
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
          )
      elif args.model_type == "neva":
          model = MegatronNevaModel.load_from_checkpoint(
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
          )
      else:
          raise ValueError(
              f"Unrecognized model_type {args.model_type}."
          )
  
      model._save_restore_connector = NLPSaveRestoreConnector()
  
      if torch.distributed.is_initialized():
          torch.distributed.barrier()
  
      model.save_to(args.nemo_file_path)
  
      logging.info(f"NeMo model saved to: {args.nemo_file_path}")
  
file_path: NeMo/examples/multimodal/convert_ckpt_to_nemo.py
imports: import os
  from argparse import ArgumentParser
  import torch
  from omegaconf.omegaconf import OmegaConf, open_dict
  from nemo.collections.multimodal.models.multimodal_llm.neva.neva_model import MegatronNevaModel
  from nemo.collections.multimodal.models.text_to_image.controlnet.controlnet import MegatronControlNet
  from nemo.collections.multimodal.models.text_to_image.imagen.imagen import MegatronImagen
  from nemo.collections.multimodal.models.text_to_image.instruct_pix2pix.ldm.ddpm_edit import MegatronLatentDiffusionEdit
  from nemo.collections.multimodal.models.text_to_image.stable_diffusion.ldm.ddpm import MegatronLatentDiffusion
  from nemo.collections.multimodal.models.vision_language_foundation.clip.megatron_clip_models import MegatronCLIPModel
  from nemo.collections.nlp.parts.megatron_trainer_builder import MegatronTrainerBuilder
  from nemo.collections.nlp.parts.nlp_overrides import NLPSaveRestoreConnector
  from nemo.utils import AppState, logging
  from nemo.utils.distributed import initialize_distributed
  from nemo.utils.model_utils import inject_model_parallel_rank
label_desc: Use image node_id 2 for calling a global function with example usage: convert(local_rank, rank, world_size, args) without return types