class_docstr: Class for LR Schedulers.
  
  Includes some basic functionality by default - setting up the warmup
  scheduler, passing the correct number of steps to train_step, loading and
  saving states.
  Subclasses must implement abstract methods train_step() and valid_step().
  Schedulers should be initialized with lr_scheduler_factory().
  __init__() should not be called directly.
docstr: Create the learning rate scheduler, and assign it to self.scheduler. This
  scheduler will be updated upon a call to receive_metrics. May also create
  self.warmup_scheduler, if appropriate.
  
  :param opt opt:
      Arguments received by torch_agent
  :param optimizer optimizer:
      Optimizer being used for training. May be wrapped in
      fp16_optimizer_wrapper depending on whether fp16 is used.
  :param state_dict states:
      Possible state_dict provided by model checkpoint, for restoring
      LR state.
  :param bool hard_reset:
      If true, the LR scheduler should ignore the state dictionary.
  :return: ParlAILRScheduler object
docstr_tok: ["Create","the","learning","rate","scheduler",",","and","assign","it","to","self.scheduler",".","This","scheduler","will","be","updated","upon","a","call","to","receive_metrics",".","May","also","create","self.warmup_scheduler",",","if","appropriate",".",":","param","opt","opt",":","Arguments","received","by","torch_agent",":","param","optimizer","optimizer",":","Optimizer","being","used","for","training",".","May","be","wrapped","in","fp16_optimizer_wrapper","depending","on","whether","fp16","is","used",".",":","param","state_dict","states",":","Possible","state_dict","provided","by","model","checkpoint",",","for","restoring","LR","state",".",":","param","bool","hard_reset",":","If","true",",","the","LR","scheduler","should","ignore","the","state","dictionary",".",":","return",":","ParlAILRScheduler","object"]
code: def lr_scheduler_factory(
      cls, opt, optimizer, states, hard_reset=False
  ):
      """
      Create the learning rate scheduler, and assign it to self.scheduler. This
      scheduler will be updated upon a call to receive_metrics. May also create
      self.warmup_scheduler, if appropriate.
  
      :param opt opt:
          Arguments received by torch_agent
      :param optimizer optimizer:
          Optimizer being used for training. May be wrapped in
          fp16_optimizer_wrapper depending on whether fp16 is used.
      :param state_dict states:
          Possible state_dict provided by model checkpoint, for restoring
          LR state.
      :param bool hard_reset:
          If true, the LR scheduler should ignore the state dictionary.
      :return: ParlAILRScheduler object
      """
  
      patience = opt.get("lr_scheduler_patience", 3)
      decay = opt.get("lr_scheduler_decay", 0.5)
      warmup_updates = opt.get("warmup_updates", -1)
      warmup_rate = opt.get("warmup_rate", 1e-4)
      max_lr_steps = opt.get("max_train_steps", -1)
      if opt.get("max_lr_steps", -1) > 0:
          raise ValueError(
              "--max-lr-steps is **DEPRECATED**; please set --max-train-steps directly"
          )
      invsqrt_lr_decay_gamma = opt.get("invsqrt_lr_decay_gamma", -1)
  
      if opt.get("lr_scheduler") == "none":
          return None
      elif decay == 1.0:
          warn_once(
              "Your LR decay is set to 1.0. Assuming you meant you wanted "
              "to disable learning rate scheduling. Adjust --lr-scheduler-decay "
              "if this is not correct."
          )
          return None
      elif opt.get("lr_scheduler") == "reduceonplateau":
          scheduler = ReduceOnPlateauLRScheduler(
              optimizer, patience, decay, warmup_updates, warmup_rate
          )
      elif opt.get("lr_scheduler") == "fixed":
          scheduler = FixedLRScheduler(
              optimizer, patience, decay, warmup_updates, warmup_rate
          )
      elif opt.get("lr_scheduler") == "invsqrt":
          scheduler = InvSqrtLRScheduler(
              optimizer,
              patience,
              decay,
              warmup_updates,
              warmup_rate,
              invsqrt_lr_decay_gamma,
              max_lr_steps,
          )
      elif opt.get("lr_scheduler") == "cosine":
          scheduler = CosineLRScheduler(
              optimizer,
              patience,
              decay,
              warmup_updates,
              warmup_rate,
              max_lr_steps,
          )
      elif opt.get("lr_scheduler") == "linear":
          scheduler = LinearLRScheduler(
              optimizer,
              patience,
              decay,
              warmup_updates,
              warmup_rate,
              max_lr_steps,
          )
      else:
          raise ValueError(
              "Don't know what to do with --lr-scheduler '{}'".format(
                  opt.get("lr_scheduler")
              )
          )
  
      # time to load LR state from the checkpoint, if possible.
      if (
          # there is already an old LR scheduler saved on disk
          states
          # and there was a scheduler in the dump
          and "lr_scheduler_type" in states
          # and the old LR scheduler is different
          and states.get("lr_scheduler_type") != opt["lr_scheduler"]
          # and we're not already using a fresh scheduler
          and not hard_reset
      ):
          # the LR scheduler changed, start things fresh
          warn_once(
              f"LR scheduler ({opt['lr_scheduler']}) is different from saved "
              f"({states.get('lr_scheduler_type')}). Starting fresh!"
          )
          hard_reset = True
  
      # setup warmup scheduler after loading saved scheduler
      scheduler._init_warmup_scheduler(optimizer, states, hard_reset)
  
      if not hard_reset:
          # do the actual loading (if possible)
          scheduler.load_state(states)
  
      return scheduler
  
imports: import math
  from typing import Optional
  from parlai.core.params import ParlaiParser
  from parlai.core.opt import Opt
  from abc import abstractmethod
  from torch import optim
  import numpy
  from parlai.core.exceptions import StopTrainException
  from parlai.utils.misc import warn_once
label_desc: Use image node_id 11 for calling the ParlAILRScheduler obj's underlying member method code with example usage: obj.lr_scheduler_factory(cls, opt, optimizer, states, hard_reset) and returns: scheduler, None, None