code: def _retrieve_checkpoint_dirpaths(
      dirpath: str,
      metadata_fname: Optional[str],
  ) -> List[str]:
      """
      Given a parent directory where checkpoints are saved, return the unsorted checkpoint subdirectories
  
      Args:
          dirpath: parent directory where checkpoints are saved.
          metadata_fname: Checks if metadata file is present in checkpoint, disregards if it does not exist.
      """
  
      if dirpath[-1] == "/":
          # removes trailing forward slash if present
          # required for regex search to work
          dirpath = dirpath[:-1]
  
      fs = get_filesystem(dirpath)
  
      if not fs.exists(dirpath):
          logger.warning(f"Input dirpath doesn't exist: {dirpath}")
          return []
  
      contents = fs.ls(dirpath, detail=True)
      contents = [
          item["name"]
          for item in contents
          if item["type"] == "directory"
      ]
      if len(contents) == 0:
          logger.warning(
              f"Input dirpath doesn't contain any subdirectories: {dirpath}"
          )
          return []
  
      # Define the regex pattern to match the directory names
      pattern = rf"^{dirpath}/epoch_\d+_step_\d+"
      snapshot_dirpath_pattern: Pattern[str] = re.compile(pattern)
      candidate_dirpaths = list(
          filter(snapshot_dirpath_pattern.match, contents)
      )
  
      if not metadata_fname:
          # return early as we don't need to filter out any paths
          return candidate_dirpaths
  
      # Iterate through all files and directories in the specified directory
      # and check if metedata is present or not
      valid_ckpt_dirpaths = []
      for candidate in candidate_dirpaths:
          if not _metadata_exists(fs, candidate, metadata_fname):
              logger.warning(
                  f"Snapshot metadata is missing from {candidate}! Skipping this path"
              )
              continue
  
          valid_ckpt_dirpaths.append(candidate)
  
      return valid_ckpt_dirpaths
  
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 5 for calling a global function with example usage: _retrieve_checkpoint_dirpaths(dirpath, metadata_fname) and returns: valid_ckpt_dirpaths, list, list, candidate_dirpaths