class_docstr: This class implements the functionality for checkpointing your model and trainer state
  during training. It is agnostic as to what those states look like (they are typed as
  `Dict[str, Any]`), but they will be fed to `torch.save` so they should be serializable
  in that sense. They will also be restored as `Dict[str, Any]`, which means the calling
  code is responsible for knowing what to do with them.
  
  # Parameters
  
  save_completed_epochs : `bool`, (default=`True`)
      Saves model and trainer state at the end of each completed epoch.
  save_every_num_seconds : `int`, optional (default=`None`)
      If set, makes sure we never go longer than this number of seconds between saving a model.
  save_every_num_batches : `int`, optional (default=`None`)
      If set, makes sure we never go longer than this number of batches between saving a model.
  keep_most_recent_by_count : `int`, optional (default=`2`)
      Sets the number of model checkpoints to keep on disk. If both `keep_most_recent_by_count` and
      `keep_most_recent_by_age` are set, we'll keep checkpoints that satisfy either criterion.
      If both are `None`, we keep all checkpoints.
  keep_most_recent_by_age : `int`, optional (default=`None`)
      Sets the number of seconds we'll keep a checkpoint before deleting it. If both
      `keep_most_recent_by_count` and `keep_most_recent_by_age` are set, we'll keep checkpoints
      that satisfy either criterion. If both are `None`, we keep all checkpoints.
class_docstr_tok: ["This","class","implements","the","functionality","for","checkpointing","your","model","and","trainer","state","during","training",".","It","is","agnostic","as","to","what","those","states","look","like","(","they","are","typed","as","`","Dict","[","str",",","Any","]","`",")",",","but","they","will","be","fed","to","`","torch.save","`","so","they","should","be","serializable","in","that","sense",".","They","will","also","be","restored","as","`","Dict","[","str",",","Any","]","`",",","which","means","the","calling","code","is","responsible","for","knowing","what","to","do","with","them",".","#","Parameters","save_completed_epochs",":","`","bool","`",",","(","default=","`","True","`",")","Saves","model","and","trainer","state","at","the","end","of","each","completed","epoch",".","save_every_num_seconds",":","`","int","`",",","optional","(","default=","`","None","`",")","If","set",",","makes","sure","we","never","go","longer","than","this","number","of","seconds","between","saving","a","model",".","save_every_num_batches",":","`","int","`",",","optional","(","default=","`","None","`",")","If","set",",","makes","sure","we","never","go","longer","than","this","number","of","batches","between","saving","a","model",".","keep_most_recent_by_count",":","`","int","`",",","optional","(","default=","`","2","`",")","Sets","the","number","of","model","checkpoints","to","keep","on","disk",".","If","both","`","keep_most_recent_by_count","`","and","`","keep_most_recent_by_age","`","are","set",",","we","'ll","keep","checkpoints","that","satisfy","either","criterion",".","If","both","are","`","None","`",",","we","keep","all","checkpoints",".","keep_most_recent_by_age",":","`","int","`",",","optional","(","default=","`","None","`",")","Sets","the","number","of","seconds","we","'ll","keep","a","checkpoint","before","deleting","it",".","If","both","`","keep_most_recent_by_count","`","and","`","keep_most_recent_by_age","`","are","set",",","we","'ll","keep","checkpoints","that","satisfy","either","criterion",".","If","both","are","`","None","`",",","we","keep","all","checkpoints","."]
code: def save_checkpoint(
      self,
      trainer: Trainer,
  ) -> None:
      if self._serialization_dir is None:
          return
  
      tcps = trainer.get_checkpoint_state()
      if tcps is None:
          # This should only happen from a non-primary node in distributed training with a
          # regular non-sharded model.
          assert not self._is_primary and not self.state_is_sharded
          return
  
      epochs_completed = tcps.trainer_state["epochs_completed"]
      batches_in_epoch_completed = tcps.trainer_state[
          "batches_in_epoch_completed"
      ]
  
      model_state_path = self._model_state_path(
          epochs_completed,
          batches_in_epoch_completed,
      )
      if not os.path.isfile(model_state_path):
          torch.save(tcps.model_state, model_state_path)
  
      trainer_state_path = self._training_state_path(
          epochs_completed,
          batches_in_epoch_completed,
      )
      if not os.path.isfile(trainer_state_path):
          torch.save(tcps.trainer_state, trainer_state_path)
  
      self._last_save_time = time.time()
      self._last_save_num_epochs_completed = epochs_completed
      self._last_save_num_batches_in_epoch_completed = (
          batches_in_epoch_completed
      )
  
      if self._is_primary and (
          self._keep_most_recent_by_age is not None
          or self._keep_most_recent_by_count is not None
      ):
          checkpoints = list(self._find_all_checkpoints())
          checkpoints.sort(reverse=True)
  
          # Keep the most recent n checkpoints
          if self._keep_most_recent_by_count is not None:
              checkpoints_to_keep = set(
                  checkpoints[: self._keep_most_recent_by_count]
              )
          else:
              checkpoints_to_keep = set()
  
          # Keep the youngest checkpoints by age
          now = time.time()
          if self._keep_most_recent_by_age is not None:
              for checkpoint in checkpoints:
                  checkpoint_mtime = max(
                      os.path.getmtime(n)
                      for n in [
                          self._model_state_path(*checkpoint),
                          self._training_state_path(*checkpoint),
                      ]
                  )
                  if (
                      now - checkpoint_mtime
                      <= self._keep_most_recent_by_age
                  ):
                      checkpoints_to_keep.add(checkpoint)
  
          # Remove everything we're not keeping
          for checkpoint in checkpoints:
              if checkpoint not in checkpoints_to_keep:
                  self._remove_checkpoint(*checkpoint)
  
imports: import glob
  from typing import Tuple, Optional, Set, Union
  import logging
  import os
  import re
  import time
  import torch
  import torch.distributed
  from allennlp.common import Registrable
  from allennlp.common.util import is_distributed
  from allennlp.nn import util
  from allennlp.training.trainer import Trainer, TrainerCheckpoint
label_desc: Use image node_id 10 for calling the Checkpointer obj's underlying member method code with example usage: obj.save_checkpoint(trainer) without return types