class_docstr: Megatron NMT training
class_docstr_tok: ["Megatron","NMT","training"]
docstr: Translates list of sentences from source language to target language.
  Should be regular text, this method performs its own tokenization/de-tokenization
  Args:
      text: list of strings to translate
      source_lang: if not "ignore", corresponding MosesTokenizer and MosesPunctNormalizer will be run
      target_lang: if not "ignore", corresponding MosesDecokenizer will be run
      return_beam_scores: if True, returns a list of translations and their corresponding beam scores.
      log_timing: if True, prints timing information.
  Returns:
      list of translated strings
code: def translate(
      self,
      text: List[str],
      source_lang: str = None,
      target_lang: str = None,
      return_beam_scores: bool = False,
      log_timing: bool = False,
  ) -> List[str]:
      """
      Translates list of sentences from source language to target language.
      Should be regular text, this method performs its own tokenization/de-tokenization
      Args:
          text: list of strings to translate
          source_lang: if not "ignore", corresponding MosesTokenizer and MosesPunctNormalizer will be run
          target_lang: if not "ignore", corresponding MosesDecokenizer will be run
          return_beam_scores: if True, returns a list of translations and their corresponding beam scores.
          log_timing: if True, prints timing information.
      Returns:
          list of translated strings
      """
      # __TODO__: This will reset both source and target processors even if you want to reset just one.
      # NOTE: This will also set up appropriate source and target processors for a given src/tgt language for multilingual models instead of creating a list of them.
      if source_lang is not None or target_lang is not None:
          (
              self.source_processor,
              self.target_processor,
          ) = MTEncDecModel.setup_pre_and_post_processing_utils(
              source_lang,
              target_lang,
              self.encoder_tokenizer_library,
              self.decoder_tokenizer_library,
          )
  
      mode = self.training
      prepend_ids = []
      if (
          self.multilingual
          and self.multilingual_type
          != MultilingualModelType.many_to_one
      ):
          if target_lang is None:
              raise ValueError(
                  "target_lang needs to be specified to run inference for multilingual model."
              )
          tgt_symbol = self.encoder_tokenizer.token_to_id(
              "<" + target_lang + ">"
          )
          if tgt_symbol in self.multilingual_ids:
              prepend_ids = [tgt_symbol]
          else:
              print(
                  "WARNING: Target language ID not found in multilingual model. Prepending nothing."
              )
  
      if log_timing:
          timer = timers.NamedTimer()
      else:
          timer = None
  
      cache = {
          "timer": timer,
      }
  
      try:
          self.eval()
          self.training = False
          src, src_mask = MTEncDecModel.prepare_inference_batch(
              text=text,
              prepend_ids=prepend_ids,
              target=False,
              source_processor=self.source_processor,
              target_processor=self.target_processor,
              encoder_tokenizer=self.encoder_tokenizer,
              decoder_tokenizer=self.decoder_tokenizer,
              device=self.device,
          )
          with torch.inference_mode():
              predicted_tokens_ids, _ = self.decode(
                  src,
                  src_mask,
                  src.size(1)
                  + self._cfg.max_generation_delta,  # Generate up to src-length + max generation delta. TODO: Implement better stopping when everything hits <EOS>.
                  tokenizer=self.decoder_tokenizer,
              )
          best_translations = self.postprocess_outputs(
              outputs=predicted_tokens_ids,
              tokenizer=self.decoder_tokenizer,
              processor=self.target_processor,
          )
          return_val = best_translations
      finally:
          self.train(mode=mode)
  
      if log_timing:
          timing = timer.export()
          timing["mean_src_length"] = (
              src_mask.sum().cpu().item() / src_mask.shape[0]
          )
          tgt, tgt_mask = self.prepare_inference_batch(
              text=best_translations,
              prepend_ids=prepend_ids,
              target=True,
              source_processor=self.source_processor,
              target_processor=self.target_processor,
              encoder_tokenizer=self.encoder_tokenizer,
              decoder_tokenizer=self.decoder_tokenizer,
              device=self.device,
          )
          timing["mean_tgt_length"] = (
              tgt_mask.sum().cpu().item() / tgt_mask.shape[0]
          )
  
          if type(return_val) is tuple:
              return_val = return_val + (timing,)
          else:
              return_val = (return_val, timing)
  
      return return_val
  
imports: import enum
  import itertools
  import random
  from typing import List, Optional
  import numpy
  import torch
  from omegaconf.dictconfig import DictConfig
  from omegaconf.listconfig import ListConfig
  from pytorch_lightning.trainer.trainer import Trainer
  from sacrebleu import corpus_bleu
  from nemo.collections.nlp.data.common.sequence_to_sequence_dataset import BinarizedMemmapSequenceToSequenceDataset, TextMemmapSequenceToSequenceDataset
  from nemo.collections.nlp.data.language_modeling.megatron.base_dataset_utils import get_datasets_weights_and_num_samples
  from nemo.collections.nlp.data.language_modeling.megatron.blendable_dataset import BlendableDataset
  from nemo.collections.nlp.data.language_modeling.megatron.megatron_batch_samplers import MegatronPretrainingBatchSampler
  from nemo.collections.nlp.data.language_modeling.megatron.xlm_dataset import BinarizedMemmapCrossLingualMLMAndTranslationDataset, TextMemmapCrossLingualMLMAndTranslationDataset
  from nemo.collections.nlp.models.language_modeling.megatron_lm_encoder_decoder_model import MegatronLMEncoderDecoderModel
  from nemo.collections.nlp.models.language_modeling.megatron_t5_model import MegatronT5Model
  from nemo.collections.nlp.models.machine_translation.mt_enc_dec_model import MTEncDecModel
  from nemo.collections.nlp.modules.common.megatron.megatron_export import DecEmb, EncEmb, TokensHeadEmb
  from nemo.collections.nlp.modules.common.megatron.utils import get_iterator_k_split
  from nemo.collections.nlp.parts.nlp_overrides import GlobalBatchDataFetcher
  from nemo.collections.nlp.parts.utils_funcs import get_last_rank
  from nemo.core.classes import Exportable
  from nemo.utils import AppState, logging, timers
label_desc: Use image node_id 27 for calling the MegatronNMTModel obj's underlying member method code with example usage: obj.translate(text, source_lang, target_lang, return_beam_scores, log_timing) and returns: return_val