class_docstr: Default RNNLM for `LMInterface` Implementation.
  
  Note:
      PyTorch seems to have memory leak when one GPU compute this after data parallel.
      If parallel GPUs compute this, it seems to be fine.
      See also https://github.com/espnet/espnet/issues/1075
class_docstr_tok: ["Default","RNNLM","for","`","LMInterface","`","Implementation",".","Note",":","PyTorch","seems","to","have","memory","leak","when","one","GPU","compute","this","after","data","parallel",".","If","parallel","GPUs","compute","this",",","it","seems","to","be","fine",".","See","also","https",":","\/\/github.com\/espnet\/espnet\/issues\/1075"]
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, t):
      """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)
  
      """
      loss = 0
      logp = 0
      count = torch.tensor(0).long()
      state = None
      batch_size, sequence_length = x.shape
      for i in range(sequence_length):
          # Compute the loss at this time step and accumulate it
          state, loss_batch = self.model(state, x[:, i], t[:, i])
          non_zeros = torch.sum(x[:, i] != 0, dtype=loss_batch.dtype)
          loss += loss_batch.mean() * non_zeros
          logp += torch.sum(loss_batch * non_zeros)
          count += int(non_zeros)
      return loss / batch_size, loss, count.to(loss.device)
  
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.e2e_asr import to_device
  from espnet.nets.scorer_interface import BatchScorerInterface
  from espnet.utils.cli_utils import strtobool
label_desc: Use image node_id 5 for calling the DefaultRNNLM obj's underlying member method code with example usage: obj.forward(x, t) and returns: unknown,  loss,  count