code: def logprobs_from_logits(
      logits: torch.Tensor, labels: torch.Tensor, gather: bool = True
  ) -> torch.Tensor:
      """
      See: https://github.com/pytorch/pytorch/issues/563#issuecomment-330103591
      """
      logp = F.log_softmax(logits, dim=2)
  
      if not gather:
          return logp
      logpy = torch.gather(logp, 2, labels.unsqueeze(2)).squeeze(-1)
      return logpy
  
imports: import gc
  import random
  import warnings
  from contextlib import contextmanager
  from typing import Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.utils.rnn import pad_sequence
  from transformers import top_k_top_p_filtering
  from .import_utils import is_npu_available, is_xpu_available
label_desc: Use image node_id 6 for calling a global function with example usage: logprobs_from_logits(logits, labels, gather) and returns: logpy, logp