class_docstr: Controller class for optimization. Mostly a thin
  wrapper for `optim`, but also useful for implementing
  rate scheduling beyond what is currently available.
  Also implements necessary methods for training RNNs such
  as grad manipulations.
  
  Args:
      optimizer: A ``torch.optim.Optimizer`` instance.
      learning_rate: The initial learning rate.
      learning_rate_decay_fn: An optional callable taking the current step
          as argument and return a learning rate scaling factor.
      max_grad_norm: Clip gradients to this global norm.
class_docstr_tok: ["Controller","class","for","optimization",".","Mostly","a","thin","wrapper","for","`","optim","`",",","but","also","useful","for","implementing","rate","scheduling","beyond","what","is","currently","available",".","Also","implements","necessary","methods","for","training","RNNs","such","as","grad","manipulations",".","Args",":","optimizer",":","A","``","torch.optim.Optimizer","``","instance",".","learning_rate",":","The","initial","learning","rate",".","learning_rate_decay_fn",":","An","optional","callable","taking","the","current","step","as","argument","and","return","a","learning","rate","scaling","factor",".","max_grad_norm",":","Clip","gradients","to","this","global","norm","."]
docstr: Builds the optimizer from options.
  
  Args:
    cls: The ``Optimizer`` class to instantiate.
    model: The model to optimize.
    opt: The dict of user options.
    checkpoint: An optional checkpoint to load states from.
  
  Returns:
    An ``Optimizer`` instance.
docstr_tok: ["Builds","the","optimizer","from","options",".","Args",":","cls",":","The","``","Optimizer","``","class","to","instantiate",".","model",":","The","model","to","optimize",".","opt",":","The","dict","of","user","options",".","checkpoint",":","An","optional","checkpoint","to","load","states","from",".","Returns",":","An","``","Optimizer","``","instance","."]
code: def from_opt(cls, model, opt, checkpoint=None):
      """Builds the optimizer from options.
  
      Args:
        cls: The ``Optimizer`` class to instantiate.
        model: The model to optimize.
        opt: The dict of user options.
        checkpoint: An optional checkpoint to load states from.
  
      Returns:
        An ``Optimizer`` instance.
      """
      optim_opt = opt
      optim_state_dict = None
  
      if (
          opt.train_from
          and checkpoint is not None
          and "optim" in checkpoint.keys()
      ):
          optim = checkpoint["optim"]
          ckpt_opt = checkpoint["opt"]
          ckpt_state_dict = {}
          if isinstance(optim, Optimizer):  # Backward compatibility.
              ckpt_state_dict["training_step"] = optim._step + 1
              ckpt_state_dict["decay_step"] = optim._step + 1
              ckpt_state_dict[
                  "optimizer"
              ] = optim.optimizer.state_dict()
          else:
              ckpt_state_dict = optim
  
          if opt.reset_optim == "none":
              # Load everything from the checkpoint.
              optim_opt = ckpt_opt
              optim_state_dict = ckpt_state_dict
          elif opt.reset_optim == "all":
              # Build everything from scratch.
              pass
          elif opt.reset_optim == "states":
              # Reset optimizer, keep options.
              optim_opt = ckpt_opt
              optim_state_dict = ckpt_state_dict
              del optim_state_dict["optimizer"]
          elif opt.reset_optim == "keep_states":
              # Reset options, keep optimizer.
              optim_state_dict = ckpt_state_dict
  
      optimizer = cls(
          build_torch_optimizer(model, optim_opt),
          optim_opt.learning_rate,
          learning_rate_decay_fn=make_learning_rate_decay_fn(optim_opt),
          max_grad_norm=optim_opt.max_grad_norm,
      )
      if opt.model_dtype == "fp16":
          if opt.optim == "fusedadam":
              if opt.apex_opt_level in ["O0", "O1", "O2", "O3"]:
                  optimizer._fp16 = "apex.amp"
              else:
                  optimizer._fp16 = "legacy"
          else:
              optimizer._fp16 = "amp"
              from torch.cuda.amp import GradScaler
  
              optimizer._scaler = GradScaler()
      if optim_state_dict:
          optimizer.load_state_dict(optim_state_dict)
      return optimizer
  
imports: import torch
  import torch.optim
  from torch.nn.utils import clip_grad_norm_
  import operator
  import functools
  from copy import copy
  from math import sqrt
  import types
  import os
  import importlib
  from onmt.utils.misc import fn_args
label_desc: Use image node_id 2 for calling the Optimizer obj's underlying member method code with example usage: obj.from_opt(cls, model, opt, checkpoint) and returns: optimizer