class_docstr: Saves checkpoints and recovers from them.
  
  Arguments:
  
  checkpoints_dir : str, pathlib.Path
      Path to directory where to save checkpoints.
  recoverables : mapping, optional
      Objects to to recover. They need a (unique) name: this is used
      to connect the parameters in a checkpoint to the correct recoverable.
      The name is also used in the filename of the
      savefile for the objects parameters. These can also be added with
      add_recoverable or add_recoverables or just modifying
      checkpointer.recoverables directly.
  custom_load_hooks : mapping, optional
      A mapping from name [same as in recoverables] to function or method.
      Sets a custom loading hook for a particular object. The
      function/method must be callable with signature (instance, path)
      using positional arguments. This is satisfied by for example:
      `def loader(self, path)`.
  custom_save_hooks : mapping, optional
      Mapping from name [same as in recoverables] to function or method.
      Sets a custom saving hook for a particular object. The
      function/method must be callable with
      signature (instance, path) using positional arguments. This is
      satisfied by for example: def saver(self, path):
  allow_partial_load : bool, optional
      If True, allows loading a checkpoint where a savefile is not found
      for every registered recoverable. In that case, only the found
      savefiles are loaded. When False, loading such a save will raise
      RuntimeError. (default: False)
  
  Example
  -------
  >>> import torch
  >>> #SETUP:
  >>> tempdir = getfixture('tmpdir')
  >>> class Recoverable(torch.nn.Module):
  ...     def __init__(self, param):
  ...         super().__init__()
  ...         self.param = torch.nn.Parameter(torch.tensor([param]))
  ...     def forward(self, x):
  ...         return x * self.param
  >>> recoverable = Recoverable(1.)
  >>> recoverables = {'recoverable': recoverable}
  >>> # SETUP DONE.
  >>> checkpointer = Checkpointer(tempdir, recoverables)
  >>> first_ckpt = checkpointer.save_checkpoint()
  >>> recoverable.param.data = torch.tensor([2.])
  >>> loaded_ckpt = checkpointer.recover_if_possible()
  >>> # Parameter has been loaded:
  >>> assert recoverable.param.data == torch.tensor([1.])
  >>> # With this call, by default, oldest checkpoints are deleted:
  >>> checkpointer.save_and_keep_only()
  >>> assert first_ckpt not in checkpointer.list_checkpoints()
class_docstr_tok: ["Saves","checkpoints","and","recovers","from","them",".","Arguments",":","checkpoints_dir",":","str",",","pathlib.Path","Path","to","directory","where","to","save","checkpoints",".","recoverables",":","mapping",",","optional","Objects","to","to","recover",".","They","need","a","(","unique",")","name",":","this","is","used","to","connect","the","parameters","in","a","checkpoint","to","the","correct","recoverable",".","The","name","is","also","used","in","the","filename","of","the","savefile","for","the","objects","parameters",".","These","can","also","be","added","with","add_recoverable","or","add_recoverables","or","just","modifying","checkpointer.recoverables","directly",".","custom_load_hooks",":","mapping",",","optional","A","mapping","from","name","[","same","as","in","recoverables","]","to","function","or","method",".","Sets","a","custom","loading","hook","for","a","particular","object",".","The","function\/method","must","be","callable","with","signature","(","instance",",","path",")","using","positional","arguments",".","This","is","satisfied","by","for","example",":","`","def","loader","(","self",",","path",")","`",".","custom_save_hooks",":","mapping",",","optional","Mapping","from","name","[","same","as","in","recoverables","]","to","function","or","method",".","Sets","a","custom","saving","hook","for","a","particular","object",".","The","function\/method","must","be","callable","with","signature","(","instance",",","path",")","using","positional","arguments",".","This","is","satisfied","by","for","example",":","def","saver","(","self",",","path",")",":","allow_partial_load",":","bool",",","optional","If","True",",","allows","loading","a","checkpoint","where","a","savefile","is","not","found","for","every","registered","recoverable",".","In","that","case",",","only","the","found","savefiles","are","loaded",".","When","False",",","loading","such","a","save","will","raise","RuntimeError",".","(","default",":","False",")","Example","--","--","--","-",">",">",">","import","torch",">",">",">","#","SETUP",":",">",">",">","tempdir","=","getfixture","(","'tmpdir","'",")",">",">",">","class","Recoverable","(","torch.nn.Module",")",":","...","def","__init__","(","self",",","param",")",":","...","super","(",")",".__init__","(",")","...","self.param","=","torch.nn.Parameter","(","torch.tensor","(","[","param","]",")",")","...","def","forward","(","self",",","x",")",":","...","return","x","*","self.param",">",">",">","recoverable","=","Recoverable","(","1",".",")",">",">",">","recoverables","=","{","'recoverable","'",":","recoverable","}",">",">",">","#","SETUP","DONE",".",">",">",">","checkpointer","=","Checkpointer","(","tempdir",",","recoverables",")",">",">",">","first_ckpt","=","checkpointer.save_checkpoint","(",")",">",">",">","recoverable.param.data","=","torch.tensor","(","[","2",".","]",")",">",">",">","loaded_ckpt","=","checkpointer.recover_if_possible","(",")",">",">",">","#","Parameter","has","been","loaded",":",">",">",">","assert","recoverable.param.data","==","torch.tensor","(","[","1",".","]",")",">",">",">","#","With","this","call",",","by","default",",","oldest","checkpoints","are","deleted",":",">",">",">","checkpointer.save_and_keep_only","(",")",">",">",">","assert","first_ckpt","not","in","checkpointer.list_checkpoints","(",")"]
docstr: Saves a checkpoint, then deletes the least important checkpoints.
  
  Essentially this combines ``save_checkpoint()`` and
  ``delete_checkpoints()`` in one call, providing short syntax.
  
  Arguments
  ---------
  meta : mapping, optional
      A mapping which is added to the meta file in the checkpoint. The
      key "unixtime" is included by default.
  end_of_epoch : bool, optional
      Whether the checkpoint is at the end of an epoch. True by default.
      May affect loading.
  name : str, optional
      Specify a custom name for your checkpoint.
      The name will still have a prefix added. If no name is given,
      a name is created from a timestamp and a random unique id.
  num_to_keep : int, optional
      Number of checkpoints to keep. Defaults to 1. This deletes all
      checkpoints remaining after filtering. Must be >=0.
  keep_recent : bool, optional
      Whether to keep the most recent ``num_to_keep`` checkpoints.
  importance_keys : list, optional
      A list of key functions used in sorting (see the sorted built-in).
      Each callable defines a sort order and num_to_keep checkpoints are
      kept for callable. The checkpoint with the highest keys are kept.
      The functions are passed Checkpoint namedtuples (see above).
  max_keys : list, optional
      A list of keys for which the *highest* value will be kept.
  min_keys : list, optional
      A list of keys for which the *lowest* value will be kept.
  ckpt_predicate : callable, optional
      Use this to exclude some checkpoints from deletion. Before any
      sorting, the list of checkpoints is filtered with this predicate.
      Only the checkpoints for which ckpt_predicate is True can be
      deleted. The function is called with Checkpoint namedtuples
      (see above).
  
  Returns
  -------
  None
      Unlike save_checkpoint, this does not return anything, since
      we cannot guarantee that the saved checkpoint actually survives
      deletion.
docstr_tok: ["Saves","a","checkpoint",",","then","deletes","the","least","important","checkpoints",".","Essentially","this","combines","``","save_checkpoint","(",")","``","and","``","delete_checkpoints","(",")","``","in","one","call",",","providing","short","syntax",".","Arguments","--","--","--","--","-","meta",":","mapping",",","optional","A","mapping","which","is","added","to","the","meta","file","in","the","checkpoint",".","The","key","``","unixtime","''","is","included","by","default",".","end_of_epoch",":","bool",",","optional","Whether","the","checkpoint","is","at","the","end","of","an","epoch",".","True","by","default",".","May","affect","loading",".","name",":","str",",","optional","Specify","a","custom","name","for","your","checkpoint",".","The","name","will","still","have","a","prefix","added",".","If","no","name","is","given",",","a","name","is","created","from","a","timestamp","and","a","random","unique","id",".","num_to_keep",":","int",",","optional","Number","of","checkpoints","to","keep",".","Defaults","to","1",".","This","deletes","all","checkpoints","remaining","after","filtering",".","Must","be",">","=0",".","keep_recent",":","bool",",","optional","Whether","to","keep","the","most","recent","``","num_to_keep","``","checkpoints",".","importance_keys",":","list",",","optional","A","list","of","key","functions","used","in","sorting","(","see","the","sorted","built-in",")",".","Each","callable","defines","a","sort","order","and","num_to_keep","checkpoints","are","kept","for","callable",".","The","checkpoint","with","the","highest","keys","are","kept",".","The","functions","are","passed","Checkpoint","namedtuples","(","see","above",")",".","max_keys",":","list",",","optional","A","list","of","keys","for","which","the","*","highest","*","value","will","be","kept",".","min_keys",":","list",",","optional","A","list","of","keys","for","which","the","*","lowest","*","value","will","be","kept",".","ckpt_predicate",":","callable",",","optional","Use","this","to","exclude","some","checkpoints","from","deletion",".","Before","any","sorting",",","the","list","of","checkpoints","is","filtered","with","this","predicate",".","Only","the","checkpoints","for","which","ckpt_predicate","is","True","can","be","deleted",".","The","function","is","called","with","Checkpoint","namedtuples","(","see","above",")",".","Returns","--","--","--","-","None","Unlike","save_checkpoint",",","this","does","not","return","anything",",","since","we","can","not","guarantee","that","the","saved","checkpoint","actually","survives","deletion","."]
code: def save_and_keep_only(
      self,
      meta={},
      end_of_epoch=True,
      name=None,
      num_to_keep=1,
      keep_recent=True,
      importance_keys=[],
      max_keys=[],
      min_keys=[],
      ckpt_predicate=None,
      verbosity=logging.INFO,
  ):
      """Saves a checkpoint, then deletes the least important checkpoints.
  
      Essentially this combines ``save_checkpoint()`` and
      ``delete_checkpoints()`` in one call, providing short syntax.
  
      Arguments
      ---------
      meta : mapping, optional
          A mapping which is added to the meta file in the checkpoint. The
          key "unixtime" is included by default.
      end_of_epoch : bool, optional
          Whether the checkpoint is at the end of an epoch. True by default.
          May affect loading.
      name : str, optional
          Specify a custom name for your checkpoint.
          The name will still have a prefix added. If no name is given,
          a name is created from a timestamp and a random unique id.
      num_to_keep : int, optional
          Number of checkpoints to keep. Defaults to 1. This deletes all
          checkpoints remaining after filtering. Must be >=0.
      keep_recent : bool, optional
          Whether to keep the most recent ``num_to_keep`` checkpoints.
      importance_keys : list, optional
          A list of key functions used in sorting (see the sorted built-in).
          Each callable defines a sort order and num_to_keep checkpoints are
          kept for callable. The checkpoint with the highest keys are kept.
          The functions are passed Checkpoint namedtuples (see above).
      max_keys : list, optional
          A list of keys for which the *highest* value will be kept.
      min_keys : list, optional
          A list of keys for which the *lowest* value will be kept.
      ckpt_predicate : callable, optional
          Use this to exclude some checkpoints from deletion. Before any
          sorting, the list of checkpoints is filtered with this predicate.
          Only the checkpoints for which ckpt_predicate is True can be
          deleted. The function is called with Checkpoint namedtuples
          (see above).
  
      Returns
      -------
      None
          Unlike save_checkpoint, this does not return anything, since
          we cannot guarantee that the saved checkpoint actually survives
          deletion.
      """
      self.save_checkpoint(
          meta=meta,
          end_of_epoch=end_of_epoch,
          name=name,
          verbosity=verbosity,
      )
  
      if keep_recent:
          importance_keys.append(ckpt_recency)
      self.delete_checkpoints(
          num_to_keep=num_to_keep,
          max_keys=max_keys,
          min_keys=min_keys,
          importance_keys=importance_keys,
          ckpt_predicate=ckpt_predicate,
          verbosity=verbosity,
      )
  
imports: import torch
  import collections
  import collections.abc
  import os
  import time
  import yaml
  import pathlib
  import inspect
  import shutil
  import logging
  import warnings
  from packaging import version
  import speechbrain.utils._workarounds
  from speechbrain.utils.distributed import main_process_only, if_main_process, ddp_barrier
label_desc: Use image node_id 5 for calling the Checkpointer obj's underlying member method code with example usage: obj.save_and_keep_only(meta, end_of_epoch, name, num_to_keep, keep_recent, importance_keys, max_keys, min_keys, ckpt_predicate, verbosity) without return types