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):
      """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) Run the encoder on the src.
      src, enc_final_hs, enc_out, src_len = self._run_encoder(batch)
  
      self.model.decoder.init_state(src, enc_out, enc_final_hs)
  
      gold_score = self._gold_score(
          batch,
          enc_out,
          src_len,
          use_src_map,
          enc_final_hs,
          batch_size,
          src,
      )
  
      # (2) prep decode_strategy. Possibly repeat src objects.
      src_map = batch["src_map"] if use_src_map else None
      target_prefix = batch["tgt"] if self.tgt_file_prefix else None
      (fn_map_state, enc_out, src_map) = decode_strategy.initialize(
          enc_out, src_len, src_map, target_prefix=target_prefix
      )
  
      if fn_map_state is not None:
          self.model.decoder.map_state(fn_map_state)
  
      # (3) Begin decoding step by step:
      for step in range(decode_strategy.max_length):
          decoder_input = decode_strategy.current_predictions.view(
              -1, 1, 1
          )
  
          log_probs, attn = self._decode_and_generate(
              decoder_input,
              enc_out,
              batch,
              src_len=decode_strategy.src_len,
              src_map=src_map,
              step=step,
              batch_offset=decode_strategy.batch_offset,
              return_attn=decode_strategy.return_attention,
          )
  
          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 isinstance(enc_out, tuple):
                  enc_out = tuple(x[select_indices] for x in enc_out)
              else:
                  enc_out = enc_out[select_indices]
  
              if src_map is not None:
                  src_map = src_map[select_indices]
  
          if parallel_paths > 1 or any_finished:
              self.model.decoder.map_state(
                  lambda state, dim: state[select_indices]
              )
  
      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 5 for calling the Translator obj's underlying member method code with example usage: obj._translate_batch_with_strategy(batch, decode_strategy) and returns: self