class_docstr: Translate a batch of sentences with a saved model.
  
  Args:
      model (onmt.modules.NMTModel): NMT model to use for translation
      vocabs (dict[str, Vocab]): A dict
          mapping each side's Vocab.
      gpu (int): GPU device. Set to negative for no GPU.
      n_best (int): How many beams to wait for.
      min_length (int): See
          :class:`onmt.translate.decode_strategy.DecodeStrategy`.
      max_length (int): See
          :class:`onmt.translate.decode_strategy.DecodeStrategy`.
      beam_size (int): Number of beams.
      random_sampling_topk (int): See
          :class:`onmt.translate.greedy_search.GreedySearch`.
      random_sampling_temp (float): See
          :class:`onmt.translate.greedy_search.GreedySearch`.
      stepwise_penalty (bool): Whether coverage penalty is applied every step
          or not.
      dump_beam (bool): Debugging option.
      block_ngram_repeat (int): See
          :class:`onmt.translate.decode_strategy.DecodeStrategy`.
      ignore_when_blocking (set or frozenset): See
          :class:`onmt.translate.decode_strategy.DecodeStrategy`.
      replace_unk (bool): Replace unknown token.
      tgt_file_prefix (bool): Force the predictions begin with provided -tgt.
      data_type (str): Source data type.
      verbose (bool): Print/log every translation.
      report_time (bool): Print/log total time/frequency.
      copy_attn (bool): Use copy attention.
      global_scorer (onmt.translate.GNMTGlobalScorer): Translation
          scoring/reranking object.
      out_file (TextIO or codecs.StreamReaderWriter): Output file.
      report_score (bool) : Whether to report scores
      logger (logging.Logger or NoneType): Logger.
class_docstr_tok: ["Translate","a","batch","of","sentences","with","a","saved","model",".","Args",":","model","(","onmt.modules.NMTModel",")",":","NMT","model","to","use","for","translation","vocabs","(","dict","[","str",",","Vocab","]",")",":","A","dict","mapping","each","side","'s","Vocab",".","gpu","(","int",")",":","GPU","device",".","Set","to","negative","for","no","GPU",".","n_best","(","int",")",":","How","many","beams","to","wait","for",".","min_length","(","int",")",":","See",":","class",":","`","onmt.translate.decode_strategy.DecodeStrategy","`",".","max_length","(","int",")",":","See",":","class",":","`","onmt.translate.decode_strategy.DecodeStrategy","`",".","beam_size","(","int",")",":","Number","of","beams",".","random_sampling_topk","(","int",")",":","See",":","class",":","`","onmt.translate.greedy_search.GreedySearch","`",".","random_sampling_temp","(","float",")",":","See",":","class",":","`","onmt.translate.greedy_search.GreedySearch","`",".","stepwise_penalty","(","bool",")",":","Whether","coverage","penalty","is","applied","every","step","or","not",".","dump_beam","(","bool",")",":","Debugging","option",".","block_ngram_repeat","(","int",")",":","See",":","class",":","`","onmt.translate.decode_strategy.DecodeStrategy","`",".","ignore_when_blocking","(","set","or","frozenset",")",":","See",":","class",":","`","onmt.translate.decode_strategy.DecodeStrategy","`",".","replace_unk","(","bool",")",":","Replace","unknown","token",".","tgt_file_prefix","(","bool",")",":","Force","the","predictions","begin","with","provided","-tgt",".","data_type","(","str",")",":","Source","data","type",".","verbose","(","bool",")",":","Print\/log","every","translation",".","report_time","(","bool",")",":","Print\/log","total","time\/frequency",".","copy_attn","(","bool",")",":","Use","copy","attention",".","global_scorer","(","onmt.translate.GNMTGlobalScorer",")",":","Translation","scoring\/reranking","object",".","out_file","(","TextIO","or","codecs.StreamReaderWriter",")",":","Output","file",".","report_score","(","bool",")",":","Whether","to","report","scores","logger","(","logging.Logger","or","NoneType",")",":","Logger","."]
docstr: Translate content of ``src`` and get gold scores from ``tgt``.
  
  Args:
      infer_iter: tensored batch iterator from DynamicDatasetIter
      attn_debug (bool): enables the attention logging
      align_debug (bool): enables the word alignment logging
  
  Returns:
      (`list`, `list`)
  
      * all_scores is a list of `batch_size` lists of `n_best` scores
      * all_predictions is a list of `batch_size` lists
          of `n_best` predictions
docstr_tok: ["Translate","content","of","``","src","``","and","get","gold","scores","from","``","tgt","``",".","Args",":","infer_iter",":","tensored","batch","iterator","from","DynamicDatasetIter","attn_debug","(","bool",")",":","enables","the","attention","logging","align_debug","(","bool",")",":","enables","the","word","alignment","logging","Returns",":","(","`","list","`",",","`","list","`",")","*","all_scores","is","a","list","of","`","batch_size","`","lists","of","`","n_best","`","scores","*","all_predictions","is","a","list","of","`","batch_size","`","lists","of","`","n_best","`","predictions"]
code: def _translate(
      self,
      infer_iter,
      transform=None,
      attn_debug=False,
      align_debug=False,
      phrase_table="",
  ):
      """Translate content of ``src`` and get gold scores from ``tgt``.
  
      Args:
          infer_iter: tensored batch iterator from DynamicDatasetIter
          attn_debug (bool): enables the attention logging
          align_debug (bool): enables the word alignment logging
  
      Returns:
          (`list`, `list`)
  
          * all_scores is a list of `batch_size` lists of `n_best` scores
          * all_predictions is a list of `batch_size` lists
              of `n_best` predictions
      """
      transform_pipe = (
          TransformPipe.build_from(
              [transform[name] for name in transform]
          )
          if transform
          else None
      )
      xlation_builder = onmt.translate.TranslationBuilder(
          self.vocabs,
          self.n_best,
          self.replace_unk,
          self.phrase_table,
      )
  
      # Statistics
      counter = count(1)
      pred_score_total, pred_words_total = 0, 0
      gold_score_total, gold_words_total = 0, 0
  
      all_scores = []
      all_predictions = []
  
      start_time = time()
  
      def _maybe_retranslate(translations, batch):
          """Here we handle the cases of mismatch in number of segments
          between source and target. We re-translate seg by seg."""
          inds, perm = torch.sort(batch["ind_in_bucket"])
          trans_copy = deepcopy(translations)
          inserted_so_far = 0
          for j, trans in enumerate(translations):
              if (
                  trans.src == self._tgt_sep_idx
              ).sum().item() != trans.pred_sents[0].count(
                  DefaultTokens.SEP
              ):
                  self._log("Mismatch in number of ((newline))")
                  # those two should be the same except feat dim
                  # batch['src'][perm[j], :, :])
                  # trans.src
  
                  # we rebuild a small batch made of the sub-segments
                  # in the long segment.
                  idx = (trans.src == self._tgt_sep_idx).nonzero()
                  sub_src = []
                  start_idx = 0
                  for i in range(len(idx)):
                      end_idx = idx[i]
                      sub_src.append(
                          batch["src"][perm[j], start_idx:end_idx, :]
                      )
                      start_idx = end_idx + 1
                  end_idx = (
                      batch["src"][perm[j], :, 0]
                      .ne(self._tgt_pad_idx)
                      .sum()
                      - 1
                  )
                  sub_src.append(
                      batch["src"][perm[j], start_idx:end_idx, :]
                  )
                  t_sub_src = pad_sequence(
                      sub_src,
                      batch_first=True,
                      padding_value=self._tgt_pad_idx,
                  )
                  t_sub_src_len = (
                      t_sub_src[:, :, 0].ne(self._tgt_pad_idx).sum(1)
                  )
                  t_sub_src_ind = torch.tensor(
                      [i for i in range(len(sub_src))],
                      dtype=torch.int16,
                  )
                  device = batch["src"].device
                  t_sub_batch = {
                      "src": t_sub_src.to(device),
                      "srclen": t_sub_src_len.to(device),
                      "ind_in_bucket": t_sub_src_ind.to(device),
                  }
                  # new sub-batch ready to be translated
                  sub_data = self.translate_batch(
                      t_sub_batch, attn_debug
                  )
                  sub_trans = xlation_builder.from_batch(sub_data)
  
                  # we re-insert the sub-batch in the initial translations
                  trans_copy[j + inserted_so_far] = sub_trans[0]
                  for i in range(1, len(sub_src)):
                      trans_copy.insert(
                          j + i + inserted_so_far, sub_trans[i]
                      )
                  inserted_so_far += len(sub_src) - 1
          return trans_copy
  
      def _process_bucket(bucket_translations):
          bucket_scores = []
          bucket_predictions = []
          bucket_score = 0
          bucket_words = 0
          bucket_gold_score = 0
          bucket_gold_words = 0
          voc_src = self.vocabs["src"].ids_to_tokens
          bucket_translations = sorted(
              bucket_translations, key=lambda x: x.ind_in_bucket
          )
          for trans in bucket_translations:
              bucket_scores += [trans.pred_scores[: self.n_best]]
              bucket_score += trans.pred_scores[0]
              bucket_words += len(trans.pred_sents[0])
              if "tgt" in batch.keys():
                  bucket_gold_score += trans.gold_score
                  bucket_gold_words += len(trans.gold_sent) + 1
  
              n_best_preds = [
                  " ".join(pred)
                  for pred in trans.pred_sents[: self.n_best]
              ]
  
              if self.report_align:
                  align_pharaohs = [
                      build_align_pharaoh(align)
                      for align in trans.word_aligns[: self.n_best]
                  ]
                  n_best_preds_align = [
                      " ".join(align[0]) for align in align_pharaohs
                  ]
                  n_best_preds = [
                      pred + DefaultTokens.ALIGNMENT_SEPARATOR + align
                      for pred, align in zip(
                          n_best_preds, n_best_preds_align
                      )
                  ]
  
              if transform_pipe is not None:
                  n_best_preds = transform_pipe.batch_apply_reverse(
                      n_best_preds
                  )
  
              bucket_predictions += [n_best_preds]
  
              if self.with_score:
                  n_best_scores = [
                      score.item()
                      for score in trans.pred_scores[: self.n_best]
                  ]
                  out_all = [
                      pred + "\t" + str(score)
                      for (pred, score) in zip(
                          n_best_preds, n_best_scores
                      )
                  ]
                  self.out_file.write("\n".join(out_all) + "\n")
              else:
                  self.out_file.write("\n".join(n_best_preds) + "\n")
              self.out_file.flush()
  
              if self.verbose:
                  srcs = [
                      voc_src[tok] for tok in trans.src[: trans.srclen]
                  ]
                  sent_number = next(counter)
                  output = trans.log(sent_number, src_raw=srcs)
                  self._log(output)
  
              if attn_debug:
                  preds = trans.pred_sents[0]
                  preds.append(DefaultTokens.EOS)
                  attns = trans.attns[0].tolist()
                  if self.data_type == "text":
                      srcs = [
                          voc_src[tok]
                          for tok in trans.src[: trans.srclen].tolist()
                      ]
                  else:
                      srcs = [
                          str(item) for item in range(len(attns[0]))
                      ]
                  output = report_matrix(srcs, preds, attns)
                  self._log(output)
  
              if align_debug:
                  if self.gold_align:
                      tgts = trans.gold_sent
                  else:
                      tgts = trans.pred_sents[0]
                  align = trans.word_aligns[0].tolist()
                  if self.data_type == "text":
                      srcs = [
                          voc_src[tok]
                          for tok in trans.src[: trans.srclen].tolist()
                      ]
                  else:
                      srcs = [
                          str(item) for item in range(len(align[0]))
                      ]
                  output = report_matrix(srcs, tgts, align)
                  self._log(output)
  
          return (
              bucket_scores,
              bucket_predictions,
              bucket_score,
              bucket_words,
              bucket_gold_score,
              bucket_gold_words,
          )
  
      bucket_translations = []
      prev_idx = 0
  
      for batch, bucket_idx in infer_iter:
          batch_data = self.translate_batch(batch, attn_debug)
  
          translations = xlation_builder.from_batch(batch_data)
          if (
              not isinstance(self, GeneratorLM)
              and self._tgt_sep_idx != self._tgt_unk_idx
              and (batch["src"] == self._tgt_sep_idx).any().item()
          ):
              # For seq2seq when we need to force doc to spit the same number of sents
              translations = _maybe_retranslate(translations, batch)
  
          bucket_translations += translations
  
          if (
              not isinstance(infer_iter, list)
              and len(bucket_translations) >= infer_iter.bucket_size
          ):
              bucket_idx += 1
  
          if bucket_idx != prev_idx:
              prev_idx = bucket_idx
              (
                  bucket_scores,
                  bucket_predictions,
                  bucket_score,
                  bucket_words,
                  bucket_gold_score,
                  bucket_gold_words,
              ) = _process_bucket(bucket_translations)
              all_scores += bucket_scores
              all_predictions += bucket_predictions
              pred_score_total += bucket_score
              pred_words_total += bucket_words
              gold_score_total += bucket_gold_score
              gold_words_total += bucket_gold_words
              bucket_translations = []
  
      if len(bucket_translations) > 0:
          (
              bucket_scores,
              bucket_predictions,
              bucket_score,
              bucket_words,
              bucket_gold_score,
              bucket_gold_words,
          ) = _process_bucket(bucket_translations)
          all_scores += bucket_scores
          all_predictions += bucket_predictions
          pred_score_total += bucket_score
          pred_words_total += bucket_words
          gold_score_total += bucket_gold_score
          gold_words_total += bucket_gold_words
  
      end_time = time()
  
      if self.report_score:
          msg = self._report_score(
              "PRED", pred_score_total, len(all_scores)
          )
          self._log(msg)
          if "tgt" in batch.keys() and not self.tgt_file_prefix:
              msg = self._report_score(
                  "GOLD", gold_score_total, len(all_scores)
              )
              self._log(msg)
  
      if self.report_time:
          total_time = end_time - start_time
          self._log("Total translation time (s): %.1f" % total_time)
          self._log(
              "Average translation time (ms): %.1f"
              % (total_time / len(all_predictions) * 1000)
          )
          self._log(
              "Tokens per second: %.1f"
              % (pred_words_total / total_time)
          )
  
      if self.dump_beam:
          import json
  
          json.dump(
              self.translator.beam_accum,
              codecs.open(self.dump_beam, "w", "utf-8"),
          )
  
      return all_scores, all_predictions
  
imports: import torch
  from torch.nn.functional import log_softmax
  from torch.nn.utils.rnn import pad_sequence
  import codecs
  from time import time
  from math import exp
  from itertools import count, zip_longest
  from copy import deepcopy
  import onmt.model_builder
  import onmt.decoders.ensemble
  from onmt.constants import DefaultTokens
  from onmt.translate.beam_search import BeamSearch, BeamSearchLM
  from onmt.translate.greedy_search import GreedySearch, GreedySearchLM
  from onmt.utils.misc import tile, set_random_seed, report_matrix
  from onmt.utils.alignment import extract_alignment, build_align_pharaoh
  from onmt.modules.copy_generator import collapse_copy_scores
  from onmt.constants import ModelTask
  from onmt.transforms import TransformPipe
label_desc: Use image node_id 5 for calling the Inference obj's underlying member method code with example usage: obj._translate(infer_iter, transform, attn_debug, align_debug, phrase_table) and returns: all_scores,  all_predictions, trans_copy, bucket_scores,  bucket_predictions,  bucket_score,  bucket_words,  bucket_gold_score,  bucket_gold_words