code: def convert(local_rank, rank, world_size, args):
      app_state = AppState()
      initialize_model_parallel_for_nemo(
          world_size=world_size,
          global_rank=rank,
          local_rank=local_rank,
          tensor_model_parallel_size=args.tensor_model_parallel_size,
          pipeline_model_parallel_size=args.pipeline_model_parallel_size,
          virtual_pipeline_model_parallel_size=None,
          pipeline_model_parallel_split_rank=0,
          micro_batch_size=None,
          global_batch_size=None,
          seed=1234,
          apex_transformer_log_level=30,
      )
      # hard set the data parallel rank to 0, otherwiaze it is default to None
      app_state.data_parallel_rank = 0
  
      # tensor_model_parallel_size = args.tensor_model_parallel_size
      num_nodes = world_size // args.gpus_per_node
      assert (
          world_size % args.gpus_per_node == 0
      ), "world_size must be divisible by gpus_per_node"
  
      trainer = Trainer(
          devices=args.gpus_per_node,
          accelerator="gpu",
          num_nodes=num_nodes,
      )
      checkpoint_path = megatron_lm_inject_model_parallel_rank(
          os.path.join(args.checkpoint_folder, args.checkpoint_name)
      )
      logging.info(f"loading checkpoint {checkpoint_path}")
  
      if args.model_type == "gpt":
          # this dictionary is used to rename the model parameters
          name_translate = {}
          name_translate["transformer"] = "encoder"
          name_translate[".attention."] = ".self_attention."
          # nemo megatron doesn't have _for_head key
          name_translate["word_embeddings_for_head"] = "word_embeddings"
          checkpoint, consumed, steps, version = load_from_checkpoint(
              MegatronGPTModel,
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
              translator=name_translate,
              strict=False,
          )
      elif args.model_type == "bert":
          # this dictionary is used to rename the model parameters
          name_translate = {}
          name_translate["transformer"] = "encoder"
          name_translate[".attention."] = ".self_attention."
          # nemo megatron doesn't have _for_head key
          name_translate["word_embeddings_for_head"] = "word_embeddings"
          checkpoint, consumed, steps, version = load_from_checkpoint(
              MegatronBertModel,
              checkpoint_path,
              hparams_file=args.hparams_file,
              trainer=trainer,
              translator=name_translate,
              strict=False,
          )
      else:
          raise NotImplemented(
              "{} is not supported".format(args.model_type)
          )
  
      if torch.distributed.is_initialized():
          torch.distributed.barrier()
  
      if args.output_ckpt_file_path:
          filepath = args.output_ckpt_file_path
          base_dir = pathlib.Path(filepath).parent
          filename_str = pathlib.Path(filepath).name
          suffix = ".ckpt"
          content = {}
          if consumed is not None:
              content["consumed"] = consumed
          else:
              content["consumed"] = 0
          if steps is not None:
              content["steps"] = steps
          else:
              content["steps"] = 0
          filename = filename_str.format(**content) + suffix
          checkpoint_path_output = inject_model_parallel_rank(
              os.path.join(base_dir, filename)
          )
          trainer.training_type_plugin.checkpoint_io.save_checkpoint(
              checkpoint, checkpoint_path_output
          )
          logging.info(
              f"NeMo model checkpoint files saved to: {args.output_ckpt_file_path}"
          )
  
      if args.nemo_file_path:
          if args.model_type == "gpt":
              model = load_model(
                  MegatronGPTModel,
                  checkpoint,
                  strict=False,
                  trainer=trainer,
              )
          elif args.model_type == "bert":
              model = load_model(
                  MegatronBertModel,
                  checkpoint,
                  strict=False,
                  trainer=trainer,
              )
          else:
              raise NotImplemented(
                  "{} is not supported".format(args.model_type)
              )
  
          # verify tensor parallel rank id and pipeline parallel rank id matches
          assert app_state.data_parallel_size == 1
          model._save_restore_connector = NLPSaveRestoreConnector()
          model.save_to(args.nemo_file_path)
          logging.info(f"NeMo model saved to: {args.nemo_file_path}")
  
imports: import importlib
  import os
  import pathlib
  import sys
  from argparse import ArgumentParser
  from collections import OrderedDict
  from typing import Any, Optional
  import torch
  from lightning_fabric.utilities.cloud_io import _load
  from megatron.core import parallel_state
  from pytorch_lightning.core.saving import _load_state
  from pytorch_lightning.core.saving import load_hparams_from_tags_csv, load_hparams_from_yaml
  from pytorch_lightning.trainer.trainer import Trainer
  from pytorch_lightning.utilities.migration import pl_legacy_patch
  from nemo.collections.nlp.models.language_modeling.megatron_bert_model import MegatronBertModel
  from nemo.collections.nlp.models.language_modeling.megatron_gpt_model import MegatronGPTModel
  from nemo.collections.nlp.modules.common.megatron.megatron_init import initialize_model_parallel_for_nemo
  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, uninject_model_parallel_rank
label_desc: Use image node_id 8 for calling a global function with example usage: convert(local_rank, rank, world_size, args) without return types