class_docstr: :class:`transformers.BeamScorer` implementing standard beam search decoding.
  
  Adapted in part from `Facebook's XLM beam search code
  <https://github.com/facebookresearch/XLM/blob/9e6f6814d17be4fe5b15f2e6c43eb2b2d76daeb4/src/model/transformer.py#L529>`__.
  
  Reference for the diverse beam search algorithm and implementation `Ashwin Kalyan's DBS implementation
  <https://github.com/ashwinkalyan/dbs/blob/master/dbs/beam_utils.lua>`__
  
  Args:
      batch_size (:obj:`int`):
          Batch Size of :obj:`input_ids` for which standard beam search decoding is run in parallel.
      max_length (:obj:`int`):
          The maximum length of the sequence to be generated.
      num_beams (:obj:`int`):
          Number of beams for beam search.
      device (:obj:`torch.device`):
          Defines the device type (*e.g.*, :obj:`"cpu"` or :obj:`"cuda"`) on which this instance of
          :obj:`BeamSearchScorer` will be allocated.
      length_penalty (:obj:`float`, `optional`, defaults to 1.0):
          Exponential penalty to the length. 1.0 means no penalty. Set to values < 1.0 in order to encourage the
          model to generate shorter sequences, to a value > 1.0 in order to encourage the model to produce longer
          sequences.
      do_early_stopping (:obj:`bool`, `optional`, defaults to :obj:`False`):
          Whether to stop the beam search when at least ``num_beams`` sentences are finished per batch or not.
      num_beam_hyps_to_keep (:obj:`int`, `optional`, defaults to 1):
          The number of beam hypotheses that shall be returned upon calling
          :meth:`~transformer.BeamSearchScorer.finalize`.
      num_beam_groups (:obj:`int`):
          Number of groups to divide :obj:`num_beams` into in order to ensure diversity among different groups of
          beams. See `this paper <https://arxiv.org/pdf/1610.02424.pdf>`__ for more details.
class_docstr_tok: [":","class",":","`","transformers.BeamScorer","`","implementing","standard","beam","search","decoding",".","Adapted","in","part","from","`","Facebook","'s","XLM","beam","search","code","<","https",":","\/\/github.com\/facebookresearch\/XLM\/blob\/9e6f6814d17be4fe5b15f2e6c43eb2b2d76daeb4\/src\/model\/transformer.py","#","L529",">","`","__",".","Reference","for","the","diverse","beam","search","algorithm","and","implementation","`","Ashwin","Kalyan","'s","DBS","implementation","<","https",":","\/\/github.com\/ashwinkalyan\/dbs\/blob\/master\/dbs\/beam_utils.lua",">","`","__","Args",":","batch_size","(",":","obj",":","`","int","`",")",":","Batch","Size","of",":","obj",":","`","input_ids","`","for","which","standard","beam","search","decoding","is","run","in","parallel",".","max_length","(",":","obj",":","`","int","`",")",":","The","maximum","length","of","the","sequence","to","be","generated",".","num_beams","(",":","obj",":","`","int","`",")",":","Number","of","beams","for","beam","search",".","device","(",":","obj",":","`","torch.device","`",")",":","Defines","the","device","type","(","*","e.g",".","*",",",":","obj",":","`","``","cpu","''","`","or",":","obj",":","`","``","cuda","''","`",")","on","which","this","instance","of",":","obj",":","`","BeamSearchScorer","`","will","be","allocated",".","length_penalty","(",":","obj",":","`","float","`",",","`","optional","`",",","defaults","to","1.0",")",":","Exponential","penalty","to","the","length",".","1.0","means","no","penalty",".","Set","to","values","<","1.0","in","order","to","encourage","the","model","to","generate","shorter","sequences",",","to","a","value",">","1.0","in","order","to","encourage","the","model","to","produce","longer","sequences",".","do_early_stopping","(",":","obj",":","`","bool","`",",","`","optional","`",",","defaults","to",":","obj",":","`","False","`",")",":","Whether","to","stop","the","beam","search","when","at","least","``","num_beams","``","sentences","are","finished","per","batch","or","not",".","num_beam_hyps_to_keep","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to","1",")",":","The","number","of","beam","hypotheses","that","shall","be","returned","upon","calling",":","meth",":","`","~transformer.BeamSearchScorer.finalize","`",".","num_beam_groups","(",":","obj",":","`","int","`",")",":","Number","of","groups","to","divide",":","obj",":","`","num_beams","`","into","in","order","to","ensure","diversity","among","different","groups","of","beams",".","See","`","this","paper","<","https",":","\/\/arxiv.org\/pdf\/1610.02424.pdf",">","`","__","for","more","details","."]
code: def __init__(
      self,
      batch_size: int,
      num_beams: int,
      device: "torch.device",
      length_penalty: Optional[float] = 1.0,
      do_early_stopping: Optional[bool] = False,
      num_beam_hyps_to_keep: Optional[int] = 1,
      num_beam_groups: Optional[int] = 1,
      **kwargs,
  ):
      self.num_beams = num_beams
      self.device = device
      self.length_penalty = length_penalty
      self.do_early_stopping = do_early_stopping
      self.num_beam_hyps_to_keep = num_beam_hyps_to_keep
      self.num_beam_groups = num_beam_groups
      self.group_size = self.num_beams // self.num_beam_groups
  
      self._is_init = False
      self._beam_hyps = [
          BeamHypotheses(
              num_beams=self.num_beams,
              length_penalty=self.length_penalty,
              early_stopping=self.do_early_stopping,
          )
          for _ in range(batch_size)
      ]
      self._done = torch.tensor(
          [False for _ in range(batch_size)],
          dtype=torch.bool,
          device=self.device,
      )
  
      if not isinstance(num_beams, int) or num_beams <= 1:
          raise ValueError(
              f"`num_beams` has to be an integer strictly greater than 1, but is {num_beams}. For `num_beams` == 1, one should make use of `greedy_search` instead."
          )
  
      if (
          not isinstance(num_beam_groups, int)
          or (num_beam_groups > num_beams)
          or (num_beams % num_beam_groups != 0)
      ):
          raise ValueError(
              f"`num_beam_groups` has to be an integer smaller or equal than `num_beams` and `num_beams` "
              f"has to be divisible by `num_beam_groups`, but is {num_beam_groups} with `num_beams` being {num_beams}."
          )
  
      if "max_length" in kwargs:
          logger.warning(
              "Passing `max_length` to BeamSearchScorer is deprecated and has no effect."
              "`max_length` should be passed directly to `beam_search(...)`, `beam_sample(...)`"
              ",or `group_beam_search(...)`."
          )
  
imports: from abc import ABC, abstractmethod
  from collections import UserDict
  from typing import Optional, Tuple
  from .file_utils import add_start_docstrings
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from fastNLP.core.log import logger
label_desc: Use image node_id 1 to create a new BeamSearchScorer object from inherited base classes: BeamScorer with example: obj = BeamSearchScorer(batch_size, num_beams, device, length_penalty, do_early_stopping, num_beam_hyps_to_keep, num_beam_groups)