class_docstr: Class to help manage, save, clean up, and average checkpoints.
docstr: save() should always be called before calling this function - to ensure
  that extra_state and self._averaged_params have been updated correctly.
docstr_tok: ["save","(",")","should","always","be","called","before","calling","this","function","-","to","ensure","that","extra_state","and","self._averaged_params","have","been","updated","correctly","."]
code: def save_best_averaged_checkpoint(
      self, args, trainer, extra_state: Dict[str, Any]
  ):
      """
      save() should always be called before calling this function - to ensure
      that extra_state and self._averaged_params have been updated correctly.
      """
      best_averaged_checkpoint_filename = os.path.join(
          args.save_dir, constants.AVERAGED_CHECKPOINT_BEST_FILENAME
      )
      self.log_if_verbose(
          f"| Preparing to save new best averaged checkpoint to "
          f"{best_averaged_checkpoint_filename}."
      )
      state_dict = trainer.state_dict()
      state_dict["args"] = args
      state_dict["cfg"] = None
      state_dict["model"] = self._averaged_params
      state_dict["extra_state"].update(extra_state)
      state_dict = fairseq_utils.move_to_cpu(state_dict)
      checkpoint_utils.torch_persistent_save(
          obj=state_dict,
          filename=best_averaged_checkpoint_filename,
      )
      self.log_if_verbose(
          f"| Finished saving new best averaged checkpoint to "
          f"{best_averaged_checkpoint_filename}."
      )
  
imports: import os
  from collections import deque, OrderedDict
  from typing import Any, Deque, Dict, List, Optional, Tuple
  import torch
  from fairseq import checkpoint_utils, utils
  from pytorch_translate import constants
  from pytorch_translate.file_io import PathManager
label_desc: Use image node_id 10 for calling the CheckpointManager obj's underlying member method code with example usage: obj.save_best_averaged_checkpoint(args, trainer, extra_state) without return types