code: def _latest_checkpoint_path(
      dirpath: str, metadata_fname: Optional[str]
  ) -> Optional[str]:
      candidate_dirpaths = _retrieve_checkpoint_dirpaths(
          dirpath, metadata_fname
      )
  
      # Initialize variables to store the largest epoch and step numbers
      largest_subdirectory = None
      largest_epoch = -1
      largest_step = -1
  
      # Iterate through all files and directories in the specified directory
      for candidate in candidate_dirpaths:
          # Extract the epoch and step numbers from the directory name
          dirname = os.path.basename(candidate)
  
          # dirname will be of the format epoch_N_step_M
          # where N is the epoch number and M is the step number as integers
          split = dirname.split("_")
          if len(split) != 4:
              raise AssertionError(
                  f"Expected exactly 4 elements for pattern of epoch_N_step_M, but received {split})"
              )
  
          epoch_num, step_num = int(split[1]), int(split[3])
          # Check if the current epoch and step numbers are larger than the largest ones found so far
          if epoch_num > largest_epoch:
              largest_epoch = epoch_num
              largest_step = step_num
              largest_subdirectory = dirname
          elif largest_epoch == epoch_num and step_num > largest_step:
              largest_step = step_num
              largest_subdirectory = dirname
  
      if largest_subdirectory is None:
          return None
  
      # Rejoin with the parent directory path and return the largest subdirectory
      return os.path.join(dirpath, none_throws(largest_subdirectory))
  
imports: import logging
  import os
  import re
  from typing import Any, Callable, cast, Dict, List, Optional, Pattern, Tuple, TypeVar
  import fsspec
  from pyre_extensions import none_throws
  from torch import distributed
  from torchtnt.framework.callbacks.checkpointer_types import RestoreOptions
  from torchtnt.framework.state import State
  from torchtnt.framework.unit import AppStateMixin
  from torchtnt.utils.distributed import get_global_rank, PGWrapper
  from torchtnt.utils.fsspec import get_filesystem
  from torchtnt.utils.stateful import Stateful
label_desc: Use image node_id 3 for calling a global function with example usage: _latest_checkpoint_path(dirpath, metadata_fname) and returns: os, None