class_docstr: An abstract class that can be used to enforce constraints on the output predictions
  by manipulating the class log probabilities during beam search.
  
  A `Constraint` just has three methods that need to be implemented by subclasses:
  `init_state()`, `apply()` and `_update_state()`.
  
  `init_state()` takes one argument:
  
  - the batch size, an int
  
  It returns a constraint state, which is a nested list of dictionaries, with any state needed for subsequent
  calls to `apply()` and `update_state()`. The length of the outer list should be equal to `batch_size`.
  Each inner list should be of length 1.
  
  `apply()` takes two arguments:
  
  - the constraint state, which is a nested list of dictionaries. The length of the outer list is `batch_size`
  and the length of each inner list is `beam_size` except on the first time `apply()` is called when it is 1.
  - `class_log_probabilities`, a tensor of shape `(batch_size, beam_size, num_classes)` that contains the
  log probabilities for the classes during search. The first time `apply()` is called, `beam_size = 1`.
  
  The `apply()` method should return new `class_log_probabilities` that enforce the constraint
  for this step of beam search. For instance, it may prevent a specific class from being selected by setting
  the corresponding log probability to a negligible value such as `float("-inf")` or
  `min_value_of_dtype(class_log_probabilities.dtype)`.
  
  `_update_state()` takes two arguments:
  
  - the copied parent constraint state, which is a nested list of dictionaries. `state[i][j]` contains the
  copied state for the parent of `last_prediction[i, j]`. It is unique to that batch and beam, so it can be
  directly edited in-place without affecting the others.
  - last_prediction, a tensor of shape `(batch_size, beam_size)` containing the predictions from the last
  step of beam search.
  
  The `_update_state()` function should return a new constraint state, a nested list of dictionaries of
  length `batch_size` and inner list of length `beam_size`, one for each of the predictions in `last_prediction`.
class_docstr_tok: ["An","abstract","class","that","can","be","used","to","enforce","constraints","on","the","output","predictions","by","manipulating","the","class","log","probabilities","during","beam","search",".","A","`","Constraint","`","just","has","three","methods","that","need","to","be","implemented","by","subclasses",":","`","init_state","(",")","`",",","`","apply","(",")","`","and","`","_update_state","(",")","`",".","`","init_state","(",")","`","takes","one","argument",":","-","the","batch","size",",","an","int","It","returns","a","constraint","state",",","which","is","a","nested","list","of","dictionaries",",","with","any","state","needed","for","subsequent","calls","to","`","apply","(",")","`","and","`","update_state","(",")","`",".","The","length","of","the","outer","list","should","be","equal","to","`","batch_size","`",".","Each","inner","list","should","be","of","length","1",".","`","apply","(",")","`","takes","two","arguments",":","-","the","constraint","state",",","which","is","a","nested","list","of","dictionaries",".","The","length","of","the","outer","list","is","`","batch_size","`","and","the","length","of","each","inner","list","is","`","beam_size","`","except","on","the","first","time","`","apply","(",")","`","is","called","when","it","is","1",".","-","`","class_log_probabilities","`",",","a","tensor","of","shape","`","(","batch_size",",","beam_size",",","num_classes",")","`","that","contains","the","log","probabilities","for","the","classes","during","search",".","The","first","time","`","apply","(",")","`","is","called",",","`","beam_size","=","1","`",".","The","`","apply","(",")","`","method","should","return","new","`","class_log_probabilities","`","that","enforce","the","constraint","for","this","step","of","beam","search",".","For","instance",",","it","may","prevent","a","specific","class","from","being","selected","by","setting","the","corresponding","log","probability","to","a","negligible","value","such","as","`","float","(","``","-inf","''",")","`","or","`","min_value_of_dtype","(","class_log_probabilities.dtype",")","`",".","`","_update_state","(",")","`","takes","two","arguments",":","-","the","copied","parent","constraint","state",",","which","is","a","nested","list","of","dictionaries",".","`","state","[","i","]","[","j","]","`","contains","the","copied","state","for","the","parent","of","`","last_prediction","[","i",",","j","]","`",".","It","is","unique","to","that","batch","and","beam",",","so","it","can","be","directly","edited","in-place","without","affecting","the","others",".","-","last_prediction",",","a","tensor","of","shape","`","(","batch_size",",","beam_size",")","`","containing","the","predictions","from","the","last","step","of","beam","search",".","The","`","_update_state","(",")","`","function","should","return","a","new","constraint","state",",","a","nested","list","of","dictionaries","of","length","`","batch_size","`","and","inner","list","of","length","`","beam_size","`",",","one","for","each","of","the","predictions","in","`","last_prediction","`","."]
code: def _update_state(
      self,
      state: ConstraintStateType,
      last_prediction: torch.Tensor,
  ) -> ConstraintStateType:
      raise NotImplementedError
  
imports: from inspect import signature
  from typing import Any, List, Callable, Tuple, Dict, cast, TypeVar, Optional
  import copy
  import warnings
  import torch
  from allennlp.common import Lazy, Registrable
  from allennlp.common.checks import ConfigurationError
  from allennlp.data import Vocabulary
  from allennlp.nn.util import min_value_of_dtype
label_desc: Use image node_id 6 for calling the Constraint obj's underlying member method code with example usage: obj._update_state(state, last_prediction) without return types