class_docstr: A `Sampler` which redistributes the probability mass function for nodes among
  the top choices with a cumulative probability of at least `p`, then samples from that subset
  after re-normalizing the probabilities.
  
  Beams are sampled in the default, deterministic way.
  
  # Parameters
  
  p : `float`, optional (default = `0.9`)
      The cumulative probability cutoff threshold. A higher value of `p` will result in more possible
      examples to sample from. If `with_replacement` is `False` and the number of possible samples is
      insufficient to sample without replacement from when calling `sample_nodes`, then the top
      `per_node_beam_size` examples will be chosen.
  temperature : `float`, optional (default = `1.0`)
      A `temperature` below 1.0 produces a sharper probability distribution and a `temperature`
      above 1.0 produces a flatter probability distribution.
  with_replacement : `bool`, optional, (default = `False`)
      If set to `True`, samples will be selected with replacement from the top choices.
class_docstr_tok: ["A","`","Sampler","`","which","redistributes","the","probability","mass","function","for","nodes","among","the","top","choices","with","a","cumulative","probability","of","at","least","`","p","`",",","then","samples","from","that","subset","after","re-normalizing","the","probabilities",".","Beams","are","sampled","in","the","default",",","deterministic","way",".","#","Parameters","p",":","`","float","`",",","optional","(","default","=","`","0.9","`",")","The","cumulative","probability","cutoff","threshold",".","A","higher","value","of","`","p","`","will","result","in","more","possible","examples","to","sample","from",".","If","`","with_replacement","`","is","`","False","`","and","the","number","of","possible","samples","is","insufficient","to","sample","without","replacement","from","when","calling","`","sample_nodes","`",",","then","the","top","`","per_node_beam_size","`","examples","will","be","chosen",".","temperature",":","`","float","`",",","optional","(","default","=","`","1.0","`",")","A","`","temperature","`","below","1.0","produces","a","sharper","probability","distribution","and","a","`","temperature","`","above","1.0","produces","a","flatter","probability","distribution",".","with_replacement",":","`","bool","`",",","optional",",","(","default","=","`","False","`",")","If","set","to","`","True","`",",","samples","will","be","selected","with","replacement","from","the","top","choices","."]
code: def sample_nodes(
      self,
      log_probs: torch.Tensor,
      per_node_beam_size: int,
      state: StateType,
  ) -> Tuple[torch.Tensor, torch.Tensor, StateType]:
      if not per_node_beam_size <= log_probs.size()[1]:
          raise ValueError(
              "per_node_beam_size cannot be greater than vocabulary size"
          )
  
      # First apply temperature coefficient:
      if self.temperature != 1.0:
          _log_probs = torch.nn.functional.log_softmax(
              log_probs / self.temperature, dim=-1
          )
      else:
          _log_probs = log_probs
  
      # Sort the probabilities in descending order to then find cumulative sum
      log_probs_descending, sorting_indices = torch.sort(
          _log_probs, descending=True
      )
  
      # shape: (batch_size, num_classes)
      probabilities_descending = log_probs_descending.exp()
      probabilities_summed = torch.cumsum(
          probabilities_descending, dim=-1
      )
  
      # Create a mask for filtering out probabilities that don't make the top `p`.
      # shape: (batch_size, num_classes)
      exclusion_mask = probabilities_summed >= self.p
  
      # We want to include the first index where probabilities_summed >= p, so we shift over one.
      exclusion_mask[..., 1:] = exclusion_mask[..., :-1].clone()
      exclusion_mask[..., 0] = False
  
      # Make sure there's at least `per_node_beam_size` options to be selected.
      if not self.with_replacement:
          exclusion_mask[..., :per_node_beam_size] = False
  
      log_probs_descending[exclusion_mask] = min_value_of_dtype(
          log_probs.dtype
      )
  
      # Now re-normalized the included log probs.
      # shape: (batch_size, num_classes)
      filtered_probabilities = torch.nn.functional.softmax(
          log_probs_descending, dim=-1
      )
  
      # Sample from the re-normalized subset.
      # NOTE: These indices are not indices into `log_probs`, they are indices into `log_probs_descending`.
      # shape: (batch_size, per_node_beam_size)
      sampled_indices = torch.multinomial(
          filtered_probabilities,
          per_node_beam_size,
          replacement=self.with_replacement,
      )
  
      # Convert `sampled_indices` back to indices in the original `log_probs` tensor.
      # shape: (batch_size, per_node_beam_size)
      selected_indices = sorting_indices.gather(-1, sampled_indices)
  
      # Return (selected log probabilities, selected classes)
      # shape: (len(log_probs),1) , (len(log_probs), 1)
      return (
          torch.gather(log_probs, 1, selected_indices),
          selected_indices,
          state,
      )
  
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 2 for calling the TopPSampler obj's underlying member method code with example usage: obj.sample_nodes(log_probs, per_node_beam_size, state) and returns: torch,  selected_indices,  state