code: def convert(local_rank, rank, world_size, args):
      app_state = AppState()
      app_state.data_parallel_rank = 0
      num_nodes = world_size // args.gpus_per_node
      if args.bcp:
          trainer = Trainer(
              devices=args.gpus_per_node,
              num_nodes=num_nodes,
              accelerator="gpu",
              plugins=[TorchElasticEnvironment()],
          )
      else:
          trainer = Trainer(
              devices=args.gpus_per_node,
              num_nodes=num_nodes,
              accelerator="gpu",
          )
  
      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()
      )
  
      if args.ckpt_path.endswith("safetensors"):
          from safetensors.torch import load_file as load_safetensors
  
          checkpoint = load_safetensors(args.ckpt_path)
      else:
          checkpoint = pl_load(args.ckpt_path, map_location="cpu")
      if "state_dict" in checkpoint.keys():
          checkpoint = checkpoint["state_dict"]
      cfg = OmegaConf.load(args.hparams_file)
      cfg.model.inductor = False
      if args.model_type == "stable_diffusion":
          model = MegatronLatentDiffusion(cfg.model, trainer)
      elif args.model_type == "controlnet":
          model = MegatronControlNet(cfg.model, trainer)
  
      if "nemo" in model.cfg.cond_stage_config._target_:
          assert (
              args.nemo_clip_path is not None
          ), "To align with current hparams file, you need to provide .nemo checkpoint of clip model for stable diffusion. If you want to convert HF clip checkpoint to .nemo checkpoint first, please refer to /opt/NeMo/examples/multimodal/foundation/clip/convert_external_clip_to_nemo.py"
          _, clip_dict = load_config_and_state_from_nemo(
              args.nemo_clip_path
          )
      else:
          clip_dict = None
  
      state_dict = mapping_hf_state_dict(
          checkpoint, model, clip_dict=clip_dict
      )
  
      model._save_restore_connector = NLPSaveRestoreConnector()
  
      model.load_state_dict(state_dict)
  
      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/text_to_image/convert_hf_ckpt_to_nemo.py
imports: import os
  import tempfile
  from argparse import ArgumentParser
  import torch
  from lightning_fabric.utilities.cloud_io import _load
  from omegaconf import OmegaConf
  from pytorch_lightning.plugins.environments import TorchElasticEnvironment
  from pytorch_lightning.trainer.trainer import Trainer
  from nemo.collections.multimodal.models.text_to_image.controlnet.controlnet import MegatronControlNet
  from nemo.collections.multimodal.models.text_to_image.stable_diffusion.ldm.ddpm import MegatronLatentDiffusion
  from nemo.collections.nlp.parts.nlp_overrides import NLPSaveRestoreConnector
  from nemo.utils import AppState, logging
  from nemo.utils.distributed import initialize_distributed
label_desc: Use image node_id 4 for calling a global function with example usage: convert(local_rank, rank, world_size, args) without return types