class_docstr: Class to help manage, save, clean up, and average checkpoints.
code: def get_averaged_params(self, new_params: OrderedDict) -> OrderedDict:
      if not self._initialized:
          self._initialize()
  
      self.log_if_verbose(
          f"| Preparing to average {len(new_params)} params."
      )
      # Special case for the first time or when we're not doing checkpoint
      # averaging.
      if (
          len(self._averaged_params) == 0
          or self._checkpoint_files.maxlen == 1
      ):
          copied_params: OrderedDict = OrderedDict()
          for k, v in new_params.items():
              copied_params[k] = convert_tensor(v, clone=True)
          self.log_if_verbose(
              f"| Finished copying {len(new_params)} params to CPU "
              f"(no averaging needed)."
          )
          return copied_params
  
      new_average: OrderedDict = OrderedDict()
      new_denom: int = min(  # noqa
          self._checkpoint_files.maxlen, len(self._checkpoint_files) + 1
      )
  
      if len(self._checkpoint_files) == self._checkpoint_files.maxlen:
          # We've reached the maximum number of checkpoints to average over,
          # so the denominator won't change even when we add a new param - we
          # just kick out the values from the oldest checkpoint.
          self.log_if_verbose(
              f"| Preparing to load old checkpoint "
              f"{self._checkpoint_files[0]} to calculate average."
          )
          state: Dict[str, Any] = load_to_cpu(self._checkpoint_files[0])
          self.log_if_verbose(
              f"| Finished loading old checkpoint "
              f"{self._checkpoint_files[0]} to calculate average."
          )
          old_params: OrderedDict = state["model"]
          for k, v in old_params.items():
              v = convert_tensor(v, clone=False)
              sanity_check_tensor(
                  tensor_name=k,
                  old_tensor=self._averaged_params[k],
                  new_tensor=v,
              )
              if is_integer_tensor(v):
                  new_average[k] = v
              else:
                  new_average[k] = self._averaged_params[k] - (
                      v / len(self._checkpoint_files)
                  )
  
      else:
          # We haven't reached the maximum number of checkpoints to average
          # over, so we simply adjust the denominator and the existing average
          # to account for the larger number of checkpoints we're now
          # averaging over.
          for k, v in self._averaged_params.items():
              if is_integer_tensor(v):
                  new_average[k] = v.clone()
              else:
                  new_average[k] = v * (
                      len(self._checkpoint_files) / new_denom
                  )
  
      # Actually add the new params after the existing average has been
      # adjusted accordingly.
      for k, v in new_params.items():
          v = convert_tensor(v, clone=False)
          sanity_check_tensor(
              tensor_name=k, old_tensor=new_average[k], new_tensor=v
          )
          if not is_integer_tensor(v):
              new_average[k].add_(v / new_denom)
  
      self.log_if_verbose(
          f"| Finished averaging {len(new_params)} params."
      )
      return new_average
  
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 5 for calling the CheckpointManager obj's underlying member method code with example usage: obj.get_averaged_params(new_params) and returns: new_average, copied_params