code: def perplexity(
      preds: Tensor, target: Tensor, ignore_index: Optional[int] = None
  ) -> Tensor:
      """Perplexity measures how well a language model predicts a text sample.
  
      This metric is calculated as the average number of bits per word a model needs to represent the sample.
  
      Args:
          preds:
              Logits or a unnormalized score assigned to each token in a sequence with shape [batch_size, seq_len,
              vocab_size], which is the output of a language model. Scores will be normalized internally using softmax.
          target:
              Ground truth values with a shape [batch_size, seq_len].
          ignore_index:
              Integer specifying a target class to ignore. If given, this class index does not contribute
              to the returned score.
  
      Returns:
          Perplexity value
  
      Examples:
          >>> import torch
          >>> gen = torch.manual_seed(42)
          >>> preds = torch.rand(2, 8, 5, generator=gen)
          >>> target = torch.randint(5, (2, 8), generator=gen)
          >>> target[0, 6:] = -100
          >>> perplexity(preds, target, ignore_index=-100)
          tensor(5.8540)
  
      """
      total, count = _perplexity_update(preds, target, ignore_index)
      return _perplexity_compute(total, count)
  
imports: from typing import Optional, Tuple
  import torch
  from torch import Tensor
label_desc: Use image node_id 4 for calling a global function with example usage: perplexity(preds, target, ignore_index) and returns: _perplexity_compute