class_docstr: Base class for criteria with need to run beam search.
class_docstr_tok: ["Base","class","for","criteria","with","need","to","run","beam","search","."]
docstr: Run beam search to generate translations from the current model.
  
  Args:
      model: FairseqModel to use (passed via FairseqCriterion.forward())
      sample: Training batch (passed via FairseqCriterion.forward())
  
  Returns:
      A tuple (tokens, bleu) of tensors. `tokens` is a [bsz, beam_size,
      max_translation_length] int tensor with generated translations (with
      EOS). `bleu` is a [bsz, beam_size] float tensor with BLEU scores.
      `tokens` corresponds to U(x) in Edunov et al., 2017.
docstr_tok: ["Run","beam","search","to","generate","translations","from","the","current","model",".","Args",":","model",":","FairseqModel","to","use","(","passed","via","FairseqCriterion.forward","(",")",")","sample",":","Training","batch","(","passed","via","FairseqCriterion.forward","(",")",")","Returns",":","A","tuple","(","tokens",",","bleu",")","of","tensors",".","`","tokens","`","is","a","[","bsz",",","beam_size",",","max_translation_length","]","int","tensor","with","generated","translations","(","with","EOS",")",".","`","bleu","`","is","a","[","bsz",",","beam_size","]","float","tensor","with","BLEU","scores",".","`","tokens","`","corresponds","to","U","(","x",")","in","Edunov","et","al.",",","2017","."]
code: def generate_translations(self, model, sample):
      """Run beam search to generate translations from the current model.
  
      Args:
          model: FairseqModel to use (passed via FairseqCriterion.forward())
          sample: Training batch (passed via FairseqCriterion.forward())
  
      Returns:
          A tuple (tokens, bleu) of tensors. `tokens` is a [bsz, beam_size,
          max_translation_length] int tensor with generated translations (with
          EOS). `bleu` is a [bsz, beam_size] float tensor with BLEU scores.
          `tokens` corresponds to U(x) in Edunov et al., 2017.
      """
      model.eval()  # Set to eval mode
      enc_input = (
          sample["net_input"]["src_tokens"],
          sample["net_input"]["src_lengths"],
      )
      trg_lens = sample["target"].size(1) - torch.sum(
          sample["target"] <= self.scorer.eos, dim=1
      )
      max_trans_len = 0
      all_translations = []
      for batch_idx, batch_trans in enumerate(
          self._batch_translations(model, enc_input)
      ):
          ref = (
              sample["target"][batch_idx, : trg_lens[batch_idx] + 1]
              .int()
              .cpu()
          )
          batch_translations = []
          for trans in batch_trans:
              max_trans_len = max(
                  max_trans_len, trans["tokens"].size(0)
              )
              self.scorer.reset()
              self.scorer.add(ref, trans["tokens"].int().cpu())
              batch_translations.append(
                  (self.scorer.score(), trans["tokens"])
              )
          batch_translations.sort(key=lambda x: -x[0])
          all_translations.append(batch_translations)
      bsz = len(all_translations)
      beam_size = self.get_translator(model).beam_size
      tokens_tensor = trans["tokens"].new_full(
          (bsz, beam_size, max_trans_len), self.scorer.pad
      )
      bleu_tensor = torch.zeros(bsz, beam_size)
      for batch_idx, batch_translations in enumerate(all_translations):
          for trans_idx, (bleu_score, tokens) in enumerate(
              batch_translations
          ):
              tokens_tensor[
                  batch_idx, trans_idx, : tokens.size(0)
              ] = tokens
              bleu_tensor[batch_idx, trans_idx] = bleu_score
      model.train()  # Set back to train mode
      return tokens_tensor, bleu_tensor
  
imports: import copy
  import math
  import torch
  from fairseq import utils
  from fairseq.criterions import LegacyFairseqCriterion, register_criterion
  from fairseq.scoring import bleu
  from pytorch_translate import generate
label_desc: Use image node_id 3 for calling the BaseSequenceLossCriterion obj's underlying member method code with example usage: obj.generate_translations(model, sample) and returns: tokens_tensor,  bleu_tensor