code: def sample_with_temperature(
      logits, sampling_temp, keep_topk, keep_topp
  ):
      """Select next tokens randomly from the top k possible next tokens.
  
      Samples from a categorical distribution over the ``keep_topk`` words using
      the category probabilities ``logits / sampling_temp``.
  
      Args:
          logits (FloatTensor): Shaped ``(batch_size, vocab_size)``.
              These can be logits (``(-inf, inf)``) or log-probs (``(-inf, 0]``).
              (The distribution actually uses the log-probabilities
              ``logits - logits.logsumexp(-1)``, which equals the logits if
              they are log-probabilities summing to 1.)
          sampling_temp (float): Used to scale down logits. The higher the
              value, the more likely it is that a non-max word will be
              sampled.
          keep_topk (int): This many words could potentially be chosen. The
              other logits are set to have probability 0.
          keep_topp (float): Keep most likely words until the cumulated
              probability is greater than p. If used with keep_topk: both
              conditions will be applied
  
      Returns:
          (LongTensor, FloatTensor):
  
          * topk_ids: Shaped ``(batch_size, 1)``. These are
            the sampled word indices in the output vocab.
          * topk_scores: Shaped ``(batch_size, 1)``. These
            are essentially ``(logits / sampling_temp)[topk_ids]``.
      """
  
      if sampling_temp == 0.0 or keep_topk == 1:
          # For temp=0.0, take the argmax to avoid divide-by-zero errors.
          # keep_topk=1 is also equivalent to argmax.
          topk_scores, topk_ids = logits.topk(1, dim=-1)
          if sampling_temp > 0:
              topk_scores /= sampling_temp
      else:
          logits = torch.div(logits, sampling_temp)
          if keep_topp > 0:
              logits = sample_topp(logits, keep_topp)
          if keep_topk > 0:
              logits = sample_topk(logits, keep_topk)
          dist = torch.distributions.Categorical(logits=logits)
          topk_ids = dist.sample().view(-1, 1)
          topk_scores = logits.gather(dim=1, index=topk_ids)
      return topk_ids, topk_scores
  
imports: import torch
  from torch.nn.functional import softmax
  from onmt.translate.decode_strategy import DecodeStrategy
label_desc: Use image node_id 3 for calling a global function with example usage: sample_with_temperature(logits, sampling_temp, keep_topk, keep_topp) and returns: topk_ids,  topk_scores