class_docstr: Transformer language model.
class_docstr_tok: ["Transformer","language","model","."]
docstr: Compute LM loss value from buffer sequences.
  
  Args:
      x (torch.Tensor): Input ids. (batch, len)
      t (torch.Tensor): Target ids. (batch, len)
  
  Returns:
      tuple[torch.Tensor, torch.Tensor, torch.Tensor]: Tuple of
          loss to backward (scalar),
          negative log-likelihood of t: -log p(t) (scalar) and
          the number of elements in x (scalar)
  
  Notes:
      The last two return values are used
      in perplexity: p(t)^{-n} = exp(-log p(t) / n)
docstr_tok: ["Compute","LM","loss","value","from","buffer","sequences",".","Args",":","x","(","torch.Tensor",")",":","Input","ids",".","(","batch",",","len",")","t","(","torch.Tensor",")",":","Target","ids",".","(","batch",",","len",")","Returns",":","tuple","[","torch.Tensor",",","torch.Tensor",",","torch.Tensor","]",":","Tuple","of","loss","to","backward","(","scalar",")",",","negative","log-likelihood","of","t",":","-log","p","(","t",")","(","scalar",")","and","the","number","of","elements","in","x","(","scalar",")","Notes",":","The","last","two","return","values","are","used","in","perplexity",":","p","(","t",")","^","{","-n","}","=","exp","(","-log","p","(","t",")","\/","n",")"]
code: def forward(
      self, x: torch.Tensor, t: torch.Tensor
  ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
      """Compute LM loss value from buffer sequences.
  
      Args:
          x (torch.Tensor): Input ids. (batch, len)
          t (torch.Tensor): Target ids. (batch, len)
  
      Returns:
          tuple[torch.Tensor, torch.Tensor, torch.Tensor]: Tuple of
              loss to backward (scalar),
              negative log-likelihood of t: -log p(t) (scalar) and
              the number of elements in x (scalar)
  
      Notes:
          The last two return values are used
          in perplexity: p(t)^{-n} = exp(-log p(t) / n)
  
      """
      xm = x != 0
  
      if self.embed_drop is not None:
          emb = self.embed_drop(self.embed(x))
      else:
          emb = self.embed(x)
  
      h, _ = self.encoder(emb, self._target_mask(x))
      y = self.decoder(h)
      loss = F.cross_entropy(
          y.view(-1, y.shape[-1]), t.view(-1), reduction="none"
      )
      mask = xm.to(dtype=loss.dtype)
      logp = loss * mask.view(-1)
      logp = logp.sum()
      count = mask.sum()
      return logp / count, logp, count
  
imports: import logging
  from typing import Any, List, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from espnet.nets.lm_interface import LMInterface
  from espnet.nets.pytorch_backend.transformer.embedding import PositionalEncoding
  from espnet.nets.pytorch_backend.transformer.encoder import Encoder
  from espnet.nets.pytorch_backend.transformer.mask import subsequent_mask
  from espnet.nets.scorer_interface import BatchScorerInterface
  from espnet.utils.cli_utils import strtobool
label_desc: Use image node_id 4 for calling the TransformerLM obj's underlying member method code with example usage: obj.forward(x, t) and returns: unknown,  logp,  count