code: def translate(rank, world_size, args):
      if args.model.endswith(".nemo"):
          logging.info("Attempting to initialize from .nemo file")
          model = MTEncDecModel.restore_from(
              restore_path=args.model, map_location=f"cuda:{rank}"
          )
      elif args.model.endswith(".ckpt"):
          logging.info("Attempting to initialize from .ckpt file")
          model = MTEncDecModel.load_from_checkpoint(
              checkpoint_path=args.model, map_location=f"cuda:{rank}"
          )
      model.replace_beam_with_sampling(topk=args.topk)
      model.eval()
      if args.twoside:
          dataset = TarredTranslationDataset(
              text_tar_filepaths=args.text2translate,
              metadata_path=args.metadata_path,
              encoder_tokenizer=model.encoder_tokenizer,
              decoder_tokenizer=model.decoder_tokenizer,
              shuffle_n=100,
              shard_strategy="scatter",
              world_size=world_size,
              global_rank=rank,
              reverse_lang_direction=args.reverse_lang_direction,
          )
      else:
          dataset = TarredSentenceDataset(
              text_tar_filepaths=args.text2translate,
              metadata_path=args.metadata_path,
              tokenizer=model.encoder_tokenizer,
              shuffle_n=100,
              shard_strategy="scatter",
              world_size=world_size,
              global_rank=rank,
          )
      loader = DataLoader(dataset, batch_size=1)
      result_dir = os.path.join(args.result_dir, f"rank{rank}")
      os.makedirs(result_dir, exist_ok=True)
      originals_file_name = os.path.join(result_dir, "originals.txt")
      translations_file_name = os.path.join(
          result_dir, "translations.txt"
      )
      num_translated_sentences = 0
  
      with open(originals_file_name, "w") as of, open(
          translations_file_name, "w"
      ) as tf:
          for batch_idx, batch in enumerate(loader):
              for i in range(len(batch)):
                  if batch[i].ndim == 3:
                      batch[i] = batch[i].squeeze(dim=0)
                  batch[i] = batch[i].to(rank)
              if args.twoside:
                  src_ids, src_mask, _, _, _ = batch
              else:
                  src_ids, src_mask = batch
              if batch_idx % 100 == 0:
                  logging.info(
                      f"{batch_idx} batches ({num_translated_sentences} sentences) were translated by process with "
                      f"rank {rank}"
                  )
              num_translated_sentences += len(src_ids)
              inputs, translations = model.batch_translate(
                  src=src_ids, src_mask=src_mask
              )
              for src, translation in zip(inputs, translations):
                  of.write(src + "\n")
                  tf.write(translation + "\n")
  
file_path: NeMo/examples/nlp/machine_translation/translate_ddp.py
imports: import os
  from argparse import ArgumentParser
  import torch
  import torch.multiprocessing
  from torch.utils.data import DataLoader
  from nemo.collections.nlp.data.language_modeling import TarredSentenceDataset
  from nemo.collections.nlp.data.machine_translation import TarredTranslationDataset
  from nemo.collections.nlp.models.machine_translation.mt_enc_dec_model import MTEncDecModel
  from nemo.utils import logging
label_desc: Use image node_id 2 for calling a global function with example usage: translate(rank, world_size, args) without return types