class_docstr: ## Configurations
  
  This inherits from
  [`NLPAutoRegressionConfigs`](../../experiments/nlp_autoregression.html)
  because it has the data pipeline implementations that we reuse here.
  We have implemented a custom training step form MLM.
class_docstr_tok: ["#","#","Configurations","This","inherits","from","[","`","NLPAutoRegressionConfigs","`","]","(","..","\/","..","\/experiments\/nlp_autoregression.html",")","because","it","has","the","data","pipeline","implementations","that","we","reuse","here",".","We","have","implemented","a","custom","training","step","form","MLM","."]
docstr: ### Training or validation step
docstr_tok: ["#","#","#","Training","or","validation","step"]
code: def step(self, batch: any, batch_idx: BatchIndex):
      """
      ### Training or validation step
      """
  
      # Move the input to the device
      data = batch[0].to(self.device)
  
      # Update global step (number of tokens processed) when in training mode
      if self.mode.is_train:
          tracker.add_global_step(data.shape[0] * data.shape[1])
  
      # Get the masked input and labels
      with torch.no_grad():
          data, labels = self.mlm(data)
  
      # Whether to capture model outputs
      with self.mode.update(is_log_activations=batch_idx.is_last):
          # Get model outputs.
          # It's returning a tuple for states when using RNNs.
          # This is not implemented yet.
          output, *_ = self.model(data)
  
      # Calculate and log the loss
      loss = self.loss_func(
          output.view(-1, output.shape[-1]), labels.view(-1)
      )
      tracker.add("loss.", loss)
  
      # Calculate and log accuracy
      self.accuracy(output, labels)
      self.accuracy.track()
  
      # Train the model
      if self.mode.is_train:
          # Calculate gradients
          loss.backward()
          # Clip gradients
          torch.nn.utils.clip_grad_norm_(
              self.model.parameters(), max_norm=self.grad_norm_clip
          )
          # Take optimizer step
          self.optimizer.step()
          # Log the model parameters and gradients on last batch of every epoch
          if batch_idx.is_last:
              tracker.add("model", self.model)
          # Clear the gradients
          self.optimizer.zero_grad()
  
      # Save the tracked metrics
      tracker.save()
  
imports: from typing import List
  import torch
  from torch import nn
  from labml import experiment, tracker, logger
  from labml.configs import option
  from labml.logger import Text
  from labml_helpers.metrics.accuracy import Accuracy
  from labml_helpers.module import Module
  from labml_helpers.train_valid import BatchIndex
  from labml_nn.experiments.nlp_autoregression import NLPAutoRegressionConfigs
  from labml_nn.transformers import Encoder, Generator
  from labml_nn.transformers import TransformerConfigs
  from labml_nn.transformers.mlm import MLM
label_desc: Use image node_id 2 for calling the Configs obj's underlying member method code with example usage: obj.step(batch, batch_idx) without return types