class_docstr: The difference between BeamSearchAndDecodeV2 and BeamSearchAndDecode is: V2 calls
  DecoderBatchedStepEnsemble2BeamWithEOS instead of DecoderBatchedStepEnsemble when
  running beam search. Also, since extra EOS token has been added, it calls
  BeamDecodeWithEOS when running beam decoding which supports adding extra EOS token.
class_docstr_tok: ["The","difference","between","BeamSearchAndDecodeV2","and","BeamSearchAndDecode","is",":","V2","calls","DecoderBatchedStepEnsemble2BeamWithEOS","instead","of","DecoderBatchedStepEnsemble","when","running","beam","search",".","Also",",","since","extra","EOS","token","has","been","added",",","it","calls","BeamDecodeWithEOS","when","running","beam","decoding","which","supports","adding","extra","EOS","token","."]
code: def __init__(
      self,
      models,
      tgt_dict,
      src_tokens,
      src_lengths,
      eos_token_id,
      length_penalty,
      nbest,
      beam_size,
      stop_at_eos,
      word_reward=0,
      unk_reward=0,
      quantize=False,
  ):
      super().__init__()
  
      self.models = models
      self.tgt_dict = tgt_dict
      self.beam_size = torch.jit.Attribute(beam_size, int)
      self.word_reward = torch.jit.Attribute(word_reward, float)
      self.unk_reward = torch.jit.Attribute(unk_reward, float)
  
      encoder_ens = EncoderEnsemble(self.models)
      encoder_ens.enable_precompute_reduced_weights = True
  
      if quantize:
          encoder_ens = torch.jit.quantized.quantize_linear_modules(
              encoder_ens
          )
          encoder_ens = torch.jit.quantized.quantize_rnn_cell_modules(
              encoder_ens
          )
  
      # not support char source model
      self.is_char_source = False
      enc_inputs = (src_tokens, src_lengths)
      example_encoder_outs = encoder_ens(*enc_inputs)
      self.encoder_ens = torch.jit.trace(
          encoder_ens, enc_inputs, _force_outplace=True
      )
      self.encoder_ens_char_source = FakeCharSourceEncoderEnsemble()
  
      decoder_ens = DecoderBatchedStepEnsemble2BeamWithEOS(
          self.models,
          tgt_dict,
          beam_size,
          word_reward,
          unk_reward,
          tile_internal=False,
      )
      decoder_ens.enable_precompute_reduced_weights = True
      if quantize:
          decoder_ens = torch.jit.quantized.quantize_linear_modules(
              decoder_ens
          )
          decoder_ens = torch.jit.quantized.quantize_rnn_cell_modules(
              decoder_ens
          )
          decoder_ens = torch.jit.quantized.quantize_rnn_modules(
              decoder_ens
          )
      decoder_ens_tile = DecoderBatchedStepEnsemble2BeamWithEOS(
          self.models,
          tgt_dict,
          beam_size,
          word_reward,
          unk_reward,
          tile_internal=True,
      )
      decoder_ens_tile.enable_precompute_reduced_weights = True
      if quantize:
          decoder_ens_tile = (
              torch.jit.quantized.quantize_linear_modules(
                  decoder_ens_tile
              )
          )
          decoder_ens_tile = (
              torch.jit.quantized.quantize_rnn_cell_modules(
                  decoder_ens_tile
              )
          )
          decoder_ens_tile = torch.jit.quantized.quantize_rnn_modules(
              decoder_ens_tile
          )
      prev_token = torch.LongTensor([0])
      prev_scores = torch.FloatTensor([0.0])
      ts = torch.LongTensor([0])
      final_step = torch.tensor([False], dtype=torch.bool)
      active_hypos = torch.LongTensor([0])
  
      _, _, _, _, _, *tiled_states = decoder_ens_tile(
          prev_token,
          prev_scores,
          active_hypos,
          ts,
          final_step,
          *example_encoder_outs,
      )
  
      self.decoder_ens_tile = torch.jit.trace(
          decoder_ens_tile,
          (
              prev_token,
              prev_scores,
              active_hypos,
              ts,
              final_step,
              *example_encoder_outs,
          ),
          _force_outplace=True,
      )
      self.decoder_ens = torch.jit.trace(
          decoder_ens,
          (
              prev_token.repeat(self.beam_size),
              prev_scores.repeat(self.beam_size),
              active_hypos.repeat(self.beam_size),
              ts,
              final_step,
              *tiled_states,
          ),
          _force_outplace=True,
      )
  
      self.beam_decode = BeamDecodeWithEOS(
          eos_token_id, length_penalty, nbest, beam_size, stop_at_eos
      )
  
      self.input_names = [
          "src_tokens",
          "src_lengths",
          "prev_token",
          "prev_scores",
          "attn_weights",
          "prev_hypos_indices",
          "num_steps",
      ]
      self.output_names = [
          "beam_output",
          "hypothesis_score",
          "token_level_scores",
          "back_alignment_weights",
          "best_indices",
      ]
  
imports: from typing import List, Tuple
  import numpy
  import torch
  import torch.jit
  import torch.jit.quantized
  from pytorch_translate.beam_decode import BeamDecode
  from pytorch_translate.ensemble_export import DecoderBatchedStepEnsemble, EncoderEnsemble, FakeCharSourceEncoderEnsemble, load_models_from_checkpoints
  from torch import Tensor
label_desc: Use image node_id 1 to create a new BeamSearchAndDecodeV2 object from inherited base classes: torch.jit with example: obj = BeamSearchAndDecodeV2(models, tgt_dict, src_tokens, src_lengths, eos_token_id, length_penalty, nbest, beam_size, stop_at_eos, word_reward, unk_reward, quantize)