docstr: Translate a batch of sentences step by step using cache.
  
  Args:
      batch: a batch of sentences, yield by data iterator.
      decode_strategy (DecodeStrategy): A decode strategy to use for
          generate translation step by step.
  
  Returns:
      results (dict): The translation results.
docstr_tok: ["Translate","a","batch","of","sentences","step","by","step","using","cache",".","Args",":","batch",":","a","batch","of","sentences",",","yield","by","data","iterator",".","decode_strategy","(","DecodeStrategy",")",":","A","decode","strategy","to","use","for","generate","translation","step","by","step",".","Returns",":","results","(","dict",")",":","The","translation","results","."]
code: def _translate_batch_with_strategy(
      self, batch, decode_strategy, left_pad=True
  ):
      """Translate a batch of sentences step by step using cache.
  
      Args:
          batch: a batch of sentences, yield by data iterator.
          decode_strategy (DecodeStrategy): A decode strategy to use for
              generate translation step by step.
  
      Returns:
          results (dict): The translation results.
      """
      # (0) Prep the components of the search.
      use_src_map = self.copy_attn
      parallel_paths = decode_strategy.parallel_paths  # beam_size
      batch_size = len(batch["srclen"])
  
      # (1) split src into src and target_prefix to avoid padding.
      src = batch["src"]
      src_len = batch["srclen"]
  
      if left_pad:
          target_prefix = None
      else:
          (
              src,
              src_len,
              target_prefix,
          ) = self.split_src_to_prevent_padding(src, src_len)
  
      # (2) init decoder
      self.model.decoder.init_state(src, None, None)
      gold_score = self._gold_score(
          batch,
          None,
          src_len,
          use_src_map,
          None,
          batch_size,
          src,
      )
  
      # (3) prep decode_strategy. Possibly repeat src objects.
      src_map = batch["src_map"] if use_src_map else None
      (fn_map_state, src, src_map) = decode_strategy.initialize(
          src,
          src_len,
          src_map,
          target_prefix=target_prefix,
      )
  
      # (4) Begin decoding step by step:
      # beg_time = time()
      for step in range(decode_strategy.max_length):
          decoder_input = (
              src
              if step == 0
              else decode_strategy.current_predictions.view(-1, 1, 1)
          )
          log_probs, attn = self._decode_and_generate(
              decoder_input,
              None,
              batch,
              src_len=decode_strategy.src_len,
              src_map=src_map,
              step=step if step == 0 else step + max(src_len.tolist()),
              batch_offset=decode_strategy.batch_offset,
          )
  
          if step == 0:
              log_probs = self.tile_to_beam_size_after_initial_step(
                  fn_map_state, log_probs
              )
  
          decode_strategy.advance(log_probs, attn)
          any_finished = any(
              [
                  any(sublist)
                  for sublist in decode_strategy.is_finished_list
              ]
          )
          if any_finished:
              decode_strategy.update_finished()
              if decode_strategy.done:
                  break
          select_indices = decode_strategy.select_indices
  
          if any_finished:
              # Reorder states.
              if src_map is not None:
                  src_map = src_map[select_indices]
  
          if parallel_paths > 1 or any_finished:
              # select indexes in model state/cache
              self.model.decoder.map_state(
                  lambda state, dim: state[select_indices]
              )
          # if step == 0:
          #    print("step0 time: ", time() - beg_time)
  
      return self.report_results(
          gold_score,
          batch,
          batch_size,
          decode_strategy,
      )
  
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 6 for calling the GeneratorLM obj's underlying member method code with example usage: obj._translate_batch_with_strategy(batch, decode_strategy, left_pad) and returns: self