class_docstr: ModelCheckpoint handler, inherits from :class:`~ignite.handlers.checkpoint.Checkpoint`, can be used
  to periodically save objects to disk only. If needed to store checkpoints to
  another storage type, please consider :class:`~ignite.handlers.checkpoint.Checkpoint`.
  It also provides `last_checkpoint` attribute to show the last saved checkpoint.
  
  This handler expects two arguments:
  
      - an :class:`~ignite.engine.engine.Engine` object
      - a `dict` mapping names (`str`) to objects that should be saved to disk.
  
  See Examples for further details.
  
  .. warning::
  
      Behaviour of this class has been changed since v0.3.0.
  
      There is no more internal counter that has been used to indicate the number of save actions. User could
      see its value `step_number` in the filename, e.g. `{filename_prefix}_{name}_{step_number}.pt`. Actually,
      `step_number` is replaced by current engine's epoch if `score_function` is specified and current iteration
      otherwise.
  
      A single `pt` file is created instead of multiple files.
  
  Args:
      dirname: Directory path where objects will be saved.
      filename_prefix: Prefix for the file names to which objects will be saved. See Notes of
          :class:`~ignite.handlers.checkpoint.Checkpoint` for more details.
      score_function: if not None, it should be a function taking a single argument, an
          :class:`~ignite.engine.engine.Engine` object, and return a score (`float`). Objects with highest scores
          will be retained.
      score_name: if ``score_function`` not None, it is possible to store its value using
          `score_name`. See Examples of :class:`~ignite.handlers.checkpoint.Checkpoint` for more details.
      n_saved: Number of objects that should be kept on disk. Older files will be removed. If set to
          `None`, all objects are kept.
      atomic: If True, objects are serialized to a temporary file, and then moved to final
          destination, so that files are guaranteed to not be damaged (for example if exception
          occurs during saving).
      require_empty: If True, will raise exception if there are any files starting with
          ``filename_prefix`` in the directory ``dirname``.
      create_dir: If True, will create directory ``dirname`` if it does not exist.
      global_step_transform: global step transform function to output a desired global step.
          Input of the function is `(engine, event_name)`. Output of function should be an integer.
          Default is None, global_step based on attached engine. If provided, uses function output as global_step.
          To setup global step from another engine, please use :meth:`~ignite.handlers.global_step_from_engine`.
      filename_pattern: If ``filename_pattern`` is provided, this pattern will be used to render
          checkpoint filenames. If the pattern is not defined, the default pattern would be used.
          See :class:`~ignite.handlers.checkpoint.Checkpoint` for details.
      include_self: Whether to include the `state_dict` of this object in the checkpoint. If `True`, then
          there must not be another object in ``to_save`` with key ``checkpointer``.
      greater_or_equal: if `True`, the latest equally scored model is stored. Otherwise, the first model.
          Default, `False`.
      save_on_rank: Which rank to save the objects on, in the distributed configuration. Used to
          instantiate a :class:`~ignite.handlers.DiskSaver` and is also passed to the parent class.
      kwargs: Accepted keyword arguments for `torch.save` or `xm.save` in `DiskSaver`.
  
  .. versionchanged:: 0.4.2
      Accept ``kwargs`` for `torch.save` or `xm.save`
  
  .. versionchanged:: 0.4.9
      Accept ``filename_pattern`` and ``greater_or_equal`` for parity
      with :class:`~ignite.handlers.checkpoint.Checkpoint`
  
  .. versionchanged:: 0.4.10
      Added `save_on_rank` arg to save objects on this rank in a distributed configuration
  
  Examples:
      .. testcode:: python
  
          import os
          from ignite.engine import Engine, Events
          from ignite.handlers import ModelCheckpoint
          from torch import nn
          trainer = Engine(lambda engine, batch: None)
          handler = ModelCheckpoint('/tmp/models', 'myprefix', n_saved=2, create_dir=True, require_empty=False)
          model = nn.Linear(3, 3)
          trainer.add_event_handler(Events.EPOCH_COMPLETED(every=2), handler, {'mymodel': model})
          trainer.run([0, 1, 2, 3, 4], max_epochs=6)
          print(sorted(os.listdir('/tmp/models')))
          print(handler.last_checkpoint)
  
      .. testoutput:: python
  
          ['myprefix_mymodel_20.pt', 'myprefix_mymodel_30.pt']
          /tmp/models/myprefix_mymodel_30.pt
class_docstr_tok: ["ModelCheckpoint","handler",",","inherits","from",":","class",":","`","~ignite.handlers.checkpoint.Checkpoint","`",",","can","be","used","to","periodically","save","objects","to","disk","only",".","If","needed","to","store","checkpoints","to","another","storage","type",",","please","consider",":","class",":","`","~ignite.handlers.checkpoint.Checkpoint","`",".","It","also","provides","`","last_checkpoint","`","attribute","to","show","the","last","saved","checkpoint",".","This","handler","expects","two","arguments",":","-","an",":","class",":","`","~ignite.engine.engine.Engine","`","object","-","a","`","dict","`","mapping","names","(","`","str","`",")","to","objects","that","should","be","saved","to","disk",".","See","Examples","for","further","details",".","..","warning",":",":","Behaviour","of","this","class","has","been","changed","since","v0.3.0",".","There","is","no","more","internal","counter","that","has","been","used","to","indicate","the","number","of","save","actions",".","User","could","see","its","value","`","step_number","`","in","the","filename",",","e.g",".","`","{","filename_prefix","}","_","{","name","}","_","{","step_number","}",".pt","`",".","Actually",",","`","step_number","`","is","replaced","by","current","engine","'s","epoch","if","`","score_function","`","is","specified","and","current","iteration","otherwise",".","A","single","`","pt","`","file","is","created","instead","of","multiple","files",".","Args",":","dirname",":","Directory","path","where","objects","will","be","saved",".","filename_prefix",":","Prefix","for","the","file","names","to","which","objects","will","be","saved",".","See","Notes","of",":","class",":","`","~ignite.handlers.checkpoint.Checkpoint","`","for","more","details",".","score_function",":","if","not","None",",","it","should","be","a","function","taking","a","single","argument",",","an",":","class",":","`","~ignite.engine.engine.Engine","`","object",",","and","return","a","score","(","`","float","`",")",".","Objects","with","highest","scores","will","be","retained",".","score_name",":","if","``","score_function","``","not","None",",","it","is","possible","to","store","its","value","using","`","score_name","`",".","See","Examples","of",":","class",":","`","~ignite.handlers.checkpoint.Checkpoint","`","for","more","details",".","n_saved",":","Number","of","objects","that","should","be","kept","on","disk",".","Older","files","will","be","removed",".","If","set","to","`","None","`",",","all","objects","are","kept",".","atomic",":","If","True",",","objects","are","serialized","to","a","temporary","file",",","and","then","moved","to","final","destination",",","so","that","files","are","guaranteed","to","not","be","damaged","(","for","example","if","exception","occurs","during","saving",")",".","require_empty",":","If","True",",","will","raise","exception","if","there","are","any","files","starting","with","``","filename_prefix","``","in","the","directory","``","dirname","``",".","create_dir",":","If","True",",","will","create","directory","``","dirname","``","if","it","does","not","exist",".","global_step_transform",":","global","step","transform","function","to","output","a","desired","global","step",".","Input","of","the","function","is","`","(","engine",",","event_name",")","`",".","Output","of","function","should","be","an","integer",".","Default","is","None",",","global_step","based","on","attached","engine",".","If","provided",",","uses","function","output","as","global_step",".","To","setup","global","step","from","another","engine",",","please","use",":","meth",":","`","~ignite.handlers.global_step_from_engine","`",".","filename_pattern",":","If","``","filename_pattern","``","is","provided",",","this","pattern","will","be","used","to","render","checkpoint","filenames",".","If","the","pattern","is","not","defined",",","the","default","pattern","would","be","used",".","See",":","class",":","`","~ignite.handlers.checkpoint.Checkpoint","`","for","details",".","include_self",":","Whether","to","include","the","`","state_dict","`","of","this","object","in","the","checkpoint",".","If","`","True","`",",","then","there","must","not","be","another","object","in","``","to_save","``","with","key","``","checkpointer","``",".","greater_or_equal",":","if","`","True","`",",","the","latest","equally","scored","model","is","stored",".","Otherwise",",","the","first","model",".","Default",",","`","False","`",".","save_on_rank",":","Which","rank","to","save","the","objects","on",",","in","the","distributed","configuration",".","Used","to","instantiate","a",":","class",":","`","~ignite.handlers.DiskSaver","`","and","is","also","passed","to","the","parent","class",".","kwargs",":","Accepted","keyword","arguments","for","`","torch.save","`","or","`","xm.save","`","in","`","DiskSaver","`",".","..","versionchanged",":",":","0.4.2","Accept","``","kwargs","``","for","`","torch.save","`","or","`","xm.save","`","..","versionchanged",":",":","0.4.9","Accept","``","filename_pattern","``","and","``","greater_or_equal","``","for","parity","with",":","class",":","`","~ignite.handlers.checkpoint.Checkpoint","`","..","versionchanged",":",":","0.4.10","Added","`","save_on_rank","`","arg","to","save","objects","on","this","rank","in","a","distributed","configuration","Examples",":","..","testcode",":",":","python","import","os","from","ignite.engine","import","Engine",",","Events","from","ignite.handlers","import","ModelCheckpoint","from","torch","import","nn","trainer","=","Engine","(","lambda","engine",",","batch",":","None",")","handler","=","ModelCheckpoint","(","'\/tmp\/models","'",",","'myprefix","'",",","n_saved=2",",","create_dir=True",",","require_empty=False",")","model","=","nn.Linear","(","3",",","3",")","trainer.add_event_handler","(","Events.EPOCH_COMPLETED","(","every=2",")",",","handler",",","{","'mymodel","'",":","model","}",")","trainer.run","(","[","0",",","1",",","2",",","3",",","4","]",",","max_epochs=6",")","print","(","sorted","(","os.listdir","(","'\/tmp\/models","'",")",")",")","print","(","handler.last_checkpoint",")","..","testoutput",":",":","python","[","'myprefix_mymodel_20.pt","'",",","'myprefix_mymodel_30.pt","'","]","\/tmp\/models\/myprefix_mymodel_30.pt"]
code: def last_checkpoint(self) -> Optional[Union[str, Path]]:
      if len(self._saved) < 1:
          return None
  
      if not isinstance(self.save_handler, DiskSaver):
          raise RuntimeError(
              f"Internal error, save_handler should be DiskSaver, but has {type(self.save_handler)}."
          )
  
      return self.save_handler.dirname / self._saved[-1].filename
  
imports: import collections.abc
  import numbers
  import os
  import stat
  import tempfile
  from abc import ABCMeta, abstractmethod
  from collections import OrderedDict
  from pathlib import Path
  from typing import Any, Callable, cast, Dict, List, Mapping, NamedTuple, Optional, Union
  import torch
  import torch.nn
  from packaging.version import Version
  import ignite.distributed
  from ignite.base import Serializable
  from ignite.engine import Engine, Events
  from ignite.utils import _tree_apply2, _tree_map
label_desc: Use image node_id 2 for calling the ModelCheckpoint obj's underlying member method code with example usage: obj.last_checkpoint() and returns: unknown, None