code: def flush_stale_checkpoints(
      checkpoint_path: Path, keep_last: tp.Optional[int] = None
  ) -> None:
      """Flush checkpoints to only keep last N checkpoints."""
      if keep_last is None or keep_last <= 0:
          return
      checkpoint_dir = checkpoint_path.parent
      suffix = ""
      if flashy.distrib.rank() > 0:
          suffix = f".{flashy.distrib.rank()}"
      checkpoint_files_with_epoch = []
      for path in Path(checkpoint_dir).glob(f"checkpoint_*.th{suffix}"):
          epoch_part = path.name.split(".", 1)[0].split("_", 1)[1]
          if epoch_part.isdigit():
              checkpoint_files_with_epoch.append(
                  (path, int(epoch_part))
              )
      checkpoint_files = [
          path
          for path, _ in list(
              sorted(checkpoint_files_with_epoch, key=lambda t: t[1])
          )
      ]
      total_to_flush = max(0, len(checkpoint_files) - keep_last)
      files_to_flush = checkpoint_files[:total_to_flush]
      for path in files_to_flush:
          logger.debug("Removing checkpoint: %s", str(path))
          path.unlink(missing_ok=True)
  
file_path: audiocraft/audiocraft/utils/checkpoint.py
imports: from enum import Enum
  import logging
  from pathlib import Path
  import re
  import typing
  import flashy
  import torch
  from ..environment import AudioCraftEnvironment
label_desc: Use image node_id 6 for calling a global function with example usage: flush_stale_checkpoints(checkpoint_path, keep_last) without return types