docstr: Compute negative log likelihood(nll) from transformer language model
  
  To avoid OOM, this fuction seperate the input into batches.
  Then call nll for each batch and combine and return results.
  Args:
      text: (Batch, Length)
      text_lengths: (Batch,)
      batch_size: int, samples each batch contain when computing nll,
                  you may change this to avoid OOM or increase
docstr_tok: ["Compute","negative","log","likelihood","(","nll",")","from","transformer","language","model","To","avoid","OOM",",","this","fuction","seperate","the","input","into","batches",".","Then","call","nll","for","each","batch","and","combine","and","return","results",".","Args",":","text",":","(","Batch",",","Length",")","text_lengths",":","(","Batch",",",")","batch_size",":","int",",","samples","each","batch","contain","when","computing","nll",",","you","may","change","this","to","avoid","OOM","or","increase"]
code: def batchify_nll(
      self,
      text: torch.Tensor,
      text_lengths: torch.Tensor,
      batch_size: int = 100,
  ) -> Tuple[torch.Tensor, torch.Tensor]:
      """Compute negative log likelihood(nll) from transformer language model
  
      To avoid OOM, this fuction seperate the input into batches.
      Then call nll for each batch and combine and return results.
      Args:
          text: (Batch, Length)
          text_lengths: (Batch,)
          batch_size: int, samples each batch contain when computing nll,
                      you may change this to avoid OOM or increase
  
      """
      total_num = text.size(0)
      if total_num <= batch_size:
          nll, x_lengths = self.nll(text, text_lengths)
      else:
          nlls = []
          x_lengths = []
          max_length = text_lengths.max()
  
          start_idx = 0
          while True:
              end_idx = min(start_idx + batch_size, total_num)
              batch_text = text[start_idx:end_idx, :]
              batch_text_lengths = text_lengths[start_idx:end_idx]
              # batch_nll: [B * T]
              batch_nll, batch_x_lengths = self.nll(
                  batch_text, batch_text_lengths, max_length=max_length
              )
              nlls.append(batch_nll)
              x_lengths.append(batch_x_lengths)
              start_idx = end_idx
              if start_idx == total_num:
                  break
          nll = torch.cat(nlls)
          x_lengths = torch.cat(x_lengths)
      assert nll.size(0) == total_num
      assert x_lengths.size(0) == total_num
      return nll, x_lengths
  
file_path: espnet/espnet2/lm/espnet_model.py
imports: from typing import Dict, Optional, Tuple
  import torch
  import torch.nn.functional
  from typeguard import check_argument_types
  from espnet2.lm.abs_model import AbsLM
  from espnet2.torch_utils.device_funcs import force_gatherable
  from espnet2.train.abs_espnet_model import AbsESPnetModel
  from espnet.nets.pytorch_backend.nets_utils import make_pad_mask
label_desc: Use image node_id 3 for calling the ESPnetLanguageModel obj's underlying member method code with example usage: obj.batchify_nll(text, text_lengths, batch_size) and returns: nll,  x_lengths