class_docstr: A class representing a Checkpoint instance of a trained model.
  
  A Checkpoint object is usually obtained from
  :func:`determined.experimental.client.get_checkpoint`. This class provides helper functionality
  for downloading checkpoints to local storage and loading checkpoints into memory.
  
  The :class:`~determined.experimental.client.Trial` class contains methods
  that return instances of this class.
  
  Attributes:
      session: HTTP request session.
      uuid: UUID of checkpoint in storage.
      task_id: (Mutable, Optional[str]) ID of associated task.
      allocation_id: (Mutable, Optional[str]) ID of associated allocation.
      report_time: (Mutable, Optional[str]) Timestamp checkpoint reported.
      resources: (Mutable, Optional[Dict]) Dictionary of file paths to file sizes in bytes of
          all files in the checkpoint.
      metadata: (Mutable, Optional[Dict]) User-defined metadata associated with the checkpoint.
      state: (Mutable, Optional[CheckpointState]) State of the checkpoint.
      training: (Mutable, Optional[CheckpointTrainingMetadata]) Training-related metadata for
          the checkpoint.
  
      Note:
          All attributes are cached by default.
  
          Some attributes are mutable and may be changed by methods that update these values,
          either automatically (eg. :meth:`add_metadata()`) or explicitly with :meth:`reload()`.
class_docstr_tok: ["A","class","representing","a","Checkpoint","instance","of","a","trained","model",".","A","Checkpoint","object","is","usually","obtained","from",":","func",":","`","determined.experimental.client.get_checkpoint","`",".","This","class","provides","helper","functionality","for","downloading","checkpoints","to","local","storage","and","loading","checkpoints","into","memory",".","The",":","class",":","`","~determined.experimental.client.Trial","`","class","contains","methods","that","return","instances","of","this","class",".","Attributes",":","session",":","HTTP","request","session",".","uuid",":","UUID","of","checkpoint","in","storage",".","task_id",":","(","Mutable",",","Optional","[","str","]",")","ID","of","associated","task",".","allocation_id",":","(","Mutable",",","Optional","[","str","]",")","ID","of","associated","allocation",".","report_time",":","(","Mutable",",","Optional","[","str","]",")","Timestamp","checkpoint","reported",".","resources",":","(","Mutable",",","Optional","[","Dict","]",")","Dictionary","of","file","paths","to","file","sizes","in","bytes","of","all","files","in","the","checkpoint",".","metadata",":","(","Mutable",",","Optional","[","Dict","]",")","User-defined","metadata","associated","with","the","checkpoint",".","state",":","(","Mutable",",","Optional","[","CheckpointState","]",")","State","of","the","checkpoint",".","training",":","(","Mutable",",","Optional","[","CheckpointTrainingMetadata","]",")","Training-related","metadata","for","the","checkpoint",".","Note",":","All","attributes","are","cached","by","default",".","Some","attributes","are","mutable","and","may","be","changed","by","methods","that","update","these","values",",","either","automatically","(","eg",".",":","meth",":","`","add_metadata","(",")","`",")","or","explicitly","with",":","meth",":","`","reload","(",")","`","."]
docstr: Download checkpoint to local storage.
  
  See also:
  
    - :func:`determined.pytorch.load_trial_from_checkpoint_path`
    - :func:`determined.keras.load_model_from_checkpoint_path`
  
  Arguments:
      path (string, optional): Top level directory to place the
          checkpoint under. If this parameter is not set, the checkpoint will
          be downloaded to ``checkpoints/<checkpoint_uuid>`` relative to the
          current working directory.
      mode (DownloadMode, optional): Governs how a checkpoint is downloaded. Defaults to
          ``AUTO``.
docstr_tok: ["Download","checkpoint","to","local","storage",".","See","also",":","-",":","func",":","`","determined.pytorch.load_trial_from_checkpoint_path","`","-",":","func",":","`","determined.keras.load_model_from_checkpoint_path","`","Arguments",":","path","(","string",",","optional",")",":","Top","level","directory","to","place","the","checkpoint","under",".","If","this","parameter","is","not","set",",","the","checkpoint","will","be","downloaded","to","``","checkpoints\/","<","checkpoint_uuid",">","``","relative","to","the","current","working","directory",".","mode","(","DownloadMode",",","optional",")",":","Governs","how","a","checkpoint","is","downloaded",".","Defaults","to","``","AUTO","``","."]
code: def download(
      self,
      path: Optional[str] = None,
      mode: DownloadMode = DownloadMode.AUTO,
  ) -> str:
      """
      Download checkpoint to local storage.
  
      See also:
  
        - :func:`determined.pytorch.load_trial_from_checkpoint_path`
        - :func:`determined.keras.load_model_from_checkpoint_path`
  
      Arguments:
          path (string, optional): Top level directory to place the
              checkpoint under. If this parameter is not set, the checkpoint will
              be downloaded to ``checkpoints/<checkpoint_uuid>`` relative to the
              current working directory.
          mode (DownloadMode, optional): Governs how a checkpoint is downloaded. Defaults to
              ``AUTO``.
      """
      if self.state not in [
          CheckpointState.COMPLETED,
          CheckpointState.PARTIALLY_DELETED,
      ]:
          if self.state is None:
              raise ValueError(
                  "Checkpoint state is unknown. Please call Checkpoint.reload to refresh."
              )
          raise errors.CheckpointStateException(
              "Only COMPLETED or PARTIALLY_DELETED checkpoints can be downloaded. "
              f"Checkpoint state: {self.state.value}"
          )
      if path is not None:
          local_ckpt_dir = pathlib.Path(path)
      else:
          local_ckpt_dir = pathlib.Path("checkpoints", self.uuid)
  
      # Backward compatibility: we used MLflow's MLmodel checkpoint format for
      # serializing pytorch models. We now use our own format that contains a
      # metadata.json file. We are checking for checkpoint existence by
      # looking for both checkpoint formats in the output directory.
      potential_metadata_paths = [
          local_ckpt_dir.joinpath(f)
          for f in ["metadata.json", "MLmodel"]
      ]
      if not any(p.exists() for p in potential_metadata_paths):
          # If the target directory doesn't already appear to contain a
          # checkpoint, attempt to fetch one.
          if self.training is None:
              raise NotImplementedError(
                  "Non-training checkpoints cannot be downloaded"
              )
  
          checkpoint_storage = self.training.experiment_config[
              "checkpoint_storage"
          ]
          if mode == DownloadMode.DIRECT:
              self._download_direct(checkpoint_storage, local_ckpt_dir)
  
          elif mode == DownloadMode.MASTER:
              self._download_via_master(
                  self._session, self.uuid, local_ckpt_dir
              )
  
          elif mode == DownloadMode.AUTO:
              self._download_auto(checkpoint_storage, local_ckpt_dir)
  
          else:
              raise ValueError(f"Unknown download mode {mode}")
  
      # As of v0.18.0, we write metadata.json once at upload time.  Checkpoints uploaded prior to
      # 0.18.0 will not have a metadata.json present.  Unfortunately, checkpoints earlier than
      # 0.17.7 depended on this file existing in order to be loaded.  Therefore, when we detect
      # that the metadata.json file is not present, we write it to make sure those checkpoints can
      # still load.
      metadata_path = local_ckpt_dir.joinpath("metadata.json")
      if not metadata_path.exists():
          self.write_metadata_file(str(metadata_path))
  
      return str(local_ckpt_dir)
  
imports: import dataclasses
  import enum
  import json
  import logging
  import pathlib
  import shutil
  import sys
  import tarfile
  import warnings
  from typing import Any, Dict, Iterable, List, Optional
  from determined import errors
  from determined.common import api, constants, storage
  from determined.common.api import bindings
  from determined.common.experimental import metrics
  from determined.common.storage import shared
label_desc: Use image node_id 3 for calling the Checkpoint obj's underlying member method code with example usage: obj.download(path, mode) and returns: str