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: Register a recoverable with possible custom hooks.
  
  Arguments
  ---------
  name : str
      Unique name for recoverable. Used to map savefiles to objects.
  obj : instance
      The object to recover.
  custom_load_hook : callable, optional
      Called to load the object's savefile. The function/method must be
      callable with signature (instance, path) using positional
      arguments. This is satisfied by for example: def load(self, path):
  custom_save_hook : callable, optional
      Called to save the object's parameters. The function/method must
      be callable with signature (instance, path) using positional
      arguments. This is satisfied by for example: def saver(self, path):
docstr_tok: ["Register","a","recoverable","with","possible","custom","hooks",".","Arguments","--","--","--","--","-","name",":","str","Unique","name","for","recoverable",".","Used","to","map","savefiles","to","objects",".","obj",":","instance","The","object","to","recover",".","custom_load_hook",":","callable",",","optional","Called","to","load","the","object","'s","savefile",".","The","function\/method","must","be","callable","with","signature","(","instance",",","path",")","using","positional","arguments",".","This","is","satisfied","by","for","example",":","def","load","(","self",",","path",")",":","custom_save_hook",":","callable",",","optional","Called","to","save","the","object","'s","parameters",".","The","function\/method","must","be","callable","with","signature","(","instance",",","path",")","using","positional","arguments",".","This","is","satisfied","by","for","example",":","def","saver","(","self",",","path",")",":"]
code: def add_recoverable(
      self,
      name,
      obj,
      custom_load_hook=None,
      custom_save_hook=None,
  ):
      """Register a recoverable with possible custom hooks.
  
      Arguments
      ---------
      name : str
          Unique name for recoverable. Used to map savefiles to objects.
      obj : instance
          The object to recover.
      custom_load_hook : callable, optional
          Called to load the object's savefile. The function/method must be
          callable with signature (instance, path) using positional
          arguments. This is satisfied by for example: def load(self, path):
      custom_save_hook : callable, optional
          Called to save the object's parameters. The function/method must
          be callable with signature (instance, path) using positional
          arguments. This is satisfied by for example: def saver(self, path):
      """
      self.recoverables[name] = obj
      if custom_load_hook is not None:
          self.custom_load_hooks[name] = custom_load_hook
      if custom_save_hook is not None:
          self.custom_save_hooks[name] = custom_save_hook
  
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 2 for calling the Checkpointer obj's underlying member method code with example usage: obj.add_recoverable(name, obj, custom_load_hook, custom_save_hook) without return types