class_docstr: The basic Trainer for basic training and evaluation strategies in text generation systems.
  
  This class defines common functions for training and evaluation processes of most text generation system models,
  including `fit()`, `evaluate()`, `resume_checkpoint()` and some other features helpful for model training and
  evaluation.
  
  Generally speaking, this class can serve most text generation system models, If the training process of the model
  is to simply optimize a single loss without involving any complex training strategies, such as adversarial learning,
  pre-training and so on.
  
  Initializing the Trainer needs two parameters: `config` and `model`. `config` records the parameters' information
  for controlling training and evaluation, such as `learning_rate`, `epochs` and so on.
  More information can be found in [placeholder]. `model` is the instantiated object of a Model Class.
class_docstr_tok: ["The","basic","Trainer","for","basic","training","and","evaluation","strategies","in","text","generation","systems",".","This","class","defines","common","functions","for","training","and","evaluation","processes","of","most","text","generation","system","models",",","including","`","fit","(",")","`",",","`","evaluate","(",")","`",",","`","resume_checkpoint","(",")","`","and","some","other","features","helpful","for","model","training","and","evaluation",".","Generally","speaking",",","this","class","can","serve","most","text","generation","system","models",",","If","the","training","process","of","the","model","is","to","simply","optimize","a","single","loss","without","involving","any","complex","training","strategies",",","such","as","adversarial","learning",",","pre-training","and","so","on",".","Initializing","the","Trainer","needs","two","parameters",":","`","config","`","and","`","model","`",".","`","config","`","records","the","parameters","'","information","for","controlling","training","and","evaluation",",","such","as","`","learning_rate","`",",","`","epochs","`","and","so","on",".","More","information","can","be","found","in","[","placeholder","]",".","`","model","`","is","the","instantiated","object","of","a","Model","Class","."]
docstr: Init the optimizer and scheduler.
  
  Returns:
      Union[optim.Optimizer, AbstractScheduler]: the optimizer
docstr_tok: ["Init","the","optimizer","and","scheduler",".","Returns",":","Union","[","optim.Optimizer",",","AbstractScheduler","]",":","the","optimizer"]
code: def _build_optimizer(
      self, optimizer: str, scheduler: Optional[str]
  ) -> Union[optim.Optimizer, AbstractScheduler]:
      """Init the optimizer and scheduler.
  
      Returns:
          Union[optim.Optimizer, AbstractScheduler]: the optimizer
      """
  
      optimizer_class = collections.defaultdict(
          lambda: optim.AdamW,
          {
              "adam": optim.Adam,
              "adamw": optim.AdamW,
              "sgd": optim.SGD,
              "adagrad": optim.Adagrad,
              "rmsprop": optim.RMSprop,
              "adafactor": transformers.Adafactor,
          },
      )
      scheduler_class = {
          "inverse": InverseSquareRootScheduler,
          "cosine": CosineScheduler,
          "linear": LinearScheduler,
          "constant": ConstantScheduler,
      }
  
      # dealing with adafactor
      if optimizer == "adafactor":
          # using adafactor_kwargs in overall.yaml
          if self.grad_clip is not None:
              self.grad_clip = None
              self.logger.warning(
                  "Additional optimizer operations like gradient clipping "
                  "should not be used alongside Adafactor."
              )
          self.optimizer_kwargs.update(self.adafactor_kwargs)
  
      # get optimizer (use default value of pytorch if self.optimizer_kwargs is empty)
      self.logger.debug(f"Using optimizer {optimizer}")
      optimizer = optimizer_class[optimizer](
          params=self._trainable_parameters, **self.optimizer_kwargs
      )
  
      # scheduling
      if scheduler is not None and scheduler in scheduler_class:
          assert isinstance(
              self.scheduler_kwargs, dict
          ), "Please specify scheduler_kwargs"
          self.logger.debug(f"Using scheduler {scheduler}.")
          self.scheduler_kwargs.setdefault("max_lr", self.learning_rate)
          optimizer = scheduler_class[scheduler](
              base_optimizer=optimizer, **self.scheduler_kwargs
          )
  
      return optimizer
  
imports: import collections
  import os
  from accelerate.logging import get_logger
  from typing import Optional, Union, List, Dict
  import math
  import torch
  import torch.optim
  import transformers
  from accelerate import Accelerator
  from accelerate.utils import set_seed
  from torch.utils.data import DataLoader
  from tqdm import tqdm
  from textbox import Config
  from textbox.utils.dashboard import get_dashboard, Timestamp, EpochTracker
  from .scheduler import AbstractScheduler, InverseSquareRootScheduler, CosineScheduler, LinearScheduler, ConstantScheduler
  from torch.utils.data import DataLoader
  from ..evaluator import BaseEvaluator
  from ..model.abstract_model import AbstractModel
  from ..utils import serialized_save, init_seed
  from ..utils.enum_type import PLM_MODELS, RNN_MODELS
label_desc: Use image node_id 2 for calling the Trainer obj's underlying member method code with example usage: obj._build_optimizer(optimizer, scheduler) and returns: optimizer