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: Picks multiple checkpoints.
  
  If none of ``importance_key``, ``max_key``, and ``min_key`` is
  used, then the most recent checkpoints will be returned. No more than
  one of these may be used.
  
  Arguments
  ---------
  importance_key : callable, optional
      The key function used in sorting.
      The checkpoint with the highest returned value is picked.
      The function is called with Checkpoint namedtuples.
  max_key : str, optional
      The checkpoint with the highest value for this key will
      be returned. Only checkpoints with this key will be considered!
  min_key : str, optional
      The checkpoint with the lowest value for this key will
      be returned. Only checkpoints with this key will be considered!
  ckpt_predicate : callable, optional
      Before sorting, the list of
      checkpoints is filtered with this predicate.
      See the filter builtin.
      The function is called with Checkpoint namedtuples (see above).
      By default, all checkpoints are considered.
  max_num_checkpoints : int, None
      The maximum number of checkpoints to return, or None to return all
      found checkpoints.
  
  Returns
  -------
  list
      List containing at most the max specified number of Checkpoints.
docstr_tok: ["Picks","multiple","checkpoints",".","If","none","of","``","importance_key","``",",","``","max_key","``",",","and","``","min_key","``","is","used",",","then","the","most","recent","checkpoints","will","be","returned",".","No","more","than","one","of","these","may","be","used",".","Arguments","--","--","--","--","-","importance_key",":","callable",",","optional","The","key","function","used","in","sorting",".","The","checkpoint","with","the","highest","returned","value","is","picked",".","The","function","is","called","with","Checkpoint","namedtuples",".","max_key",":","str",",","optional","The","checkpoint","with","the","highest","value","for","this","key","will","be","returned",".","Only","checkpoints","with","this","key","will","be","considered","!","min_key",":","str",",","optional","The","checkpoint","with","the","lowest","value","for","this","key","will","be","returned",".","Only","checkpoints","with","this","key","will","be","considered","!","ckpt_predicate",":","callable",",","optional","Before","sorting",",","the","list","of","checkpoints","is","filtered","with","this","predicate",".","See","the","filter","builtin",".","The","function","is","called","with","Checkpoint","namedtuples","(","see","above",")",".","By","default",",","all","checkpoints","are","considered",".","max_num_checkpoints",":","int",",","None","The","maximum","number","of","checkpoints","to","return",",","or","None","to","return","all","found","checkpoints",".","Returns","--","--","--","-","list","List","containing","at","most","the","max","specified","number","of","Checkpoints","."]
code: def find_checkpoints(
      self,
      importance_key=None,
      max_key=None,
      min_key=None,
      ckpt_predicate=None,
      max_num_checkpoints=None,
  ):
      """Picks multiple checkpoints.
  
      If none of ``importance_key``, ``max_key``, and ``min_key`` is
      used, then the most recent checkpoints will be returned. No more than
      one of these may be used.
  
      Arguments
      ---------
      importance_key : callable, optional
          The key function used in sorting.
          The checkpoint with the highest returned value is picked.
          The function is called with Checkpoint namedtuples.
      max_key : str, optional
          The checkpoint with the highest value for this key will
          be returned. Only checkpoints with this key will be considered!
      min_key : str, optional
          The checkpoint with the lowest value for this key will
          be returned. Only checkpoints with this key will be considered!
      ckpt_predicate : callable, optional
          Before sorting, the list of
          checkpoints is filtered with this predicate.
          See the filter builtin.
          The function is called with Checkpoint namedtuples (see above).
          By default, all checkpoints are considered.
      max_num_checkpoints : int, None
          The maximum number of checkpoints to return, or None to return all
          found checkpoints.
  
      Returns
      -------
      list
          List containing at most the max specified number of Checkpoints.
  
      """
      if importance_key is None and min_key is None and max_key is None:
          importance_key = ckpt_recency
  
      if max_key and not importance_key:
  
          def importance_key(ckpt):
              "Defines the importance key."
              return ckpt.meta[max_key]
  
          def ckpt_predicate(ckpt, old_predicate=ckpt_predicate):
              "Checkpoints predicate."
              if old_predicate is not None:
                  return max_key in ckpt.meta and old_predicate(ckpt)
              else:
                  return max_key in ckpt.meta
  
      elif min_key and not importance_key:
  
          def importance_key(ckpt):
              "Defines the importance key."
              return -ckpt.meta[min_key]
  
          def ckpt_predicate(ckpt, old_predicate=ckpt_predicate):
              "Checkpoints predicate."
              if old_predicate is not None:
                  return min_key in ckpt.meta and old_predicate(ckpt)
              else:
                  return min_key in ckpt.meta
  
      elif min_key or max_key:
          raise ValueError(
              "Must specify only one of 'importance_key', 'max_key', "
              "and 'min_key'."
          )
  
      ckpts = self.list_checkpoints()
      ckpts = list(filter(ckpt_predicate, ckpts))
      # First sort by recency, so that importance being equal,
      # the most checkpoints are returned
      ckpts = sorted(ckpts, key=ckpt_recency, reverse=True)
      if ckpts:
          ranked_ckpts = sorted(ckpts, key=importance_key, reverse=True)
          # NOTE: apparently, you can also slice [:None],
          # and this is the same as [:], so the following if-else is not
          # strictly speaking needed. However, this feature does not seem to
          # be documented Python so I don't want to trust it.
          if max_num_checkpoints is not None:
              return ranked_ckpts[:max_num_checkpoints]
          else:  # No max number -> return all ckpts, but just sorted
              return ranked_ckpts
      else:
          return []
  
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 7 for calling the Checkpointer obj's underlying member method code with example usage: obj.find_checkpoints(importance_key, max_key, min_key, ckpt_predicate, max_num_checkpoints) and returns: list, ckpt, ranked_ckpts, ranked_ckpts, unknown, unknown, unknown, unknown, unknown