class_docstr: [`BeamScorer`] implementing constrained beam search decoding.
  
  
  Args:
      batch_size (`int`):
          Batch Size of `input_ids` for which standard beam search decoding is run in parallel.
      max_length (`int`):
          The maximum length of the sequence to be generated.
      num_beams (`int`):
          Number of beams for beam search.
      constraints (`List[Constraint]`):
          A list of positive constraints represented as `Constraint` objects that must be fulfilled in the generation
          output. For more information, the documentation of [`Constraint`] should be read.
      device (`torch.device`):
          Defines the device type (*e.g.*, `"cpu"` or `"cuda"`) on which this instance of `BeamSearchScorer` will be
          allocated.
      length_penalty (`float`, *optional*, defaults to 1.0):
          Exponential penalty to the length that is used with beam-based generation. It is applied as an exponent to
          the sequence length, which in turn is used to divide the score of the sequence. Since the score is the log
          likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while
          `length_penalty` < 0.0 encourages shorter sequences.
      do_early_stopping (`bool`, *optional*, defaults to `False`):
          Whether to stop the beam search when at least `num_beams` sentences are finished per batch or not.
      num_beam_hyps_to_keep (`int`, *optional*, defaults to 1):
          The number of beam hypotheses that shall be returned upon calling
          [`~transformer.BeamSearchScorer.finalize`].
      num_beam_groups (`int`):
          Number of groups to divide `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,
      constraints: List[Constraint],
      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.constraints = constraints
  
      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(
              "`num_beam_groups` has to be an integer smaller or equal than `num_beams` and `num_beams` has to be"
              f" divisible by `num_beam_groups`, but is {num_beam_groups} with `num_beams` being {num_beams}."
          )
  
      if "max_length" in kwargs:
          warnings.warn(
              "Passing `max_length` to ConstrainedBeamSearchScorer is deprecated and has no effect. "
              "`max_length` should be passed directly to `beam_search(...)`, `beam_sample(...)`"
              ", or `group_beam_search(...)`."
          )
  
imports: import warnings
  from abc import ABC, abstractmethod
  from collections import UserDict
  from typing import List, Optional, Tuple
  import numpy
  import torch
  from .generation_beam_constraints import Constraint, ConstraintListState
  from .utils import add_start_docstrings
label_desc: Use image node_id 1 to create a new ConstrainedBeamSearchScorer object from inherited base classes: BeamScorer with example: obj = ConstrainedBeamSearchScorer(batch_size, num_beams, constraints, device, length_penalty, do_early_stopping, num_beam_hyps_to_keep, num_beam_groups)