class_docstr: Left-to-right Transformer language model.
class_docstr_tok: ["Left-to-right","Transformer","language","model","."]
code: def eval_step(self, batch, batch_idx):
      mode = "test" if self.trainer.testing else "val"
      for i in range(len(batch)):
          if batch[i].ndim == 3:
              # Dataset returns already batched data and the first dimension of size 1
              # added by DataLoader is excess.
              batch[i] = batch[i].squeeze(dim=0)
      ids, mask = batch
      input_ids, labels = ids[:, :-1], ids[:, 1:]
      input_mask, output_mask = mask[:, :-1], mask[:, 1:]
      log_probs = self(input_ids=input_ids, attention_mask=input_mask)
      eval_loss = self.eval_loss_fn(log_probs=log_probs, labels=labels)
  
      self.eval_loss(
          loss=eval_loss,
          num_measurements=log_probs.shape[0] * log_probs.shape[1],
      )
      self.eval_ppl(
          log_probs=log_probs, labels=labels, mask=output_mask
      )
      self.validation_step_outputs.append(
          {}
      ) if mode == "val" else self.test_step_outputs.append({})
      return {}
  
imports: import json
  import math
  from typing import Dict, Optional
  import numpy
  import torch
  import torch.utils.data
  from omegaconf import DictConfig, OmegaConf
  from pytorch_lightning import Trainer
  from nemo.collections.common.losses import SmoothedCrossEntropyLoss
  from nemo.collections.common.metrics import GlobalAverageLossMetric
  from nemo.collections.common.parts import transformer_weights_init
  from nemo.collections.nlp.data import SentenceDataset, TarredSentenceDataset
  from nemo.collections.nlp.metrics import SequencePerplexity
  from nemo.collections.nlp.modules.common import TokenClassifier
  from nemo.collections.nlp.modules.common.lm_utils import get_transformer
  from nemo.collections.nlp.modules.common.tokenizer_utils import get_tokenizer
  from nemo.core.classes.common import typecheck
  from nemo.core.classes.modelPT import ModelPT
  from nemo.utils import logging, model_utils
label_desc: Use image node_id 4 for calling the TransformerLMModel obj's underlying member method code with example usage: obj.eval_step(batch, batch_idx) and returns: dict