docstr: The real training loop.
docstr_tok: ["The","real","training","loop","."]
code: def train(self):
      """
      The real training loop.
      """
      if self.is_master:
          logger.info("Starting training")
      self.last_log = time.time()
      self.student.train()
      self.teacher.eval()
  
      for _ in range(self.params.n_epoch):
          if self.is_master:
              logger.info(
                  f"--- Starting epoch {self.epoch}/{self.params.n_epoch-1}"
              )
          if self.multi_gpu:
              torch.distributed.barrier()
  
          iter_bar = tqdm(
              self.dataloader,
              desc="-Iter",
              disable=self.params.local_rank not in [-1, 0],
          )
          for batch in iter_bar:
              if self.params.n_gpu > 0:
                  batch = tuple(
                      t.to(f"cuda:{self.params.local_rank}")
                      for t in batch
                  )
  
              if self.mlm:
                  (
                      token_ids,
                      attn_mask,
                      lm_labels,
                  ) = self.prepare_batch_mlm(batch=batch)
              else:
                  (
                      token_ids,
                      attn_mask,
                      lm_labels,
                  ) = self.prepare_batch_clm(batch=batch)
              self.step(
                  input_ids=token_ids,
                  attention_mask=attn_mask,
                  lm_labels=lm_labels,
              )
  
              iter_bar.update()
              iter_bar.set_postfix(
                  {
                      "Last_loss": f"{self.last_loss:.2f}",
                      "Avg_cum_loss": f"{self.total_loss_epoch/self.n_iter:.2f}",
                  }
              )
          iter_bar.close()
  
          if self.is_master:
              logger.info(
                  f"--- Ending epoch {self.epoch}/{self.params.n_epoch-1}"
              )
          self.end_epoch()
  
      if self.is_master:
          logger.info(
              "Save very last checkpoint as `pytorch_model.bin`."
          )
          self.save_checkpoint(checkpoint_name="pytorch_model.bin")
          logger.info("Training is finished")
  
imports: import math
  import os
  import time
  import psutil
  import torch
  from grouped_batch_sampler import GroupedBatchSampler, create_lengths_groups
  from lm_seqs_dataset import LmSeqsDataset
  from torch import nn
  from torch.optim import AdamW
  from torch.utils.data import BatchSampler, DataLoader, RandomSampler
  from torch.utils.data.distributed import DistributedSampler
  from tqdm import tqdm
  from transformers import get_linear_schedule_with_warmup
  from utils import logger
label_desc: Use image node_id 5 for calling the Distiller obj's underlying member method code with example usage: obj.train() without return types