class_docstr: Stores a constituency model and its optimizer
class_docstr_tok: ["Stores","a","constituency","model","and","its","optimizer"]
code: def load(filename, args, foundation_cache=None, load_optimizer=False):
      if not os.path.exists(filename):
          if args.save_dir is None:
              raise FileNotFoundError(
                  "Cannot find model in {} and args.save_dir is None".format(
                      filename
                  )
              )
          elif os.path.exists(os.path.join(args.save_dir, filename)):
              filename = os.path.join(args.save_dir, filename)
          else:
              raise FileNotFoundError(
                  "Cannot find model in {} or in {}".format(
                      filename, os.path.join(args.save_dir, filename)
                  )
              )
      try:
          checkpoint = torch.load(
              filename, lambda storage, loc: storage
          )
      except BaseException:
          logger.exception("Cannot load model from {}".format(filename))
          raise
      logger.debug("Loaded model {}".format(filename))
  
      epochs_trained = checkpoint.get("epochs_trained", 0)
      global_step = checkpoint.get("global_step", 0)
      best_score = checkpoint.get("best_score", None)
  
      # TODO: can remove this block once all models are retrained
      if "params" not in checkpoint:
          model_params = {
              "model": checkpoint["model"],
              "config": checkpoint["config"],
              "labels": checkpoint["labels"],
              "extra_vocab": checkpoint["extra_vocab"],
          }
      else:
          model_params = checkpoint["params"]
      model_type = model_params["config"].model_type
  
      if model_type == ModelType.CNN:
          pretrain = Trainer.load_pretrain(args, foundation_cache)
          elmo_model = (
              utils.load_elmo(args.elmo_model)
              if args.use_elmo
              else None
          )
          charmodel_forward = load_charlm(
              args.charlm_forward_file, foundation_cache
          )
          charmodel_backward = load_charlm(
              args.charlm_backward_file, foundation_cache
          )
  
          bert_model = model_params["config"].bert_model
          bert_model, bert_tokenizer = load_bert(
              bert_model, foundation_cache
          )
          model = cnn_classifier.CNNClassifier(
              pretrain=pretrain,
              extra_vocab=model_params["extra_vocab"],
              labels=model_params["labels"],
              charmodel_forward=charmodel_forward,
              charmodel_backward=charmodel_backward,
              elmo_model=elmo_model,
              bert_model=bert_model,
              bert_tokenizer=bert_tokenizer,
              args=model_params["config"],
          )
      elif model_type == ModelType.CONSTITUENCY:
          pretrain_args = {
              "wordvec_pretrain_file": args.wordvec_pretrain_file,
              "charlm_forward_file": args.charlm_forward_file,
              "charlm_backward_file": args.charlm_backward_file,
          }
          tree_embedding = TreeEmbedding.model_from_params(
              model_params["tree_embedding"],
              pretrain_args,
              foundation_cache,
          )
          model = constituency_classifier.ConstituencyClassifier(
              tree_embedding=tree_embedding,
              labels=model_params["labels"],
              args=model_params["config"],
          )
      else:
          raise ValueError("Unknown model type {}".format(model_type))
      model.load_state_dict(model_params["model"], strict=False)
      model = model.to(args.device)
  
      logger.debug("-- MODEL CONFIG --")
      for k in model.config.__dict__:
          logger.debug("  --{}: {}".format(k, model.config.__dict__[k]))
  
      logger.debug("-- MODEL LABELS --")
      logger.debug("  {}".format(" ".join(model.labels)))
  
      optimizer = None
      if load_optimizer:
          optimizer = Trainer.build_optimizer(model, args)
          if checkpoint.get("optimizer_state_dict", None) is not None:
              optimizer.load_state_dict(
                  checkpoint["optimizer_state_dict"]
              )
          else:
              logger.info(
                  "Attempted to load optimizer to resume training, but optimizer not saved.  Creating new optimizer"
              )
  
      trainer = Trainer(
          model, optimizer, epochs_trained, global_step, best_score
      )
  
      return trainer
  
file_path: stanza/stanza/models/classifiers/trainer.py
imports: import logging
  import os
  import torch
  import torch.optim
  import stanza.models.classifiers.data
  import stanza.models.classifiers.cnn_classifier
  import stanza.models.classifiers.constituency_classifier
  from stanza.models.classifiers.utils import ModelType
  from stanza.models.common.foundation_cache import load_bert, load_charlm, load_pretrain
  from stanza.models.common.pretrain import Pretrain
  from stanza.models.constituency.tree_embedding import TreeEmbedding
label_desc: Use image node_id 3 for calling the Trainer obj's underlying member method code with example usage: obj.load(filename, args, foundation_cache, load_optimizer) and returns: trainer